retina_fetch/
request.rs

1// Copyright (C) 2023 Tristan Gerritsen <tristan@thewoosh.org>
2// All Rights Reserved.
3
4use hyper::Method;
5use url::Url;
6
7use crate::{
8    RequestDestination,
9    RequestInitiator,
10    RequestMode,
11    RequestReferrer,
12};
13
14/// The [Request][spec] class.
15///
16/// [spec]: https://fetch.spec.whatwg.org/#request-class
17#[derive(Clone, Debug, PartialEq)]
18pub struct Request {
19    pub(crate) initiator: RequestInitiator,
20    pub(crate) destination: RequestDestination,
21    pub(crate) mode: RequestMode,
22    pub(crate) referrer: RequestReferrer,
23
24    pub(crate) method: hyper::Method,
25    pub(crate) url: Url,
26}
27
28impl Request {
29    /// Create a new [Request][https://fetch.spec.whatwg.org/#request-class].
30    pub fn new(
31        url: Url,
32        initiator: RequestInitiator,
33        destination: RequestDestination,
34        mode: RequestMode,
35        referrer: RequestReferrer,
36    ) -> Self {
37        Request {
38            initiator,
39            destination,
40            mode,
41            referrer,
42
43            method: hyper::Method::GET,
44            url,
45        }
46    }
47
48    /// A helper to create a [Request][spec], specifically for Document
49    /// retrieval with navigation (i.e. top-level browser contexts).
50    ///
51    /// [spec]: https://fetch.spec.whatwg.org/#request-class
52    pub fn get_document(url: Url, referrer: RequestReferrer) -> Self {
53        Request {
54            initiator: RequestInitiator::None,
55            destination: RequestDestination::Document,
56            mode: RequestMode::Navigate,
57            referrer,
58
59            method: Method::GET,
60            url,
61        }
62    }
63
64    /// Compute or get the value of the [`Accept`][spec] header, which specifies
65    /// what type of content is acceptable for us to handle.
66    ///
67    /// [spec]: https://httpwg.org/specs/rfc9110.html#field.accept
68    pub fn accept_header_value(&self) -> &str {
69        match self.destination {
70            RequestDestination::Document => "text/html,*/*;q=0.8",
71            RequestDestination::Style => "text/css,*/*;q=0.8",
72            _ => "*/*",
73        }
74    }
75
76    /// Get the [Url] that this request should retrieve.
77    pub fn url(&self) -> &Url {
78        &self.url
79    }
80}