U
    lufӏ                     @   sH  d Z ddlZddlZddlZddlZddlmZmZmZ ddl	m
Z
mZ ddlmZ ddlmZ ddlmZ ddlmZmZ dd	lmZ dd
lmZmZmZmZmZmZmZ G dd deZG dd deZ G dd deZ!G dd dej"Z#G dd dej$Z%G dd deZ&e'dZ(e'dZ)e*e+dddZ,e*e+dddZ-dS )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Tuplec                   @   s   e Zd ZddddZdS )_QuietExceptionNreturnc                 C   s   d S N selfr   r   ;/tmp/pip-unpacked-wheel-bmg6zs32/tornado/http1connection.py__init__+   s    z_QuietException.__init__)__name__
__module____qualname__r   r   r   r   r   r   *   s   r   c                   @   sJ   e Zd ZdZejddddZddddZd	ee	 e
jdd
ddZdS )_ExceptionLoggingContextzUsed with the ``with`` statement when calling delegate methods to
    log any exceptions with the given logger.  Any exceptions caught are
    converted to _QuietException
    N)loggerr   c                 C   s
   || _ d S r   )r!   )r   r!   r   r   r   r   5   s    z!_ExceptionLoggingContext.__init__r   c                 C   s   d S r   r   r   r   r   r   	__enter__8   s    z"_ExceptionLoggingContext.__enter__zOptional[Type[BaseException]])typvaluetbr   c                 C   s2   |d k	r.|d k	st | jjd|||fd td S )NUncaught exceptionexc_info)AssertionErrorr!   errorr   )r   r#   r$   r%   r   r   r   __exit__;   s    z!_ExceptionLoggingContext.__exit__)r   r   r   __doc__loggingLoggerr   r"   r   BaseExceptiontypesTracebackTyper+   r   r   r   r   r    /   s   r    c                
   @   sB   e Zd ZdZdeee ee ee ee ee eddddZdS )HTTP1ConnectionParametersz?Parameters for `.HTTP1Connection` and `.HTTP1ServerConnection`.FN)no_keep_alive
chunk_sizemax_header_sizeheader_timeoutmax_body_sizebody_timeout
decompressr   c                 C   s6   || _ |pd| _|pd| _|| _|| _|| _|| _dS )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)r3   r4   r5   r6   r7   r8   r9   )r   r3   r4   r5   r6   r7   r8   r9   r   r   r   r   J   s    

z"HTTP1ConnectionParameters.__init__)FNNNNNF)	r   r   r   r,   boolr   intfloatr   r   r   r   r   r2   G   s$          r2   c                   @   s  e Zd ZdZd>ejeee ee	 ddddZ
ejee dddZejedd	d
ZddddZeeg df  ddddZddddZddddZejdddZeddddZeddddZd?eejejf ejee dddd Zeed!d"d#Z edd!d$d%Z!ddd&d'Z"ddd(d)d*Z#ejejed+d,d-Z$d.dd(d/d0Z%ee&e'ejf d1d2d3Z(eejejeed  d4d5d6Z)eejdd7d8d9Z*ejddd:d;Z+ejddd<d=Z,dS )@HTTP1ConnectionzImplements 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                 C   s   || _ || _|dkrt }|| _|| _|j| _| jjdk	rB| jjn| jj| _| jj	| _
d| _d| _t | _d| _|   d| _d| _d| _d| _d| _d| _dS )a  
        :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)r?   r>   r2   r@   rA   r3   r7   max_buffer_size_max_body_sizer8   _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   r>   r?   r@   rA   r   r   r   r   o   s.    


zHTTP1Connection.__init__delegater   c                 C   s    | j jrt|| j j}| |S )az  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.
        )r@   r9   _GzipMessageDelegater4   _read_message)r   rQ   r   r   r   read_response   s    zHTTP1Connection.read_responsec                    s  d}zzD| jjd| jjd}| jjd kr8|I d H }nVz,t	j
