]> Cypherpunks repositories - gostls13.git/commitdiff
websocket: return an error HTTP response for bad websocket request.
authorFumitoshi Ukai <ukai@google.com>
Thu, 3 Nov 2011 03:13:39 +0000 (14:13 +1100)
committerAndrew Gerrand <adg@golang.org>
Thu, 3 Nov 2011 03:13:39 +0000 (14:13 +1100)
websocket spec had changed server-side requiements to return
an HTTP response with an appropriate error code (such as 400 Bad
Request) when it finds client did not send a handshake that matches
websocket protocol, rather than just closing connection.
It needs to flush out response before closing connection.
Fixes issues 2396.

R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/5318072

src/pkg/websocket/server.go
src/pkg/websocket/websocket_test.go

index 9420c47191d1964ffff2a34e3cff799a1ecef5b9..8f16517c03e7a30f4c0b93858dfe82ba74e896db 100644 (file)
@@ -20,6 +20,7 @@ func newServerConn(rwc io.ReadWriteCloser, buf *bufio.ReadWriter, req *http.Requ
                fmt.Fprintf(buf, "Sec-WebSocket-Version: %s\r\n", SupportedProtocolVersion)
                buf.WriteString("\r\n")
                buf.WriteString(err.Error())
+               buf.Flush()
                return
        }
        if err != nil {
@@ -34,12 +35,17 @@ func newServerConn(rwc io.ReadWriteCloser, buf *bufio.ReadWriter, req *http.Requ
                fmt.Fprintf(buf, "HTTP/1.1 %03d %s\r\n", code, http.StatusText(code))
                buf.WriteString("\r\n")
                buf.WriteString(err.Error())
+               buf.Flush()
                return
        }
        config.Protocol = nil
 
        err = hs.AcceptHandshake(buf.Writer)
        if err != nil {
+               code = http.StatusBadRequest
+               fmt.Fprintf(buf, "HTTP/1.1 %03d %s\r\n", code, http.StatusText(code))
+               buf.WriteString("\r\n")
+               buf.Flush()
                return
        }
        conn = hs.NewServerConn(buf, rwc, req)
index 69b5335cfa9aa32be395c2c71074edb43b67ac07..25fe2646730bc8c78c33e445c2e030940ffad795 100644 (file)
@@ -200,20 +200,19 @@ func TestHTTP(t *testing.T) {
        once.Do(startServer)
 
        // If the client did not send a handshake that matches the protocol
-       // specification, the server should abort the WebSocket connection.
-       _, err := http.Get(fmt.Sprintf("http://%s/echo", serverAddr))
-       if err == nil {
-               t.Error("Get: unexpected success")
+       // specification, the server MUST return an HTTP respose with an
+       // appropriate error code (such as 400 Bad Request)
+       resp, err := http.Get(fmt.Sprintf("http://%s/echo", serverAddr))
+       if err != nil {
+               t.Errorf("Get: error %#v", err)
                return
        }
-       urlerr, ok := err.(*url.Error)
-       if !ok {
-               t.Errorf("Get: not url.Error %#v", err)
+       if resp == nil {
+               t.Error("Get: resp is null")
                return
        }
-       if urlerr.Err != io.ErrUnexpectedEOF {
-               t.Errorf("Get: error %#v", err)
-               return
+       if resp.StatusCode != http.StatusBadRequest {
+               t.Errorf("Get: expected %q got %q", http.StatusBadRequest, resp.StatusCode)
        }
 }