2 * Copyright (C) 2003, 2006 Apple 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 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 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 ResourceRequestBase_h
29 #define ResourceRequestBase_h
32 #include "HTTPHeaderMap.h"
35 #include "ResourceLoadPriority.h"
39 enum class ResourceRequestCachePolicy : uint8_t {
40 UseProtocolCachePolicy, // normal load, equivalent to fetch "default" cache mode.
41 ReloadIgnoringCacheData, // reload, equivalent to fetch "reload"cache mode.
42 ReturnCacheDataElseLoad, // back/forward or encoding change - allow stale data, equivalent to fetch "force-cache" cache mode.
43 ReturnCacheDataDontLoad, // results of a post - allow stale data and only use cache, equivalent to fetch "only-if-cached" cache mode.
44 DoNotUseAnyCache, // Bypass the cache entirely, equivalent to fetch "no-store" cache mode.
45 RefreshAnyCacheData, // Serve cache data only if revalidated, equivalent to fetch "no-cache" mode.
48 enum HTTPBodyUpdatePolicy : uint8_t {
53 class ResourceRequest;
54 class ResourceResponse;
56 // Do not use this type directly. Use ResourceRequest instead.
57 class ResourceRequestBase {
58 WTF_MAKE_FAST_ALLOCATED;
60 ResourceRequest isolatedCopy() const;
61 WEBCORE_EXPORT void setAsIsolatedCopy(const ResourceRequest&);
63 WEBCORE_EXPORT bool isNull() const;
64 WEBCORE_EXPORT bool isEmpty() const;
66 WEBCORE_EXPORT const URL& url() const;
67 WEBCORE_EXPORT void setURL(const URL& url);
69 WEBCORE_EXPORT ResourceRequest redirectedRequest(const ResourceResponse&, bool shouldClearReferrerOnHTTPSToHTTPRedirect) const;
71 WEBCORE_EXPORT void removeCredentials();
73 WEBCORE_EXPORT ResourceRequestCachePolicy cachePolicy() const;
74 WEBCORE_EXPORT void setCachePolicy(ResourceRequestCachePolicy cachePolicy);
76 double timeoutInterval() const; // May return 0 when using platform default.
77 void setTimeoutInterval(double timeoutInterval);
79 WEBCORE_EXPORT const URL& firstPartyForCookies() const;
80 WEBCORE_EXPORT void setFirstPartyForCookies(const URL&);
82 // Same-Site cookies; see <https://tools.ietf.org/html/draft-ietf-httpbis-cookie-same-site-00#section-2.1>
83 // and <https://tools.ietf.org/html/draft-ietf-httpbis-cookie-same-site-00#section-5.2>.
84 // FIXME: For some reason the main resource request may be updated more than once. We start off as Unspecified
85 // to detect if we need to compute the same-site and top-site state or not. See FIXME in DocumentLoader::startLoadingMainResource().
86 enum class SameSiteDisposition { Unspecified, SameSite, CrossSite };
87 bool isSameSiteUnspecified() const { return m_sameSiteDisposition == SameSiteDisposition::Unspecified; }
88 WEBCORE_EXPORT bool isSameSite() const; // Whether this request's registrable domain matches the request's initiator's "site for cookies".
89 WEBCORE_EXPORT void setIsSameSite(bool);
90 WEBCORE_EXPORT bool isTopSite() const; // Whether this request is for a top-level navigation.
91 WEBCORE_EXPORT void setIsTopSite(bool);
93 WEBCORE_EXPORT const String& httpMethod() const;
94 WEBCORE_EXPORT void setHTTPMethod(const String& httpMethod);
96 WEBCORE_EXPORT const HTTPHeaderMap& httpHeaderFields() const;
97 WEBCORE_EXPORT void setHTTPHeaderFields(HTTPHeaderMap);
99 WEBCORE_EXPORT String httpHeaderField(const String& name) const;
100 WEBCORE_EXPORT String httpHeaderField(HTTPHeaderName) const;
101 WEBCORE_EXPORT void setHTTPHeaderField(const String& name, const String& value);
102 WEBCORE_EXPORT void setHTTPHeaderField(HTTPHeaderName, const String& value);
103 WEBCORE_EXPORT void addHTTPHeaderField(HTTPHeaderName, const String& value);
104 WEBCORE_EXPORT void addHTTPHeaderField(const String& name, const String& value);
105 WEBCORE_EXPORT void addHTTPHeaderFieldIfNotPresent(HTTPHeaderName, const String&);
107 WEBCORE_EXPORT bool hasHTTPHeaderField(HTTPHeaderName) const;
109 // Instead of passing a string literal to any of these functions, just use a HTTPHeaderName instead.
110 template<size_t length> String httpHeaderField(const char (&)[length]) const = delete;
111 template<size_t length> void setHTTPHeaderField(const char (&)[length], const String&) = delete;
112 template<size_t length> void addHTTPHeaderField(const char (&)[length], const String&) = delete;
114 WEBCORE_EXPORT void clearHTTPAuthorization();
116 WEBCORE_EXPORT String httpContentType() const;
117 WEBCORE_EXPORT void setHTTPContentType(const String&);
118 WEBCORE_EXPORT void clearHTTPContentType();
120 bool hasHTTPHeader(HTTPHeaderName) const;
122 WEBCORE_EXPORT String httpReferrer() const;
123 bool hasHTTPReferrer() const;
124 WEBCORE_EXPORT void setHTTPReferrer(const String&);
125 WEBCORE_EXPORT void setExistingHTTPReferrerToOriginString();
126 WEBCORE_EXPORT void clearHTTPReferrer();
128 WEBCORE_EXPORT String httpOrigin() const;
129 bool hasHTTPOrigin() const;
130 void setHTTPOrigin(const String&);
131 WEBCORE_EXPORT void clearHTTPOrigin();
133 WEBCORE_EXPORT String httpUserAgent() const;
134 WEBCORE_EXPORT void setHTTPUserAgent(const String&);
135 void clearHTTPUserAgent();
137 String httpAccept() const;
138 void setHTTPAccept(const String&);
139 void clearHTTPAccept();
141 void clearHTTPAcceptEncoding();
143 const Vector<String>& responseContentDispositionEncodingFallbackArray() const { return m_responseContentDispositionEncodingFallbackArray; }
144 WEBCORE_EXPORT void setResponseContentDispositionEncodingFallbackArray(const String& encoding1, const String& encoding2 = String(), const String& encoding3 = String());
146 WEBCORE_EXPORT FormData* httpBody() const;
147 WEBCORE_EXPORT void setHTTPBody(RefPtr<FormData>&&);
149 bool allowCookies() const;
150 void setAllowCookies(bool allowCookies);
152 WEBCORE_EXPORT ResourceLoadPriority priority() const;
153 WEBCORE_EXPORT void setPriority(ResourceLoadPriority);
155 WEBCORE_EXPORT static String partitionName(const String& domain);
156 const String& cachePartition() const { return m_cachePartition; }
157 WEBCORE_EXPORT void setCachePartition(const String&);
158 void setDomainForCachePartition(const String& domain) { setCachePartition(partitionName(domain)); }
160 WEBCORE_EXPORT bool isConditional() const;
161 WEBCORE_EXPORT void makeUnconditional();
163 // Whether this request should be hidden from the Inspector.
164 bool hiddenFromInspector() const { return m_hiddenFromInspector; }
165 void setHiddenFromInspector(bool hiddenFromInspector) { m_hiddenFromInspector = hiddenFromInspector; }
167 enum class Requester { Unspecified, Main, XHR, Fetch, Media, ImportScripts };
168 Requester requester() const { return m_requester; }
169 void setRequester(Requester requester) { m_requester = requester; }
171 // Who initiated the request so the Inspector can associate it with a context. E.g. a Web Worker.
172 String initiatorIdentifier() const { return m_initiatorIdentifier; }
173 void setInitiatorIdentifier(const String& identifier) { m_initiatorIdentifier = identifier; }
175 #if USE(SYSTEM_PREVIEW)
176 WEBCORE_EXPORT bool isSystemPreview() const;
177 WEBCORE_EXPORT void setSystemPreview(bool);
179 WEBCORE_EXPORT const IntRect& systemPreviewRect() const;
180 WEBCORE_EXPORT void setSystemPreviewRect(const IntRect&);
184 bool encodingRequiresPlatformData() const { return true; }
186 template<class Encoder> void encodeWithoutPlatformData(Encoder&) const;
187 template<class Decoder> bool decodeWithoutPlatformData(Decoder&);
189 WEBCORE_EXPORT static double defaultTimeoutInterval(); // May return 0 when using platform default.
190 WEBCORE_EXPORT static void setDefaultTimeoutInterval(double);
192 WEBCORE_EXPORT static bool equal(const ResourceRequest&, const ResourceRequest&);
195 // Used when ResourceRequest is initialized from a platform representation of the request
196 ResourceRequestBase()
197 : m_platformRequestUpdated(true)
198 , m_platformRequestBodyUpdated(true)
202 ResourceRequestBase(const URL& url, ResourceRequestCachePolicy policy)
204 , m_timeoutInterval(s_defaultTimeoutInterval)
205 , m_httpMethod("GET"_s)
206 , m_cachePolicy(policy)
207 , m_allowCookies(true)
208 , m_resourceRequestUpdated(true)
209 , m_resourceRequestBodyUpdated(true)
213 void updatePlatformRequest(HTTPBodyUpdatePolicy = HTTPBodyUpdatePolicy::DoNotUpdateHTTPBody) const;
214 void updateResourceRequest(HTTPBodyUpdatePolicy = HTTPBodyUpdatePolicy::DoNotUpdateHTTPBody) const;
216 template<class Encoder> void encodeBase(Encoder&) const;
217 template<class Decoder> bool decodeBase(Decoder&);
219 // The ResourceRequest subclass may "shadow" this method to compare platform specific fields
220 static bool platformCompare(const ResourceRequest&, const ResourceRequest&) { return true; }
223 double m_timeoutInterval; // 0 is a magic value for platform default on platforms that have one.
224 URL m_firstPartyForCookies;
226 String m_initiatorIdentifier;
227 String m_cachePartition { emptyString() };
228 HTTPHeaderMap m_httpHeaderFields;
229 Vector<String> m_responseContentDispositionEncodingFallbackArray;
230 RefPtr<FormData> m_httpBody;
231 ResourceRequestCachePolicy m_cachePolicy { ResourceRequestCachePolicy::UseProtocolCachePolicy };
232 SameSiteDisposition m_sameSiteDisposition { SameSiteDisposition::Unspecified };
233 ResourceLoadPriority m_priority { ResourceLoadPriority::Low };
234 Requester m_requester { Requester::Unspecified };
235 bool m_allowCookies { false };
236 mutable bool m_resourceRequestUpdated { false };
237 mutable bool m_platformRequestUpdated { false };
238 mutable bool m_resourceRequestBodyUpdated { false };
239 mutable bool m_platformRequestBodyUpdated { false };
240 bool m_hiddenFromInspector { false };
241 bool m_isTopSite { false };
242 #if USE(SYSTEM_PREVIEW)
243 bool m_isSystemPreview { false };
244 IntRect m_systemPreviewRect;
248 const ResourceRequest& asResourceRequest() const;
250 WEBCORE_EXPORT static double s_defaultTimeoutInterval;
253 bool equalIgnoringHeaderFields(const ResourceRequestBase&, const ResourceRequestBase&);
255 // FIXME: Find a better place for these functions.
256 inline bool registrableDomainsAreEqual(const URL& a, const URL& b)
258 return ResourceRequestBase::partitionName(a.host().toString()) == ResourceRequestBase::partitionName(b.host().toString());
260 inline bool registrableDomainsAreEqual(const URL& a, const String& registrableDomain)
262 return ResourceRequestBase::partitionName(a.host().toString()) == registrableDomain;
265 inline bool operator==(const ResourceRequest& a, const ResourceRequest& b) { return ResourceRequestBase::equal(a, b); }
266 inline bool operator!=(ResourceRequest& a, const ResourceRequest& b) { return !(a == b); }
268 WEBCORE_EXPORT unsigned initializeMaximumHTTPConnectionCountPerHost();
270 WEBCORE_EXPORT void initializeHTTPConnectionSettingsOnStartup();
273 template<class Encoder>
274 ALWAYS_INLINE void ResourceRequestBase::encodeBase(Encoder& encoder) const
277 encoder << m_timeoutInterval;
278 encoder << m_firstPartyForCookies.string();
279 encoder << m_httpMethod;
280 encoder << m_httpHeaderFields;
281 encoder << m_responseContentDispositionEncodingFallbackArray;
282 encoder.encodeEnum(m_cachePolicy);
283 encoder << m_allowCookies;
284 encoder.encodeEnum(m_sameSiteDisposition);
285 encoder << m_isTopSite;
286 encoder.encodeEnum(m_priority);
287 encoder.encodeEnum(m_requester);
290 template<class Decoder>
291 ALWAYS_INLINE bool ResourceRequestBase::decodeBase(Decoder& decoder)
293 if (!decoder.decode(m_url))
296 if (!decoder.decode(m_timeoutInterval))
299 String firstPartyForCookies;
300 if (!decoder.decode(firstPartyForCookies))
302 m_firstPartyForCookies = URL(ParsedURLString, firstPartyForCookies);
304 if (!decoder.decode(m_httpMethod))
307 if (!decoder.decode(m_httpHeaderFields))
310 if (!decoder.decode(m_responseContentDispositionEncodingFallbackArray))
313 ResourceRequestCachePolicy cachePolicy;
314 if (!decoder.decodeEnum(cachePolicy))
316 m_cachePolicy = cachePolicy;
319 if (!decoder.decode(allowCookies))
321 m_allowCookies = allowCookies;
323 SameSiteDisposition sameSiteDisposition;
324 if (!decoder.decodeEnum(sameSiteDisposition))
326 m_sameSiteDisposition = sameSiteDisposition;
329 if (!decoder.decode(isTopSite))
331 m_isTopSite = isTopSite;
333 ResourceLoadPriority priority;
334 if (!decoder.decodeEnum(priority))
336 m_priority = priority;
338 if (!decoder.decodeEnum(m_requester))
344 template<class Encoder>
345 void ResourceRequestBase::encodeWithoutPlatformData(Encoder& encoder) const
348 ASSERT(!m_platformRequestUpdated);
352 template<class Decoder>
353 bool ResourceRequestBase::decodeWithoutPlatformData(Decoder& decoder)
355 return decodeBase(decoder);
358 } // namespace WebCore
360 #endif // ResourceRequestBase_h