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.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
28 #ifndef ResourceRequest_h
29 #define ResourceRequest_h
31 #include "platform/network/FormData.h"
32 #include "platform/network/HTTPHeaderMap.h"
33 #include "platform/network/HTTPParsers.h"
34 #include "platform/network/ResourceLoadPriority.h"
35 #include "platform/weborigin/KURL.h"
36 #include "platform/weborigin/Referrer.h"
37 #include "wtf/OwnPtr.h"
41 enum ResourceRequestCachePolicy {
42 UseProtocolCachePolicy, // normal load
43 ReloadIgnoringCacheData, // reload
44 ReturnCacheDataElseLoad, // back/forward or encoding change - allow stale data
45 ReturnCacheDataDontLoad // results of a post - allow stale data and only use cache
48 struct CrossThreadResourceRequestData;
50 class PLATFORM_EXPORT ResourceRequest {
51 WTF_MAKE_FAST_ALLOCATED;
53 // The type of this ResourceRequest, based on how the resource will be used.
57 TargetIsSubresource, // Resource is a generic subresource. (Generally a specific type should be specified)
71 TargetIsServiceWorker,
75 class ExtraData : public RefCounted<ExtraData> {
77 virtual ~ExtraData() { }
82 initialize(KURL(), UseProtocolCachePolicy);
85 ResourceRequest(const String& urlString)
87 initialize(KURL(ParsedURLString, urlString), UseProtocolCachePolicy);
90 ResourceRequest(const KURL& url)
92 initialize(url, UseProtocolCachePolicy);
95 ResourceRequest(const KURL& url, const Referrer& referrer, ResourceRequestCachePolicy cachePolicy = UseProtocolCachePolicy)
97 initialize(url, cachePolicy);
98 setHTTPReferrer(referrer);
101 static PassOwnPtr<ResourceRequest> adopt(PassOwnPtr<CrossThreadResourceRequestData>);
103 // Gets a copy of the data suitable for passing to another thread.
104 PassOwnPtr<CrossThreadResourceRequestData> copyData() const;
107 bool isEmpty() const;
109 const KURL& url() const;
110 void setURL(const KURL& url);
112 void removeCredentials();
114 ResourceRequestCachePolicy cachePolicy() const;
115 void setCachePolicy(ResourceRequestCachePolicy cachePolicy);
117 double timeoutInterval() const; // May return 0 when using platform default.
118 void setTimeoutInterval(double timeoutInterval);
120 const KURL& firstPartyForCookies() const;
121 void setFirstPartyForCookies(const KURL& firstPartyForCookies);
123 const AtomicString& httpMethod() const;
124 void setHTTPMethod(const AtomicString&);
126 const HTTPHeaderMap& httpHeaderFields() const;
127 const AtomicString& httpHeaderField(const AtomicString& name) const;
128 const AtomicString& httpHeaderField(const char* name) const;
129 void setHTTPHeaderField(const AtomicString& name, const AtomicString& value);
130 void setHTTPHeaderField(const char* name, const AtomicString& value);
131 void addHTTPHeaderField(const AtomicString& name, const AtomicString& value);
132 void addHTTPHeaderFields(const HTTPHeaderMap& headerFields);
133 void clearHTTPHeaderField(const AtomicString& name);
135 void clearHTTPAuthorization();
137 const AtomicString& httpContentType() const { return httpHeaderField("Content-Type"); }
138 void setHTTPContentType(const AtomicString& httpContentType) { setHTTPHeaderField("Content-Type", httpContentType); }
140 const AtomicString& httpReferrer() const { return httpHeaderField("Referer"); }
141 ReferrerPolicy referrerPolicy() const { return m_referrerPolicy; }
142 void setHTTPReferrer(const Referrer& httpReferrer) { setHTTPHeaderField("Referer", httpReferrer.referrer); m_referrerPolicy = httpReferrer.referrerPolicy; }
143 void clearHTTPReferrer();
145 const AtomicString& httpOrigin() const { return httpHeaderField("Origin"); }
146 void setHTTPOrigin(const AtomicString& httpOrigin) { setHTTPHeaderField("Origin", httpOrigin); }
147 void clearHTTPOrigin();
149 const AtomicString& httpUserAgent() const { return httpHeaderField("User-Agent"); }
150 void setHTTPUserAgent(const AtomicString& httpUserAgent) { setHTTPHeaderField("User-Agent", httpUserAgent); }
151 void clearHTTPUserAgent();
153 const AtomicString& httpAccept() const { return httpHeaderField("Accept"); }
154 void setHTTPAccept(const AtomicString& httpAccept) { setHTTPHeaderField("Accept", httpAccept); }
156 FormData* httpBody() const;
157 void setHTTPBody(PassRefPtr<FormData> httpBody);
159 bool allowStoredCredentials() const;
160 void setAllowStoredCredentials(bool allowCredentials);
162 ResourceLoadPriority priority() const;
163 void setPriority(ResourceLoadPriority, int intraPriorityValue = 0);
165 bool isConditional() const;
167 // Whether the associated ResourceHandleClient needs to be notified of
168 // upload progress made for that resource.
169 bool reportUploadProgress() const { return m_reportUploadProgress; }
170 void setReportUploadProgress(bool reportUploadProgress) { m_reportUploadProgress = reportUploadProgress; }
172 // Whether the timing information should be collected for the request.
173 bool reportLoadTiming() const { return m_reportLoadTiming; }
174 void setReportLoadTiming(bool reportLoadTiming) { m_reportLoadTiming = reportLoadTiming; }
176 // Whether actual headers being sent/received should be collected and reported for the request.
177 bool reportRawHeaders() const { return m_reportRawHeaders; }
178 void setReportRawHeaders(bool reportRawHeaders) { m_reportRawHeaders = reportRawHeaders; }
180 // Allows the request to be matched up with its requestor.
181 int requestorID() const { return m_requestorID; }
182 void setRequestorID(int requestorID) { m_requestorID = requestorID; }
184 // The process id of the process from which this request originated. In
185 // the case of out-of-process plugins, this allows to link back the
186 // request to the plugin process (as it is processed through a render
188 int requestorProcessID() const { return m_requestorProcessID; }
189 void setRequestorProcessID(int requestorProcessID) { m_requestorProcessID = requestorProcessID; }
191 // Allows the request to be matched up with its app cache host.
192 int appCacheHostID() const { return m_appCacheHostID; }
193 void setAppCacheHostID(int id) { m_appCacheHostID = id; }
195 // True if request was user initiated.
196 bool hasUserGesture() const { return m_hasUserGesture; }
197 void setHasUserGesture(bool hasUserGesture) { m_hasUserGesture = hasUserGesture; }
199 // True if request should be downloaded to file.
200 bool downloadToFile() const { return m_downloadToFile; }
201 void setDownloadToFile(bool downloadToFile) { m_downloadToFile = downloadToFile; }
203 // Extra data associated with this request.
204 ExtraData* extraData() const { return m_extraData.get(); }
205 void setExtraData(PassRefPtr<ExtraData> extraData) { m_extraData = extraData; }
207 // What this request is for.
208 TargetType targetType() const { return m_targetType; }
209 void setTargetType(TargetType type) { m_targetType = type; }
211 bool cacheControlContainsNoCache();
212 bool cacheControlContainsNoStore();
213 bool hasCacheValidatorFields();
215 static double defaultTimeoutInterval(); // May return 0 when using platform default.
216 static void setDefaultTimeoutInterval(double);
218 static bool compare(const ResourceRequest&, const ResourceRequest&);
221 void initialize(const KURL& url, ResourceRequestCachePolicy cachePolicy);
224 ResourceRequestCachePolicy m_cachePolicy;
225 double m_timeoutInterval; // 0 is a magic value for platform default on platforms that have one.
226 KURL m_firstPartyForCookies;
227 AtomicString m_httpMethod;
228 HTTPHeaderMap m_httpHeaderFields;
229 RefPtr<FormData> m_httpBody;
230 bool m_allowStoredCredentials : 1;
231 bool m_reportUploadProgress : 1;
232 bool m_reportLoadTiming : 1;
233 bool m_reportRawHeaders : 1;
234 bool m_hasUserGesture : 1;
235 bool m_downloadToFile : 1;
236 ResourceLoadPriority m_priority;
237 int m_intraPriorityValue;
239 int m_requestorProcessID;
240 int m_appCacheHostID;
241 RefPtr<ExtraData> m_extraData;
242 TargetType m_targetType;
243 ReferrerPolicy m_referrerPolicy;
244 CacheControlHeader m_cacheControlHeader;
246 static double s_defaultTimeoutInterval;
249 bool equalIgnoringHeaderFields(const ResourceRequest&, const ResourceRequest&);
251 inline bool operator==(const ResourceRequest& a, const ResourceRequest& b) { return ResourceRequest::compare(a, b); }
252 inline bool operator!=(ResourceRequest& a, const ResourceRequest& b) { return !(a == b); }
254 struct CrossThreadResourceRequestData {
255 WTF_MAKE_NONCOPYABLE(CrossThreadResourceRequestData); WTF_MAKE_FAST_ALLOCATED;
257 CrossThreadResourceRequestData() { }
260 ResourceRequestCachePolicy m_cachePolicy;
261 double m_timeoutInterval;
262 KURL m_firstPartyForCookies;
265 OwnPtr<CrossThreadHTTPHeaderMapData> m_httpHeaders;
266 RefPtr<FormData> m_httpBody;
267 bool m_allowStoredCredentials;
268 bool m_reportUploadProgress;
269 bool m_hasUserGesture;
270 bool m_downloadToFile;
271 ResourceLoadPriority m_priority;
272 int m_intraPriorityValue;
274 int m_requestorProcessID;
275 int m_appCacheHostID;
276 ResourceRequest::TargetType m_targetType;
277 ReferrerPolicy m_referrerPolicy;
280 unsigned initializeMaximumHTTPConnectionCountPerHost();
282 } // namespace WebCore
284 #endif // ResourceRequest_h