Skip to content

Commit 0674c7c

Browse files
committed
Improve upgrade error messages
Upgrade typically fails because the request is not a handshake, not because the handshake is malformed. To help developers diagnose the common case, state explicitly that the request is not a handshake in error messages. To help diagnose malformed requests, capitalize and 'quote' header names in error messages.
1 parent 2257eda commit 0674c7c

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

server.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,36 +104,36 @@ func (u *Upgrader) selectSubprotocol(r *http.Request, responseHeader http.Header
104104
// response.
105105
func (u *Upgrader) Upgrade(w http.ResponseWriter, r *http.Request, responseHeader http.Header) (*Conn, error) {
106106
if r.Method != "GET" {
107-
return u.returnError(w, r, http.StatusMethodNotAllowed, "websocket: method not GET")
107+
return u.returnError(w, r, http.StatusMethodNotAllowed, "websocket: not a websocket handshake: request method is not GET")
108108
}
109109

110110
if _, ok := responseHeader["Sec-Websocket-Extensions"]; ok {
111-
return u.returnError(w, r, http.StatusInternalServerError, "websocket: application specific Sec-Websocket-Extensions headers are unsupported")
112-
}
113-
114-
if !tokenListContainsValue(r.Header, "Sec-Websocket-Version", "13") {
115-
return u.returnError(w, r, http.StatusBadRequest, "websocket: version != 13")
111+
return u.returnError(w, r, http.StatusInternalServerError, "websocket: application specific 'Sec-Websocket-Extensions' headers are unsupported")
116112
}
117113

118114
if !tokenListContainsValue(r.Header, "Connection", "upgrade") {
119-
return u.returnError(w, r, http.StatusBadRequest, "websocket: could not find connection header with token 'upgrade'")
115+
return u.returnError(w, r, http.StatusBadRequest, "websocket: not a websocket handshake: 'upgrade' token not found in 'Connection' header")
120116
}
121117

122118
if !tokenListContainsValue(r.Header, "Upgrade", "websocket") {
123-
return u.returnError(w, r, http.StatusBadRequest, "websocket: could not find upgrade header with token 'websocket'")
119+
return u.returnError(w, r, http.StatusBadRequest, "websocket: not a websocket handshake: 'websocket' token not found in 'Upgrade' header")
120+
}
121+
122+
if !tokenListContainsValue(r.Header, "Sec-Websocket-Version", "13") {
123+
return u.returnError(w, r, http.StatusBadRequest, "websocket: unsupported version: 13 not found in 'Sec-Websocket-Version' header")
124124
}
125125

126126
checkOrigin := u.CheckOrigin
127127
if checkOrigin == nil {
128128
checkOrigin = checkSameOrigin
129129
}
130130
if !checkOrigin(r) {
131-
return u.returnError(w, r, http.StatusForbidden, "websocket: origin not allowed")
131+
return u.returnError(w, r, http.StatusForbidden, "websocket: 'Origin' header value not allowed")
132132
}
133133

134134
challengeKey := r.Header.Get("Sec-Websocket-Key")
135135
if challengeKey == "" {
136-
return u.returnError(w, r, http.StatusBadRequest, "websocket: key missing or blank")
136+
return u.returnError(w, r, http.StatusBadRequest, "websocket: not a websocket handshake: `Sec-Websocket-Key' header is missing or blank")
137137
}
138138

139139
subprotocol := u.selectSubprotocol(r, responseHeader)

0 commit comments

Comments
 (0)