forked from mirror/websocket
compression: add tests, rename option
This commit is contained in:
parent
c09b72d2ee
commit
a0ef436d00
|
@ -73,11 +73,11 @@ type Dialer struct {
|
||||||
// Subprotocols specifies the client's requested subprotocols.
|
// Subprotocols specifies the client's requested subprotocols.
|
||||||
Subprotocols []string
|
Subprotocols []string
|
||||||
|
|
||||||
// CompressionSupported specifies if the client should attempt to negotiate per
|
// EnableCompression specifies if the client should attempt to negotiate
|
||||||
// message compression (RFC 7692). Setting this value to true does not
|
// per message compression (RFC 7692). Setting this value to true does not
|
||||||
// guarantee that compression will be supported. Currently only "no context
|
// guarantee that compression will be supported. Currently only "no context
|
||||||
// takeover" modes are supported.
|
// takeover" modes are supported.
|
||||||
CompressionSupported bool
|
EnableCompression bool
|
||||||
}
|
}
|
||||||
|
|
||||||
var errMalformedURL = errors.New("malformed ws or wss URL")
|
var errMalformedURL = errors.New("malformed ws or wss URL")
|
||||||
|
@ -230,7 +230,7 @@ func (d *Dialer) Dial(urlStr string, requestHeader http.Header) (*Conn, *http.Re
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if d.CompressionSupported {
|
if d.EnableCompression {
|
||||||
req.Header.Set("Sec-Websocket-Extensions", "permessage-deflate; server_no_context_takeover; client_no_context_takeover")
|
req.Header.Set("Sec-Websocket-Extensions", "permessage-deflate; server_no_context_takeover; client_no_context_takeover")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -23,6 +23,7 @@ var cstUpgrader = Upgrader{
|
||||||
Subprotocols: []string{"p0", "p1"},
|
Subprotocols: []string{"p0", "p1"},
|
||||||
ReadBufferSize: 1024,
|
ReadBufferSize: 1024,
|
||||||
WriteBufferSize: 1024,
|
WriteBufferSize: 1024,
|
||||||
|
EnableCompression: true,
|
||||||
Error: func(w http.ResponseWriter, r *http.Request, status int, reason error) {
|
Error: func(w http.ResponseWriter, r *http.Request, status int, reason error) {
|
||||||
http.Error(w, reason.Error(), status)
|
http.Error(w, reason.Error(), status)
|
||||||
},
|
},
|
||||||
|
@ -446,3 +447,17 @@ func TestHostHeader(t *testing.T) {
|
||||||
|
|
||||||
sendRecv(t, ws)
|
sendRecv(t, ws)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDialCompression(t *testing.T) {
|
||||||
|
s := newServer(t)
|
||||||
|
defer s.Close()
|
||||||
|
|
||||||
|
dialer := cstDialer
|
||||||
|
dialer.EnableCompression = true
|
||||||
|
ws, _, err := dialer.Dial(s.URL, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Dial: %v", err)
|
||||||
|
}
|
||||||
|
defer ws.Close()
|
||||||
|
sendRecv(t, ws)
|
||||||
|
}
|
||||||
|
|
|
@ -48,16 +48,20 @@ func TestFraming(t *testing.T) {
|
||||||
writeBuf[i] = byte(i)
|
writeBuf[i] = byte(i)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, compress := range []bool{false, true} {
|
||||||
for _, isServer := range []bool{true, false} {
|
for _, isServer := range []bool{true, false} {
|
||||||
for _, chunker := range readChunkers {
|
for _, chunker := range readChunkers {
|
||||||
|
|
||||||
var connBuf bytes.Buffer
|
var connBuf bytes.Buffer
|
||||||
wc := newConn(fakeNetConn{Reader: nil, Writer: &connBuf}, isServer, 1024, 1024)
|
wc := newConn(fakeNetConn{Reader: nil, Writer: &connBuf}, isServer, 1024, 1024)
|
||||||
rc := newConn(fakeNetConn{Reader: chunker.f(&connBuf), Writer: nil}, !isServer, 1024, 1024)
|
rc := newConn(fakeNetConn{Reader: chunker.f(&connBuf), Writer: nil}, !isServer, 1024, 1024)
|
||||||
|
if compress {
|
||||||
|
wc.newCompressionWriter = compressNoContextTakeover
|
||||||
|
rc.newDecompressionReader = decompressNoContextTakeover
|
||||||
|
}
|
||||||
for _, n := range frameSizes {
|
for _, n := range frameSizes {
|
||||||
for _, iocopy := range []bool{true, false} {
|
for _, iocopy := range []bool{true, false} {
|
||||||
name := fmt.Sprintf("s:%v, r:%s, n:%d c:%v", isServer, chunker.name, n, iocopy)
|
name := fmt.Sprintf("z:%v, s:%v, r:%s, n:%d c:%v", compress, isServer, chunker.name, n, iocopy)
|
||||||
|
|
||||||
w, err := wc.NextWriter(TextMessage)
|
w, err := wc.NextWriter(TextMessage)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -108,6 +112,7 @@ func TestFraming(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestControl(t *testing.T) {
|
func TestControl(t *testing.T) {
|
||||||
|
|
4
doc.go
4
doc.go
|
@ -153,8 +153,8 @@
|
||||||
// Compression [Experimental]
|
// Compression [Experimental]
|
||||||
//
|
//
|
||||||
// Per message compression extensions (RFC 7692) are experimentally supported
|
// Per message compression extensions (RFC 7692) are experimentally supported
|
||||||
// by this package in a limited capacity. Enabling the CompressionSupported
|
// by this package in a limited capacity. Setting the EnableCompression option
|
||||||
// option in Dialer or Upgrader will attempt to negotiate per message deflate
|
// to true in Dialer or Upgrader will attempt to negotiate per message deflate
|
||||||
// support. If compression was successfully negotiated with the connection's
|
// support. If compression was successfully negotiated with the connection's
|
||||||
// peer, any message received in compressed form will be automatically
|
// peer, any message received in compressed form will be automatically
|
||||||
// decompressed. All Read methods will return uncompressed bytes.
|
// decompressed. All Read methods will return uncompressed bytes.
|
||||||
|
|
|
@ -8,17 +8,19 @@ package main
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
"github.com/gorilla/websocket"
|
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
|
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
var upgrader = websocket.Upgrader{
|
var upgrader = websocket.Upgrader{
|
||||||
ReadBufferSize: 4096,
|
ReadBufferSize: 4096,
|
||||||
WriteBufferSize: 4096,
|
WriteBufferSize: 4096,
|
||||||
|
EnableCompression: true,
|
||||||
CheckOrigin: func(r *http.Request) bool {
|
CheckOrigin: func(r *http.Request) bool {
|
||||||
return true
|
return true
|
||||||
},
|
},
|
||||||
|
|
|
@ -47,11 +47,11 @@ type Upgrader struct {
|
||||||
// must match the host of the request.
|
// must match the host of the request.
|
||||||
CheckOrigin func(r *http.Request) bool
|
CheckOrigin func(r *http.Request) bool
|
||||||
|
|
||||||
// CompressionSupported specify if the server should attempt to negotiate per
|
// EnableCompression specify if the server should attempt to negotiate per
|
||||||
// message compression (RFC 7692). Setting this value to true does not
|
// message compression (RFC 7692). Setting this value to true does not
|
||||||
// guarantee that compression will be supported. Currently only "no context
|
// guarantee that compression will be supported. Currently only "no context
|
||||||
// takeover" modes are supported.
|
// takeover" modes are supported.
|
||||||
CompressionSupported bool
|
EnableCompression bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *Upgrader) returnError(w http.ResponseWriter, r *http.Request, status int, reason string) (*Conn, error) {
|
func (u *Upgrader) returnError(w http.ResponseWriter, r *http.Request, status int, reason string) (*Conn, error) {
|
||||||
|
@ -140,7 +140,7 @@ func (u *Upgrader) Upgrade(w http.ResponseWriter, r *http.Request, responseHeade
|
||||||
|
|
||||||
// Negotiate PMCE
|
// Negotiate PMCE
|
||||||
var compress bool
|
var compress bool
|
||||||
if u.CompressionSupported {
|
if u.EnableCompression {
|
||||||
for _, ext := range parseExtensions(r.Header) {
|
for _, ext := range parseExtensions(r.Header) {
|
||||||
if ext[""] != "permessage-deflate" {
|
if ext[""] != "permessage-deflate" {
|
||||||
continue
|
continue
|
||||||
|
|
Loading…
Reference in New Issue