diff options
| author | Andrew <saintruler@gmail.com> | 2021-02-14 18:33:29 +0400 |
|---|---|---|
| committer | Andrew <saintruler@gmail.com> | 2021-02-14 18:33:29 +0400 |
| commit | 04a3c64713ae4f0a7f1b5cfdab132f78cc33a862 (patch) | |
| tree | 012bd7b0a0352b48103a8a6b40dcaa919fbb2111 | |
| parent | e59827e90c65f781cf054b52614308f538f7b018 (diff) | |
Renamed path field in Request struct to resource and implemented Display trait for HttpServer
| -rw-r--r-- | spider/src/http_server.rs | 8 | ||||
| -rw-r--r-- | spider/src/request.rs | 18 | ||||
| -rw-r--r-- | spider/src/response.rs | 2 |
3 files changed, 19 insertions, 9 deletions
diff --git a/spider/src/http_server.rs b/spider/src/http_server.rs index f12c45d..479ef3f 100644 --- a/spider/src/http_server.rs +++ b/spider/src/http_server.rs @@ -1,6 +1,7 @@ use std::net::{TcpListener, TcpStream, Shutdown}; use std::io::Write; use std::str; +use std::fmt; use crate::request::Request; use crate::response::Response; @@ -125,3 +126,10 @@ impl<T> HttpServer<T> where T: HttpHandler { }; } } + +impl<T> fmt::Display for HttpServer<T> where T: HttpHandler { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + return write!(f, "HttpServer({}:{})", self.host, self.port); + } +} + diff --git a/spider/src/request.rs b/spider/src/request.rs index fa32bbd..db1f2ad 100644 --- a/spider/src/request.rs +++ b/spider/src/request.rs @@ -5,7 +5,7 @@ use crate::http_method::HttpMethod; pub struct Request { pub method: HttpMethod, - path: String, + resource: String, http_version: String, headers: HashMap<String, String>, body: Vec<u8>, @@ -13,13 +13,13 @@ pub struct Request { impl Request { pub fn new( method: HttpMethod - , path: String + , resource: String , http_version: String , headers: HashMap<String, String> , body: Vec<u8> ) -> Request { return Request { method, - path, + resource, http_version, headers, body, @@ -60,18 +60,19 @@ impl Request { headers.insert(String::from(line[0]), line[1..].join(" ")); } - let first_line = lines[0] + let request_line = lines[0] .trim_end_matches("\r\n") .split(" ") .collect::<Vec<&str>>(); - let method = String::from(first_line[0]); + let method = String::from(request_line[0]); match HttpMethod::parse(method) { Some(method) => return Some(Request::new( method, - String::from(first_line[1]), - String::from(first_line[2]), + // TODO(andrew): add parsing of resource line. + String::from(request_line[1]), + String::from(request_line[2]), headers, body )), None => return None @@ -81,7 +82,6 @@ impl Request { impl fmt::Display for Request { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - return write!(f, "Request({}, {})", self.method, self.path); + return write!(f, "Request({}, {})", self.method, self.resource); } } - diff --git a/spider/src/response.rs b/spider/src/response.rs index 3873315..fe1df78 100644 --- a/spider/src/response.rs +++ b/spider/src/response.rs @@ -24,6 +24,8 @@ impl Response { } pub fn format<'a>(&self) -> Vec<u8> { + // FIXME(andrew): is undefined status code an error that should + // be handled here? let status_text = match get_status_text(self.code) { Some(text) => text, None => String::from("UNDEFINED") |