diff options
| author | omagdy7 <omar.professional8777@gmail.com> | 2024-06-03 20:40:54 +0300 |
|---|---|---|
| committer | omagdy7 <omar.professional8777@gmail.com> | 2024-06-03 20:40:54 +0300 |
| commit | 4f9d09b58ae81e4e6144b85ce36ba424e319b779 (patch) | |
| tree | 3b85a7f352928ba0ff05678ba3fb2341f16361ce /src/request.rs | |
| parent | 66f3fcdf0fe097e37b9e3dc8d1a984c35b4d34a4 (diff) | |
| download | tiny-server-4f9d09b58ae81e4e6144b85ce36ba424e319b779.tar.xz tiny-server-4f9d09b58ae81e4e6144b85ce36ba424e319b779.zip | |
codecrafters submit [skip ci]
Diffstat (limited to 'src/request.rs')
| -rw-r--r-- | src/request.rs | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/request.rs b/src/request.rs new file mode 100644 index 0000000..8728e79 --- /dev/null +++ b/src/request.rs @@ -0,0 +1,43 @@ +use crate::http_types::*; + +#[derive(Debug)] +pub struct Request { + pub method: HTTPMethod, + pub headers: Option<Headers>, + body: Option<String>, +} + +impl Request { + fn new(method: HTTPMethod, headers: Headers, body: String) -> Self { + let headers = if headers.0.len() == 0 { + None + } else { + Some(headers) + }; + let body = if body.is_empty() { None } else { Some(body) }; + Request { + method, + headers, + body, + } + } +} + +impl From<&str> for Request { + fn from(val: &str) -> Self { + let request: Vec<&str> = val.split("\r\n").collect(); + match &request[..] { + [request_line, headers @ .., body] => { + let (method, headers, body) = ( + HTTPMethod::from(*request_line), + Headers::from(headers), + body.to_string(), + ); + Request::new(method, headers, body) + } + _ => { + unreachable!(); + } + } + } +} |
