
    ^h                        S r SSKrSSKrSSKrSSKrSSKJrJrJr  SSK	J
r
Jr  SSKJr  SSKJr  SSKJr  SSKJrJr  SS	KJr  SS
KJrJrJrJrJrJrJr  \R:                  " S5      r " S S\5      r  " S S5      r! " S S5      r" " S S\RF                  5      r$ " S S\RJ                  5      r& " S S5      r'\R:                  " S5      r(\R:                  " S5      r)S\*S\+4S jr,S\*S\+4S jr-S\R\                  S\/4S jr0g) zFClient and server implementations of HTTP/1.x.

.. versionadded:: 4.0
    N)Futurefuture_add_done_callback"future_set_result_unless_cancelled)
native_strutf8)gen)httputil)iostream)gen_logapp_log)GzipDecompressor)castOptionalType	AwaitableCallableUnionTuples   |
c                       \ rS rSrSS jrSrg)_QuietException,   Nc                     g N selfs    Q/home/james-whalen/.local/lib/python3.13/site-packages/tornado/http1connection.py__init___QuietException.__init__-           r   returnN)__name__
__module____qualname____firstlineno__r   __static_attributes__r   r!   r   r   r   ,   s    r!   r   c                   |    \ rS rSrSrS\R                  SS4S jrSS jrSS	S
\	\
   S\R                  SS4S jrSrg)_ExceptionLoggingContext1   zUsed with the ``with`` statement when calling delegate methods to
log any exceptions with the given logger.  Any exceptions caught are
converted to _QuietException
loggerr#   Nc                     Xl         g r   r,   )r   r,   s     r   r   !_ExceptionLoggingContext.__init__7   s    r!   c                     g r   r   r   s    r   	__enter__"_ExceptionLoggingContext.__enter__:   r    r!   typzOptional[Type[BaseException]]valuetbc                     UbG  Uc   e[        U[        R                  5      (       a  g U R                  R	                  SXU4S9  [
        eg )NUncaught exceptionexc_info)
isinstancer	   HTTPInputErrorr,   errorr   )r   r3   r4   r5   s       r   __exit__!_ExceptionLoggingContext.__exit__=   sR     ?"?%!8!899KK2c"=MN!! r!   r.   r"   )r$   r%   r&   r'   __doc__loggingLoggerr   r1   r   BaseExceptiontypesTracebackTyper=   r(   r   r!   r   r*   r*   1   sY    
w~~ $ "," &" 	"
 
"r!   r*   c                   r    \ rS rSrSr       SS\S\\   S\\   S\\   S\\   S	\\   S
\SS4S jjr	Sr
g)HTTP1ConnectionParametersL   z?Parameters for `.HTTP1Connection` and `.HTTP1ServerConnection`.Nno_keep_alive
chunk_sizemax_header_sizeheader_timeoutmax_body_sizebody_timeout
decompressr#   c                     Xl         U=(       d    SU l        U=(       d    SU l        X@l        XPl        X`l        Xpl        g)a  
:arg bool no_keep_alive: If true, always close the connection after
    one request.
:arg int chunk_size: how much data to read into memory at once
:arg int max_header_size:  maximum amount of data for HTTP headers
:arg float header_timeout: how long to wait for all headers (seconds)
:arg int max_body_size: maximum amount of data for body
:arg float body_timeout: how long to wait while reading body (seconds)
:arg bool decompress: if true, decode incoming
    ``Content-Encoding: gzip``
i   N)rH   rI   rJ   rK   rL   rM   rN   )r   rH   rI   rJ   rK   rL   rM   rN   s           r   r   "HTTP1ConnectionParameters.__init__O   s:    * +$-.7%,*($r!   )rM   rI   rN   rK   rL   rJ   rH   )FNNNNNF)r$   r%   r&   r'   r?   boolr   intfloatr   r(   r   r!   r   rF   rF   L   s    I $$()-*.'+(, %% SM% "#	%
 !%  }% uo% % 
% %r!   rF   c                      \ rS rSrSr  S+S\R                  S\S\\	   S\\
   SS4
