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

View file

@ -19,13 +19,42 @@ pub struct Request {
pub body: Option<Box<[u8]>>,
}
#[derive(Debug)]
#[derive(Debug, PartialEq)]
pub enum RequestHeader {
Host(String),
UserAgent(String),
ContentType(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 {
@ -45,10 +74,22 @@ impl FromStr for RequestHeader {
.map(Content::from_str)
.collect::<Result<Vec<Content>, io::Error>>()?,
)),
_ => Ok(RequestHeader::Other(
header_type.to_string().into_boxed_str(),
value.to_string().into_boxed_str(),
"Connection" => Ok(RequestHeader::Connection(match *value {
"close" => Connection::Close,
"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(
io::ErrorKind::InvalidData,
@ -81,7 +122,7 @@ impl Request {
let header = RequestHeader::from_str(&current_line)?;
headers.push(header);
if let RequestHeader::Other(_, _) = headers.last().unwrap() {
if let RequestHeader::Other { .. } = headers.last().unwrap() {
continue;
}

View file

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