1 /* 2 * Copyright (C) 2003, 2006 Apple Computer, Inc. All rights reserved. 3 * Copyright (C) 2006 Samuel Weinig <sam.weinig (at) 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 "wtf/OwnPtr.h" 38 39 namespace WebCore { 40 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 46 ReloadBypassingCache, // end-to-end reload 47 }; 48 49 struct CrossThreadResourceRequestData; 50 51 class PLATFORM_EXPORT ResourceRequest { 52 WTF_MAKE_FAST_ALLOCATED; 53 public: 54 // The type of this ResourceRequest, based on how the resource will be used. 55 enum TargetType { 56 TargetIsMainFrame, 57 TargetIsSubframe, 58 TargetIsSubresource, // Resource is a generic subresource. (Generally a specific type should be specified) 59 TargetIsStyleSheet, 60 TargetIsScript, 61 TargetIsFont, 62 TargetIsImage, 63 TargetIsObject, 64 TargetIsMedia, 65 TargetIsWorker, 66 TargetIsSharedWorker, 67 TargetIsPrefetch, 68 TargetIsFavicon, 69 TargetIsXHR, 70 TargetIsTextTrack, 71 TargetIsPing, 72 TargetIsServiceWorker, 73 TargetIsUnspecified, 74 }; 75 76 class ExtraData : public RefCounted<ExtraData> { 77 public: 78 virtual ~ExtraData() { } 79 }; 80 81 ResourceRequest() 82 { 83 initialize(KURL(), UseProtocolCachePolicy); 84 } 85 86 ResourceRequest(const String& urlString) 87 { 88 initialize(KURL(ParsedURLString, urlString), UseProtocolCachePolicy); 89 } 90 91 ResourceRequest(const KURL& url) 92 { 93 initialize(url, UseProtocolCachePolicy); 94 } 95 96 ResourceRequest(const KURL& url, const Referrer& referrer, ResourceRequestCachePolicy cachePolicy = UseProtocolCachePolicy) 97 { 98 initialize(url, cachePolicy); 99 setHTTPReferrer(referrer); 100 } 101 102 static PassOwnPtr<ResourceRequest> adopt(PassOwnPtr<CrossThreadResourceRequestData>); 103 104 // Gets a copy of the data suitable for passing to another thread. 105 PassOwnPtr<CrossThreadResourceRequestData> copyData() const; 106 107 bool isNull() const; 108 bool isEmpty() const; 109 110 const KURL& url() const; 111 void setURL(const KURL& url); 112 113 void removeCredentials(); 114 115 ResourceRequestCachePolicy cachePolicy() const; 116 void setCachePolicy(ResourceRequestCachePolicy cachePolicy); 117 118 double timeoutInterval() const; // May return 0 when using platform default. 119 void setTimeoutInterval(double timeoutInterval); 120 121 const KURL& firstPartyForCookies() const; 122 void setFirstPartyForCookies(const KURL& firstPartyForCookies); 123 124 const AtomicString& httpMethod() const; 125 void setHTTPMethod(const AtomicString&); 126 127 const HTTPHeaderMap& httpHeaderFields() const; 128 const AtomicString& httpHeaderField(const AtomicString& name) const; 129 const AtomicString& httpHeaderField(const char* name) const; 130 void setHTTPHeaderField(const AtomicString& name, const AtomicString& value); 131 void setHTTPHeaderField(const char* name, const AtomicString& value); 132 void addHTTPHeaderField(const AtomicString& name, const AtomicString& value); 133 void addHTTPHeaderFields(const HTTPHeaderMap& headerFields); 134 void clearHTTPHeaderField(const AtomicString& name); 135 136 void clearHTTPAuthorization(); 137 138 const AtomicString& httpContentType() const { return httpHeaderField("Content-Type"); } 139 void setHTTPContentType(const AtomicString& httpContentType) { setHTTPHeaderField("Content-Type", httpContentType); } 140 141 const AtomicString& httpReferrer() const { return httpHeaderField("Referer"); } 142 ReferrerPolicy referrerPolicy() const { return m_referrerPolicy; } 143 void setHTTPReferrer(const Referrer& httpReferrer) { setHTTPHeaderField("Referer", httpReferrer.referrer); m_referrerPolicy = httpReferrer.referrerPolicy; } 144 void clearHTTPReferrer(); 145 146 const AtomicString& httpOrigin() const { return httpHeaderField("Origin"); } 147 void setHTTPOrigin(const AtomicString& httpOrigin) { setHTTPHeaderField("Origin", httpOrigin); } 148 void clearHTTPOrigin(); 149 150 const AtomicString& httpUserAgent() const { return httpHeaderField("User-Agent"); } 151 void setHTTPUserAgent(const AtomicString& httpUserAgent) { setHTTPHeaderField("User-Agent", httpUserAgent); } 152 void clearHTTPUserAgent(); 153 154 const AtomicString& httpAccept() const { return httpHeaderField("Accept"); } 155 void setHTTPAccept(const AtomicString& httpAccept) { setHTTPHeaderField("Accept", httpAccept); } 156 157 FormData* httpBody() const; 158 void setHTTPBody(PassRefPtr<FormData> httpBody); 159 160 bool allowStoredCredentials() const; 161 void setAllowStoredCredentials(bool allowCredentials); 162 163 ResourceLoadPriority priority() const; 164 void setPriority(ResourceLoadPriority, int intraPriorityValue = 0); 165 166 bool isConditional() const; 167 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; } 172 173 // Whether actual headers being sent/received should be collected and reported for the request. 174 bool reportRawHeaders() const { return m_reportRawHeaders; } 175 void setReportRawHeaders(bool reportRawHeaders) { m_reportRawHeaders = reportRawHeaders; } 176 177 // Allows the request to be matched up with its requestor. 178 int requestorID() const { return m_requestorID; } 179 void setRequestorID(int requestorID) { m_requestorID = requestorID; } 180 181 // The process id of the process from which this request originated. In 182 // the case of out-of-process plugins, this allows to link back the 183 // request to the plugin process (as it is processed through a render 184 // view process). 185 int requestorProcessID() const { return m_requestorProcessID; } 186 void setRequestorProcessID(int requestorProcessID) { m_requestorProcessID = requestorProcessID; } 187 188 // Allows the request to be matched up with its app cache host. 189 int appCacheHostID() const { return m_appCacheHostID; } 190 void setAppCacheHostID(int id) { m_appCacheHostID = id; } 191 192 // True if request was user initiated. 193 bool hasUserGesture() const { return m_hasUserGesture; } 194 void setHasUserGesture(bool hasUserGesture) { m_hasUserGesture = hasUserGesture; } 195 196 // True if request should be downloaded to file. 197 bool downloadToFile() const { return m_downloadToFile; } 198 void setDownloadToFile(bool downloadToFile) { m_downloadToFile = downloadToFile; } 199 200 // Extra data associated with this request. 201 ExtraData* extraData() const { return m_extraData.get(); } 202 void setExtraData(PassRefPtr<ExtraData> extraData) { m_extraData = extraData; } 203 204 // What this request is for. 205 TargetType targetType() const { return m_targetType; } 206 void setTargetType(TargetType type) { m_targetType = type; } 207 208 bool cacheControlContainsNoCache(); 209 bool cacheControlContainsNoStore(); 210 bool hasCacheValidatorFields(); 211 212 static double defaultTimeoutInterval(); // May return 0 when using platform default. 213 static void setDefaultTimeoutInterval(double); 214 215 static bool compare(const ResourceRequest&, const ResourceRequest&); 216 217 private: 218 void initialize(const KURL& url, ResourceRequestCachePolicy cachePolicy); 219 220 KURL m_url; 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_allowStoredCredentials : 1; 228 bool m_reportUploadProgress : 1; 229 bool m_reportRawHeaders : 1; 230 bool m_hasUserGesture : 1; 231 bool m_downloadToFile : 1; 232 ResourceLoadPriority m_priority; 233 int m_intraPriorityValue; 234 int m_requestorID; 235 int m_requestorProcessID; 236 int m_appCacheHostID; 237 RefPtr<ExtraData> m_extraData; 238 TargetType m_targetType; 239 ReferrerPolicy m_referrerPolicy; 240 CacheControlHeader m_cacheControlHeader; 241 242 static double s_defaultTimeoutInterval; 243 }; 244 245 bool equalIgnoringHeaderFields(const ResourceRequest&, const ResourceRequest&); 246 247 inline bool operator==(const ResourceRequest& a, const ResourceRequest& b) { return ResourceRequest::compare(a, b); } 248 inline bool operator!=(ResourceRequest& a, const ResourceRequest& b) { return !(a == b); } 249 250 struct CrossThreadResourceRequestData { 251 WTF_MAKE_NONCOPYABLE(CrossThreadResourceRequestData); WTF_MAKE_FAST_ALLOCATED; 252 public: 253 CrossThreadResourceRequestData() { } 254 KURL m_url; 255 256 ResourceRequestCachePolicy m_cachePolicy; 257 double m_timeoutInterval; 258 KURL m_firstPartyForCookies; 259 260 String m_httpMethod; 261 OwnPtr<CrossThreadHTTPHeaderMapData> m_httpHeaders; 262 RefPtr<FormData> m_httpBody; 263 bool m_allowStoredCredentials; 264 bool m_reportUploadProgress; 265 bool m_hasUserGesture; 266 bool m_downloadToFile; 267 ResourceLoadPriority m_priority; 268 int m_intraPriorityValue; 269 int m_requestorID; 270 int m_requestorProcessID; 271 int m_appCacheHostID; 272 ResourceRequest::TargetType m_targetType; 273 ReferrerPolicy m_referrerPolicy; 274 }; 275 276 unsigned initializeMaximumHTTPConnectionCountPerHost(); 277 278 } // namespace WebCore 279 280 #endif // ResourceRequest_h 281