OSDN Git Service

am 2f7dd354: (-s ours) am 49fd7191: am e17c8aee: DO NOT MERGE: The clip should be...
[android-x86/external-webkit.git] / Source / WebCore / platform / network / cf / ResourceResponseCFNet.cpp
1 /*
2  * Copyright (C) 2006, 2007 Apple Inc.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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. 
24  */
25
26 #include "config.h"
27 #include "ResourceResponse.h"
28
29 #if USE(CFNETWORK)
30
31 #include "HTTPParsers.h"
32 #include "MIMETypeRegistry.h"
33 #include <CFNetwork/CFURLResponsePriv.h>
34 #include <wtf/RetainPtr.h>
35
36 using namespace std;
37
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)    
42
43 namespace WebCore {
44
45 CFURLResponseRef ResourceResponse::cfURLResponse() const
46 {  
47     return m_cfResponse.get();
48 }
49
50 static inline bool filenameHasSaneExtension(const String& filename)
51 {
52     int dot = filename.find('.');
53
54     // The dot can't be the first or last character in the filename.
55     int length = filename.length();
56     return dot > 0 && dot < length - 1;
57 }
58
59 static time_t toTimeT(CFAbsoluteTime time)
60 {
61     static const double maxTimeAsDouble = std::numeric_limits<time_t>::max();
62     static const double minTimeAsDouble = std::numeric_limits<time_t>::min();
63     return min(max(minTimeAsDouble, time + kCFAbsoluteTimeIntervalSince1970), maxTimeAsDouble);
64 }
65
66 void ResourceResponse::platformLazyInit()
67 {
68     if (m_isUpToDate)
69         return;
70     m_isUpToDate = true;
71
72     if (m_isNull) {
73         ASSERT(!m_cfResponse.get());
74         return;
75     }
76
77     // FIXME: We may need to do MIME type sniffing here (unless that is done in CFURLResponseGetMIMEType).
78
79     m_url = CFURLResponseGetURL(m_cfResponse.get());
80     m_mimeType = CFURLResponseGetMIMEType(m_cfResponse.get());
81     m_expectedContentLength = CFURLResponseGetExpectedContentLength(m_cfResponse.get());
82     m_textEncodingName = CFURLResponseGetTextEncodingName(m_cfResponse.get());
83
84     // Workaround for <rdar://problem/8757088>, can be removed once that is fixed.
85     unsigned textEncodingNameLength = m_textEncodingName.length();
86     if (textEncodingNameLength >= 2 && m_textEncodingName[0U] == '"' && m_textEncodingName[textEncodingNameLength - 1] == '"')
87         m_textEncodingName = m_textEncodingName.substring(1, textEncodingNameLength - 2);
88
89     m_lastModifiedDate = toTimeT(CFURLResponseGetLastModifiedDate(m_cfResponse.get()));
90
91     RetainPtr<CFStringRef> suggestedFilename(AdoptCF, CFURLResponseCopySuggestedFilename(m_cfResponse.get()));
92     m_suggestedFilename = suggestedFilename.get();
93
94     CFHTTPMessageRef httpResponse = CFURLResponseGetHTTPResponse(m_cfResponse.get());
95     if (httpResponse) {
96         m_httpStatusCode = CFHTTPMessageGetResponseStatusCode(httpResponse);
97
98         RetainPtr<CFStringRef> statusLine(AdoptCF, CFHTTPMessageCopyResponseStatusLine(httpResponse));
99         m_httpStatusText = extractReasonPhraseFromHTTPStatusLine(statusLine.get());
100
101         RetainPtr<CFDictionaryRef> headers(AdoptCF, CFHTTPMessageCopyAllHeaderFields(httpResponse));
102         CFIndex headerCount = CFDictionaryGetCount(headers.get());
103         Vector<const void*, 128> keys(headerCount);
104         Vector<const void*, 128> values(headerCount);
105         CFDictionaryGetKeysAndValues(headers.get(), keys.data(), values.data());
106         for (int i = 0; i < headerCount; ++i)
107             m_httpHeaderFields.set((CFStringRef)keys[i], (CFStringRef)values[i]);
108     } else
109         m_httpStatusCode = 0;
110 }
111
112 bool ResourceResponse::platformCompare(const ResourceResponse& a, const ResourceResponse& b)
113 {
114     return CFEqual(a.cfURLResponse(), b.cfURLResponse());
115 }
116
117
118 } // namespace WebCore
119
120 #endif // USE(CFNETWORK)