| jj | jj |tjdI d H }W n( t	jk
r   |   Y W W .dS X | |\}}| jrt|}|| _|}d| _n*t|}	|	| _|| _|	}| |	| | _d}t t& |||}
|
d k	r|
I d H  W 5 Q R X | jd kr<d}W W ~dS d}| jrt|tjsZt| jd k	rx| jjdkrxd}|j }|dkrd}d|  krd	k r n n2d
|ksd|krt!d| | "|I d H  n$|#ddkr | j$s | j%d |s| &| jr|j nd||}|d k	r| j'd krD|I d H  nfz*t	j
| jj | j' |tjdI d H  W n: t	jk
r   t()d| j* | j  Y W W dS X d| _+| j$r| jrd}t t |,  W 5 Q R X | j-. s| jd k	r| j/ s| j0| j1 | j-I d H  | jr6| jr6|   | jd krLW W ndS W nb tj!k
r } z@t()d| j*| | js| j%dI d H  |   W Y W dS d }~X Y nX W 5 |rt t |  W 5 Q R X d }|   X dS )NFs   ?
?
Z	max_bytes)Zquiet_exceptionsTHEAD0  d      Content-LengthTransfer-Encodingz!Response code %d cannot have bodyZ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

)2r    r   on_connection_closerI   r>   Zread_until_regexr@   r5   r6   r   Zwith_timeoutio_looptimer	   StreamClosedErrorTimeoutErrorclose_parse_headersr?   r   Zparse_response_start_linerK   rH   Zparse_request_start_linerJ   rL   _can_keep_aliveheaders_received
isinstanceResponseStartLiner)   methodcodeHTTPInputErrorrS   getrE   write
_read_bodyrD   r
   inforA   rF   finishrG   doneclosedset_close_callback_on_connection_close)r   rQ   Zneed_delegate_closeZheader_futureZheader_dataZstart_line_strheadersZresp_start_line
start_lineZreq_start_lineZheader_recv_futureZ	skip_bodyrh   Zbody_futureer   r   r   rS      s     

 



  


 

zHTTP1Connection._read_messager   c                 C   s,   d| _ d| _d| _| jdk	r(| jd dS )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_callbackr>   rq   r   r   r   r   rI   1  s
    
z HTTP1Connection._clear_callbacks)callbackr   c                 C   s
   || _ dS )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)rx   r   ry   r   r   r   rq   =  s    z"HTTP1Connection.set_close_callbackc                 C   s>   | j d k	r| j }d | _ |  | j s2t| jd  |   d S r   )rx   rG   ro   r   rI   rz   r   r   r   rr   K  s    

z$HTTP1Connection._on_connection_closec                 C   s6   | j d k	r| j   |   | j s2t| jd  d S r   )r>   ra   rI   rG   ro   r   r   r   r   r   ra   W  s
    


zHTTP1Connection.closec                 C   s.   |    | j}d| _| j s*t| jd |S )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)rI   r>   rG   ro   r   )r   r>   r   r   r   detach^  s    
zHTTP1Connection.detach)timeoutr   c                 C   s
   || _ dS )ztSets the body timeout for a single request.

        Overrides the value from `.HTTP1ConnectionParameters`.
        N)rD   )r   r|   r   r   r   set_body_timeoutm  s    z HTTP1Connection.set_body_timeout)r7   r   c                 C   s
   || _ dS )zwSets the body size limit for a single request.

        Overrides the value from `.HTTP1ConnectionParameters`.
        N)rC   )r   r7   r   r   r   set_max_body_sizet  s    z!HTTP1Connection.set_max_body_sizezFuture[None])rt   rs   chunkr   c           	      C   s|  g }| j rht|tjst|| _|td|d |d f  |jdkobd|kobd|kpb|d dk| _	nt|tj
