diff --git a/src/apps/chat.rs b/src/apps/chat.rs index bcbc5c4..f2d2f8b 100644 --- a/src/apps/chat.rs +++ b/src/apps/chat.rs @@ -2,7 +2,7 @@ use core::fmt::Write; use dhcparse::dhcpv4::MAX_MESSAGE_SIZE; use embassy_sync::{blocking_mutex::raw::ThreadModeRawMutex, mutex::Mutex}; use heapless::{String, Vec}; -use log::info; +use log::{info, warn}; use percent_encoding::percent_decode_str; use pico_website::unwrap; use ringbuffer::{ConstGenericRingBuffer, RingBuffer}; @@ -72,15 +72,38 @@ impl App for ChatApp { username = Some(name); } Some(("msg", m)) => { - info!("raw_msg: {}", m); - let mut msg = String::::new(); - // TODO: fix this (maybe do it by hand) - for c in percent_decode_str(m) { - if let Err(_) = msg.push(c as char) { + let mut msg = Vec::::new(); + let mut i = 0; + while i < m.len() { + let c = if m.as_bytes()[i] == b'%' { + let c = match m + .get(i + 1..=i + 2) + .map(|s| u8::from_str_radix(s, 16)) + { + Some(Ok(c)) => c, + _ => { + warn!("Invalid percent encoding of msg argument"); + return (HttpResCode::BadRequest, "", &[]); + } + }; + i += 2; + c + } else { + m.as_bytes()[i] + }; + if let Err(_) = msg.push(c) { return (HttpResCode::BadRequest, "", &[]); } + i += 1; } - msg_content = Some(msg); + + msg_content = Some(match String::from_utf8(msg) { + Ok(msg) => msg, + Err(_) => { + warn!("Invalid utf8 msg argument"); + return (HttpResCode::BadRequest, "", &[]); + } + }); } Some(("poll", "true")) => poll = true, _ => {} diff --git a/src/socket.rs b/src/socket.rs index bf2fb80..ae15454 100644 --- a/src/socket.rs +++ b/src/socket.rs @@ -63,7 +63,6 @@ pub async fn listen_task(stack: embassy_net::Stack<'static>, mut app: impl App, break; } }; - info!("{:?}", n); let (headers, content) = match from_utf8(&buf[..n]) { Ok(b) => match b.split_once("\r\n\r\n") {