This commit is contained in:
Arkitu 2025-08-28 14:35:25 +02:00
parent ea43024446
commit 785671d8cd
2 changed files with 23 additions and 13 deletions

View File

@ -17,18 +17,18 @@ pub mod ws;
#[cfg(feature = "ttt")] #[cfg(feature = "ttt")]
#[embassy_executor::task(pool_size = 2)] #[embassy_executor::task(pool_size = 2)]
pub async fn ttt_listen_task(stack: embassy_net::Stack<'static>, team: apps::ttt::Team, port: u16) { pub async fn ttt_listen_task(stack: embassy_net::Stack<'static>, team: apps::ttt::Team, port: u16) {
listen_task::<32, 32, 256, 256>(stack, apps::ttt::TttApp::new(team), port).await listen_task::<32, 32, 1024, 256>(stack, apps::ttt::TttApp::new(team), port).await
} }
#[embassy_executor::task(pool_size = 2)] #[embassy_executor::task(pool_size = 2)]
pub async fn index_listen_task(stack: embassy_net::Stack<'static>, port: u16) { pub async fn index_listen_task(stack: embassy_net::Stack<'static>, port: u16) {
listen_task::<64, 0, 1024, 1024>(stack, apps::index::IndexApp, port).await listen_task::<64, 0, 2048, 1024>(stack, apps::index::IndexApp, port).await
} }
#[cfg(feature = "chat")] #[cfg(feature = "chat")]
#[embassy_executor::task(pool_size = 4)] #[embassy_executor::task(pool_size = 4)]
pub async fn chat_listen_task(stack: embassy_net::Stack<'static>, id: u8, port: u16) { pub async fn chat_listen_task(stack: embassy_net::Stack<'static>, id: u8, port: u16) {
listen_task::<64, 512, 512, 512>(stack, apps::chat::ChatApp::new(id), port).await listen_task::<64, 1024, 1024, 1024>(stack, apps::chat::ChatApp::new(id), port).await
} }
pub async fn listen_task< pub async fn listen_task<
@ -61,7 +61,7 @@ pub async fn listen_task<
socket.remote_endpoint() socket.remote_endpoint()
); );
let (mut rx, mut tx) = socket.split(); // let (mut rx, mut tx) = socket.split();
let mut buf = String::<BUF_LEN>::new(); let mut buf = String::<BUF_LEN>::new();
let mut path = String::<PATH_LEN>::new(); let mut path = String::<PATH_LEN>::new();
let mut request_type = HttpRequestType::Get; let mut request_type = HttpRequestType::Get;
@ -69,13 +69,16 @@ pub async fn listen_task<
loop { loop {
Timer::after_secs(0).await; Timer::after_secs(0).await;
match rx match socket
.read_with(|msg| { .read_with(|msg| {
let (headers, content) = match from_utf8(msg) { let (headers, content) = match from_utf8(msg) {
Ok(b) => match b.split_once("\r\n\r\n") { Ok(b) => {
Some(t) => t, info!("{}", b);
None => (b, ""), match b.split_once("\r\n\r\n") {
}, Some(t) => t,
None => (b, ""),
}
}
Err(_) => { Err(_) => {
warn!("Non utf8 http request"); warn!("Non utf8 http request");
return (0, Err(())); return (0, Err(()));
@ -230,10 +233,10 @@ pub async fn listen_task<
}; };
let w: Result<(), embassy_net::tcp::Error> = try { let w: Result<(), embassy_net::tcp::Error> = try {
tx.write_all(&head_buf).await?; socket.write_all(&head_buf).await?;
if let Some(ref c) = res_content { if let Some(ref c) = res_content {
for s in c.0.iter() { for s in c.0.iter() {
tx.write_all(s.as_bytes()).await?; socket.write_all(s.as_bytes()).await?;
} }
} }
}; };
@ -242,6 +245,10 @@ pub async fn listen_task<
warn!("write error: {:?}", e); warn!("write error: {:?}", e);
break; break;
}; };
if is_ws {
break;
}
} }
if is_ws { if is_ws {
let mut buf = buf.into_bytes(); let mut buf = buf.into_bytes();
@ -297,13 +304,13 @@ impl Into<&str> for HttpResCode {
fn compute_ws_accept(key: &str) -> Result<String<28>, ()> { fn compute_ws_accept(key: &str) -> Result<String<28>, ()> {
let mut res = Vec::<u8, 28>::new(); let mut res = Vec::<u8, 28>::new();
res.fill(0); res.resize_default(28)?;
let mut hasher = Sha1::new(); let mut hasher = Sha1::new();
hasher.update(key.as_bytes()); hasher.update(key.as_bytes());
hasher.update(b"258EAFA5-E914-47DA-95CA-C5AB0DC85B11"); hasher.update(b"258EAFA5-E914-47DA-95CA-C5AB0DC85B11");
let hash = hasher.finalize(); let hash = hasher.finalize();
if let Err(e) = BASE64_STANDARD.encode_slice(hash, &mut res) { if let Err(e) = BASE64_STANDARD.encode_slice(hash, &mut res) {
warn!("Error while base64 encoding : {}", e); warn!("Error while base64 encoding : {}\nkey: {}", e, key);
return Err(()); return Err(());
}; };
match String::from_utf8(res) { match String::from_utf8(res) {

View File

@ -67,6 +67,9 @@ impl<'a> WsTx<'a> {
) -> Result<(), ()> { ) -> Result<(), ()> {
self.socket self.socket
.write_with(|buf| { .write_with(|buf| {
if buf.len() < 6 {
return (0, Err(()));
}
buf[0] = 0b1000_0000 | msg_code; buf[0] = 0b1000_0000 | msg_code;
let Ok(n) = f(&mut buf[4..]) else { let Ok(n) = f(&mut buf[4..]) else {
return (0, Err(())); return (0, Err(()));