sxt| jdk	st| jdk	st|| _|td	|d |d
 f  | jjdko| jjdko|jdko|jdk s|jdkod|kod|k| _	| jjdkr| jrd|d< | jjdkrL| jdd dkrLd|d< | j	r\d|d< | j s| jjdksttj
|jdkrd| _n d|krt|d | _nd| _dd | D }|dd |D  |D ] }d|krtdt| qd}| j r.t  }| _|t  |   nJt  }| _d!|d }|r\|| "|7 }| j#|| _$t%| j$| j& |S )z+Implements `.HTTPConnection.write_headers`.z%s %s HTTP/1.1r      )POSTPUTPATCHrZ   r[   chunkedNzHTTP/1.1 %d %s   HTTP/1.1rV   )   rW   rX   rY   ra   
ConnectionzHTTP/1.0 
keep-alivez
Keep-AliverW   c                 s   s&   | ]\}}t |d  t | V  qdS )z: N)r   ).0nvr   r   r   	<genexpr>  s    z0HTTP1Connection.write_headers.<locals>.<genexpr>c                 s   s   | ]}| d V  qdS )latin1N)encode)r   liner   r   r   r     s        
zNewline in header:    
s   

)'r?   re   r   RequestStartLiner)   rJ   appendr   rg   rM   rf   rL   rK   versionrh   rH   rj   lowerr   rN   	parse_intget_allextend
ValueErrorreprr>   rp   r   rw   set_exceptionr	   r_   	exceptionjoin_format_chunkrk   rO   r   _on_write_complete)	r   rt   rs   r   linesZheader_linesr   futuredatar   r   r   write_headers{  s    

	
	





zHTTP1Connection.write_headersr   r   c                 C   sh   | j d k	r:|  j t|8  _ | j dk r:| j  td| jr`|r`tdt| d | d S |S d S )Nr   z,Tried to write more data than Content-Lengthz%xr   )rN   lenr>   ra   r   HTTPOutputErrorrM   r   )r   r   r   r   r   r     s    



zHTTP1Connection._format_chunkc                 C   sh   d}| j  r6t  }| _| jt  | j  n.t  }| _| j | 	|| _
t| j
| j |S )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)r>   rp   r   rw   r   r	   r_   r   rk   r   rO   r   r   )r   r   r   r   r   r   rk     s    
zHTTP1Connection.writec                 C   s   | j dk	r8| j dkr8| j s8| j  td| j  | jrd| j sd| jd| _| j	| j
 d| _| jsvd| _| jd | jdkr| d nt| j| j dS )z$Implements `.HTTPConnection.finish`.Nr   z0Tried to write %d bytes less than Content-Lengths   0

T)rN   r>   rp   ra   r   r   rM   rk   rO   Zadd_done_callbackr   rE   rF   rH   set_nodelay_finish_requestr   r   r   r   r   rn     s.    


zHTTP1Connection.finish)r   r   c                 C   sl   |  }|d k	r$t|tjs$|  | jd k	rH| j}d | _| jj| | j	d k	rh| j	}d | _	t
|d  d S r   )r   re   r	   r_   resultrv   r>   r]   Zadd_callbackrw   r   )r   r   excry   r   r   r   r     s    

z"HTTP1Connection._on_write_completert   rs   r   c                 C   sp   | j jrdS |d}|d k	r&| }|jdkr8|dkS d|ksd|dd dksdt|d	d d
krl|dkS dS )NFr   r   ra   rZ   r[   r   r   rg   )rV   GETr   )r@   r3   rj   r   r   getattr)r   rt   rs   Zconnection_headerr   r   r   rc   '  s    

zHTTP1Connection._can_keep_alivezOptional[Future[None]]c                 C   sF   |    | js | jr |   d S | jd | j sBt| jd  d S )NF)	rI   r?   rH   ra   r>   r   rG   ro   r   )r   r   r   r   r   r   ;  s    
zHTTP1Connection._finish_request)r   r   c                 C   sL   t |dd}|d}|d | d}tj||d  }||fS )Nr   z

)r   decodelstripfindrstripr   HTTPHeadersparse)r   r   Zdata_streolrt   rs   r   r   r   rb   F  s
    
