Skip to content

Commit 045dd8c

Browse files
author
Nikolay Kim
committed
refactor htpp message parsing
1 parent b521674 commit 045dd8c

8 files changed

Lines changed: 297 additions & 209 deletions

File tree

aiohttp/parsers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,10 @@ def __init__(self, *, loop=None, disconnect_error=RuntimeError, **kwargs):
311311

312312
self.transport = None
313313
self.writer = None
314+
self.buffer = ParserBuffer()
314315
self.reader = StreamParser(
315-
loop=loop, eof_exc_class=disconnect_error, **kwargs)
316+
buf=self.buffer, loop=loop,
317+
eof_exc_class=disconnect_error, **kwargs)
316318

317319
def is_connected(self):
318320
return self.transport is not None

aiohttp/protocol.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -161,16 +161,7 @@ class HttpRequestParser(HttpParser):
161161
Returns RawRequestMessage.
162162
"""
163163

164-
def __call__(self, out, buf):
165-
# read HTTP message (request line + headers)
166-
try:
167-
raw_data = yield from buf.readuntil(
168-
b'\r\n\r\n', self.max_headers)
169-
except errors.LineLimitExceededParserError as exc:
170-
raise errors.LineTooLong('request header', exc.limit) from None
171-
172-
lines = raw_data.split(b'\r\n')
173-
164+
def parse_message(self, lines):
174165
# request line
175166
line = lines[0].decode('utf-8', 'surrogateescape')
176167
try:
@@ -202,11 +193,21 @@ def __call__(self, out, buf):
202193
else: # HTTP 1.1 must ask to close.
203194
close = False
204195

205-
out.feed_data(
206-
RawRequestMessage(
207-
method, path, version, headers, raw_headers,
208-
close, compression, upgrade),
209-
len(raw_data))
196+
return RawRequestMessage(
197+
method, path, version, headers, raw_headers,
198+
close, compression, upgrade)
199+
200+
def __call__(self, out, buf):
201+
# read HTTP message (request line + headers)
202+
try:
203+
raw_data = yield from buf.readuntil(
204+
b'\r\n\r\n', self.max_headers)
205+
except errors.LineLimitExceededParserError as exc:
206+
raise errors.LineTooLong('request header', exc.limit) from None
207+
208+
lines = raw_data.split(b'\r\n')
209+
210+
out.feed_data(self.parse_message(lines), len(raw_data))
210211
out.feed_eof()
211212

212213

0 commit comments

Comments
 (0)