keep alive working I feel like a god frfr (it's late an this is my personal project so if anyone is reading this (including my future self) why?)

This commit is contained in:
maxstrb 2025-10-13 22:04:57 +02:00
parent 40ccfa01d6
commit a9ccdaf3f5
3 changed files with 20 additions and 7 deletions

View file

@ -9,7 +9,7 @@ use std::{
}; };
use crate::{ use crate::{
request::ServerPath, request::Connection,
response::{Response, ResponseCode, ResponseHeader}, response::{Response, ResponseCode, ResponseHeader},
shared_enums::{Content, ContentType}, shared_enums::{Content, ContentType},
}; };
@ -30,12 +30,12 @@ fn main() -> std::io::Result<()> {
println!("{req:?}"); println!("{req:?}");
let response = match req.path.path.to_string().as_str(){ 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() _ => Response::new()
.with_code(ResponseCode::Ok) .with_code(ResponseCode::Ok)
.with_data(b"<!doctype html><html lang=\"en\"><head><link rel=\"stylesheet\" href=\"css.css\"><meta charset=\"UTF-8\"/><title>Hello World!</title></head><body><h1>Ahojky</h1><p>Jou jou jou</p></body></html>".to_vec()) .with_data(b"<!doctype html><html lang=\"en\"><head><link rel=\"stylesheet\" href=\"css.css\"><meta charset=\"UTF-8\"/><title>Hello World!</title></head><body><h1>Ahojky</h1><p>Jou jou jou</p></body></html>".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)?; response.respond(&mut stream)?;
@ -45,6 +45,7 @@ fn main() -> std::io::Result<()> {
if req.headers.contains(&request::RequestHeader::Connection( if req.headers.contains(&request::RequestHeader::Connection(
request::Connection::Close, request::Connection::Close,
)) { )) {
println!("Connection closed");
break; break;
} }
} }

View file

@ -38,6 +38,17 @@ pub enum Connection {
Other(Box<str>), Other(Box<str>),
} }
impl Connection {
pub fn to_str(&self) -> Box<str> {
match self {
Connection::Close => "close".into(),
Connection::KeepAlive => "keep-alive".into(),
Connection::Upgrade => "Upgrade".into(),
Connection::Other(o) => o.clone(),
}
}
}
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub struct Upgrade { pub struct Upgrade {
protocol: Protocol, protocol: Protocol,

View file

@ -3,7 +3,7 @@ use std::{
net::TcpStream, net::TcpStream,
}; };
use crate::shared_enums::Content; use crate::{request::Connection, shared_enums::Content};
pub struct Response { pub struct Response {
http_version: Box<str>, http_version: Box<str>,
@ -19,7 +19,8 @@ impl Response {
output.extend_from_slice(b"\r\n"); output.extend_from_slice(b"\r\n");
if !self.data.is_empty() { 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); output.extend_from_slice(&self.data);
} }
@ -215,20 +216,20 @@ impl ResponseCode {
} }
pub enum ResponseHeader { pub enum ResponseHeader {
ContentLength(u32),
ContentType(Content), ContentType(Content),
CacheControl(CacheControl), CacheControl(CacheControl),
Connection(Connection),
} }
impl ResponseHeader { impl ResponseHeader {
fn to_str(&self) -> Box<str> { fn to_str(&self) -> Box<str> {
type R = ResponseHeader; type R = ResponseHeader;
match self { match self {
R::ContentLength(length) => format!("Content-Length: {length}").into_boxed_str(),
R::ContentType(content) => { R::ContentType(content) => {
format!("Content-Type: {}", content.to_str()).into_boxed_str() format!("Content-Type: {}", content.to_str()).into_boxed_str()
} }
R::CacheControl(c) => format!("Cache-Control: {}", c.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(),
} }
} }
} }