a
    õ£Be”l  ã                   @   s°  d dl mZ d dlmZmZ d dlmZmZmZm	Z	m
Z
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 d dlmZmZmZmZ d d	lmZ d d
lmZ d dlZd dlZd dl Z d dl!Z!d dl"Z"d dl#Z#d dl$Z$d dl%Z%d dl&Z&d dl'm(Z( d dl)Z*d dl+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1 d dl2m3Z3 d dl+Z+e+j4�rPd dl+m5Z5m6Z6m7Z7 G dd„ deƒZ8G dd„ deƒZ9G dd„ deƒZ:G dd„ dej;ƒZ<e=dk�r¬e >e:¡ e	ƒ  dS )é    )Ú_unicode)ÚgenÚversion)ÚHTTPResponseÚ	HTTPErrorÚAsyncHTTPClientÚmainÚ_RequestProxyÚHTTPRequest)Úhttputil)ÚHTTP1ConnectionÚHTTP1ConnectionParameters)ÚIOLoop)ÚStreamClosedErrorÚIOStream)ÚResolverÚOverrideResolverÚ_client_ssl_defaultsÚis_valid_ip)Úgen_log)Ú	TCPClientN)ÚBytesIO)ÚDictÚAnyÚCallableÚOptionalÚTypeÚUnion)ÚTracebackType)ÚDequeÚTupleÚListc                       s6   e Zd ZdZeddœ‡ fdd„Zedœdd„Z‡  ZS )	ÚHTTPTimeoutErrorzÃError raised by SimpleAsyncHTTPClient on timeout.

    For historical reasons, this is a subclass of `.HTTPClientError`
    which simulates a response code of 599.

    .. versionadded:: 5.1
    N©ÚmessageÚreturnc                    s   t ƒ jd|d� d S ©NéW  ©r$   ©ÚsuperÚ__init__©Úselfr$   ©Ú	__class__© ú\/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/tornado/simple_httpclient.pyr+   5   s    zHTTPTimeoutError.__init__©r%   c                 C   s
   | j pdS )NÚTimeoutr(   ©r-   r0   r0   r1   Ú__str__8   s    zHTTPTimeoutError.__str__©Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ústrr+   r5   Ú__classcell__r0   r0   r.   r1   r"   ,   s   r"   c                       s6   e Zd ZdZeddœ‡ fdd„Zedœdd„Z‡  ZS )	ÚHTTPStreamClosedErroraZ  Error raised by SimpleAsyncHTTPClient when the underlying stream is closed.

    When a more specific exception is available (such as `ConnectionResetError`),
    it may be raised instead of this one.

    For historical reasons, this is a subclass of `.HTTPClientError`
    which simulates a response code of 599.

    .. versionadded:: 5.1
    Nr#   c                    s   t ƒ jd|d� d S r&   r)   r,   r.   r0   r1   r+   H   s    zHTTPStreamClosedError.__init__r2   c                 C   s
   | j pdS )NúStream closedr(   r4   r0   r0   r1   r5   K   s    zHTTPStreamClosedError.__str__r6   r0   r0   r.   r1   r=   <   s   r=   c                
       s  e Zd ZdZdeeeeef  eee eeee	f  ee ee ddœ‡ fdd„Z
ddœ‡ fd	d
„Zeeegdf ddœdd„Zddœdd„Zedœdd„Zeeg df eegdf ddœdd„Zeddœdd„Zeddœdd„Zdeee ddœdd„Z‡  ZS )ÚSimpleAsyncHTTPClienta«  Non-blocking HTTP client with no external dependencies.

    This class implements an HTTP 1.1 client on top of Tornado's IOStreams.
    Some features found in the curl-based AsyncHTTPClient are not yet
    supported.  In particular, proxies are not supported, connections
    are not reused, and callers cannot select the network interface to be
    used.

    This implementation supports the following arguments, which can be passed
    to ``configure()`` to control the global singleton, or to the constructor
    when ``force_instance=True``.

    ``max_clients`` is the number of concurrent requests that can be
    in progress; when this limit is reached additional requests will be
    queued. Note that time spent waiting in this queue still counts
    against the ``request_timeout``.

    ``defaults`` is a dict of parameters that will be used as defaults on all
    `.HTTPRequest` objects submitted to this client.

    ``hostname_mapping`` is a dictionary mapping hostnames to IP addresses.
    It can be used to make local DNS changes when modifying system-wide
    settings like ``/etc/hosts`` is not possible or desirable (e.g. in
    unittests). ``resolver`` is similar, but using the `.Resolver` interface
    instead of a simple mapping.

    ``max_buffer_size`` (default 100MB) is the number of bytes
    that can be read into memory at once. ``max_body_size``
    (defaults to ``max_buffer_size``) is the largest response body
    that the client will accept.  Without a
    ``streaming_callback``, the smaller of these two limits
    applies; with a ``streaming_callback`` only ``max_body_size``
    does.

    .. versionchanged:: 4.2
        Added the ``max_body_size`` argument.
    é
   Né  @)Úmax_clientsÚhostname_mappingÚmax_buffer_sizeÚresolverÚdefaultsÚmax_header_sizeÚmax_body_sizer%   c                    s†   t ƒ j|d� || _t ¡ | _i | _i | _|| _|| _	|| _
