Skip to content

Commit 7a45d7e

Browse files
committed
Improve docs and CONTRIBUTING.md
1 parent f0ac704 commit 7a45d7e

File tree

10 files changed

+42
-25
lines changed

10 files changed

+42
-25
lines changed

accept.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,31 +41,31 @@ type AcceptOptions struct {
4141

4242
func verifyClientRequest(w http.ResponseWriter, r *http.Request) error {
4343
if !headerValuesContainsToken(r.Header, "Connection", "Upgrade") {
44-
err := xerrors.Errorf("websocket: protocol violation: Connection header %q does not contain Upgrade", r.Header.Get("Connection"))
44+
err := xerrors.Errorf("websocket protocol violation: Connection header %q does not contain Upgrade", r.Header.Get("Connection"))
4545
http.Error(w, err.Error(), http.StatusBadRequest)
4646
return err
4747
}
4848

4949
if !headerValuesContainsToken(r.Header, "Upgrade", "WebSocket") {
50-
err := xerrors.Errorf("websocket: protocol violation: Upgrade header %q does not contain websocket", r.Header.Get("Upgrade"))
50+
err := xerrors.Errorf("websocket protocol violation: Upgrade header %q does not contain websocket", r.Header.Get("Upgrade"))
5151
http.Error(w, err.Error(), http.StatusBadRequest)
5252
return err
5353
}
5454

5555
if r.Method != "GET" {
56-
err := xerrors.Errorf("websocket: protocol violation: handshake request method %q is not GET", r.Method)
56+
err := xerrors.Errorf("websocket protocol violation: handshake request method %q is not GET", r.Method)
5757
http.Error(w, err.Error(), http.StatusBadRequest)
5858
return err
5959
}
6060

6161
if r.Header.Get("Sec-WebSocket-Version") != "13" {
62-
err := xerrors.Errorf("websocket: unsupported protocol version: %q", r.Header.Get("Sec-WebSocket-Version"))
62+
err := xerrors.Errorf("unsupported websocket protocol version: %q", r.Header.Get("Sec-WebSocket-Version"))
6363
http.Error(w, err.Error(), http.StatusBadRequest)
6464
return err
6565
}
6666

6767
if r.Header.Get("Sec-WebSocket-Key") == "" {
68-
err := xerrors.New("websocket: protocol violation: missing Sec-WebSocket-Key")
68+
err := xerrors.New("websocket protocol violation: missing Sec-WebSocket-Key")
6969
http.Error(w, err.Error(), http.StatusBadRequest)
7070
return err
7171
}
@@ -78,6 +78,14 @@ func verifyClientRequest(w http.ResponseWriter, r *http.Request) error {
7878
// Accept will reject the handshake if the Origin is not the same as the Host unless
7979
// the InsecureSkipVerify option is set.
8080
func Accept(w http.ResponseWriter, r *http.Request, opts AcceptOptions) (*Conn, error) {
81+
c, err := accept(w, r, opts)
82+
if err != nil {
83+
return nil, xerrors.Errorf("failed to accept websocket connection: %w", err)
84+
}
85+
return c, nil
86+
}
87+
88+
func accept(w http.ResponseWriter, r *http.Request, opts AcceptOptions) (*Conn, error) {
8189
err := verifyClientRequest(w, r)
8290
if err != nil {
8391
return nil, err
@@ -93,7 +101,7 @@ func Accept(w http.ResponseWriter, r *http.Request, opts AcceptOptions) (*Conn,
93101

94102
hj, ok := w.(http.Hijacker)
95103
if !ok {
96-
err = xerrors.New("websocket: response writer does not implement http.Hijacker")
104+
err = xerrors.New("response writer must implement http.Hijacker")
97105
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
98106
return nil, err
99107
}
@@ -112,7 +120,7 @@ func Accept(w http.ResponseWriter, r *http.Request, opts AcceptOptions) (*Conn,
112120

113121
netConn, brw, err := hj.Hijack()
114122
if err != nil {
115-
err = xerrors.Errorf("websocket: failed to hijack connection: %w", err)
123+
err = xerrors.Errorf("failed to hijack connection: %w", err)
116124
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
117125
return nil, err
118126
}
@@ -166,5 +174,5 @@ func authenticateOrigin(r *http.Request) error {
166174
if strings.EqualFold(u.Host, r.Host) {
167175
return nil
168176
}
169-
return xerrors.Errorf("request origin %q is not authorized for host %v", origin, r.Host)
177+
return xerrors.Errorf("request origin %q is not authorized for host %q", origin, r.Host)
170178
}

bench_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func benchConn(b *testing.B, stream bool) {
2828
if stream {
2929
streamEchoLoop(r.Context(), c)
3030
} else {
31-
bufferedEchoLoop(r.Context(), c)
31+
streamEchoLoop(r.Context(), c)
3232
}
3333

3434
}))

ci/bench/entrypoint.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ go test --vet=off --run=^$ -bench=. \
1212
./...
1313

1414
set +x
15+
echo
1516
echo "profiles are in ./profs
1617
keep in mind that every profiler Go provides is enabled so that may skew the benchmarks"

dial.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,15 @@ type DialOptions struct {
3535
var secWebSocketKey = base64.StdEncoding.EncodeToString(make([]byte, 16))
3636

3737
// Dial performs a WebSocket handshake on the given url with the given options.
38-
func Dial(ctx context.Context, u string, opts DialOptions) (_ *Conn, _ *http.Response, err error) {
38+
func Dial(ctx context.Context, u string, opts DialOptions) (*Conn, *http.Response, error) {
39+
c, r, err := dial(ctx, u, opts)
40+
if err != nil {
41+
return nil, r, xerrors.Errorf("failed to websocket dial: %w", err)
42+
}
43+
return c, r, nil
44+
}
45+
46+
func dial(ctx context.Context, u string, opts DialOptions) (_ *Conn, _ *http.Response, err error) {
3947
if opts.HTTPClient == nil {
4048
opts.HTTPClient = http.DefaultClient
4149
}
@@ -45,7 +53,7 @@ func Dial(ctx context.Context, u string, opts DialOptions) (_ *Conn, _ *http.Res
4553

4654
parsedURL, err := url.Parse(u)
4755
if err != nil {
48-
return nil, nil, xerrors.Errorf("failed to parse websocket url: %w", err)
56+
return nil, nil, xerrors.Errorf("failed to parse url: %w", err)
4957
}
5058

5159
switch parsedURL.Scheme {
@@ -54,7 +62,7 @@ func Dial(ctx context.Context, u string, opts DialOptions) (_ *Conn, _ *http.Res
5462
case "wss":
5563
parsedURL.Scheme = "https"
5664
default:
57-
return nil, nil, xerrors.Errorf("unknown scheme in url: %q", parsedURL.Scheme)
65+
return nil, nil, xerrors.Errorf("unexpected url scheme scheme: %q", parsedURL.Scheme)
5866
}
5967

6068
req, _ := http.NewRequest("GET", parsedURL.String(), nil)
@@ -70,7 +78,7 @@ func Dial(ctx context.Context, u string, opts DialOptions) (_ *Conn, _ *http.Res
7078

7179
resp, err := opts.HTTPClient.Do(req)
7280
if err != nil {
73-
return nil, nil, err
81+
return nil, nil, xerrors.Errorf("failed to send handshake request: %w", err)
7482
}
7583
defer func() {
7684
respBody := resp.Body
@@ -90,7 +98,7 @@ func Dial(ctx context.Context, u string, opts DialOptions) (_ *Conn, _ *http.Res
9098

9199
rwc, ok := resp.Body.(io.ReadWriteCloser)
92100
if !ok {
93-
return nil, resp, xerrors.Errorf("websocket: body is not a read write closer but should be: %T", rwc)
101+
return nil, resp, xerrors.Errorf("response body is not a read write closer: %T", rwc)
94102
}
95103

96104
// TODO pool bufio
@@ -108,15 +116,15 @@ func Dial(ctx context.Context, u string, opts DialOptions) (_ *Conn, _ *http.Res
108116

109117
func verifyServerResponse(resp *http.Response) error {
110118
if resp.StatusCode != http.StatusSwitchingProtocols {
111-
return xerrors.Errorf("websocket: expected status code %v but got %v", http.StatusSwitchingProtocols, resp.StatusCode)
119+
return xerrors.Errorf("expected handshake response status code %v but got %v", http.StatusSwitchingProtocols, resp.StatusCode)
112120
}
113121

114122
if !headerValuesContainsToken(resp.Header, "Connection", "Upgrade") {
115-
return xerrors.Errorf("websocket: protocol violation: Connection header does not contain Upgrade: %q", resp.Header.Get("Connection"))
123+
return xerrors.Errorf("websocket protocol violation: Connection header does not contain Upgrade: %q", resp.Header.Get("Connection"))
116124
}
117125

118126
if !headerValuesContainsToken(resp.Header, "Upgrade", "WebSocket") {
119-
return xerrors.Errorf("websocket: protocol violation: Upgrade header does not contain websocket: %q", resp.Header.Get("Upgrade"))
127+
return xerrors.Errorf("websocket protocol violation: Upgrade header does not contain websocket: %q", resp.Header.Get("Upgrade"))
120128
}
121129

122130
// We do not care about Sec-WebSocket-Accept because it does not matter.

docs/CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ Be sure to link to an existing issue if one exists. In general, try creating an
1313
before making a PR to get some discussion going and to make sure you do not spend time
1414
on a PR that may be rejected.
1515

16-
Run `test/run.sh` to test your changes. You only need docker and bash to run the tests.
16+
Run `ci/run.sh` to test your changes. You only need docker and bash to run the tests.

export_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
// to write an additional fin frame when Close is called on the writer which
1313
// can result in worse performance if the full message exceeds the buffer size
1414
// which is 4096 right now as then two syscalls will be necessary to complete the message.
15-
// TODO this is no good as we cannot write daata frame msg in between other ones
15+
// TODO this is no good as we cannot write data frame msg in between other ones
1616
func (c *Conn) Write(ctx context.Context, typ MessageType, p []byte) error {
1717
return c.writeControl(ctx, opcode(typ), p)
1818
}

header.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func readHeader(r io.Reader) (header, error) {
128128
case payloadLength == 127:
129129
h.payloadLength = int64(binary.BigEndian.Uint64(b))
130130
if h.payloadLength < 0 {
131-
return header{}, xerrors.Errorf("websocket: header has negative payload length: %v", h.payloadLength)
131+
return header{}, xerrors.Errorf("header with negative payload length: %v", h.payloadLength)
132132
}
133133
b = b[8:]
134134
}

json.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (jc JSONConn) read(ctx context.Context, v interface{}) error {
3939
}
4040

4141
if typ != MessageText {
42-
return xerrors.Errorf("unexpected frame type for json (expected DataText): %v", typ)
42+
return xerrors.Errorf("unexpected frame type for json (expected %v): %v", MessageText, typ)
4343
}
4444

4545
r = io.LimitReader(r, 131072)
@@ -65,7 +65,7 @@ func (jc JSONConn) Write(ctx context.Context, v interface{}) error {
6565
func (jc JSONConn) write(ctx context.Context, v interface{}) error {
6666
w, err := jc.Conn.Writer(ctx, MessageText)
6767
if err != nil {
68-
return xerrors.Errorf("failed to get message writer: %w", err)
68+
return err
6969
}
7070

7171
e := json.NewEncoder(w)

statuscode.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const (
4343
)
4444

4545
// CloseError represents an error from a WebSocket close frame.
46-
// It is returned by a Conn's method when the Connection was closed with a WebSocket close frame.
46+
// It is returned by Conn's methods when the Connection is closed with a WebSocket close frame.
4747
type CloseError struct {
4848
Code StatusCode
4949
Reason string

websocket.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ type Conn struct {
6060
}
6161

6262
func (c *Conn) close(err error) {
63-
err = xerrors.Errorf("websocket: connection broken: %w", err)
63+
err = xerrors.Errorf("connection broken: %w", err)
6464

6565
c.closeOnce.Do(func() {
6666
runtime.SetFinalizer(c, nil)
@@ -96,7 +96,7 @@ func (c *Conn) init() {
9696
c.readDone = make(chan int)
9797

9898
runtime.SetFinalizer(c, func(c *Conn) {
99-
c.Close(StatusInternalError, "websocket: connection ended up being garbage collected")
99+
c.Close(StatusInternalError, "connection garbage collected")
100100
})
101101

102102
go c.writeLoop()

0 commit comments

Comments
 (0)