diff --git a/CHANGES.rst b/CHANGES.rst index ae1fb8e34a2..38a37fe7e08 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -49,7 +49,7 @@ CHANGES - Added option on `StaticRoute` to follow symlinks #1299 -- +- Fix invalid invocations of errors.LineTooLong #1335 - diff --git a/aiohttp/errors.py b/aiohttp/errors.py index 85c6eed7ba9..79e86e94da4 100644 --- a/aiohttp/errors.py +++ b/aiohttp/errors.py @@ -134,7 +134,7 @@ class LineTooLong(BadHttpMessage): def __init__(self, line, limit='Unknown'): super().__init__( - "got more than %s bytes when reading %s" % (limit, line)) + "Got more than %s bytes when reading %s." % (limit, line)) class InvalidHeader(BadHttpMessage): diff --git a/aiohttp/protocol.py b/aiohttp/protocol.py index 457f0cf5897..82ee78c1d45 100644 --- a/aiohttp/protocol.py +++ b/aiohttp/protocol.py @@ -102,7 +102,9 @@ def parse_headers(self, lines): header_length += len(line) if header_length > self.max_field_size: raise errors.LineTooLong( - 'limit request headers fields size') + 'request header field {}'.format( + bname.decode("utf8", "xmlcharrefreplace")), + self.max_field_size) bvalue.append(line) # next line @@ -113,7 +115,9 @@ def parse_headers(self, lines): else: if header_length > self.max_field_size: raise errors.LineTooLong( - 'limit request headers fields size') + 'request header field {}'.format( + bname.decode("utf8", "xmlcharrefreplace")), + self.max_field_size) bvalue = bvalue.strip() @@ -173,7 +177,7 @@ def __call__(self, out, buf): raw_data = yield from buf.readuntil( b'\r\n\r\n', self.max_headers) except errors.LineLimitExceededParserError as exc: - raise errors.LineTooLong(exc.limit) from None + raise errors.LineTooLong('request header', exc.limit) from None lines = raw_data.split(b'\r\n') @@ -227,7 +231,7 @@ def __call__(self, out, buf): raw_data = yield from buf.readuntil( b'\r\n\r\n', self.max_line_size + self.max_headers) except errors.LineLimitExceededParserError as exc: - raise errors.LineTooLong(exc.limit) from None + raise errors.LineTooLong('response header', exc.limit) from None lines = raw_data.split(b'\r\n') diff --git a/tests/test_http_parser.py b/tests/test_http_parser.py index 22766b01d41..6ff11fc95e9 100644 --- a/tests/test_http_parser.py +++ b/tests/test_http_parser.py @@ -84,14 +84,21 @@ def test_max_field_size(self): parser = protocol.HttpParser(8190, 32768, 5) parser.parse_headers( [b'', b'test: line data data\r\n', b'data\r\n', b'\r\n']) - self.assertIn("limit request headers fields size", str(cm.exception)) + self.assertIn("request header field TEST", str(cm.exception)) def test_max_continuation_headers_size(self): with self.assertRaises(errors.LineTooLong) as cm: parser = protocol.HttpParser(8190, 32768, 5) parser.parse_headers([b'', b'test: line\r\n', b' test\r\n', b'\r\n']) - self.assertIn("limit request headers fields size", str(cm.exception)) + self.assertIn("request header field TEST", str(cm.exception)) + + def test_max_header_size(self): + with self.assertRaises(errors.LineTooLong) as cm: + parser = protocol.HttpParser(5, 5, 5) + parser.parse_headers( + [b'', b'test: line data data\r\n', b'data\r\n', b'\r\n']) + self.assertIn("request header", str(cm.exception)) def test_invalid_header(self): with self.assertRaisesRegex(