|rN|| _d| _ntƒ | _d| _|d urtt| j|d�| _t| jd�| _d S )N)rF   FT)rE   Úmapping)rE   )r*   Ú
initializerB   ÚcollectionsÚdequeÚqueueÚactiveÚwaitingrD   rG   rH   rE   Úown_resolverr   r   r   Ú
tcp_client)r-   rB   rC   rD   rE   rF   rG   rH   r.   r0   r1   rJ   v   s*    
ÿÿÿÿz SimpleAsyncHTTPClient.initializer2   c                    s(   t ƒ  ¡  | jr| j ¡  | j ¡  d S ©N)r*   ÚcloserP   rE   rQ   r4   r.   r0   r1   rS   œ   s    

zSimpleAsyncHTTPClient.close)ÚrequestÚcallbackr%   c                 C   sÊ   t ƒ }| j |||f¡ |jd us&J ‚|jd us4J ‚d }t| jƒ| jkrŠt|j|jƒp`|jp`|j}|rŠ| j	 
| j	 ¡ | t | j|d¡¡}|||f| j|< |  ¡  | jrÆt dt| jƒt| jƒf ¡ d S )Nzin request queuezImax_clients limit reached, request queued. %d active, %d queued requests.)ÚobjectrM   ÚappendÚconnect_timeoutÚrequest_timeoutÚlenrN   rB   ÚminÚio_loopÚadd_timeoutÚtimeÚ	functoolsÚpartialÚ_on_timeoutrO   Ú_process_queuer   Údebug)r-   rT   rU   ÚkeyÚtimeout_handleÚtimeoutr0   r0   r1   Ú
fetch_impl¢   s0    ÿýþÿÿz SimpleAsyncHTTPClient.fetch_implc                 C   sl   | j rht| jƒ| jk rh| j  ¡ \}}}|| jvr2q |  |¡ ||f| j|< t | j	|¡}|  
|||¡ q d S rR   )rM   rZ   rN   rB   ÚpopleftrO   Ú_remove_timeoutr_   r`   Ú_release_fetchÚ_handle_request)r-   rd   rT   rU   Úrelease_callbackr0   r0   r1   rb   ½   s    

z$SimpleAsyncHTTPClient._process_queuec                 C   s   t S rR   )Ú_HTTPConnectionr4   r0   r0   r1   Ú_connection_classÇ   s    z'SimpleAsyncHTTPClient._connection_class)rT   rl   Úfinal_callbackr%   c              	   C   s&   |   ¡ | |||| j| j| j| jƒ d S rR   )rn   rD   rQ   rG   rH   )r-   rT   rl   ro   r0   r0   r1   rk   Ê   s    øz%SimpleAsyncHTTPClient._handle_request)rd   r%   c                 C   s   | j |= |  ¡  d S rR   )rN   rb   )r-   rd   r0   r0   r1   rj   Û   s    z$SimpleAsyncHTTPClient._release_fetchc                 C   s:   || j v r6| j | \}}}|d ur.| j |¡ | j |= d S rR   )rO   r\   Úremove_timeout)r-   rd   rT   rU   re   r0   r0   r1   ri   ß   s
    
z%SimpleAsyncHTTPClient._remove_timeout)rd   Úinfor%   c                 C   sn   | j | \}}}| j |||f¡ |r0d |¡nd}t|dt|ƒ| j ¡ |j d�}| j 	||¡ | j |= dS )zâTimeout callback of request.

        Construct a timeout HTTPResponse when a timeout occurs.

        :arg object key: A simple object to mark the request.
        :info string key: More detailed timeout information.
        úTimeout {0}r3   r'   )ÚerrorÚrequest_timeN)
rO   rM   ÚremoveÚformatr   r"   r\   r^   Ú
start_timeÚadd_callback)r-   rd   rq   rT   rU   re   Úerror_messageZtimeout_responser0   r0   r1   ra   æ   s    üz!SimpleAsyncHTTPClient._on_timeout)r@   NrA   NNNN)N)r7   r8   r9   r:   Úintr   r   r;   r   r   rJ   rS   r
   r   r   rg   rb   Útypern   rk   rV   rj   ri   ra   r<   r0   r0   r.   r1   r?   O   s@   (       ø÷&þ

ûr?   c                
   @   sd  e Zd Zeg d¢ƒZee eeg df ee	gdf e
ee
e
ddœ	dd„Zddœdd„Zeedeeef ejf d	œd
d„Zd.ee ddœdd„Zddœdd„Zeedœdd„Zeddœdd„Zddœdd„Ze	ddœdd„Zdee ee edœdd„Z ddœd d!„Z!ee"j#e"j$f e"j%dd"œd#d$„Z&edœd%d&„Z'ddœd'd(„Z(ddœd)d*„Z)e*dd+œd,d-„Z+dS )/rm   )ÚGETÚHEADÚPOSTÚPUTÚDELETEÚPATCHÚOPTIONSN)	ÚclientrT   rl   ro   rD   rQ   rG   rH   r%   c	           	      C   s–   t  ¡ | _| j ¡ | _t ¡ | _|| _|| _|| _|| _	|| _
|| _|| _|| _d | _d | _g | _d | _d | _d | _t  ¡  t |  ¡ ¡dd„ ¡ d S )Nc                 S   s   |   ¡ S rR   ©Úresult©Úfr0   r0   r1   Ú<lambda>  ó    z*_HTTPConnection.__init__.<locals>.<lambda>)r   Úcurrentr\   r^   rw   Ústart_wall_timerƒ   rT   rl   ro   rD   rQ   rG   rH   ÚcodeÚheadersÚchunksÚ_decompressorÚ_timeoutÚ	_sockaddrZ
add_futurer   Zconvert_yieldedÚrun)	r-   rƒ   rT   rl   ro   rD   rQ   rG   rH   r0   r0   r1   r+     s(    

ÿz_HTTPConnection.__init__r2   c                 Ã   s8  �z
t j t| jjƒ¡| _| jjdvr6td| jj ƒ‚| jj	}d|v rV| 
d¡\}}}t |¡\}}|d u r€| jjdkr|dnd}t d|¡r˜|dd	… }|| _| jjd
u r²tj}ntj}|  | jj¡}d }| jjrút| jjƒrè| jj}ntd| jjf ƒ‚| jj�r"| jj�r"t| jj| jjƒ}	n,| jj�r6| jj}	n| jj�rJ| jj}	nd}	|	�rt| j | j|	 t | jd¡¡| _ | j!j"||||| j#|d�I d H }
| j$d u �r®|
 %¡  W d S |
| _&| j& '| j(¡ |  )¡  | j$d u �rÜW d S | jj�r
| j | j| jj t | jd¡¡| _ | jj*| j+v�r4| jj,�s4t-d| jj* ƒ‚dD ]"}t.| j|d ƒ�r8t/d| ƒ‚�q8d| jj0v�rvd| jj0d< d| jj0v�r¾d| jj	v �r®| jj	 
d¡d	 | jj0d< n| jj	| jj0d< d\}}| jj1d u�rè| jj1| jj2 }}n$| jj3d u�r| jj3}| jj4�p
d}|d u�rb|d u�s$J ‚| jj5dv�r@td| jj5ƒ‚dtt6 7t 8||¡¡ƒ | jj0d< | jj9�r~| jj9| jj0d< n&| jj0 :d¡d u �r¤d ;t<¡| jj0d< | jj,�s
| jj*dv }| jj=d u�pÒ| jj>d u}|�rà|�rì|�r
|�s
td|�rúd nd| jj*f ƒ‚| jj?�r d!| jj0d"< | jj=d u�rFt@tA| jj=ƒƒ| jj0d#< | jj*d$k�rnd%| jj0v�rnd&| jj0d%< | jjB�r„d'| jj0d(< | jjC�p�d)| jjD�r¦d*| jjD nd }|  E|
¡| _Ft G| jj*|d¡}| jF H|| jj0¡ | jj?�rú| jF I| ¡I d H  n|  Jd+¡I d H  W n& tK�y2   | jLtM N¡ Ž �s.‚ Y n0 d S ),N)ÚhttpÚhttpszUnsupported url scheme: %sú@r”   i»  éP   z^\[.*\]$é   éÿÿÿÿFz?Unrecognized IPv4 or IPv6 address for network_interface, got %rr   zwhile connecting)ÚafÚssl_optionsrD   Ú	source_ipzduring requestzunknown method %s)Z
proxy_hostZ
proxy_portZproxy_usernameZproxy_passwordZproxy_auth_modez%s not supportedÚ
ConnectionrS   ÚHost)NNÚ )NÚbasiczunsupported auth_mode %szBasic ÚAuthorizationz
User-Agentz
Tornado/{})r~   r�   r   zLBody must %sbe None for method %s (unless allow_nonstandard_methods is true)znot z100-continueZExpectúContent-Lengthr~   úContent-Typez!application/x-www-form-urlencodedÚgzipzAccept-Encodingú/ú?T)OÚurllibÚparseÚurlsplitr   rT   ÚurlÚparsedÚschemeÚ
ValueErrorÚnetlocÚ
rpartitionr   Zsplit_host_and_portÚreÚmatchZparsed_hostnameZ
allow_ipv6ÚsocketÚAF_INETÚ	AF_UNSPECÚ_get_ssl_optionsZnetwork_interfacer   rX   rY   r[   r\   r]   rw   r_   r`   ra   r�   rQ   ÚconnectrD   ro   rS   ÚstreamZset_close_callbackÚon_connection_closeri   ÚmethodÚ_SUPPORTED_METHODSZallow_nonstandard_methodsÚKeyErrorÚgetattrÚNotImplementedErrorr�   ÚusernameÚpasswordZauth_usernameZauth_passwordZ	auth_modeÚbase64Ú	b64encodeZencode_username_passwordÚ
user_agentÚgetrv   r   ÚbodyÚbody_producerÚexpect_100_continuer;   rZ   Údecompress_responseÚpathÚqueryÚ_create_connectionÚ
connectionÚRequestStartLineZwrite_headersÚread_responseÚ_write_bodyÚ	ExceptionÚ_handle_exceptionÚsysÚexc_info)r-   r­   ZuserpassÚ_ÚhostÚportr™   rš   r›   rf   r¶   rd   r½   r¾   Zbody_expectedZbody_presentÚreq_pathÚ
start_liner0   r0   r1   r’   "  s   
ÿÿÿ



