From a633a10fb558ad5b93247ca57f45480f643f7ce6 Mon Sep 17 00:00:00 2001 From: Anmol Sethi Date: Fri, 13 Oct 2023 00:49:40 -0700 Subject: [PATCH] lint.sh: Pass --- accept.go | 39 ++-------------------------------- autobahn_test.go | 2 +- close_go113.go | 3 +-- close_go116.go | 3 +-- compress.go | 6 +++--- frame.go | 2 ++ frame_test.go | 6 ++++-- go.mod | 1 - go.sum | 3 --- internal/test/assert/assert.go | 3 +-- internal/wsjs/wsjs_js.go | 2 -- netconn.go | 4 ++-- ws_js.go | 26 ++++++++++++++++++++--- 13 files changed, 40 insertions(+), 60 deletions(-) diff --git a/accept.go b/accept.go index d918aab5..ff2033e7 100644 --- a/accept.go +++ b/accept.go @@ -245,11 +245,10 @@ func acceptCompression(r *http.Request, w http.ResponseWriter, mode CompressionM for _, ext := range websocketExtensions(r.Header) { switch ext.name { + // We used to implement x-webkit-deflate-fram too but Safari has bugs. + // See https://github.com/nhooyr/websocket/issues/218 case "permessage-deflate": return acceptDeflate(w, ext, mode) - // Disabled for now, see https://github.com/nhooyr/websocket/issues/218 - // case "x-webkit-deflate-frame": - // return acceptWebkitDeflate(w, ext, mode) } } return nil, nil @@ -283,40 +282,6 @@ func acceptDeflate(w http.ResponseWriter, ext websocketExtension, mode Compressi return copts, nil } -func acceptWebkitDeflate(w http.ResponseWriter, ext websocketExtension, mode CompressionMode) (*compressionOptions, error) { - copts := mode.opts() - // The peer must explicitly request it. - copts.serverNoContextTakeover = false - - for _, p := range ext.params { - if p == "no_context_takeover" { - copts.serverNoContextTakeover = true - continue - } - - // We explicitly fail on x-webkit-deflate-frame's max_window_bits parameter instead - // of ignoring it as the draft spec is unclear. It says the server can ignore it - // but the server has no way of signalling to the client it was ignored as the parameters - // are set one way. - // Thus us ignoring it would make the client think we understood it which would cause issues. - // See https://tools.ietf.org/html/draft-tyoshino-hybi-websocket-perframe-deflate-06#section-4.1 - // - // Either way, we're only implementing this for webkit which never sends the max_window_bits - // parameter so we don't need to worry about it. - err := fmt.Errorf("unsupported x-webkit-deflate-frame parameter: %q", p) - http.Error(w, err.Error(), http.StatusBadRequest) - return nil, err - } - - s := "x-webkit-deflate-frame" - if copts.clientNoContextTakeover { - s += "; no_context_takeover" - } - w.Header().Set("Sec-WebSocket-Extensions", s) - - return copts, nil -} - func headerContainsTokenIgnoreCase(h http.Header, key, token string) bool { for _, t := range headerTokens(h, key) { if strings.EqualFold(t, token) { diff --git a/autobahn_test.go b/autobahn_test.go index 8100c37f..41fae555 100644 --- a/autobahn_test.go +++ b/autobahn_test.go @@ -38,7 +38,7 @@ var autobahnCases = []string{"*"} // and not excluded by excludedAutobahnCases. Adding cases here means excludedAutobahnCases // is niled. // TODO: -var forceAutobahnCases = []string{} +// var forceAutobahnCases = []string{} func TestAutobahn(t *testing.T) { t.Parallel() diff --git a/close_go113.go b/close_go113.go index fb226475..caf1b89e 100644 --- a/close_go113.go +++ b/close_go113.go @@ -1,5 +1,4 @@ -//go:build !go1.16 -// +build !go1.16 +//go:build !go1.16 && !js package websocket diff --git a/close_go116.go b/close_go116.go index 2724e0ca..9d986109 100644 --- a/close_go116.go +++ b/close_go116.go @@ -1,5 +1,4 @@ -//go:build go1.16 -// +build go1.16 +//go:build go1.16 && !js package websocket diff --git a/compress.go b/compress.go index a9e1fa35..e6722fc7 100644 --- a/compress.go +++ b/compress.go @@ -201,9 +201,9 @@ func (sw *slidingWindow) init(n int) { } p := slidingWindowPool(n) - buf, ok := p.Get().([]byte) + buf, ok := p.Get().(*[]byte) if ok { - sw.buf = buf[:0] + sw.buf = (*buf)[:0] } else { sw.buf = make([]byte, 0, n) } @@ -215,7 +215,7 @@ func (sw *slidingWindow) close() { } swPoolMu.Lock() - swPool[cap(sw.buf)].Put(sw.buf) + swPool[cap(sw.buf)].Put(&sw.buf) swPoolMu.Unlock() sw.buf = nil } diff --git a/frame.go b/frame.go index 2a036f94..351632fd 100644 --- a/frame.go +++ b/frame.go @@ -1,3 +1,5 @@ +//go:build !js + package websocket import ( diff --git a/frame_test.go b/frame_test.go index 93ad8b5f..2f4f2e25 100644 --- a/frame_test.go +++ b/frame_test.go @@ -55,7 +55,7 @@ func TestHeader(t *testing.T) { r := rand.New(rand.NewSource(time.Now().UnixNano())) randBool := func() bool { - return r.Intn(1) == 0 + return r.Intn(2) == 0 } for i := 0; i < 10000; i++ { @@ -67,9 +67,11 @@ func TestHeader(t *testing.T) { opcode: opcode(r.Intn(16)), masked: randBool(), - maskKey: r.Uint32(), payloadLength: r.Int63(), } + if h.masked { + h.maskKey = r.Uint32() + } testHeader(t, h) } diff --git a/go.mod b/go.mod index 50c873bf..95a1df92 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,6 @@ go 1.19 require ( github.com/gin-gonic/gin v1.9.1 github.com/gobwas/ws v1.3.0 - github.com/golang/protobuf v1.5.3 github.com/google/go-cmp v0.5.9 github.com/gorilla/websocket v1.5.0 golang.org/x/time v0.3.0 diff --git a/go.sum b/go.sum index 78c452e4..dc4743dd 100644 --- a/go.sum +++ b/go.sum @@ -29,8 +29,6 @@ github.com/gobwas/ws v1.3.0/go.mod h1:hRKAFb8wOxFROYNsT1bqfWnhX+b5MFeJM9r2ZSwg/K github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= -github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= -github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= @@ -87,7 +85,6 @@ golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4= golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= -google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng= google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= diff --git a/internal/test/assert/assert.go b/internal/test/assert/assert.go index 6eaf7fc3..e37e9573 100644 --- a/internal/test/assert/assert.go +++ b/internal/test/assert/assert.go @@ -6,7 +6,6 @@ import ( "strings" "testing" - "github.com/golang/protobuf/proto" "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" ) @@ -15,7 +14,7 @@ import ( func Diff(v1, v2 interface{}) string { return cmp.Diff(v1, v2, cmpopts.EquateErrors(), cmp.Exporter(func(r reflect.Type) bool { return true - }), cmp.Comparer(proto.Equal)) + })) } // Equal asserts exp == act. diff --git a/internal/wsjs/wsjs_js.go b/internal/wsjs/wsjs_js.go index 88e8f43f..11eb59cb 100644 --- a/internal/wsjs/wsjs_js.go +++ b/internal/wsjs/wsjs_js.go @@ -119,8 +119,6 @@ func (c WebSocket) OnMessage(fn func(m MessageEvent)) (remove func()) { Data: data, } fn(me) - - return }) } diff --git a/netconn.go b/netconn.go index aea1a02d..4af6c202 100644 --- a/netconn.go +++ b/netconn.go @@ -200,7 +200,7 @@ func (nc *netConn) SetWriteDeadline(t time.Time) error { if t.IsZero() { nc.writeTimer.Stop() } else { - nc.writeTimer.Reset(t.Sub(time.Now())) + nc.writeTimer.Reset(time.Until(t)) } return nil } @@ -210,7 +210,7 @@ func (nc *netConn) SetReadDeadline(t time.Time) error { if t.IsZero() { nc.readTimer.Stop() } else { - nc.readTimer.Reset(t.Sub(time.Now())) + nc.readTimer.Reset(time.Until(t)) } return nil } diff --git a/ws_js.go b/ws_js.go index d1361328..3248933c 100644 --- a/ws_js.go +++ b/ws_js.go @@ -18,6 +18,26 @@ import ( "nhooyr.io/websocket/internal/xsync" ) +// opcode represents a WebSocket opcode. +type opcode int + +// https://tools.ietf.org/html/rfc6455#section-11.8. +const ( + opContinuation opcode = iota + opText + opBinary + // 3 - 7 are reserved for further non-control frames. + _ + _ + _ + _ + _ + opClose + opPing + opPong + // 11-16 are reserved for further control frames. +) + // Conn provides a wrapper around the browser WebSocket API. type Conn struct { ws wsjs.WebSocket @@ -302,7 +322,7 @@ func (c *Conn) Reader(ctx context.Context) (MessageType, io.Reader, error) { // It buffers the entire message in memory and then sends it when the writer // is closed. func (c *Conn) Writer(ctx context.Context, typ MessageType) (io.WriteCloser, error) { - return writer{ + return &writer{ c: c, ctx: ctx, typ: typ, @@ -320,7 +340,7 @@ type writer struct { b *bytes.Buffer } -func (w writer) Write(p []byte) (int, error) { +func (w *writer) Write(p []byte) (int, error) { if w.closed { return 0, errors.New("cannot write to closed writer") } @@ -331,7 +351,7 @@ func (w writer) Write(p []byte) (int, error) { return n, nil } -func (w writer) Close() error { +func (w *writer) Close() error { if w.closed { return errors.New("cannot close closed writer") }