diff --git a/proxy/http/parser/parser.py b/proxy/http/parser/parser.py index f81a100b22..151e037f2a 100644 --- a/proxy/http/parser/parser.py +++ b/proxy/http/parser/parser.py @@ -27,7 +27,6 @@ from .chunk import ChunkParser, chunkParserStates from .types import httpParserTypes, httpParserStates - flags.add_argument( '--enable-proxy-protocol', action='store_true', @@ -305,16 +304,27 @@ def _process_line_and_headers(self, raw: bytes) -> Tuple[bool, bytes]: def _process_line(self, raw: bytes) -> None: if self.type == httpParserTypes.REQUEST_PARSER: + # Ref: + # https://datatracker.ietf.org/doc/html/rfc2616#section-5.1 + # https://greenbytes.de/tech/webdav/rfc7230.html#request.line + # https://greenbytes.de/tech/webdav/rfc7231.html#methods + # http://www.iana.org/assignments/http-methods/http-methods.xhtml if self.protocol is not None and self.protocol.version is None: # We expect to receive entire proxy protocol v1 line # in one network read and don't expect partial packets self.protocol.parse(raw) else: line = raw.split(WHITESPACE) - self.method = line[0].upper() - self.set_url(line[1]) - self.version = line[2] - self.state = httpParserStates.LINE_RCVD + if len(line) == 3: + self.method = line[0].upper() + self.set_url(line[1]) + self.version = line[2] + self.state = httpParserStates.LINE_RCVD + else: + # raise exception + # TODO, it would be better to use raise HttpProtocolException, + # but we should solve circular import problem first + raise NotImplementedError('Invalid request line') else: line = raw.split(WHITESPACE) self.version = line[0] diff --git a/tests/http/test_http_parser.py b/tests/http/test_http_parser.py index 11aa5c47b7..4ef44123c2 100644 --- a/tests/http/test_http_parser.py +++ b/tests/http/test_http_parser.py @@ -23,6 +23,15 @@ class TestHttpParser(unittest.TestCase): def setUp(self) -> None: self.parser = HttpParser(httpParserTypes.REQUEST_PARSER) + def test_issue_127(self) -> None: + with self.assertRaises(NotImplementedError): + self.parser.parse(CRLF) + + with self.assertRaises(NotImplementedError): + raw = b'qwqrqw!@!#@!#ad adfad\r\n' + while True: + self.parser.parse(raw) + def test_issue_398(self) -> None: p = HttpParser(httpParserTypes.RESPONSE_PARSER) p.parse(HTTP_1_0 + b' 200 OK' + CRLF)