• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/ResourceLoadInfo.h"
34 #include "platform/network/ResourceLoadTiming.h"
35 #include "platform/weborigin/KURL.h"
36 #include "wtf/PassOwnPtr.h"
37 #include "wtf/RefPtr.h"
38 #include "wtf/text/CString.h"
39 
40 namespace WebCore {
41 
42 struct CrossThreadResourceResponseData;
43 
44 class PLATFORM_EXPORT ResourceResponse {
45     WTF_MAKE_FAST_ALLOCATED;
46 public:
47     enum HTTPVersion { Unknown, HTTP_0_9, HTTP_1_0, HTTP_1_1 };
48 
49     class ExtraData : public RefCounted<ExtraData> {
50     public:
~ExtraData()51         virtual ~ExtraData() { }
52     };
53 
54     static PassOwnPtr<ResourceResponse> adopt(PassOwnPtr<CrossThreadResourceResponseData>);
55 
56     // Gets a copy of the data suitable for passing to another thread.
57     PassOwnPtr<CrossThreadResourceResponseData> copyData() const;
58 
59     ResourceResponse();
60     ResourceResponse(const KURL&, const AtomicString& mimeType, long long expectedLength, const AtomicString& textEncodingName, const String& filename);
61 
isNull()62     bool isNull() const { return m_isNull; }
63     bool isHTTP() const;
64 
65     const KURL& url() const;
66     void setURL(const KURL&);
67 
68     const AtomicString& mimeType() const;
69     void setMimeType(const AtomicString&);
70 
71     long long expectedContentLength() const;
72     void setExpectedContentLength(long long);
73 
74     const AtomicString& textEncodingName() const;
75     void setTextEncodingName(const AtomicString&);
76 
77     // FIXME: Should compute this on the fly.
78     // 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.
79     const String& suggestedFilename() const;
80     void setSuggestedFilename(const String&);
81 
82     int httpStatusCode() const;
83     void setHTTPStatusCode(int);
84 
85     const AtomicString& httpStatusText() const;
86     void setHTTPStatusText(const AtomicString&);
87 
88     const AtomicString& httpHeaderField(const AtomicString& name) const;
89     const AtomicString& httpHeaderField(const char* name) const;
90     void setHTTPHeaderField(const AtomicString& name, const AtomicString& value);
91     void addHTTPHeaderField(const AtomicString& name, const AtomicString& value);
92     void clearHTTPHeaderField(const AtomicString& name);
93     const HTTPHeaderMap& httpHeaderFields() const;
94 
isMultipart()95     bool isMultipart() const { return mimeType() == "multipart/x-mixed-replace"; }
96 
97     bool isAttachment() const;
98 
99     // FIXME: These are used by PluginStream on some platforms. Calculations may differ from just returning plain Last-Modified header.
100     // Leaving it for now but this should go away in favor of generic solution.
101     void setLastModifiedDate(time_t);
102     time_t lastModifiedDate() const;
103 
104     // These functions return parsed values of the corresponding response headers.
105     // NaN means that the header was not present or had invalid value.
106     bool cacheControlContainsNoCache() const;
107     bool cacheControlContainsNoStore() const;
108     bool cacheControlContainsMustRevalidate() const;
109     bool hasCacheValidatorFields() const;
110     double cacheControlMaxAge() const;
111     double date() const;
112     double age() const;
113     double expires() const;
114     double lastModified() const;
115 
116     unsigned connectionID() const;
117     void setConnectionID(unsigned);
118 
119     bool connectionReused() const;
120     void setConnectionReused(bool);
121 
122     bool wasCached() const;
123     void setWasCached(bool);
124 
125     ResourceLoadTiming* resourceLoadTiming() const;
126     void setResourceLoadTiming(PassRefPtr<ResourceLoadTiming>);
127 
128     PassRefPtr<ResourceLoadInfo> resourceLoadInfo() const;
129     void setResourceLoadInfo(PassRefPtr<ResourceLoadInfo>);
130 
httpVersion()131     HTTPVersion httpVersion() const { return m_httpVersion; }
setHTTPVersion(HTTPVersion version)132     void setHTTPVersion(HTTPVersion version) { m_httpVersion = version; }
133 
getSecurityInfo()134     const CString& getSecurityInfo() const { return m_securityInfo; }
setSecurityInfo(const CString & securityInfo)135     void setSecurityInfo(const CString& securityInfo) { m_securityInfo = securityInfo; }
136 
appCacheID()137     long long appCacheID() const { return m_appCacheID; }
setAppCacheID(long long id)138     void setAppCacheID(long long id) { m_appCacheID = id; }
139 
appCacheManifestURL()140     const KURL& appCacheManifestURL() const { return m_appCacheManifestURL; }
setAppCacheManifestURL(const KURL & url)141     void setAppCacheManifestURL(const KURL& url) { m_appCacheManifestURL = url; }
142 
wasFetchedViaSPDY()143     bool wasFetchedViaSPDY() const { return m_wasFetchedViaSPDY; }
setWasFetchedViaSPDY(bool value)144     void setWasFetchedViaSPDY(bool value) { m_wasFetchedViaSPDY = value; }
145 
wasNpnNegotiated()146     bool wasNpnNegotiated() const { return m_wasNpnNegotiated; }
setWasNpnNegotiated(bool value)147     void setWasNpnNegotiated(bool value) { m_wasNpnNegotiated = value; }
148 
wasAlternateProtocolAvailable()149     bool wasAlternateProtocolAvailable() const
150     {
151       return m_wasAlternateProtocolAvailable;
152     }
setWasAlternateProtocolAvailable(bool value)153     void setWasAlternateProtocolAvailable(bool value)
154     {
155       m_wasAlternateProtocolAvailable = value;
156     }
157 
wasFetchedViaProxy()158     bool wasFetchedViaProxy() const { return m_wasFetchedViaProxy; }
setWasFetchedViaProxy(bool value)159     void setWasFetchedViaProxy(bool value) { m_wasFetchedViaProxy = value; }
160 
isMultipartPayload()161     bool isMultipartPayload() const { return m_isMultipartPayload; }
setIsMultipartPayload(bool value)162     void setIsMultipartPayload(bool value) { m_isMultipartPayload = value; }
163 
responseTime()164     double responseTime() const { return m_responseTime; }
setResponseTime(double responseTime)165     void setResponseTime(double responseTime) { m_responseTime = responseTime; }
166 
remoteIPAddress()167     const AtomicString& remoteIPAddress() const { return m_remoteIPAddress; }
setRemoteIPAddress(const AtomicString & value)168     void setRemoteIPAddress(const AtomicString& value) { m_remoteIPAddress = value; }
169 
remotePort()170     unsigned short remotePort() const { return m_remotePort; }
setRemotePort(unsigned short value)171     void setRemotePort(unsigned short value) { m_remotePort = value; }
172 
downloadedFilePath()173     const String& downloadedFilePath() const { return m_downloadedFilePath; }
174     void setDownloadedFilePath(const String&);
175 
176     // Extra data associated with this response.
extraData()177     ExtraData* extraData() const { return m_extraData.get(); }
setExtraData(PassRefPtr<ExtraData> extraData)178     void setExtraData(PassRefPtr<ExtraData> extraData) { m_extraData = extraData; }
179 
180     // The ResourceResponse subclass may "shadow" this method to provide platform-specific memory usage information
memoryUsage()181     unsigned memoryUsage() const
182     {
183         // average size, mostly due to URL and Header Map strings
184         return 1280;
185     }
186 
187     static bool compare(const ResourceResponse&, const ResourceResponse&);
188 
189 private:
190     void parseCacheControlDirectives() const;
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     mutable bool m_haveParsedCacheControlHeader : 1;
211     mutable bool m_haveParsedAgeHeader : 1;
212     mutable bool m_haveParsedDateHeader : 1;
213     mutable bool m_haveParsedExpiresHeader : 1;
214     mutable bool m_haveParsedLastModifiedHeader : 1;
215 
216     mutable bool m_cacheControlContainsNoCache : 1;
217     mutable bool m_cacheControlContainsNoStore : 1;
218     mutable bool m_cacheControlContainsMustRevalidate : 1;
219     mutable double m_cacheControlMaxAge;
220 
221     mutable double m_age;
222     mutable double m_date;
223     mutable double m_expires;
224     mutable double m_lastModified;
225 
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;
230 
231     // HTTP version used in the response, if known.
232     HTTPVersion m_httpVersion;
233 
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;
237 
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;
241 
242     // Set to true if this is part of a multipart response.
243     bool m_isMultipartPayload;
244 
245     // Was the resource fetched over SPDY.  See http://dev.chromium.org/spdy
246     bool m_wasFetchedViaSPDY;
247 
248     // Was the resource fetched over a channel which used TLS/Next-Protocol-Negotiation (also SPDY related).
249     bool m_wasNpnNegotiated;
250 
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;
254 
255     // Was the resource fetched over an explicit proxy (HTTP, SOCKS, etc).
256     bool m_wasFetchedViaProxy;
257 
258     // The time at which the response headers were received.  For cached
259     // responses, this time could be "far" in the past.
260     double m_responseTime;
261 
262     // Remote IP address of the socket which fetched this resource.
263     AtomicString m_remoteIPAddress;
264 
265     // Remote port number of the socket which fetched this resource.
266     unsigned short m_remotePort;
267 
268     // The downloaded file path if the load streamed to a file.
269     String m_downloadedFilePath;
270 
271     // The handle to the downloaded file to ensure the underlying file will not
272     // be deleted.
273     RefPtr<BlobDataHandle> m_downloadedFileHandle;
274 
275     // ExtraData associated with the response.
276     RefPtr<ExtraData> m_extraData;
277 };
278 
279 inline bool operator==(const ResourceResponse& a, const ResourceResponse& b) { return ResourceResponse::compare(a, b); }
280 inline bool operator!=(const ResourceResponse& a, const ResourceResponse& b) { return !(a == b); }
281 
282 struct CrossThreadResourceResponseData {
283     WTF_MAKE_NONCOPYABLE(CrossThreadResourceResponseData); WTF_MAKE_FAST_ALLOCATED;
284 public:
CrossThreadResourceResponseDataCrossThreadResourceResponseData285     CrossThreadResourceResponseData() { }
286     KURL m_url;
287     String m_mimeType;
288     long long m_expectedContentLength;
289     String m_textEncodingName;
290     String m_suggestedFilename;
291     int m_httpStatusCode;
292     String m_httpStatusText;
293     OwnPtr<CrossThreadHTTPHeaderMapData> m_httpHeaders;
294     time_t m_lastModifiedDate;
295     RefPtr<ResourceLoadTiming> m_resourceLoadTiming;
296     CString m_securityInfo;
297     ResourceResponse::HTTPVersion m_httpVersion;
298     long long m_appCacheID;
299     KURL m_appCacheManifestURL;
300     bool m_isMultipartPayload;
301     bool m_wasFetchedViaSPDY;
302     bool m_wasNpnNegotiated;
303     bool m_wasAlternateProtocolAvailable;
304     bool m_wasFetchedViaProxy;
305     double m_responseTime;
306     String m_remoteIPAddress;
307     unsigned short m_remotePort;
308     String m_downloadedFilePath;
309     RefPtr<BlobDataHandle> m_downloadedFileHandle;
310 };
311 
312 } // namespace WebCore
313 
314 #endif // ResourceResponse_h
315