• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2003, 2006 Apple Computer, Inc.  All rights reserved.
3  * Copyright (C) 2006 Samuel Weinig <sam.weinig@gmail.com>
4  * Copyright (C) 2009, 2012 Google Inc. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
16  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef ResourceRequest_h
29 #define ResourceRequest_h
30 
31 #include "platform/network/FormData.h"
32 #include "platform/network/HTTPHeaderMap.h"
33 #include "platform/network/ResourceLoadPriority.h"
34 #include "platform/weborigin/KURL.h"
35 #include "wtf/OwnPtr.h"
36 
37 namespace WebCore {
38 
39 enum ResourceRequestCachePolicy {
40     UseProtocolCachePolicy, // normal load
41     ReloadIgnoringCacheData, // reload
42     ReturnCacheDataElseLoad, // back/forward or encoding change - allow stale data
43     ReturnCacheDataDontLoad  // results of a post - allow stale data and only use cache
44 };
45 
46 struct CrossThreadResourceRequestData;
47 
48 class PLATFORM_EXPORT ResourceRequest {
49     WTF_MAKE_FAST_ALLOCATED;
50 public:
51     // The type of this ResourceRequest, based on how the resource will be used.
52     enum TargetType {
53         TargetIsMainFrame,
54         TargetIsSubframe,
55         TargetIsSubresource, // Resource is a generic subresource. (Generally a specific type should be specified)
56         TargetIsStyleSheet,
57         TargetIsScript,
58         TargetIsFont,
59         TargetIsImage,
60         TargetIsObject,
61         TargetIsMedia,
62         TargetIsWorker,
63         TargetIsSharedWorker,
64         TargetIsPrefetch,
65         TargetIsFavicon,
66         TargetIsXHR,
67         TargetIsTextTrack,
68         TargetIsPing,
69         TargetIsServiceWorker,
70         TargetIsUnspecified,
71     };
72 
73     class ExtraData : public RefCounted<ExtraData> {
74     public:
~ExtraData()75         virtual ~ExtraData() { }
76     };
77 
ResourceRequest()78     ResourceRequest()
79     {
80         initialize(KURL(), UseProtocolCachePolicy);
81     }
82 
ResourceRequest(const String & urlString)83     ResourceRequest(const String& urlString)
84     {
85         initialize(KURL(ParsedURLString, urlString), UseProtocolCachePolicy);
86     }
87 
ResourceRequest(const KURL & url)88     ResourceRequest(const KURL& url)
89     {
90         initialize(url, UseProtocolCachePolicy);
91     }
92 
93     ResourceRequest(const KURL& url, const AtomicString& referrer, ResourceRequestCachePolicy cachePolicy = UseProtocolCachePolicy)
94     {
95         initialize(url, cachePolicy);
96         setHTTPReferrer(referrer);
97     }
98 
99     static PassOwnPtr<ResourceRequest> adopt(PassOwnPtr<CrossThreadResourceRequestData>);
100 
101     // Gets a copy of the data suitable for passing to another thread.
102     PassOwnPtr<CrossThreadResourceRequestData> copyData() const;
103 
104     bool isNull() const;
105     bool isEmpty() const;
106 
107     const KURL& url() const;
108     void setURL(const KURL& url);
109 
110     void removeCredentials();
111 
112     ResourceRequestCachePolicy cachePolicy() const;
113     void setCachePolicy(ResourceRequestCachePolicy cachePolicy);
114 
115     double timeoutInterval() const; // May return 0 when using platform default.
116     void setTimeoutInterval(double timeoutInterval);
117 
118     const KURL& firstPartyForCookies() const;
119     void setFirstPartyForCookies(const KURL& firstPartyForCookies);
120 
121     const AtomicString& httpMethod() const;
122     void setHTTPMethod(const AtomicString&);
123 
124     const HTTPHeaderMap& httpHeaderFields() const;
125     const AtomicString& httpHeaderField(const AtomicString& name) const;
126     const AtomicString& httpHeaderField(const char* name) const;
127     void setHTTPHeaderField(const AtomicString& name, const AtomicString& value);
128     void setHTTPHeaderField(const char* name, const AtomicString& value);
129     void addHTTPHeaderField(const AtomicString& name, const AtomicString& value);
130     void addHTTPHeaderFields(const HTTPHeaderMap& headerFields);
131     void clearHTTPHeaderField(const AtomicString& name);
132 
133     void clearHTTPAuthorization();
134 
httpContentType()135     const AtomicString& httpContentType() const { return httpHeaderField("Content-Type");  }
setHTTPContentType(const AtomicString & httpContentType)136     void setHTTPContentType(const AtomicString& httpContentType) { setHTTPHeaderField("Content-Type", httpContentType); }
137     void clearHTTPContentType();
138 
httpReferrer()139     const AtomicString& httpReferrer() const { return httpHeaderField("Referer"); }
setHTTPReferrer(const AtomicString & httpReferrer)140     void setHTTPReferrer(const AtomicString& httpReferrer) { setHTTPHeaderField("Referer", httpReferrer); }
141     void clearHTTPReferrer();
142 
httpOrigin()143     const AtomicString& httpOrigin() const { return httpHeaderField("Origin"); }
setHTTPOrigin(const AtomicString & httpOrigin)144     void setHTTPOrigin(const AtomicString& httpOrigin) { setHTTPHeaderField("Origin", httpOrigin); }
145     void clearHTTPOrigin();
146 
httpUserAgent()147     const AtomicString& httpUserAgent() const { return httpHeaderField("User-Agent"); }
setHTTPUserAgent(const AtomicString & httpUserAgent)148     void setHTTPUserAgent(const AtomicString& httpUserAgent) { setHTTPHeaderField("User-Agent", httpUserAgent); }
149     void clearHTTPUserAgent();
150 
httpAccept()151     const AtomicString& httpAccept() const { return httpHeaderField("Accept"); }
setHTTPAccept(const AtomicString & httpAccept)152     void setHTTPAccept(const AtomicString& httpAccept) { setHTTPHeaderField("Accept", httpAccept); }
153     void clearHTTPAccept();
154 
155     FormData* httpBody() const;
156     void setHTTPBody(PassRefPtr<FormData> httpBody);
157 
158     bool allowCookies() const;
159     void setAllowCookies(bool allowCookies);
160 
161     ResourceLoadPriority priority() const;
162     void setPriority(ResourceLoadPriority);
163 
164     bool isConditional() const;
165 
166     // Whether the associated ResourceHandleClient needs to be notified of
167     // upload progress made for that resource.
reportUploadProgress()168     bool reportUploadProgress() const { return m_reportUploadProgress; }
setReportUploadProgress(bool reportUploadProgress)169     void setReportUploadProgress(bool reportUploadProgress) { m_reportUploadProgress = reportUploadProgress; }
170 
171     // Whether the timing information should be collected for the request.
reportLoadTiming()172     bool reportLoadTiming() const { return m_reportLoadTiming; }
setReportLoadTiming(bool reportLoadTiming)173     void setReportLoadTiming(bool reportLoadTiming) { m_reportLoadTiming = reportLoadTiming; }
174 
175     // Whether actual headers being sent/received should be collected and reported for the request.
reportRawHeaders()176     bool reportRawHeaders() const { return m_reportRawHeaders; }
setReportRawHeaders(bool reportRawHeaders)177     void setReportRawHeaders(bool reportRawHeaders) { m_reportRawHeaders = reportRawHeaders; }
178 
179     // Allows the request to be matched up with its requestor.
requestorID()180     int requestorID() const { return m_requestorID; }
setRequestorID(int requestorID)181     void setRequestorID(int requestorID) { m_requestorID = requestorID; }
182 
183     // The process id of the process from which this request originated. In
184     // the case of out-of-process plugins, this allows to link back the
185     // request to the plugin process (as it is processed through a render
186     // view process).
requestorProcessID()187     int requestorProcessID() const { return m_requestorProcessID; }
setRequestorProcessID(int requestorProcessID)188     void setRequestorProcessID(int requestorProcessID) { m_requestorProcessID = requestorProcessID; }
189 
190     // Allows the request to be matched up with its app cache host.
appCacheHostID()191     int appCacheHostID() const { return m_appCacheHostID; }
setAppCacheHostID(int id)192     void setAppCacheHostID(int id) { m_appCacheHostID = id; }
193 
194     // True if request was user initiated.
hasUserGesture()195     bool hasUserGesture() const { return m_hasUserGesture; }
setHasUserGesture(bool hasUserGesture)196     void setHasUserGesture(bool hasUserGesture) { m_hasUserGesture = hasUserGesture; }
197 
198     // True if request should be downloaded to file.
downloadToFile()199     bool downloadToFile() const { return m_downloadToFile; }
setDownloadToFile(bool downloadToFile)200     void setDownloadToFile(bool downloadToFile) { m_downloadToFile = downloadToFile; }
201 
202     // Extra data associated with this request.
extraData()203     ExtraData* extraData() const { return m_extraData.get(); }
setExtraData(PassRefPtr<ExtraData> extraData)204     void setExtraData(PassRefPtr<ExtraData> extraData) { m_extraData = extraData; }
205 
206     // What this request is for.
targetType()207     TargetType targetType() const { return m_targetType; }
setTargetType(TargetType type)208     void setTargetType(TargetType type) { m_targetType = type; }
209 
210     static double defaultTimeoutInterval(); // May return 0 when using platform default.
211     static void setDefaultTimeoutInterval(double);
212 
213     static bool compare(const ResourceRequest&, const ResourceRequest&);
214 
215 private:
216     void initialize(const KURL& url, ResourceRequestCachePolicy cachePolicy);
217 
218     KURL m_url;
219     ResourceRequestCachePolicy m_cachePolicy;
220     double m_timeoutInterval; // 0 is a magic value for platform default on platforms that have one.
221     KURL m_firstPartyForCookies;
222     AtomicString m_httpMethod;
223     HTTPHeaderMap m_httpHeaderFields;
224     RefPtr<FormData> m_httpBody;
225     bool m_allowCookies : 1;
226     bool m_reportUploadProgress : 1;
227     bool m_reportLoadTiming : 1;
228     bool m_reportRawHeaders : 1;
229     bool m_hasUserGesture : 1;
230     bool m_downloadToFile : 1;
231     ResourceLoadPriority m_priority;
232     int m_requestorID;
233     int m_requestorProcessID;
234     int m_appCacheHostID;
235     RefPtr<ExtraData> m_extraData;
236     TargetType m_targetType;
237 
238     static double s_defaultTimeoutInterval;
239 };
240 
241 bool equalIgnoringHeaderFields(const ResourceRequest&, const ResourceRequest&);
242 
243 inline bool operator==(const ResourceRequest& a, const ResourceRequest& b) { return ResourceRequest::compare(a, b); }
244 inline bool operator!=(ResourceRequest& a, const ResourceRequest& b) { return !(a == b); }
245 
246 struct CrossThreadResourceRequestData {
247     WTF_MAKE_NONCOPYABLE(CrossThreadResourceRequestData); WTF_MAKE_FAST_ALLOCATED;
248 public:
CrossThreadResourceRequestDataCrossThreadResourceRequestData249     CrossThreadResourceRequestData() { }
250     KURL m_url;
251 
252     ResourceRequestCachePolicy m_cachePolicy;
253     double m_timeoutInterval;
254     KURL m_firstPartyForCookies;
255 
256     String m_httpMethod;
257     OwnPtr<CrossThreadHTTPHeaderMapData> m_httpHeaders;
258     RefPtr<FormData> m_httpBody;
259     bool m_allowCookies;
260     bool m_reportUploadProgress;
261     bool m_hasUserGesture;
262     bool m_downloadToFile;
263     ResourceLoadPriority m_priority;
264     int m_requestorID;
265     int m_requestorProcessID;
266     int m_appCacheHostID;
267     ResourceRequest::TargetType m_targetType;
268 };
269 
270 unsigned initializeMaximumHTTPConnectionCountPerHost();
271 
272 } // namespace WebCore
273 
274 #endif // ResourceRequest_h
275