S	 jjrS
\R                  S\\   4S jrS
\R                  S\4S jrS,S jrS\\/ S4      SS4S jrS,S jrS,S jrS\R                  4S jrS\SS4S jrS\SS4S jr S-S\\R8                  \R:                  4   S\R<                  S\\   SS4S jjr S\S\4S jr!S\SS4S jr"S,S jr#S.S jr$S\R8                  S\R<                  S\4S  jr%S/S! jr&S"\S\'\(\R<                  4   4S# jr)S$\S\R<                  S
\R                  S\\S      4S% jr*S&\S
\R                  SS4S' jr+S
\R                  SS4S( jr,S
\R                  SS4S) jr-S*r.g)0HTTP1Connectionm   zyImplements the HTTP/1.x protocol.

This class can be on its own for clients, or via `HTTP1ServerConnection`
for servers.
Nstream	is_clientparamscontextr#   c                    X l         Xl        Uc
  [        5       nX0l        X@l        UR
                  U l        U R                  R                  b  U R                  R                  OU R                  R                  U l        U R                  R                  U l
        SU l        SU l        [        5       U l        SU l        U R!                  5         SU l        SU l        SU l        SU l        SU l        SU l        g)z
:arg stream: an `.IOStream`
:arg bool is_client: client or server
:arg params: a `.HTTP1ConnectionParameters` instance or ``None``
:arg context: an opaque application-defined object that can be accessed
    as ``connection.context``.
NF)rX   rW   rF   rY   rZ   rH   rL   max_buffer_size_max_body_sizerM   _body_timeout_write_finished_read_finishedr   _finish_future_disconnect_on_finish_clear_callbacks_request_start_line_response_start_line_request_headers_chunking_output_expected_content_remaining_pending_write)r   rW   rX   rY   rZ   s        r   r   HTTP1Connection.__init__t   s     #>.0F#11
 {{((4 KK%%,, 	
 "[[55  %# %h &+" $( $(! $ % ,0("r!   delegatec                     U R                   R                  (       a  [        XR                   R                  5      nU R	                  U5      $ )aJ  Read a single HTTP response.

Typical client-mode usage is to write a request using `write_headers`,
`write`, and `finish`, and then call ``read_response``.

:arg delegate: a `.HTTPMessageDelegate`

Returns a `.Future` that resolves to a bool after the full response has
been read. The result is true if the stream is still open.
)rY   rN   _GzipMessageDelegaterI   _read_message)r   rk   s     r   read_responseHTTP1Connection.read_response   s6     ;;!!+Hkk6L6LMH!!(++r!   c                   #    Sn U R                   R                  SU R                  R                  S9nU R                  R                  c  UI S h  vN nOf [
        R                  " U R                   R                  R                  5       U R                  R                  -   U[        R                  S9I S h  vN nU R#                  U5      u  pVU R$                  (       a&  [&        R(                  " U5      nXpl        UnSU l        O?[&        R.                  " U5      n	Xl        X`l        U	nU R5                  X5      (       + U l        Sn[        [        5         UR7                  X5      n
U
b
  U
I S h  vN   S S S 5        U R                   cE  Sn U(       a(  [        [        5         UR                  5         S S S 5        S nU R!                  5         gSnU R$                  (       a  [9        U[&        R:                  5      (       d   eU R0                  b  U R0                  R<                  S:X  a  SnUR>                  nUS:X  a  SnSUs=::  a  S	:  aA  O  OS
U;   d  SU;   a  [&        R@                  " SU-  5      eU RC                  U5      I S h  vN   OAURE                  S5      S:X  a,  U RF                  (       d  U R                   RI                  S5        U(       d  U RK                  U R$                  (       a  WR>                  OSXa5      nUbt  U RL                  c  UI S h  vN   O\ [
        R                  " U R                   R                  R                  5       U RL                  -   U[        R                  S9I S h  vN   SU l*        U RF                  (       a  U R$                  (       a*  Sn[        [        5         URW                  5         S S S 5        U RX                  R[                  5       (       de  U R                   bX  U R                   R]                  5       (       d9  U R                   R_                  U R`                  5        U RX                  I S h  vN   U R$                  (       a!  U R,                  (       a  U R                  5         U R                   cC   U(       a(  [        [        5         UR                  5         S S S 5        S nU R!                  5         g U(       a(  [        [        5         UR                  5         S S S 5        S nU R!                  5         g GN GNe! [
        R                   af    U R                  5          U(       a:  [        [        5         UR                  5         S S S 5        O! , (       d  f       O= fS nU R!                  5         gf = f GN,! , (       d  f       GN1= f! , (       d  f       GN= f GN: GN GNQ! [
        R                   a    [N        RP                  " SU RR                  5        U R                   R                  5          U(       a:  [        [        5         UR                  5         S S S 5        O! , (       d  f       O= fS nU R!                  5         gf = f! , (       d  f       GN= f GN7! , (       d  f       GN= f! [&        R@                   a  n[N        RP                  " SU RR                  U5        U R$                  (       d$  U R                   RI                  S5      I S h  vN    U R                  5          S nAU(       a:  [        [        5         UR                  5         S S S 5        O! , (       d  f       O= fS nU R!                  5         gS nAff = f! , (       d  f       GN= f! U(       a:  [        [        5         UR                  5         S S S 5        O! , (       d  f       O= fS nU R!                  5         f = f7f)NFs   ?
?
	max_bytes)quiet_exceptionsTHEAD0  d      Content-LengthTransfer-Encodingz!Response code %d cannot have bodyExpectz100-continues   HTTP/1.1 100 (Continue)

r   zTimeout reading body from %sz"Malformed HTTP message from %s: %ss   HTTP/1.1 400 Bad Request

)1rW   read_until_regexrY   rJ   rK   r   with_timeoutio_looptimer
   StreamClosedErrorTimeoutErrorcloser*   r   on_connection_closerc   _parse_headersrX   r	   parse_response_start_linere   rb   parse_request_start_linerd   rf   _can_keep_aliveheaders_receivedr:   ResponseStartLinemethodcoder;   rn   getr_   write
_read_bodyr^   r   inforZ   r`   finishra   doneclosedset_close_callback_on_connection_close)r   rk   need_delegate_closeheader_futureheader_datastart_line_strheadersresp_start_line
start_linereq_start_lineheader_recv_future	skip_bodyr   body_futurees                  r   rn   HTTP1Connection._read_message   s    #u	$ KK88)D)D 9 M {{))1$11!(+(8(8++002T[[5O5OO%)1)C)C) #K '+&9&9+&F#N~~"*"D"D^"T,;)#  .3*!)!B!B>!R+9((/%+
151E1E"2 .* #')'2%-%>%>z%S"%1,,, 3 {{"&+#P #-g6002 7 M!!#W I~~!*h.H.HIIII,,800776A $I!3; !%I$$$ (726IW6T&55?$F 
 ,,X666;;x(N:4CWCWKK%%&HI"oo,0NNO((7 *))1)))	)"%"2"2 $ 3 3 8 8 :T=O=O O +191K1K#   #'D''4>>&+#-g6OO% 7 '',,..KK+**,,..t/H/HI))))~~$"<"<

{{" #-g6002 7 M!!# # #-g6002 7 M!!#c 2#
 '' !JJL D #-g6002 766 M!!#Q!0 - 32` 76c 7 *
  #// )#LL)GV KK--/#(6 #-g6002 766 M!!#E) 76 * 76 && 	LL=t||QO>>kk''(KLLLJJL"-g6002 766 M!!#	 76 #-g6002 766 M!!#s  _ A
Z TZ A T 7T8T <BZ V0V1V5Z _ #V14_ B:Z 	W
BZ WZ "AW 8W	9W =:Z 7Y4BZ ZAZ _ *Z	;_ _ -]6>_ Z T $VZ _ U4+	_ 4
V>_ VZ V
V.)Z 1
W ;_ Z Z 	W AY1Z _ 2Y	_ 
Y_ 0Y11Z 4
Z>	Z 	
Z_ ]3/A]. \].^ _ 0]
	_ 

]_ .]33^ 6
^ _ _^90	_9
___ c                     SU l         SU l        SU l        U R                  b  U R                  R	                  S5        gg)zClears the callback attributes.

This allows the request handler to be garbage collected more
quickly in CPython by breaking up reference cycles.
N)_write_callback_write_future_close_callbackrW   r   r   s    r   rc    HTTP1Connection._clear_callbacks6  s=      $!#;;"KK**40 #r!   callbackc                     Xl         g)a  Sets a callback that will be run when the connection is closed.

Note that this callback is slightly different from
`.HTTPMessageDelegate.on_connection_close`: The
`.HTTPMessageDelegate` method is called when the connection is
closed while receiving a message. This callback is used when
there is not an active delegate (for example, on the server
side this callback is used if the client closes the connection
after sending its request but before receiving all the
response.
N)r   r   r   s     r   r   "HTTP1Connection.set_close_callbackB  s
      (r!   c                     U R                   b  U R                   nS U l         U" 5         U R                  R                  5       (       d  [        U R                  S 5        U R	                  5         g r   )r   ra   r   r   rc   r   s     r   r   $HTTP1Connection._on_connection_closeP  sY     +++H#'D J""'')).t/B/BDIr!   c                     U R                   b  U R                   R                  5         U R                  5         U R                  R	                  5       (       d  [        U R                  S 5        g g r   )rW   r   rc   ra   r   r   r   s    r   r   HTTP1Connection.close\  sR    ;;"KK""'')).t/B/BDI *r!   c                     U R                  5         U R                  nSU l        U R                  R                  5       (       d  [	        U R                  S5        U$ )a  Take control of the underlying stream.

Returns the underlying `.IOStream` object and stops all further
HTTP processing.  May only be called during
`.HTTPMessageDelegate.headers_received`.  Intended for implementing
protocols like websockets that tunnel over an HTTP handshake.
N)rc   rW   ra   r   r   )r   rW   s     r   detachHTTP1Connection.detachc  sJ     	""'')).t/B/BDIr!   timeoutc                     Xl         g)zdSets the body timeout for a single request.

Overrides the value from `.HTTP1ConnectionParameters`.
N)r^   )r   r   s     r   set_body_timeout HTTP1Connection.set_body_timeoutr  s
    
 %r!   rL   c                     Xl         g)zgSets the body size limit for a single request.

Overrides the value from `.HTTP1ConnectionParameters`.
N)r]   )r   rL   s     r   set_max_body_size!HTTP1Connection.set_max_body_sizey  s
    
 ,r!   r   r   chunkFuture[None]c                    / nU R                   (       ao  [        U[        R                  5      (       d   eXl        UR                  [        US    SUS    S35      5        UR                  S;   =(       a    SU;  U l        GOs[        U[        R                  5      (       d   eU R                  c   eU R                  c   eXl        UR                  [        SUS   US	   4-  5      5        U R                  R                  S
:H  =(       ag    U R                  R                  S:g  =(       aG    UR                  S;  =(       a1    UR                  S:  =(       d    UR                  S:  =(       a    SU;  U l        U R                  R                  S
:X  a  U R                  (       a  SUS'   U R                  R                  S:X  a3  U R                  R                  SS5      R!                  5       S:X  a  SUS'   U R                  (       a  SUS'   U R                   (       dJ  U R                  R                  S:X  d(  [#        [        R                  U5      R                  S:X  a  SU l        O!SU;   a  ['        US   5      U l        OSU l        S UR)                  5        5       nUR+                  S U 5       5        U H+  n[,        R/                  U5      (       d  M  [1        SU-  5      e   SnU R2                  R5                  5       (       aF  [7        5       =opl        UR;                  [<        R>                  " 5       5        URA                  5         U$ [7        5       =opl        SRC                  U5      S-   nU(       a  XRE                  U5      -  nU R2                  RG                  U5      U l$        [K        U RH                  U RL                  5        U$ )z+Implements `.HTTPConnection.write_headers`.r       z	 HTTP/1.1)POSTPUTPATCHry   NzHTTP/1.1 %d %s   HTTP/1.1ru   )   rv   rw   rx   r   
ConnectionzHTTP/1.0 
keep-alivez
Keep-Alivechunkedrz   rv   c              3   Z   #    U  H!  u  p[        U5      S -   [        U5      -   v   M#     g7f)z: N)r   ).0nvs      r   	<genexpr>0HTTP1Connection.write_headers.<locals>.<genexpr>  s(      
=NTQJqMD :a=0=Ns   )+c              3   B   #    U  H  oR                  S 5      v   M     g7f)latin1N)encode)r   lines     r   r   r     s     D|t[[**|s   z+Illegal characters (CR or LF) in header: %r   
s   

)'rX   r:   r	   RequestStartLinerd   appendr   r   rg   r   rf   re   versionr   rb   r   lowerr   rh   	parse_intget_allextendCR_OR_LF_REsearch
ValueErrorrW   r   r   r   set_exceptionr
   r   	exceptionjoin_format_chunkr   ri   r   _on_write_complete)	r   r   r   r   linesheader_linesr   futuredatas	            r   write_headersHTTP1Connection.write_headers  sR    >>j(*C*CDDDD'1$LLAqAyIJK
 !!%== 4$G3 !
 j(*D*DEEEE++777((444(2%LL.*Q-A1OOPQ
 ((00J> 	4,,33v=	4 OO:5	4  __s*Djoo.D	4 %G3 !" ((00J>..(/% ((00J>))--lB?EEG<W(4%  +4G'(~~$$++v5H..
;@@CG/0D,(/8AQ9R/SD,/3D,
=D__=N
 	D|DDD!!$'' !NQU!UVV  ;;*0(2F'  !;!;!=>  +1(2F'<<&4D**511"&++"3"3D"9D$T%8%8$:Q:QRr!   c                 L   U R                   b^  U =R                   [        U5      -  sl         U R                   S:  a0  U R                  R                  5         [        R
                  " S5      eU R                  (       a'  U(       a   [        S[        U5      -  5      S-   U-   S-   $ U$ )Nr   z,Tried to write more data than Content-Lengthz%xr   )rh   lenrW   r   r	   HTTPOutputErrorrg   r   )r   r   s     r   r   HTTP1Connection._format_chunk  s    ++7,,E
:,//!3!!#..B    U s5z)*W4u<wFFLr!   c                    SnU R                   R                  5       (       aZ  [        5       =o l        U R                  R	                  [
        R                  " 5       5        U R                  R                  5         U$ [        5       =o l        U R                   R                  U R                  U5      5      U l
        [        U R                  U R                  5        U$ )zImplements `.HTTPConnection.write`.

For backwards compatibility it is allowed but deprecated to
skip `write_headers` and instead call `write()` with a
pre-encoded header block.
N)rW   r   r   r   r   r
   r   r   r   r   ri   r   r   )r   r   r   s      r   r   HTTP1Connection.write  s     ;;*0(2F',,X-G-G-IJ((*
  +1(2F'"&++"3"3D4F4Fu4M"ND$T%8%8$:Q:QRr!   c                    U R                   bl  U R                   S:w  a\  U R                  R                  5       (       d=  U R                  R                  5         [        R
                  " SU R                   -  5      eU R                  (       ad  U R                  R                  5       (       dE  U R                  R                  S5      U l        U R                  R                  U R                  5        SU l        U R                  (       d  SU l        U R                  R                  S5        U R                  c  U R                  S5        g[!        U R                  U R                  5        g)z$Implements `.HTTPConnection.finish`.Nr   z0Tried to write %d bytes less than Content-Lengths   0

T)rh   rW   r   r   r	   r   rg   r   ri   add_done_callbackr   r_   r`   rb   set_nodelay_finish_requestr   r   s    r   r   HTTP1Connection.finish  s    ,,800A5KK&&((KK**B223    ;;%%''&*kk&7&7&E###55d6M6MN# "")-D& 	%&  &$T%8%8$:N:NOr!   c                 l   UR                  5       nUb/  [        U[        R                  5      (       d  UR	                  5         U R
                  b8  U R
                  nS U l        U R                  R                  R                  U5        U R                  b   U R                  nS U l	        [        US 5        g g r   )r   r:   r
   r   resultr   rW   r~   add_callbackr   r   )r   r   excr   s       r   r   "HTTP1Connection._on_write_complete  s     ?:c83M3M#N#NMMO+++H#'D KK,,X6)''F!%D.vt< *r!   c                    U R                   R                  (       a  gUR                  S5      nUb  UR                  5       nUR                  S:X  a  US:g  $ SU;   d!  [        U5      (       d  [        USS 5      S;   a  US:H  $ g)	NFr   r   r   ry   r   )ru   GETr   )rY   rH   r   r   r   is_transfer_encoding_chunkedgetattr)r   r   r   connection_headers       r   r   HTTP1Connection._can_keep_alive&  s     ;;$$#KK5( 1 7 7 9+$//'+G44z8T2oE %44r!   c                 ,   U R                  5         U R                  (       d"  U R                  (       a  U R                  5         g U R                  R                  S5        U R                  R                  5       (       d  [        U R                  S 5        g g )NF)	rc   rX   rb   r   rW   r   ra   r   r   )r   r   s     r   r   HTTP1Connection._finish_request:  sg    ~~$"<"<JJL 	&""'')).t/B/BDI *r!   r   c                     [        UR                  S5      5      R                  S5      nUR                  S5      nUS U R	                  S5      n[
        R                  R                  X#S  5      nXE4$ )Nr   z

)r   decodelstripfindrstripr	   HTTPHeadersparse)r   r   data_streolr   r   s         r   r   HTTP1Connection._parse_headersE  si    
 dkk(34;;FCmmD!ds^**40
&&,,Xd^<""r!   r   c                   ^ SU;   a  SUS   ;   aX  [         R                  " SUS   5      m[        U4S jT 5       5      (       a  [        R                  " SUS   -  5      eTS   US'    [        US   5      n[        [        U5      U R                  :  a  [        R                  " S5      eOS n[        U5      nUS	:X  a(  U(       d  US
;  a  [        R                  " SU-  5      eSnU(       a  U R                  U5      $ Ub  U R                  XC5      $ U R                  (       a  U R                  U5      $ g ! [         a    [        R                  " SUS   -  5      ef = f)Nry   ,z,\s*c              3   2   >#    U  H  oTS    :g  v   M     g7f)r   Nr   )r   ipiecess     r   r   -HTTP1Connection._read_body.<locals>.<genexpr>]  s     6v!F1I~vs   z$Multiple unequal Content-Lengths: %rr   z*Only integer Content-Length is allowed: %szContent-Length too longr   )Nr   z*Response with code %d should not have body)resplitanyr	   r;   r   r   r   rR   r]   r   _read_chunked_body_read_fixed_bodyrX   _read_body_until_close)r   r   r   rk   content_length
is_chunkedr  s         @r   r   HTTP1Connection._read_bodyQ  s}    w&g.// '73C+DE6v666"11>!"234  -31I()09'BR:S0T C(4+>+>>--.GHH ? "N1':
3; ^9<--@4G  N**844%((BB>>..x88=  --@./0 s   *D7 7'Er  c                   #    US:  a  U R                   R                  [        U R                  R                  U5      SS9I S h  vN nU[        U5      -  nU R                  (       a  U R                  (       a6  [        [        5         UR                  U5      nUb
  UI S h  vN   S S S 5        US:  a  M  g g  Nt N! , (       d  f       N= f7f)Nr   Tpartial)rW   
read_bytesminrY   rI   r   r_   rX   r*   r   data_received)r   r  rk   bodyrets        r   r   HTTP1Connection._read_fixed_body  s      q //DKK**N;T 0  D c$i'N''4>>-g6"006C!		 7 q  " 76sC   ACB9ACB="B;#B='C7C;B==
CCc                   #    Sn U R                   R                  SSS9I S h  vN n [        [        US S 5      5      nUS:X  a@  U R                   R                  S5      I S h  vN nUS:w  a  [
        R                  " S	5      eg X$-  nX R                  :  a  [
        R                  " S
5      eUnU(       a  U R                   R                  [        X`R                  R                  5      SS9I S h  vN nU[        U5      -  nU R                  (       a  U R                  (       a6  [        [         5         UR#                  U5      nUb
  UI S h  vN   S S S 5        U(       a  M  U R                   R                  S5      I S h  vN nUS:X  d   eGM   GNq! [         a    [
        R                  " S5      ef = f GNW N No! , (       d  f       Ns= f NN7f)Nr   Tr   @   rr   zinvalid chunk sizer   z%improperly terminated chunked requestzchunked body too larger  )rW   
read_untilparse_hex_intr   r   r	   r;   r  r]   r  rY   rI   r   r_   rX   r*   r   r   )	r   rk   
total_sizechunk_len_str	chunk_lencrlfbytes_to_readr   r"  s	            r   r  "HTTP1Connection._read_chunked_body  s    
"&++"8"8B"8"OOMD)*]3B5G*HI	 A~![[33A667?"11?  #J///--.FGG%M"kk44{{'='=> 5   U+++t~~1':&44U;?"%II ;  - //22D7?"?; O  D--.BCCD 7 & ;:
 3s   !GFGF  $G$F;%BG2F>3AG6GG GG&GGG!F88G>G G
GGc                 ,  #    U R                   R                  5       I S h  vN nU R                  (       a  U R                  (       a7  [	        [
        5         UR                  U5      nUb
  UI S h  vN   S S S 5        g g  N^ N! , (       d  f       g = f7fr   )rW   read_until_closer_   rX   r*   r   r   )r   rk   r!  r"  s       r   r  &HTTP1Connection._read_body_until_close  sn      [[1133##t~~)'2,,T2?II 32 (6 4
  32s9   BA?5BB0B1B5BB
BB)r^   rg   r   rb   rh   ra   r]   ri   r`   rf   rd   re   r   r_   r   rZ   rX   rH   rY   rW   NNr"   r   )r   r   r#   N)r   zOptional[Future[None]]r#   N)/r$   r%   r&   r'   r?   r
   IOStreamrQ   r   rF   objectr   r	   HTTPMessageDelegater   ro   rn   rc   r   r   r   r   r   rS   r   rR   r   r   r   r   r  bytesr   r   r   r   r   r   r   r   strr   r   r  r  r  r(   r   r!   r   rU   rU   m   sn    7;$(7#!!7# 7# 23	7#
 &!7# 
7#r,h&B&B ,yQU ,xH,H,H xT xt
1(8HRX4F+G (D (
 J)) % %$ %,s ,t , "&	U(33X5O5OOPU %%U 	U
 
Un5 U  5 ^ $P@="33>F>R>R	(	J
#5 
#U38L8L3L-M 
#33 %%3 ..	3
 
)D/	"3j"!"-5-I-I"	" #1M1M  #RV  #D 44	r!   rU   c                       \ rS rSrSrS\R                  S\SS4S jrS\	\R                  \R                  4   S	\R                  S\\S      4S
 jrS\SS4S jrSS jrSS jrSrg)rm   i  zDWraps an `HTTPMessageDelegate` to decode ``Content-Encoding: gzip``.rk   rI   r#   Nc                 *    Xl         X l        S U l        g r   )	_delegate_chunk_size_decompressor)r   rk   rI   s      r   r   _GzipMessageDelegate.__init__  s    !%!r!   r   r   c                     UR                  SS5      R                  5       S:X  a'  [        5       U l        UR	                  SUS   5        US	 U R
                  R                  X5      $ )NzContent-Encodingr   gzipzX-Consumed-Content-Encoding)r   r   r   r<  addr:  r   )r   r   r   s      r   r   %_GzipMessageDelegate.headers_received  sa    
 ;;)2.446&@!1!3D KK5w?Q7RS*+~~..zCCr!   r   c                   #    U R                   (       a  UnU(       a  U R                   R                  X R                  5      nU(       a(  U R                  R	                  U5      nUb
  UI S h  vN   U R                   R
                  nU(       a  U(       d  [        R                  " S5      eU(       a  M  g g U R                  R	                  U5      nUb  UI S h  vN   g g  Ns N7f)Nz8encountered unconsumed gzip data without making progress)r<  rN   r;  r:  r   unconsumed_tailr	   r;   )r   r   compressed_datadecompressedr"  s        r   r   "_GzipMessageDelegate.data_received  s     #O!#11<<#%5%5   ..66|DC!		"&"4"4"D"D"<"11R  "/ ....u5C		  " s+   A*C$,C -AC$4%C$C"C$"C$c                     U R                   b,  U R                   R                  5       nU(       a  [        S5      eU R                  R	                  5       $ )Nz:decompressor.flush returned data; possible truncated input)r<  flushr   r:  r   )r   tails     r   r   _GzipMessageDelegate.finish  sL    )%%++-D !P  ~~$$&&r!   c                 6    U R                   R                  5       $ r   )r:  r   r   s    r   r   (_GzipMessageDelegate.on_connection_close  s    ~~1133r!   )r;  r<  r:  r"   )r$   r%   r&   r'   r?   r	   r5  rR   r   r   r   r   r  r   r   r   r6  r   r   r   r(   r   r!   r   rm   rm     s    N"!=!= "3 "SW "
D(33X5O5OOPD %%D 
)D/	"	D 4 *'4r!   rm   c            	           \ rS rSrSr  SS\R                  S\\   S\\	   SS4S jjr
SS	 jrS
\R                  SS4S jrS
\R                  SS4S jrSrg)HTTP1ServerConnectioni  zAn HTTP/1.x server.NrW   rY   rZ   r#   c                 P    Xl         Uc
  [        5       nX l        X0l        SU l        g)z
:arg stream: an `.IOStream`
:arg params: a `.HTTP1ConnectionParameters` or None
:arg context: an opaque application-defined object that is accessible
    as ``connection.context``
N)rW   rF   rY   rZ   _serving_future)r   rW   rY   rZ   s       r   r   HTTP1ServerConnection.__init__  s(     >.0F#r!   c                    #    U R                   R                  5         U R                  c   e U R                  I Sh  vN   g N! [         a     gf = f7f)z]Closes the connection.

Returns a `.Future` that resolves after the serving loop has exited.
N)rW   r   rP  	Exceptionr   s    r   r   HTTP1ServerConnection.close  sP     
 	 ##///	&&&& 		s8   *AA AA AA 
AAAArk   c                     [        U[        R                  5      (       d   e[        R                  " U R                  U5      5      nX l        U R                  R                  R                  US 5        g)z^Starts serving requests on this connection.

:arg delegate: a `.HTTPServerConnectionDelegate`
c                 "    U R                  5       $ r   )r   )fs    r   <lambda>5HTTP1ServerConnection.start_serving.<locals>.<lambda>,  s
    ahhjr!   N)
r:   r	   HTTPServerConnectionDelegater   convert_yielded_server_request_looprP  rW   r~   
add_future)r   rk   futs      r   start_serving#HTTP1ServerConnection.start_serving#  sY    
 (H$I$IJJJJ!!$";";H"EF"&&s,@Ar!   c                   #      [        U R                  SU R                  U R                  5      nUR	                  X5      n UR                  U5      I S h  vN nU(       d   UR                  U 5        g [        R"                  " S5      I S h  vN   M   N>! [        R                  [        R                  [        R                  4 a     UR                  U 5        g [         a$    UR                  5          UR                  U 5        g [         a9    [        R                   " SSS9  UR                  5          UR                  U 5        g f = f N! UR                  U 5        f = f7f)NTFr7   r8   r   )rU   rW   rY   rZ   start_requestro   r
   r   UnsatisfiableReadErrorasyncioCancelledErroron_closer   r   rS  r   r<   sleep)r   rk   connrequest_delegater"  s        r   r\  *HTTP1ServerConnection._server_request_loop.  s:    	$&t{{E4;;U#+#9#9$#E  $ 2 23C DDC  d# mmA&&&+  E..33** 
  d# ' JJL d# ! MM"6FJJL
 d# 'd#s   E'>E B BB E $E'6E EE B 3EE E'E7E 8E'
.E8E 9E'EE E$$E')rP  rZ   rY   rW   r2  r"   )r$   r%   r&   r'   r?   r
   r3  r   rF   r4  r   r   r	   rZ  r_  r\  r(   r   r!   r   rN  rN    s    
 7;$(	$!!$ 23$ &!	$
 
$&	Bh&K&K 	BPT 	B$ ==$	$r!   rN  z[0-9]+z[0-9a-fA-F]+sr#   c                 `    [         R                  U 5      c  [        SU -  5      e[        U 5      $ )z+Parse a non-negative integer from a string.znot an integer: %r)DIGITS	fullmatchr   rR   rk  s    r   r   r   P  s-    "-122q6Mr!   c                 b    [         R                  U 5      c  [        SU -  5      e[        U S5      $ )z7Parse a non-negative hexadecimal integer from a string.znot a hexadecimal integer: %r   )	HEXDIGITSrn  r   rR   ro  s    r   r(  r(  W  s0    1%81<==q":r!   r   c                     SU ;  a  gSU ;   a  [         R                  " S5      eU S   R                  5       S:X  a  g[         R                  " SU S   -  5      e)zReturns true if the headers specify Transfer-Encoding: chunked.

Raise httputil.HTTPInputError if any other transfer encoding is used.
rz   Fry   z6Message with both Transfer-Encoding and Content-Lengthr   Tz Unsupported Transfer-Encoding %s)r	   r;   r   )r   s    r   r   r   ^  sn     ')7" %%D
 	
 "#))+y8 
!
!*W5H-II r!   )1r?   rd  r@   r  rC   tornado.concurrentr   r   r   tornado.escaper   r   tornador   r	   r
   tornado.logr   r   tornado.utilr   typingr   r   r   r   r   r   r   compiler   rS  r   r*   rF   HTTPConnectionrU   r5  rm   rN  rm  rr  r7  rR   r   r(  r  rQ   r   r   r!   r   <module>r|     s   
   	  
 ,    ( ) K J Jjj"i 
" "6% %BQ	h-- Q	h;4877 ;4|J$ J$Z 
I	JJ'	  S S (*>*> 4 r!   