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/HTTPParsers.h" 34 #include "platform/network/ResourceLoadPriority.h" 35 #include "platform/weborigin/KURL.h" 36 #include "platform/weborigin/Referrer.h" 37 #include "public/platform/WebURLRequest.h" 38 #include "wtf/OwnPtr.h" 39 40 namespace blink { 41 42 enum ResourceRequestCachePolicy { 43 UseProtocolCachePolicy, // normal load 44 ReloadIgnoringCacheData, // reload 45 ReturnCacheDataElseLoad, // back/forward or encoding change - allow stale data 46 ReturnCacheDataDontLoad, // results of a post - allow stale data and only use cache 47 ReloadBypassingCache, // end-to-end reload 48 }; 49 50 struct CrossThreadResourceRequestData; 51 52 class PLATFORM_EXPORT ResourceRequest { 53 WTF_MAKE_FAST_ALLOCATED; 54 public: 55 class ExtraData : public RefCounted<ExtraData> { 56 public: ~ExtraData()57 virtual ~ExtraData() { } 58 }; 59 ResourceRequest()60 ResourceRequest() 61 { 62 initialize(KURL(), UseProtocolCachePolicy); 63 } 64 ResourceRequest(const String & urlString)65 ResourceRequest(const String& urlString) 66 { 67 initialize(KURL(ParsedURLString, urlString), UseProtocolCachePolicy); 68 } 69 ResourceRequest(const KURL & url)70 ResourceRequest(const KURL& url) 71 { 72 initialize(url, UseProtocolCachePolicy); 73 } 74 75 ResourceRequest(const KURL& url, const Referrer& referrer, ResourceRequestCachePolicy cachePolicy = UseProtocolCachePolicy) 76 { 77 initialize(url, cachePolicy); 78 setHTTPReferrer(referrer); 79 } 80 81 static PassOwnPtr<ResourceRequest> adopt(PassOwnPtr<CrossThreadResourceRequestData>); 82 83 // Gets a copy of the data suitable for passing to another thread. 84 PassOwnPtr<CrossThreadResourceRequestData> copyData() const; 85 86 bool isNull() const; 87 bool isEmpty() const; 88 89 const KURL& url() const; 90 void setURL(const KURL& url); 91 92 void removeCredentials(); 93 94 ResourceRequestCachePolicy cachePolicy() const; 95 void setCachePolicy(ResourceRequestCachePolicy cachePolicy); 96 97 double timeoutInterval() const; // May return 0 when using platform default. 98 void setTimeoutInterval(double timeoutInterval); 99 100 const KURL& firstPartyForCookies() const; 101 void setFirstPartyForCookies(const KURL& firstPartyForCookies); 102 103 const AtomicString& httpMethod() const; 104 void setHTTPMethod(const AtomicString&); 105 106 const HTTPHeaderMap& httpHeaderFields() const; 107 const AtomicString& httpHeaderField(const AtomicString& name) const; 108 const AtomicString& httpHeaderField(const char* name) const; 109 void setHTTPHeaderField(const AtomicString& name, const AtomicString& value); 110 void setHTTPHeaderField(const char* name, const AtomicString& value); 111 void addHTTPHeaderField(const AtomicString& name, const AtomicString& value); 112 void addHTTPHeaderFields(const HTTPHeaderMap& headerFields); 113 void clearHTTPHeaderField(const AtomicString& name); 114 115 void clearHTTPAuthorization(); 116 httpContentType()117 const AtomicString& httpContentType() const { return httpHeaderField("Content-Type"); } setHTTPContentType(const AtomicString & httpContentType)118 void setHTTPContentType(const AtomicString& httpContentType) { setHTTPHeaderField("Content-Type", httpContentType); } 119 httpReferrer()120 const AtomicString& httpReferrer() const { return httpHeaderField("Referer"); } referrerPolicy()121 ReferrerPolicy referrerPolicy() const { return m_referrerPolicy; } setHTTPReferrer(const Referrer & httpReferrer)122 void setHTTPReferrer(const Referrer& httpReferrer) { setHTTPHeaderField("Referer", httpReferrer.referrer); m_referrerPolicy = httpReferrer.referrerPolicy; } 123 void clearHTTPReferrer(); 124 httpOrigin()125 const AtomicString& httpOrigin() const { return httpHeaderField("Origin"); } setHTTPOrigin(const AtomicString & httpOrigin)126 void setHTTPOrigin(const AtomicString& httpOrigin) { setHTTPHeaderField("Origin", httpOrigin); } 127 void clearHTTPOrigin(); 128 void addHTTPOriginIfNeeded(const AtomicString& origin); 129 httpUserAgent()130 const AtomicString& httpUserAgent() const { return httpHeaderField("User-Agent"); } setHTTPUserAgent(const AtomicString & httpUserAgent)131 void setHTTPUserAgent(const AtomicString& httpUserAgent) { setHTTPHeaderField("User-Agent", httpUserAgent); } 132 void clearHTTPUserAgent(); 133 httpAccept()134 const AtomicString& httpAccept() const { return httpHeaderField("Accept"); } setHTTPAccept(const AtomicString & httpAccept)135 void setHTTPAccept(const AtomicString& httpAccept) { setHTTPHeaderField("Accept", httpAccept); } 136 137 FormData* httpBody() const; 138 void setHTTPBody(PassRefPtr<FormData> httpBody); 139 140 bool allowStoredCredentials() const; 141 void setAllowStoredCredentials(bool allowCredentials); 142 143 ResourceLoadPriority priority() const; 144 void setPriority(ResourceLoadPriority, int intraPriorityValue = 0); 145 146 bool isConditional() const; 147 148 // Whether the associated ResourceHandleClient needs to be notified of 149 // upload progress made for that resource. reportUploadProgress()150 bool reportUploadProgress() const { return m_reportUploadProgress; } setReportUploadProgress(bool reportUploadProgress)151 void setReportUploadProgress(bool reportUploadProgress) { m_reportUploadProgress = reportUploadProgress; } 152 153 // Whether actual headers being sent/received should be collected and reported for the request. reportRawHeaders()154 bool reportRawHeaders() const { return m_reportRawHeaders; } setReportRawHeaders(bool reportRawHeaders)155 void setReportRawHeaders(bool reportRawHeaders) { m_reportRawHeaders = reportRawHeaders; } 156 157 // Allows the request to be matched up with its requestor. requestorID()158 int requestorID() const { return m_requestorID; } setRequestorID(int requestorID)159 void setRequestorID(int requestorID) { m_requestorID = requestorID; } 160 161 // The process id of the process from which this request originated. In 162 // the case of out-of-process plugins, this allows to link back the 163 // request to the plugin process (as it is processed through a render 164 // view process). requestorProcessID()165 int requestorProcessID() const { return m_requestorProcessID; } setRequestorProcessID(int requestorProcessID)166 void setRequestorProcessID(int requestorProcessID) { m_requestorProcessID = requestorProcessID; } 167 168 // Allows the request to be matched up with its app cache host. appCacheHostID()169 int appCacheHostID() const { return m_appCacheHostID; } setAppCacheHostID(int id)170 void setAppCacheHostID(int id) { m_appCacheHostID = id; } 171 172 // True if request was user initiated. hasUserGesture()173 bool hasUserGesture() const { return m_hasUserGesture; } setHasUserGesture(bool hasUserGesture)174 void setHasUserGesture(bool hasUserGesture) { m_hasUserGesture = hasUserGesture; } 175 176 // True if request should be downloaded to file. downloadToFile()177 bool downloadToFile() const { return m_downloadToFile; } setDownloadToFile(bool downloadToFile)178 void setDownloadToFile(bool downloadToFile) { m_downloadToFile = downloadToFile; } 179 180 // True if the request should not be handled by the ServiceWorker. skipServiceWorker()181 bool skipServiceWorker() const { return m_skipServiceWorker; } setSkipServiceWorker(bool skipServiceWorker)182 void setSkipServiceWorker(bool skipServiceWorker) { m_skipServiceWorker = skipServiceWorker; } 183 184 // Extra data associated with this request. extraData()185 ExtraData* extraData() const { return m_extraData.get(); } setExtraData(PassRefPtr<ExtraData> extraData)186 void setExtraData(PassRefPtr<ExtraData> extraData) { m_extraData = extraData; } 187 requestContext()188 blink::WebURLRequest::RequestContext requestContext() const { return m_requestContext; } setRequestContext(blink::WebURLRequest::RequestContext context)189 void setRequestContext(blink::WebURLRequest::RequestContext context) { m_requestContext = context; } 190 frameType()191 blink::WebURLRequest::FrameType frameType() const { return m_frameType; } setFrameType(blink::WebURLRequest::FrameType frameType)192 void setFrameType(blink::WebURLRequest::FrameType frameType) { m_frameType = frameType; } 193 194 bool cacheControlContainsNoCache() const; 195 bool cacheControlContainsNoStore() const; 196 bool hasCacheValidatorFields() const; 197 198 static double defaultTimeoutInterval(); // May return 0 when using platform default. 199 static void setDefaultTimeoutInterval(double); 200 201 static bool compare(const ResourceRequest&, const ResourceRequest&); 202 203 private: 204 void initialize(const KURL& url, ResourceRequestCachePolicy cachePolicy); 205 206 const CacheControlHeader& cacheControlHeader() const; 207 208 KURL m_url; 209 ResourceRequestCachePolicy m_cachePolicy; 210 double m_timeoutInterval; // 0 is a magic value for platform default on platforms that have one. 211 KURL m_firstPartyForCookies; 212 AtomicString m_httpMethod; 213 HTTPHeaderMap m_httpHeaderFields; 214 RefPtr<FormData> m_httpBody; 215 bool m_allowStoredCredentials : 1; 216 bool m_reportUploadProgress : 1; 217 bool m_reportRawHeaders : 1; 218 bool m_hasUserGesture : 1; 219 bool m_downloadToFile : 1; 220 bool m_skipServiceWorker : 1; 221 ResourceLoadPriority m_priority; 222 int m_intraPriorityValue; 223 int m_requestorID; 224 int m_requestorProcessID; 225 int m_appCacheHostID; 226 RefPtr<ExtraData> m_extraData; 227 blink::WebURLRequest::RequestContext m_requestContext; 228 blink::WebURLRequest::FrameType m_frameType; 229 ReferrerPolicy m_referrerPolicy; 230 231 mutable CacheControlHeader m_cacheControlHeaderCache; 232 233 static double s_defaultTimeoutInterval; 234 }; 235 236 bool equalIgnoringHeaderFields(const ResourceRequest&, const ResourceRequest&); 237 238 inline bool operator==(const ResourceRequest& a, const ResourceRequest& b) { return ResourceRequest::compare(a, b); } 239 inline bool operator!=(ResourceRequest& a, const ResourceRequest& b) { return !(a == b); } 240 241 struct CrossThreadResourceRequestData { 242 WTF_MAKE_NONCOPYABLE(CrossThreadResourceRequestData); WTF_MAKE_FAST_ALLOCATED; 243 public: CrossThreadResourceRequestDataCrossThreadResourceRequestData244 CrossThreadResourceRequestData() { } 245 KURL m_url; 246 247 ResourceRequestCachePolicy m_cachePolicy; 248 double m_timeoutInterval; 249 KURL m_firstPartyForCookies; 250 251 String m_httpMethod; 252 OwnPtr<CrossThreadHTTPHeaderMapData> m_httpHeaders; 253 RefPtr<FormData> m_httpBody; 254 bool m_allowStoredCredentials; 255 bool m_reportUploadProgress; 256 bool m_hasUserGesture; 257 bool m_downloadToFile; 258 bool m_skipServiceWorker; 259 ResourceLoadPriority m_priority; 260 int m_intraPriorityValue; 261 int m_requestorID; 262 int m_requestorProcessID; 263 int m_appCacheHostID; 264 blink::WebURLRequest::RequestContext m_requestContext; 265 blink::WebURLRequest::FrameType m_frameType; 266 ReferrerPolicy m_referrerPolicy; 267 }; 268 269 unsigned initializeMaximumHTTPConnectionCountPerHost(); 270 271 } // namespace blink 272 273 #endif // ResourceRequest_h 274