2 * Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2009 Google Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 #ifndef ResourceResponse_h
28 #define ResourceResponse_h
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"
43 struct CrossThreadResourceResponseData;
45 class PLATFORM_EXPORT ResourceResponse {
46 WTF_MAKE_FAST_ALLOCATED;
48 enum HTTPVersion { Unknown, HTTP_0_9, HTTP_1_0, HTTP_1_1 };
50 class ExtraData : public RefCounted<ExtraData> {
52 virtual ~ExtraData() { }
55 static PassOwnPtr<ResourceResponse> adopt(PassOwnPtr<CrossThreadResourceResponseData>);
57 // Gets a copy of the data suitable for passing to another thread.
58 PassOwnPtr<CrossThreadResourceResponseData> copyData() const;
61 ResourceResponse(const KURL&, const AtomicString& mimeType, long long expectedLength, const AtomicString& textEncodingName, const String& filename);
63 bool isNull() const { return m_isNull; }
66 const KURL& url() const;
67 void setURL(const KURL&);
69 const AtomicString& mimeType() const;
70 void setMimeType(const AtomicString&);
72 long long expectedContentLength() const;
73 void setExpectedContentLength(long long);
75 const AtomicString& textEncodingName() const;
76 void setTextEncodingName(const AtomicString&);
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&);
83 int httpStatusCode() const;
84 void setHTTPStatusCode(int);
86 const AtomicString& httpStatusText() const;
87 void setHTTPStatusText(const AtomicString&);
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;
96 bool isMultipart() const { return mimeType() == "multipart/x-mixed-replace"; }
98 bool isAttachment() const;
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;
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();
114 double expires() const;
115 double lastModified() const;
117 unsigned connectionID() const;
118 void setConnectionID(unsigned);
120 bool connectionReused() const;
121 void setConnectionReused(bool);
123 bool wasCached() const;
124 void setWasCached(bool);
126 ResourceLoadTiming* resourceLoadTiming() const;
127 void setResourceLoadTiming(PassRefPtr<ResourceLoadTiming>);
129 PassRefPtr<ResourceLoadInfo> resourceLoadInfo() const;
130 void setResourceLoadInfo(PassRefPtr<ResourceLoadInfo>);
132 HTTPVersion httpVersion() const { return m_httpVersion; }
133 void setHTTPVersion(HTTPVersion version) { m_httpVersion = version; }
135 const CString& getSecurityInfo() const { return m_securityInfo; }
136 void setSecurityInfo(const CString& securityInfo) { m_securityInfo = securityInfo; }
138 long long appCacheID() const { return m_appCacheID; }
139 void setAppCacheID(long long id) { m_appCacheID = id; }
141 const KURL& appCacheManifestURL() const { return m_appCacheManifestURL; }
142 void setAppCacheManifestURL(const KURL& url) { m_appCacheManifestURL = url; }
144 bool wasFetchedViaSPDY() const { return m_wasFetchedViaSPDY; }
145 void setWasFetchedViaSPDY(bool value) { m_wasFetchedViaSPDY = value; }
147 bool wasNpnNegotiated() const { return m_wasNpnNegotiated; }
148 void setWasNpnNegotiated(bool value) { m_wasNpnNegotiated = value; }
150 bool wasAlternateProtocolAvailable() const
152 return m_wasAlternateProtocolAvailable;
154 void setWasAlternateProtocolAvailable(bool value)
156 m_wasAlternateProtocolAvailable = value;
159 bool wasFetchedViaProxy() const { return m_wasFetchedViaProxy; }
160 void setWasFetchedViaProxy(bool value) { m_wasFetchedViaProxy = value; }
162 bool wasFetchedViaServiceWorker() const { return m_wasFetchedViaServiceWorker; }
163 void setWasFetchedViaServiceWorker(bool value) { m_wasFetchedViaServiceWorker = value; }
165 bool isMultipartPayload() const { return m_isMultipartPayload; }
166 void setIsMultipartPayload(bool value) { m_isMultipartPayload = value; }
168 double responseTime() const { return m_responseTime; }
169 void setResponseTime(double responseTime) { m_responseTime = responseTime; }
171 const AtomicString& remoteIPAddress() const { return m_remoteIPAddress; }
172 void setRemoteIPAddress(const AtomicString& value) { m_remoteIPAddress = value; }
174 unsigned short remotePort() const { return m_remotePort; }
175 void setRemotePort(unsigned short value) { m_remotePort = value; }
177 const String& downloadedFilePath() const { return m_downloadedFilePath; }
178 void setDownloadedFilePath(const String&);
180 // Extra data associated with this response.
181 ExtraData* extraData() const { return m_extraData.get(); }
182 void setExtraData(PassRefPtr<ExtraData> extraData) { m_extraData = extraData; }
184 // The ResourceResponse subclass may "shadow" this method to provide platform-specific memory usage information
185 unsigned memoryUsage() const
187 // average size, mostly due to URL and Header Map strings
191 // This method doesn't compare the all members.
192 static bool compare(const ResourceResponse&, const ResourceResponse&);
195 void updateHeaderParsedState(const AtomicString& name);
198 AtomicString m_mimeType;
199 long long m_expectedContentLength;
200 AtomicString m_textEncodingName;
201 String m_suggestedFilename;
202 int m_httpStatusCode;
203 AtomicString m_httpStatusText;
204 HTTPHeaderMap m_httpHeaderFields;
205 time_t m_lastModifiedDate;
206 bool m_wasCached : 1;
207 unsigned m_connectionID;
208 bool m_connectionReused : 1;
209 RefPtr<ResourceLoadTiming> m_resourceLoadTiming;
210 RefPtr<ResourceLoadInfo> m_resourceLoadInfo;
214 CacheControlHeader m_cacheControlHeader;
216 mutable bool m_haveParsedAgeHeader : 1;
217 mutable bool m_haveParsedDateHeader : 1;
218 mutable bool m_haveParsedExpiresHeader : 1;
219 mutable bool m_haveParsedLastModifiedHeader : 1;
221 mutable double m_age;
222 mutable double m_date;
223 mutable double m_expires;
224 mutable double m_lastModified;
226 // An opaque value that contains some information regarding the security of
227 // the connection for this request, such as SSL connection info (empty
228 // string if not over HTTPS).
229 CString m_securityInfo;
231 // HTTP version used in the response, if known.
232 HTTPVersion m_httpVersion;
234 // The id of the appcache this response was retrieved from, or zero if
235 // the response was not retrieved from an appcache.
236 long long m_appCacheID;
238 // The manifest url of the appcache this response was retrieved from, if any.
239 // Note: only valid for main resource responses.
240 KURL m_appCacheManifestURL;
242 // Set to true if this is part of a multipart response.
243 bool m_isMultipartPayload;
245 // Was the resource fetched over SPDY. See http://dev.chromium.org/spdy
246 bool m_wasFetchedViaSPDY;
248 // Was the resource fetched over a channel which used TLS/Next-Protocol-Negotiation (also SPDY related).
249 bool m_wasNpnNegotiated;
251 // Was the resource fetched over a channel which specified "Alternate-Protocol"
252 // (e.g.: Alternate-Protocol: 443:npn-spdy/1).
253 bool m_wasAlternateProtocolAvailable;
255 // Was the resource fetched over an explicit proxy (HTTP, SOCKS, etc).
256 bool m_wasFetchedViaProxy;
258 // Was the resource fetched over a ServiceWorker.
259 bool m_wasFetchedViaServiceWorker;
261 // The time at which the response headers were received. For cached
262 // responses, this time could be "far" in the past.
263 double m_responseTime;
265 // Remote IP address of the socket which fetched this resource.
266 AtomicString m_remoteIPAddress;
268 // Remote port number of the socket which fetched this resource.
269 unsigned short m_remotePort;
271 // The downloaded file path if the load streamed to a file.
272 String m_downloadedFilePath;
274 // The handle to the downloaded file to ensure the underlying file will not
276 RefPtr<BlobDataHandle> m_downloadedFileHandle;
278 // ExtraData associated with the response.
279 RefPtr<ExtraData> m_extraData;
282 inline bool operator==(const ResourceResponse& a, const ResourceResponse& b) { return ResourceResponse::compare(a, b); }
283 inline bool operator!=(const ResourceResponse& a, const ResourceResponse& b) { return !(a == b); }
285 struct CrossThreadResourceResponseData {
286 WTF_MAKE_NONCOPYABLE(CrossThreadResourceResponseData); WTF_MAKE_FAST_ALLOCATED;
288 CrossThreadResourceResponseData() { }
291 long long m_expectedContentLength;
292 String m_textEncodingName;
293 String m_suggestedFilename;
294 int m_httpStatusCode;
295 String m_httpStatusText;
296 OwnPtr<CrossThreadHTTPHeaderMapData> m_httpHeaders;
297 time_t m_lastModifiedDate;
298 RefPtr<ResourceLoadTiming> m_resourceLoadTiming;
299 CString m_securityInfo;
300 ResourceResponse::HTTPVersion m_httpVersion;
301 long long m_appCacheID;
302 KURL m_appCacheManifestURL;
303 bool m_isMultipartPayload;
304 bool m_wasFetchedViaSPDY;
305 bool m_wasNpnNegotiated;
306 bool m_wasAlternateProtocolAvailable;
307 bool m_wasFetchedViaProxy;
308 bool m_wasFetchedViaServiceWorker;
309 double m_responseTime;
310 String m_remoteIPAddress;
311 unsigned short m_remotePort;
312 String m_downloadedFilePath;
313 RefPtr<BlobDataHandle> m_downloadedFileHandle;
318 #endif // ResourceResponse_h