Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/gorilla/websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
savsgio committed Mar 3, 2024
2 parents 0709026 + 695e909 commit 766dd2e
Show file tree
Hide file tree
Showing 18 changed files with 183 additions and 45 deletions.
2 changes: 1 addition & 1 deletion _examples/chat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ sends them to the hub.
### Hub

The code for the `Hub` type is in
[hub.go](https://github.com/fasthttp/websocket/blob/master/examples/chat/hub.go).
[hub.go](https://github.com/fasthttp/websocket/blob/master/examples/chat/hub.go).
The application's `main` function starts the hub's `run` method as a goroutine.
Clients send requests to the hub using the `register`, `unregister` and
`broadcast` channels.
Expand Down
2 changes: 0 additions & 2 deletions _examples/command/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ func pumpStdin(ws *websocket.Conn, w io.Writer) {
}

func pumpStdout(ws *websocket.Conn, r io.Reader, done chan struct{}) {
defer func() {
}()
s := bufio.NewScanner(r)
for s.Scan() {
ws.SetWriteDeadline(time.Now().Add(writeWait))
Expand Down
4 changes: 2 additions & 2 deletions _examples/echo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ func main() {
go func() {
defer close(done)
for {
_, message, err := c.ReadMessage()
mt, message, err := c.ReadMessage()
if err != nil {
log.Println("read:", err)
return
}
log.Printf("recv: %s", message)
log.Printf("recv: %s, type: %s", message, websocket.FormatMessageType(mt))
}
}()

Expand Down
3 changes: 2 additions & 1 deletion _examples/echo/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ func echo(w http.ResponseWriter, r *http.Request) {
log.Println("read:", err)
break
}
log.Printf("recv: %s", message)

log.Printf("recv: %s, type: %s", message, websocket.FormatMessageType(mt))
err = c.WriteMessage(mt, message)
if err != nil {
log.Println("write:", err)
Expand Down
4 changes: 2 additions & 2 deletions _examples/filewatch/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package main
import (
"flag"
"html/template"
"io/ioutil"
"log"
"net/http"
"os"
Expand Down Expand Up @@ -50,7 +49,8 @@ func readFileIfModified(lastMod time.Time) ([]byte, time.Time, error) {
if !fi.ModTime().After(lastMod) {
return nil, lastMod, nil
}
p, err := ioutil.ReadFile(filepath.Clean(filename))

p, err := os.ReadFile(filepath.Clean(filename))
if err != nil {
return nil, fi.ModTime(), err
}
Expand Down
22 changes: 22 additions & 0 deletions client_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ func sendRecv(t *testing.T, ws *Conn) {
}

func TestProxyDial(t *testing.T) {
t.Parallel()

s := newServer(t)
defer s.Close()
Expand Down Expand Up @@ -180,6 +181,7 @@ func TestProxyDial(t *testing.T) {
}

func TestProxyAuthorizationDial(t *testing.T) {
t.Parallel()
s := newServer(t)
defer s.Close()

Expand Down Expand Up @@ -220,6 +222,7 @@ func TestProxyAuthorizationDial(t *testing.T) {
}

func TestDial(t *testing.T) {
t.Parallel()
s := newServer(t)
defer s.Close()

Expand All @@ -232,6 +235,7 @@ func TestDial(t *testing.T) {
}

func TestDialCookieJar(t *testing.T) {
t.Parallel()
s := newServer(t)
defer s.Close()

Expand Down Expand Up @@ -294,6 +298,7 @@ func rootCAs(t *testing.T, s *httptest.Server) *x509.CertPool {
}

func TestDialTLS(t *testing.T) {
t.Parallel()
s := newTLSServer(t)
defer s.Close()

Expand All @@ -308,6 +313,7 @@ func TestDialTLS(t *testing.T) {
}

func TestDialTimeout(t *testing.T) {
t.Parallel()
s := newServer(t)
defer s.Close()

Expand Down Expand Up @@ -364,6 +370,7 @@ func (c *requireDeadlineNetConn) LocalAddr() net.Addr { return c.c.LocalAddr()
func (c *requireDeadlineNetConn) RemoteAddr() net.Addr { return c.c.RemoteAddr() }

func TestHandshakeTimeout(t *testing.T) {
t.Parallel()
s := newServer(t)
defer s.Close()

Expand All @@ -380,6 +387,7 @@ func TestHandshakeTimeout(t *testing.T) {
}

func TestHandshakeTimeoutInContext(t *testing.T) {
t.Parallel()
s := newServer(t)
defer s.Close()

Expand All @@ -401,6 +409,7 @@ func TestHandshakeTimeoutInContext(t *testing.T) {
}

func TestDialBadScheme(t *testing.T) {
t.Parallel()
s := newServer(t)
defer s.Close()

Expand All @@ -412,6 +421,7 @@ func TestDialBadScheme(t *testing.T) {
}

func TestDialBadOrigin(t *testing.T) {
t.Parallel()
s := newServer(t)
defer s.Close()

Expand All @@ -429,6 +439,7 @@ func TestDialBadOrigin(t *testing.T) {
}

func TestDialBadHeader(t *testing.T) {
t.Parallel()
s := newServer(t)
defer s.Close()

Expand All @@ -448,6 +459,7 @@ func TestDialBadHeader(t *testing.T) {
}

func TestBadMethod(t *testing.T) {
t.Parallel()
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ws, err := cstUpgrader.Upgrade(w, r, nil)
if err == nil {
Expand Down Expand Up @@ -476,6 +488,7 @@ func TestBadMethod(t *testing.T) {
}

func TestDialExtraTokensInRespHeaders(t *testing.T) {
t.Parallel()
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
challengeKey := r.Header.Get("Sec-Websocket-Key")
w.Header().Set("Upgrade", "foo, websocket")
Expand All @@ -493,6 +506,7 @@ func TestDialExtraTokensInRespHeaders(t *testing.T) {
}

func TestHandshake(t *testing.T) {
t.Parallel()
s := newServer(t)
defer s.Close()

Expand All @@ -519,6 +533,7 @@ func TestHandshake(t *testing.T) {
}

func TestRespOnBadHandshake(t *testing.T) {
t.Parallel()
const expectedStatus = http.StatusGone
const expectedBody = "This is the response body."

Expand Down Expand Up @@ -564,6 +579,7 @@ func (w testLogWriter) Write(p []byte) (int, error) {

// TestHost tests handling of host names and confirms that it matches net/http.
func TestHost(t *testing.T) {
t.Parallel()

upgrader := Upgrader{}
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -745,6 +761,7 @@ func TestHost(t *testing.T) {
}

func TestDialCompression(t *testing.T) {
t.Parallel()
s := newServer(t)
defer s.Close()

Expand All @@ -759,6 +776,7 @@ func TestDialCompression(t *testing.T) {
}

func TestSocksProxyDial(t *testing.T) {
t.Parallel()
s := newServer(t)
defer s.Close()

Expand Down Expand Up @@ -846,6 +864,7 @@ func TestSocksProxyDial(t *testing.T) {
}

func TestTracingDialWithContext(t *testing.T) {
t.Parallel()

var headersWrote, requestWrote, getConn, gotConn, connectDone, gotFirstResponseByte bool
trace := &httptrace.ClientTrace{
Expand Down Expand Up @@ -905,6 +924,7 @@ func TestTracingDialWithContext(t *testing.T) {
}

func TestEmptyTracingDialWithContext(t *testing.T) {
t.Parallel()

trace := &httptrace.ClientTrace{}
ctx := httptrace.WithClientTrace(context.Background(), trace)
Expand All @@ -926,6 +946,7 @@ func TestEmptyTracingDialWithContext(t *testing.T) {

// TestNetDialConnect tests selection of dial method between NetDial, NetDialContext, NetDialTLS or NetDialTLSContext
func TestNetDialConnect(t *testing.T) {
t.Parallel()

upgrader := Upgrader{}
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -1097,6 +1118,7 @@ func TestNetDialConnect(t *testing.T) {
}
}
func TestNextProtos(t *testing.T) {
t.Parallel()
ts := httptest.NewUnstartedServer(
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}),
)
Expand Down
1 change: 1 addition & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var hostPortNoPortTests = []struct {
}

func TestHostPortNoPort(t *testing.T) {
t.Parallel()
for _, tt := range hostPortNoPortTests {
hostPort, hostNoPort := hostPortNoPort(tt.u)
if hostPort != tt.hostPort {
Expand Down
5 changes: 1 addition & 4 deletions compression.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package websocket
import (
"errors"
"io"
"log"
"strings"
"sync"

Expand Down Expand Up @@ -136,9 +135,7 @@ func (r *flateReadWrapper) Read(p []byte) (int, error) {
// Preemptively place the reader back in the pool. This helps with
// scenarios where the application does not call NextReader() soon after
// this final read.
if err := r.Close(); err != nil {
log.Printf("websocket: flateReadWrapper.Close() returned error: %v", err)
}
_ = r.Close()
}
return n, err
}
Expand Down
2 changes: 2 additions & 0 deletions compression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type nopCloser struct{ io.Writer }
func (nopCloser) Close() error { return nil }

func TestTruncWriter(t *testing.T) {
t.Parallel()
const data = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijlkmnopqrstuvwxyz987654321"
for n := 1; n <= 10; n++ {
var b bytes.Buffer
Expand Down Expand Up @@ -71,6 +72,7 @@ func BenchmarkWriteWithCompression(b *testing.B) {
}

func TestValidCompressionLevel(t *testing.T) {
t.Parallel()
c := newTestConn(nil, nil, false)
for _, level := range []int{minCompressionLevel - 1, maxCompressionLevel + 1} {
if err := c.SetCompressionLevel(level); err == nil {
Expand Down
Loading

0 comments on commit 766dd2e

Please sign in to comment.