Merge branch 'feature/add_extension_header' into feature/impl_decompressContextTakeover

This commit is contained in:
misu 2018-01-21 15:28:20 +09:00
commit a2b487748b
1 changed files with 26 additions and 8 deletions

View File

@ -142,14 +142,21 @@ func (u *Upgrader) Upgrade(w http.ResponseWriter, r *http.Request, responseHeade
subprotocol := u.selectSubprotocol(r, responseHeader)
// Negotiate PMCE
var compress bool
var (
compress bool
contextTakeover bool
)
if u.EnableCompression {
for _, ext := range parseExtensions(r.Header) {
if ext[""] != "permessage-deflate" {
continue
}
// map[string]string{"":"permessage-deflate", "client_max_window_bits":""}
// detect context-takeover from client_max_window_bits
if ext[""] == "permessage-deflate" {
compress = true
break
}
if _, ok := ext["client_max_window_bits"]; ok {
contextTakeover = true
}
}
}
@ -177,9 +184,15 @@ func (u *Upgrader) Upgrade(w http.ResponseWriter, r *http.Request, responseHeade
c.subprotocol = subprotocol
if compress {
switch {
case contextTakeover:
c.newCompressionWriter = compressContextTakeover
c.newDecompressionReader = decompressContextTakeover
default:
c.newCompressionWriter = compressNoContextTakeover
c.newDecompressionReader = decompressNoContextTakeover
}
}
p := c.writeBuf[:0]
p = append(p, "HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: "...)
@ -191,8 +204,13 @@ func (u *Upgrader) Upgrade(w http.ResponseWriter, r *http.Request, responseHeade
p = append(p, "\r\n"...)
}
if compress {
switch {
case contextTakeover:
p = append(p, "Sec-Websocket-Extensions: permessage-deflate; server_max_window_bits=15; client_max_window_bits=15\r\n"...)
default:
p = append(p, "Sec-Websocket-Extensions: permessage-deflate; server_no_context_takeover; client_no_context_takeover\r\n"...)
}
}
for k, vs := range responseHeader {
if k == "Sec-Websocket-Protocol" {
continue