firefox communication

This commit is contained in:
maxstrb 2025-10-13 21:36:52 +02:00
parent 80fabcde47
commit 40ccfa01d6
3 changed files with 82 additions and 25 deletions

View file

@ -5,34 +5,49 @@ mod shared_enums;
use std::{ use std::{
io::{BufReader, Write}, io::{BufReader, Write},
net::TcpListener, net::TcpListener,
time::Duration,
}; };
use crate::{ use crate::{
request::ServerPath,
response::{Response, ResponseCode, ResponseHeader}, response::{Response, ResponseCode, ResponseHeader},
shared_enums::Content, shared_enums::{Content, ContentType},
}; };
fn main() -> std::io::Result<()> { fn main() -> std::io::Result<()> {
let listener = TcpListener::bind("127.0.0.1:8080")?; let listener = TcpListener::bind("127.0.0.1:8080")?;
for incoming_stream in listener.incoming() { for incoming_stream in listener.incoming() {
let stream = incoming_stream?; let mut stream = incoming_stream?;
stream.set_read_timeout(Some(Duration::from_millis(500)))?;
let reader = BufReader::new(&stream); loop {
let reader = BufReader::new(&stream);
let req = match request::Request::from_bufreader(reader) {
Ok(r) => r,
Err(_) => break,
};
let req = request::Request::from_bufreader(reader)?; println!("{req:?}");
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)))),
let mut writer = stream; _ => Response::new()
let response = Response::new()
.with_code(ResponseCode::Ok) .with_code(ResponseCode::Ok)
.with_data(b"<!doctype html><html lang=\"en\"><head><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())),
};
response.respond(&mut writer)?; response.respond(&mut stream)?;
writer.flush()?; stream.flush()?;
if req.headers.contains(&request::RequestHeader::Connection(
request::Connection::Close,
)) {
break;
}
}
} }
Ok(()) Ok(())
} }

View file

@ -19,13 +19,42 @@ pub struct Request {
pub body: Option<Box<[u8]>>, pub body: Option<Box<[u8]>>,
} }
#[derive(Debug)] #[derive(Debug, PartialEq)]
pub enum RequestHeader { pub enum RequestHeader {
Host(String), Host(String),
UserAgent(String), UserAgent(String),
ContentType(Content), ContentType(Content),
Accept(Vec<Content>), Accept(Vec<Content>),
Other(Box<str>, Box<str>), Connection(Connection),
Upgrade(Vec<Upgrade>),
Other { name: Box<str>, value: Box<str> },
}
#[derive(Debug, PartialEq)]
pub enum Connection {
Close,
KeepAlive,
Upgrade,
Other(Box<str>),
}
#[derive(Debug, PartialEq)]
pub struct Upgrade {
protocol: Protocol,
version: Box<str>,
}
impl FromStr for Upgrade {
type Err = io::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
todo!()
}
}
#[derive(Debug, PartialEq)]
pub enum Protocol {
HTTP,
Websocket,
} }
impl FromStr for RequestHeader { impl FromStr for RequestHeader {
@ -45,10 +74,22 @@ impl FromStr for RequestHeader {
.map(Content::from_str) .map(Content::from_str)
.collect::<Result<Vec<Content>, io::Error>>()?, .collect::<Result<Vec<Content>, io::Error>>()?,
)), )),
_ => Ok(RequestHeader::Other( "Connection" => Ok(RequestHeader::Connection(match *value {
header_type.to_string().into_boxed_str(), "close" => Connection::Close,
value.to_string().into_boxed_str(), "keep-alive" => Connection::KeepAlive,
"Upgrade" => Connection::Upgrade,
other => Connection::Other(other.to_string().into_boxed_str()),
})),
"Upgrade" => Ok(RequestHeader::Upgrade(
value
.split(',')
.map(Upgrade::from_str)
.collect::<Result<Vec<Upgrade>, io::Error>>()?,
)), )),
_ => Ok(RequestHeader::Other {
name: header_type.to_string().into_boxed_str(),
value: value.to_string().into_boxed_str(),
}),
}, },
_ => Err(io::Error::new( _ => Err(io::Error::new(
io::ErrorKind::InvalidData, io::ErrorKind::InvalidData,
@ -81,7 +122,7 @@ impl Request {
let header = RequestHeader::from_str(&current_line)?; let header = RequestHeader::from_str(&current_line)?;
headers.push(header); headers.push(header);
if let RequestHeader::Other(_, _) = headers.last().unwrap() { if let RequestHeader::Other { .. } = headers.last().unwrap() {
continue; continue;
} }

View file

@ -1,6 +1,6 @@
use std::{io, str::FromStr}; use std::{io, str::FromStr};
#[derive(Debug)] #[derive(Debug, PartialEq)]
pub enum ContentType { pub enum ContentType {
Text(TextType), Text(TextType),
Aplication(ApplicationType), Aplication(ApplicationType),
@ -21,7 +21,7 @@ impl ContentType {
} }
} }
#[derive(Debug)] #[derive(Debug, PartialEq)]
pub enum Parameter { pub enum Parameter {
Preference(f32), Preference(f32),
Charset(Charset), Charset(Charset),
@ -38,7 +38,7 @@ impl Parameter {
} }
} }
#[derive(Debug)] #[derive(Debug, PartialEq)]
pub enum Charset { pub enum Charset {
UTF8, UTF8,
} }
@ -51,7 +51,7 @@ impl Charset {
} }
} }
#[derive(Debug)] #[derive(Debug, PartialEq)]
pub struct Content { pub struct Content {
pub content_type: ContentType, pub content_type: ContentType,
pub parameter: Option<Vec<Parameter>>, pub parameter: Option<Vec<Parameter>>,
@ -232,6 +232,7 @@ impl FromStr for ContentType {
["application", "*"] => Ok(ContentType::Aplication(ApplicationType::Any)), ["application", "*"] => Ok(ContentType::Aplication(ApplicationType::Any)),
["image", "png"] => Ok(ContentType::Image(Image::Png)), ["image", "png"] => Ok(ContentType::Image(Image::Png)),
["image", "apng"] => Ok(ContentType::Image(Image::Png)),
["image", "jpeg"] | ["image", "jpg"] => Ok(ContentType::Image(Image::Jpeg)), ["image", "jpeg"] | ["image", "jpg"] => Ok(ContentType::Image(Image::Jpeg)),
["image", "avif"] => Ok(ContentType::Image(Image::Avif)), ["image", "avif"] => Ok(ContentType::Image(Image::Avif)),
["image", "webp"] => Ok(ContentType::Image(Image::Webp)), ["image", "webp"] => Ok(ContentType::Image(Image::Webp)),
@ -246,7 +247,7 @@ impl FromStr for ContentType {
} }
} }
#[derive(Debug)] #[derive(Debug, PartialEq)]
pub enum Image { pub enum Image {
Png, Png,
Avif, Avif,
@ -269,7 +270,7 @@ impl Image {
} }
} }
#[derive(Debug)] #[derive(Debug, PartialEq)]
pub enum TextType { pub enum TextType {
Html, Html,
Css, Css,
@ -288,7 +289,7 @@ impl TextType {
} }
} }
#[derive(Debug)] #[derive(Debug, PartialEq)]
pub enum ApplicationType { pub enum ApplicationType {
Json, Json,
Any, Any,