Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix invalid invocations of errors.LineTooLong. #1335

Merged
merged 1 commit into from
Nov 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ CHANGES

- Added option on `StaticRoute` to follow symlinks #1299

-
- Fix invalid invocations of errors.LineTooLong #1335

-

Expand Down
2 changes: 1 addition & 1 deletion aiohttp/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
12 changes: 8 additions & 4 deletions aiohttp/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()

Expand Down Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks inconvenient: LineTooLong above is raised with single argument (formated string) and with two args here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not, and the call here is wrong. Check out the constructor of LineTooLong (didn't change that one), it expects a "reason" and a limit that was exceeded. The way this was passed before resulted in a broken error message like "400 got more than Unknown bytes when reading 32768" when it should have read "400 got more than 32768 when reading request header".

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true, missed that one.


lines = raw_data.split(b'\r\n')

Expand Down Expand Up @@ -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')

Expand Down
11 changes: 9 additions & 2 deletions tests/test_http_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down