diff options
| author | Andrew <saintruler@gmail.com> | 2021-02-14 13:43:29 +0400 |
|---|---|---|
| committer | Andrew <saintruler@gmail.com> | 2021-02-14 13:43:29 +0400 |
| commit | 679f1c88ea9c56110f8f66f6f253fce30704e45e (patch) | |
| tree | c8eba9915a827c9dd667a1e8c553dda4f98365cc /spider/src/request.rs | |
| parent | 626e11a725934b64260e17e68d273727be76c57e (diff) | |
Changed structure of library
Diffstat (limited to 'spider/src/request.rs')
| -rw-r--r-- | spider/src/request.rs | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/spider/src/request.rs b/spider/src/request.rs new file mode 100644 index 0000000..4fae0c5 --- /dev/null +++ b/spider/src/request.rs @@ -0,0 +1,56 @@ +use std::collections::HashMap; +use std::fmt; + +use crate::http_method::HttpMethod; + +pub struct Request { + pub method: HttpMethod, + path: String, + http_version: String, + headers: HashMap<String, String>, + // body: &'a [u8] +} + +impl Request { + pub fn new( method: HttpMethod + , path: String + , http_version: String + , headers: HashMap<String, String>) -> Request { + return Request { + method, + path, + http_version, + headers + }; + } + + // TODO(andrew): Add some error handling. + pub fn from(data: &str) -> Option<Request> { + let lines = data.split("\r\n").collect::<Vec<&str>>(); + + let mut headers: HashMap<String, String> = HashMap::new(); + for line in &lines[1..] { + let line = line.split(": ").collect::<Vec<&str>>(); + headers.insert(String::from(line[0]), line[1..].join(" ")); + } + + let first_line = lines[0].split(" ").collect::<Vec<&str>>(); + + let method = String::from(first_line[0]); + match HttpMethod::parse(method) { + Some(method) => + return Some(Request::new( + method, + String::from(first_line[1]), + String::from(first_line[2]), + headers )), + None => return None + }; + } +} + +impl fmt::Display for Request { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + return write!(f, "Request({}, {})", self.method, self.path); + } +} |