@@ -12,6 +12,7 @@ import (
1212 "net/http"
1313 "net/textproto"
1414 "net/url"
15+ "strconv"
1516 "strings"
1617
1718 "nhooyr.io/websocket/internal/errd"
@@ -208,6 +209,7 @@ func acceptCompression(r *http.Request, w http.ResponseWriter, mode CompressionM
208209
209210func acceptDeflate (w http.ResponseWriter , ext websocketExtension , mode CompressionMode ) (* compressionOptions , error ) {
210211 copts := mode .opts ()
212+ copts .serverMaxWindowBits = 8
211213
212214 for _ , p := range ext .params {
213215 switch p {
@@ -219,7 +221,27 @@ func acceptDeflate(w http.ResponseWriter, ext websocketExtension, mode Compressi
219221 continue
220222 }
221223
222- if strings .HasPrefix (p , "client_max_window_bits" ) || strings .HasPrefix (p , "server_max_window_bits" ) {
224+ if strings .HasPrefix (p , "client_max_window_bits" ) {
225+ continue
226+
227+ // bits, ok := parseExtensionParameter(p, 15)
228+ // if !ok || bits < 8 || bits > 16 {
229+ // err := fmt.Errorf("invalid client_max_window_bits: %q", p)
230+ // http.Error(w, err.Error(), http.StatusBadRequest)
231+ // return nil, err
232+ // }
233+ // copts.clientMaxWindowBits = bits
234+ // continue
235+ }
236+
237+ if false && strings .HasPrefix (p , "server_max_window_bits" ) {
238+ // We always send back 8 but make sure to validate.
239+ bits , ok := parseExtensionParameter (p , 0 )
240+ if ! ok || bits < 8 || bits > 16 {
241+ err := fmt .Errorf ("invalid server_max_window_bits: %q" , p )
242+ http .Error (w , err .Error (), http .StatusBadRequest )
243+ return nil , err
244+ }
223245 continue
224246 }
225247
@@ -233,6 +255,21 @@ func acceptDeflate(w http.ResponseWriter, ext websocketExtension, mode Compressi
233255 return copts , nil
234256}
235257
258+ // parseExtensionParameter parses the value in the extension parameter p.
259+ // It falls back to defaultVal if there is no value.
260+ // If defaultVal == 0, then ok == false if there is no value.
261+ func parseExtensionParameter (p string , defaultVal int ) (int , bool ) {
262+ ps := strings .Split (p , "=" )
263+ if len (ps ) == 1 {
264+ if defaultVal > 0 {
265+ return defaultVal , true
266+ }
267+ return 0 , false
268+ }
269+ i , e := strconv .Atoi (strings .Trim (ps [1 ], `"` ))
270+ return i , e == nil
271+ }
272+
236273func acceptWebkitDeflate (w http.ResponseWriter , ext websocketExtension , mode CompressionMode ) (* compressionOptions , error ) {
237274 copts := mode .opts ()
238275 // The peer must explicitly request it.
0 commit comments