-
Notifications
You must be signed in to change notification settings - Fork 418
Open
Description
Currently vcrpy doesn't decompress response in Brotli compression format, but Brotli is well supported in HTTP clients like requests, httpx.
Lines 127 to 162 in c79a06f
| def decode_response(response): | |
| """ | |
| If the response is compressed with gzip or deflate: | |
| 1. decompress the response body | |
| 2. delete the content-encoding header | |
| 3. update content-length header to decompressed length | |
| """ | |
| def is_compressed(headers): | |
| encoding = headers.get("content-encoding", []) | |
| return encoding and encoding[0] in ("gzip", "deflate") | |
| def decompress_body(body, encoding): | |
| """Returns decompressed body according to encoding using zlib. | |
| to (de-)compress gzip format, use wbits = zlib.MAX_WBITS | 16 | |
| """ | |
| if encoding == "gzip": | |
| return zlib.decompress(body, zlib.MAX_WBITS | 16) | |
| else: # encoding == 'deflate' | |
| return zlib.decompress(body) | |
| # Deepcopy here in case `headers` contain objects that could | |
| # be mutated by a shallow copy and corrupt the real response. | |
| response = copy.deepcopy(response) | |
| headers = CaseInsensitiveDict(response["headers"]) | |
| if is_compressed(headers): | |
| encoding = headers["content-encoding"][0] | |
| headers["content-encoding"].remove(encoding) | |
| if not headers["content-encoding"]: | |
| del headers["content-encoding"] | |
| new_body = decompress_body(response["body"]["string"], encoding) | |
| response["body"]["string"] = new_body | |
| headers["content-length"] = [str(len(new_body))] | |
| response["headers"] = dict(headers) | |
| return response |
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding#directives
kovalevvlad
Metadata
Metadata
Assignees
Labels
No labels