U
    Zhm9                     @  sB  d dl mZ d dlZd dlZd dlZd dl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 ddlmZ ddlmZ dd	lmZ d
dlmZ d
dlmZ d
dlmZ d
dlm Z  ej!e"e#f Z$ej%ej&e$e$f  Z'ej(e$e$f Z)e*dZ+dddddddZ,G dd deZ-G dd de Z.G dd de Z/dS )    )annotationsN   )SOCKET_OPTIONAsyncNetworkBackend)
ProxyError)URLOriginRequestResponseenforce_bytesenforce_headersenforce_url)default_ssl_context)	AsyncLock)Trace   )AsyncHTTPConnection)AsyncConnectionPool)AsyncHTTP11Connection)AsyncConnectionInterfacezhttpcore.proxy+typing.Sequence[tuple[bytes, bytes]] | Nonezlist[tuple[bytes, bytes]])default_headersoverride_headersreturnc                   sT   | dkrg nt | } |dkr g nt |}tdd |D   fdd| D } | | S )zh
    Append default_headers and override_headers, de-duplicating if a key exists
    in both cases.
    Nc                 s  s   | ]\}}|  V  qd S Nlower.0keyvalue r!   >/tmp/pip-unpacked-wheel-s37ty00c/httpcore/_async/http_proxy.py	<genexpr>-   s     z merge_headers.<locals>.<genexpr>c                   s$   g | ]\}}|   kr||fqS r!   r   r   Zhas_overrider!   r"   
<listcomp>.   s   z!merge_headers.<locals>.<listcomp>)listset)r   r   r!   r$   r"   merge_headers#   s    
r(   c                      sV   e Zd ZdZdddd	d
d
dddddddddddd fddZdddddZ  ZS )AsyncHTTPProxyzB
    A connection pool that sends requests via an HTTP proxy.
    N
   TFr   zURL | bytes | strz&tuple[bytes | str, bytes | str] | None+HeadersAsMapping | HeadersAsSequence | Nonessl.SSLContext | Nonez
int | Nonefloat | Noneboolintz
str | NoneAsyncNetworkBackend | None%typing.Iterable[SOCKET_OPTION] | NoneNone)	proxy_url
proxy_authproxy_headersssl_contextproxy_ssl_contextmax_connectionsmax_keepalive_connectionskeepalive_expiryhttp1http2retrieslocal_addressudsnetwork_backendsocket_optionsr   c                   s   t  j|||||	|
|||||d t|dd| _| jjdkrL|dk	rLtd|| _|| _t|dd| _	|dk	rt
|d d	d}t
|d
 d	d}|d | }dt| }d|fg| j	 | _	dS )a  
        A connection pool for making HTTP requests.

        Parameters:
            proxy_url: The URL to use when connecting to the proxy server.
                For example `"http://127.0.0.1:8080/"`.
            proxy_auth: Any proxy authentication as a two-tuple of
                (username, password). May be either bytes or ascii-only str.
            proxy_headers: Any HTTP headers to use for the proxy requests.
                For example `{"Proxy-Authorization": "Basic <username>:<password>"}`.
            ssl_context: An SSL context to use for verifying connections.
                If not specified, the default `httpcore.default_ssl_context()`
                will be used.
            proxy_ssl_context: The same as `ssl_context`, but for a proxy server rather than a remote origin.
            max_connections: The maximum number of concurrent HTTP connections that
                the pool should allow. Any attempt to send a request on a pool that
                would exceed this amount will block until a connection is available.
            max_keepalive_connections: The maximum number of idle HTTP connections
                that will be maintained in the pool.
            keepalive_expiry: The duration in seconds that an idle HTTP connection
                may be maintained for before being expired from the pool.
            http1: A boolean indicating if HTTP/1.1 requests should be supported
                by the connection pool. Defaults to True.
            http2: A boolean indicating if HTTP/2 requests should be supported by
                the connection pool. Defaults to False.
            retries: The maximum number of retries when trying to establish
                a connection.
            local_address: Local address to connect from. Can also be used to
                connect using a particular address family. Using
                `local_address="0.0.0.0"` will connect using an `AF_INET` address
                (IPv4), while using `local_address="::"` will connect using an
                `AF_INET6` address (IPv6).
            uds: Path to a Unix Domain Socket to use instead of TCP sockets.
            network_backend: A backend instance to use for handling network I/O.
        )r6   r8   r9   r:   r;   r<   r@   r=   r>   r?   rA   r3   name   httpNzCThe `proxy_ssl_context` argument is not allowed for the http schemer5   r   r4   r      :s   Basic s   Proxy-Authorization)super__init__r   
