diff --git a/core/http/src/handlers/handlers.rs b/core/http/src/handlers/handlers.rs index c3e06cf880b66a4fd897b6767fa7f5f9702e363e..2045ee57c227fdeb8562094bb05bb03980cb6e43 100755 --- a/core/http/src/handlers/handlers.rs +++ b/core/http/src/handlers/handlers.rs @@ -4,11 +4,14 @@ use std::{ path::PathBuf, }; -use crate::routing::routes::{Data, Outcome, Request, Response, Status}; +use crate::handling::{ + file_handlers::NamedFile, + request::Request, + response::{Outcome, Response, ResponseBody, Status}, + routes::Data, +}; use crate::setup::MountPoint; -use super::file_handlers::NamedFile; - pub fn handle_connection(mut stream: TcpStream, mountpoints: Vec<MountPoint>) { let buf_reader = BufReader::new(&mut stream); let http_request: Vec<String> = buf_reader @@ -40,17 +43,14 @@ pub fn handle_connection(mut stream: TcpStream, mountpoints: Vec<MountPoint>) { let mut handled_response: Option<Outcome<Response, Status, Data>> = None; for mountpoint in mountpoints { if !request.uri.starts_with(mountpoint.mountpoint) { - println!("MOUNTPOINT COMPARISON {}", request.uri); continue; } let mounted_request_uri = request.uri.strip_prefix(mountpoint.mountpoint).unwrap(); for route in mountpoint.routes { if route.method != request.method { - println!("METHOD COMPARE {}", request.method); continue; } if !route.compare_uri(mounted_request_uri) { - println!("URI COMPARISON {} {}", mounted_request_uri, route.uri); continue; } handled_response = Some((route.handler)( @@ -71,23 +71,25 @@ pub fn handle_connection(mut stream: TcpStream, mountpoints: Vec<MountPoint>) { + "\r\n\r\n", success.body.get_data(), ), - Outcome::Failure(error) => handler_404(error), - Outcome::Forward(_) => handler_404(Status { code: 404 }), + Outcome::Failure(error) => failure_handler(error), + Outcome::Forward(_) => failure_handler(Status { code: 404 }), }, - None => handler_404(Status { code: 404 }), + None => failure_handler(Status { code: 404 }), }; stream.write_all(response.0.as_bytes()).unwrap(); stream.write(&response.1).unwrap(); } -fn handler_404(status: Status) -> (String, Vec<u8>) { +fn failure_handler(status: Status) -> (String, Vec<u8>) { let page_404 = NamedFile::open(PathBuf::from("404.html")).unwrap(); ( format!( "HTTP/1.1 {} NOT FOUND\r\nContent-Length: {}\r\nContent-Type: {}\r\n\r\n", - status.code, page_404.content_len, page_404.content_type + status.code, + page_404.get_len(), + page_404.get_mime() ), - page_404.content, + page_404.get_data(), ) } diff --git a/core/http/src/handlers/mod.rs b/core/http/src/handlers/mod.rs index 826c4f4a8da69e957ae06d869b949c5e82dcad32..c3d449565249e4defdc38ea3426fe3e09d00730c 100644 --- a/core/http/src/handlers/mod.rs +++ b/core/http/src/handlers/mod.rs @@ -1,2 +1 @@ -pub mod file_handlers; pub mod handlers; diff --git a/core/http/src/handlers/file_handlers.rs b/core/http/src/handling/file_handlers.rs similarity index 87% rename from core/http/src/handlers/file_handlers.rs rename to core/http/src/handling/file_handlers.rs index 879da038ee564a468bfec27e99cd564f07fb22c5..fdfd3d0a1fc50e8028acc174a1579ee118058cb2 100644 --- a/core/http/src/handlers/file_handlers.rs +++ b/core/http/src/handling/file_handlers.rs @@ -2,7 +2,7 @@ use std::{fs, path::PathBuf}; use mime::Mime; -use crate::routing::routes::{ResponseBody, Status}; +use crate::handling::response::{ResponseBody, Status}; #[derive(Debug)] pub struct NamedFile { @@ -15,6 +15,14 @@ impl ResponseBody for NamedFile { fn get_data(&self) -> Vec<u8> { self.content.clone() } + + fn get_mime(&self) -> Mime { + self.content_type.clone() + } + + fn get_len(&self) -> usize { + self.content_len + } } impl NamedFile { diff --git a/core/http/src/routing/methods.rs b/core/http/src/handling/methods.rs similarity index 100% rename from core/http/src/routing/methods.rs rename to core/http/src/handling/methods.rs diff --git a/core/http/src/handling/mod.rs b/core/http/src/handling/mod.rs new file mode 100644 index 0000000000000000000000000000000000000000..20cf31e7b362da8f020dd48a8df4047c13c1d093 --- /dev/null +++ b/core/http/src/handling/mod.rs @@ -0,0 +1,5 @@ +pub mod file_handlers; +pub mod methods; +pub mod request; +pub mod response; +pub mod routes; diff --git a/core/http/src/handling/request.rs b/core/http/src/handling/request.rs new file mode 100644 index 0000000000000000000000000000000000000000..d37ebb50824a2cf61cf4fdf54b79d69030c4f21f --- /dev/null +++ b/core/http/src/handling/request.rs @@ -0,0 +1,24 @@ +use std::net::SocketAddr; + +use super::{methods::Method, routes::Uri}; + +type HeaderMap = Vec<String>; +#[derive(Clone)] +pub struct Request<'a> { + pub uri: Uri<'a>, + pub headers: HeaderMap, + pub method: Method, + // pub connection: ConnectionMeta, +} + +struct ConnectionMeta { + remote: Option<SocketAddr>, + // certificates +} + +#[derive(Clone, Debug, Copy)] +pub enum MediaType { + Json, + Plain, + Html, +} diff --git a/core/http/src/handling/response.rs b/core/http/src/handling/response.rs new file mode 100644 index 0000000000000000000000000000000000000000..9ca6f4b389353fa203ec4828da0b193ef29b1516 --- /dev/null +++ b/core/http/src/handling/response.rs @@ -0,0 +1,70 @@ +use mime::Mime; + +use super::routes::Body; + +type HeaderMap = Vec<String>; + +#[derive(Debug)] +pub enum Outcome<S, E, F> { + Success(S), + Failure(E), + Forward(F), +} + +pub trait ResponseBody { + fn get_data(&self) -> Vec<u8>; + fn get_mime(&self) -> Mime; + fn get_len(&self) -> usize; +} + +impl ResponseBody for Body { + fn get_data(&self) -> Vec<u8> { + self.body.clone() + } + fn get_mime(&self) -> Mime { + mime::TEXT_PLAIN + } + + fn get_len(&self) -> usize { + self.get_data().len() + } +} + +impl ResponseBody for &str { + fn get_data(&self) -> Vec<u8> { + self.as_bytes().to_vec() + } + + fn get_mime(&self) -> Mime { + mime::TEXT_PLAIN + } + + fn get_len(&self) -> usize { + self.len() + } +} + +impl ResponseBody for String { + fn get_data(&self) -> Vec<u8> { + self.as_bytes().to_vec() + } + + fn get_mime(&self) -> Mime { + mime::TEXT_PLAIN + } + + fn get_len(&self) -> usize { + self.len() + } +} + +pub struct Response { + pub headers: HeaderMap, + pub status: Option<Status>, + pub body: Box<dyn ResponseBody>, +} + +#[derive(Debug)] +pub struct Status { + pub code: u16, +} diff --git a/core/http/src/routing/routes.rs b/core/http/src/handling/routes.rs similarity index 63% rename from core/http/src/routing/routes.rs rename to core/http/src/handling/routes.rs index 6da12188eeb779b0890c1a5162ca3b66507f4420..fd3512d0e6a5ab2f2ceb7a30eb70631c6af7c050 100644 --- a/core/http/src/routing/routes.rs +++ b/core/http/src/handling/routes.rs @@ -1,10 +1,8 @@ -use std::net::SocketAddr; - -use super::methods::Method; - -pub trait ResponseBody { - fn get_data(&self) -> Vec<u8>; -} +use super::{ + methods::Method, + request::{MediaType, Request}, + response::{Outcome, Response, Status}, +}; pub struct RoutInfo { name: Option<&'static str>, @@ -55,63 +53,17 @@ impl Route<'_> { return false; } } - return true; + true } } -#[derive(Clone)] -pub struct Request<'a> { - pub uri: Uri<'a>, - pub headers: HeaderMap, - pub method: Method, - // pub connection: ConnectionMeta, -} - -struct ConnectionMeta { - remote: Option<SocketAddr>, - // certificates -} - -type HeaderMap = Vec<String>; pub type Uri<'a> = &'a str; -#[derive(Debug)] -pub enum Outcome<S, E, F> { - Success(S), - Failure(E), - Forward(F), -} - -#[derive(Clone, Debug, Copy)] -pub enum MediaType { - Json, - Plain, - Html, -} - -#[derive(Debug)] -pub struct Status { - pub code: u16, -} - -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct Body { - pub size: Option<usize>, pub body: Vec<u8>, } -impl ResponseBody for Body { - fn get_data(&self) -> Vec<u8> { - self.body.clone() - } -} - -pub struct Response { - pub headers: HeaderMap, - pub status: Option<Status>, - pub body: Box<dyn ResponseBody>, -} - #[derive(Debug, Clone)] pub struct Data { pub buffer: Vec<u8>, diff --git a/core/http/src/lib.rs b/core/http/src/lib.rs index bf072c9effda051de92bec7d014632d7f7237860..7a1379c13fe8fe4d268bf66b20e0c96ae1545bd5 100644 --- a/core/http/src/lib.rs +++ b/core/http/src/lib.rs @@ -1,5 +1,5 @@ pub mod handlers; -pub mod routing; +pub mod handling; mod setup; mod threading; diff --git a/core/http/src/routing/mod.rs b/core/http/src/routing/mod.rs deleted file mode 100644 index f1e52f9c20770f0918189a5ab5f14057fba84c4a..0000000000000000000000000000000000000000 --- a/core/http/src/routing/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -pub mod methods; -pub mod routes; diff --git a/core/http/src/setup.rs b/core/http/src/setup.rs index 35c8a06a0a672cbd228a13e2b32f5c98381884a1..e2ba2328688c9385b5e06bb8092ef9aef252b29a 100644 --- a/core/http/src/setup.rs +++ b/core/http/src/setup.rs @@ -9,7 +9,7 @@ use std::{ use crate::{ handlers::handlers::handle_connection, - routing::routes::{Route, Uri}, + handling::routes::{Route, Uri}, threading::ThreadPool, }; diff --git a/site/404.html b/site/404.html index 550831fc747d34725736886d29e8281579f54ec5..d4c09f49bc9d7f84d662038a7ef4a469d9279dde 100644 --- a/site/404.html +++ b/site/404.html @@ -11,6 +11,9 @@ <label for="message">Enter your message:</label> <input type="text" id="message" name="message" /> <button type="submit">Send</button> + <label for="message">Enter your message:</label> + <input type="text" id="asdf" name="message" /> + <button type="submit">Send</button> </form> </body> </html> diff --git a/site/src/main.rs b/site/src/main.rs index 8974db9eff8fc24d6dd40ed41ee5fed4d4d74abd..da51a333454d60fb0be7d7af9d50539be641bc93 100644 --- a/site/src/main.rs +++ b/site/src/main.rs @@ -1,31 +1,31 @@ use std::path::PathBuf; -use http::{ - handlers::file_handlers::NamedFile, - routing::routes::{Data, Outcome, Request, Response, Route, Status}, +use http::handling::{ + file_handlers::NamedFile, + methods::Method, + request::Request, + response::{Outcome, Response, ResponseBody, Status}, + routes::{Data, Route}, }; fn handler(request: Request, _data: Data) -> Outcome<Response, Status, Data> { - println!("called"); let response = fileserver(request.uri.strip_prefix("static/").unwrap()); let response = match response { Ok(dat) => Response { headers: vec![ - format!("Content-Length: {}", dat.content_len), - format!("Content-Type: {}", dat.content_type), + format!("Content-Length: {}", dat.get_len()), + format!("Content-Type: {}", dat.get_mime()), ], status: Some(Status { code: 200 }), body: Box::new(dat), }, Err(_) => return Outcome::Failure(Status { code: 404 }), }; - println!("{:?}", response.headers); Outcome::Success(response) } fn fileserver(path: &str) -> Result<NamedFile, Status> { - let file = NamedFile::open(PathBuf::from("static/".to_string() + path)); - return file; + NamedFile::open(PathBuf::from("static/".to_string() + path)) } fn main() { @@ -34,7 +34,7 @@ fn main() { handler, name: Some("file_server"), uri: "static/<path..>", - method: http::routing::methods::Method::Get, + method: Method::Get, rank: 0, }; diff --git a/site/static/hello.css b/site/static/hello.css new file mode 100644 index 0000000000000000000000000000000000000000..aba0df0603e473b6811c64f41e53ebd3559100d9 --- /dev/null +++ b/site/static/hello.css @@ -0,0 +1,4 @@ +body { + background-color: #212121; + color: #ffffff; +} diff --git a/site/static/hello.html b/site/static/hello.html index 1afeefd5312b9461c8b6392dcd67bc8d0dfb29bd..ff042af8d7af0972f5997ef4813dafbe995dc9b2 100644 --- a/site/static/hello.html +++ b/site/static/hello.html @@ -2,6 +2,11 @@ <html> <head> <meta charset="UTF-8" /> - <title>JE</title> + <title>Hello</title> + <link rel="stylesheet" href="/static/hello.css" /> </head> + <body> + <h1>Managed</h1> + <img src="/static/img.jpg" alt="" /> + </body> </html> diff --git a/site/static/img.jpg b/site/static/img.jpg new file mode 100644 index 0000000000000000000000000000000000000000..07eb7f85ac26c70344d552f56a81c44a0bf1b3db Binary files /dev/null and b/site/static/img.jpg differ diff --git a/tree.txt b/tree.txt new file mode 100644 index 0000000000000000000000000000000000000000..05d432b7e346d3f283399b681f39d6cb0463c3fd --- /dev/null +++ b/tree.txt @@ -0,0 +1,44 @@ +. +├── core +│  ├── codegen +│  │  ├── Cargo.lock +│  │  ├── Cargo.toml +│  │  ├── src +│  │  │  └── lib.rs +│  ├── html +│  │  ├── Cargo.lock +│  │  ├── Cargo.toml +│  │  └── src +│  │    └── lib.rs +│  └── http +│  ├── Cargo.lock +│  ├── Cargo.toml +│  └── src +│    ├── handlers +│    │  ├── file_handlers.rs +│    │  ├── handlers.rs +│    │  └── mod.rs +│    ├── lib.rs +│    ├── routing +│    │  ├── methods.rs +│    │  ├── mod.rs +│    │  └── routes.rs +│    ├── setup.rs +│    ├── threading.rs +│    └── utils +│    └── mod.rs +├── README.md +├── site +│  ├── 404.html +│  ├── Cargo.lock +│  ├── Cargo.toml +│  ├── hello.css +│  ├── img.jpg +│  ├── src +│  │  └── main.rs +│  ├── static +│  │  └── hello.html +│  └── target +│  └── CACHEDIR.TAG +└── tree.txt +