1
//! Helper functions for the directory client code
2

            
3
use std::fmt::Write;
4

            
5
use crate::body::{EncodedRequest, RequestBody};
6

            
7
/// Encode an HTTP request in a quick and dirty HTTP 1.0 format.
8
3517
pub(crate) fn encode_request(req: &http::Request<RequestBody>) -> EncodedRequest {
9
3517
    let mut s = format!("{} {} HTTP/1.0\r\n", req.method(), req.uri());
10

            
11
3517
    for (key, val) in req.headers().iter() {
12
3517
        write!(
13
3517
            s,
14
3517
            "{}: {}\r\n",
15
3517
            key,
16
3517
            val.to_str()
17
3517
                .expect("Added an HTTP header that wasn't UTF-8!")
18
3517
        )
19
3517
        .expect("Write to string failed");
20
3517
    }
21

            
22
3517
    if req.method() == http::Method::POST || !req.body().is_empty() {
23
3124
        write!(s, "Content-Length: {}\r\n", req.body().len())
24
3124
            .expect("Added an HTTP header that wasn't UTF-8!");
25
3162
    }
26

            
27
3517
    s.push_str("\r\n");
28

            
29
3517
    let mut body = EncodedRequest::from_header(s);
30
3517
    body.set_body(req.body().clone());
31

            
32
3517
    body
33
3517
}
34

            
35
/// Testing helper: convert a request to a String.
36
///
37
/// # Panics
38
///
39
/// Panics if the request's body is not valid UTF-8
40
#[cfg(test)]
41
32
pub(crate) fn request_to_string(req: &http::Request<RequestBody>) -> String {
42
32
    let body = encode_request(req);
43
32
    let bytes: Vec<u8> = body.to_owned();
44
32
    String::from_utf8(bytes).expect("Body was not UTF-8")
45
32
}
46

            
47
#[cfg(test)]
48
mod test {
49
    // @@ begin test lint list maintained by maint/add_warning @@
50
    #![allow(clippy::bool_assert_comparison)]
51
    #![allow(clippy::clone_on_copy)]
52
    #![allow(clippy::dbg_macro)]
53
    #![allow(clippy::mixed_attributes_style)]
54
    #![allow(clippy::print_stderr)]
55
    #![allow(clippy::print_stdout)]
56
    #![allow(clippy::single_char_pattern)]
57
    #![allow(clippy::unwrap_used)]
58
    #![allow(clippy::unchecked_time_subtraction)]
59
    #![allow(clippy::useless_vec)]
60
    #![allow(clippy::needless_pass_by_value)]
61
    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
62
    use std::sync::Arc;
63

            
64
    use super::*;
65

            
66
    fn build_request(body: String, headers: &[(&str, &str)]) -> http::Request<RequestBody> {
67
        let mut builder = http::Request::builder().method("GET").uri("/index.html");
68

            
69
        for (name, value) in headers {
70
            builder = builder.header(*name, *value);
71
        }
72

            
73
        let body = RequestBody::from(Arc::<str>::from(body));
74

            
75
        builder.body(body).unwrap()
76
    }
77

            
78
    #[test]
79
    fn format() {
80
        fn chk_format(body: &str, content_length_expected: &str) {
81
            let req = build_request(body.to_string(), &[]);
82

            
83
            assert_eq!(
84
                request_to_string(&req),
85
                format!("GET /index.html HTTP/1.0\r\n{content_length_expected}\r\n{body}")
86
            );
87

            
88
            let req = build_request(body.to_string(), &[("X-Marsupial", "Opossum")]);
89
            assert_eq!(
90
                request_to_string(&req),
91
                format!(
92
                    "GET /index.html HTTP/1.0\r\nx-marsupial: Opossum\r\n{content_length_expected}\r\n{body}",
93
                )
94
            );
95
        }
96

            
97
        chk_format("", "");
98
        chk_format("hello", "Content-Length: 5\r\n");
99
    }
100
}