save
This commit is contained in:
parent
1c0108cefa
commit
c629ae5ce3
7
Cargo.lock
generated
7
Cargo.lock
generated
@ -1063,6 +1063,12 @@ version = "1.0.15"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a"
|
checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "percent-encoding"
|
||||||
|
version = "2.3.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "petgraph"
|
name = "petgraph"
|
||||||
version = "0.7.1"
|
version = "0.7.1"
|
||||||
@ -1110,6 +1116,7 @@ dependencies = [
|
|||||||
"heapless",
|
"heapless",
|
||||||
"log",
|
"log",
|
||||||
"panic-probe",
|
"panic-probe",
|
||||||
|
"percent-encoding",
|
||||||
"portable-atomic",
|
"portable-atomic",
|
||||||
"rand_core",
|
"rand_core",
|
||||||
"ringbuffer",
|
"ringbuffer",
|
||||||
|
@ -58,4 +58,5 @@ serde = { version = "*", optional = true, default-features = false, features = [
|
|||||||
] }
|
] }
|
||||||
dhcparse = { version = "*", default-features = false, optional = true }
|
dhcparse = { version = "*", default-features = false, optional = true }
|
||||||
dnsparse = { version = "*", optional = true }
|
dnsparse = { version = "*", optional = true }
|
||||||
ringbuffer = { version = "*", default-features = false }
|
ringbuffer = { version = "*", default-features = false }
|
||||||
|
percent-encoding = { version = "*", default-features = false }
|
@ -19,17 +19,6 @@
|
|||||||
<html>
|
<html>
|
||||||
<body>
|
<body>
|
||||||
<h1>Chat</h1>
|
<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">
|
<form id="login-page">
|
||||||
Enter your name :
|
Enter your name :
|
||||||
<input
|
<input
|
||||||
@ -40,7 +29,11 @@
|
|||||||
maxlength="16"
|
maxlength="16"
|
||||||
required
|
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
|
Connect
|
||||||
</button>
|
</button>
|
||||||
</form>
|
</form>
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
use core::fmt::Write;
|
use core::fmt::Write;
|
||||||
use embassy_sync::{blocking_mutex::raw::ThreadModeRawMutex, mutex::Mutex};
|
use embassy_sync::{blocking_mutex::raw::ThreadModeRawMutex, mutex::Mutex};
|
||||||
use heapless::{String, Vec};
|
use heapless::{String, Vec};
|
||||||
|
use percent_encoding::percent_decode_str;
|
||||||
use pico_website::unwrap;
|
use pico_website::unwrap;
|
||||||
use ringbuffer::{ConstGenericRingBuffer, RingBuffer};
|
use ringbuffer::{ConstGenericRingBuffer, RingBuffer};
|
||||||
|
|
||||||
@ -9,6 +10,10 @@ use crate::socket::{HttpRequestType, HttpResCode};
|
|||||||
use super::App;
|
use super::App;
|
||||||
|
|
||||||
const MEMORY_SIZE: usize = 16;
|
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());
|
static MESSAGES: Mutex<ThreadModeRawMutex, Messages> = Mutex::new(Messages::new());
|
||||||
|
|
||||||
pub struct ChatApp {
|
pub struct ChatApp {
|
||||||
@ -39,6 +44,7 @@ impl App for ChatApp {
|
|||||||
let (path, args) = path.split_once('?').unwrap_or((path, ""));
|
let (path, args) = path.split_once('?').unwrap_or((path, ""));
|
||||||
let mut load = None;
|
let mut load = None;
|
||||||
let mut username = None;
|
let mut username = None;
|
||||||
|
// let mut msg_content = None;
|
||||||
for arg in args.split('&').chain(content.split('&')) {
|
for arg in args.split('&').chain(content.split('&')) {
|
||||||
match arg.split_once('=') {
|
match arg.split_once('=') {
|
||||||
Some(("load", n)) => {
|
Some(("load", n)) => {
|
||||||
@ -51,30 +57,42 @@ impl App for ChatApp {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Some(("username", u)) => {
|
Some(("username", u)) => {
|
||||||
if u.len() < 3 || u.len() > 16 {
|
if u.len() < USERNAME_MIN_SIZE || u.len() > USERNAME_SIZE {
|
||||||
return (HttpResCode::BadRequest, "", &[]);
|
return (HttpResCode::BadRequest, "", &[]);
|
||||||
}
|
}
|
||||||
username = Some(u);
|
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/connect" && username.is_some() {
|
||||||
if path == "/chat" && username.is_some() {
|
|
||||||
self.res_buf.clear();
|
self.res_buf.clear();
|
||||||
unwrap(write!(
|
unwrap(write!(
|
||||||
&mut self.res_buf,
|
&mut self.res_buf,
|
||||||
"<div \
|
"<div \
|
||||||
class=\"message\" \
|
class=\"message\" \
|
||||||
hx-get=\"/chat/message/abs/0?load={}\" \
|
hx-get=\"/chat/message/abs/0?load={}&username={}\" \
|
||||||
hx-target=\"this\" \
|
hx-target=\"this\" \
|
||||||
hx-swap=\"outerHTML\" \
|
hx-swap=\"outerHTML\" \
|
||||||
hx-trigger=\"load\" \
|
hx-trigger=\"load\" \
|
||||||
></div>",
|
></div>",
|
||||||
MEMORY_SIZE
|
MEMORY_SIZE,
|
||||||
|
username.unwrap()
|
||||||
))
|
))
|
||||||
.await;
|
.await;
|
||||||
return (HttpResCode::Ok, "html", &self.res_buf);
|
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 {
|
} else if path.starts_with("/chat/message/abs/") && path.len() > 18 {
|
||||||
let msg_id: u16 = match path[18..].parse() {
|
let msg_id: u16 = match path[18..].parse() {
|
||||||
Ok(n) => n,
|
Ok(n) => n,
|
||||||
@ -121,8 +139,8 @@ impl App for ChatApp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct Message {
|
struct Message {
|
||||||
author: String<16>,
|
author: String<USERNAME_SIZE>,
|
||||||
content: String<256>,
|
content: String<MSG_SIZE>,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Messages {
|
struct Messages {
|
||||||
|
Loading…
Reference in New Issue
Block a user