working utf8 messages

This commit is contained in:
Arkitu 2025-05-06 22:22:00 +02:00
parent 21e6a50e36
commit bfdf18c1da
2 changed files with 30 additions and 8 deletions

View File

@ -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::<MSG_SIZE>::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::<u8, MSG_SIZE>::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,
_ => {}

View File

@ -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") {