þú	
þÿþÿ

ÿÿ


þÿÿþÿ

ÿ
þþÿ
ÿ
z_HTTPConnection.run)r«   r%   c                 C   sº   |dkr¶| j jd ur| j jS | j jrL| j jd u rL| j jd u rL| j jd u rLtS tjtj	j
| j jd�}| j jsxd|_tj|_| j jd ur˜| | j j| j j¡ ttdƒr²| jtjO  _|S d S )Nr”   )ÚcafileFÚOP_NO_COMPRESSION)rT   rš   Zvalidate_certÚca_certsÚclient_certZ
client_keyr   ÚsslÚcreate_default_contextÚPurposeÚSERVER_AUTHÚcheck_hostnameÚ	CERT_NONEÚverify_modeÚload_cert_chainÚhasattrÚoptionsrØ   )r-   r«   Zssl_ctxr0   r0   r1   r´   Ã  s4    ÿ
þ
ý
üÿÿ
z _HTTPConnection._get_ssl_options)rq   r%   c                 C   s8   d| _ |rd |¡nd}| jdur4|  tt|ƒd¡ dS )z¯Timeout callback of _HTTPConnection instance.

        Raise a `HTTPTimeoutError` when a timeout occurs.

        :info string key: More detailed timeout information.
        Nrr   r3   )r�   rv   ro   rÏ   r"   )r-   rq   ry   r0   r0   r1   ra   â  s    

