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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use crate::{Error, ErrorKind};
use hyper::body;
use hyper::body::Buf;
use hyper::header::AsHeaderName;
use hyper::header::HeaderValue;
use hyper::Body as HyperBody;
use hyper::Request as HyperRequest;

///
/// Stores a HTTP request
///
#[derive(Debug)]
pub struct Request {
    inner: HyperRequest<HyperBody>,
    body: Option<Vec<u8>>,
}

impl Request {
    pub(crate) fn new(request: HyperRequest<HyperBody>) -> Self {
        Request {
            inner: request,
            body: None,
        }
    }

    /// The HTTP method
    pub fn method(&self) -> &str {
        self.inner.method().as_ref()
    }

    /// The path excluding the query part
    pub fn path(&self) -> &str {
        self.inner.uri().path()
    }

    /// The path including the query part
    pub fn path_and_query(&self) -> &str {
        self.inner
            .uri()
            .path_and_query()
            .map(|pq| pq.as_str())
            .unwrap_or("")
    }

    /// Retrieves all the header values for the given header field name
    pub fn header<T: AsHeaderName>(&self, header_name: T) -> Vec<&HeaderValue> {
        self.inner.headers().get_all(header_name).iter().collect()
    }

    /// Checks whether the provided header field exists
    pub fn has_header<T: AsHeaderName>(&self, header_name: T) -> bool {
        self.inner.headers().contains_key(header_name)
    }

    /// Returns the request body or an error, if the body hasn't been read
    /// up to this moment.
    pub fn body(&self) -> Result<&Vec<u8>, Error> {
        self.body
            .as_ref()
            .ok_or_else(|| Error::new(ErrorKind::RequestBodyFailure))
    }

    /// Reads the body (if it hasn't been read already) and returns it
    pub(crate) async fn read_body(&mut self) -> &Vec<u8> {
        if self.body.is_none() {
            let raw_body = self.inner.body_mut();
            let mut buf = body::aggregate(raw_body)
                .await
                .map_err(|err| Error::new_with_context(ErrorKind::RequestBodyFailure, err))
                .unwrap();
            let bytes = buf.copy_to_bytes(buf.remaining()).to_vec();
            self.body = Some(bytes);
        }

        self.body.as_ref().unwrap()
    }

    pub(crate) fn formatted(&self) -> String {
        let mut formatted = format!(
            "\r\n{} {}\r\n",
            &self.inner.method(),
            &self
                .inner
                .uri()
                .path_and_query()
                .map(|pq| pq.as_str())
                .unwrap_or("")
        );

        for (key, value) in self.inner.headers() {
            formatted.push_str(&format!(
                "{}: {}\r\n",
                key,
                value.to_str().unwrap_or("<invalid>")
            ));
        }

        if let Some(body) = &self.body {
            if !body.is_empty() {
                formatted.push_str(&format!("{}\r\n", &String::from_utf8_lossy(body)));
            }
        }

        formatted
    }
}