1717
1818log = 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
3121class 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 )
0 commit comments