ÿz_HTTPConnection._on_timeoutc                 C   s"   | j d ur| j | j ¡ d | _ d S rR   )r�   r\   rp   r4   r0   r0   r1   ri   ð  s    
z_HTTPConnection._remove_timeout)r¶   r%   c              	   C   s6   |  d¡ t|dtd| j| jt| jjƒd�| jƒ}|S )NT)Zno_keep_aliverG   rH   Ú
decompress)	Zset_nodelayr   r   rG   rH   ÚboolrT   rÆ   r‘   )r-   r¶   rÊ   r0   r0   r1   rÉ   õ  s    

ü÷z"_HTTPConnection._create_connection)Ú
start_readr%   c                 Ã   s˜   | j jd ur| j | j j¡ n.| j jd urL| j  | jj¡}|d urL|I d H  | j ¡  |r”z| j | ¡I d H  W n" ty’   | jt	 
¡ Ž sŽ‚ Y n0 d S rR   )rT   rÃ   rÊ   ÚwriterÄ   ÚfinishrÌ   r   rÏ   rÐ   rÑ   )r-   rç   Úfutr0   r0   r1   rÍ     s    

z_HTTPConnection._write_bodyc                 C   s    | j d ur| j }d | _ |ƒ  d S rR   )rl   )r-   rl   r0   r0   r1   Ú_release  s    
z_HTTPConnection._release)Úresponser%   c                 C   s0   |   ¡  | jd ur,| j}d | _| j ||¡ d S rR   )rë   ro   r\   rx   )r-   rì   ro   r0   r0   r1   Ú_run_callback  s
    
