Fix inconsistent naming of WebSocket rejections (#416)

This commit is contained in:
David Pedersen
2021-10-25 23:09:47 +00:00
committed by GitHub
parent 040f63b8e2
commit e949c47df6
3 changed files with 53 additions and 49 deletions
+4
View File
@@ -144,6 +144,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
// ... // ...
} }
``` ```
- Misc:
- `InvalidWebsocketVersionHeader` has been renamed to `InvalidWebSocketVersionHeader` ([#416])
- `WebsocketKeyHeaderMissing` has been renamed to `WebSocketKeyHeaderMissing` ([#416])
[#339]: https://github.com/tokio-rs/axum/pull/339 [#339]: https://github.com/tokio-rs/axum/pull/339
[#286]: https://github.com/tokio-rs/axum/pull/286 [#286]: https://github.com/tokio-rs/axum/pull/286
@@ -156,6 +159,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[#405]: https://github.com/tokio-rs/axum/pull/405 [#405]: https://github.com/tokio-rs/axum/pull/405
[#408]: https://github.com/tokio-rs/axum/pull/408 [#408]: https://github.com/tokio-rs/axum/pull/408
[#412]: https://github.com/tokio-rs/axum/pull/412 [#412]: https://github.com/tokio-rs/axum/pull/412
[#416]: https://github.com/tokio-rs/axum/pull/416
# 0.2.8 (07. October, 2021) # 0.2.8 (07. October, 2021)
+40 -40
View File
@@ -1,52 +1,52 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>Websocket Chat</title> <title>WebSocket Chat</title>
</head> </head>
<body> <body>
<h1>Websocket Chat Example</h1> <h1>WebSocket Chat Example</h1>
<input id="username" style="display:block; width:100px; box-sizing: border-box" type="text" placeholder="username"> <input id="username" style="display:block; width:100px; box-sizing: border-box" type="text" placeholder="username">
<button id="join-chat" type="button">Join Chat</button> <button id="join-chat" type="button">Join Chat</button>
<textarea id="chat" style="display:block; width:600px; height:400px; box-sizing: border-box" cols="30" rows="10"></textarea> <textarea id="chat" style="display:block; width:600px; height:400px; box-sizing: border-box" cols="30" rows="10"></textarea>
<input id="input" style="display:block; width:600px; box-sizing: border-box" type="text" placeholder="chat"> <input id="input" style="display:block; width:600px; box-sizing: border-box" type="text" placeholder="chat">
<script> <script>
const username = document.querySelector("#username"); const username = document.querySelector("#username");
const join_btn = document.querySelector("#join-chat"); const join_btn = document.querySelector("#join-chat");
const textarea = document.querySelector("#chat"); const textarea = document.querySelector("#chat");
const input = document.querySelector("#input"); const input = document.querySelector("#input");
join_btn.addEventListener("click", function(e) { join_btn.addEventListener("click", function(e) {
this.disabled = true; this.disabled = true;
const websocket = new WebSocket("ws://localhost:3000/websocket"); const websocket = new WebSocket("ws://localhost:3000/websocket");
websocket.onopen = function() { websocket.onopen = function() {
console.log("connection opened"); console.log("connection opened");
websocket.send(username.value); websocket.send(username.value);
} }
const btn = this; const btn = this;
websocket.onclose = function() { websocket.onclose = function() {
console.log("connection closed"); console.log("connection closed");
btn.disabled = false; btn.disabled = false;
} }
websocket.onmessage = function(e) { websocket.onmessage = function(e) {
console.log("received message: "+e.data); console.log("received message: "+e.data);
textarea.value += e.data+"\r\n"; textarea.value += e.data+"\r\n";
} }
input.onkeydown = function(e) { input.onkeydown = function(e) {
if (e.key == "Enter") { if (e.key == "Enter") {
websocket.send(input.value); websocket.send(input.value);
input.value = ""; input.value = "";
} }
} }
}); });
</script> </script>
</body> </body>
</html> </html>
+9 -9
View File
@@ -209,7 +209,7 @@ where
} }
if !header_eq(req, header::SEC_WEBSOCKET_VERSION, "13")? { if !header_eq(req, header::SEC_WEBSOCKET_VERSION, "13")? {
return Err(InvalidWebsocketVersionHeader.into()); return Err(InvalidWebSocketVersionHeader.into());
} }
let sec_websocket_key = if let Some(key) = req let sec_websocket_key = if let Some(key) = req
@@ -219,7 +219,7 @@ where
{ {
key key
} else { } else {
return Err(WebsocketKeyHeaderMissing.into()); return Err(WebSocketKeyHeaderMissing.into());
}; };
let on_upgrade = req let on_upgrade = req
@@ -309,7 +309,7 @@ where
} else { } else {
return ( return (
StatusCode::BAD_REQUEST, StatusCode::BAD_REQUEST,
"`Sec-Websocket-Protocol` header is invalid", "`Sec-WebSocket-Protocol` header is invalid",
) )
.into_response(); .into_response();
} }
@@ -578,16 +578,16 @@ pub mod rejection {
define_rejection! { define_rejection! {
#[status = BAD_REQUEST] #[status = BAD_REQUEST]
#[body = "`Sec-Websocket-Version` header did not include '13'"] #[body = "`Sec-WebSocket-Version` header did not include '13'"]
/// Rejection type for [`WebSocketUpgrade`](super::WebSocketUpgrade). /// Rejection type for [`WebSocketUpgrade`](super::WebSocketUpgrade).
pub struct InvalidWebsocketVersionHeader; pub struct InvalidWebSocketVersionHeader;
} }
define_rejection! { define_rejection! {
#[status = BAD_REQUEST] #[status = BAD_REQUEST]
#[body = "`Sec-Websocket-Key` header missing"] #[body = "`Sec-WebSocket-Key` header missing"]
/// Rejection type for [`WebSocketUpgrade`](super::WebSocketUpgrade). /// Rejection type for [`WebSocketUpgrade`](super::WebSocketUpgrade).
pub struct WebsocketKeyHeaderMissing; pub struct WebSocketKeyHeaderMissing;
} }
composite_rejection! { composite_rejection! {
@@ -599,8 +599,8 @@ pub mod rejection {
MethodNotGet, MethodNotGet,
InvalidConnectionHeader, InvalidConnectionHeader,
InvalidUpgradeHeader, InvalidUpgradeHeader,
InvalidWebsocketVersionHeader, InvalidWebSocketVersionHeader,
WebsocketKeyHeaderMissing, WebSocketKeyHeaderMissing,
HeadersAlreadyExtracted, HeadersAlreadyExtracted,
ExtensionsAlreadyExtracted, ExtensionsAlreadyExtracted,
} }