1/*
2    Copyright (C) 1998 Lars Knoll (knoll@mpi-hd.mpg.de)
3    Copyright (C) 2001 Dirk Mueller (mueller@kde.org)
4    Copyright (C) 2002 Waldo Bastian (bastian@kde.org)
5    Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com)
6    Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
7
8    This library is free software; you can redistribute it and/or
9    modify it under the terms of the GNU Library General Public
10    License as published by the Free Software Foundation; either
11    version 2 of the License, or (at your option) any later version.
12
13    This library is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    Library General Public License for more details.
17
18    You should have received a copy of the GNU Library General Public License
19    along with this library; see the file COPYING.LIB.  If not, write to
20    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21    Boston, MA 02110-1301, USA.
22
23    This class provides all functionality needed for loading images, style sheets and html
24    pages from the web. It has a memory cache for these objects.
25*/
26
27#include "config.h"
28#include "CachedScript.h"
29
30#include "CachedResourceClient.h"
31#include "CachedResourceClientWalker.h"
32#include "HTTPParsers.h"
33#include "MIMETypeRegistry.h"
34#include "MemoryCache.h"
35#include "ResourceBuffer.h"
36#include "RuntimeApplicationChecks.h"
37#include "TextResourceDecoder.h"
38#include <wtf/Vector.h>
39
40namespace WebCore {
41
42CachedScript::CachedScript(const ResourceRequest& resourceRequest, const String& charset)
43    : CachedResource(resourceRequest, Script)
44    , m_decoder(TextResourceDecoder::create("application/javascript", charset))
45{
46    // It's javascript we want.
47    // But some websites think their scripts are <some wrong mimetype here>
48    // and refuse to serve them if we only accept application/x-javascript.
49    setAccept("*/*");
50}
51
52CachedScript::~CachedScript()
53{
54}
55
56void CachedScript::setEncoding(const String& chs)
57{
58    m_decoder->setEncoding(chs, TextResourceDecoder::EncodingFromHTTPHeader);
59}
60
61String CachedScript::encoding() const
62{
63    return m_decoder->encoding().name();
64}
65
66String CachedScript::mimeType() const
67{
68    return extractMIMETypeFromMediaType(m_response.httpHeaderField("Content-Type")).lower();
69}
70
71const String& CachedScript::script()
72{
73    ASSERT(!isPurgeable());
74
75    if (!m_script && m_data) {
76        m_script = m_decoder->decode(m_data->data(), encodedSize());
77        m_script.append(m_decoder->flush());
78        setDecodedSize(m_script.sizeInBytes());
79    }
80    m_decodedDataDeletionTimer.startOneShot(0);
81
82    return m_script;
83}
84
85void CachedScript::finishLoading(ResourceBuffer* data)
86{
87    m_data = data;
88    setEncodedSize(m_data.get() ? m_data->size() : 0);
89    CachedResource::finishLoading(data);
90}
91
92void CachedScript::destroyDecodedData()
93{
94    m_script = String();
95    setDecodedSize(0);
96    if (!MemoryCache::shouldMakeResourcePurgeableOnEviction() && isSafeToMakePurgeable())
97        makePurgeable(true);
98}
99
100#if ENABLE(NOSNIFF)
101bool CachedScript::mimeTypeAllowedByNosniff() const
102{
103    return !parseContentTypeOptionsHeader(m_response.httpHeaderField("X-Content-Type-Options")) == ContentTypeOptionsNosniff || MIMETypeRegistry::isSupportedJavaScriptMIMEType(mimeType());
104}
105#endif
106
107bool CachedScript::shouldIgnoreHTTPStatusCodeErrors() const
108{
109    // This is a workaround for <rdar://problem/13916291>
110    // REGRESSION (r119759): Adobe Flash Player "smaller" installer relies on the incorrect firing
111    // of a load event and needs an app-specific hack for compatibility.
112    // The installer in question tries to load .js file that doesn't exist, causing the server to
113    // return a 404 response. Normally, this would trigger an error event to be dispatched, but the
114    // installer expects a load event instead so we work around it here.
115    if (applicationIsSolidStateNetworksDownloader())
116        return true;
117
118    return CachedResource::shouldIgnoreHTTPStatusCodeErrors();
119}
120
121} // namespace WebCore
122