Skip to content

Commit

Permalink
Fix invalid invocations of errors.LineTooLong.
Browse files Browse the repository at this point in the history
  • Loading branch information
filmor committed Oct 26, 2016
1 parent 973d8a8 commit f5cab5b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
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

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

0 comments on commit f5cab5b

Please sign in to comment.