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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
extern crate alloc;

use alloc::collections::BTreeMap;
use alloc::string::{String, ToString};
use core::convert::{TryFrom, TryInto};

use crate::error::HttpError;
use crate::headers::Headers;
use crate::status::Status;
use crate::version::Version;

#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub struct Response {
    pub version: Version,
    pub status: Status,
    pub headers: Headers,
    pub body: String,
}

impl TryFrom<&str> for Response {
    type Error = HttpError;

    fn try_from(mut src: &str) -> Result<Self, Self::Error> {
        let version = if let Some(index) = src.find(" ") {
            let version_split = src.split_at(index);

            src = &version_split.1[1..];

            version_split.0.try_into()?
        } else {
            return Err(HttpError::Exhausted);
        };

        let skip_headers = src.find("\r\n") == src.find("\r\n\r\n");

        let status = if let Some(index) = src.find("\r\n") {
            let status_split = src.split_at(index);

            src = &status_split.1[2..];

            status_split.0.try_into()?
        } else {
            return Err(HttpError::Exhausted);
        };

        let headers: Headers = if skip_headers {
            src = &src[2..];
            Headers {
                headers: BTreeMap::new(),
            }
        } else {
            if let Some(index) = src.find("\r\n\r\n") {
                let header_split = src.split_at(index);

                src = &header_split.1[4..];

                header_split.0.try_into()?
            } else {
                return Err(HttpError::Exhausted);
            }
        };

        let content_length = if let Some(content_length) = headers.headers.get("Content-Length") {
            match content_length.parse::<u32>() {
                Ok(content_length) => content_length,
                Err(_) => return Err(HttpError::InvalidResponse),
            }
        } else {
            0
        };

        let body = src.to_string();

        if (body.len() as u32) < content_length {
            return Err(HttpError::Exhausted);
        } else if (body.len() as u32) > content_length {
            return Err(HttpError::InvalidResponse);
        }

        Ok(Self {
            version,
            status,
            headers,
            body,
        })
    }
}

impl ToString for Response {
    fn to_string(&self) -> String {
        let mut data = String::new();
        let mut headers = self.headers.clone();

        if self.body.len() > 0 {
            headers
                .headers
                .insert("Content-Length".to_string(), self.body.len().to_string());
        }

        data += self.version.to_string().as_str();
        data += " ";
        data += self.status.to_string().as_str();
        if headers.headers.len() > 0 {
            data += "\r\n";
            data += headers.to_string().as_str();
        }
        data += "\r\n\r\n";
        data += self.body.as_str();

        data
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use alloc::collections::BTreeMap;

    use crate::status::StatusCode;

    #[test]
    fn from_str_full_test() {
        let mut headers = BTreeMap::new();
        headers.insert("Content-Length".to_string(), "4".to_string());

        let version = Version::Http11;
        let status = Status::from(StatusCode::Ok);
        let headers = Headers { headers };
        let body = "Body".to_string();
        assert_eq!(
            Response::try_from("HTTP/1.1 200 Ok\r\nContent-Length: 4\r\n\r\nBody"),
            Ok(Response {
                version,
                status,
                headers,
                body,
            }),
        );
    }

    #[test]
    fn to_string_full_test() {
        let version = Version::Http11;
        let status = Status::from(StatusCode::Ok);
        let headers = Headers {
            headers: BTreeMap::new(),
        };
        let body = "Body".to_string();
        assert_eq!(
            Response {
                version,
                status,
                headers,
                body,
            }
            .to_string(),
            "HTTP/1.1 200 Ok\r\nContent-Length: 4\r\n\r\nBody".to_string()
        );
    }

    #[test]
    fn from_str_no_body_test() {
        let version = Version::Http11;
        let status = Status::from(StatusCode::Ok);
        let headers = Headers {
            headers: BTreeMap::new(),
        };
        let body = "".to_string();
        assert_eq!(
            Response::try_from("HTTP/1.1 200 Ok\r\n\r\n"),
            Ok(Response {
                version,
                status,
                headers,
                body,
            }),
        );
    }

    #[test]
    fn to_string_no_body_test() {
        let version = Version::Http11;
        let status = Status::from(StatusCode::Ok);
        let headers = Headers {
            headers: BTreeMap::new(),
        };
        let body = "".to_string();
        assert_eq!(
            Response {
                version,
                status,
                headers,
                body,
            }
            .to_string(),
            "HTTP/1.1 200 Ok\r\n\r\n".to_string(),
        );
    }

    #[test]
    fn from_str_invalid_request1_test() {
        assert_eq!(
            Response::try_from("HTTP/1.1 200 Ok\r\nContent-Length: 4\r\n\r\nBody "),
            Err(HttpError::InvalidResponse)
        );
    }

    #[test]
    fn from_str_exhausted1_test() {
        assert_eq!(
            Response::try_from("HTTP/1.1 200 Ok\r\nContent-Length: 4\r\n\r\nBod"),
            Err(HttpError::Exhausted)
        );
    }

    #[test]
    fn from_str_exhausted2_test() {
        assert_eq!(
            Response::try_from("HTTP/1.1 200 Ok\r\nContent-Length: 4"),
            Err(HttpError::Exhausted)
        );
    }

    #[test]
    fn from_str_exhausted3_test() {
        assert_eq!(
            Response::try_from("HTTP/1.1 200 Ok\r\n"),
            Err(HttpError::Exhausted)
        );
    }
}