Skip to content

Commit

Permalink
Fix inconsistent naming of WebSocket rejections
Browse files Browse the repository at this point in the history
  • Loading branch information
davidpdrsn committed Oct 25, 2021
1 parent 68e44fa commit a1b28fe
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 58 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
- `WebsocketKeyHeaderMissing` has been renamed to `WebSocketKeyHeaderMissing`

[#339]: https://github.com/tokio-rs/axum/pull/339
[#286]: https://github.com/tokio-rs/axum/pull/286
Expand Down
98 changes: 49 additions & 49 deletions examples/chat/chat.html
Original file line number Diff line number Diff line change
@@ -1,52 +1,52 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Websocket Chat</title>
</head>
<body>
<h1>Websocket Chat Example</h1>

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

<script>
const username = document.querySelector("#username");
const join_btn = document.querySelector("#join-chat");
const textarea = document.querySelector("#chat");
const input = document.querySelector("#input");

join_btn.addEventListener("click", function(e) {
this.disabled = true;

const websocket = new WebSocket("ws://localhost:3000/websocket");

websocket.onopen = function() {
console.log("connection opened");
websocket.send(username.value);
}

const btn = this;

websocket.onclose = function() {
console.log("connection closed");
btn.disabled = false;
}

websocket.onmessage = function(e) {
console.log("received message: "+e.data);
textarea.value += e.data+"\r\n";
}

input.onkeydown = function(e) {
if (e.key == "Enter") {
websocket.send(input.value);
input.value = "";
}
}
});
</script>
</body>
<head>
<meta charset="UTF-8">
<title>WebSocket Chat</title>
</head>
<body>
<h1>WebSocket Chat Example</h1>

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

<script>
const username = document.querySelector("#username");
const join_btn = document.querySelector("#join-chat");
const textarea = document.querySelector("#chat");
const input = document.querySelector("#input");

join_btn.addEventListener("click", function(e) {
this.disabled = true;

const websocket = new WebSocket("ws://localhost:3000/websocket");

websocket.onopen = function() {
console.log("connection opened");
websocket.send(username.value);
}

const btn = this;

websocket.onclose = function() {
console.log("connection closed");
btn.disabled = false;
}

websocket.onmessage = function(e) {
console.log("received message: "+e.data);
textarea.value += e.data+"\r\n";
}

input.onkeydown = function(e) {
if (e.key == "Enter") {
websocket.send(input.value);
input.value = "";
}
}
});
</script>
</body>
</html>
18 changes: 9 additions & 9 deletions src/extract/ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ where
}

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
Expand All @@ -219,7 +219,7 @@ where
{
key
} else {
return Err(WebsocketKeyHeaderMissing.into());
return Err(WebSocketKeyHeaderMissing.into());
};

let on_upgrade = req
Expand Down Expand Up @@ -309,7 +309,7 @@ where
} else {
return (
StatusCode::BAD_REQUEST,
"`Sec-Websocket-Protocol` header is invalid",
"`Sec-WebSocket-Protocol` header is invalid",
)
.into_response();
}
Expand Down Expand Up @@ -578,16 +578,16 @@ pub mod rejection {

define_rejection! {
#[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).
pub struct InvalidWebsocketVersionHeader;
pub struct InvalidWebSocketVersionHeader;
}

define_rejection! {
#[status = BAD_REQUEST]
#[body = "`Sec-Websocket-Key` header missing"]
#[body = "`Sec-WebSocket-Key` header missing"]
/// Rejection type for [`WebSocketUpgrade`](super::WebSocketUpgrade).
pub struct WebsocketKeyHeaderMissing;
pub struct WebSocketKeyHeaderMissing;
}

composite_rejection! {
Expand All @@ -599,8 +599,8 @@ pub mod rejection {
MethodNotGet,
InvalidConnectionHeader,
InvalidUpgradeHeader,
InvalidWebsocketVersionHeader,
WebsocketKeyHeaderMissing,
InvalidWebSocketVersionHeader,
WebSocketKeyHeaderMissing,
HeadersAlreadyExtracted,
ExtensionsAlreadyExtracted,
}
Expand Down

0 comments on commit a1b28fe

Please sign in to comment.