diff --git a/src/main.rs b/src/main.rs index 7ff2561..e1fef7e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,7 +9,7 @@ use std::{ }; use crate::{ - request::ServerPath, + request::Connection, response::{Response, ResponseCode, ResponseHeader}, shared_enums::{Content, ContentType}, }; @@ -30,12 +30,12 @@ fn main() -> std::io::Result<()> { println!("{req:?}"); let response = match req.path.path.to_string().as_str(){ - "css.css" => Response::new().with_code(ResponseCode::Ok).with_data(b"body{background-color: #000000;}".to_vec()).with_header(ResponseHeader::ContentType(Content::new(ContentType::Text(shared_enums::TextType::Css)))), + "css.css" => Response::new().with_code(ResponseCode::Ok).with_data(b"body{background-color: #ff0000;}".to_vec()).with_header(ResponseHeader::ContentType(Content::new(ContentType::Text(shared_enums::TextType::Css)))), _ => Response::new() .with_code(ResponseCode::Ok) .with_data(b"Hello World!

Ahojky

Jou jou jou

".to_vec()) - .with_header(ResponseHeader::ContentType(Content::html_utf8())), + .with_header(ResponseHeader::ContentType(Content::html_utf8())).with_header(ResponseHeader::Connection(Connection::KeepAlive)), }; response.respond(&mut stream)?; @@ -45,6 +45,7 @@ fn main() -> std::io::Result<()> { if req.headers.contains(&request::RequestHeader::Connection( request::Connection::Close, )) { + println!("Connection closed"); break; } } diff --git a/src/request.rs b/src/request.rs index 43bcabb..a08301d 100644 --- a/src/request.rs +++ b/src/request.rs @@ -38,6 +38,17 @@ pub enum Connection { Other(Box), } +impl Connection { + pub fn to_str(&self) -> Box { + match self { + Connection::Close => "close".into(), + Connection::KeepAlive => "keep-alive".into(), + Connection::Upgrade => "Upgrade".into(), + Connection::Other(o) => o.clone(), + } + } +} + #[derive(Debug, PartialEq)] pub struct Upgrade { protocol: Protocol, diff --git a/src/response.rs b/src/response.rs index 7ce93b2..77cc58a 100644 --- a/src/response.rs +++ b/src/response.rs @@ -3,7 +3,7 @@ use std::{ net::TcpStream, }; -use crate::shared_enums::Content; +use crate::{request::Connection, shared_enums::Content}; pub struct Response { http_version: Box, @@ -19,7 +19,8 @@ impl Response { output.extend_from_slice(b"\r\n"); if !self.data.is_empty() { - output.extend_from_slice(b"\r\n"); + output.extend_from_slice(format!("Content-Length: {}", self.data.len()).as_bytes()); + output.extend_from_slice(b"\r\n\r\n"); output.extend_from_slice(&self.data); } @@ -215,20 +216,20 @@ impl ResponseCode { } pub enum ResponseHeader { - ContentLength(u32), ContentType(Content), CacheControl(CacheControl), + Connection(Connection), } impl ResponseHeader { fn to_str(&self) -> Box { type R = ResponseHeader; match self { - R::ContentLength(length) => format!("Content-Length: {length}").into_boxed_str(), R::ContentType(content) => { format!("Content-Type: {}", content.to_str()).into_boxed_str() } R::CacheControl(c) => format!("Cache-Control: {}", c.to_str()).into_boxed_str(), + R::Connection(c) => format!("Connection: {}", c.to_str()).into_boxed_str(), } } }