Skip to content

Commit

Permalink
lint.sh: Pass
Browse files Browse the repository at this point in the history
  • Loading branch information
nhooyr committed Oct 13, 2023
1 parent 9b5a15b commit a633a10
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 60 deletions.
39 changes: 2 additions & 37 deletions accept.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion autobahn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
3 changes: 1 addition & 2 deletions close_go113.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !go1.16
// +build !go1.16
//go:build !go1.16 && !js

package websocket

Expand Down
3 changes: 1 addition & 2 deletions close_go116.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build go1.16
// +build go1.16
//go:build go1.16 && !js

package websocket

Expand Down
6 changes: 3 additions & 3 deletions compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions frame.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !js

package websocket

import (
Expand Down
6 changes: 4 additions & 2 deletions frame_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++ {
Expand All @@ -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)
}
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down
3 changes: 1 addition & 2 deletions internal/test/assert/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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.
Expand Down
2 changes: 0 additions & 2 deletions internal/wsjs/wsjs_js.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@ func (c WebSocket) OnMessage(fn func(m MessageEvent)) (remove func()) {
Data: data,
}
fn(me)

return
})
}

Expand Down
4 changes: 2 additions & 2 deletions netconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
26 changes: 23 additions & 3 deletions ws_js.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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")
}
Expand All @@ -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")
}
Expand Down

0 comments on commit a633a10

Please sign in to comment.