]> Cypherpunks repositories - gostls13.git/commitdiff
websocket: fix bug involving spaces in header keys
authorBill Neubauer <wcn@golang.org>
Fri, 30 Jul 2010 19:27:03 +0000 (12:27 -0700)
committerRuss Cox <rsc@golang.org>
Fri, 30 Jul 2010 19:27:03 +0000 (12:27 -0700)
R=rsc, ukai
CC=golang-dev
https://golang.org/cl/1669056

src/pkg/websocket/client.go
src/pkg/websocket/websocket_test.go

index c74fe934f1dea371549a6f9140ae74dc435a065d..221b47cadf68577298a70194c0c8077073773f7b 100644 (file)
@@ -123,14 +123,7 @@ func generateKeyNumber() (key string, number uint32) {
        // to U+0039 DIGIT NINE (9).
        key = fmt.Sprintf("%d", product)
 
-       // 21. Insert /spaces_n/ U+0020 SPACE characters into /key_n/ at random
-       //     posisions.
-       for i := 0; i < spaces; i++ {
-               pos := rand.Intn(len(key)-1) + 1
-               key = key[0:pos] + " " + key[pos:]
-       }
-
-       // 22. Insert between one and twelve random characters from the ranges
+       // 21. Insert between one and twelve random characters from the ranges
        //     U+0021 to U+002F and U+003A to U+007E into /key_n/ at random
        //     positions.
        n := rand.Intn(12) + 1
@@ -139,6 +132,14 @@ func generateKeyNumber() (key string, number uint32) {
                ch := secKeyRandomChars[rand.Intn(len(secKeyRandomChars))]
                key = key[0:pos] + string(ch) + key[pos:]
        }
+
+       // 22. Insert /spaces_n/ U+0020 SPACE characters into /key_n/ at random
+       //     positions other than the start or end of the string.
+       for i := 0; i < spaces; i++ {
+               pos := rand.Intn(len(key)-1) + 1
+               key = key[0:pos] + " " + key[pos:]
+       }
+
        return
 }
 
index df7e9f4dae944f3e9cbbbd7c49bdca834234e213..5f91b73e8d5bc265150c5b6ed467eda8a6cef08d 100644 (file)
@@ -143,3 +143,18 @@ func TestHTTPDraft75(t *testing.T) {
                t.Errorf("Get: got status %d", r.StatusCode)
        }
 }
+
+func TestTrailingSpaces(t *testing.T) {
+       // http://code.google.com/p/go/issues/detail?id=955
+       // The last runs of this create keys with trailing spaces that should not be
+       // generated by the client.
+       once.Do(startServer)
+       for i := 0; i < 30; i++ {
+               // body
+               _, err := Dial(fmt.Sprintf("ws://%s/echo", serverAddr), "",
+                       "http://localhost/")
+               if err != nil {
+                       panic("Dial failed: " + err.String())
+               }
+       }
+}