2 * Copyright (C) 2006-2007, 2016 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "ResourceResponse.h"
31 #include "CFNetworkSPI.h"
33 #include "HTTPParsers.h"
34 #include "MIMETypeRegistry.h"
35 #include <wtf/RetainPtr.h>
38 #include "WebCoreSystemInterface.h"
43 static CFStringRef const commonHeaderFields[] = {
44 CFSTR("Age"), CFSTR("Cache-Control"), CFSTR("Content-Type"), CFSTR("Date"), CFSTR("Etag"), CFSTR("Expires"), CFSTR("Last-Modified"), CFSTR("Pragma")
47 CFURLResponseRef ResourceResponse::cfURLResponse() const
49 if (!m_cfResponse && !m_isNull) {
53 RetainPtr<CFURLRef> url = m_url.createCFURL();
54 // FIXME: This creates a very incomplete CFURLResponse, which does not even have a status code.
55 m_cfResponse = adoptCF(CFURLResponseCreate(0, url.get(), m_mimeType.string().createCFString().get(), m_expectedContentLength, m_textEncodingName.string().createCFString().get(), kCFURLCacheStorageAllowed));
59 return m_cfResponse.get();
62 static void addToHTTPHeaderMap(const void* key, const void* value, void* context)
64 HTTPHeaderMap* httpHeaderMap = (HTTPHeaderMap*)context;
65 httpHeaderMap->set((CFStringRef)key, (CFStringRef)value);
68 void ResourceResponse::platformLazyInit(InitLevel initLevel)
70 if (m_initLevel > initLevel)
73 if (m_isNull || !m_cfResponse.get())
76 if (m_initLevel < CommonFieldsOnly && initLevel >= CommonFieldsOnly) {
77 m_url = CFURLResponseGetURL(m_cfResponse.get());
78 m_mimeType = CFURLResponseGetMIMEType(m_cfResponse.get());
79 m_expectedContentLength = CFURLResponseGetExpectedContentLength(m_cfResponse.get());
80 m_textEncodingName = CFURLResponseGetTextEncodingName(m_cfResponse.get());
82 // Workaround for <rdar://problem/8757088>, can be removed once that is fixed.
83 unsigned textEncodingNameLength = m_textEncodingName.length();
84 if (textEncodingNameLength >= 2 && m_textEncodingName[0U] == '"' && m_textEncodingName[textEncodingNameLength - 1] == '"')
85 m_textEncodingName = m_textEncodingName.string().substring(1, textEncodingNameLength - 2);
87 CFHTTPMessageRef httpResponse = CFURLResponseGetHTTPResponse(m_cfResponse.get());
89 RetainPtr<CFStringRef> messageString = adoptCF(CFHTTPMessageCopyVersion(httpResponse));
90 m_httpVersion = String(messageString.get()).upper();
91 m_httpStatusCode = CFHTTPMessageGetResponseStatusCode(httpResponse);
93 if (initLevel < AllFields) {
94 RetainPtr<CFDictionaryRef> headers = adoptCF(CFHTTPMessageCopyAllHeaderFields(httpResponse));
95 for (auto& commonHeader : commonHeaderFields) {
97 if (CFDictionaryGetValueIfPresent(headers.get(), commonHeader, (const void **)&value))
98 m_httpHeaderFields.set(commonHeader, value);
102 m_httpStatusCode = 0;
105 if (m_initLevel < AllFields && initLevel == AllFields) {
106 CFHTTPMessageRef httpResponse = CFURLResponseGetHTTPResponse(m_cfResponse.get());
108 RetainPtr<CFStringRef> statusLine = adoptCF(CFHTTPMessageCopyResponseStatusLine(httpResponse));
109 m_httpStatusText = extractReasonPhraseFromHTTPStatusLine(statusLine.get());
111 RetainPtr<CFDictionaryRef> headers = adoptCF(CFHTTPMessageCopyAllHeaderFields(httpResponse));
112 CFDictionaryApplyFunction(headers.get(), addToHTTPHeaderMap, &m_httpHeaderFields);
116 m_initLevel = initLevel;
120 CertificateInfo ResourceResponse::platformCertificateInfo() const
126 String ResourceResponse::platformSuggestedFilename() const
128 if (!cfURLResponse())
130 RetainPtr<CFStringRef> suggestedFilename = adoptCF(CFURLResponseCopySuggestedFilename(cfURLResponse()));
131 return suggestedFilename.get();
134 bool ResourceResponse::platformCompare(const ResourceResponse& a, const ResourceResponse& b)
136 // CFEqual crashes if you pass it 0 so do an early check before calling it.
137 if (!a.cfURLResponse() || !b.cfURLResponse())
138 return a.cfURLResponse() == b.cfURLResponse();
139 return CFEqual(a.cfURLResponse(), b.cfURLResponse());
143 } // namespace WebCore
145 #endif // USE(CFNETWORK)