| 195 | if brotli is not None: |
| 196 | |
| 197 | class BrotliDecoder(ContentDecoder): |
| 198 | # Supports both 'brotlipy' and 'Brotli' packages |
| 199 | # since they share an import name. The top branches |
| 200 | # are for 'brotlipy' and bottom branches for 'Brotli' |
| 201 | def __init__(self) -> None: |
| 202 | self._obj = brotli.Decompressor() |
| 203 | if hasattr(self._obj, "decompress"): |
| 204 | setattr(self, "_decompress", self._obj.decompress) |
| 205 | else: |
| 206 | setattr(self, "_decompress", self._obj.process) |
| 207 | |
| 208 | # Requires Brotli >= 1.2.0 for `output_buffer_limit`. |
| 209 | def _decompress(self, data: bytes, output_buffer_limit: int = -1) -> bytes: |
| 210 | raise NotImplementedError() |
| 211 | |
| 212 | def decompress(self, data: bytes, max_length: int = -1) -> bytes: |
| 213 | try: |
| 214 | if max_length > 0: |
| 215 | return self._decompress(data, output_buffer_limit=max_length) |
| 216 | else: |
| 217 | return self._decompress(data) |
| 218 | except TypeError: |
| 219 | # Fallback for Brotli/brotlicffi/brotlipy versions without |
| 220 | # the `output_buffer_limit` parameter. |
| 221 | warnings.warn( |
| 222 | "Brotli >= 1.2.0 is required to prevent decompression bombs.", |
| 223 | DependencyWarning, |
| 224 | ) |
| 225 | return self._decompress(data) |
| 226 | |
| 227 | @property |
| 228 | def has_unconsumed_tail(self) -> bool: |
| 229 | try: |
| 230 | return not self._obj.can_accept_more_data() |
| 231 | except AttributeError: |
| 232 | return False |
| 233 | |
| 234 | def flush(self) -> bytes: |
| 235 | if hasattr(self._obj, "flush"): |
| 236 | return self._obj.flush() # type: ignore[no-any-return] |
| 237 | return b"" |
| 238 | |
| 239 | |
| 240 | try: |