2 * Copyright (C) 2006, 2007 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 COMPUTER, 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 COMPUTER, 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 "HTTPParsers.h"
32 #include "MIMETypeRegistry.h"
33 #include <CFNetwork/CFURLResponsePriv.h>
34 #include <wtf/RetainPtr.h>
38 // We would like a better value for a maximum time_t,
39 // but there is no way to do that in C with any certainty.
40 // INT_MAX should work well enough for our purposes.
41 #define MAX_TIME_T ((time_t)INT_MAX)
45 static CFStringRef const commonHeaderFields[] = {
46 CFSTR("Age"), CFSTR("Cache-Control"), CFSTR("Content-Type"), CFSTR("Date"), CFSTR("Etag"), CFSTR("Expires"), CFSTR("Last-Modified"), CFSTR("Pragma")
48 static const int numCommonHeaderFields = sizeof(commonHeaderFields) / sizeof(CFStringRef);
50 CFURLResponseRef ResourceResponse::cfURLResponse() const
52 if (!m_cfResponse && !m_isNull) {
53 RetainPtr<CFURLRef> url(AdoptCF, m_url.createCFURL());
54 RetainPtr<CFStringRef> mimeType(AdoptCF, m_mimeType.createCFString());
55 RetainPtr<CFStringRef> textEncodingName(AdoptCF, m_textEncodingName.createCFString());
56 m_cfResponse.adoptCF(CFURLResponseCreate(0, url.get(), mimeType.get(), m_expectedContentLength, textEncodingName.get(), kCFURLCacheStorageAllowed));
59 return m_cfResponse.get();
62 static inline bool filenameHasSaneExtension(const String& filename)
64 int dot = filename.find('.');
66 // The dot can't be the first or last character in the filename.
67 int length = filename.length();
68 return dot > 0 && dot < length - 1;
71 static time_t toTimeT(CFAbsoluteTime time)
73 static const double maxTimeAsDouble = std::numeric_limits<time_t>::max();
74 static const double minTimeAsDouble = std::numeric_limits<time_t>::min();
75 return static_cast<time_t>(min(max(minTimeAsDouble, time + kCFAbsoluteTimeIntervalSince1970), maxTimeAsDouble));
78 void ResourceResponse::platformLazyInit(InitLevel initLevel)
80 if (m_initLevel > initLevel)
83 if (m_isNull || !m_cfResponse.get())
86 if (m_initLevel < CommonFieldsOnly && initLevel >= CommonFieldsOnly) {
87 m_url = CFURLResponseGetURL(m_cfResponse.get());
88 m_mimeType = CFURLResponseGetMIMEType(m_cfResponse.get());
89 m_expectedContentLength = CFURLResponseGetExpectedContentLength(m_cfResponse.get());
90 m_textEncodingName = CFURLResponseGetTextEncodingName(m_cfResponse.get());
92 // Workaround for <rdar://problem/8757088>, can be removed once that is fixed.
93 unsigned textEncodingNameLength = m_textEncodingName.length();
94 if (textEncodingNameLength >= 2 && m_textEncodingName[0U] == '"' && m_textEncodingName[textEncodingNameLength - 1] == '"')
95 m_textEncodingName = m_textEncodingName.substring(1, textEncodingNameLength - 2);
97 m_lastModifiedDate = toTimeT(CFURLResponseGetLastModifiedDate(m_cfResponse.get()));
99 CFHTTPMessageRef httpResponse = CFURLResponseGetHTTPResponse(m_cfResponse.get());
101 m_httpStatusCode = CFHTTPMessageGetResponseStatusCode(httpResponse);
103 RetainPtr<CFDictionaryRef> headers(AdoptCF, CFHTTPMessageCopyAllHeaderFields(httpResponse));
105 for (int i = 0; i < numCommonHeaderFields; i++) {
107 if (CFDictionaryGetValueIfPresent(headers.get(), commonHeaderFields[i], (const void **)&value))
108 m_httpHeaderFields.set(commonHeaderFields[i], value);
111 m_httpStatusCode = 0;
114 if (m_initLevel < CommonAndUncommonFields && initLevel >= CommonAndUncommonFields) {
115 CFHTTPMessageRef httpResponse = CFURLResponseGetHTTPResponse(m_cfResponse.get());
117 RetainPtr<CFStringRef> statusLine(AdoptCF, CFHTTPMessageCopyResponseStatusLine(httpResponse));
118 m_httpStatusText = extractReasonPhraseFromHTTPStatusLine(statusLine.get());
120 RetainPtr<CFDictionaryRef> headers(AdoptCF, CFHTTPMessageCopyAllHeaderFields(httpResponse));
121 CFIndex headerCount = CFDictionaryGetCount(headers.get());
122 Vector<const void*, 128> keys(headerCount);
123 Vector<const void*, 128> values(headerCount);
124 CFDictionaryGetKeysAndValues(headers.get(), keys.data(), values.data());
125 for (int i = 0; i < headerCount; ++i)
126 m_httpHeaderFields.set((CFStringRef)keys[i], (CFStringRef)values[i]);
130 if (m_initLevel < AllFields && initLevel >= AllFields) {
131 RetainPtr<CFStringRef> suggestedFilename(AdoptCF, CFURLResponseCopySuggestedFilename(m_cfResponse.get()));
132 m_suggestedFilename = suggestedFilename.get();
135 m_initLevel = initLevel;
138 bool ResourceResponse::platformCompare(const ResourceResponse& a, const ResourceResponse& b)
140 // CFEqual crashes if you pass it 0 so do an early check before calling it.
141 if (!a.cfURLResponse() || !b.cfURLResponse())
142 return a.cfURLResponse() == b.cfURLResponse();
143 return CFEqual(a.cfURLResponse(), b.cfURLResponse());
147 } // namespace WebCore
149 #endif // USE(CFNETWORK)