Skip to content

Commit

Permalink
fix: websocket conns do not require Connection: upgrade header (#161)
Browse files Browse the repository at this point in the history
It turns out that the WebSocket API provided by browsers [1]
does not actually send the `Connection: upgrade` header that our
websocket implementation (wrongly?) requires, so here we're dropping
that requirement.

[1]: https://developer.mozilla.org/en-US/docs/Web/API/WebSocket
  • Loading branch information
mccutchen committed Dec 13, 2023
1 parent 21c68b8 commit 1a41486
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 8 deletions.
3 changes: 0 additions & 3 deletions httpbin/websocket/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,6 @@ func (s *WebSocket) Handshake() error {
panic("websocket: handshake already completed")
}

if strings.ToLower(s.r.Header.Get("Connection")) != "upgrade" {
return fmt.Errorf("missing required `Connection: upgrade` header")
}
if strings.ToLower(s.r.Header.Get("Upgrade")) != "websocket" {
return fmt.Errorf("missing required `Upgrade: websocket` header")
}
Expand Down
10 changes: 5 additions & 5 deletions httpbin/websocket/websocket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,22 @@ func TestHandshake(t *testing.T) {
},
wantStatus: http.StatusSwitchingProtocols,
},
"missing Connection header": {
"missing Connection header is okay": {
reqHeaders: map[string]string{
"Upgrade": "websocket",
"Sec-WebSocket-Key": "dGhlIHNhbXBsZSBub25jZQ==",
"Sec-WebSocket-Version": "13",
},
wantStatus: http.StatusBadRequest,
wantStatus: http.StatusSwitchingProtocols,
},
"incorrect Connection header": {
"incorrect Connection header is also okay": {
reqHeaders: map[string]string{
"Connection": "close",
"Connection": "foo",
"Upgrade": "websocket",
"Sec-WebSocket-Key": "dGhlIHNhbXBsZSBub25jZQ==",
"Sec-WebSocket-Version": "13",
},
wantStatus: http.StatusBadRequest,
wantStatus: http.StatusSwitchingProtocols,
},
"missing Upgrade header": {
reqHeaders: map[string]string{
Expand Down

0 comments on commit 1a41486

Please sign in to comment.