Merge pull request #4741 from Mattwmaster58/main

case insensitive encoding
This commit is contained in:
Maximilian Hils 2021-08-10 08:18:56 +02:00 committed by GitHub
commit ab6f1ebb44
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 4 deletions

View File

@ -1,5 +1,9 @@
# Release History # Release History
## Unreleased: mitmproxy next
* fix some responses not being decoded properly if the encoding was uppercase #4735 (@Mattwmaster58)
## 4 August 2021: mitmproxy 7.0.2 ## 4 August 2021: mitmproxy 7.0.2
* Fix a WebSocket crash introduced in 7.0.1 (@mhils) * Fix a WebSocket crash introduced in 7.0.1 (@mhils)

View File

@ -52,6 +52,7 @@ def decode(
""" """
if encoded is None: if encoded is None:
return None return None
encoding = encoding.lower()
global _cache global _cache
cached = ( cached = (
@ -67,7 +68,7 @@ def decode(
decoded = custom_decode[encoding](encoded) decoded = custom_decode[encoding](encoded)
except KeyError: except KeyError:
decoded = codecs.decode(encoded, encoding, errors) # type: ignore decoded = codecs.decode(encoded, encoding, errors) # type: ignore
if encoding in ("gzip", "deflate", "br", "zstd"): if encoding in ("gzip", "deflate", "deflateraw", "br", "zstd"):
_cache = CachedDecode(encoded, encoding, errors, decoded) _cache = CachedDecode(encoded, encoding, errors, decoded)
return decoded return decoded
except TypeError: except TypeError:
@ -108,6 +109,7 @@ def encode(decoded: Union[None, str, bytes], encoding, errors='strict') -> Union
""" """
if decoded is None: if decoded is None:
return None return None
encoding = encoding.lower()
global _cache global _cache
cached = ( cached = (
@ -123,7 +125,7 @@ def encode(decoded: Union[None, str, bytes], encoding, errors='strict') -> Union
encoded = custom_encode[encoding](decoded) encoded = custom_encode[encoding](decoded)
except KeyError: except KeyError:
encoded = codecs.encode(decoded, encoding, errors) # type: ignore encoded = codecs.encode(decoded, encoding, errors) # type: ignore
if encoding in ("gzip", "deflate", "br", "zstd"): if encoding in ("gzip", "deflate", "deflateraw", "br", "zstd"):
_cache = CachedDecode(encoded, encoding, errors, decoded) _cache = CachedDecode(encoded, encoding, errors, decoded)
return encoded return encoded
except TypeError: except TypeError:
@ -216,7 +218,7 @@ custom_decode = {
"identity": identity, "identity": identity,
"gzip": decode_gzip, "gzip": decode_gzip,
"deflate": decode_deflate, "deflate": decode_deflate,
"deflateRaw": decode_deflate, "deflateraw": decode_deflate,
"br": decode_brotli, "br": decode_brotli,
"zstd": decode_zstd, "zstd": decode_zstd,
} }
@ -225,7 +227,7 @@ custom_encode = {
"identity": identity, "identity": identity,
"gzip": encode_gzip, "gzip": encode_gzip,
"deflate": encode_deflate, "deflate": encode_deflate,
"deflateRaw": encode_deflate, "deflateraw": encode_deflate,
"br": encode_brotli, "br": encode_brotli,
"zstd": encode_zstd, "zstd": encode_zstd,
} }

View File

@ -17,6 +17,7 @@ def test_identity(encoder):
@pytest.mark.parametrize("encoder", [ @pytest.mark.parametrize("encoder", [
'gzip', 'gzip',
'GZIP',
'br', 'br',
'deflate', 'deflate',
'zstd', 'zstd',