zHTTP1Connection._parse_headers)rh   rs   rQ   r   c                    s8  d|krd|krt dd|d krjtd|d  t fdd D r^t d|d   d	 |d< zt|d }W n& tk
r   t d
|d  Y nX tt|| j	krt dnd }|dkrd|ks|dkrt d| d	}|d k	r| 
||S |dd dkr"| |S | jr4| |S d S )NrZ   r[   z7Response with both Transfer-Encoding and Content-Length,z,\s*c                 3   s   | ]}| d  kV  qdS )r   Nr   )r   ipiecesr   r   r   e  s     z-HTTP1Connection._read_body.<locals>.<genexpr>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 bodyr   r   )r   ri   resplitanyr   r   r   r;   rC   _read_fixed_bodyrj   r   _read_chunked_bodyr?   _read_body_until_close)r   rh   rs   rQ   content_lengthr   r   r   rl   R  sL    



zHTTP1Connection._read_body)r   rQ   r   c              	      sv   |dkrr| j jt| jj|ddI d H }|t|8 }| jr@| jr tt	" |
|}|d k	rf|I d H  W 5 Q R X q d S )Nr   Tpartial)r>   
read_bytesminr@   r4   r   rE   r?   r    r   data_received)r   r   rQ   bodyretr   r   r   r     s     

z HTTP1Connection._read_fixed_bodyc           	   	      s8  d}| j jdddI d H }ztt|d d }W n tk
rP   tdY nX |dkr| j dI d H }|dkr~tdd S ||7 }|| jkrtd	|}|r| j jt	|| j
jd
dI d H }|t|8 }| jr| jrtt$ ||}|d k	r|I d H  W 5 Q R X q| j dI d H }|dkstqd S )Nr   r   @   rU   zinvalid chunk sizer   z%improperly terminated chunked requestzchunked body too largeTr   )r>   Z
read_untilparse_hex_intr   r   r   ri   r   rC   r   r@   r4   r   rE   r?   r    r   r   r)   )	r   rQ   Z
total_sizeZchunk_len_strZ	chunk_lenZcrlfZbytes_to_readr   r   r   r   r   r     s<    

 


z"HTTP1Connection._read_chunked_bodyc              	      sP   | j  I d H }| jr| jrLtt" ||}|d k	rB|I d H  W 5 Q R X d S r   )r>   Zread_until_closerE   r?   r    r   r   )r   rQ   r   r   r   r   r   r     s    

z&HTTP1Connection._read_body_until_close)NN)N)-r   r   r   r,   r	   IOStreamr:   r   r2   objectr   r   HTTPMessageDelegater   rT   rS   rI   r   rq   rr   ra   r{   r<   r}   r;   r~   r   r   rf   r   bytesr   r   rk   rn   r   rc   r   r   strrb   rl   r   r   r   r   r   r   r   r=   h   sb   
  9z ]  
; #r=   c                   @   sx   e Zd ZdZejeddddZeej	ej
f ejeed  dddZedd	d
dZddddZddddZdS )rR   zDWraps an `HTTPMessageDelegate` to decode ``Content-Encoding: gzip``.N)rQ   r4   r   c                 C   s   || _ || _d | _d S r   )	_delegate_chunk_size_decompressor)r   rQ   r4   r   r   r   r     s    z_GzipMessageDelegate.__init__r   c                 C   s@   | dd dkr2t | _|d|d  |d= | j||S )NzContent-Encodingr   gzipzX-Consumed-Content-Encoding)rj   r   r   r   addr   rd   )r   rt   rs   r   r   r   rd     s
    z%_GzipMessageDelegate.headers_receivedr   c                    s   | j r^|}|r|| j || j}|r@| j|}|d k	r@|I d H  | j j}|r
