U
    kuf8                     @  s   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mZ ddlmZ ddlmZ ddlmZ eZedZG d	d
 d
eje ZG dd deZG dd deZddddZddddZdS )    )annotationsN)BaseHTTPResponse   )HTTPHeaderDict)HTTPSConnection)HTTPSConnectionPoolTc                   @  s@   e Zd ZdZddddZddddZd	d
dddddZdS )_LockedObjectaY  
    A wrapper class that hides a specific object behind a lock.

    The goal here is to provide a simple way to protect access to an object
    that cannot safely be simultaneously accessed from multiple threads. The
    intended use of this class is simple: take hold of it with a context
    manager, which returns the protected object.
    r   )objc                 C  s   t  | _|| _d S N)	threadingRLocklock_obj)selfr
    r   1/tmp/pip-unpacked-wheel-5in6dhvn/urllib3/http2.py__init__"   s    
z_LockedObject.__init__returnc                 C  s   | j   | jS r   )r   acquirer   r   r   r   r   	__enter__&   s    
z_LockedObject.__enter__ztype[BaseException] | NonezBaseException | Noneztypes.TracebackType | NoneNone)exc_typeexc_valexc_tbr   c                 C  s   | j   d S r   )r   release)r   r   r   r   r   r   r   __exit__*   s    z_LockedObject.__exit__N)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r	      s   	r	   c                      s   e Zd Zd"ddddd fddZd	d
ddZdd
 fddZd#ddddddddZddddddZdd
ddZdddddZ	dd
ddZ
dd
 fd d!Z  ZS )$HTTP2ConnectionNstrz
int | Nonez
typing.Anyr   )hostportkwargsr   c                   sD   |   | _d | _g | _d|ks&d|kr.tdt j||f| d S )NproxyZproxy_configz$Proxies aren't supported with HTTP/2)_new_h2_conn_h2_conn
_h2_stream_h2_headersNotImplementedErrorsuperr   )r   r%   r&   r'   	__class__r   r   r   4   s    
zHTTP2Connection.__init__z)_LockedObject[h2.connection.H2Connection]r   c                 C  s    t jjdd}tt jj|dS )NT)Zclient_side)config)h2r1   ZH2Configurationr	   
connectionZH2Connection)r   r1   r   r   r   r)   @   s    zHTTP2Connection._new_h2_connc              	     s8   t    | j}|  | j|  W 5 Q R X d S r   )r.   connectr*   Zinitiate_connectionsocksendalldata_to_send)r   h2_connr/   r   r   r4   D   s    
zHTTP2Connection.connectFbool)methodurl	skip_hostskip_accept_encodingr   c              	   C  s   | j |}|| _| | _d| jkr<d| j d| jp4d }n| j d| jpLd }| jdd| fd| fd| ff W 5 Q R X d S )	N:[z]:i  )s   :schemes   httpss   :methods
   :authoritys   :path)	r*   _request_urlZget_next_available_stream_idr+   r%   r&   r,   extendencode)r   r:   r;   r<   r=   r8   	authorityr   r   r   
putrequestK   s    




zHTTP2Connection.putrequest)headervaluesr   c                 G  s.   |D ]$}| j |d |df qd S )Nzutf-8)r,   appendrB   lower)r   rE   rF   valuer   r   r   	putheaderd   s    zHTTP2Connection.putheaderc              	   C  sB   | j 2}|j| j| jdd |  }r4| j| W 5 Q R X d S )NT)	stream_idheaders
end_stream)r*   send_headersr+   r,   r7   r5   r6   )r   r8   r7   r   r   r   
endheadersj   s    zHTTP2Connection.endheadersbytes)datar   c                 C  s   |sd S t dd S )Nz Sending data isn't supported yet)r-   )r   rQ   r   r   r   sendt   s    zHTTP2Connection.sendHTTP2Responsec              	   C  s  d }t  }| j}d}|s| jd }r||}|D ]}t|tjjrt	 }|j
D ]6\}	}
|	dkrtt|
 }qV||	d|
d qVq8t|tjjr||j7 }||j|j q8t|tjjr8d}q8|  }r| j| qW 5 Q R X |   |d k	stt||| jt|dS )NFi  s   :statusasciiT)statusrL   request_urlrQ   )	bytearrayr*   r5   recvZreceive_data
isinstancer2   eventsZResponseReceivedr   rL   intdecodeaddZDataReceivedrQ   Zacknowledge_received_dataZflow_controlled_lengthrK   ZStreamEndedr7   r6   closeAssertionErrorrS   r@   rP   )r   rU   rQ   r8   rM   Zreceived_datarZ   eventrL   rE   rI   r7   r   r   r   getresponsey   sF    
 
 zHTTP2Connection.getresponsec              	     sp   | j @}z$|  |  }r*| j| W n tk
r@   Y nX W 5 Q R X |  | _ d | _g | _t	 
  d S r   )r*   Zclose_connectionr7   r5   r6   	Exceptionr)   r+   r,   r.   r^   )r   r8   rQ   r/   r   r   r^      s    
zHTTP2Connection.close)N)FF)r   r    r!   r   r)   r4   rD   rJ   rO   rR   ra   r^   __classcell__r   r   r/   r   r#   3   s      
-r#   c                      s\   e Zd Zdddddddd fd	d
ZeddddZddddZddddZ  ZS )rS   Fr[   r   r$   rP   r9   r   )rU   rL   rV   rQ   decode_contentr   c                   s(   t  j||dd ||d || _d| _d S )N   )rU   rL   versionreasonrd   rV   r   )r.   r   _datalength_remaining)r   rU   rL   rV   rQ   rd   r/   r   r   r      s    
zHTTP2Response.__init__r   c                 C  s   | j S r   )rh   r   r   r   r   rQ      s    zHTTP2Response.datac                 C  s   d S r   r   r   r   r   r   get_redirect_location   s    z#HTTP2Response.get_redirect_locationc                 C  s   d S r   r   r   r   r   r   r^      s    zHTTP2Response.close)F)	r   r    r!   r   propertyrQ   rj   r^   rc   r   r   r/   r   rS      s    rS   r   r   c                   C  s   t t_t tj_dgtjj_d S )Nr2   )	r#   r   ConnectionClsurllib3r3   r   utilssl_ALPN_PROTOCOLSr   r   r   r   inject_into_urllib3   s    rq   c                   C  s   t t_t tj_dgtjj_d S )Nzhttp/1.1)	orig_HTTPSConnectionr   rl   rm   r3   r   rn   ro   rp   r   r   r   r   extract_from_urllib3   s    rs   )
__future__r   r   typestypingZ	h2.configr2   Zh2.connectionZ	h2.eventsZurllib3.connectionrm   Zurllib3.util.ssl_Zurllib3.responser   _collectionsr   r3   r   connectionpoolr   rr   TypeVarr   Genericr	   r#   rS   rq   rs   r   r   r   r   <module>   s(   
 "