relentless_http/
record.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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use bytes::Bytes;
use http::header::CONTENT_TYPE;
use http_body::Body;
use http_body_util::{BodyExt, Collected};
use relentless::assault::service::record::{CollectClone, IoRecord, RequestIoRecord};

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Default, Hash)]
pub struct HttpIoRecorder;

impl<B> IoRecord<http::Request<B>> for HttpIoRecorder
where
    B: Body + From<Bytes> + Send,
    B::Data: Send,
{
    type Error = std::io::Error;
    fn extension(&self, r: &http::Request<B>) -> &'static str {
        if let Some(content_type) = r.headers().get(CONTENT_TYPE) {
            if content_type == mime::APPLICATION_JSON.as_ref() {
                "json"
            } else {
                "txt"
            }
        } else {
            "txt"
        }
    }
    async fn record<W: std::io::Write + Send>(&self, w: &mut W, r: http::Request<B>) -> Result<(), Self::Error> {
        let body = BodyExt::collect(r.into_body()).await.map(Collected::to_bytes).unwrap_or_default();
        write!(w, "{}", String::from_utf8_lossy(&body))
    }
    async fn record_raw<W: std::io::Write + Send>(&self, w: &mut W, r: http::Request<B>) -> Result<(), Self::Error> {
        let (http::request::Parts { method, uri, version, headers, .. }, body) = r.into_parts();

        writeln!(w, "{} {} {:?}", method, uri, version)?;
        for (header, value) in headers.iter() {
            writeln!(w, "{}: {:?}", header, value)?;
        }
        writeln!(w)?;
        if let Ok(b) = BodyExt::collect(body).await.map(Collected::to_bytes) {
            write!(w, "{}", String::from_utf8_lossy(&b))?;
        }

        Ok(())
    }
}

impl<B> CollectClone<http::Request<B>> for HttpIoRecorder
where
    B: Body + From<Bytes> + Send,
    B::Data: Send,
{
    type Error = B::Error;
    async fn collect_clone(&self, r: http::Request<B>) -> Result<(http::Request<B>, http::Request<B>), Self::Error> {
        // once consume body to record, and reconstruct to request
        let (req_parts, req_body) = r.into_parts();
        let req_bytes = BodyExt::collect(req_body).await.map(Collected::to_bytes)?;
        let req1 = http::Request::from_parts(req_parts.clone(), B::from(req_bytes.clone()));
        let req2 = http::Request::from_parts(req_parts, B::from(req_bytes));
        Ok((req1, req2))
    }
}
impl<B> RequestIoRecord<http::Request<B>> for HttpIoRecorder {
    fn record_dir(&self, r: &http::Request<B>) -> std::path::PathBuf {
        r.uri().to_string().into()
    }
}

impl<B> IoRecord<http::Response<B>> for HttpIoRecorder
where
    B: Body + From<Bytes> + Send,
    B::Data: Send,
{
    type Error = std::io::Error;
    fn extension(&self, r: &http::Response<B>) -> &'static str {
        if let Some(content_type) = r.headers().get(CONTENT_TYPE) {
            if content_type == mime::APPLICATION_JSON.as_ref() {
                "json"
            } else {
                "txt"
            }
        } else {
            "txt"
        }
    }
    async fn record<W: std::io::Write>(&self, w: &mut W, r: http::Response<B>) -> Result<(), Self::Error> {
        let body = BodyExt::collect(r.into_body()).await.map(Collected::to_bytes).unwrap_or_default();
        write!(w, "{}", String::from_utf8_lossy(&body))
    }

    async fn record_raw<W: std::io::Write>(&self, w: &mut W, r: http::Response<B>) -> Result<(), Self::Error> {
        let (http::response::Parts { version, status, headers, .. }, body) = r.into_parts();

        writeln!(w, "{:?} {}", version, status)?;
        for (header, value) in headers.iter() {
            writeln!(w, "{}: {:?}", header, value)?;
        }
        writeln!(w)?;
        if let Ok(b) = BodyExt::collect(body).await.map(Collected::to_bytes) {
            write!(w, "{}", String::from_utf8_lossy(&b))?;
        }

        Ok(())
    }
}
impl<B> CollectClone<http::Response<B>> for HttpIoRecorder
where
    B: Body + From<Bytes> + Send,
    B::Data: Send,
{
    type Error = B::Error;
    async fn collect_clone(&self, r: http::Response<B>) -> Result<(http::Response<B>, http::Response<B>), Self::Error> {
        // once consume body to record, and reconstruct to response
        let (res_parts, res_body) = r.into_parts();
        let res_bytes = BodyExt::collect(res_body).await.map(Collected::to_bytes)?;
        let res1 = http::Response::from_parts(res_parts.clone(), B::from(res_bytes.clone()));
        let res2 = http::Response::from_parts(res_parts, B::from(res_bytes));
        Ok((res1, res2))
    }
}

#[cfg(test)]
mod tests {
    use bytes::Bytes;
    use http::Method;

    use super::*;

    #[tokio::test]
    async fn test_empty_body_request() {
        let request = http::Request::builder()
            .method(Method::GET)
            .uri("http://localhost:3000")
            .body(http_body_util::Full::<Bytes>::new(Default::default()))
            .unwrap();

        let mut buf = Vec::new();
        HttpIoRecorder.record_raw(&mut buf, request).await.unwrap();
        assert_eq!(buf, b"GET http://localhost:3000/ HTTP/1.1\n\n");
    }

    #[tokio::test]
    async fn test_empty_body_response() {
        let response = http::Response::builder()
            .status(http::StatusCode::OK)
            .body(http_body_util::Full::<Bytes>::new(Default::default()))
            .unwrap();

        let mut buf = Vec::new();
        HttpIoRecorder.record_raw(&mut buf, response).await.unwrap();
        assert_eq!(buf, b"HTTP/1.1 200 OK\n\n");
    }
}