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
41namespace blink {
42
43struct CrossThreadResourceResponseData;
44
45class PLATFORM_EXPORT ResourceResponse {
46    WTF_MAKE_FAST_ALLOCATED;
47public:
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 wasFetchedViaServiceWorker() const { return m_wasFetchedViaServiceWorker; }
163    void setWasFetchedViaServiceWorker(bool value) { m_wasFetchedViaServiceWorker = value; }
164
165    bool isMultipartPayload() const { return m_isMultipartPayload; }
166    void setIsMultipartPayload(bool value) { m_isMultipartPayload = value; }
167
168    double responseTime() const { return m_responseTime; }
169    void setResponseTime(double responseTime) { m_responseTime = responseTime; }
170
171    const AtomicString& remoteIPAddress() const { return m_remoteIPAddress; }
172    void setRemoteIPAddress(const AtomicString& value) { m_remoteIPAddress = value; }
173
174    unsigned short remotePort() const { return m_remotePort; }
175    void setRemotePort(unsigned short value) { m_remotePort = value; }
176
177    const String& downloadedFilePath() const { return m_downloadedFilePath; }
178    void setDownloadedFilePath(const String&);
179
180    // Extra data associated with this response.
181    ExtraData* extraData() const { return m_extraData.get(); }
182    void setExtraData(PassRefPtr<ExtraData> extraData) { m_extraData = extraData; }
183
184    // The ResourceResponse subclass may "shadow" this method to provide platform-specific memory usage information
185    unsigned memoryUsage() const
186    {
187        // average size, mostly due to URL and Header Map strings
188        return 1280;
189    }
190
191    // This method doesn't compare the all members.
192    static bool compare(const ResourceResponse&, const ResourceResponse&);
193
194private:
195    void updateHeaderParsedState(const AtomicString& name);
196
197    KURL m_url;
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;
211
212    bool m_isNull : 1;
213
214    CacheControlHeader m_cacheControlHeader;
215
216    mutable bool m_haveParsedAgeHeader : 1;
217    mutable bool m_haveParsedDateHeader : 1;
218    mutable bool m_haveParsedExpiresHeader : 1;
219    mutable bool m_haveParsedLastModifiedHeader : 1;
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    // Was the resource fetched over a ServiceWorker.
259    bool m_wasFetchedViaServiceWorker;
260
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;
264
265    // Remote IP address of the socket which fetched this resource.
266    AtomicString m_remoteIPAddress;
267
268    // Remote port number of the socket which fetched this resource.
269    unsigned short m_remotePort;
270
271    // The downloaded file path if the load streamed to a file.
272    String m_downloadedFilePath;
273
274    // The handle to the downloaded file to ensure the underlying file will not
275    // be deleted.
276    RefPtr<BlobDataHandle> m_downloadedFileHandle;
277
278    // ExtraData associated with the response.
279    RefPtr<ExtraData> m_extraData;
280};
281
282inline bool operator==(const ResourceResponse& a, const ResourceResponse& b) { return ResourceResponse::compare(a, b); }
283inline bool operator!=(const ResourceResponse& a, const ResourceResponse& b) { return !(a == b); }
284
285struct CrossThreadResourceResponseData {
286    WTF_MAKE_NONCOPYABLE(CrossThreadResourceResponseData); WTF_MAKE_FAST_ALLOCATED;
287public:
288    CrossThreadResourceResponseData() { }
289    KURL m_url;
290    String m_mimeType;
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;
314};
315
316} // namespace blink
317
318#endif // ResourceResponse_h
319