diff options
Diffstat (limited to 'spider/src/request.rs')
| -rw-r--r-- | spider/src/request.rs | 63 |
1 files changed, 45 insertions, 18 deletions
diff --git a/spider/src/request.rs b/spider/src/request.rs index db1f2ad..3bd6a8f 100644 --- a/spider/src/request.rs +++ b/spider/src/request.rs @@ -5,21 +5,21 @@ use crate::http_method::HttpMethod; pub struct Request { pub method: HttpMethod, - resource: String, - http_version: String, - headers: HashMap<String, String>, - body: Vec<u8>, + pub path: String, + pub http_version: String, + pub headers: HashMap<String, String>, + pub body: Vec<u8>, } impl Request { pub fn new( method: HttpMethod - , resource: String + , path: String , http_version: String , headers: HashMap<String, String> , body: Vec<u8> ) -> Request { return Request { method, - resource, + path, http_version, headers, body, @@ -49,15 +49,17 @@ impl Request { } } - let body = data[idx..].to_vec(); + let mut body = data[idx..].to_vec(); let mut headers: HashMap<String, String> = HashMap::new(); - for line in &lines[1..] { - let line = line - .trim_end_matches("\r\n") - .split(": ") - .collect::<Vec<&str>>(); - headers.insert(String::from(line[0]), line[1..].join(" ")); + if lines.len() > 0 { + for line in &lines[1..] { + let line = line + .trim_end_matches("\r\n") + .split(": ") + .collect::<Vec<&str>>(); + headers.insert(String::from(line[0]), line[1..].join(" ")); + } } let request_line = lines[0] @@ -65,23 +67,48 @@ impl Request { .split(" ") .collect::<Vec<&str>>(); + let (path, params) = split_path(String::from(request_line[1])); + let method = String::from(request_line[0]); match HttpMethod::parse(method) { - Some(method) => + Some(method) => { + if method == HttpMethod::GET { + if let Some(p) = params { + body = p.as_bytes().to_vec(); + } + } return Some(Request::new( method, - // TODO(andrew): add parsing of resource line. - String::from(request_line[1]), + path, String::from(request_line[2]), headers, - body )), + body )); + } None => return None }; } } +fn split_path(resource: String) -> (String, Option<String>) { + let mut idx = None; + for (i, c) in resource.chars().enumerate() { + if c == '?' { + idx = Some(i); + break; + } + } + match idx { + Some(n) => { + let path = &resource[..n]; + let params = &resource[n + 1..]; + return (path.to_string(), Some(params.to_string())); + }, + None => return (resource, None) + }; +} + impl fmt::Display for Request { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - return write!(f, "Request({}, {})", self.method, self.resource); + return write!(f, "Request({}, {})", self.method, self.path); } } |