Format: application/x-python-code
⬇ Download File (15.5 KB)
   1  o
   2  
   3  .'h�g�@sjddlmZmZmZmZmZmZmZmZm	Z	ddl
   4  mZmZm
   5  Z
   6  mZmZmZmZddlmZmZmZddlmZmZddlmZddlmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%ddl&m'Z'm(Z(m)Z)dd	l*m+Z+m,Z,gd
   7  �Z-Gdd�de)e)d
   8  �Z.Gdd�de)e)d
   9  �Z/dZ0de	eefde1fdd�Z2de3de	eefdee4e	edee5fffdd�Z6Gdd�d�Z7dS)�)	�Any�Callable�cast�Dict�List�Optional�Tuple�Type�Union�)�ConnectionClosed�Data�EndOfMessage�Event�InformationalResponse�Request�Response)�get_comma_header�has_expect_100_continue�set_comma_header)�READERS�ReadersType)�
  10  ReceiveBuffer)
  11  �_SWITCH_CONNECT�_SWITCH_UPGRADE�CLIENT�ConnectionState�DONE�ERROR�MIGHT_SWITCH_PROTOCOL�	SEND_BODY�SERVER�SWITCHED_PROTOCOL)�LocalProtocolError�RemoteProtocolError�Sentinel)�WRITERS�WritersType)�
  12  Connection�	NEED_DATA�PAUSEDc@�eZdZdS)r)N��__name__�
  13  __module__�__qualname__�r0r0�EC:\Users\shubh\Videos\agent\nova\Lib\site-packages\h11/_connection.pyr)(�r))�	metaclassc@r+)r*Nr,r0r0r0r1r*,r2r*i@�event�returncCs0t|jd�}d|vrdSt|dd�dkrdSdS)N�
  14  connection�closeF�http_version�1.1T)r�headers�getattr)r4�
  15  connectionr0r0r1�_keep_aliveHsr=�request_methodr0cCs�t|�ttfvs
  16  J�t|�tur4|jdvs+|dks+|dkr-d|jkr)dkr-dSndS|jdks4J�t|jd�}|rE|dgksCJ�d	St|jd
  17  �}|rVdt|d�ffSt|�tur^dSd
  18  S)N)��i0�HEAD�CONNECT���,)�content-length)r�transfer-encoding�chunked)�chunkedr0�content-lengthrDr)�http/1.0r0)�typerr�status_coderr:�int)r>r4Ztransfer_encodingsZcontent_lengthsr0r0r1�
  19  _body_framingQs&
  20  �rMc@s�eZdZdZefdeededdfdd�Ze	de
  21  eeeeffdd	��Ze	deefd
  22  d��Ze	deefdd
  23  ��Z
  24  e	defdd��Zd3dd�Zdeeddfdd�Zdedeeefdd�Zdeededdfdd�Zdeedeedeeefdeedeffdd�Z	d4de
  25  eeeefdeeddfdd �Ze	deeeffd!d"��Zd#eddfd$d%�Z deeee!ee"ffd&d'�Z#deeee!ee"ffd(d)�Z$dedeefd*d+�Z%dedee&efd,d-�Z'd3d.d/�Z(d0e)de)fd1d2�Z*dS)5r(aBAn object encapsulating the state of an HTTP connection.
  26  
  27      Args:
  28          our_role: If you're implementing a client, pass :data:`h11.CLIENT`. If
  29              you're implementing a server, pass :data:`h11.SERVER`.
  30  
  31          max_incomplete_event_size (int):
  32              The maximum number of bytes we're willing to buffer of an
  33              incomplete event. In practice this mostly sets a limit on the
  34              maximum size of the request/response line + headers. If this is
  35              exceeded, then :meth:`next_event` will raise
  36              :exc:`RemoteProtocolError`.
  37  
  38      �our_role�max_incomplete_event_sizer5NcCs�||_|ttfvrtd�|���||_||turt|_nt|_t�|_|�	|jdt
  39  �|_|�	|jdt�|_
  40  t�|_d|_d|_d|_d|_dS)Nz#expected CLIENT or SERVER, not {!r}F)�_max_incomplete_event_sizerr!�
  41  ValueError�formatrN�
  42  their_roler�_cstate�_get_io_objectr&�_writerr�_readerr�_receive_buffer�_receive_buffer_closed�their_http_version�_request_method�"client_is_waiting_for_100_continue)�selfrNrOr0r0r1�__init__�s 
  43  zConnection.__init__cCst|jj�S)z�A dictionary like::
  44  
  45             {CLIENT: <client state>, SERVER: <server state>}
  46  
  47          See :ref:`state-machine` for details.
  48  
  49          )�dictrT�states�r]r0r0r1r`�s	zConnection.statescC�|jj|jS)zjThe current state of whichever role we are playing. See
  50          :ref:`state-machine` for details.
  51          )rTr`rNrar0r0r1�	our_state��zConnection.our_statecCrb)znThe current state of whichever role we are NOT playing. See
  52          :ref:`state-machine` for details.
  53          )rTr`rSrar0r0r1�their_state�rdzConnection.their_statecCs|jtuo|jS�N)rSrr\rar0r0r1�!they_are_waiting_for_100_continue�sz,Connection.they_are_waiting_for_100_continuecCs4t|jj�}|j��d|_|jrJ�|�|�dS)a�Attempt to reset our connection state for a new request/response
  54          cycle.
  55  
  56          If both client and server are in :data:`DONE` state, then resets them
  57          both to :data:`IDLE` state in preparation for a new request/response
  58          cycle on this same connection. Otherwise, raises a
  59          :exc:`LocalProtocolError`.
  60  
  61          See :ref:`keepalive-and-pipelining`.
  62  
  63          N)r_rTr`�start_next_cycler[r\�_respond_to_state_changes)r]�
  64  old_statesr0r0r1rh�s
  65  
  66  
  67  zConnection.start_next_cycle�rolecCs&t|jj�}|j�|�|�|�dSrf)r_rTr`Z
  68  process_errorri)r]rkrjr0r0r1�_process_error�szConnection._process_errorr4cCsTt|�tur
  69  |jdkr
  70  tSt|�tur(t|jjvr(d|jkr%dkr(tSdSdS)N�erBrC)rJrrKrrrrTZpending_switch_proposals)r]r4r0r0r1�_server_switch_event�s�zConnection._server_switch_eventcCsRt|jj�}|tur't|�tur'|jdkr|j�t�t	|j
  71  d�r'|j�t�d}|tur2|�
  72  |�}|j�|t|�|�t|�turF|j|_||jurbt|�tttfvrbtttttf|�}|j|_t|�ttfvrztttttf|��sz|j��t|�tur�t|�r�d|_t|�ttfvr�d|_|tur�t|�ttfvr�d|_|�||�dS)NrAsupgradeTF)r_rTr`rrJr�methodZprocess_client_switch_proposalrrr:rr!rnZ
  73  process_eventr[rSrrrr
  74  r8rZr=Zprocess_keep_alive_disabledrr\r
  75  rri)r]rkr4rjZserver_switch_eventr0r0r1�_process_events<
  76  
  77  ��
  78  zConnection._process_event�io_dict.cCsV|jj|}|tur$ttt|j�tttt	f|��\}}|t||�S|�
  79  ||f�Srf)rTr`r rMr�bytesr[r
  80  rr�get)r]rkr4rq�state�framing_type�argsr0r0r1rU4s�zConnection._get_io_objectrjcCsL|j||jkr|�|j|t�|_|j||jkr$|�|j|t�|_dSdSrf)	rcrNrUr&rVrerSrrW)r]rjr4r0r0r1riJs
  81  �z$Connection._respond_to_state_changescCst|j�|jfS)abData that has been received, but not yet processed, represented as
  82          a tuple with two elements, where the first is a byte-string containing
  83          the unprocessed data itself, and the second is a bool that is True if
  84          the receive connection was closed.
  85  
  86          See :ref:`switching-protocols` for discussion of why you'd want this.
  87          )rrrXrYrar0r0r1�
  88  trailing_dataUs	zConnection.trailing_data�datacCs.|r|jr	td��|j|7_dSd|_dS)a�Add data to our internal receive buffer.
  89  
  90          This does not actually do any processing on the data, just stores
  91          it. To trigger processing, you have to call :meth:`next_event`.
  92  
  93          Args:
  94              data (:term:`bytes-like object`):
  95                  The new data that was just received.
  96  
  97                  Special case: If *data* is an empty byte-string like ``b""``,
  98                  then this indicates that the remote side has closed the
  99                  connection (end of file). Normally this is convenient, because
 100                  standard Python APIs like :meth:`file.read` or
 101                  :meth:`socket.recv` use ``b""`` to indicate end-of-file, while
 102                  other failures to read are indicated using other mechanisms
 103                  like raising :exc:`TimeoutError`. When using such an API you
 104                  can just blindly pass through whatever you get from ``read``
 105                  to :meth:`receive_data`, and everything will work.
 106  
 107                  But, if you have an API where reading an empty string is a
 108                  valid non-EOF condition, then you need to be aware of this and
 109                  make sure to check for such strings and avoid passing them to
 110                  :meth:`receive_data`.
 111  
 112          Returns:
 113              Nothing, but after calling this you should call :meth:`next_event`
 114              to parse the newly received data.
 115  
 116          Raises:
 117              RuntimeError:
 118                  Raised if you pass an empty *data*, indicating EOF, and then
 119                  pass a non-empty *data*, indicating more data that somehow
 120                  arrived after the EOF.
 121  
 122                  (Calling ``receive_data(b"")`` multiple times is fine,
 123                  and equivalent to calling it once.)
 124  
 125          z(received close, then received more data?TN)rY�RuntimeErrorrX)r]rxr0r0r1�receive_data`s
 126  '
 127  zConnection.receive_datacCs�|j}|tur|jrtS|tus|turtS|jdusJ�|�|j�}|dur<|js<|jr<t|jd�r9|j�	�}nt
 128  �}|durBt}|S)N�read_eof)rerrXr*rr"rWrY�hasattrr{rr))r]rtr4r0r0r1�_extract_next_receive_event�sz&Connection._extract_next_receive_eventc
 129  Cs�|jtur	td��z0|��}|ttfvr|�|jtt	|��|tur7t
 130  |j�|jkr0tddd��|j
 131  r7td��|WSty]}z|�|j�t|t�rQ|��n�WYd}~dSd}~ww)aFParse the next event out of our receive buffer, update our internal
 132          state, and return it.
 133  
 134          This is a mutating operation -- think of it like calling :func:`next`
 135          on an iterator.
 136  
 137          Returns:
 138              : One of three things:
 139  
 140              1) An event object -- see :ref:`events`.
 141  
 142              2) The special constant :data:`NEED_DATA`, which indicates that
 143                 you need to read more data from your socket and pass it to
 144                 :meth:`receive_data` before this method will be able to return
 145                 any more events.
 146  
 147              3) The special constant :data:`PAUSED`, which indicates that we
 148                 are not in a state where we can process incoming data (usually
 149                 because the peer has finished their part of the current
 150                 request/response cycle, and you have not yet called
 151                 :meth:`start_next_cycle`). See :ref:`flow-control` for details.
 152  
 153          Raises:
 154              RemoteProtocolError:
 155                  The peer has misbehaved. You should close the connection
 156                  (possibly after sending some kind of 4xx response).
 157  
 158          Once this method returns :class:`ConnectionClosed` once, then all
 159          subsequent calls will also return :class:`ConnectionClosed`.
 160  
 161          If this method raises any exception besides :exc:`RemoteProtocolError`
 162          then that's a bug -- if it happens please file a bug report!
 163  
 164          If this method raises any exception then it also sets
 165          :attr:`Connection.their_state` to :data:`ERROR` -- see
 166          :ref:`error-handling` for discussion.
 167  
 168          z+Can't receive data when peer state is ERRORzReceive buffer too longi�)Zerror_status_hintz#peer unexpectedly closed connectionN)rerr$r}r)r*rprSrr�lenrXrPrY�
 169  BaseExceptionrl�
 170  isinstancer#Z!_reraise_as_remote_protocol_error)r]r4�excr0r0r1�
 171  next_event�s,
 172  (�
 173  
 174  ���zConnection.next_eventcCs |�|�}|durdSd�|�S)a�Convert a high-level event into bytes that can be sent to the peer,
 175          while updating our internal state machine.
 176  
 177          Args:
 178              event: The :ref:`event <events>` to send.
 179  
 180          Returns:
 181              If ``type(event) is ConnectionClosed``, then returns
 182              ``None``. Otherwise, returns a :term:`bytes-like object`.
 183  
 184          Raises:
 185              LocalProtocolError:
 186                  Sending this event at this time would violate our
 187                  understanding of the HTTP/1.1 protocol.
 188  
 189          If this method raises any exception then it also sets
 190          :attr:`Connection.our_state` to :data:`ERROR` -- see
 191          :ref:`error-handling` for discussion.
 192  
 193          N�)�send_with_data_passthrough�join)r]r4�	data_listr0r0r1�send�s
 194  
 195  zConnection.sendcCs�|jtur	td��z/t|�tur|�|�}|j}|�|j|�t|�t	ur(WdS|dus.J�g}|||j
 196  �|WS|�|j��)ahIdentical to :meth:`send`, except that in situations where
 197          :meth:`send` returns a single :term:`bytes-like object`, this instead
 198          returns a list of them -- and when sending a :class:`Data` event, this
 199          list is guaranteed to contain the exact object you passed in as
 200          :attr:`Data.data`. See :ref:`sendfile` for discussion.
 201  
 202          z'Can't send data when our state is ERRORN)rcrr#rJr�&_clean_up_response_headers_for_sendingrVrprNr�appendrl)r]r4�writerr�r0r0r1r�s 
 203  
 204  z%Connection.send_with_data_passthroughcCs|�|j�dS)z�Notify the state machine that we failed to send the data it gave
 205          us.
 206  
 207          This causes :attr:`Connection.our_state` to immediately become
 208          :data:`ERROR` -- see :ref:`error-handling` for discussion.
 209  
 210          N)rlrNrar0r0r1�send_failed'szConnection.send_failed�responsecCs�t|�tusJ�|j}d}tt|j�}|dkrd}t||�\}}|dvrIt|dg�}|jdus4|jdkrBt|dg�}|jdkrAd}nt|dd	g�}|j	j
 211  rO|rhtt|d
 212  ��}|�
 213  d�|�d�t|d
 214  t|��}t||j|j|jd
 215  �S)NFr@sGET)rGrIrHr9rETrFr6s
 216  keep-aliver7)r:rKr8�reason)rJrr:rrrr[rMrrZrT�
 217  keep_alive�setr�discard�add�sortedrKr8r�)r]r�r:Z
 218  need_closeZmethod_for_choosing_headersru�_r<r0r0r1r�@s4	
 219  �
 220  
 221  �z1Connection._clean_up_response_headers_for_sending)r5Nrf)+r-r.r/�__doc__�!DEFAULT_MAX_INCOMPLETE_EVENT_SIZEr	r%rLr^�propertyrr`rcre�boolrgrhrlrrrnrpr
 222  rr'rrrUrirrrrwrzr)r*r}r�r�rr�r�rr�r0r0r0r1r(�sd���
 223  �' 
 224  
 225  0��
 226  �
 227  ����
 228  �
 229  .
 230  � A
 231  !r(N)8�typingrrrrrrrr	r
 232  Z_eventsrr
 233  rrrrr�_headersrrr�_readersrrZ_receivebufferr�_staterrrrrrrr r!r"Z_utilr#r$r%�_writersr&r'�__all__r)r*r�r�r=rr�strrLrMr(r0r0r0r1�<module>s*,$	0	�
 234  �
 235  �: