From e6bc8561d4946b81186677486b5be1600146cecb Mon Sep 17 00:00:00 2001 From: Arkitu <85173315+Arkitu@users.noreply.github.com> Date: Fri, 2 May 2025 00:37:11 +0200 Subject: [PATCH] save --- src/apps/chat.html | 9 ++------ src/apps/chat.rs | 33 ++++++++++++++++++---------- src/apps/index.rs | 9 ++++++-- src/apps/mod.rs | 9 ++++++-- src/apps/ttt.rs | 9 ++++++-- src/socket.rs | 55 +++++++++++++++------------------------------- 6 files changed, 62 insertions(+), 62 deletions(-) diff --git a/src/apps/chat.html b/src/apps/chat.html index 706ec69..040f456 100644 --- a/src/apps/chat.html +++ b/src/apps/chat.html @@ -33,19 +33,14 @@
Enter your name : -
diff --git a/src/apps/chat.rs b/src/apps/chat.rs index 38665a8..ea30145 100644 --- a/src/apps/chat.rs +++ b/src/apps/chat.rs @@ -1,10 +1,10 @@ -use core::fmt::Write; +use core::{fmt::Write, str::from_utf8}; use heapless::{String, Vec}; use log::info; use pico_website::unwrap; use ringbuffer::{ConstGenericRingBuffer, RingBuffer}; -use crate::socket::HttpResCode; +use crate::socket::{HttpRequestType, HttpResCode}; use super::App; @@ -47,16 +47,21 @@ impl App for ChatApp { fn socket_name(&self) -> &'static str { "chat" } - async fn handle_request<'a>(&'a mut self, path: &str) -> (HttpResCode, &'static str, &'a [u8]) { - match path { - "/" | "/index" | "/index.html" | "/chat" | "/chat.html" => { + async fn handle_request<'a>( + &'a mut self, + path: &str, + req_type: HttpRequestType, + content: &str, + ) -> (HttpResCode, &'static str, &'a [u8]) { + match (req_type, path) { + (HttpRequestType::Get, "/" | "/index" | "/index.html" | "/chat" | "/chat.html") => { (HttpResCode::Ok, "html", include_bytes!("./chat.html")) } - path => { + (req_type, path) => { let (path, args) = path.split_once('?').unwrap_or((path, "")); let mut load = None; let mut username = None; - for arg in args.split('&') { + for arg in args.split('&').chain(content.split('&')) { match arg.split_once('=') { Some(("load", n)) => { let n: u16 = match n.parse() { @@ -76,18 +81,22 @@ impl App for ChatApp { _ => {} } } + if path == "/chat" && username.is_some() { - return ( - HttpResCode::Ok, - "html", + self.res_buf.clear(); + unwrap(write!( + &mut self.res_buf, "
", - ); + MEMORY_SIZE + )) + .await; + return (HttpResCode::Ok, "html", &self.res_buf); } else if path.starts_with("/chat/message/abs/") && path.len() > 18 { let msg_id: u16 = match path[18..].parse() { Ok(n) => n, diff --git a/src/apps/index.rs b/src/apps/index.rs index 03114cf..59b02dd 100644 --- a/src/apps/index.rs +++ b/src/apps/index.rs @@ -1,4 +1,4 @@ -use crate::socket::HttpResCode; +use crate::socket::{HttpRequestType, HttpResCode}; use super::App; @@ -7,7 +7,12 @@ impl App for IndexApp { fn socket_name(&self) -> &'static str { "index" } - async fn handle_request<'a>(&'a mut self, path: &str) -> (HttpResCode, &'static str, &'a [u8]) { + async fn handle_request<'a>( + &'a mut self, + path: &str, + _req_type: HttpRequestType, + _content: &str, + ) -> (HttpResCode, &'static str, &'a [u8]) { match path { "/" | "/index" | "/index.html" => { (HttpResCode::Ok, "html", include_bytes!("./index.html")) diff --git a/src/apps/mod.rs b/src/apps/mod.rs index 4074c14..d941728 100644 --- a/src/apps/mod.rs +++ b/src/apps/mod.rs @@ -1,4 +1,4 @@ -use crate::socket::HttpResCode; +use crate::socket::{HttpRequestType, HttpResCode}; pub mod chat; pub mod index; @@ -6,5 +6,10 @@ pub mod ttt; pub trait App { fn socket_name(&self) -> &'static str; - async fn handle_request<'a>(&'a mut self, path: &str) -> (HttpResCode, &'static str, &'a [u8]); + async fn handle_request<'a>( + &'a mut self, + path: &str, + req_type: HttpRequestType, + content: &str, + ) -> (HttpResCode, &'static str, &'a [u8]); } diff --git a/src/apps/ttt.rs b/src/apps/ttt.rs index 3d4d049..d5d5abe 100644 --- a/src/apps/ttt.rs +++ b/src/apps/ttt.rs @@ -5,7 +5,7 @@ use heapless::Vec; use pico_website::unwrap; use portable_atomic::{AtomicBool, AtomicU32}; -use crate::socket::HttpResCode; +use crate::socket::{HttpRequestType, HttpResCode}; use super::App; @@ -154,7 +154,12 @@ impl App for TttApp { fn socket_name(&self) -> &'static str { self.team.name() } - async fn handle_request<'a>(&'a mut self, path: &str) -> (HttpResCode, &'static str, &'a [u8]) { + async fn handle_request<'a>( + &'a mut self, + path: &str, + _req_type: HttpRequestType, + _content: &str, + ) -> (HttpResCode, &'static str, &'a [u8]) { match path { "/" | "/index" | "/index.html" | "/ttt" | "/ttt.html" => { (HttpResCode::Ok, "html", include_bytes!("./ttt.html")) diff --git a/src/socket.rs b/src/socket.rs index 6b2842b..a6d1f91 100644 --- a/src/socket.rs +++ b/src/socket.rs @@ -64,41 +64,31 @@ pub async fn listen_task(stack: embassy_net::Stack<'static>, mut app: impl App, } }; - let mut headers: &[u8] = &buf[..n]; - let mut _content: &[u8] = &[]; - for i in 0..(n - 1) { - if &buf[i..i + 1] == b"\r\n" { - headers = &buf[0..i]; - if i + 2 < n { - _content = &buf[i + 2..n]; - } + let (headers, content) = match from_utf8(&buf[..n]) { + Ok(b) => match b.split_once("\r\n\r\n") { + Some(t) => t, + None => (b, ""), + }, + Err(_) => { + warn!("Non utf8 http request"); + break; } - } + }; - let mut headers = headers.split(|x| *x == b'\n'); - let (request_type, path) = match headers.next() { + // let mut headers = headers.split(|x| *x == b'\n'); + let (request_type, path) = match headers.lines().next() { None => { warn!("Empty request"); break; } Some(l1) => { - let mut l1 = l1.split(|x| *x == b' '); + let mut l1 = l1.split(' '); ( match l1.next() { - Some(b"GET") => HttpRequestType::Get, - Some(b"POST") => HttpRequestType::Post, + Some("GET") => HttpRequestType::Get, + Some("POST") => HttpRequestType::Post, Some(t) => { - match from_utf8(t) { - Ok(t) => { - warn!("Unknown request type : {}", t); - } - Err(e) => { - warn!( - "Error while parsing request type : {}\nRaw type : {:?}", - e, t - ); - } - } + warn!("Unknown request type : {}", t); break; } None => { @@ -107,16 +97,7 @@ pub async fn listen_task(stack: embassy_net::Stack<'static>, mut app: impl App, } }, match l1.next() { - Some(path) => match from_utf8(path) { - Ok(p) => p, - Err(e) => { - warn!( - "Error while parsing requested path : {}\nRaw path : {:?}", - e, path - ); - break; - } - }, + Some(path) => path, None => { warn!("No path"); break; @@ -143,7 +124,7 @@ pub async fn listen_task(stack: embassy_net::Stack<'static>, mut app: impl App, #[cfg(not(debug_assertions))] include_bytes!("../static/htmx.min.js"), ), - p => app.handle_request(p).await, + p => app.handle_request(p, request_type, content).await, }; res_head_buf.clear(); @@ -179,7 +160,7 @@ pub async fn listen_task(stack: embassy_net::Stack<'static>, mut app: impl App, } #[derive(Clone, Copy, Debug)] -enum HttpRequestType { +pub enum HttpRequestType { Get, Post, }