Requests.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /*
  2. * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/ByteBuffer.h>
  8. #include <AK/Error.h>
  9. #include <AK/Forward.h>
  10. #include <AK/Optional.h>
  11. #include <AK/String.h>
  12. #include <AK/URL.h>
  13. #include <AK/Variant.h>
  14. #include <AK/Vector.h>
  15. #include <LibWeb/Fetch/Infrastructure/HTTP/Bodies.h>
  16. #include <LibWeb/Fetch/Infrastructure/HTTP/Headers.h>
  17. #include <LibWeb/HTML/Origin.h>
  18. #include <LibWeb/HTML/PolicyContainers.h>
  19. #include <LibWeb/HTML/Scripting/Environments.h>
  20. namespace Web::Fetch::Infrastructure {
  21. // https://fetch.spec.whatwg.org/#concept-request
  22. class Request final {
  23. public:
  24. enum class CacheMode {
  25. Default,
  26. NoStore,
  27. Reload,
  28. NoCache,
  29. ForceCache,
  30. OnlyIfCached,
  31. };
  32. enum class CredentialsMode {
  33. Omit,
  34. SameOrigin,
  35. Include,
  36. };
  37. enum class Destination {
  38. Audio,
  39. AudioWorklet,
  40. Document,
  41. Embed,
  42. Font,
  43. Frame,
  44. IFrame,
  45. Image,
  46. Manifest,
  47. Object,
  48. PaintWorklet,
  49. Report,
  50. Script,
  51. ServiceWorker,
  52. SharedWorker,
  53. Style,
  54. Track,
  55. Video,
  56. Worker,
  57. XSLT,
  58. };
  59. enum class Initiator {
  60. Download,
  61. ImageSet,
  62. Manifest,
  63. Prefetch,
  64. Prerender,
  65. XSLT,
  66. };
  67. enum class InitiatorType {
  68. Audio,
  69. Beacon,
  70. Body,
  71. CSS,
  72. EarlyHint,
  73. Embed,
  74. Fetch,
  75. Font,
  76. Frame,
  77. IFrame,
  78. Image,
  79. IMG,
  80. Input,
  81. Link,
  82. Object,
  83. Ping,
  84. Script,
  85. Track,
  86. Video,
  87. XMLHttpRequest,
  88. Other,
  89. };
  90. enum class Mode {
  91. SameOrigin,
  92. CORS,
  93. NoCORS,
  94. Navigate,
  95. WebSocket,
  96. };
  97. enum class Origin {
  98. Client,
  99. };
  100. enum class ParserMetadata {
  101. ParserInserted,
  102. NotParserInserted,
  103. };
  104. enum class PolicyContainer {
  105. Client,
  106. };
  107. enum class RedirectMode {
  108. Follow,
  109. Error,
  110. Manual,
  111. };
  112. enum class Referrer {
  113. NoReferrer,
  114. Client,
  115. };
  116. enum class ResponseTainting {
  117. Basic,
  118. CORS,
  119. Opaque,
  120. };
  121. enum class ServiceWorkersMode {
  122. All,
  123. None,
  124. };
  125. enum class Window {
  126. NoWindow,
  127. Client,
  128. };
  129. // Members are implementation-defined
  130. struct Priority { };
  131. using BodyType = Variant<Empty, ByteBuffer, Body>;
  132. using OriginType = Variant<Origin, HTML::Origin>;
  133. using PolicyContainerType = Variant<PolicyContainer, HTML::PolicyContainer>;
  134. using ReferrerType = Variant<Referrer, AK::URL>;
  135. using ReservedClientType = Variant<Empty, HTML::Environment*, HTML::EnvironmentSettingsObject*>;
  136. using WindowType = Variant<Window, HTML::EnvironmentSettingsObject*>;
  137. Request();
  138. [[nodiscard]] ReadonlyBytes method() const { return m_method; }
  139. void set_method(ByteBuffer method) { m_method = move(method); }
  140. [[nodiscard]] bool local_urls_only() const { return m_local_urls_only; }
  141. void set_local_urls_only(bool local_urls_only) { m_local_urls_only = local_urls_only; }
  142. [[nodiscard]] NonnullRefPtr<HeaderList> const& header_list() const { return m_header_list; }
  143. [[nodiscard]] NonnullRefPtr<HeaderList>& header_list() { return m_header_list; }
  144. void set_header_list(NonnullRefPtr<HeaderList> header_list) { m_header_list = move(header_list); }
  145. [[nodiscard]] bool unsafe_request() const { return m_unsafe_request; }
  146. void set_unsafe_request(bool unsafe_request) { m_unsafe_request = unsafe_request; }
  147. [[nodiscard]] BodyType const& body() const { return m_body; }
  148. [[nodiscard]] BodyType& body() { return m_body; }
  149. void set_body(BodyType body) { m_body = move(body); }
  150. [[nodiscard]] HTML::EnvironmentSettingsObject const* client() const { return m_client; }
  151. [[nodiscard]] HTML::EnvironmentSettingsObject* client() { return m_client; }
  152. void set_client(HTML::EnvironmentSettingsObject& client) { m_client = &client; }
  153. [[nodiscard]] ReservedClientType const& reserved_client() const { return m_reserved_client; }
  154. [[nodiscard]] ReservedClientType& reserved_client() { return m_reserved_client; }
  155. void set_reserved_client(ReservedClientType reserved_client) { m_reserved_client = move(reserved_client); }
  156. [[nodiscard]] String const& replaces_client_id() const { return m_replaces_client_id; }
  157. void set_replaces_client_id(String replaces_client_id) { m_replaces_client_id = move(replaces_client_id); }
  158. [[nodiscard]] WindowType const& window() const { return m_window; }
  159. void set_window(WindowType window) { m_window = move(window); }
  160. [[nodiscard]] bool keepalive() const { return m_keepalive; }
  161. void set_keepalive(bool keepalive) { m_keepalive = keepalive; }
  162. [[nodiscard]] Optional<InitiatorType> const& initiator_type() const { return m_initiator_type; }
  163. void set_initiator_type(Optional<InitiatorType> initiator_type) { m_initiator_type = move(initiator_type); }
  164. [[nodiscard]] ServiceWorkersMode service_workers_mode() const { return m_service_workers_mode; }
  165. void set_service_workers_mode(ServiceWorkersMode service_workers_mode) { m_service_workers_mode = service_workers_mode; }
  166. [[nodiscard]] Optional<Initiator> const& initiator() const { return m_initiator; }
  167. void set_initiator(Optional<Initiator> initiator) { m_initiator = move(initiator); }
  168. [[nodiscard]] Optional<Destination> const& destination() const { return m_destination; }
  169. void set_destination(Optional<Destination> destination) { m_destination = move(destination); }
  170. [[nodiscard]] Optional<Priority> const& priority() const { return m_priority; }
  171. void set_priority(Optional<Priority> priority) { m_priority = move(priority); }
  172. [[nodiscard]] OriginType const& origin() const { return m_origin; }
  173. void set_origin(OriginType origin) { m_origin = move(origin); }
  174. [[nodiscard]] Mode mode() const { return m_mode; }
  175. void set_mode(Mode mode) { m_mode = mode; }
  176. [[nodiscard]] bool use_cors_preflight() const { return m_use_cors_preflight; }
  177. void set_use_cors_preflight(bool use_cors_preflight) { m_use_cors_preflight = use_cors_preflight; }
  178. [[nodiscard]] CredentialsMode credentials_mode() const { return m_credentials_mode; }
  179. void set_credentials_mode(CredentialsMode credentials_mode) { m_credentials_mode = credentials_mode; }
  180. [[nodiscard]] bool use_url_credentials() const { return m_use_url_credentials; }
  181. void set_use_url_credentials(bool use_url_credentials) { m_use_url_credentials = use_url_credentials; }
  182. [[nodiscard]] CacheMode cache_mode() const { return m_cache_mode; }
  183. void set_cache_mode(CacheMode cache_mode) { m_cache_mode = cache_mode; }
  184. [[nodiscard]] RedirectMode redirect_mode() const { return m_redirect_mode; }
  185. void set_redirect_mode(RedirectMode redirect_mode) { m_redirect_mode = redirect_mode; }
  186. [[nodiscard]] String const& integrity_metadata() const { return m_integrity_metadata; }
  187. void set_integrity_metadata(String integrity_metadata) { m_integrity_metadata = move(integrity_metadata); }
  188. [[nodiscard]] String const& cryptographic_nonce_metadata() const { return m_cryptographic_nonce_metadata; }
  189. void set_cryptographic_nonce_metadata(String cryptographic_nonce_metadata) { m_cryptographic_nonce_metadata = move(cryptographic_nonce_metadata); }
  190. [[nodiscard]] Optional<ParserMetadata> const& parser_metadata() const { return m_parser_metadata; }
  191. void set_parser_metadata(Optional<ParserMetadata> parser_metadata) { m_parser_metadata = move(parser_metadata); }
  192. [[nodiscard]] bool reload_navigation() const { return m_reload_navigation; }
  193. void set_reload_navigation(bool reload_navigation) { m_reload_navigation = reload_navigation; }
  194. [[nodiscard]] bool history_navigation() const { return m_history_navigation; }
  195. void set_history_navigation(bool history_navigation) { m_history_navigation = history_navigation; }
  196. [[nodiscard]] bool user_activation() const { return m_user_activation; }
  197. void set_user_activation(bool user_activation) { m_user_activation = user_activation; }
  198. [[nodiscard]] bool render_blocking() const { return m_render_blocking; }
  199. void set_render_blocking(bool render_blocking) { m_render_blocking = render_blocking; }
  200. [[nodiscard]] Vector<AK::URL> const& url_list() const { return m_url_list; }
  201. [[nodiscard]] Vector<AK::URL>& url_list() { return m_url_list; }
  202. void set_url_list(Vector<AK::URL> url_list) { m_url_list = move(url_list); }
  203. [[nodiscard]] u8 redirect_count() const { return m_redirect_count; }
  204. void set_redirect_count(u8 redirect_count) { m_redirect_count = redirect_count; }
  205. [[nodiscard]] ReferrerType const& referrer() const { return m_referrer; }
  206. void set_referrer(ReferrerType referrer) { m_referrer = move(referrer); }
  207. [[nodiscard]] Optional<ReferrerPolicy::ReferrerPolicy> const& referrer_policy() const { return m_referrer_policy; }
  208. void set_referrer_policy(Optional<ReferrerPolicy::ReferrerPolicy> referrer_policy) { m_referrer_policy = move(referrer_policy); }
  209. [[nodiscard]] ResponseTainting response_tainting() const { return m_response_tainting; }
  210. void set_response_tainting(ResponseTainting response_tainting) { m_response_tainting = response_tainting; }
  211. [[nodiscard]] bool prevent_no_cache_cache_control_header_modification() const { return m_prevent_no_cache_cache_control_header_modification; }
  212. void set_prevent_no_cache_cache_control_header_modification(bool prevent_no_cache_cache_control_header_modification) { m_prevent_no_cache_cache_control_header_modification = prevent_no_cache_cache_control_header_modification; }
  213. [[nodiscard]] bool done() const { return m_done; }
  214. void set_done(bool done) { m_done = done; }
  215. [[nodiscard]] bool timing_allow_failed() const { return m_timing_allow_failed; }
  216. void set_timing_allow_failed(bool timing_allow_failed) { m_timing_allow_failed = timing_allow_failed; }
  217. [[nodiscard]] AK::URL const& url() const;
  218. [[nodiscard]] AK::URL const& current_url();
  219. void set_url(AK::URL url);
  220. [[nodiscard]] bool destination_is_script_like() const;
  221. [[nodiscard]] bool is_subresource_request() const;
  222. [[nodiscard]] bool is_non_subresource_request() const;
  223. [[nodiscard]] bool is_navigation_request() const;
  224. [[nodiscard]] bool has_redirect_tainted_origin() const;
  225. [[nodiscard]] String serialize_origin() const;
  226. [[nodiscard]] ErrorOr<ByteBuffer> byte_serialize_origin() const;
  227. [[nodiscard]] WebIDL::ExceptionOr<NonnullOwnPtr<Request>> clone() const;
  228. [[nodiscard]] ErrorOr<void> add_range_reader(u64 first, Optional<u64> const& last);
  229. [[nodiscard]] bool cross_origin_embedder_policy_allows_credentials() const;
  230. private:
  231. // https://fetch.spec.whatwg.org/#concept-request-method
  232. // A request has an associated method (a method). Unless stated otherwise it is `GET`.
  233. ByteBuffer m_method { ByteBuffer::copy("GET"sv.bytes()).release_value() };
  234. // https://fetch.spec.whatwg.org/#local-urls-only-flag
  235. // A request has an associated local-URLs-only flag. Unless stated otherwise it is unset.
  236. bool m_local_urls_only { false };
  237. // https://fetch.spec.whatwg.org/#concept-request-header-list
  238. // A request has an associated header list (a header list). Unless stated otherwise it is empty.
  239. NonnullRefPtr<HeaderList> m_header_list;
  240. // https://fetch.spec.whatwg.org/#unsafe-request-flag
  241. // A request has an associated unsafe-request flag. Unless stated otherwise it is unset.
  242. bool m_unsafe_request { false };
  243. // https://fetch.spec.whatwg.org/#concept-request-body
  244. // A request has an associated body (null, a byte sequence, or a body). Unless stated otherwise it is null.
  245. BodyType m_body;
  246. // https://fetch.spec.whatwg.org/#concept-request-client
  247. // A request has an associated client (null or an environment settings object).
  248. HTML::EnvironmentSettingsObject* m_client { nullptr };
  249. // https://fetch.spec.whatwg.org/#concept-request-reserved-client
  250. // A request has an associated reserved client (null, an environment, or an environment settings object). Unless stated otherwise it is null.
  251. ReservedClientType m_reserved_client;
  252. // https://fetch.spec.whatwg.org/#concept-request-replaces-client-id
  253. // A request has an associated replaces client id (a string). Unless stated otherwise it is the empty string.
  254. String m_replaces_client_id { String::empty() };
  255. // https://fetch.spec.whatwg.org/#concept-request-window
  256. // A request has an associated window ("no-window", "client", or an environment settings object whose global object is a Window object). Unless stated otherwise it is "client".
  257. WindowType m_window { Window::Client };
  258. // https://fetch.spec.whatwg.org/#request-keepalive-flag
  259. // A request has an associated boolean keepalive. Unless stated otherwise it is false.
  260. bool m_keepalive { false };
  261. // https://fetch.spec.whatwg.org/#request-initiator-type
  262. // A request has an associated initiator type, which is null, "audio", "beacon", "body", "css", "early-hint", "embed", "fetch", "font", "frame", "iframe", "image", "img", "input", "link", "object", "ping", "script", "track", "video", "xmlhttprequest", or "other". Unless stated otherwise it is null. [RESOURCE-TIMING]
  263. Optional<InitiatorType> m_initiator_type;
  264. // https://fetch.spec.whatwg.org/#request-service-workers-mode
  265. // A request has an associated service-workers mode, that is "all" or "none". Unless stated otherwise it is "all".
  266. ServiceWorkersMode m_service_workers_mode { ServiceWorkersMode::All };
  267. // https://fetch.spec.whatwg.org/#concept-request-initiator
  268. // A request has an associated initiator, which is the empty string, "download", "imageset", "manifest", "prefetch", "prerender", or "xslt". Unless stated otherwise it is the empty string.
  269. Optional<Initiator> m_initiator;
  270. // https://fetch.spec.whatwg.org/#concept-request-destination
  271. // A request has an associated destination, which is the empty string, "audio", "audioworklet", "document", "embed", "font", "frame", "iframe", "image", "manifest", "object", "paintworklet", "report", "script", "serviceworker", "sharedworker", "style", "track", "video", "worker", or "xslt". Unless stated otherwise it is the empty string.
  272. Optional<Destination> m_destination;
  273. // https://fetch.spec.whatwg.org/#concept-request-priority
  274. // A request has an associated priority (null or a user-agent-defined object). Unless otherwise stated it is null.
  275. Optional<Priority> m_priority;
  276. // https://fetch.spec.whatwg.org/#concept-request-origin
  277. // A request has an associated origin, which is "client" or an origin. Unless stated otherwise it is "client".
  278. OriginType m_origin { Origin::Client };
  279. // https://fetch.spec.whatwg.org/#concept-request-policy-container
  280. // A request has an associated policy container, which is "client" or a policy container. Unless stated otherwise it is "client".
  281. PolicyContainerType m_policy_container { PolicyContainer::Client };
  282. // https://fetch.spec.whatwg.org/#concept-request-referrer
  283. // A request has an associated referrer, which is "no-referrer", "client", or a URL. Unless stated otherwise it is "client".
  284. ReferrerType m_referrer { Referrer::Client };
  285. // https://fetch.spec.whatwg.org/#concept-request-referrer-policy
  286. // A request has an associated referrer policy, which is a referrer policy. Unless stated otherwise it is the empty string.
  287. Optional<ReferrerPolicy::ReferrerPolicy> m_referrer_policy;
  288. // https://fetch.spec.whatwg.org/#concept-request-mode
  289. // A request has an associated mode, which is "same-origin", "cors", "no-cors", "navigate", or "websocket". Unless stated otherwise, it is "no-cors".
  290. Mode m_mode { Mode::NoCORS };
  291. // https://fetch.spec.whatwg.org/#use-cors-preflight-flag
  292. // A request has an associated use-CORS-preflight flag. Unless stated otherwise, it is unset.
  293. bool m_use_cors_preflight { false };
  294. // https://fetch.spec.whatwg.org/#concept-request-credentials-mode
  295. // A request has an associated credentials mode, which is "omit", "same-origin", or "include". Unless stated otherwise, it is "same-origin".
  296. CredentialsMode m_credentials_mode { CredentialsMode::SameOrigin };
  297. // https://fetch.spec.whatwg.org/#concept-request-use-url-credentials-flag
  298. // A request has an associated use-URL-credentials flag. Unless stated otherwise, it is unset.
  299. bool m_use_url_credentials { false };
  300. // https://fetch.spec.whatwg.org/#concept-request-cache-mode
  301. // A request has an associated cache mode, which is "default", "no-store", "reload", "no-cache", "force-cache", or "only-if-cached". Unless stated otherwise, it is "default".
  302. CacheMode m_cache_mode { CacheMode::Default };
  303. // A request has an associated redirect mode, which is "follow", "error", or "manual". Unless stated otherwise, it is "follow".
  304. RedirectMode m_redirect_mode { RedirectMode::Follow };
  305. // A request has associated integrity metadata (a string). Unless stated otherwise, it is the empty string.
  306. String m_integrity_metadata { String::empty() };
  307. // A request has associated cryptographic nonce metadata (a string). Unless stated otherwise, it is the empty string.
  308. String m_cryptographic_nonce_metadata { String::empty() };
  309. // A request has associated parser metadata which is the empty string, "parser-inserted", or "not-parser-inserted". Unless otherwise stated, it is the empty string.
  310. Optional<ParserMetadata> m_parser_metadata;
  311. // A request has an associated reload-navigation flag. Unless stated otherwise, it is unset.
  312. bool m_reload_navigation { false };
  313. // https://fetch.spec.whatwg.org/#concept-request-history-navigation-flag
  314. // A request has an associated history-navigation flag. Unless stated otherwise, it is unset.
  315. bool m_history_navigation { false };
  316. // https://fetch.spec.whatwg.org/#request-user-activation
  317. // A request has an associated boolean user-activation. Unless stated otherwise, it is false.
  318. bool m_user_activation { false };
  319. // https://fetch.spec.whatwg.org/#request-render-blocking
  320. // A request has an associated boolean render-blocking. Unless stated otherwise, it is false.
  321. bool m_render_blocking { false };
  322. // https://fetch.spec.whatwg.org/#concept-request-url-list
  323. // A request has an associated URL list (a list of one or more URLs). Unless stated otherwise, it is a list containing a copy of request’s URL.
  324. Vector<AK::URL> m_url_list;
  325. // https://fetch.spec.whatwg.org/#concept-request-redirect-count
  326. // A request has an associated redirect count. Unless stated otherwise, it is zero.
  327. // NOTE: '4.4. HTTP-redirect fetch' infers a limit of 20.
  328. u8 m_redirect_count { 0 };
  329. // https://fetch.spec.whatwg.org/#concept-request-response-tainting
  330. // A request has an associated response tainting, which is "basic", "cors", or "opaque". Unless stated otherwise, it is "basic".
  331. ResponseTainting m_response_tainting { ResponseTainting::Basic };
  332. // https://fetch.spec.whatwg.org/#no-cache-prevent-cache-control
  333. // A request has an associated prevent no-cache cache-control header modification flag. Unless stated otherwise, it is unset.
  334. bool m_prevent_no_cache_cache_control_header_modification { false };
  335. // https://fetch.spec.whatwg.org/#done-flag
  336. // A request has an associated done flag. Unless stated otherwise, it is unset.
  337. bool m_done { false };
  338. // https://fetch.spec.whatwg.org/#timing-allow-failed
  339. // A request has an associated timing allow failed flag. Unless stated otherwise, it is unset.
  340. bool m_timing_allow_failed { false };
  341. };
  342. }