o
    ;)h1                     @  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mZ ddlmZ ddlmZ eZed	ZeeZed
ZedZdddZdddZ G dd dej!e Z"G dd deZ#G dd deZ$dS )    )annotationsN   )
_TYPE_BODY)HTTPHeaderDict)HTTPSConnection_get_default_user_agent)ConnectionError)BaseHTTPResponseTs   ^[!#$%&'*+\-.^_`|~0-9a-z]+$s*   [\0\x00\x0a\x0d\r\n]|^[ \r\n\t]|[ \r\n\t]$namebytesreturnboolc                 C     t t| S )a5  
    "An implementation that validates fields according to the definitions in Sections
    5.1 and 5.5 of [HTTP] only needs an additional check that field names do not
    include uppercase characters." (https://httpwg.org/specs/rfc9113.html#n-field-validity)

    `http.client._is_legal_header_name` does not validate the field name according to the
    HTTP 1.1 spec, so we do that here, in addition to checking for uppercase characters.

    This does not allow for the `:` character in the header name, so should not
    be used to validate pseudo-headers.
    )r   RE_IS_LEGAL_HEADER_NAMEmatch)r    r   WC:\Users\User\Downloads\Proyecto_IoT\venv\Lib\site-packages\urllib3/http2/connection.py_is_legal_header_name   s   r   valuec                 C  r   )aK  
    "A field value MUST NOT contain the zero value (ASCII NUL, 0x00), line feed
    (ASCII LF, 0x0a), or carriage return (ASCII CR, 0x0d) at any position. A field
    value MUST NOT start or end with an ASCII whitespace character (ASCII SP or HTAB,
    0x20 or 0x09)." (https://httpwg.org/specs/rfc9113.html#n-field-validity)
    )r   RE_IS_ILLEGAL_HEADER_VALUEsearch)r   r   r   r   _is_illegal_header_value,   s   r   c                   @  s2   e Zd ZdZdZdddZddd	ZdddZdS )_LockedObjectaX  
    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.
    )lock_objobjr
   c                 C  s   t  | _|| _d S N)	threadingRLockr   r   )selfr   r   r   r   __init__D   s   

z_LockedObject.__init__r   c                 C  s   | j   | jS r   )r   acquirer   r    r   r   r   	__enter__H   s   
z_LockedObject.__enter__exc_typetype[BaseException] | Noneexc_valBaseException | Noneexc_tbtypes.TracebackType | NoneNonec                 C  s   | j   d S r   )r   release)r    r%   r'   r)   r   r   r   __exit__L   s   z_LockedObject.__exit__N)r   r
   )r   r
   )r%   r&   r'   r(   r)   r*   r   r+   )__name__
__module____qualname____doc__	__slots__r!   r$   r-   r   r   r   r   r   6   s    

r   c                      s   e Zd Z	d5d6 fd
dZd7ddZd8 fddZd9ddZd:ddZd5d;ddZd<ddZ				 d=d>d$d%Z
d?d'd(Z		d@d)d)d)d*dAd1d2Zd8 fd3d4Z  ZS )BHTTP2ConnectionNhoststrport
int | Nonekwargs
typing.Anyr   r+   c                   sZ   |   | _d | _g | _d|v sd|v rtdt j||fi | | jd ur+tdd S )Nproxyproxy_configz$Proxies aren't supported with HTTP/2z%Tunneling isn't supported with HTTP/2)_new_h2_conn_h2_conn
_h2_stream_headersNotImplementedErrorsuperr!   _tunnel_host)r    r4   r6   r8   	__class__r   r   r!   V   s   

zHTTP2Connection.__init__)_LockedObject[h2.connection.H2Connection]c                 C  s    t jjdd}tt jj|dS )NT)Zclient_side)config)h2rF   ZH2Configurationr   
connectionZH2Connection)r    rF   r   r   r   r<   e   s   zHTTP2Connection._new_h2_connc                   sf   t    | j!}|  |  }r!| j| W d    d S W d    d S 1 s,w   Y  d S r   )rA   connectr=   Zinitiate_connectiondata_to_sendsocksendall)r    connrJ   rC   r   r   rI   i   s   
"zHTTP2Connection.connectmethodurlc                 K  s   d|v rt dd|v rt d|pd| _| | d| jv r,d| j d| jp(d	 }n| j d| jp4d	 }| jd
 | jd| f | jd| f | jd| f | j}|	 | _
W d   dS 1 sow   Y  dS )zputrequest
        This deviates from the HTTPConnection method signature since we never need to override
        sending accept-encoding headers or the host header.
        	skip_hostz`skip_host` isn't supportedskip_accept_encodingz&`skip_accept_encoding` isn't supported/:[z]:i  )s   :schemes   httpss   :methods
   :authoritys   :pathN)r@   _request_url_validate_pathr4   r6   r?   appendencoder=   Zget_next_available_stream_idr>   )r    rN   rO   r8   	authorityrM   r   r   r   
putrequestp   s    



"zHTTP2Connection.putrequestheaderstr | bytesvaluesc                 G  s   t |tr	| n|}| }t|stdt| |D ]"}t |tr)| n|}t|r8tdt| | j||f qd S )NzIllegal header name zIllegal header value )	
isinstancer5   rX   lowerr   
ValueErrorr   r?   rW   )r    r[   r]   r   r   r   r   	putheader   s   zHTTP2Connection.putheadermessage_bodyc                 C  sr   | j d u r	td| j }|j| j | j|d u d |  }r%| j| W d    n1 s/w   Y  g | _d S )NMust call `putrequest` first.)	stream_idheaders
end_stream)r>   r   r=   send_headersr?   rJ   rK   rL   )r    rb   rM   rJ   r   r   r   
endheaders   s   

