11# This contains the main Connection class. Everything in h11 revolves around
22# this.
33
4- # Import all event types
5- from ._events import *
6- # Import all state sentinels
7- from ._state import *
8- # Import the internal things we need
9- from ._util import LocalProtocolError , RemoteProtocolError , make_sentinel
10- from ._state import ConnectionState , _SWITCH_UPGRADE , _SWITCH_CONNECT
11- from ._headers import (
12- get_comma_header , set_comma_header , has_expect_100_continue ,
13- )
14- from ._receivebuffer import ReceiveBuffer
4+ from ._events import * # Import all event types
5+ from ._headers import get_comma_header , has_expect_100_continue , set_comma_header
156from ._readers import READERS
7+ from ._receivebuffer import ReceiveBuffer
8+ from ._state import * # Import all state sentinels
9+ from ._state import _SWITCH_CONNECT , _SWITCH_UPGRADE , ConnectionState
10+ from ._util import ( # Import the internal things we need
11+ LocalProtocolError ,
12+ make_sentinel ,
13+ RemoteProtocolError ,
14+ )
1615from ._writers import WRITERS
1716
1817# Everything in __all__ gets re-exported as part of the h11 public API.
@@ -53,6 +52,7 @@ def _keep_alive(event):
5352 return False
5453 return True
5554
55+
5656def _body_framing (request_method , event ):
5757 # Called when we enter SEND_BODY to figure out framing information for
5858 # this body.
@@ -73,10 +73,11 @@ def _body_framing(request_method, event):
7373 # Step 1: some responses always have an empty body, regardless of what the
7474 # headers say.
7575 if type (event ) is Response :
76- if (event .status_code in (204 , 304 )
76+ if (
77+ event .status_code in (204 , 304 )
7778 or request_method == b"HEAD"
78- or (request_method == b"CONNECT"
79- and 200 <= event . status_code < 300 ) ):
79+ or (request_method == b"CONNECT" and 200 <= event . status_code < 300 )
80+ ):
8081 return ("content-length" , (0 ,))
8182 # Section 3.3.3 also lists another case -- responses with status_code
8283 # < 200. For us these are InformationalResponses, not Responses, so
@@ -100,12 +101,14 @@ def _body_framing(request_method, event):
100101 else :
101102 return ("http/1.0" , ())
102103
104+
103105################################################################
104106#
105107# The main Connection class
106108#
107109################################################################
108110
111+
109112class Connection (object ):
110113 """An object encapsulating the state of an HTTP connection.
111114
@@ -121,14 +124,14 @@ class Connection(object):
121124 :exc:`RemoteProtocolError`.
122125
123126 """
124- def __init__ (self ,
125- our_role ,
126- max_incomplete_event_size = DEFAULT_MAX_INCOMPLETE_EVENT_SIZE ):
127+
128+ def __init__ (
129+ self , our_role , max_incomplete_event_size = DEFAULT_MAX_INCOMPLETE_EVENT_SIZE
130+ ):
127131 self ._max_incomplete_event_size = max_incomplete_event_size
128132 # State and role tracking
129133 if our_role not in (CLIENT , SERVER ):
130- raise ValueError (
131- "expected CLIENT or SERVER, not {!r}" .format (our_role ))
134+ raise ValueError ("expected CLIENT or SERVER, not {!r}" .format (our_role ))
132135 self .our_role = our_role
133136 if our_role is CLIENT :
134137 self .their_role = SERVER
@@ -185,8 +188,7 @@ def their_state(self):
185188
186189 @property
187190 def they_are_waiting_for_100_continue (self ):
188- return (self .their_role is CLIENT
189- and self .client_is_waiting_for_100_continue )
191+ return self .their_role is CLIENT and self .client_is_waiting_for_100_continue
190192
191193 def start_next_cycle (self ):
192194 """Attempt to reset our connection state for a new request/response
@@ -217,8 +219,10 @@ def _server_switch_event(self, event):
217219 if type (event ) is InformationalResponse and event .status_code == 101 :
218220 return _SWITCH_UPGRADE
219221 if type (event ) is Response :
220- if (_SWITCH_CONNECT in self ._cstate .pending_switch_proposals
221- and 200 <= event .status_code < 300 ):
222+ if (
223+ _SWITCH_CONNECT in self ._cstate .pending_switch_proposals
224+ and 200 <= event .status_code < 300
225+ ):
222226 return _SWITCH_CONNECT
223227 return None
224228
@@ -244,8 +248,11 @@ def _process_event(self, role, event):
244248 self ._request_method = event .method
245249
246250 # self.their_http_version
247- if (role is self .their_role
248- and type (event ) in (Request , Response , InformationalResponse )):
251+ if role is self .their_role and type (event ) in (
252+ Request ,
253+ Response ,
254+ InformationalResponse ,
255+ ):
249256 self .their_http_version = event .http_version
250257
251258 # Keep alive handling
@@ -341,8 +348,7 @@ def receive_data(self, data):
341348 """
342349 if data :
343350 if self ._receive_buffer_closed :
344- raise RuntimeError (
345- "received close, then received more data?" )
351+ raise RuntimeError ("received close, then received more data?" )
346352 self ._receive_buffer += data
347353 else :
348354 self ._receive_buffer_closed = True
@@ -414,8 +420,7 @@ def next_event(self):
414420 """
415421
416422 if self .their_state is ERROR :
417- raise RemoteProtocolError (
418- "Can't receive data when peer state is ERROR" )
423+ raise RemoteProtocolError ("Can't receive data when peer state is ERROR" )
419424 try :
420425 event = self ._extract_next_receive_event ()
421426 if event not in [NEED_DATA , PAUSED ]:
@@ -425,13 +430,13 @@ def next_event(self):
425430 if len (self ._receive_buffer ) > self ._max_incomplete_event_size :
426431 # 431 is "Request header fields too large" which is pretty
427432 # much the only situation where we can get here
428- raise RemoteProtocolError ("Receive buffer too long" ,
429- error_status_hint = 431 )
433+ raise RemoteProtocolError (
434+ "Receive buffer too long" , error_status_hint = 431
435+ )
430436 if self ._receive_buffer_closed :
431437 # We're still trying to complete some event, but that's
432438 # never going to happen because no more data is coming
433- raise RemoteProtocolError (
434- "peer unexpectedly closed connection" )
439+ raise RemoteProtocolError ("peer unexpectedly closed connection" )
435440 return event
436441 except BaseException as exc :
437442 self ._process_error (self .their_role )
@@ -476,8 +481,7 @@ def send_with_data_passthrough(self, event):
476481
477482 """
478483 if self .our_state is ERROR :
479- raise LocalProtocolError (
480- "Can't send data when our state is ERROR" )
484+ raise LocalProtocolError ("Can't send data when our state is ERROR" )
481485 try :
482486 if type (event ) is Response :
483487 self ._clean_up_response_headers_for_sending (event )
@@ -557,8 +561,7 @@ def _clean_up_response_headers_for_sending(self, response):
557561 # to fix it instead of erroring out, so we'll accord the user the
558562 # same respect).
559563 set_comma_header (headers , b"content-length" , [])
560- if (self .their_http_version is None
561- or self .their_http_version < b"1.1" ):
564+ if self .their_http_version is None or self .their_http_version < b"1.1" :
562565 # Either we never got a valid request and are sending back an
563566 # error (their_http_version is None), so we assume the worst;
564567 # or else we did get a valid HTTP/1.0 request, so we know that
0 commit comments