Skip to content

Commit 567453a

Browse files
committed
Require GET in Upgrader.Upgrade.
Return error if the request method is not GET. Remove all request method tests from the examples.
1 parent a4e0143 commit 567453a

File tree

5 files changed

+23
-18
lines changed

5 files changed

+23
-18
lines changed

client_server_test.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,6 @@ func newTLSServer(t *testing.T) *cstServer {
5656
}
5757

5858
func (t cstHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
59-
if r.Method != "GET" {
60-
t.Logf("method %s not allowed", r.Method)
61-
http.Error(w, "method not allowed", 405)
62-
return
63-
}
6459
subprotos := Subprotocols(r)
6560
if !reflect.DeepEqual(subprotos, cstDialer.Subprotocols) {
6661
t.Logf("subprotols=%v, want %v", subprotos, cstDialer.Subprotocols)
@@ -287,6 +282,26 @@ func TestDialBadHeader(t *testing.T) {
287282
}
288283
}
289284

285+
func TestBadMethod(t *testing.T) {
286+
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
287+
ws, err := cstUpgrader.Upgrade(w, r, nil)
288+
if err == nil {
289+
t.Errorf("handshake succeeded, expect fail")
290+
ws.Close()
291+
}
292+
}))
293+
defer s.Close()
294+
295+
resp, err := http.PostForm(s.URL, url.Values{})
296+
if err != nil {
297+
t.Fatalf("PostForm returned error %v", err)
298+
}
299+
resp.Body.Close()
300+
if resp.StatusCode != http.StatusMethodNotAllowed {
301+
t.Errorf("Status = %d, want %d", resp.StatusCode, http.StatusMethodNotAllowed)
302+
}
303+
}
304+
290305
func TestHandshake(t *testing.T) {
291306
s := newServer(t)
292307
defer s.Close()

examples/chat/conn.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,6 @@ func (c *connection) writePump() {
9090

9191
// serveWs handles websocket requests from the peer.
9292
func serveWs(w http.ResponseWriter, r *http.Request) {
93-
if r.Method != "GET" {
94-
http.Error(w, "Method not allowed", 405)
95-
return
96-
}
9793
ws, err := upgrader.Upgrade(w, r, nil)
9894
if err != nil {
9995
log.Println(err)

examples/command/main.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,6 @@ func internalError(ws *websocket.Conn, msg string, err error) {
9595
var upgrader = websocket.Upgrader{}
9696

9797
func serveWs(w http.ResponseWriter, r *http.Request) {
98-
if r.Method != "GET" {
99-
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
100-
return
101-
}
102-
10398
ws, err := upgrader.Upgrade(w, r, nil)
10499
if err != nil {
105100
log.Println("upgrade:", err)

examples/echo/server.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ func echo(w http.ResponseWriter, r *http.Request) {
2323
http.Error(w, "Not found", 404)
2424
return
2525
}
26-
if r.Method != "GET" {
27-
http.Error(w, "Method not allowed", 405)
28-
return
29-
}
3026
c, err := upgrader.Upgrade(w, r, nil)
3127
if err != nil {
3228
log.Print("upgrade:", err)

server.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ func (u *Upgrader) selectSubprotocol(r *http.Request, responseHeader http.Header
9393
// request. Use the responseHeader to specify cookies (Set-Cookie) and the
9494
// application negotiated subprotocol (Sec-Websocket-Protocol).
9595
func (u *Upgrader) Upgrade(w http.ResponseWriter, r *http.Request, responseHeader http.Header) (*Conn, error) {
96+
if r.Method != "GET" {
97+
return u.returnError(w, r, http.StatusMethodNotAllowed, "websocket: method not GET")
98+
}
9699
if values := r.Header["Sec-Websocket-Version"]; len(values) == 0 || values[0] != "13" {
97100
return u.returnError(w, r, http.StatusBadRequest, "websocket: version != 13")
98101
}

0 commit comments

Comments
 (0)