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