This commit is contained in:
Arkitu 2025-05-02 16:18:52 +02:00
parent 1c0108cefa
commit c629ae5ce3
4 changed files with 39 additions and 20 deletions

7
Cargo.lock generated
View File

@ -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",

View File

@ -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 }
ringbuffer = { version = "*", default-features = false }
percent-encoding = { version = "*", default-features = false }

View File

@ -19,17 +19,6 @@
<html>
<body>
<h1>Chat</h1>
<!-- <div
class="message"
hx-get="/chat/message/abs/0?load=3"
hx-target="this"
hx-swap="outerHTML"
hx-trigger="load"
></div>
<div class="message"></div>
<div class="message"></div>
<div class="message"></div> -->
<form id="login-page">
Enter your name :
<input
@ -40,7 +29,11 @@
maxlength="16"
required
/>
<button hx-post="/chat" hx-target="#login-page" hx-swap="outerHTML">
<button
hx-post="/chat/connect"
hx-target="#login-page"
hx-swap="outerHTML"
>
Connect
</button>
</form>

View File

@ -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<ThreadModeRawMutex, Messages> = 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::<MSG_SIZE>::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,
"<div \
class=\"message\" \
hx-get=\"/chat/message/abs/0?load={}\" \
hx-get=\"/chat/message/abs/0?load={}&username={}\" \
hx-target=\"this\" \
hx-swap=\"outerHTML\" \
hx-trigger=\"load\" \
></div>",
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<USERNAME_SIZE>,
content: String<MSG_SIZE>,
}
struct Messages {