a
    Lf]                     @  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   F/var/www/ai-form-bot/venv/lib/python3.9/site-packages/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                   sH   |   | _d | _g | _d|v s&d|v r.tdt j||fi | d S )Nproxy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)h2r2   ZH2Configurationr	   
connectionZH2Connection)r   r2   r   r   r   r*   @   s    zHTTP2Connection._new_h2_connc                   sL   t    | j(}|  | j|  W d    n1 s>0    Y  d S r   )r/   connectr+   Zinitiate_connectionsocksendalldata_to_send)r   h2_connr0   r   r   r5   D   s    
zHTTP2Connection.connectFbool)methodurl	skip_hostskip_accept_encodingr   c              	   C  s   | j }|| _| | _d| jv r<d| j d| jp4d }n| j d| jpLd }| jdd| fd| fd| ff W d    n1 s0    Y  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>   r9   	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-   appendrC   lower)r   rF   rG   valuer   r   r   	putheaderd   s    zHTTP2Connection.putheaderc                 C  sV   | j <}|j| j| jdd |  }r4| j| W d    n1 sH0    Y  d S )NT)	stream_idheaders
end_stream)r+   send_headersr,   r-   r8   r6   r7   )r   r9   r8   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   rR   r   r   r   sendt   s    zHTTP2Connection.sendHTTP2Responsec              	   C  s2  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 d    n1 s0    Y  |   |d usJ t||| jt|dS )NFi  s   :statusasciiT)statusrM   request_urlrR   )	bytearrayr+   r6   recvZreceive_data
isinstancer3   eventsZResponseReceivedr   rM   intdecodeaddZDataReceivedrR   Zacknowledge_received_dataZflow_controlled_lengthrL   ZStreamEndedr8   r7   closerT   rA   rQ   )r   rV   rR   r9   rN   Zreceived_datar[   eventrM   rF   rJ   r8   r   r   r   getresponsey   sB    

,zHTTP2Connection.getresponsec              	     s   | j H}z$|  |  }r*| j| W n ty>   Y n0 W d    n1 sT0    Y  |  | _ d | _g | _t	 
  d S r   )r+   Zclose_connectionr8   r6   r7   	Exceptionr*   r,   r-   r/   r_   )r   r9   rR   r0   r   r   r_      s    $
zHTTP2Connection.close)N)FF)r   r    r!   r   r*   r5   rE   rK   rP   rS   ra   r_   __classcell__r   r   r0   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 )rT   Fr\   r   r$   rQ   r:   r   )rV   rM   rW   rR   decode_contentr   c              	     s*   t  j||ddd ||d || _d| _d S )N   zHTTP/2)rV   rM   versionversion_stringreasonrd   rW   r   )r/   r   _datalength_remaining)r   rV   rM   rW   rR   rd   r0   r   r   r      s    zHTTP2Response.__init__r   c                 C  s   | j S r   )ri   r   r   r   r   rR      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   propertyrR   rk   r_   rc   r   r   r0   r   rT      s    rT   r   r   c                   C  s   t t_t tj_dgtjj_d S )Nr3   )	r#   r   ConnectionClsurllib3r4   r   utilssl_ALPN_PROTOCOLSr   r   r   r   inject_into_urllib3   s    rr   c                   C  s   t t_t tj_dgtjj_d S )Nzhttp/1.1)	orig_HTTPSConnectionr   rm   rn   r4   r   ro   rp   rq   r   r   r   r   extract_from_urllib3   s    rt   )
__future__r   r   typestypingZ	h2.configr3   Zh2.connectionZ	h2.eventsZurllib3.connectionrn   Zurllib3.util.ssl_Zurllib3.responser   _collectionsr   r4   r   connectionpoolr   rs   TypeVarr   Genericr	   r#   rT   rr   rt   r   r   r   r   <module>   s(   
 #