1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
U
M±d«gã@sfddlmZmZmZmZmZmZmZmZm    Z    ddl
m Z m Z m Z 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,d
d d gZ-Gd d „d e)e)dZ.Gdd „d e)e)dZ/dZ0e    eefe1dœdd„Z2e3e    eefee4e    edee5ffdœdd„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)Ú ReceiveBuffer)
Ú_SWITCH_CONNECTÚ_SWITCH_UPGRADEÚCLIENTÚConnectionStateÚDONEÚERRORÚMIGHT_SWITCH_PROTOCOLÚ    SEND_BODYÚSERVERÚSWITCHED_PROTOCOL)ÚLocalProtocolErrorÚRemoteProtocolErrorÚSentinel)ÚWRITERSÚ WritersTypeÚ
ConnectionÚ    NEED_DATAÚPAUSEDc@s eZdZdS)r)N©Ú__name__Ú
__module__Ú __qualname__©r/r/úFd:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\h11/_connection.pyr)(s)Ú    metaclassc@s eZdZdS)r*Nr+r/r/r/r0r*,si@©ÚeventÚreturncCs0t|jdƒ}d|krdSt|ddƒdkr,dSdS)Nó
connectionócloseFÚ http_versionó1.1T)rÚheadersÚgetattr)r3Ú
connectionr/r/r0Ú _keep_aliveHs  r<r/)Úrequest_methodr3r4cCsÂt|ƒttfkst‚t|ƒtkrf|jdksT|dksT|dkrXd|jkrPdkrXnndS|jdksft‚t|jdƒ}|rˆ|dgks„t‚d    St|jd
ƒ}|rªd t|d ƒffSt|ƒtkrºdSd SdS)N)éÌi0óHEADóCONNECTéÈé,)úcontent-length)rótransfer-encodingóchunked)Úchunkedr/ócontent-lengthrCr)úhttp/1.0r/)ÚtyperrÚAssertionErrorÚ status_coderr9Úint)r=r3Ztransfer_encodingsZcontent_lengthsr/r/r0Ú _body_framingQs2 ÿþýýý
   rMc@sæeZdZdZefeeeddœdd„Ze    e
eeeefdœdd„ƒZ e    eedœd    d
„ƒZ e    eedœd d „ƒZ e    edœd d„ƒZddœdd„Zeeddœdd„Zeeeedœdd„Zeeeddœdd„Zeeeeeeefeedefdœdd„Zd3e
eeeefeeddœdd „Ze    eeefdœd!d"„ƒZedd#œd$d%„Z eeee!ee"fdœd&d'„Z#eeee!ee"fdœd(d)„Z$eeedœd*d+„Z%eee&edœd,d-„Z'ddœd.d/„Z(e)e)d0œd1d2„Z*dS)4r(aBAn object encapsulating the state of an HTTP connection.
 
    Args:
        our_role: If you're implementing a client, pass :data:`h11.CLIENT`. If
            you're implementing a server, pass :data:`h11.SERVER`.
 
        max_incomplete_event_size (int):
            The maximum number of bytes we're willing to buffer of an
            incomplete event. In practice this mostly sets a limit on the
            maximum size of the request/response line + headers. If this is
            exceeded, then :meth:`next_event` will raise
            :exc:`RemoteProtocolError`.
 
    N)Úour_roleÚmax_incomplete_event_sizer4cCs||_|ttfkr td |¡ƒ‚||_||tkr:t|_nt|_tƒ|_|     |jdt
¡|_ |     |jdt ¡|_ tƒ|_d|_d|_d|_d|_dS)Nz#expected CLIENT or SERVER, not {!r}F)Ú_max_incomplete_event_sizerr!Ú
ValueErrorÚformatrNÚ
their_rolerÚ_cstateÚ_get_io_objectr&Ú_writerrÚ_readerrÚ_receive_bufferÚ_receive_buffer_closedÚtheir_http_versionÚ_request_methodÚ"client_is_waiting_for_100_continue)ÚselfrNrOr/r/r0Ú__init__›s  zConnection.__init__)r4cCs t|jjƒS)z‰A dictionary like::
 
           {CLIENT: <client state>, SERVER: <server state>}
 
        See :ref:`state-machine` for details.
 
        )ÚdictrTÚstates©r]r/r/r0r`Âs    zConnection.statescCs|jj|jS)zjThe current state of whichever role we are playing. See
        :ref:`state-machine` for details.
        )rTr`rNrar/r/r0Ú    our_stateÍszConnection.our_statecCs|jj|jS)znThe current state of whichever role we are NOT playing. See
        :ref:`state-machine` for details.
        )rTr`rSrar/r/r0Ú their_stateÔszConnection.their_statecCs|jtko|jS©N)rSrr\rar/r/r0Ú!they_are_waiting_for_100_continueÛsz,Connection.they_are_waiting_for_100_continuecCs4t|jjƒ}|j ¡d|_|jr&t‚| |¡dS)aAttempt to reset our connection state for a new request/response
        cycle.
 
        If both client and server are in :data:`DONE` state, then resets them
        both to :data:`IDLE` state in preparation for a new request/response
        cycle on this same connection. Otherwise, raises a
        :exc:`LocalProtocolError`.
 
        See :ref:`keepalive-and-pipelining`.
 
        N)r_rTr`Ústart_next_cycler[r\rJÚ_respond_to_state_changes)r]Ú
