| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import zlib |
| 4 |
from collections.abc import Sequence |
| 5 |
from typing import Any, Literal |
| 6 |
|
| 7 |
from ..exceptions import ( |
| 8 |
DuplicateParameter, |
| 9 |
InvalidParameterName, |
| 10 |
InvalidParameterValue, |
| 11 |
NegotiationError, |
| 12 |
PayloadTooBig, |
| 13 |
ProtocolError, |
| 14 |
) |
| 15 |
from ..frames import CONT, CTRL_OPCODES, Frame |
| 16 |
from ..typing import BytesLike, ExtensionName, ExtensionParameter |
| 17 |
from .base import ClientExtensionFactory, Extension, ServerExtensionFactory |
| 18 |
|
| 19 |
|
| 20 |
__all__ = [ |
| 21 |
"PerMessageDeflate", |
| 22 |
"ClientPerMessageDeflateFactory", |
| 23 |
"enable_client_permessage_deflate", |
| 24 |
"ServerPerMessageDeflateFactory", |
| 25 |
"enable_server_permessage_deflate", |
| 26 |
] |
| 27 |
|
| 28 |
_EMPTY_UNCOMPRESSED_BLOCK = b"\x00\x00\xff\xff" |
| 29 |
|
| 30 |
_MAX_WINDOW_BITS_VALUES = [str(bits) for bits in range(8, 16)] |
| 31 |
|
| 32 |
|
| 33 |
class PerMessageDeflate(Extension): |
| 34 |
""" |
| 35 |
Per-Message Deflate extension. |
| 36 |
|
| 37 |
""" |
| 38 |
|
| 39 |
name = ExtensionName("permessage-deflate") |
| 40 |
|
| 41 |
def __init__( |
| 42 |
self, |
| 43 |
remote_no_context_takeover: bool, |
| 44 |
local_no_context_takeover: bool, |
| 45 |
remote_max_window_bits: int, |
| 46 |
local_max_window_bits: int, |
| 47 |
compress_settings: dict[Any, Any] | None = None, |
| 48 |
) -> None: |
| 49 |
""" |
| 50 |
Configure the Per-Message Deflate extension. |
| 51 |
|
| 52 |
""" |
| 53 |
if compress_settings is None: |
| 54 |
compress_settings = {} |
| 55 |
|
| 56 |
assert remote_no_context_takeover in [False, True] |
| 57 |
assert local_no_context_takeover in [False, True] |
| 58 |
assert 8 <= remote_max_window_bits <= 15 |
| 59 |
assert 8 <= local_max_window_bits <= 15 |
| 60 |
assert "wbits" not in compress_settings |
| 61 |
|
| 62 |
self.remote_no_context_takeover = remote_no_context_takeover |
| 63 |
self.local_no_context_takeover = local_no_context_takeover |
| 64 |
self.remote_max_window_bits = remote_max_window_bits |
| 65 |
self.local_max_window_bits = local_max_window_bits |
| 66 |
self.compress_settings = compress_settings |
| 67 |
|
| 68 |
if not self.remote_no_context_takeover: |
| 69 |
self.decoder = zlib.decompressobj(wbits=-self.remote_max_window_bits) |
| 70 |
|
| 71 |
if not self.local_no_context_takeover: |
| 72 |
self.encoder = zlib.compressobj( |
| 73 |
wbits=-self.local_max_window_bits, |
| 74 |
**self.compress_settings, |
| 75 |
) |
| 76 |
|
| 77 |
# To handle continuation frames properly, we must keep track of |
| 78 |
# whether that initial frame was encoded. |
| 79 |
self.decode_cont_data = False |
| 80 |
# There's no need for self.encode_cont_data because we always encode |
| 81 |
# outgoing frames, so it would always be True. |
| 82 |
|
| 83 |
def __repr__(self) -> str: |
| 84 |
return ( |
| 85 |
f"PerMessageDeflate(" |
| 86 |
f"remote_no_context_takeover={self.remote_no_context_takeover}, " |
| 87 |
f"local_no_context_takeover={self.local_no_context_takeover}, " |
| 88 |
f"remote_max_window_bits={self.remote_max_window_bits}, " |
| 89 |
f"local_max_window_bits={self.local_max_window_bits})" |
| 90 |
) |
| 91 |
|
| 92 |
def decode( |
| 93 |
self, |
| 94 |
frame: Frame, |
| 95 |
*, |
| 96 |
max_size: int | None = None, |
| 97 |
) -> Frame: |
| 98 |
""" |
| 99 |
Decode an incoming frame. |
| 100 |
|
| 101 |
""" |
| 102 |
# Skip control frames. |
| 103 |
if frame.opcode in CTRL_OPCODES: |
| 104 |
return frame |
| 105 |
|
| 106 |
# Handle continuation data frames: |
| 107 |
# - skip if the message isn't encoded |
| 108 |
# - reset "decode continuation data" flag if it's a final frame |
| 109 |
if frame.opcode is CONT: |
| 110 |
if not self.decode_cont_data: |
| 111 |
return frame |
| 112 |
if frame.rsv1: |
| 113 |
raise ProtocolError("RSV1 bit set in continuation frame") |
| 114 |
if frame.fin: |
| 115 |
self.decode_cont_data = False |
| 116 |
|
| 117 |
# Handle text and binary data frames: |
| 118 |
# - skip if the message isn't encoded |
| 119 |
# - unset the rsv1 flag on the first frame of a compressed message |
| 120 |
# - set "decode continuation data" flag if it's a non-final frame |
| 121 |
else: |
| 122 |
if not frame.rsv1: |
| 123 |
return frame |
| 124 |
if not frame.fin: |
| 125 |
self.decode_cont_data = True |
| 126 |
|
| 127 |
# Re-initialize per-message decoder. |
| 128 |
if self.remote_no_context_takeover: |
| 129 |
self.decoder = zlib.decompressobj(wbits=-self.remote_max_window_bits) |
| 130 |
|
| 131 |
# Uncompress data. Protect against zip bombs by preventing zlib from |
| 132 |
# decompressing more than max_length bytes (except when the limit is |
| 133 |
# disabled with max_size = None). |
| 134 |
data: BytesLike |
| 135 |
if frame.fin and len(frame.data) < 2044: |
| 136 |
# Profiling shows that appending four bytes, which makes a copy, is |
| 137 |
# faster than calling decompress() again when data is less than 2kB. |
| 138 |
data = bytes(frame.data) + _EMPTY_UNCOMPRESSED_BLOCK |
| 139 |
else: |
| 140 |
data = frame.data |
| 141 |
max_length = 0 if max_size is None else max_size |
| 142 |
try: |
| 143 |
data = self.decoder.decompress(data, max_length) |
| 144 |
if self.decoder.unconsumed_tail: |
| 145 |
raise PayloadTooBig(None, max_size) |
| 146 |
if frame.fin and len(frame.data) >= 2044: |
| 147 |
# In edge cases, flushing may yield data held back by max_size. |
| 148 |
if self.decoder.decompress(_EMPTY_UNCOMPRESSED_BLOCK, 1): |
| 149 |
raise PayloadTooBig(None, max_size) |
| 150 |
except zlib.error as exc: |
| 151 |
raise ProtocolError("decompression failed") from exc |
| 152 |
|
| 153 |
# Allow garbage collection of the decoder if it won't be reused. |
| 154 |
if frame.fin and self.remote_no_context_takeover: |
| 155 |
del self.decoder |
| 156 |
|
| 157 |
return Frame( |
| 158 |
frame.opcode, |
| 159 |
data, |
| 160 |
frame.fin, |
| 161 |
# Unset the rsv1 flag on the first frame of a compressed message. |
| 162 |
False, |
| 163 |
frame.rsv2, |
| 164 |
frame.rsv3, |
| 165 |
) |
| 166 |
|
| 167 |
def encode(self, frame: Frame) -> Frame: |
| 168 |
""" |
| 169 |
Encode an outgoing frame. |
| 170 |
|
| 171 |
""" |
| 172 |
# Skip control frames. |
| 173 |
if frame.opcode in CTRL_OPCODES: |
| 174 |
return frame |
| 175 |
|
| 176 |
# Since we always encode messages, there's no "encode continuation |
| 177 |
# data" flag similar to "decode continuation data" at this time. |
| 178 |
|
| 179 |
if frame.opcode is not CONT: |
| 180 |
# Re-initialize per-message decoder. |
| 181 |
if self.local_no_context_takeover: |
| 182 |
self.encoder = zlib.compressobj( |
| 183 |
wbits=-self.local_max_window_bits, |
| 184 |
**self.compress_settings, |
| 185 |
) |
| 186 |
|
| 187 |
# Compress data. |
| 188 |
data: BytesLike |
| 189 |
data = self.encoder.compress(frame.data) + self.encoder.flush(zlib.Z_SYNC_FLUSH) |
| 190 |
if frame.fin: |
| 191 |
# Sync flush generates between 5 or 6 bytes, ending with the bytes |
| 192 |
# 0x00 0x00 0xff 0xff, which must be removed. |
| 193 |
assert data[-4:] == _EMPTY_UNCOMPRESSED_BLOCK |
| 194 |
# Making a copy is faster than memoryview(a)[:-4] until 2kB. |
| 195 |
if len(data) < 2048: |
| 196 |
data = data[:-4] |
| 197 |
else: |
| 198 |
data = memoryview(data)[:-4] |
| 199 |
|
| 200 |
# Allow garbage collection of the encoder if it won't be reused. |
| 201 |
if frame.fin and self.local_no_context_takeover: |
| 202 |
del self.encoder |
| 203 |
|
| 204 |
return Frame( |
| 205 |
frame.opcode, |
| 206 |
data, |
| 207 |
frame.fin, |
| 208 |
# Set the rsv1 flag on the first frame of a compressed message. |
| 209 |
frame.opcode is not CONT, |
| 210 |
frame.rsv2, |
| 211 |
frame.rsv3, |
| 212 |
) |
| 213 |
|
| 214 |
|
| 215 |
def _build_parameters( |
| 216 |
server_no_context_takeover: bool, |
| 217 |
client_no_context_takeover: bool, |
| 218 |
server_max_window_bits: int | None, |
| 219 |
client_max_window_bits: int | Literal[True] | None, |
| 220 |
) -> list[ExtensionParameter]: |
| 221 |
""" |
| 222 |
Build a list of ``(name, value)`` pairs for some compression parameters. |
| 223 |
|
| 224 |
""" |
| 225 |
params: list[ExtensionParameter] = [] |
| 226 |
if server_no_context_takeover: |
| 227 |
params.append(("server_no_context_takeover", None)) |
| 228 |
if client_no_context_takeover: |
| 229 |
params.append(("client_no_context_takeover", None)) |
| 230 |
if server_max_window_bits: |
| 231 |
params.append(("server_max_window_bits", str(server_max_window_bits))) |
| 232 |
if client_max_window_bits is True: # only in handshake requests |
| 233 |
params.append(("client_max_window_bits", None)) |
| 234 |
elif client_max_window_bits: |
| 235 |
params.append(("client_max_window_bits", str(client_max_window_bits))) |
| 236 |
return params |
| 237 |
|
| 238 |
|
| 239 |
def _extract_parameters( |
| 240 |
params: Sequence[ExtensionParameter], *, is_server: bool |
| 241 |
) -> tuple[bool, bool, int | None, int | Literal[True] | None]: |
| 242 |
""" |
| 243 |
Extract compression parameters from a list of ``(name, value)`` pairs. |
| 244 |
|
| 245 |
If ``is_server`` is :obj:`True`, ``client_max_window_bits`` may be |
| 246 |
provided without a value. This is only allowed in handshake requests. |
| 247 |
|
| 248 |
""" |
| 249 |
server_no_context_takeover: bool = False |
| 250 |
client_no_context_takeover: bool = False |
| 251 |
server_max_window_bits: int | None = None |
| 252 |
client_max_window_bits: int | Literal[True] | None = None |
| 253 |
|
| 254 |
for name, value in params: |
| 255 |
if name == "server_no_context_takeover": |
| 256 |
if server_no_context_takeover: |
| 257 |
raise DuplicateParameter(name) |
| 258 |
if value is None: |
| 259 |
server_no_context_takeover = True |
| 260 |
else: |
| 261 |
raise InvalidParameterValue(name, value) |
| 262 |
|
| 263 |
elif name == "client_no_context_takeover": |
| 264 |
if client_no_context_takeover: |
| 265 |
raise DuplicateParameter(name) |
| 266 |
if value is None: |
| 267 |
client_no_context_takeover = True |
| 268 |
else: |
| 269 |
raise InvalidParameterValue(name, value) |
| 270 |
|
| 271 |
elif name == "server_max_window_bits": |
| 272 |
if server_max_window_bits is not None: |
| 273 |
raise DuplicateParameter(name) |
| 274 |
if value in _MAX_WINDOW_BITS_VALUES: |
| 275 |
server_max_window_bits = int(value) |
| 276 |
else: |
| 277 |
raise InvalidParameterValue(name, value) |
| 278 |
|
| 279 |
elif name == "client_max_window_bits": |
| 280 |
if client_max_window_bits is not None: |
| 281 |
raise DuplicateParameter(name) |
| 282 |
if is_server and value is None: # only in handshake requests |
| 283 |
client_max_window_bits = True |
| 284 |
elif value in _MAX_WINDOW_BITS_VALUES: |
| 285 |
client_max_window_bits = int(value) |
| 286 |
else: |
| 287 |
raise InvalidParameterValue(name, value) |
| 288 |
|
| 289 |
else: |
| 290 |
raise InvalidParameterName(name) |
| 291 |
|
| 292 |
return ( |
| 293 |
server_no_context_takeover, |
| 294 |
client_no_context_takeover, |
| 295 |
server_max_window_bits, |
| 296 |
client_max_window_bits, |
| 297 |
) |
| 298 |
|
| 299 |
|
| 300 |
class ClientPerMessageDeflateFactory(ClientExtensionFactory): |
| 301 |
""" |
| 302 |
Client-side extension factory for the Per-Message Deflate extension. |
| 303 |
|
| 304 |
Parameters behave as described in `section 7.1 of RFC 7692`_. |
| 305 |
|
| 306 |
.. _section 7.1 of RFC 7692: https://datatracker.ietf.org/doc/html/rfc7692#section-7.1 |
| 307 |
|
| 308 |
Set them to :obj:`True` to include them in the negotiation offer without a |
| 309 |
value or to an integer value to include them with this value. |
| 310 |
|
| 311 |
Args: |
| 312 |
server_no_context_takeover: Prevent server from using context takeover. |
| 313 |
client_no_context_takeover: Prevent client from using context takeover. |
| 314 |
server_max_window_bits: Maximum size of the server's LZ77 sliding window |
| 315 |
in bits, between 8 and 15. |
| 316 |
client_max_window_bits: Maximum size of the client's LZ77 sliding window |
| 317 |
in bits, between 8 and 15, or :obj:`True` to indicate support without |
| 318 |
setting a limit. |
| 319 |
compress_settings: Additional keyword arguments for :func:`zlib.compressobj`, |
| 320 |
excluding ``wbits``. |
| 321 |
|
| 322 |
""" |
| 323 |
|
| 324 |
name = ExtensionName("permessage-deflate") |
| 325 |
|
| 326 |
def __init__( |
| 327 |
self, |
| 328 |
server_no_context_takeover: bool = False, |
| 329 |
client_no_context_takeover: bool = False, |
| 330 |
server_max_window_bits: int | None = None, |
| 331 |
client_max_window_bits: int | Literal[True] | None = True, |
| 332 |
compress_settings: dict[str, Any] | None = None, |
| 333 |
) -> None: |
| 334 |
""" |
| 335 |
Configure the Per-Message Deflate extension factory. |
| 336 |
|
| 337 |
""" |
| 338 |
if not (server_max_window_bits is None or 8 <= server_max_window_bits <= 15): |
| 339 |
raise ValueError("server_max_window_bits must be between 8 and 15") |
| 340 |
if not ( |
| 341 |
client_max_window_bits is None |
| 342 |
or client_max_window_bits is True |
| 343 |
or 8 <= client_max_window_bits <= 15 |
| 344 |
): |
| 345 |
raise ValueError("client_max_window_bits must be between 8 and 15") |
| 346 |
if compress_settings is not None and "wbits" in compress_settings: |
| 347 |
raise ValueError( |
| 348 |
"compress_settings must not include wbits, " |
| 349 |
"set client_max_window_bits instead" |
| 350 |
) |
| 351 |
|
| 352 |
self.server_no_context_takeover = server_no_context_takeover |
| 353 |
self.client_no_context_takeover = client_no_context_takeover |
| 354 |
self.server_max_window_bits = server_max_window_bits |
| 355 |
self.client_max_window_bits = client_max_window_bits |
| 356 |
self.compress_settings = compress_settings |
| 357 |
|
| 358 |
def get_request_params(self) -> Sequence[ExtensionParameter]: |
| 359 |
""" |
| 360 |
Build request parameters. |
| 361 |
|
| 362 |
""" |
| 363 |
return _build_parameters( |
| 364 |
self.server_no_context_takeover, |
| 365 |
self.client_no_context_takeover, |
| 366 |
self.server_max_window_bits, |
| 367 |
self.client_max_window_bits, |
| 368 |
) |
| 369 |
|
| 370 |
def process_response_params( |
| 371 |
self, |
| 372 |
params: Sequence[ExtensionParameter], |
| 373 |
accepted_extensions: Sequence[Extension], |
| 374 |
) -> PerMessageDeflate: |
| 375 |
""" |
| 376 |
Process response parameters. |
| 377 |
|
| 378 |
Return an extension instance. |
| 379 |
|
| 380 |
""" |
| 381 |
if any(other.name == self.name for other in accepted_extensions): |
| 382 |
raise NegotiationError(f"received duplicate {self.name}") |
| 383 |
|
| 384 |
# Request parameters are available in instance variables. |
| 385 |
|
| 386 |
# Load response parameters in local variables. |
| 387 |
( |
| 388 |
server_no_context_takeover, |
| 389 |
client_no_context_takeover, |
| 390 |
server_max_window_bits, |
| 391 |
client_max_window_bits, |
| 392 |
) = _extract_parameters(params, is_server=False) |
| 393 |
|
| 394 |
# After comparing the request and the response, the final |
| 395 |
# configuration must be available in the local variables. |
| 396 |
|
| 397 |
# server_no_context_takeover |
| 398 |
# |
| 399 |
# Req. Resp. Result |
| 400 |
# ------ ------ -------------------------------------------------- |
| 401 |
# False False False |
| 402 |
# False True True |
| 403 |
# True False Error! |
| 404 |
# True True True |
| 405 |
|
| 406 |
if self.server_no_context_takeover: |
| 407 |
if not server_no_context_takeover: |
| 408 |
raise NegotiationError("expected server_no_context_takeover") |
| 409 |
|
| 410 |
# client_no_context_takeover |
| 411 |
# |
| 412 |
# Req. Resp. Result |
| 413 |
# ------ ------ -------------------------------------------------- |
| 414 |
# False False False |
| 415 |
# False True True |
| 416 |
# True False True - must change value |
| 417 |
# True True True |
| 418 |
|
| 419 |
if self.client_no_context_takeover: |
| 420 |
if not client_no_context_takeover: |
| 421 |
client_no_context_takeover = True |
| 422 |
|
| 423 |
# server_max_window_bits |
| 424 |
|
| 425 |
# Req. Resp. Result |
| 426 |
# ------ ------ -------------------------------------------------- |
| 427 |
# None None None |
| 428 |
# None 8≤M≤15 M |
| 429 |
# 8≤N≤15 None Error! |
| 430 |
# 8≤N≤15 8≤M≤N M |
| 431 |
# 8≤N≤15 N<M≤15 Error! |
| 432 |
|
| 433 |
if self.server_max_window_bits is None: |
| 434 |
pass |
| 435 |
|
| 436 |
else: |
| 437 |
if server_max_window_bits is None: |
| 438 |
raise NegotiationError("expected server_max_window_bits") |
| 439 |
elif server_max_window_bits > self.server_max_window_bits: |
| 440 |
raise NegotiationError("unsupported server_max_window_bits") |
| 441 |
|
| 442 |
# client_max_window_bits |
| 443 |
|
| 444 |
# Req. Resp. Result |
| 445 |
# ------ ------ -------------------------------------------------- |
| 446 |
# None None None |
| 447 |
# None 8≤M≤15 Error! |
| 448 |
# True None None |
| 449 |
# True 8≤M≤15 M |
| 450 |
# 8≤N≤15 None N - must change value |
| 451 |
# 8≤N≤15 8≤M≤N M |
| 452 |
# 8≤N≤15 N<M≤15 Error! |
| 453 |
|
| 454 |
if self.client_max_window_bits is None: |
| 455 |
if client_max_window_bits is not None: |
| 456 |
raise NegotiationError("unexpected client_max_window_bits") |
| 457 |
|
| 458 |
elif self.client_max_window_bits is True: |
| 459 |
pass |
| 460 |
|
| 461 |
else: |
| 462 |
if client_max_window_bits is None: |
| 463 |
client_max_window_bits = self.client_max_window_bits |
| 464 |
elif client_max_window_bits > self.client_max_window_bits: |
| 465 |
raise NegotiationError("unsupported client_max_window_bits") |
| 466 |
|
| 467 |
return PerMessageDeflate( |
| 468 |
server_no_context_takeover, # remote_no_context_takeover |
| 469 |
client_no_context_takeover, # local_no_context_takeover |
| 470 |
server_max_window_bits or 15, # remote_max_window_bits |
| 471 |
client_max_window_bits or 15, # local_max_window_bits |
| 472 |
self.compress_settings, |
| 473 |
) |
| 474 |
|
| 475 |
|
| 476 |
def enable_client_permessage_deflate( |
| 477 |
extensions: Sequence[ClientExtensionFactory] | None, |
| 478 |
) -> Sequence[ClientExtensionFactory]: |
| 479 |
""" |
| 480 |
Enable Per-Message Deflate with default settings in client extensions. |
| 481 |
|
| 482 |
If the extension is already present, perhaps with non-default settings, |
| 483 |
the configuration isn't changed. |
| 484 |
|
| 485 |
""" |
| 486 |
if extensions is None: |
| 487 |
extensions = [] |
| 488 |
if not any( |
| 489 |
extension_factory.name == ClientPerMessageDeflateFactory.name |
| 490 |
for extension_factory in extensions |
| 491 |
): |
| 492 |
extensions = list(extensions) + [ |
| 493 |
ClientPerMessageDeflateFactory( |
| 494 |
compress_settings={"memLevel": 5}, |
| 495 |
) |
| 496 |
] |
| 497 |
return extensions |
| 498 |
|
| 499 |
|
| 500 |
class ServerPerMessageDeflateFactory(ServerExtensionFactory): |
| 501 |
""" |
| 502 |
Server-side extension factory for the Per-Message Deflate extension. |
| 503 |
|
| 504 |
Parameters behave as described in `section 7.1 of RFC 7692`_. |
| 505 |
|
| 506 |
.. _section 7.1 of RFC 7692: https://datatracker.ietf.org/doc/html/rfc7692#section-7.1 |
| 507 |
|
| 508 |
Set them to :obj:`True` to include them in the negotiation offer without a |
| 509 |
value or to an integer value to include them with this value. |
| 510 |
|
| 511 |
Args: |
| 512 |
server_no_context_takeover: Prevent server from using context takeover. |
| 513 |
client_no_context_takeover: Prevent client from using context takeover. |
| 514 |
server_max_window_bits: Maximum size of the server's LZ77 sliding window |
| 515 |
in bits, between 8 and 15. |
| 516 |
client_max_window_bits: Maximum size of the client's LZ77 sliding window |
| 517 |
in bits, between 8 and 15. |
| 518 |
compress_settings: Additional keyword arguments for :func:`zlib.compressobj`, |
| 519 |
excluding ``wbits``. |
| 520 |
require_client_max_window_bits: Do not enable compression at all if |
| 521 |
client doesn't advertise support for ``client_max_window_bits``; |
| 522 |
the default behavior is to enable compression without enforcing |
| 523 |
``client_max_window_bits``. |
| 524 |
|
| 525 |
""" |
| 526 |
|
| 527 |
name = ExtensionName("permessage-deflate") |
| 528 |
|
| 529 |
def __init__( |
| 530 |
self, |
| 531 |
server_no_context_takeover: bool = False, |
| 532 |
client_no_context_takeover: bool = False, |
| 533 |
server_max_window_bits: int | None = None, |
| 534 |
client_max_window_bits: int | None = None, |
| 535 |
compress_settings: dict[str, Any] | None = None, |
| 536 |
require_client_max_window_bits: bool = False, |
| 537 |
) -> None: |
| 538 |
""" |
| 539 |
Configure the Per-Message Deflate extension factory. |
| 540 |
|
| 541 |
""" |
| 542 |
if not (server_max_window_bits is None or 8 <= server_max_window_bits <= 15): |
| 543 |
raise ValueError("server_max_window_bits must be between 8 and 15") |
| 544 |
if not (client_max_window_bits is None or 8 <= client_max_window_bits <= 15): |
| 545 |
raise ValueError("client_max_window_bits must be between 8 and 15") |
| 546 |
if compress_settings is not None and "wbits" in compress_settings: |
| 547 |
raise ValueError( |
| 548 |
"compress_settings must not include wbits, " |
| 549 |
"set server_max_window_bits instead" |
| 550 |
) |
| 551 |
if client_max_window_bits is None and require_client_max_window_bits: |
| 552 |
raise ValueError( |
| 553 |
"require_client_max_window_bits is enabled, " |
| 554 |
"but client_max_window_bits isn't configured" |
| 555 |
) |
| 556 |
|
| 557 |
self.server_no_context_takeover = server_no_context_takeover |
| 558 |
self.client_no_context_takeover = client_no_context_takeover |
| 559 |
self.server_max_window_bits = server_max_window_bits |
| 560 |
self.client_max_window_bits = client_max_window_bits |
| 561 |
self.compress_settings = compress_settings |
| 562 |
self.require_client_max_window_bits = require_client_max_window_bits |
| 563 |
|
| 564 |
def process_request_params( |
| 565 |
self, |
| 566 |
params: Sequence[ExtensionParameter], |
| 567 |
accepted_extensions: Sequence[Extension], |
| 568 |
) -> tuple[list[ExtensionParameter], PerMessageDeflate]: |
| 569 |
""" |
| 570 |
Process request parameters. |
| 571 |
|
| 572 |
Return response params and an extension instance. |
| 573 |
|
| 574 |
""" |
| 575 |
if any(other.name == self.name for other in accepted_extensions): |
| 576 |
raise NegotiationError(f"skipped duplicate {self.name}") |
| 577 |
|
| 578 |
# Load request parameters in local variables. |
| 579 |
( |
| 580 |
server_no_context_takeover, |
| 581 |
client_no_context_takeover, |
| 582 |
server_max_window_bits, |
| 583 |
client_max_window_bits, |
| 584 |
) = _extract_parameters(params, is_server=True) |
| 585 |
|
| 586 |
# Configuration parameters are available in instance variables. |
| 587 |
|
| 588 |
# After comparing the request and the configuration, the response must |
| 589 |
# be available in the local variables. |
| 590 |
|
| 591 |
# server_no_context_takeover |
| 592 |
# |
| 593 |
# Config Req. Resp. |
| 594 |
# ------ ------ -------------------------------------------------- |
| 595 |
# False False False |
| 596 |
# False True True |
| 597 |
# True False True - must change value to True |
| 598 |
# True True True |
| 599 |
|
| 600 |
if self.server_no_context_takeover: |
| 601 |
if not server_no_context_takeover: |
| 602 |
server_no_context_takeover = True |
| 603 |
|
| 604 |
# client_no_context_takeover |
| 605 |
# |
| 606 |
# Config Req. Resp. |
| 607 |
# ------ ------ -------------------------------------------------- |
| 608 |
# False False False |
| 609 |
# False True True (or False) |
| 610 |
# True False True - must change value to True |
| 611 |
# True True True (or False) |
| 612 |
|
| 613 |
if self.client_no_context_takeover: |
| 614 |
if not client_no_context_takeover: |
| 615 |
client_no_context_takeover = True |
| 616 |
|
| 617 |
# server_max_window_bits |
| 618 |
|
| 619 |
# Config Req. Resp. |
| 620 |
# ------ ------ -------------------------------------------------- |
| 621 |
# None None None |
| 622 |
# None 8≤M≤15 M |
| 623 |
# 8≤N≤15 None N - must change value |
| 624 |
# 8≤N≤15 8≤M≤N M |
| 625 |
# 8≤N≤15 N<M≤15 N - must change value |
| 626 |
|
| 627 |
if self.server_max_window_bits is None: |
| 628 |
pass |
| 629 |
|
| 630 |
else: |
| 631 |
if server_max_window_bits is None: |
| 632 |
server_max_window_bits = self.server_max_window_bits |
| 633 |
elif server_max_window_bits > self.server_max_window_bits: |
| 634 |
server_max_window_bits = self.server_max_window_bits |
| 635 |
|
| 636 |
# client_max_window_bits |
| 637 |
|
| 638 |
# Config Req. Resp. |
| 639 |
# ------ ------ -------------------------------------------------- |
| 640 |
# None None None |
| 641 |
# None True None - must change value |
| 642 |
# None 8≤M≤15 M (or None) |
| 643 |
# 8≤N≤15 None None or Error! |
| 644 |
# 8≤N≤15 True N - must change value |
| 645 |
# 8≤N≤15 8≤M≤N M (or None) |
| 646 |
# 8≤N≤15 N<M≤15 N |
| 647 |
|
| 648 |
if self.client_max_window_bits is None: |
| 649 |
if client_max_window_bits is True: |
| 650 |
client_max_window_bits = self.client_max_window_bits |
| 651 |
|
| 652 |
else: |
| 653 |
if client_max_window_bits is None: |
| 654 |
if self.require_client_max_window_bits: |
| 655 |
raise NegotiationError("required client_max_window_bits") |
| 656 |
elif client_max_window_bits is True: |
| 657 |
client_max_window_bits = self.client_max_window_bits |
| 658 |
elif self.client_max_window_bits < client_max_window_bits: |
| 659 |
client_max_window_bits = self.client_max_window_bits |
| 660 |
|
| 661 |
return ( |
| 662 |
_build_parameters( |
| 663 |
server_no_context_takeover, |
| 664 |
client_no_context_takeover, |
| 665 |
server_max_window_bits, |
| 666 |
client_max_window_bits, |
| 667 |
), |
| 668 |
PerMessageDeflate( |
| 669 |
client_no_context_takeover, # remote_no_context_takeover |
| 670 |
server_no_context_takeover, # local_no_context_takeover |
| 671 |
client_max_window_bits or 15, # remote_max_window_bits |
| 672 |
server_max_window_bits or 15, # local_max_window_bits |
| 673 |
self.compress_settings, |
| 674 |
), |
| 675 |
) |
| 676 |
|
| 677 |
|
| 678 |
def enable_server_permessage_deflate( |
| 679 |
extensions: Sequence[ServerExtensionFactory] | None, |
| 680 |
) -> Sequence[ServerExtensionFactory]: |
| 681 |
""" |
| 682 |
Enable Per-Message Deflate with default settings in server extensions. |
| 683 |
|
| 684 |
If the extension is already present, perhaps with non-default settings, |
| 685 |
the configuration isn't changed. |
| 686 |
|
| 687 |
""" |
| 688 |
if extensions is None: |
| 689 |
extensions = [] |
| 690 |
if not any( |
| 691 |
ext_factory.name == ServerPerMessageDeflateFactory.name |
| 692 |
for ext_factory in extensions |
| 693 |
): |
| 694 |
extensions = list(extensions) + [ |
| 695 |
ServerPerMessageDeflateFactory( |
| 696 |
server_max_window_bits=12, |
| 697 |
client_max_window_bits=12, |
| 698 |
compress_settings={"memLevel": 5}, |
| 699 |
) |
| 700 |
] |
| 701 |
return extensions |