|s
tdq
n| j|}|d k	r||I d H  d S )Nz8encountered unconsumed gzip data without making progress)r   r9   r   r   r   unconsumed_tailr   ri   )r   r   Zcompressed_datadecompressedr   r   r   r   r     s&     
z"_GzipMessageDelegate.data_receivedr   c                 C   s*   | j d k	r | j  }|r td| j S )Nz:decompressor.flush returned data; possible truncated input)r   flushr   r   rn   )r   tailr   r   r   rn     s    

z_GzipMessageDelegate.finishc                 C   s
   | j  S r   )r   r\   r   r   r   r   r\     s    z(_GzipMessageDelegate.on_connection_close)r   r   r   r,   r   r   r;   r   r   r   rf   r   r   r   rd   r   r   rn   r\   r   r   r   r   rR     s   
rR   c                   @   sb   e Zd ZdZdejee ee ddddZ	ddddZ
ejdd	d
dZejdd	ddZdS )HTTP1ServerConnectionzAn HTTP/1.x server.N)r>   r@   rA   r   c                 C   s*   || _ |dkrt }|| _|| _d| _dS )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)r>   r2   r@   rA   _serving_future)r   r>   r@   rA   r   r   r   r     s    zHTTP1ServerConnection.__init__r   c                    sB   | j   | jdk	stz| jI dH  W n tk
r<   Y nX dS )zmCloses the connection.

        Returns a `.Future` that resolves after the serving loop has exited.
        N)r>   ra   r   r)   	Exceptionr   r   r   r   ra     s    
zHTTP1ServerConnection.closerP   c                 C   s>   t |tjstt| |}|| _| jj	
|dd  dS )znStarts serving requests on this connection.

        :arg delegate: a `.HTTPServerConnectionDelegate`
        c                 S   s   |   S r   )r   )fr   r   r   <lambda>2      z5HTTP1ServerConnection.start_serving.<locals>.<lambda>N)re   r   HTTPServerConnectionDelegater)   r   Zconvert_yielded_server_request_loopr   r>   r]   Z
add_future)r   rQ   Zfutr   r   r   start_serving)  s    z#HTTP1ServerConnection.start_servingc              
      s   zt| jd| j| j}|| |}z||I d H }W nt tjtj	t
jfk
r\   Y W vd S  tk
r|   |  Y W Vd S  tk
r   tjddd |  Y W (d S X |sW d S t
dI d H  qW 5 | |  X d S )NFr&   Tr'   r   )Zon_closer=   r>   r@   rA   Zstart_requestrT   r	   r_   ZUnsatisfiableReadErrorasyncioZCancelledErrorr   ra   r   r
   r*   sleep)r   rQ   connZrequest_delegater   r   r   r   r   4  s,    

z*HTTP1ServerConnection._server_request_loop)NN)r   r   r   r,   r	   r   r   r2   r   r   ra   r   r   r   r   r   r   r   r   r     s     r   z[0-9]+z[0-9a-fA-F]+)sr   c                 C   s"   t | dkrtd|  t| S )z+Parse a non-negative integer from a string.Nznot an integer: %r)DIGITS	fullmatchr   r;   r   r   r   r   r   V  s    r   c                 C   s$   t | dkrtd|  t| dS )z7Parse a non-negative hexadecimal integer from a string.Nznot a hexadecimal integer: %r   )	HEXDIGITSr   r   r;   r   r   r   r   r   ]  s    r   ).r,   r   r-   r   r0   Ztornado.concurrentr   r   r   Ztornado.escaper   r   Ztornador   r   r	   Ztornado.logr
   r   Ztornado.utilr   typingr   r   r   r   r   r   r   r   r   r   r    r2   HTTPConnectionr=   r   rR   r   compiler   r   r   r;   r   r   r   r   r   r   <module>   s4   $!    c>M

