Skip to content

Commit 599534b

Browse files
committed
Implement core API for WASM
Closes #121
1 parent e09e295 commit 599534b

33 files changed

+523
-65
lines changed

accept.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build !js
2+
13
package websocket
24

35
import (
@@ -41,6 +43,12 @@ type AcceptOptions struct {
4143
}
4244

4345
func verifyClientRequest(w http.ResponseWriter, r *http.Request) error {
46+
if !r.ProtoAtLeast(1, 1) {
47+
err := fmt.Errorf("websocket protocol violation: handshake request must be at least HTTP/1.1: %q", r.Proto)
48+
http.Error(w, err.Error(), http.StatusBadRequest)
49+
return err
50+
}
51+
4452
if !headerValuesContainsToken(r.Header, "Connection", "Upgrade") {
4553
err := fmt.Errorf("websocket protocol violation: Connection header %q does not contain Upgrade", r.Header.Get("Connection"))
4654
http.Error(w, err.Error(), http.StatusBadRequest)

accept_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build !js
2+
13
package websocket
24

35
import (
@@ -45,6 +47,7 @@ func Test_verifyClientHandshake(t *testing.T) {
4547
testCases := []struct {
4648
name string
4749
method string
50+
http1 bool
4851
h map[string]string
4952
success bool
5053
}{
@@ -86,6 +89,16 @@ func Test_verifyClientHandshake(t *testing.T) {
8689
"Sec-WebSocket-Key": "",
8790
},
8891
},
92+
{
93+
name: "badHTTPVersion",
94+
h: map[string]string{
95+
"Connection": "Upgrade",
96+
"Upgrade": "websocket",
97+
"Sec-WebSocket-Version": "13",
98+
"Sec-WebSocket-Key": "meow123",
99+
},
100+
http1: true,
101+
},
89102
{
90103
name: "success",
91104
h: map[string]string{
@@ -106,6 +119,12 @@ func Test_verifyClientHandshake(t *testing.T) {
106119
w := httptest.NewRecorder()
107120
r := httptest.NewRequest(tc.method, "/", nil)
108121

122+
r.ProtoMajor = 1
123+
r.ProtoMinor = 1
124+
if tc.http1 {
125+
r.ProtoMinor = 0
126+
}
127+
109128
for k, v := range tc.h {
110129
r.Header.Set(k, v)
111130
}

ci/wasm.sh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,5 @@ cd "$(git rev-parse --show-toplevel)"
66

77
GOOS=js GOARCH=wasm go vet ./...
88
go install golang.org/x/lint/golint
9-
# Get passing later.
10-
#GOOS=js GOARCH=wasm golint -set_exit_status ./...
11-
GOOS=js GOARCH=wasm go test ./internal/wsjs
9+
GOOS=js GOARCH=wasm golint -set_exit_status ./...
10+
GOOS=js GOARCH=wasm go test ./...

dial.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build !js
2+
13
package websocket
24

35
import (
@@ -149,6 +151,10 @@ func verifyServerResponse(r *http.Request, resp *http.Response) error {
149151
)
150152
}
151153

154+
if proto := resp.Header.Get("Sec-WebSocket-Protocol"); proto != "" && !headerValuesContainsToken(r.Header, "Sec-WebSocket-Protocol", proto) {
155+
return fmt.Errorf("websocket protocol violation: unexpected Sec-WebSocket-Protocol from server: %q", proto)
156+
}
157+
152158
return nil
153159
}
154160

dial_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build !js
2+
13
package websocket
24

35
import (
@@ -97,6 +99,16 @@ func Test_verifyServerHandshake(t *testing.T) {
9799
},
98100
success: false,
99101
},
102+
{
103+
name: "badSecWebSocketProtocol",
104+
response: func(w http.ResponseWriter) {
105+
w.Header().Set("Connection", "Upgrade")
106+
w.Header().Set("Upgrade", "websocket")
107+
w.Header().Set("Sec-WebSocket-Protocol", "xd")
108+
w.WriteHeader(http.StatusSwitchingProtocols)
109+
},
110+
success: false,
111+
},
100112
{
101113
name: "success",
102114
response: func(w http.ResponseWriter) {

doc.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build !js
2+
13
// Package websocket is a minimal and idiomatic implementation of the WebSocket protocol.
24
//
35
// https://tools.ietf.org/html/rfc6455

example_echo_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build !js
2+
13
package websocket_test
24

35
import (

example_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build !js
2+
13
package websocket_test
24

35
import (

export_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build !js
2+
13
package websocket
24

35
import (

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ require (
2222
golang.org/x/sys v0.0.0-20190919044723-0c1ff786ef13 // indirect
2323
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4
2424
golang.org/x/tools v0.0.0-20190920225731-5eefd052ad72
25+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7
2526
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
2627
gotest.tools/gotestsum v0.3.5
2728
mvdan.cc/sh v2.6.4+incompatible

0 commit comments

Comments
 (0)