z_HTTPConnection._run_callbackzOptional[Type[BaseException]])ÚtypÚvalueÚtbr%   c              	   C   s„   | j d urv|  ¡  t|tƒr6|jd u r0tdƒ}n|j}|  t| jd|| j	 
¡ | j | jd�¡ t| dƒrr| j ¡  dS t|tƒS d S )Nr>   r'   )rs   rt   rw   r¶   T)ro   ri   Ú
isinstancer   Z
real_errorr=   rí   r   rT   r\   r^   rw   r‹   rã   r¶   rS   )r-   rî   rï   rð   r0   r0   r1   rÏ      s&    



ûÿ


z!_HTTPConnection._handle_exceptionc                 C   sP   | j d urLd}| jjr| jj‚zt|ƒ‚W n  tyJ   | jt ¡ Ž  Y n0 d S )NzConnection closed)ro   r¶   rs   r=   rÏ   rÐ   rÑ   r,   r0   r0   r1   r·   D  s    
z#_HTTPConnection.on_connection_close)Ú
first_liner�   r%   c                 Ã   s¬   t |tjƒsJ ‚| jjr6|jdkr6|  d¡I d H  d S |j| _|j| _|| _|  	¡ rXd S | jj
d ur¨| j 
d| ¡ | j ¡ D ]\}}| j 
d||f ¡ q~| j 
d¡ d S )Néd   Fz
%s %s %s
z%s: %s
z
)rñ   r   ÚResponseStartLinerT   rÅ   rŒ   rÍ   Úreasonr�   Ú_should_follow_redirectZheader_callbackÚget_all)r-   rò   r�   ÚkÚvr0   r0   r1   Úheaders_receivedN  s    z _HTTPConnection.headers_receivedc                 C   sL   | j jrH| j jd usJ ‚| jdv oF| j jdkoF| jd uoF| j d¡d uS dS )N)é-  é.  é/  i3  i4  r   ÚLocationF)rT   Zfollow_redirectsÚmax_redirectsrŒ   r�   rÂ   r4   r0   r0   r1   rö   e  s    

