Last active
November 22, 2019 07:55
-
-
Save momocow/5c6766aaa66ef9db9cd26ff762d26239 to your computer and use it in GitHub Desktop.
Monkey patch for h11. Although HTTP uses CRLF as line-breaks in headers, some implementations may still use LF. The script provides CRLF insensitive patch for ReceiveBuffer from h11.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from h11._receivebuffer import ReceiveBuffer | |
def monkey_patch(): | |
def _maybe_extract_lines_crlf_insensitive(self): | |
if self._data[self._start : self._start + 2] == b"\r\n": | |
self._start += 2 | |
return [] | |
elif self._data[self._start : self._start + 1] == b"\n": | |
self._start += 1 | |
return [] | |
else: | |
data = self.maybe_extract_until_next(b"\r\n\r\n") \ | |
or self.maybe_extract_until_next(b"\n\n") | |
if data is None: | |
# header not finish | |
return None | |
lines = [] | |
lf_lines = data.split(b"\r\n") | |
for lf_line in lf_lines: | |
lines += lf_line.split(b"\n") | |
assert lines[-2] == lines[-1] == b"" | |
del lines[-2:] | |
return lines | |
_original_ReceiveBuffer_maybe_extract_lines = ReceiveBuffer.maybe_extract_lines | |
ReceiveBuffer.maybe_extract_lines = _maybe_extract_lines_crlf_insensitive | |
def _recover(): | |
ReceiveBuffer.maybe_extract_lines = _original_ReceiveBuffer_maybe_extract_lines | |
return _recover |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The following header may cause error in h11 when trying to decode it since there are CRLFs and LFs.
LocalProtocolError
orRemoteProtocolError
may be raised with the messagemalformed data
.Patching the library before any HTTP requests fixes the error.