Skip to content
This repository was archived by the owner on Jan 13, 2021. It is now read-only.

Commit 97b088a

Browse files
committed
Improved HTTP/1.1 implementation.
1 parent 01d412c commit 97b088a

File tree

2 files changed

+8
-24
lines changed

2 files changed

+8
-24
lines changed

hyper/http11/connection.py

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,6 @@
1717

1818
log = logging.getLogger(__name__)
1919

20-
# This regular expression provides a fairly generous parsing of a HTTP status
21-
# line. It matches any amount of leading LWS, followed by a three-digit status
22-
# code, followed by LWS, followed by a reason phrase (allowing only space
23-
# separators), followed by the version (must be HTTP/1.1).
24-
#
25-
# For now this is a 'good enough' way to parse the status line. We may want a
26-
# dedicated parsing implementation later on, though it'd have to be faster than
27-
# this regex to be worthwhile.
28-
STATUS_LINE_REGEX = re.compile(rb'[ \t]*(?P<code>\d{3})[ \t]+(?P<reason>[\S ]+)[ \t]+HTTP/1\.1[ \t]*\r?\n')
29-
3020

3121
class HTTP11Connection(object):
3222
"""
@@ -119,16 +109,10 @@ def get_response(self):
119109
"""
120110
headers = HTTPHeaderMap()
121111

122-
# First read the header line and 'parse' it. This particular part of
123-
# the response can safely be parsed by regular expression, so do that.
124-
status_line = self._sock.readline()
125-
match = STATUS_LINE_REGEX.match(status_line)
126-
if match is None:
127-
raise RuntimeError("Invalid status line")
128-
129-
code, reason = int(match.group('code')), match.group('reason')
130-
print(code)
131-
print(reason)
112+
# First read the header line and 'parse' it.
113+
status_line = self._sock.readline().tobytes().rstrip()
114+
version, code, reason = status_line.split(b' ', 2)
115+
code = int(code)
132116

133117
while True:
134118
line = self._sock.readline().tobytes()
@@ -139,4 +123,4 @@ def get_response(self):
139123
val = val.lstrip().rstrip(b'\r\n')
140124
headers[name] = val
141125

142-
return HTTP11Response(headers, self._sock)
126+
return HTTP11Response(code, reason, headers, self._sock)

hyper/http11/response.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ class HTTP11Response(object):
5757
provides access to the response headers and the entity body. The response
5858
is an iterable object and can be used in a with statement.
5959
"""
60-
def __init__(self, headers, sock):
60+
def __init__(self, code, reason, headers, sock):
6161
#: The reason phrase returned by the server.
62-
self.reason = ''
62+
self.reason = reason
6363

6464
#: The status code returned by the server.
65-
self.status = 0
65+
self.status = code
6666

6767
#: The response headers. These are determined upon creation, assigned
6868
#: once, and never assigned again.

0 commit comments

Comments
 (0)