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
// SPDX-License-Identifier: MIT
// Copyright 2023 IROX Contributors
//

use crate::http::headers::HttpHeaders;
use crate::http::{HttpBody, HttpMethod, HttpVersion};
use crate::url::URL;
use log::debug;
use std::io::Write;

pub struct HttpRequest {
    pub(crate) url: URL,
    pub(crate) method: HttpMethod,
    pub(crate) version: HttpVersion,
    pub(crate) headers: HttpHeaders,
    pub(crate) body: HttpBody,
}

impl HttpRequest {
    pub fn new(url: URL) -> Self {
        Self {
            method: HttpMethod::Get,
            url,
            version: HttpVersion::Http1_1,
            headers: HttpHeaders::new_request(),
            body: HttpBody::Empty,
        }
    }

    pub fn set_method(&mut self, method: HttpMethod) {
        self.method = method
    }
    pub fn method(&self) -> &HttpMethod {
        &self.method
    }

    pub fn set_url(&mut self, url: URL) {
        self.url = url;
    }
    pub fn url(&self) -> &URL {
        &self.url
    }

    pub fn write_to<T: Write>(mut self, mut out: &mut T) -> Result<(), std::io::Error> {
        if !self.headers.contains_header("Host") {
            let port = if let Some(port) = self.url.port {
                format!(":{port}")
            } else {
                String::new()
            };
            let host = format!("{}{port}", self.url.host);
            self.headers.maybe_add("Host", &host);
        }
        self.headers
            .maybe_add("User-Agent", crate::http::DEFAULT_USER_AGENT);
        match &self.body {
            HttpBody::Empty => {
                self.headers.maybe_add("Content-Length", "0");
            }
            HttpBody::String(s) => {
                self.headers
                    .maybe_add("Content-Length", format!("{}", s.len()));
            }
            HttpBody::Bytes(b) => {
                self.headers
                    .maybe_add("Content-Length", format!("{}", b.len()));
            }
            _ => {}
        }

        let hdr = format!(
            "{} {} {}",
            self.method,
            self.url.get_path_query_fragment(),
            self.version
        );
        debug!("{hdr}");
        write!(out, "{hdr}\r\n")?;
        self.headers.write_to(&mut out)?;

        self.body.write_to(&mut out)?;

        out.flush()?;

        Ok(())
    }
}