@@ -32,9 +32,14 @@ def __init__(self, stream, endpoint):
3232 def handle_requests (self ):
3333 try :
3434 while not self ._parser .is_closed ():
35+ if not self ._parser .wait_ready ():
36+ # Wait until we have read data, or return
37+ # if the stream closes.
38+ return
39+ # Read the initial part of the request,
40+ # and setup a stream for reading the body.
3541 method , url , headers = self ._recv_head ()
3642 stream = HTTPStream (self ._recv_body , self ._reset )
37- # TODO: Handle endpoint exceptions
3843 with Request (method , url , headers = headers , content = stream ) as request :
3944 try :
4045 response = self ._endpoint (request )
@@ -50,7 +55,10 @@ def handle_requests(self):
5055 self ._send_head (response )
5156 self ._send_body (response )
5257 if self ._parser .is_keepalive ():
58+ # If the client hasn't read the request body to
59+ # completion, then do that here.
5360 stream .read ()
61+ # Either revert to idle, or close the connection.
5462 self ._reset ()
5563 except Exception :
5664 logger .error ("Internal Server Error" , exc_info = True )
@@ -102,10 +110,7 @@ def __init__(self, host, port):
102110
103111 def wait (self ):
104112 while (True ):
105- try :
106- sleep (1 )
107- except KeyboardInterrupt :
108- break
113+ sleep (1 )
109114
110115
111116@contextlib .contextmanager
0 commit comments