Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: context-takeover option to client #7

Merged
merged 1 commit into from
Jan 24, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ type Dialer struct {
// takeover" modes are supported.
EnableCompression bool

// EnableContextTakeover specifies specifies if the client should attempt to negotiate
// per message compression with context-takeover (RFC 7692).
// but window bits is allowed only 15, because go's flate library support 15 bits only.
EnableContextTakeover bool

// Jar specifies the cookie jar.
// If Jar is nil, cookies are not sent in requests and ignored
// in responses.
Expand Down Expand Up @@ -196,7 +201,10 @@ func (d *Dialer) Dial(urlStr string, requestHeader http.Header) (*Conn, *http.Re
}
}

if d.EnableCompression {
switch {
case d.EnableCompression && d.EnableContextTakeover:
req.Header.Set("Sec-Websocket-Extensions", "permessage-deflate; server_max_window_bits=15; client_max_window_bits=15")
case d.EnableCompression:
req.Header.Set("Sec-Websocket-Extensions", "permessage-deflate; server_no_context_takeover; client_no_context_takeover")
}

Expand Down Expand Up @@ -307,13 +315,20 @@ func (d *Dialer) Dial(urlStr string, requestHeader http.Header) (*Conn, *http.Re
if ext[""] != "permessage-deflate" {
continue
}
_, snct := ext["server_no_context_takeover"]
_, cnct := ext["client_no_context_takeover"]
if !snct || !cnct {
return nil, resp, errInvalidCompression

_, cmwb := ext["client_max_window_bits"]
_, smwb := ext["server_max_window_bits"]

switch {
case cmwb && smwb:
conn.contextTakeover = true
conn.newCompressionWriter = compressContextTakeover
conn.newDecompressionReader = decompressContextTakeover
default:
conn.newCompressionWriter = compressNoContextTakeover
conn.newDecompressionReader = decompressNoContextTakeover
}
conn.newCompressionWriter = compressNoContextTakeover
conn.newDecompressionReader = decompressNoContextTakeover

break
}

Expand Down