From c629ae5ce3b911672c8b28f56975d59f17ffecae Mon Sep 17 00:00:00 2001 From: Arkitu <85173315+Arkitu@users.noreply.github.com> Date: Fri, 2 May 2025 16:18:52 +0200 Subject: [PATCH] save --- Cargo.lock | 7 +++++++ Cargo.toml | 3 ++- src/apps/chat.html | 17 +++++------------ src/apps/chat.rs | 32 +++++++++++++++++++++++++------- 4 files changed, 39 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f8dbe71..f378d22 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1063,6 +1063,12 @@ version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" +[[package]] +name = "percent-encoding" +version = "2.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" + [[package]] name = "petgraph" version = "0.7.1" @@ -1110,6 +1116,7 @@ dependencies = [ "heapless", "log", "panic-probe", + "percent-encoding", "portable-atomic", "rand_core", "ringbuffer", diff --git a/Cargo.toml b/Cargo.toml index b44d3ee..960ff39 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -58,4 +58,5 @@ serde = { version = "*", optional = true, default-features = false, features = [ ] } dhcparse = { version = "*", default-features = false, optional = true } dnsparse = { version = "*", optional = true } -ringbuffer = { version = "*", default-features = false } \ No newline at end of file +ringbuffer = { version = "*", default-features = false } +percent-encoding = { version = "*", default-features = false } \ No newline at end of file diff --git a/src/apps/chat.html b/src/apps/chat.html index 040f456..3074e84 100644 --- a/src/apps/chat.html +++ b/src/apps/chat.html @@ -19,17 +19,6 @@

Chat

- -
Enter your name : -
diff --git a/src/apps/chat.rs b/src/apps/chat.rs index fcd7259..a3105ee 100644 --- a/src/apps/chat.rs +++ b/src/apps/chat.rs @@ -1,6 +1,7 @@ use core::fmt::Write; use embassy_sync::{blocking_mutex::raw::ThreadModeRawMutex, mutex::Mutex}; use heapless::{String, Vec}; +use percent_encoding::percent_decode_str; use pico_website::unwrap; use ringbuffer::{ConstGenericRingBuffer, RingBuffer}; @@ -9,6 +10,10 @@ use crate::socket::{HttpRequestType, HttpResCode}; use super::App; const MEMORY_SIZE: usize = 16; +const USERNAME_MIN_SIZE: usize = 3; +const USERNAME_SIZE: usize = 16; +const MSG_SIZE: usize = 256; + static MESSAGES: Mutex = Mutex::new(Messages::new()); pub struct ChatApp { @@ -39,6 +44,7 @@ impl App for ChatApp { let (path, args) = path.split_once('?').unwrap_or((path, "")); let mut load = None; let mut username = None; + // let mut msg_content = None; for arg in args.split('&').chain(content.split('&')) { match arg.split_once('=') { Some(("load", n)) => { @@ -51,30 +57,42 @@ impl App for ChatApp { } } Some(("username", u)) => { - if u.len() < 3 || u.len() > 16 { + if u.len() < USERNAME_MIN_SIZE || u.len() > USERNAME_SIZE { return (HttpResCode::BadRequest, "", &[]); } username = Some(u); } + Some(("msg", m)) => { + let mut msg = String::::new(); + for c in percent_decode_str(m) { + msg.push(c as char); + } + if m.len() > MSG_SIZE { + return (HttpResCode::BadRequest, "", &[]); + } + // msg_content = Some(); + } _ => {} } } - - if path == "/chat" && username.is_some() { + if path == "/chat/connect" && username.is_some() { self.res_buf.clear(); unwrap(write!( &mut self.res_buf, "
", - MEMORY_SIZE + MEMORY_SIZE, + username.unwrap() )) .await; return (HttpResCode::Ok, "html", &self.res_buf); + } else if path == "/chat/send" && username.is_some() { + 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, @@ -121,8 +139,8 @@ impl App for ChatApp { } struct Message { - author: String<16>, - content: String<256>, + author: String, + content: String, } struct Messages {