_proxy_urlschemeRuntimeError_ssl_context_proxy_ssl_contextr   _proxy_headersr   base64	b64encode)selfr3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   usernamepasswordZuserpassauthorization	__class__r!   r"   rG   ;   sB    5
zAsyncHTTPProxy.__init__r   r   originr   c                 C  sV   |j dkr*t| jj| j|| j| j| jdS t| jj| j|| j	| j| j| j
| j| jd	S )NrD   )proxy_originr5   remote_originr:   r@   r7   )	rX   r5   rY   r6   r7   r:   r;   r<   r@   )rI   AsyncForwardHTTPConnectionrH   rW   rM   _keepalive_expiryZ_network_backendrL   AsyncTunnelHTTPConnectionrK   _http1_http2rP   rW   r!   r!   r"   create_connection   s(    
z AsyncHTTPProxy.create_connection)NNNNr*   NNTFr   NNNN)__name__
__module____qualname____doc__rG   r`   __classcell__r!   r!   rT   r"   r)   6   s"                 2Wr)   c                
   @  s   e Zd Zd%ddddddddd	d
dZdddddZdddddZddddZddddZddddZddddZ	dddd Z
ddd!d"Zddd#d$ZdS )&rZ   Nr   r+   r-   r0   r1   r,   r2   )rX   rY   r5   r:   r@   rA   r7   r   c                 C  s2   t |||||d| _|| _t|dd| _|| _d S )NrW   r:   r@   rA   r6   r5   rB   )r   _connection_proxy_originr   rM   _remote_origin)rP   rX   rY   r5   r:   r@   rA   r7   r!   r!   r"   rG      s    
z#AsyncForwardHTTPConnection.__init__r	   r
   requestr   c                   sZ   t | j|j}t| jj| jj| jjt|j	d}t
|j|||j|jd}| j|I d H S )NrI   hostporttarget)methodurlheaderscontent
extensions)r(   rM   rr   r   rh   rI   rm   rn   bytesrq   r	   rp   streamrt   rg   handle_async_request)rP   rk   rr   rq   Zproxy_requestr!   r!   r"   rw      s    z/AsyncForwardHTTPConnection.handle_async_requestr.   rV   c                 C  s
   || j kS r   ri   r_   r!   r!   r"   can_handle_request   s    z-AsyncForwardHTTPConnection.can_handle_requestr   c                   s   | j  I d H  d S r   rg   acloserP   r!   r!   r"   r|      s    z!AsyncForwardHTTPConnection.aclosestrc                 C  s
   | j  S r   rg   infor}   r!   r!   r"   r      s    zAsyncForwardHTTPConnection.infoc                 C  s
   | j  S r   rg   is_availabler}   r!   r!   r"   r      s    z'AsyncForwardHTTPConnection.is_availablec                 C  s
   | j  S r   rg   has_expiredr}   r!   r!   r"   r      s    z&AsyncForwardHTTPConnection.has_expiredc                 C  s
   | j  S r   rg   is_idler}   r!   r!   r"   r      s    z"AsyncForwardHTTPConnection.is_idlec                 C  s
   | j  S r   rg   	is_closedr}   r!   r!   r"   r      s    z$AsyncForwardHTTPConnection.is_closedc                 C  s   d| j j d|   dS N<z [z]>rU   ra   r   r}   r!   r!   r"   __repr__   s    z#AsyncForwardHTTPConnection.__repr__)NNNNNra   rb   rc   rG   rw   ry   r|   r   r   r   r   r   r   r!   r!   r!   r"   rZ      s        rZ   c                   @  s   e Zd Zd'ddddddddd	d
ddddZdddddZdddddZddddZddddZddddZdddd Z	ddd!d"Z
ddd#d$Zddd%d&ZdS )(r\   NTFr   r,   r   r-   r.   r0   r1   r2   )rX   rY   r6   r7   r5   r:   r;   r<   r@   rA   r   c                 C  s^   t |||	|
|d| _|| _|| _|| _|| _t|dd| _|| _|| _	|| _
t | _d| _d S )Nrf   r5   rB   F)r   rg   rh   ri   rK   rL   r   rM   r[   r]   r^   r   _connect_lock
_connected)rP   rX   rY   r6   r7   r5   r:   r;   r<   r@   rA   r!   r!   r"   rG      s"    z"AsyncTunnelHTTPConnection.__init__r	   r
   rj   c                   s  |j di }|dd }| j4 I d H  | jsd| jj| jjf }t| jj	| jj| jj|d}t
d|fdg| j}td|||j d}| j|I d H }|jd	k s|jd
kr|j dd}	|	jddd}
d|j|
f }| j I d H  t||j d }| jd krt n| j}| jr$ddgndg}|| || jjd|d}tdt||4 I d H }|jf |I d H }||_W 5 Q I d H R X |d}|d k	o| dk}|s| jr| jsddlm} || j|| j d| _nt!| j|| j d| _d| _W 5 Q I d H R X | j|I d H S )Ntimeoutconnects   %b:%drl   s   Host)s   Accepts   */*s   CONNECT)rp   rq   rr   rt      i+  Zreason_phrase    asciiignore)errorsz%d %sZnetwork_streamzhttp/1.1h2)r6   server_hostnamer   	start_tls
ssl_objectr   )AsyncHTTP2Connection)rW   rv   r:   T)"rt   getr   r   ri   rm   rn   r   rh   rI   r(   rM   r	   rg   rw   statusdecoder|   r   rK   r   r^   set_alpn_protocolsr   loggerr   Zreturn_valueZget_extra_infoselected_alpn_protocolr]   r<   r   r[   r   )rP   rk   Ztimeoutsr   ro   Zconnect_urlZconnect_headersZconnect_requestZconnect_responseZreason_bytesZ
reason_strmsgrv   r6   alpn_protocolskwargstracer   Zhttp2_negotiatedr   r!   r!   r"   rw   	  s|    
 







z.AsyncTunnelHTTPConnection.handle_async_requestrV   c                 C  s
   || j kS r   rx   r_   r!   r!   r"   ry   Y  s    z,AsyncTunnelHTTPConnection.can_handle_requestrz   c                   s   | j  I d H  d S r   r{   r}   r!   r!   r"   r|   \  s    z AsyncTunnelHTTPConnection.acloser~   c                 C  s
   | j  S r   r   r}   r!   r!   r"   r   _  s    zAsyncTunnelHTTPConnection.infoc                 C  s
   | j  S r   r   r}   r!   r!   r"   r   b  s    z&AsyncTunnelHTTPConnection.is_availablec                 C  s
   | j  S r   r   r}   r!   r!   r"   r   e  s    z%AsyncTunnelHTTPConnection.has_expiredc                 C  s
   | j  S r   r   r}   r!   r!   r"   r   h  s    z!AsyncTunnelHTTPConnection.is_idlec                 C  s
   | j  S r   r   r}   r!   r!   r"   r   k  s    z#AsyncTunnelHTTPConnection.is_closedc                 C  s   d| j j d|   dS r   r   r}   r!   r!   r"   r   n  s    z"AsyncTunnelHTTPConnection.__repr__)NNNNTFNNr   r!   r!   r!   r"   r\      s$           $Pr\   )NN)0
__future__r   rN   loggingssltypingZ_backends.baser   r   _exceptionsr   Z_modelsr   r   r	   r
   r   r   r   _sslr   Z_synchronizationr   Z_tracer   
connectionr   Zconnection_poolr   Zhttp11r   
interfacesr   Unionru   r~   Z	ByteOrStrSequenceTupleZHeadersAsSequenceMappingZHeadersAsMapping	getLoggerr   r(   r)   rZ   r\   r!   r!   r!   r"   <module>   s0   $	
  s@