@@ -12,6 +12,7 @@ import (
12
12
"net/http"
13
13
"net/textproto"
14
14
"net/url"
15
+ "strconv"
15
16
"strings"
16
17
17
18
"nhooyr.io/websocket/internal/errd"
@@ -208,6 +209,7 @@ func acceptCompression(r *http.Request, w http.ResponseWriter, mode CompressionM
208
209
209
210
func acceptDeflate (w http.ResponseWriter , ext websocketExtension , mode CompressionMode ) (* compressionOptions , error ) {
210
211
copts := mode .opts ()
212
+ copts .serverMaxWindowBits = 8
211
213
212
214
for _ , p := range ext .params {
213
215
switch p {
@@ -219,7 +221,27 @@ func acceptDeflate(w http.ResponseWriter, ext websocketExtension, mode Compressi
219
221
continue
220
222
}
221
223
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
+ }
223
245
continue
224
246
}
225
247
@@ -233,6 +255,21 @@ func acceptDeflate(w http.ResponseWriter, ext websocketExtension, mode Compressi
233
255
return copts , nil
234
256
}
235
257
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
+
236
273
func acceptWebkitDeflate (w http.ResponseWriter , ext websocketExtension , mode CompressionMode ) (* compressionOptions , error ) {
237
274
copts := mode .opts ()
238
275
// The peer must explicitly request it.
0 commit comments