old_statesr/r/r0rfßs
 
 
zConnection.start_next_cycle)Úroler4cCs&t|jjƒ}|j |¡| |¡dSrd)r_rTr`Z process_errorrg)r]rirhr/r/r0Ú_process_errorós  zConnection._process_errorr2cCsTt|ƒtkr|jdkrtSt|ƒtkrPt|jjkrPd|jkrHdkrPnntSdS)NéerArB)rIrrKrrrrTZpending_switch_proposals)r]r3r/r/r0Ú_server_switch_eventøs 
ÿþþ
zConnection._server_switch_event)rir3r4cCs\t|jjƒ}|tkrNt|ƒtkrN|jdkr6|j t¡t    |j
dƒrN|j t ¡d}|t krd|  |¡}|j |t|ƒ|¡t|ƒtkrŒ|j|_||jkrÄt|ƒtttfkrÄtttttf|ƒ}|j|_t|ƒttfkrôtttttf|ƒƒsô|j ¡t|ƒtkrt|ƒrd|_t|ƒttfkr*d|_|tkrLt|ƒttfkrLd|_| ||¡dS)Nr@supgradeTF)r_rTr`rrIrÚmethodZprocess_client_switch_proposalrrr9rr!rlZ process_eventr[rSrrrr
r7rZr<Zprocess_keep_alive_disabledrr\r rrg)r]rir3rhZserver_switch_eventr/r/r0Ú_process_events< 
 
 ýÿ
zConnection._process_event.)rir3Úio_dictr4cCsZ|jj|}|tkrHttt|jƒtttt    f|ƒƒ\}}|t||ŽS| 
||f¡SdSrd) rTr`r rMrÚbytesr[r
rrÚget)r]rir3roÚstateÚ framing_typeÚargsr/r/r0rU4s 
ÿzConnection._get_io_object)rhr3r4cCsH|j||jkr"| |j|t¡|_|j||jkrD| |j|t¡|_dSrd)    rbrNrUr&rVrcrSrrW)r]rhr3r/r/r0rgJsz$Connection._respond_to_state_changescCst|jƒ|jfS)abData that has been received, but not yet processed, represented as
        a tuple with two elements, where the first is a byte-string containing
        the unprocessed data itself, and the second is a bool that is True if
        the receive connection was closed.
 
        See :ref:`switching-protocols` for discussion of why you'd want this.
        )rprXrYrar/r/r0Ú trailing_dataUs    zConnection.trailing_data)Údatar4cCs,|r"|jrtdƒ‚|j|7_nd|_dS)aØAdd data to our internal receive buffer.
 
        This does not actually do any processing on the data, just stores
        it. To trigger processing, you have to call :meth:`next_event`.
 
        Args:
            data (:term:`bytes-like object`):
                The new data that was just received.
 
                Special case: If *data* is an empty byte-string like ``b""``,
                then this indicates that the remote side has closed the
                connection (end of file). Normally this is convenient, because
                standard Python APIs like :meth:`file.read` or
                :meth:`socket.recv` use ``b""`` to indicate end-of-file, while
                other failures to read are indicated using other mechanisms
                like raising :exc:`TimeoutError`. When using such an API you
                can just blindly pass through whatever you get from ``read``
                to :meth:`receive_data`, and everything will work.
 
                But, if you have an API where reading an empty string is a
                valid non-EOF condition, then you need to be aware of this and
                make sure to check for such strings and avoid passing them to
                :meth:`receive_data`.
 
        Returns:
            Nothing, but after calling this you should call :meth:`next_event`
            to parse the newly received data.
 
        Raises:
            RuntimeError:
                Raised if you pass an empty *data*, indicating EOF, and then
                pass a non-empty *data*, indicating more data that somehow
                arrived after the EOF.
 
                (Calling ``receive_data(b"")`` multiple times is fine,
                and equivalent to calling it once.)
 
        z(received close, then received more data?TN)rYÚ RuntimeErrorrX)r]rvr/r/r0Ú receive_data`s
