Skip to content

Commit 0c05d25

Browse files
committed
More tests and client side autobahn tests
Closes #46
1 parent 6442a5a commit 0c05d25

File tree

4 files changed

+416
-121
lines changed

4 files changed

+416
-121
lines changed

accept.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ func (o acceptSubprotocols) acceptOption() {}
2323

2424
// AcceptSubprotocols list the subprotocols that Accept will negotiate with a client.
2525
// The first protocol that a client supports will be negotiated.
26-
// Pass "" as a subprotocol if you would like to allow the default protocol along with
27-
// specific subprotocols.
26+
// The empty protocol will always be negotiated as per RFC 6455. If you would like to
27+
// reject it, close the connection is c.Subprotocol() == "".
2828
func AcceptSubprotocols(subprotocols ...string) AcceptOption {
2929
return acceptSubprotocols(subprotocols)
3030
}
@@ -42,7 +42,7 @@ func (o acceptOrigins) acceptOption() {}
4242
// See https://stackoverflow.com/a/37837709/4283659
4343
// You can use a * for wildcards.
4444
func AcceptOrigins(origins ...string) AcceptOption {
45-
return AcceptOrigins(origins...)
45+
return acceptOrigins(origins)
4646
}
4747

4848
// Accept accepts a WebSocket handshake from a client and upgrades the
@@ -135,7 +135,7 @@ func Accept(w http.ResponseWriter, r *http.Request, opts ...AcceptOption) (*Conn
135135
}
136136

137137
func selectSubprotocol(w http.ResponseWriter, r *http.Request, subprotocols []string) {
138-
clientSubprotocols := strings.Split(r.Header.Get("Sec-WebSocket-Protocol"), "\n")
138+
clientSubprotocols := strings.Split(r.Header.Get("Sec-WebSocket-Protocol"), ",")
139139
for _, sp := range subprotocols {
140140
for _, cp := range clientSubprotocols {
141141
if sp == strings.TrimSpace(cp) {
@@ -168,9 +168,9 @@ func authenticateOrigin(r *http.Request, origins []string) error {
168168
return xerrors.Errorf("failed to parse Origin header %q: %w", origin, err)
169169
}
170170
for _, o := range origins {
171-
if u.Host == o {
171+
if strings.EqualFold(u.Host, o) {
172172
return nil
173173
}
174174
}
175-
return xerrors.New("request origin is not authorized")
175+
return xerrors.Errorf("request origin %q is not authorized", r.Header.Get("Origin"))
176176
}

dial.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,15 @@ func Dial(ctx context.Context, u string, opts ...DialOption) (_ *Conn, _ *http.R
7878
}
7979

8080
switch parsedURL.Scheme {
81-
case "ws", "http":
81+
case "ws":
8282
parsedURL.Scheme = "http"
83-
case "wss", "https":
83+
case "wss":
8484
parsedURL.Scheme = "https"
8585
default:
8686
return nil, nil, xerrors.Errorf("unknown scheme in url: %q", parsedURL.Scheme)
8787
}
8888

89-
req, _ := http.NewRequest("GET", u, nil)
89+
req, _ := http.NewRequest("GET", parsedURL.String(), nil)
9090
req = req.WithContext(ctx)
9191
req.Header = header
9292
req.Header.Set("Connection", "Upgrade")

websocket.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,11 @@ messageLoop:
139139
masked: c.client,
140140
}
141141
c.writeFrame(h, control.payload)
142-
c.writeDone <- struct{}{}
142+
select {
143+
case <-c.closed:
144+
return
145+
case c.writeDone <- struct{}{}:
146+
}
143147
continue
144148
}
145149

@@ -280,6 +284,7 @@ func (c *Conn) readLoop() {
280284
return
281285
}
282286
default:
287+
// TODO send back protocol violation message or figure out what RFC wants.
283288
c.close(xerrors.Errorf("unexpected opcode in header: %#v", h))
284289
return
285290
}
@@ -481,8 +486,14 @@ func (w *MessageWriter) Close() error {
481486
}
482487
}
483488
close(w.c.writeBytes)
484-
<-w.c.writeDone
485-
return nil
489+
select {
490+
case <-w.c.closed:
491+
return w.c.getCloseErr()
492+
case <-w.ctx.Done():
493+
return w.ctx.Err()
494+
case <-w.c.writeDone:
495+
return nil
496+
}
486497
}
487498

488499
// MessageReader enables reading a data frame from the WebSocket connection.

0 commit comments

Comments
 (0)