| 778 | |
| 779 | |
| 780 | class _PerMessageDeflateDecompressor: |
| 781 | def __init__( |
| 782 | self, |
| 783 | persistent: bool, |
| 784 | max_wbits: Optional[int], |
| 785 | max_message_size: int, |
| 786 | compression_options: Optional[Dict[str, Any]] = None, |
| 787 | ) -> None: |
| 788 | self._max_message_size = max_message_size |
| 789 | if max_wbits is None: |
| 790 | max_wbits = zlib.MAX_WBITS |
| 791 | if not (8 <= max_wbits <= zlib.MAX_WBITS): |
| 792 | raise ValueError( |
| 793 | "Invalid max_wbits value %r; allowed range 8-%d", |
| 794 | max_wbits, |
| 795 | zlib.MAX_WBITS, |
| 796 | ) |
| 797 | self._max_wbits = max_wbits |
| 798 | if persistent: |
| 799 | self._decompressor = ( |
| 800 | self._create_decompressor() |
| 801 | ) # type: Optional[_Decompressor] |
| 802 | else: |
| 803 | self._decompressor = None |
| 804 | |
| 805 | def _create_decompressor(self) -> "_Decompressor": |
| 806 | return zlib.decompressobj(-self._max_wbits) |
| 807 | |
| 808 | def decompress(self, data: bytes) -> bytes: |
| 809 | decompressor = self._decompressor or self._create_decompressor() |
| 810 | result = decompressor.decompress( |
| 811 | data + b"\x00\x00\xff\xff", self._max_message_size |
| 812 | ) |
| 813 | if decompressor.unconsumed_tail: |
| 814 | raise _DecompressTooLargeError() |
| 815 | return result |
| 816 | |
| 817 | |
| 818 | class WebSocketProtocol13(WebSocketProtocol): |