zHTTP2Connection.endheadersdatac              	   C  s|  | j du r	td| j}|  }r| j| t|drV	 || j}|s(nt	|t
r1| }|j| j |dd |  }rF| j| q|| j  	 W d   dS t	|t
r_| }z=t	|tr{|j| j |dd |  }rz| j| n |D ]}|j| j |dd |  }r| j| q}|| j  W n ty   tdt| w W d   dS 1 sw   Y  dS )zSend data to the server.
        `data` can be: `str`, `bytes`, an iterable, or file-like objects
        that support a .read() method.
        Nrc   readTF)rf   z6`data` should be str, bytes, iterable, or file. got %r)r>   r   r=   rJ   rK   rL   hasattrrj   	blocksizer^   r5   rX   Z	send_datarf   r   	TypeErrortype)r    ri   rM   rJ   chunkr   r   r   send   sV   


	

"zHTTP2Connection.sendhttpre   typing.Mapping[str, str] | Noneschemec                 C  s   t d)Nz;HTTP/2 does not support setting up a tunnel through a proxy)r@   )r    r4   r6   re   rs   r   r   r   
set_tunnel   s   zHTTP2Connection.set_tunnelHTTP2Responsec              	   C  s*  d }t  }| jt}d}|su| jd }rg||}|D ]J}t|tjjrHt	 }|j
D ]\}	}
|	dkr:t|
 }q+||	d|
d q+qt|tjjr]||j7 }||j|j qt|tjjrfd}q|  }rs| j| |rW d    n1 sw   Y  |d usJ t||| jt|dS )NFi  s   :statusasciiT)statusre   request_urlri   )	bytearrayr=   rK   recvZreceive_datar^   rG   eventsZResponseReceivedr   re   intdecodeaddZDataReceivedri   Zacknowledge_received_dataZflow_controlled_lengthrd   ZStreamEndedrJ   rL   ru   rU   r   )r    rw   ri   rM   rf   Zreceived_datar{   eventre   r[   r   rJ   r   r   r   getresponse   sJ   

zHTTP2Connection.getresponseT)preload_contentdecode_contentenforce_content_lengthbody_TYPE_BODY | Noner   r   r   r   c                K  s   d|v r	 | j dur| j | j | || |pi }| D ]\}	}
|	 dkr.|
dkr.q| |	|
 qdt| jvrC| dt	  |rR| j
|d | | dS | 
  dS )zSend an HTTP/2 requestchunkedNztransfer-encodings
   user-agent)rb   )rK   
settimeouttimeoutrZ   itemsr_   ra   dictr?   r   rh   rp   )r    rN   rO   r   re   r   r   r   r8   kvr   r   r   request  s    
zHTTP2Connection.requestc              	     s   | j $}z|  |  }r| j| W n	 ty   Y nw W d    n1 s*w   Y  |  | _ d | _g | _t	 
  d S r   )r=   Zclose_connectionrJ   rK   rL   	Exceptionr<   r>   r?   rA   close)r    rM   ri   rC   r   r   r   3  s   
	zHTTP2Connection.closer   )r4   r5   r6   r7   r8   r9   r   r+   )r   rE   r   r+   )rN   r5   rO   r5   r8   r9   r   r+   )r[   r\   r]   r\   r   r+   )rb   r9   r   r+   )ri   r9   r   r+   )NNrq   )
r4   r5   r6   r7   re   rr   rs   r5   r   r+   )r   ru   )NN)rN   r5   rO   r5   r   r   re   rr   r   r   r   r   r   r   r8   r9   r   r+   )r.   r/   r0   r!   r<   rI   rZ   ra   rh   rp   rt   r   r   r   __classcell__r   r   rC   r   r3   U   s,    



0
.'r3   c                      sD   e Zd Z	dd fddZedddZdddZdddZ  ZS )ru   Frw   r|   re   r   rx   r5   ri   r   r   r   r   r+   c              	     s*   t  j||ddd ||d || _d| _d S )N   zHTTP/2)rw   re   versionversion_stringreasonr   rx   r   )rA   r!   _datalength_remaining)r    rw   re   rx   ri   r   rC   r   r   r!   F  s   
zHTTP2Response.__init__c                 C  s   | j S r   )r   r#   r   r   r   ri   \  s   zHTTP2Response.datac                 C     d S r   r   r#   r   r   r   get_redirect_location`     z#HTTP2Response.get_redirect_locationc                 C  r   r   r   r#   r   r   r   r   c  r   zHTTP2Response.close)F)rw   r|   re   r   rx   r5   ri   r   r   r   r   r+   )r   r   r   )	r.   r/   r0   r!   propertyri   r   r   r   r   r   rC   r   ru   D  s    
ru   )r   r   r   r   )r   r   r   r   )%
__future__r   loggingrer   typestypingZ	h2.configrG   Zh2.connectionZ	h2.eventsZ_base_connectionr   _collectionsr   rH   r   r   
exceptionsr   responser	   Zorig_HTTPSConnectionTypeVarr
   	getLoggerr.   logcompiler   r   r   r   Genericr   r3   ru   r   r   r   r   <module>   s2    






 p