diff --git a/src/http/server.rs b/src/http/server.rs new file mode 100644 index 0000000..3deaf81 --- /dev/null +++ b/src/http/server.rs @@ -0,0 +1,54 @@ +use std::fs; +use std::io::prelude::*; +use std::net::TcpStream; + +mod generators; +mod parsers; +mod types; + +use parsers::*; +use types::*; + +struct HTTPServer { + config: ; + routes: ; +} + +impl HTTPServer { + +fn process_petition(stream: &mut TcpStream) -> ProcessedResponse { + let mut buffer = [0; 1024]; // TODO: manage this size + let _amount = stream.read(&mut buffer); + let petition = String::from_utf8_lossy(&buffer[..]); + let petition = parse_request(&petition); + + match petition { + Ok(petition_parsed) => { + let response_status = "200 OK"; + + let response_content = fs::read_to_string("./routes/index.html").unwrap(); + + let response: ProcessedResponse = ProcessedResponse { + data: format!( + "HTTP/1.1 {}\r\nContent-Length: {}\r\n\r\n{}", + response_status, + response_content.len(), + response_content + ), + status: 200, + }; + + response + } + Err(error) => { + let response: ProcessedResponse = ProcessedResponse { + data: format!("HTTP/1.1 {}\r\nContent-Length: 0\r\n\r\n", error), + status: error, + }; + + response + } + } +} + +} diff --git a/src/test/basic_get.txt b/src/test/assets/basic_get.txt similarity index 100% rename from src/test/basic_get.txt rename to src/test/assets/basic_get.txt diff --git a/routes/index.html b/src/test/assets/routes/index.html similarity index 100% rename from routes/index.html rename to src/test/assets/routes/index.html