diff --git a/.github/lib.sh b/.github/lib.sh index 06c8f4a508a188b1f0e276264dddd3ecf0e1f245..e656feef5efa7468748c8ad402ee5de2c5ddc732 100644 --- a/.github/lib.sh +++ b/.github/lib.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash -set -euxo pipefail +set -euxo pipefail || exit 1 export GO111MODULE=on export PAGER=cat diff --git a/accept.go b/accept.go index d2ec8b207f6daf36f39ed974ec0d3ea240a84706..bde56e907db68d460757b5c66cae2b54ed183e9d 100644 --- a/accept.go +++ b/accept.go @@ -22,11 +22,11 @@ type acceptSubprotocols []string func (o acceptSubprotocols) acceptOption() {} -// AcceptProtocols lists the websocket protocols that Accept will negotiate with a client. -// The empty protocol will always be negotiated as per RFC 6455. If you would like to +// AcceptSubprotocols lists the websocket subprotocols that Accept will negotiate with a client. +// The empty subprotocol will always be negotiated as per RFC 6455. If you would like to // reject it, close the connection if c.Subprotocol() == "". -func AcceptProtocols(subprotocols ...string) AcceptOption { - return acceptSubprotocols(subprotocols) +func AcceptSubprotocols(protocols ...string) AcceptOption { + return acceptSubprotocols(protocols) } type acceptOrigins []string @@ -34,13 +34,13 @@ type acceptOrigins []string func (o acceptOrigins) acceptOption() {} // AcceptOrigins lists the origins that Accept will accept. -// Accept will always accept r.Host as the origin so you do not need to -// specify that with this option. +// Accept will always accept r.Host as the origin. Use this +// option when you want to accept an origin with a different domain +// than the one the WebSocket server is running on. // // Use this option with caution to avoid exposing your WebSocket // server to a CSRF attack. // See https://stackoverflow.com/a/37837709/4283659 -// You can use a * for wildcards. func AcceptOrigins(origins ...string) AcceptOption { return acceptOrigins(origins) } diff --git a/websocket.go b/websocket.go index 4a552b59b70c6fb5547f68cfd6d6cc2e854d2a48..09a94e7809ca6c8860093acb4ce46a34bef0e38a 100644 --- a/websocket.go +++ b/websocket.go @@ -70,7 +70,7 @@ func (c *Conn) close(err error) { // Subprotocol returns the negotiated subprotocol. // An empty string means the default protocol. -func (c *Conn) Protocol() string { +func (c *Conn) Subprotocol() string { return c.subprotocol } diff --git a/websocket_test.go b/websocket_test.go index 6f5869c994534eb59b9a5312be3a8d59480cbeaf..14dcc8c51fc1a9eb74f9448d7a33ae546611ff80 100644 --- a/websocket_test.go +++ b/websocket_test.go @@ -34,7 +34,7 @@ func TestHandshake(t *testing.T) { { name: "handshake", server: func(w http.ResponseWriter, r *http.Request) error { - c, err := websocket.Accept(w, r, websocket.AcceptProtocols("myproto")) + c, err := websocket.Accept(w, r, websocket.AcceptSubprotocols("myproto")) if err != nil { return err } @@ -74,8 +74,8 @@ func TestHandshake(t *testing.T) { } defer c.Close(websocket.StatusInternalError, "") - if c.Protocol() != "" { - return xerrors.Errorf("unexpected subprotocol: %v", c.Protocol()) + if c.Subprotocol() != "" { + return xerrors.Errorf("unexpected subprotocol: %v", c.Subprotocol()) } return nil }, @@ -86,8 +86,8 @@ func TestHandshake(t *testing.T) { } defer c.Close(websocket.StatusInternalError, "") - if c.Protocol() != "" { - return xerrors.Errorf("unexpected subprotocol: %v", c.Protocol()) + if c.Subprotocol() != "" { + return xerrors.Errorf("unexpected subprotocol: %v", c.Subprotocol()) } return nil }, @@ -95,14 +95,14 @@ func TestHandshake(t *testing.T) { { name: "subprotocol", server: func(w http.ResponseWriter, r *http.Request) error { - c, err := websocket.Accept(w, r, websocket.AcceptProtocols("echo", "lar")) + c, err := websocket.Accept(w, r, websocket.AcceptSubprotocols("echo", "lar")) if err != nil { return err } defer c.Close(websocket.StatusInternalError, "") - if c.Protocol() != "echo" { - return xerrors.Errorf("unexpected subprotocol: %q", c.Protocol()) + if c.Subprotocol() != "echo" { + return xerrors.Errorf("unexpected subprotocol: %q", c.Subprotocol()) } return nil }, @@ -113,8 +113,8 @@ func TestHandshake(t *testing.T) { } defer c.Close(websocket.StatusInternalError, "") - if c.Protocol() != "echo" { - return xerrors.Errorf("unexpected subprotocol: %q", c.Protocol()) + if c.Subprotocol() != "echo" { + return xerrors.Errorf("unexpected subprotocol: %q", c.Subprotocol()) } return nil }, @@ -266,7 +266,7 @@ func TestAutobahnServer(t *testing.T) { s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { c, err := websocket.Accept(w, r, - websocket.AcceptProtocols("echo"), + websocket.AcceptSubprotocols("echo"), ) if err != nil { t.Logf("server handshake failed: %+v", err)