worker_route/http/
request.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use super::HttpHeaders;

use worker::{Method, Request, Url};

/// Extracted from [`worker::Request`](https://docs.rs/worker/latest/worker/struct.Request.html) mainly used for [`Responder`](crate::Responder) trait.
#[allow(clippy::module_name_repetitions)]
#[derive(Debug)]
pub struct HttpRequest {
    headers: HttpHeaders,
    method: Method,
    path: String,
    url: Option<Url>,
}

impl HttpRequest {
    /// Returns the cloned request's headers.
    pub fn headers(&self) -> &HttpHeaders {
        &self.headers
    }

    /// Request method.
    pub fn method(&self) -> &Method {
        &self.method
    }

    /// The path of this request.
    pub fn path(&self) -> &str {
        &self.path
    }

    /// The parsed [`Url`] of this `Request`.
    ///
    /// None if errors occured from parsing the `Url`.
    pub fn url(&self) -> Option<&Url> {
        self.url.as_ref()
    }

    #[cfg_attr(docsrs, doc(cfg(feature = "cookies")))]
    #[cfg(feature = "cookies")]
    /// Request cookies.
    pub fn cookies(&self) -> impl Iterator<Item = cookie::Cookie<'_>> {
        use crate::http::cookies::CookieHelper;
        CookieHelper::Get.get(&self.headers)
    }
}

impl From<&Request> for HttpRequest {
    /// This is constructed from code generation. Not a public method.
    fn from(req: &Request) -> Self {
        Self {
            headers: req.headers().into(),
            method: req.method(),
            path: req.path(),
            url: req.url().ok(),
        }
    }
}