ÿþüz'_HTTPConnection._should_follow_redirectc              
      sÐ  | j d usJ ‚d | j¡}|  ¡  t| jd| jƒ}|  ¡ �rjt| jtƒsLJ ‚| j	d usZJ ‚t
 
| jj¡}tj | jj| j	d ¡|_| jjd us’J ‚| jjd |_|j	d= | j dkr¾| jjdksØ| j dv �r| jjd	k�rd
|_d |_dD ](}z| jj	|= W qè t�y   Y qè0 qè||_| j‰ d | _|  ¡  | jd u�s<J ‚| jj|dd�}| ‡ fdd„¡ |  ¡  d S | jj�r|tƒ }nt|ƒ}t|| j t| dd ƒ| j	| j ¡ | j | j || jjd�}|  !|¡ |  ¡  d S )Nr‰   Úoriginal_requestrþ   r—   r�   rý   r}   )rû   rü   r~   r|   )r¡   r¢   zContent-EncodingzTransfer-EncodingF)Zraise_errorc                    s   ˆ |   ¡ ƒS rR   r„   r†   ©ro   r0   r1   rˆ   �  r‰   z(_HTTPConnection.finish.<locals>.<lambda>rõ   )rõ   r�   rt   rw   ÚbufferZeffective_url)"rŒ   ÚjoinrŽ   ri   r»   rT   rö   rñ   r	   r�   Úcopyr¦   r§   Úurljoinr©   rÿ   r¸   rÃ   rº   r   ro   rë   rƒ   ÚfetchÚadd_done_callbackÚ_on_end_requestÚstreaming_callbackr   r   r\   r^   rw   r‹   rí   )r-   Údatar   Znew_requestÚhrê   r  rì   r0   r  r1   ré   p  sd    
ÿ
ÿ
ÿ

ø

z_HTTPConnection.finishc                 C   s   | j  ¡  d S rR   )r¶   rS   r4   r0   r0   r1   r  ±  s    z_HTTPConnection._on_end_request)Úchunkr%   c                 C   s6   |   ¡ rd S | jjd ur&| j |¡ n| j |¡ d S rR   )rö   rT   r	  rŽ   rW   )r-   r  r0   r0   r1   Údata_received´  s
    z_HTTPConnection.data_received)N),r7   r8   r9   Úsetr¹   r   r?   r
   r   r   rz   r   r+   r’   r;   r   r   r   rÛ   Ú
SSLContextr´   ra   ri   r   r   rÉ   ræ   rÍ   rë   rí   ÚBaseExceptionr   rÏ   r·   r   rô   rË   ZHTTPHeadersrú   rö   ré   r  Úbytesr  r0   r0   r0   r1   rm   ü   sL   ÿ
ö! #þ	û$üArm   Ú__main__)?Ztornado.escaper   Útornador   r   Ztornado.httpclientr   r   r   r   r	   r
   r   Ztornado.http1connectionr   r   Ztornado.ioloopr   Ztornado.iostreamr   r   Ztornado.netutilr   r   r   r   Ztornado.logr   Ztornado.tcpclientr   r¿   rK   r  r_   r¯   r±   rÛ   rÐ   r^   Úior   Úurllib.parser¦   Útypingr   r   r   r   r   r   Útypesr   ÚTYPE_CHECKINGr   r    r!   r"   r=   r?   ZHTTPMessageDelegaterm   r7   Ú	configurer0   r0   r0   r1   Ú<module>   sH      .   E

