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 "public/platform/WebServiceWorkerResponseType.h"
38 #include "wtf/PassOwnPtr.h"
39 #include "wtf/RefPtr.h"
40 #include "wtf/text/CString.h"
44 struct CrossThreadResourceResponseData;
46 class PLATFORM_EXPORT ResourceResponse {
47 WTF_MAKE_FAST_ALLOCATED;
49 enum HTTPVersion { Unknown, HTTP_0_9, HTTP_1_0, HTTP_1_1 };
51 class ExtraData : public RefCounted<ExtraData> {
53 virtual ~ExtraData() { }
56 static PassOwnPtr<ResourceResponse> adopt(PassOwnPtr<CrossThreadResourceResponseData>);
58 // Gets a copy of the data suitable for passing to another thread.
59 PassOwnPtr<CrossThreadResourceResponseData> copyData() const;
62 ResourceResponse(const KURL&, const AtomicString& mimeType, long long expectedLength, const AtomicString& textEncodingName, const String& filename);
64 bool isNull() const { return m_isNull; }
67 const KURL& url() const;
68 void setURL(const KURL&);
70 const AtomicString& mimeType() const;
71 void setMimeType(const AtomicString&);
73 long long expectedContentLength() const;
74 void setExpectedContentLength(long long);
76 const AtomicString& textEncodingName() const;
77 void setTextEncodingName(const AtomicString&);
79 // FIXME: Should compute this on the fly.
80 // 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.
81 const String& suggestedFilename() const;
82 void setSuggestedFilename(const String&);
84 int httpStatusCode() const;
85 void setHTTPStatusCode(int);
87 const AtomicString& httpStatusText() const;
88 void setHTTPStatusText(const AtomicString&);
90 const AtomicString& httpHeaderField(const AtomicString& name) const;
91 const AtomicString& httpHeaderField(const char* name) const;
92 void setHTTPHeaderField(const AtomicString& name, const AtomicString& value);
93 void addHTTPHeaderField(const AtomicString& name, const AtomicString& value);
94 void clearHTTPHeaderField(const AtomicString& name);
95 const HTTPHeaderMap& httpHeaderFields() const;
97 bool isMultipart() const { return mimeType() == "multipart/x-mixed-replace"; }
99 bool isAttachment() const;
101 // FIXME: These are used by PluginStream on some platforms. Calculations may differ from just returning plain Last-Modified header.
102 // Leaving it for now but this should go away in favor of generic solution.
103 void setLastModifiedDate(time_t);
104 time_t lastModifiedDate() const;
106 // These functions return parsed values of the corresponding response headers.
107 // NaN means that the header was not present or had invalid value.
108 bool cacheControlContainsNoCache();
109 bool cacheControlContainsNoStore();
110 bool cacheControlContainsMustRevalidate();
111 bool hasCacheValidatorFields() const;
112 double cacheControlMaxAge();
115 double expires() const;
116 double lastModified() const;
118 unsigned connectionID() const;
119 void setConnectionID(unsigned);
121 bool connectionReused() const;
122 void setConnectionReused(bool);
124 bool wasCached() const;
125 void setWasCached(bool);
127 ResourceLoadTiming* resourceLoadTiming() const;
128 void setResourceLoadTiming(PassRefPtr<ResourceLoadTiming>);
130 PassRefPtr<ResourceLoadInfo> resourceLoadInfo() const;
131 void setResourceLoadInfo(PassRefPtr<ResourceLoadInfo>);
133 HTTPVersion httpVersion() const { return m_httpVersion; }
134 void setHTTPVersion(HTTPVersion version) { m_httpVersion = version; }
136 const CString& getSecurityInfo() const { return m_securityInfo; }
137 void setSecurityInfo(const CString& securityInfo) { m_securityInfo = securityInfo; }
139 long long appCacheID() const { return m_appCacheID; }
140 void setAppCacheID(long long id) { m_appCacheID = id; }
142 const KURL& appCacheManifestURL() const { return m_appCacheManifestURL; }
143 void setAppCacheManifestURL(const KURL& url) { m_appCacheManifestURL = url; }
145 bool wasFetchedViaSPDY() const { return m_wasFetchedViaSPDY; }
146 void setWasFetchedViaSPDY(bool value) { m_wasFetchedViaSPDY = value; }
148 bool wasNpnNegotiated() const { return m_wasNpnNegotiated; }
149 void setWasNpnNegotiated(bool value) { m_wasNpnNegotiated = value; }
151 bool wasAlternateProtocolAvailable() const
153 return m_wasAlternateProtocolAvailable;
155 void setWasAlternateProtocolAvailable(bool value)
157 m_wasAlternateProtocolAvailable = value;
160 bool wasFetchedViaProxy() const { return m_wasFetchedViaProxy; }
161 void setWasFetchedViaProxy(bool value) { m_wasFetchedViaProxy = value; }
163 bool wasFetchedViaServiceWorker() const { return m_wasFetchedViaServiceWorker; }
164 void setWasFetchedViaServiceWorker(bool value) { m_wasFetchedViaServiceWorker = value; }
166 bool wasFallbackRequiredByServiceWorker() const { return m_wasFallbackRequiredByServiceWorker; }
167 void setWasFallbackRequiredByServiceWorker(bool value) { m_wasFallbackRequiredByServiceWorker = value; }
169 WebServiceWorkerResponseType serviceWorkerResponseType() const { return m_serviceWorkerResponseType; }
170 void setServiceWorkerResponseType(WebServiceWorkerResponseType value) { m_serviceWorkerResponseType = value; }
172 bool isMultipartPayload() const { return m_isMultipartPayload; }
173 void setIsMultipartPayload(bool value) { m_isMultipartPayload = value; }
175 double responseTime() const { return m_responseTime; }
176 void setResponseTime(double responseTime) { m_responseTime = responseTime; }
178 const AtomicString& remoteIPAddress() const { return m_remoteIPAddress; }
179 void setRemoteIPAddress(const AtomicString& value) { m_remoteIPAddress = value; }
181 unsigned short remotePort() const { return m_remotePort; }
182 void setRemotePort(unsigned short value) { m_remotePort = value; }
184 const String& downloadedFilePath() const { return m_downloadedFilePath; }
185 void setDownloadedFilePath(const String&);
187 // Extra data associated with this response.
188 ExtraData* extraData() const { return m_extraData.get(); }
189 void setExtraData(PassRefPtr<ExtraData> extraData) { m_extraData = extraData; }
191 // The ResourceResponse subclass may "shadow" this method to provide platform-specific memory usage information
192 unsigned memoryUsage() const
194 // average size, mostly due to URL and Header Map strings
198 // This method doesn't compare the all members.
199 static bool compare(const ResourceResponse&, const ResourceResponse&);
202 void updateHeaderParsedState(const AtomicString& name);
205 AtomicString m_mimeType;
206 long long m_expectedContentLength;
207 AtomicString m_textEncodingName;
208 String m_suggestedFilename;
209 int m_httpStatusCode;
210 AtomicString m_httpStatusText;
211 HTTPHeaderMap m_httpHeaderFields;
212 time_t m_lastModifiedDate;
213 bool m_wasCached : 1;
214 unsigned m_connectionID;
215 bool m_connectionReused : 1;
216 RefPtr<ResourceLoadTiming> m_resourceLoadTiming;
217 RefPtr<ResourceLoadInfo> m_resourceLoadInfo;
221 CacheControlHeader m_cacheControlHeader;
223 mutable bool m_haveParsedAgeHeader : 1;
224 mutable bool m_haveParsedDateHeader : 1;
225 mutable bool m_haveParsedExpiresHeader : 1;
226 mutable bool m_haveParsedLastModifiedHeader : 1;
228 mutable double m_age;
229 mutable double m_date;
230 mutable double m_expires;
231 mutable double m_lastModified;
233 // An opaque value that contains some information regarding the security of
234 // the connection for this request, such as SSL connection info (empty
235 // string if not over HTTPS).
236 CString m_securityInfo;
238 // HTTP version used in the response, if known.
239 HTTPVersion m_httpVersion;
241 // The id of the appcache this response was retrieved from, or zero if
242 // the response was not retrieved from an appcache.
243 long long m_appCacheID;
245 // The manifest url of the appcache this response was retrieved from, if any.
246 // Note: only valid for main resource responses.
247 KURL m_appCacheManifestURL;
249 // Set to true if this is part of a multipart response.
250 bool m_isMultipartPayload;
252 // Was the resource fetched over SPDY. See http://dev.chromium.org/spdy
253 bool m_wasFetchedViaSPDY;
255 // Was the resource fetched over a channel which used TLS/Next-Protocol-Negotiation (also SPDY related).
256 bool m_wasNpnNegotiated;
258 // Was the resource fetched over a channel which specified "Alternate-Protocol"
259 // (e.g.: Alternate-Protocol: 443:npn-spdy/1).
260 bool m_wasAlternateProtocolAvailable;
262 // Was the resource fetched over an explicit proxy (HTTP, SOCKS, etc).
263 bool m_wasFetchedViaProxy;
265 // Was the resource fetched over a ServiceWorker.
266 bool m_wasFetchedViaServiceWorker;
268 // Was the fallback request with skip service worker flag required.
269 bool m_wasFallbackRequiredByServiceWorker;
271 // The type of the response which was fetched by the ServiceWorker.
272 WebServiceWorkerResponseType m_serviceWorkerResponseType;
274 // The time at which the response headers were received. For cached
275 // responses, this time could be "far" in the past.
276 double m_responseTime;
278 // Remote IP address of the socket which fetched this resource.
279 AtomicString m_remoteIPAddress;
281 // Remote port number of the socket which fetched this resource.
282 unsigned short m_remotePort;
284 // The downloaded file path if the load streamed to a file.
285 String m_downloadedFilePath;
287 // The handle to the downloaded file to ensure the underlying file will not
289 RefPtr<BlobDataHandle> m_downloadedFileHandle;
291 // ExtraData associated with the response.
292 RefPtr<ExtraData> m_extraData;
295 inline bool operator==(const ResourceResponse& a, const ResourceResponse& b) { return ResourceResponse::compare(a, b); }
296 inline bool operator!=(const ResourceResponse& a, const ResourceResponse& b) { return !(a == b); }
298 struct CrossThreadResourceResponseData {
299 WTF_MAKE_NONCOPYABLE(CrossThreadResourceResponseData); WTF_MAKE_FAST_ALLOCATED;
301 CrossThreadResourceResponseData() { }
304 long long m_expectedContentLength;
305 String m_textEncodingName;
306 String m_suggestedFilename;
307 int m_httpStatusCode;
308 String m_httpStatusText;
309 OwnPtr<CrossThreadHTTPHeaderMapData> m_httpHeaders;
310 time_t m_lastModifiedDate;
311 RefPtr<ResourceLoadTiming> m_resourceLoadTiming;
312 CString m_securityInfo;
313 ResourceResponse::HTTPVersion m_httpVersion;
314 long long m_appCacheID;
315 KURL m_appCacheManifestURL;
316 bool m_isMultipartPayload;
317 bool m_wasFetchedViaSPDY;
318 bool m_wasNpnNegotiated;
319 bool m_wasAlternateProtocolAvailable;
320 bool m_wasFetchedViaProxy;
321 bool m_wasFetchedViaServiceWorker;
322 bool m_wasFallbackRequiredByServiceWorker;
323 WebServiceWorkerResponseType m_serviceWorkerResponseType;
324 double m_responseTime;
325 String m_remoteIPAddress;
326 unsigned short m_remotePort;
327 String m_downloadedFilePath;
328 RefPtr<BlobDataHandle> m_downloadedFileHandle;
333 #endif // ResourceResponse_h