1 /* 2 Copyright (C) 1998 Lars Knoll (knoll (at) mpi-hd.mpg.de) 3 Copyright (C) 2001 Dirk Mueller (mueller (at) kde.org) 4 Copyright (C) 2002 Waldo Bastian (bastian (at) kde.org) 5 Copyright (C) 2006 Samuel Weinig (sam.weinig (at) 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 "core/fetch/ScriptResource.h" 29 30 #include "core/fetch/TextResourceDecoder.h" 31 #include "platform/MIMETypeRegistry.h" 32 #include "platform/SharedBuffer.h" 33 #include "platform/network/HTTPParsers.h" 34 35 namespace WebCore { 36 37 ScriptResource::ScriptResource(const ResourceRequest& resourceRequest, const String& charset) 38 : Resource(resourceRequest, Script) 39 , m_decoder(TextResourceDecoder::create("application/javascript", charset)) 40 { 41 DEFINE_STATIC_LOCAL(const AtomicString, acceptScript, ("*/*", AtomicString::ConstructFromLiteral)); 42 43 // It's javascript we want. 44 // But some websites think their scripts are <some wrong mimetype here> 45 // and refuse to serve them if we only accept application/x-javascript. 46 setAccept(acceptScript); 47 } 48 49 ScriptResource::~ScriptResource() 50 { 51 } 52 53 void ScriptResource::setEncoding(const String& chs) 54 { 55 m_decoder->setEncoding(chs, TextResourceDecoder::EncodingFromHTTPHeader); 56 } 57 58 String ScriptResource::encoding() const 59 { 60 return m_decoder->encoding().name(); 61 } 62 63 AtomicString ScriptResource::mimeType() const 64 { 65 return extractMIMETypeFromMediaType(m_response.httpHeaderField("Content-Type")).lower(); 66 } 67 68 const String& ScriptResource::script() 69 { 70 ASSERT(!isPurgeable()); 71 ASSERT(isLoaded()); 72 73 if (!m_script && m_data) { 74 String script = m_decoder->decode(m_data->data(), encodedSize()); 75 script.append(m_decoder->flush()); 76 m_data.clear(); 77 // We lie a it here and claim that script counts as encoded data (even though it's really decoded data). 78 // That's because the MemoryCache thinks that it can clear out decoded data by calling destroyDecodedData(), 79 // but we can't destroy script in destroyDecodedData because that's our only copy of the data! 80 setEncodedSize(script.sizeInBytes()); 81 m_script = script; 82 } 83 84 return m_script.string(); 85 } 86 87 bool ScriptResource::mimeTypeAllowedByNosniff() const 88 { 89 return parseContentTypeOptionsHeader(m_response.httpHeaderField("X-Content-Type-Options")) != ContentTypeOptionsNosniff || MIMETypeRegistry::isSupportedJavaScriptMIMEType(mimeType()); 90 } 91 92 } // namespace WebCore 93