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 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 ResourceRequestBase_h 29 #define ResourceRequestBase_h 30 31 #include "FormData.h" 32 #include "HTTPHeaderMap.h" 33 #include "KURL.h" 34 #include "ResourceLoadPriority.h" 35 36 #include <wtf/OwnPtr.h> 37 38 namespace WebCore { 39 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 45 }; 46 47 class ResourceRequest; 48 struct CrossThreadResourceRequestData; 49 50 // Do not use this type directly. Use ResourceRequest instead. 51 class ResourceRequestBase { 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 TargetIsFontResource, 62 TargetIsImage, 63 TargetIsObject, 64 TargetIsMedia, 65 TargetIsWorker, 66 TargetIsSharedWorker, 67 TargetIsPrefetch, 68 TargetIsFavicon, 69 }; 70 71 static PassOwnPtr<ResourceRequest> adopt(PassOwnPtr<CrossThreadResourceRequestData>); 72 73 // Gets a copy of the data suitable for passing to another thread. 74 PassOwnPtr<CrossThreadResourceRequestData> copyData() const; 75 76 bool isNull() const; 77 bool isEmpty() const; 78 79 const KURL& url() const; 80 void setURL(const KURL& url); 81 82 void removeCredentials(); 83 84 ResourceRequestCachePolicy cachePolicy() const; 85 void setCachePolicy(ResourceRequestCachePolicy cachePolicy); 86 87 double timeoutInterval() const; // May return 0 when using platform default. 88 void setTimeoutInterval(double timeoutInterval); 89 90 const KURL& firstPartyForCookies() const; 91 void setFirstPartyForCookies(const KURL& firstPartyForCookies); 92 93 const String& httpMethod() const; 94 void setHTTPMethod(const String& httpMethod); 95 96 const HTTPHeaderMap& httpHeaderFields() const; 97 String httpHeaderField(const AtomicString& name) const; 98 String httpHeaderField(const char* name) const; 99 void setHTTPHeaderField(const AtomicString& name, const String& value); 100 void setHTTPHeaderField(const char* name, const String& value); 101 void addHTTPHeaderField(const AtomicString& name, const String& value); 102 void addHTTPHeaderFields(const HTTPHeaderMap& headerFields); 103 104 void clearHTTPAuthorization(); 105 106 String httpContentType() const { return httpHeaderField("Content-Type"); } 107 void setHTTPContentType(const String& httpContentType) { setHTTPHeaderField("Content-Type", httpContentType); } 108 109 String httpReferrer() const { return httpHeaderField("Referer"); } 110 void setHTTPReferrer(const String& httpReferrer) { setHTTPHeaderField("Referer", httpReferrer); } 111 void clearHTTPReferrer(); 112 113 String httpOrigin() const { return httpHeaderField("Origin"); } 114 void setHTTPOrigin(const String& httpOrigin) { setHTTPHeaderField("Origin", httpOrigin); } 115 void clearHTTPOrigin(); 116 117 String httpUserAgent() const { return httpHeaderField("User-Agent"); } 118 void setHTTPUserAgent(const String& httpUserAgent) { setHTTPHeaderField("User-Agent", httpUserAgent); } 119 120 String httpAccept() const { return httpHeaderField("Accept"); } 121 void setHTTPAccept(const String& httpAccept) { setHTTPHeaderField("Accept", httpAccept); } 122 123 void setResponseContentDispositionEncodingFallbackArray(const String& encoding1, const String& encoding2 = String(), const String& encoding3 = String()); 124 125 FormData* httpBody() const; 126 void setHTTPBody(PassRefPtr<FormData> httpBody); 127 128 bool allowCookies() const; 129 void setAllowCookies(bool allowCookies); 130 131 ResourceLoadPriority priority() const; 132 void setPriority(ResourceLoadPriority); 133 134 bool isConditional() const; 135 136 // Whether the associated ResourceHandleClient needs to be notified of 137 // upload progress made for that resource. 138 bool reportUploadProgress() const { return m_reportUploadProgress; } 139 void setReportUploadProgress(bool reportUploadProgress) { m_reportUploadProgress = reportUploadProgress; } 140 141 // Whether the timing information should be collected for the request. 142 bool reportLoadTiming() const { return m_reportLoadTiming; } 143 void setReportLoadTiming(bool reportLoadTiming) { m_reportLoadTiming = reportLoadTiming; } 144 145 // Whether actual headers being sent/received should be collected and reported for the request. 146 bool reportRawHeaders() const { return m_reportRawHeaders; } 147 void setReportRawHeaders(bool reportRawHeaders) { m_reportRawHeaders = reportRawHeaders; } 148 149 // What this request is for. 150 // FIXME: This should be moved out of ResourceRequestBase, <https://bugs.webkit.org/show_bug.cgi?id=48483>. 151 TargetType targetType() const { return m_targetType; } 152 void setTargetType(TargetType type) { m_targetType = type; } 153 154 static double defaultTimeoutInterval(); // May return 0 when using platform default. 155 static void setDefaultTimeoutInterval(double); 156 157 static bool compare(const ResourceRequest&, const ResourceRequest&); 158 159 protected: 160 // Used when ResourceRequest is initialized from a platform representation of the request 161 ResourceRequestBase() 162 : m_resourceRequestUpdated(false) 163 , m_platformRequestUpdated(true) 164 , m_reportUploadProgress(false) 165 , m_reportLoadTiming(false) 166 , m_reportRawHeaders(false) 167 , m_priority(ResourceLoadPriorityLow) 168 , m_targetType(TargetIsSubresource) 169 { 170 } 171 172 ResourceRequestBase(const KURL& url, ResourceRequestCachePolicy policy) 173 : m_url(url) 174 , m_cachePolicy(policy) 175 , m_timeoutInterval(s_defaultTimeoutInterval) 176 , m_httpMethod("GET") 177 , m_allowCookies(true) 178 , m_resourceRequestUpdated(true) 179 , m_platformRequestUpdated(false) 180 , m_reportUploadProgress(false) 181 , m_reportLoadTiming(false) 182 , m_reportRawHeaders(false) 183 , m_priority(ResourceLoadPriorityLow) 184 , m_targetType(TargetIsSubresource) 185 { 186 } 187 188 void updatePlatformRequest() const; 189 void updateResourceRequest() const; 190 191 // The ResourceRequest subclass may "shadow" this method to compare platform specific fields 192 static bool platformCompare(const ResourceRequest&, const ResourceRequest&) { return true; } 193 194 KURL m_url; 195 196 ResourceRequestCachePolicy m_cachePolicy; 197 double m_timeoutInterval; // 0 is a magic value for platform default on platforms that have one. 198 KURL m_firstPartyForCookies; 199 String m_httpMethod; 200 HTTPHeaderMap m_httpHeaderFields; 201 Vector<String> m_responseContentDispositionEncodingFallbackArray; 202 RefPtr<FormData> m_httpBody; 203 bool m_allowCookies; 204 mutable bool m_resourceRequestUpdated; 205 mutable bool m_platformRequestUpdated; 206 bool m_reportUploadProgress; 207 bool m_reportLoadTiming; 208 bool m_reportRawHeaders; 209 ResourceLoadPriority m_priority; 210 TargetType m_targetType; 211 212 private: 213 const ResourceRequest& asResourceRequest() const; 214 215 static double s_defaultTimeoutInterval; 216 }; 217 218 bool equalIgnoringHeaderFields(const ResourceRequestBase&, const ResourceRequestBase&); 219 220 inline bool operator==(const ResourceRequest& a, const ResourceRequest& b) { return ResourceRequestBase::compare(a, b); } 221 inline bool operator!=(ResourceRequest& a, const ResourceRequest& b) { return !(a == b); } 222 223 struct CrossThreadResourceRequestDataBase { 224 WTF_MAKE_NONCOPYABLE(CrossThreadResourceRequestDataBase); WTF_MAKE_FAST_ALLOCATED; 225 public: 226 CrossThreadResourceRequestDataBase() { } 227 KURL m_url; 228 229 ResourceRequestCachePolicy m_cachePolicy; 230 double m_timeoutInterval; 231 KURL m_firstPartyForCookies; 232 233 String m_httpMethod; 234 OwnPtr<CrossThreadHTTPHeaderMapData> m_httpHeaders; 235 Vector<String> m_responseContentDispositionEncodingFallbackArray; 236 RefPtr<FormData> m_httpBody; 237 bool m_allowCookies; 238 ResourceLoadPriority m_priority; 239 ResourceRequestBase::TargetType m_targetType; 240 }; 241 242 unsigned initializeMaximumHTTPConnectionCountPerHost(); 243 244 } // namespace WebCore 245 246 #endif // ResourceRequestBase_h 247