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 "HTTPHeaderNames.h"
33#include "HTTPParsers.h"
34#include "MIMETypeRegistry.h"
35#include "MemoryCache.h"
36#include "ResourceBuffer.h"
37#include "RuntimeApplicationChecks.h"
38#include "TextResourceDecoder.h"
39#include <wtf/Vector.h>
40
41namespace WebCore {
42
43CachedScript::CachedScript(const ResourceRequest& resourceRequest, const String& charset, SessionID sessionID)
44    : CachedResource(resourceRequest, Script, sessionID)
45    , m_decoder(TextResourceDecoder::create(ASCIILiteral("application/javascript"), charset))
46{
47    // It's javascript we want.
48    // But some websites think their scripts are <some wrong mimetype here>
49    // and refuse to serve them if we only accept application/x-javascript.
50    setAccept("*/*");
51}
52
53CachedScript::~CachedScript()
54{
55}
56
57void CachedScript::setEncoding(const String& chs)
58{
59    m_decoder->setEncoding(chs, TextResourceDecoder::EncodingFromHTTPHeader);
60}
61
62String CachedScript::encoding() const
63{
64    return m_decoder->encoding().name();
65}
66
67String CachedScript::mimeType() const
68{
69    return extractMIMETypeFromMediaType(m_response.httpHeaderField(HTTPHeaderName::ContentType)).lower();
70}
71
72const String& CachedScript::script()
73{
74    ASSERT(!isPurgeable());
75
76    if (!m_script && m_data) {
77        m_script = m_decoder->decodeAndFlush(m_data->data(), encodedSize());
78        setDecodedSize(m_script.sizeInBytes());
79    }
80    m_decodedDataDeletionTimer.restart();
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(HTTPHeaderName::XContentTypeOptions)) == 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