'zConnection.receive_datacCsˆ|j}|tkr|jrtS|tks(|tkr,tS|jdk    s:t‚| |j¡}|dkrx|jsx|jrxt    |jdƒrr|j 
¡}nt ƒ}|dkr„t }|S)NÚread_eof) rcrrXr*rr"rWrJrYÚhasattrryr r))r]rrr3r/r/r0Ú_extract_next_receive_eventŽs    z&Connection._extract_next_receive_eventc
Cs¼|jtkrtdƒ‚z`| ¡}|ttfkr<| |jtt    |ƒ¡|tkrnt
|j ƒ|j kr`tddd‚|j rntdƒ‚|WStk
r¶}z&| |j¡t|tƒr¤| ¡n‚W5d}~XYnXdS)aFParse the next event out of our receive buffer, update our internal
        state, and return it.
 
        This is a mutating operation -- think of it like calling :func:`next`
        on an iterator.
 
        Returns:
            : One of three things:
 
            1) An event object -- see :ref:`events`.
 
            2) The special constant :data:`NEED_DATA`, which indicates that
               you need to read more data from your socket and pass it to
               :meth:`receive_data` before this method will be able to return
               any more events.
 
            3) The special constant :data:`PAUSED`, which indicates that we
               are not in a state where we can process incoming data (usually
               because the peer has finished their part of the current
               request/response cycle, and you have not yet called
               :meth:`start_next_cycle`). See :ref:`flow-control` for details.
 
        Raises:
            RemoteProtocolError:
                The peer has misbehaved. You should close the connection
                (possibly after sending some kind of 4xx response).
 
        Once this method returns :class:`ConnectionClosed` once, then all
        subsequent calls will also return :class:`ConnectionClosed`.
 
        If this method raises any exception besides :exc:`RemoteProtocolError`
        then that's a bug -- if it happens please file a bug report!
 
        If this method raises any exception then it also sets
        :attr:`Connection.their_state` to :data:`ERROR` -- see
        :ref:`error-handling` for discussion.
 
        z+Can't receive data when peer state is ERRORzReceive buffer too longi¯)Zerror_status_hintz#peer unexpectedly closed connectionN)rcrr$r{r)r*rnrSrrÚlenrXrPrYÚ BaseExceptionrjÚ
isinstancer#Z!_reraise_as_remote_protocol_error)r]r3Úexcr/r/r0Ú
next_eventªs((
 ÿ 
 
zConnection.next_eventcCs$| |¡}|dkrdSd |¡SdS)a³Convert a high-level event into bytes that can be sent to the peer,
        while updating our internal state machine.
 
        Args:
            event: The :ref:`event <events>` to send.
 
        Returns:
            If ``type(event) is ConnectionClosed``, then returns
            ``None``. Otherwise, returns a :term:`bytes-like object`.
 
        Raises:
            LocalProtocolError:
                Sending this event at this time would violate our
                understanding of the HTTP/1.1 protocol.
 
        If this method raises any exception then it also sets
        :attr:`Connection.our_state` to :data:`ERROR` -- see
        :ref:`error-handling` for discussion.
 
        Nó)Úsend_with_data_passthroughÚjoin)r]r3Ú    data_listr/r/r0Úsendës
zConnection.sendcCs”|jtkrtdƒ‚zbt|ƒtkr*| |¡}|j}| |j|¡t|ƒt    krPWdS|dk    s\t
‚g}|||j ƒ|WSWn|  |j¡‚YnXdS)ahIdentical to :meth:`send`, except that in situations where
        :meth:`send` returns a single :term:`bytes-like object`, this instead
        returns a list of them -- and when sending a :class:`Data` event, this
        list is guaranteed to contain the exact object you passed in as
        :attr:`Data.data`. See :ref:`sendfile` for discussion.
 
        z'Can't send data when our state is ERRORN) rbrr#rIrÚ&_clean_up_response_headers_for_sendingrVrnrNr rJÚappendrj)r]r3Úwriterr„r/r/r0r‚s 
 
 
 z%Connection.send_with_data_passthroughcCs| |j¡dS)záNotify the state machine that we failed to send the data it gave
        us.
 
        This causes :attr:`Connection.our_state` to immediately become
        :data:`ERROR` -- see :ref:`error-handling` for discussion.
 
        N)rjrNrar/r/r0Ú send_failed'szConnection.send_failed)Úresponser4cCsæt|ƒtkst‚|j}d}tt|jƒ}|dkr2d}t||ƒ\}}|dkr’t|dgƒ}|j    dksh|j    dkr„t|dgƒ}|jdkr’d}nt|dd    gƒ}|j
j rž|rÐt t |d
ƒƒ}| d ¡| d ¡t|d
t|ƒƒ}t||j|j|jd S)NFr?sGET)rFrHrGr8rDTrEr5s
keep-aliver6)r9rKr7Úreason)rIrrJr9rrpr[rMrrZrTÚ
keep_aliveÚsetrÚdiscardÚaddÚsortedrKr7r‹)r]rŠr9Z
need_closeZmethod_for_choosing_headersrsÚ_r;r/r/r0r†@s2       
 
 
üz1Connection._clean_up_response_headers_for_sending)N)+r,r-r.Ú__doc__Ú!DEFAULT_MAX_INCOMPLETE_EVENT_SIZEr    r%rLr^Úpropertyrr`rbrcÚboolrerfrjrrrlrnr
rr'rrrUrgrrprurxr)r*r{r€r…rr‚r‰rr†r/r/r/r0r(‹sPýü ' 
 2
û ýü 
0þ  A!N)8Útypingrrrrrrrr    r
Z_eventsr r rrrrrÚ_headersrrrÚ_readersrrZ_receivebufferrÚ_staterrrrrrrr r!r"Z_utilr#r$r%Ú_writersr&r'Ú__all__r)r*r“r•r<rpÚstrrLrMr(r/r/r/r0Ú<module>s",$     0 
 
 
þ :