started changing socket.rs to remove some buffers (not finished)
This commit is contained in:
parent
47e8ba586c
commit
4cb3736e56
@ -2,10 +2,18 @@
|
||||
<html>
|
||||
<head>
|
||||
<script src="/chat.js" defer></script>
|
||||
<style>
|
||||
#users_box {
|
||||
float: right;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Chat</h1>
|
||||
<div id="users_box">
|
||||
<h3>Online Users :</h3>
|
||||
<div id="users"></div>
|
||||
</div>
|
||||
<div id="msgs"></div>
|
||||
<form id="send">
|
||||
<input
|
||||
|
@ -26,7 +26,7 @@ ws.onmessage = (event) => {
|
||||
let elem = document.createElement("p");
|
||||
elem["data-id"] = msg.id;
|
||||
elem.innerHTML =
|
||||
"<span>" + users[msg.author] + " :</span> " + msg.content;
|
||||
"<strong>" + users[msg.author] + " :</strong> " + msg.content;
|
||||
for (c of msgs.children) {
|
||||
if (c["data-id"] > msg.id) {
|
||||
msgs.insertBefore(elem, c);
|
||||
|
@ -53,22 +53,12 @@ pub async fn listen_task(stack: embassy_net::Stack<'static>, mut app: impl apps:
|
||||
socket.remote_endpoint()
|
||||
);
|
||||
|
||||
let (mut rx, mut tx) = socket.split();
|
||||
let mut ws_path: Option<String<16>> = None;
|
||||
loop {
|
||||
Timer::after_secs(0).await;
|
||||
let n = match socket.read(&mut buf).await {
|
||||
Ok(0) => {
|
||||
warn!("read EOF");
|
||||
break;
|
||||
}
|
||||
Ok(n) => n,
|
||||
Err(e) => {
|
||||
warn!("Socket {}: read error: {:?}", app.socket_name(), e);
|
||||
break;
|
||||
}
|
||||
};
|
||||
head_buf.clear();
|
||||
let msg = unwrap_opt(buf.get(..n)).await;
|
||||
let mut cont = &[];
|
||||
rx.read_with(|msg| {
|
||||
let (headers, content) = match from_utf8(msg) {
|
||||
Ok(b) => match b.split_once("\r\n\r\n") {
|
||||
Some(t) => t,
|
||||
@ -76,15 +66,14 @@ pub async fn listen_task(stack: embassy_net::Stack<'static>, mut app: impl apps:
|
||||
},
|
||||
Err(_) => {
|
||||
warn!("Non utf8 http request");
|
||||
break;
|
||||
return (0, Err(()));
|
||||
}
|
||||
};
|
||||
|
||||
let mut hl = headers.lines();
|
||||
let (request_type, path) = match hl.next() {
|
||||
None => {
|
||||
warn!("Empty request");
|
||||
break;
|
||||
return (0, Err(()));
|
||||
}
|
||||
Some(l1) => {
|
||||
let mut l1 = l1.split(' ');
|
||||
@ -94,18 +83,18 @@ pub async fn listen_task(stack: embassy_net::Stack<'static>, mut app: impl apps:
|
||||
Some("POST") => HttpRequestType::Post,
|
||||
Some(t) => {
|
||||
warn!("Unknown request type : {}", t);
|
||||
break;
|
||||
return (0, Err(()));
|
||||
}
|
||||
None => {
|
||||
warn!("No request type");
|
||||
break;
|
||||
return (0, Err(()));
|
||||
}
|
||||
},
|
||||
match l1.next() {
|
||||
Some(path) => path,
|
||||
None => {
|
||||
warn!("No path");
|
||||
break;
|
||||
return (0, Err(()));
|
||||
}
|
||||
},
|
||||
)
|
||||
@ -129,9 +118,8 @@ pub async fn listen_task(stack: embassy_net::Stack<'static>, mut app: impl apps:
|
||||
}
|
||||
let Some(host) = host else {
|
||||
warn!("No host");
|
||||
break;
|
||||
return (0, Err(()));
|
||||
};
|
||||
|
||||
info!(
|
||||
"Socket {}: {:?}{} request for {}{}",
|
||||
app.socket_name(),
|
||||
@ -140,7 +128,6 @@ pub async fn listen_task(stack: embassy_net::Stack<'static>, mut app: impl apps:
|
||||
host,
|
||||
path,
|
||||
);
|
||||
|
||||
head_buf.clear();
|
||||
let res_content: Result<Option<Content>, core::fmt::Error> = try {
|
||||
if ws_handshake {
|
||||
@ -155,18 +142,14 @@ pub async fn listen_task(stack: embassy_net::Stack<'static>, mut app: impl apps:
|
||||
} else {
|
||||
if path.len() > 16 {
|
||||
warn!("Ws socket cannot have path longer than 16 chars!");
|
||||
break;
|
||||
return (0, Err(()));
|
||||
}
|
||||
let Some(key) = ws_key else {
|
||||
warn!("No ws key!");
|
||||
break;
|
||||
return (0, Err(()));
|
||||
};
|
||||
let accept = match compute_ws_accept(key).await {
|
||||
Ok(a) => a,
|
||||
Err(e) => {
|
||||
warn!("compute ws accept error : {:?}", e);
|
||||
break;
|
||||
}
|
||||
let Ok(accept) = compute_ws_accept(key) else {
|
||||
return (0, Err(()));
|
||||
};
|
||||
write!(
|
||||
&mut head_buf,
|
||||
@ -198,6 +181,20 @@ pub async fn listen_task(stack: embassy_net::Stack<'static>, mut app: impl apps:
|
||||
res_content
|
||||
}
|
||||
};
|
||||
(msg.len(), Ok(()))
|
||||
})
|
||||
.await;
|
||||
let n = match socket.read(&mut buf).await {
|
||||
Ok(0) => {
|
||||
warn!("read EOF");
|
||||
break;
|
||||
}
|
||||
Ok(n) => n,
|
||||
Err(e) => {
|
||||
warn!("Socket {}: read error: {:?}", app.socket_name(), e);
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
let res_content = match res_content {
|
||||
Ok(rc) => rc,
|
||||
@ -269,13 +266,22 @@ impl Into<&str> for HttpResCode {
|
||||
}
|
||||
}
|
||||
|
||||
async fn compute_ws_accept(key: &str) -> Result<String<28>, EncodeSliceError> {
|
||||
fn compute_ws_accept(key: &str) -> Result<String<28>, ()> {
|
||||
let mut res = Vec::<u8, 28>::new();
|
||||
res.extend_from_slice(&[0; 28]).unwrap();
|
||||
res.fill(0);
|
||||
let mut hasher = Sha1::new();
|
||||
hasher.update(key.as_bytes());
|
||||
hasher.update(b"258EAFA5-E914-47DA-95CA-C5AB0DC85B11");
|
||||
let hash = hasher.finalize();
|
||||
BASE64_STANDARD.encode_slice(hash, &mut res)?;
|
||||
Ok(unwrap(String::from_utf8(res)).await)
|
||||
if let Err(e) = BASE64_STANDARD.encode_slice(hash, &mut res) {
|
||||
warn!("Error while base64 encoding : {}", e);
|
||||
return Err(());
|
||||
};
|
||||
match String::from_utf8(res) {
|
||||
Ok(r) => Ok(r),
|
||||
Err(e) => {
|
||||
warn!("Ws accept is not utf8! ({})", e);
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user