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/ResourceLoadPriority.h"
34 #include "platform/weborigin/KURL.h"
35 #include "platform/weborigin/Referrer.h"
36 #include "wtf/OwnPtr.h"
40 enum ResourceRequestCachePolicy {
41 UseProtocolCachePolicy, // normal load
42 ReloadIgnoringCacheData, // reload
43 ReturnCacheDataElseLoad, // back/forward or encoding change - allow stale data
44 ReturnCacheDataDontLoad // results of a post - allow stale data and only use cache
47 struct CrossThreadResourceRequestData;
49 class PLATFORM_EXPORT ResourceRequest {
50 WTF_MAKE_FAST_ALLOCATED;
52 // The type of this ResourceRequest, based on how the resource will be used.
56 TargetIsSubresource, // Resource is a generic subresource. (Generally a specific type should be specified)
70 TargetIsServiceWorker,
74 class ExtraData : public RefCounted<ExtraData> {
76 virtual ~ExtraData() { }
81 initialize(KURL(), UseProtocolCachePolicy);
84 ResourceRequest(const String& urlString)
86 initialize(KURL(ParsedURLString, urlString), UseProtocolCachePolicy);
89 ResourceRequest(const KURL& url)
91 initialize(url, UseProtocolCachePolicy);
94 ResourceRequest(const KURL& url, const Referrer& referrer, ResourceRequestCachePolicy cachePolicy = UseProtocolCachePolicy)
96 initialize(url, cachePolicy);
97 setHTTPReferrer(referrer);
100 static PassOwnPtr<ResourceRequest> adopt(PassOwnPtr<CrossThreadResourceRequestData>);
102 // Gets a copy of the data suitable for passing to another thread.
103 PassOwnPtr<CrossThreadResourceRequestData> copyData() const;
106 bool isEmpty() const;
108 const KURL& url() const;
109 void setURL(const KURL& url);
111 void removeCredentials();
113 ResourceRequestCachePolicy cachePolicy() const;
114 void setCachePolicy(ResourceRequestCachePolicy cachePolicy);
116 double timeoutInterval() const; // May return 0 when using platform default.
117 void setTimeoutInterval(double timeoutInterval);
119 const KURL& firstPartyForCookies() const;
120 void setFirstPartyForCookies(const KURL& firstPartyForCookies);
122 const AtomicString& httpMethod() const;
123 void setHTTPMethod(const AtomicString&);
125 const HTTPHeaderMap& httpHeaderFields() const;
126 const AtomicString& httpHeaderField(const AtomicString& name) const;
127 const AtomicString& httpHeaderField(const char* name) const;
128 void setHTTPHeaderField(const AtomicString& name, const AtomicString& value);
129 void setHTTPHeaderField(const char* name, const AtomicString& value);
130 void addHTTPHeaderField(const AtomicString& name, const AtomicString& value);
131 void addHTTPHeaderFields(const HTTPHeaderMap& headerFields);
132 void clearHTTPHeaderField(const AtomicString& name);
134 void clearHTTPAuthorization();
136 const AtomicString& httpContentType() const { return httpHeaderField("Content-Type"); }
137 void setHTTPContentType(const AtomicString& httpContentType) { setHTTPHeaderField("Content-Type", httpContentType); }
138 void clearHTTPContentType();
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); }
155 void clearHTTPAccept();
157 FormData* httpBody() const;
158 void setHTTPBody(PassRefPtr<FormData> httpBody);
160 bool allowCookies() const;
161 void setAllowCookies(bool allowCookies);
163 ResourceLoadPriority priority() const;
164 void setPriority(ResourceLoadPriority);
166 bool isConditional() const;
168 // Whether the associated ResourceHandleClient needs to be notified of
169 // upload progress made for that resource.
170 bool reportUploadProgress() const { return m_reportUploadProgress; }
171 void setReportUploadProgress(bool reportUploadProgress) { m_reportUploadProgress = reportUploadProgress; }
173 // Whether the timing information should be collected for the request.
174 bool reportLoadTiming() const { return m_reportLoadTiming; }
175 void setReportLoadTiming(bool reportLoadTiming) { m_reportLoadTiming = reportLoadTiming; }
177 // Whether actual headers being sent/received should be collected and reported for the request.
178 bool reportRawHeaders() const { return m_reportRawHeaders; }
179 void setReportRawHeaders(bool reportRawHeaders) { m_reportRawHeaders = reportRawHeaders; }
181 // Allows the request to be matched up with its requestor.
182 int requestorID() const { return m_requestorID; }
183 void setRequestorID(int requestorID) { m_requestorID = requestorID; }
185 // The process id of the process from which this request originated. In
186 // the case of out-of-process plugins, this allows to link back the
187 // request to the plugin process (as it is processed through a render
189 int requestorProcessID() const { return m_requestorProcessID; }
190 void setRequestorProcessID(int requestorProcessID) { m_requestorProcessID = requestorProcessID; }
192 // Allows the request to be matched up with its app cache host.
193 int appCacheHostID() const { return m_appCacheHostID; }
194 void setAppCacheHostID(int id) { m_appCacheHostID = id; }
196 // True if request was user initiated.
197 bool hasUserGesture() const { return m_hasUserGesture; }
198 void setHasUserGesture(bool hasUserGesture) { m_hasUserGesture = hasUserGesture; }
200 // True if request should be downloaded to file.
201 bool downloadToFile() const { return m_downloadToFile; }
202 void setDownloadToFile(bool downloadToFile) { m_downloadToFile = downloadToFile; }
204 // Extra data associated with this request.
205 ExtraData* extraData() const { return m_extraData.get(); }
206 void setExtraData(PassRefPtr<ExtraData> extraData) { m_extraData = extraData; }
208 // What this request is for.
209 TargetType targetType() const { return m_targetType; }
210 void setTargetType(TargetType type) { m_targetType = type; }
212 static double defaultTimeoutInterval(); // May return 0 when using platform default.
213 static void setDefaultTimeoutInterval(double);
215 static bool compare(const ResourceRequest&, const ResourceRequest&);
218 void initialize(const KURL& url, ResourceRequestCachePolicy cachePolicy);
221 ResourceRequestCachePolicy m_cachePolicy;
222 double m_timeoutInterval; // 0 is a magic value for platform default on platforms that have one.
223 KURL m_firstPartyForCookies;
224 AtomicString m_httpMethod;
225 HTTPHeaderMap m_httpHeaderFields;
226 RefPtr<FormData> m_httpBody;
227 bool m_allowCookies : 1;
228 bool m_reportUploadProgress : 1;
229 bool m_reportLoadTiming : 1;
230 bool m_reportRawHeaders : 1;
231 bool m_hasUserGesture : 1;
232 bool m_downloadToFile : 1;
233 ResourceLoadPriority m_priority;
235 int m_requestorProcessID;
236 int m_appCacheHostID;
237 RefPtr<ExtraData> m_extraData;
238 TargetType m_targetType;
239 ReferrerPolicy m_referrerPolicy;
241 static double s_defaultTimeoutInterval;
244 bool equalIgnoringHeaderFields(const ResourceRequest&, const ResourceRequest&);
246 inline bool operator==(const ResourceRequest& a, const ResourceRequest& b) { return ResourceRequest::compare(a, b); }
247 inline bool operator!=(ResourceRequest& a, const ResourceRequest& b) { return !(a == b); }
249 struct CrossThreadResourceRequestData {
250 WTF_MAKE_NONCOPYABLE(CrossThreadResourceRequestData); WTF_MAKE_FAST_ALLOCATED;
252 CrossThreadResourceRequestData() { }
255 ResourceRequestCachePolicy m_cachePolicy;
256 double m_timeoutInterval;
257 KURL m_firstPartyForCookies;
260 OwnPtr<CrossThreadHTTPHeaderMapData> m_httpHeaders;
261 RefPtr<FormData> m_httpBody;
263 bool m_reportUploadProgress;
264 bool m_hasUserGesture;
265 bool m_downloadToFile;
266 ResourceLoadPriority m_priority;
268 int m_requestorProcessID;
269 int m_appCacheHostID;
270 ResourceRequest::TargetType m_targetType;
271 ReferrerPolicy m_referrerPolicy;
274 unsigned initializeMaximumHTTPConnectionCountPerHost();
276 } // namespace WebCore
278 #endif // ResourceRequest_h