From ad92fb3dc6a3350b7b8952be40c5c3a7aa543892 Mon Sep 17 00:00:00 2001 From: Andrew Date: Sun, 14 Feb 2021 14:38:03 +0400 Subject: Added more TODOs --- blog/src/main.rs | 2 +- spider/src/http_method.rs | 1 + spider/src/http_server.rs | 5 +++++ spider/src/request.rs | 3 ++- spider/src/response.rs | 9 ++++----- 5 files changed, 13 insertions(+), 7 deletions(-) diff --git a/blog/src/main.rs b/blog/src/main.rs index c779191..9693f88 100644 --- a/blog/src/main.rs +++ b/blog/src/main.rs @@ -20,7 +20,7 @@ impl MyHandler { } } - +// TODO(andrew): create logging package. fn main() { let handler = MyHandler::new(); let server = HttpServer::new("localhost", 3000, handler); diff --git a/spider/src/http_method.rs b/spider/src/http_method.rs index b1cc884..e308983 100644 --- a/spider/src/http_method.rs +++ b/spider/src/http_method.rs @@ -1,5 +1,6 @@ use std::fmt; +// TODO(andrew): add rest of the methods. pub enum HttpMethod { GET, POST } diff --git a/spider/src/http_server.rs b/spider/src/http_server.rs index 419f39e..a3f4bc2 100644 --- a/spider/src/http_server.rs +++ b/spider/src/http_server.rs @@ -7,6 +7,7 @@ use crate::response::Response; use crate::http_method::HttpMethod; pub trait HttpHandler { + // TODO(andrew): add default behaviour to handlers? fn do_get(&self, request: Request) -> Response; fn do_post(&self, request: Request) -> Response; } @@ -55,6 +56,9 @@ impl HttpServer where T: HttpHandler { let mut buf: [u8; 1024] = [0; 1024]; stream.peek(&mut buf).expect("Couldn't read from socket"); + // TODO(andrew): parse only headers as string and leave decoding of + // body to handler. + // TODO(andrew): read all body, not first 1024 bytes. // TODO(andrew): remove panic. let s = match str::from_utf8(&buf) { Ok(v) => v, @@ -68,6 +72,7 @@ impl HttpServer where T: HttpHandler { None => panic!("Request parsed with errors") }; + // TODO(andrew): add more methods. let response = match request.method { HttpMethod::GET => self.handler.do_get(request), HttpMethod::POST => self.handler.do_post(request) diff --git a/spider/src/request.rs b/spider/src/request.rs index 4fae0c5..c7d5ab0 100644 --- a/spider/src/request.rs +++ b/spider/src/request.rs @@ -24,7 +24,8 @@ impl Request { }; } - // TODO(andrew): Add some error handling. + // TODO(andrew): accept &[u8] instead of &str and add parsing of body. + // TODO(andrew): add some error handling. pub fn from(data: &str) -> Option { let lines = data.split("\r\n").collect::>(); diff --git a/spider/src/response.rs b/spider/src/response.rs index e192b22..35b2f00 100644 --- a/spider/src/response.rs +++ b/spider/src/response.rs @@ -1,6 +1,8 @@ use std::collections::HashMap; use std::fmt; +// TODO(andrew): add more flexible status structure. +// (Possibly with enum variants?) pub struct Response { status: i32, headers: HashMap, @@ -18,13 +20,10 @@ impl Response { } pub fn format<'a>(&self) -> &'a [u8] { + // TODO(andrew): replace placeholder response with actual + // formatted response; let s = "HTTP/1.1 200 OK\r\nConnection: keep-alive\r\nContent-Type: text/html\r\n\r\nHello"; return s.as_bytes(); - - // let buf: &[u8]; - // buf = &[0; 1024]; -// - // return buf; } } -- cgit v1.2.3