Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / network / ResourceResponse.h
1 /*
2  * Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
3  * Copyright (C) 2009 Google Inc. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #ifndef ResourceResponse_h
28 #define ResourceResponse_h
29
30 #include "platform/PlatformExport.h"
31 #include "platform/blob/BlobData.h"
32 #include "platform/network/HTTPHeaderMap.h"
33 #include "platform/network/HTTPParsers.h"
34 #include "platform/network/ResourceLoadInfo.h"
35 #include "platform/network/ResourceLoadTiming.h"
36 #include "platform/weborigin/KURL.h"
37 #include "wtf/PassOwnPtr.h"
38 #include "wtf/RefPtr.h"
39 #include "wtf/text/CString.h"
40
41 namespace WebCore {
42
43 struct CrossThreadResourceResponseData;
44
45 class PLATFORM_EXPORT ResourceResponse {
46     WTF_MAKE_FAST_ALLOCATED;
47 public:
48     enum HTTPVersion { Unknown, HTTP_0_9, HTTP_1_0, HTTP_1_1 };
49
50     class ExtraData : public RefCounted<ExtraData> {
51     public:
52         virtual ~ExtraData() { }
53     };
54
55     static PassOwnPtr<ResourceResponse> adopt(PassOwnPtr<CrossThreadResourceResponseData>);
56
57     // Gets a copy of the data suitable for passing to another thread.
58     PassOwnPtr<CrossThreadResourceResponseData> copyData() const;
59
60     ResourceResponse();
61     ResourceResponse(const KURL&, const AtomicString& mimeType, long long expectedLength, const AtomicString& textEncodingName, const String& filename);
62
63     bool isNull() const { return m_isNull; }
64     bool isHTTP() const;
65
66     const KURL& url() const;
67     void setURL(const KURL&);
68
69     const AtomicString& mimeType() const;
70     void setMimeType(const AtomicString&);
71
72     long long expectedContentLength() const;
73     void setExpectedContentLength(long long);
74
75     const AtomicString& textEncodingName() const;
76     void setTextEncodingName(const AtomicString&);
77
78     // FIXME: Should compute this on the fly.
79     // There should not be a setter exposed, as suggested file name is determined based on other headers in a manner that WebCore does not necessarily know about.
80     const String& suggestedFilename() const;
81     void setSuggestedFilename(const String&);
82
83     int httpStatusCode() const;
84     void setHTTPStatusCode(int);
85
86     const AtomicString& httpStatusText() const;
87     void setHTTPStatusText(const AtomicString&);
88
89     const AtomicString& httpHeaderField(const AtomicString& name) const;
90     const AtomicString& httpHeaderField(const char* name) const;
91     void setHTTPHeaderField(const AtomicString& name, const AtomicString& value);
92     void addHTTPHeaderField(const AtomicString& name, const AtomicString& value);
93     void clearHTTPHeaderField(const AtomicString& name);
94     const HTTPHeaderMap& httpHeaderFields() const;
95
96     bool isMultipart() const { return mimeType() == "multipart/x-mixed-replace"; }
97
98     bool isAttachment() const;
99
100     // FIXME: These are used by PluginStream on some platforms. Calculations may differ from just returning plain Last-Modified header.
101     // Leaving it for now but this should go away in favor of generic solution.
102     void setLastModifiedDate(time_t);
103     time_t lastModifiedDate() const;
104
105     // These functions return parsed values of the corresponding response headers.
106     // NaN means that the header was not present or had invalid value.
107     bool cacheControlContainsNoCache();
108     bool cacheControlContainsNoStore();
109     bool cacheControlContainsMustRevalidate();
110     bool hasCacheValidatorFields() const;
111     double cacheControlMaxAge();
112     double date() const;
113     double age() const;
114     double expires() const;
115     double lastModified() const;
116
117     unsigned connectionID() const;
118     void setConnectionID(unsigned);
119
120     bool connectionReused() const;
121     void setConnectionReused(bool);
122
123     bool wasCached() const;
124     void setWasCached(bool);
125
126     ResourceLoadTiming* resourceLoadTiming() const;
127     void setResourceLoadTiming(PassRefPtr<ResourceLoadTiming>);
128
129     PassRefPtr<ResourceLoadInfo> resourceLoadInfo() const;
130     void setResourceLoadInfo(PassRefPtr<ResourceLoadInfo>);
131
132     HTTPVersion httpVersion() const { return m_httpVersion; }
133     void setHTTPVersion(HTTPVersion version) { m_httpVersion = version; }
134
135     const CString& getSecurityInfo() const { return m_securityInfo; }
136     void setSecurityInfo(const CString& securityInfo) { m_securityInfo = securityInfo; }
137
138     long long appCacheID() const { return m_appCacheID; }
139     void setAppCacheID(long long id) { m_appCacheID = id; }
140
141     const KURL& appCacheManifestURL() const { return m_appCacheManifestURL; }
142     void setAppCacheManifestURL(const KURL& url) { m_appCacheManifestURL = url; }
143
144     bool wasFetchedViaSPDY() const { return m_wasFetchedViaSPDY; }
145     void setWasFetchedViaSPDY(bool value) { m_wasFetchedViaSPDY = value; }
146
147     bool wasNpnNegotiated() const { return m_wasNpnNegotiated; }
148     void setWasNpnNegotiated(bool value) { m_wasNpnNegotiated = value; }
149
150     bool wasAlternateProtocolAvailable() const
151     {
152       return m_wasAlternateProtocolAvailable;
153     }
154     void setWasAlternateProtocolAvailable(bool value)
155     {
156       m_wasAlternateProtocolAvailable = value;
157     }
158
159     bool wasFetchedViaProxy() const { return m_wasFetchedViaProxy; }
160     void setWasFetchedViaProxy(bool value) { m_wasFetchedViaProxy = value; }
161
162     bool isMultipartPayload() const { return m_isMultipartPayload; }
163     void setIsMultipartPayload(bool value) { m_isMultipartPayload = value; }
164
165     double responseTime() const { return m_responseTime; }
166     void setResponseTime(double responseTime) { m_responseTime = responseTime; }
167
168     const AtomicString& remoteIPAddress() const { return m_remoteIPAddress; }
169     void setRemoteIPAddress(const AtomicString& value) { m_remoteIPAddress = value; }
170
171     unsigned short remotePort() const { return m_remotePort; }
172     void setRemotePort(unsigned short value) { m_remotePort = value; }
173
174     const String& downloadedFilePath() const { return m_downloadedFilePath; }
175     void setDownloadedFilePath(const String&);
176
177     // Extra data associated with this response.
178     ExtraData* extraData() const { return m_extraData.get(); }
179     void setExtraData(PassRefPtr<ExtraData> extraData) { m_extraData = extraData; }
180
181     // The ResourceResponse subclass may "shadow" this method to provide platform-specific memory usage information
182     unsigned memoryUsage() const
183     {
184         // average size, mostly due to URL and Header Map strings
185         return 1280;
186     }
187
188     static bool compare(const ResourceResponse&, const ResourceResponse&);
189
190 private:
191     void updateHeaderParsedState(const AtomicString& name);
192
193     KURL m_url;
194     AtomicString m_mimeType;
195     long long m_expectedContentLength;
196     AtomicString m_textEncodingName;
197     String m_suggestedFilename;
198     int m_httpStatusCode;
199     AtomicString m_httpStatusText;
200     HTTPHeaderMap m_httpHeaderFields;
201     time_t m_lastModifiedDate;
202     bool m_wasCached : 1;
203     unsigned m_connectionID;
204     bool m_connectionReused : 1;
205     RefPtr<ResourceLoadTiming> m_resourceLoadTiming;
206     RefPtr<ResourceLoadInfo> m_resourceLoadInfo;
207
208     bool m_isNull : 1;
209
210     CacheControlHeader m_cacheControlHeader;
211
212     mutable bool m_haveParsedAgeHeader : 1;
213     mutable bool m_haveParsedDateHeader : 1;
214     mutable bool m_haveParsedExpiresHeader : 1;
215     mutable bool m_haveParsedLastModifiedHeader : 1;
216
217     mutable double m_age;
218     mutable double m_date;
219     mutable double m_expires;
220     mutable double m_lastModified;
221
222     // An opaque value that contains some information regarding the security of
223     // the connection for this request, such as SSL connection info (empty
224     // string if not over HTTPS).
225     CString m_securityInfo;
226
227     // HTTP version used in the response, if known.
228     HTTPVersion m_httpVersion;
229
230     // The id of the appcache this response was retrieved from, or zero if
231     // the response was not retrieved from an appcache.
232     long long m_appCacheID;
233
234     // The manifest url of the appcache this response was retrieved from, if any.
235     // Note: only valid for main resource responses.
236     KURL m_appCacheManifestURL;
237
238     // Set to true if this is part of a multipart response.
239     bool m_isMultipartPayload;
240
241     // Was the resource fetched over SPDY.  See http://dev.chromium.org/spdy
242     bool m_wasFetchedViaSPDY;
243
244     // Was the resource fetched over a channel which used TLS/Next-Protocol-Negotiation (also SPDY related).
245     bool m_wasNpnNegotiated;
246
247     // Was the resource fetched over a channel which specified "Alternate-Protocol"
248     // (e.g.: Alternate-Protocol: 443:npn-spdy/1).
249     bool m_wasAlternateProtocolAvailable;
250
251     // Was the resource fetched over an explicit proxy (HTTP, SOCKS, etc).
252     bool m_wasFetchedViaProxy;
253
254     // The time at which the response headers were received.  For cached
255     // responses, this time could be "far" in the past.
256     double m_responseTime;
257
258     // Remote IP address of the socket which fetched this resource.
259     AtomicString m_remoteIPAddress;
260
261     // Remote port number of the socket which fetched this resource.
262     unsigned short m_remotePort;
263
264     // The downloaded file path if the load streamed to a file.
265     String m_downloadedFilePath;
266
267     // The handle to the downloaded file to ensure the underlying file will not
268     // be deleted.
269     RefPtr<BlobDataHandle> m_downloadedFileHandle;
270
271     // ExtraData associated with the response.
272     RefPtr<ExtraData> m_extraData;
273 };
274
275 inline bool operator==(const ResourceResponse& a, const ResourceResponse& b) { return ResourceResponse::compare(a, b); }
276 inline bool operator!=(const ResourceResponse& a, const ResourceResponse& b) { return !(a == b); }
277
278 struct CrossThreadResourceResponseData {
279     WTF_MAKE_NONCOPYABLE(CrossThreadResourceResponseData); WTF_MAKE_FAST_ALLOCATED;
280 public:
281     CrossThreadResourceResponseData() { }
282     KURL m_url;
283     String m_mimeType;
284     long long m_expectedContentLength;
285     String m_textEncodingName;
286     String m_suggestedFilename;
287     int m_httpStatusCode;
288     String m_httpStatusText;
289     OwnPtr<CrossThreadHTTPHeaderMapData> m_httpHeaders;
290     time_t m_lastModifiedDate;
291     RefPtr<ResourceLoadTiming> m_resourceLoadTiming;
292     CString m_securityInfo;
293     ResourceResponse::HTTPVersion m_httpVersion;
294     long long m_appCacheID;
295     KURL m_appCacheManifestURL;
296     bool m_isMultipartPayload;
297     bool m_wasFetchedViaSPDY;
298     bool m_wasNpnNegotiated;
299     bool m_wasAlternateProtocolAvailable;
300     bool m_wasFetchedViaProxy;
301     double m_responseTime;
302     String m_remoteIPAddress;
303     unsigned short m_remotePort;
304     String m_downloadedFilePath;
305     RefPtr<BlobDataHandle> m_downloadedFileHandle;
306 };
307
308 } // namespace WebCore
309
310 #endif // ResourceResponse_h