Skip to content

Commit 8c6cfd4

Browse files
authored
Improve bad handshake error text
Change the error text for bad handshake errors from websocket: not a websocket handshake: to: websocket: the client is not using the websocket protocol: The new text should be more helpful to developers who do not know or understand the details of the protocol. Test for bad handshake before other request errors.
1 parent 2b58522 commit 8c6cfd4

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

client_server_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,9 +398,17 @@ func TestBadMethod(t *testing.T) {
398398
}))
399399
defer s.Close()
400400

401-
resp, err := http.PostForm(s.URL, url.Values{})
401+
req, err := http.NewRequest("POST", s.URL, strings.NewReader(""))
402402
if err != nil {
403-
t.Fatalf("PostForm returned error %v", err)
403+
t.Fatalf("NewRequest returned error %v", err)
404+
}
405+
req.Header.Set("Connection", "upgrade")
406+
req.Header.Set("Upgrade", "websocket")
407+
req.Header.Set("Sec-Websocket-Version", "13")
408+
409+
resp, err := http.DefaultClient.Do(req)
410+
if err != nil {
411+
t.Fatalf("Do returned error %v", err)
404412
}
405413
resp.Body.Close()
406414
if resp.StatusCode != http.StatusMethodNotAllowed {

server.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,26 +104,28 @@ func (u *Upgrader) selectSubprotocol(r *http.Request, responseHeader http.Header
104104
// If the upgrade fails, then Upgrade replies to the client with an HTTP error
105105
// response.
106106
func (u *Upgrader) Upgrade(w http.ResponseWriter, r *http.Request, responseHeader http.Header) (*Conn, error) {
107-
if r.Method != "GET" {
108-
return u.returnError(w, r, http.StatusMethodNotAllowed, "websocket: not a websocket handshake: request method is not GET")
109-
}
110-
111-
if _, ok := responseHeader["Sec-Websocket-Extensions"]; ok {
112-
return u.returnError(w, r, http.StatusInternalServerError, "websocket: application specific 'Sec-Websocket-Extensions' headers are unsupported")
113-
}
107+
const badHandshake = "websocket: the client is not using the websocket protocol: "
114108

115109
if !tokenListContainsValue(r.Header, "Connection", "upgrade") {
116-
return u.returnError(w, r, http.StatusBadRequest, "websocket: not a websocket handshake: 'upgrade' token not found in 'Connection' header")
110+
return u.returnError(w, r, http.StatusBadRequest, badHandshake+"'upgrade' token not found in 'Connection' header")
117111
}
118112

119113
if !tokenListContainsValue(r.Header, "Upgrade", "websocket") {
120-
return u.returnError(w, r, http.StatusBadRequest, "websocket: not a websocket handshake: 'websocket' token not found in 'Upgrade' header")
114+
return u.returnError(w, r, http.StatusBadRequest, badHandshake+"'websocket' token not found in 'Upgrade' header")
115+
}
116+
117+
if r.Method != "GET" {
118+
return u.returnError(w, r, http.StatusMethodNotAllowed, badHandshake+"request method is not GET")
121119
}
122120

123121
if !tokenListContainsValue(r.Header, "Sec-Websocket-Version", "13") {
124122
return u.returnError(w, r, http.StatusBadRequest, "websocket: unsupported version: 13 not found in 'Sec-Websocket-Version' header")
125123
}
126124

125+
if _, ok := responseHeader["Sec-Websocket-Extensions"]; ok {
126+
return u.returnError(w, r, http.StatusInternalServerError, "websocket: application specific 'Sec-Websocket-Extensions' headers are unsupported")
127+
}
128+
127129
checkOrigin := u.CheckOrigin
128130
if checkOrigin == nil {
129131
checkOrigin = checkSameOrigin

0 commit comments

Comments
 (0)