mirror of https://github.com/gorilla/websocket.git
upgrade: TestFraming
This commit is contained in:
parent
8e146c3b73
commit
c83088956f
16
client.go
16
client.go
|
@ -42,7 +42,6 @@ func NewClient(netConn net.Conn, u *url.URL, requestHeader http.Header, readBufS
|
|||
NetDial: func(net, addr string) (net.Conn, error) {
|
||||
return netConn, nil
|
||||
},
|
||||
CompressionLevel: defaultCompressionLevel,
|
||||
}
|
||||
return d.Dial(u.String(), requestHeader)
|
||||
}
|
||||
|
@ -289,6 +288,9 @@ func (d *Dialer) Dial(urlStr string, requestHeader http.Header) (*Conn, *http.Re
|
|||
conn := newConn(netConn, false, d.ReadBufferSize, d.WriteBufferSize)
|
||||
|
||||
if d.EnableCompression {
|
||||
if !isValidCompressionLevel(d.CompressionLevel) {
|
||||
return nil, nil, errors.New("websocket: invalid compression level")
|
||||
}
|
||||
conn.compressionLevel = d.CompressionLevel
|
||||
}
|
||||
|
||||
|
@ -332,14 +334,14 @@ func (d *Dialer) Dial(urlStr string, requestHeader http.Header) (*Conn, *http.Re
|
|||
case cmwb && smwb:
|
||||
conn.contextTakeover = true
|
||||
|
||||
var f contextTakeoverWriterFactory
|
||||
f.fw, _ = flate.NewWriter(&f.tw, d.CompressionLevel)
|
||||
conn.newCompressionWriter = f.newCompressionWriter
|
||||
var wf contextTakeoverWriterFactory
|
||||
wf.fw, _ = flate.NewWriter(&wf.tw, d.CompressionLevel)
|
||||
conn.newCompressionWriter = wf.newCompressionWriter
|
||||
|
||||
var frf contextTakeoverReaderFactory
|
||||
var rf contextTakeoverReaderFactory
|
||||
fr := flate.NewReader(nil)
|
||||
frf.fr = fr
|
||||
conn.newDecompressionReader = frf.newDeCompressionReader
|
||||
rf.fr = fr
|
||||
conn.newDecompressionReader = rf.newDeCompressionReader
|
||||
default:
|
||||
conn.newCompressionWriter = compressNoContextTakeover
|
||||
conn.newDecompressionReader = decompressNoContextTakeover
|
||||
|
|
|
@ -225,7 +225,7 @@ func (r *flateTakeoverReadWrapper) Read(p []byte) (int, error) {
|
|||
|
||||
n, err := r.f.fr.Read(p)
|
||||
|
||||
// add dictionary
|
||||
// add window
|
||||
r.f.window = append(r.f.window, p[:n]...)
|
||||
if len(r.f.window) > maxWindowBits {
|
||||
offset := len(r.f.window) - maxWindowBits
|
||||
|
|
39
conn_test.go
39
conn_test.go
|
@ -7,6 +7,7 @@ package websocket
|
|||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"compress/flate"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
|
@ -77,20 +78,52 @@ func TestFraming(t *testing.T) {
|
|||
}},
|
||||
}
|
||||
|
||||
for _, compress := range []bool{false, true} {
|
||||
compressConditions := []struct {
|
||||
compress bool
|
||||
contextTakeover bool
|
||||
}{
|
||||
{
|
||||
compress: false,
|
||||
contextTakeover: false,
|
||||
},
|
||||
{
|
||||
compress: true,
|
||||
contextTakeover: false,
|
||||
},
|
||||
{
|
||||
compress: true,
|
||||
contextTakeover: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, compressCondition := range compressConditions {
|
||||
for _, isServer := range []bool{true, false} {
|
||||
for _, chunker := range readChunkers {
|
||||
|
||||
var connBuf bytes.Buffer
|
||||
wc := newConn(fakeNetConn{Reader: nil, Writer: &connBuf}, isServer, 1024, 1024)
|
||||
rc := newConn(fakeNetConn{Reader: chunker.f(&connBuf), Writer: nil}, !isServer, 1024, 1024)
|
||||
if compress {
|
||||
switch {
|
||||
case compressCondition.compress && compressCondition.contextTakeover:
|
||||
|
||||
var wf contextTakeoverWriterFactory
|
||||
wf.fw, _ = flate.NewWriter(&wf.tw, defaultCompressionLevel)
|
||||
wc.newCompressionWriter = wf.newCompressionWriter
|
||||
wc.contextTakeover = true
|
||||
|
||||
var rf contextTakeoverReaderFactory
|
||||
fr := flate.NewReader(nil)
|
||||
rf.fr = fr
|
||||
rc.newDecompressionReader = rf.newDeCompressionReader
|
||||
|
||||
rc.contextTakeover = true
|
||||
case compressCondition.compress:
|
||||
wc.newCompressionWriter = compressNoContextTakeover
|
||||
rc.newDecompressionReader = decompressNoContextTakeover
|
||||
}
|
||||
for _, n := range frameSizes {
|
||||
for _, writer := range writers {
|
||||
name := fmt.Sprintf("z:%v, s:%v, r:%s, n:%d w:%s", compress, isServer, chunker.name, n, writer.name)
|
||||
name := fmt.Sprintf("z:%v, c:%v, s:%v, r:%s, n:%d w:%s", compressCondition.compress, compressCondition.contextTakeover, isServer, chunker.name, n, writer.name)
|
||||
|
||||
w, err := wc.NextWriter(TextMessage)
|
||||
if err != nil {
|
||||
|
|
16
server.go
16
server.go
|
@ -193,20 +193,24 @@ func (u *Upgrader) Upgrade(w http.ResponseWriter, r *http.Request, responseHeade
|
|||
c.subprotocol = subprotocol
|
||||
|
||||
if compress {
|
||||
if !isValidCompressionLevel(u.CompressionLevel) {
|
||||
return nil, errors.New("websocket: invalid compression level")
|
||||
}
|
||||
|
||||
c.compressionLevel = u.CompressionLevel
|
||||
|
||||
switch {
|
||||
case contextTakeover && u.EnableContextTakeover:
|
||||
c.contextTakeover = contextTakeover
|
||||
|
||||
var fwf contextTakeoverWriterFactory
|
||||
fwf.fw, _ = flate.NewWriter(&fwf.tw, u.CompressionLevel)
|
||||
c.newCompressionWriter = fwf.newCompressionWriter
|
||||
var wf contextTakeoverWriterFactory
|
||||
wf.fw, _ = flate.NewWriter(&wf.tw, u.CompressionLevel)
|
||||
c.newCompressionWriter = wf.newCompressionWriter
|
||||
|
||||
var frf contextTakeoverReaderFactory
|
||||
var rf contextTakeoverReaderFactory
|
||||
fr := flate.NewReader(nil)
|
||||
frf.fr = fr
|
||||
c.newDecompressionReader = frf.newDeCompressionReader
|
||||
rf.fr = fr
|
||||
c.newDecompressionReader = rf.newDeCompressionReader
|
||||
default:
|
||||
c.newCompressionWriter = compressNoContextTakeover
|
||||
c.newDecompressionReader = decompressNoContextTakeover
|
||||
|
|
Loading…
Reference in New Issue