@@ -83,16 +83,15 @@ class HTTPParser:
8383 client.complete()
8484 client.close()
8585 """
86- def __init__ (self , writer : Stream , reader : Stream , mode : Mode = Mode .CLIENT ) -> None :
87- self .writer = writer
88- self .reader = reader
89- self .parser = ReadAheadParser (reader )
90- self .mode = mode
91-
92- # Track client and server state...
93- if mode == Mode .CLIENT :
94- self .send_state = State .SEND_METHOD_LINE
95- self .recv_state = State .WAIT
86+ def __init__ (self , stream : Stream , mode : str ) -> None :
87+ self .stream = stream
88+ self .parser = ReadAheadParser (stream )
89+ self .mode = {'CLIENT' : Mode .CLIENT , 'SERVER' : Mode .SERVER }[mode ]
90+
91+ # Track state...
92+ if self .mode == Mode .CLIENT :
93+ self .send_state : State = State .SEND_METHOD_LINE
94+ self .recv_state : State = State .WAIT
9695 else :
9796 self .recv_state = State .RECV_METHOD_LINE
9897 self .send_state = State .WAIT
@@ -119,14 +118,14 @@ async def send_method_line(self, method: bytes, target: bytes, protocol: bytes)
119118 Sending state will switch to SEND_HEADERS state.
120119 """
121120 if self .send_state != State .SEND_METHOD_LINE :
122- msg = f"Called 'send_method_line' in invalid state { self .description () } "
121+ msg = f"Called 'send_method_line' in invalid state { self .send_state } "
123122 raise ProtocolError (msg )
124123
125124 # Send initial request line, eg. "GET / HTTP/1.1"
126125 if protocol != b'HTTP/1.1' :
127126 raise ProtocolError ("Sent unsupported protocol version" )
128127 data = b" " .join ([method , target , protocol ]) + b"\r \n "
129- await self .writer .write (data )
128+ await self .stream .write (data )
130129
131130 self .send_state = State .SEND_HEADERS
132131 self .recv_state = State .RECV_STATUS_LINE
@@ -140,15 +139,15 @@ async def send_status_line(self, protocol: bytes, status_code: int, reason: byte
140139 Sending state will switch to SEND_HEADERS state.
141140 """
142141 if self .send_state != State .SEND_STATUS_LINE :
143- msg = f"Called 'send_status_line' in invalid state { self .description () } "
142+ msg = f"Called 'send_status_line' in invalid state { self .send_state } "
144143 raise ProtocolError (msg )
145144
146145 # Send initial request line, eg. "GET / HTTP/1.1"
147146 if protocol != b'HTTP/1.1' :
148147 raise ProtocolError ("Sent unsupported protocol version" )
149148 status_code_bytes = str (status_code ).encode ('ascii' )
150149 data = b" " .join ([protocol , status_code_bytes , reason ]) + b"\r \n "
151- await self .writer .write (data )
150+ await self .stream .write (data )
152151
153152 self .send_state = State .SEND_HEADERS
154153
@@ -161,7 +160,7 @@ async def send_headers(self, headers: list[tuple[bytes, bytes]]) -> None:
161160 Sending state will switch to SEND_BODY state.
162161 """
163162 if self .send_state != State .SEND_HEADERS :
164- msg = f"Called 'send_headers' in invalid state { self .description () } "
163+ msg = f"Called 'send_headers' in invalid state { self .send_state } "
165164 raise ProtocolError (msg )
166165
167166 # Update header state
@@ -187,7 +186,7 @@ async def send_headers(self, headers: list[tuple[bytes, bytes]]) -> None:
187186 # Send request headers
188187 lines = [name + b": " + value + b"\r \n " for name , value in headers ]
189188 data = b"" .join (lines ) + b"\r \n "
190- await self .writer .write (data )
189+ await self .stream .write (data )
191190
192191 self .send_state = State .SEND_BODY
193192
@@ -200,14 +199,14 @@ async def send_body(self, body: bytes) -> None:
200199 Sending state will switch to DONE.
201200 """
202201 if self .send_state != State .SEND_BODY :
203- msg = f"Called 'send_body' in invalid state { self .description () } "
202+ msg = f"Called 'send_body' in invalid state { self .send_state } "
204203 raise ProtocolError (msg )
205204
206205 if self .send_content_length is None :
207206 # Transfer-Encoding: chunked
208207 self .send_seen_length += len (body )
209208 marker = f'{ len (body ):x} \r \n ' .encode ('ascii' )
210- await self .writer .write (marker + body + b'\r \n ' )
209+ await self .stream .write (marker + body + b'\r \n ' )
211210
212211 else :
213212 # Content-Length: xxx
@@ -219,7 +218,7 @@ async def send_body(self, body: bytes) -> None:
219218 msg = 'Not enough data sent for declared Content-Length'
220219 raise ProtocolError (msg )
221220 if body :
222- await self .writer .write (body )
221+ await self .stream .write (body )
223222
224223 if body == b'' :
225224 # Handle body close
@@ -234,7 +233,7 @@ async def recv_method_line(self) -> tuple[bytes, bytes, bytes]:
234233 Receive state will switch to RECV_HEADERS.
235234 """
236235 if self .recv_state != State .RECV_METHOD_LINE :
237- msg = f"Called 'recv_method_line' in invalid state { self .description () } "
236+ msg = f"Called 'recv_method_line' in invalid state { self .recv_state } "
238237 raise ProtocolError (msg )
239238
240239 # Read initial response line, eg. "GET / HTTP/1.1"
@@ -257,7 +256,7 @@ async def recv_status_line(self) -> tuple[bytes, int, bytes]:
257256 Receive state will switch to RECV_HEADERS.
258257 """
259258 if self .recv_state != State .RECV_STATUS_LINE :
260- msg = f"Called 'recv_status_line' in invalid state { self .description () } "
259+ msg = f"Called 'recv_status_line' in invalid state { self .recv_state } "
261260 raise ProtocolError (msg )
262261
263262 # Read initial response line, eg. "HTTP/1.1 200 OK"
@@ -290,7 +289,7 @@ async def recv_headers(self) -> list[tuple[bytes, bytes]]:
290289 Receive state will revert to RECV_STATUS_CODE for interim 1xx responses.
291290 """
292291 if self .recv_state != State .RECV_HEADERS :
293- msg = f"Called 'recv_headers' in invalid state { self .description () } "
292+ msg = f"Called 'recv_headers' in invalid state { self .recv_state } "
294293 raise ProtocolError (msg )
295294
296295 # Read response headers
@@ -340,7 +339,7 @@ async def recv_body(self) -> bytes:
340339 The server will switch to DONE.
341340 """
342341 if self .recv_state != State .RECV_BODY :
343- msg = f"Called 'recv_body' in invalid state { self .description () } "
342+ msg = f"Called 'recv_body' in invalid state { self .recv_state } "
344343 raise ProtocolError (msg )
345344
346345 if self .recv_content_length is None :
@@ -390,6 +389,7 @@ async def complete(self):
390389 else :
391390 self .recv_state = State .RECV_METHOD_LINE
392391 self .send_state = State .WAIT
392+
393393 self .send_content_length = 0
394394 self .recv_content_length = 0
395395 self .send_seen_length = 0
@@ -402,7 +402,7 @@ async def close(self):
402402 if self .send_state != State .CLOSED :
403403 self .send_state = State .CLOSED
404404 self .recv_state = State .CLOSED
405- await self .writer .close ()
405+ await self .stream .close ()
406406
407407 def is_idle (self ) -> bool :
408408 return (
0 commit comments