o
    .j4                     @  s2  U d dl mZ d dl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	mZ ddlmZ dd	lmZ dd
lmZmZmZmZmZmZmZmZ ddlmZ e dd ej!ddddd D dkZ"de#d< G dd de	j$j%Z&e	jj'e	jj(hZ)e	jj*e	jj+hZ,G dd deZ-dS )    )annotations)deque)token_bytes)IteratorN)__version__   )StreamMatrix)HeadersType)ConnectionTerminatedDataReceivedEarlyHeadersReceivedEventGoawayReceivedHandshakeCompletedHeadersReceivedStreamResetReceived   )HTTP2Protocolc                 c  s    | ]}t |V  qd S N)int).0p r   ^/home/thesage/.local/lib/python3.10/site-packages/urllib3/contrib/hface/protocols/http2/_h2.py	<genexpr>3   s    
r   .)maxsplit)   r      boolJH2_NOT_RFC_COMPLIANT_SETTINGSc                      s>   e Zd ZdZ		dd fd	d
Zd fddZdd Z  ZS )_PatchedH2Connectionzm
    This is a performance hotfix class. We internally, already keep
    track of the open stream count.
    Nconfig!jh2.config.H2Configuration | Noneobservable_implHTTP2ProtocolHyperImpl | NonereturnNonec                   sv   t  j|d ts tjjjdtjjjdtjjjdtjjj	di}ntjjj
dtjjj	| ji}tjjd|d| _|| _d S )	N)r"   i   r   i  ` i   d   T)clientinitial_values)super__init__r    jh2settingsSettingCodesHEADER_TABLE_SIZEENABLE_PUSHINITIAL_WINDOW_SIZEMAX_HEADER_LIST_SIZEMAX_CONCURRENT_STREAMSDEFAULT_MAX_HEADER_LIST_SIZESettingslocal_settings_observable_impl)selfr"   r$   initial_settings	__class__r   r   r,   >   s   





z_PatchedH2Connection.__init__r   c                   s$   | j d ur	| j jS t j|i |S r   )r8   _open_stream_countr+   _open_streams)r9   argskwargsr;   r   r   r>   X   s   
z"_PatchedH2Connection._open_streamsc                 C  s   | j tjjj}tj|j}|dkr&| j	dur&| j	j
dkr&tjjj| j _|   tj }||_|j|_|jr<|jnd|_|| g |fS )z
        Receive a GOAWAY frame on the connection.
        We purposely override this method to work around a known bug of jh2.
        r   N)state_machineprocess_inputr-   
connectionConnectionInputsRECV_GOAWAYerrors_error_code_from_int
error_coder8   r=   ConnectionStateCLIENT_OPENstateclear_outbound_data_buffereventsr
   last_stream_idadditional_dataappend)r9   framerM   err_code	new_eventr   r   r   _receive_goaway_frame]   s    


z*_PatchedH2Connection._receive_goaway_frame)NN)r"   r#   r$   r%   r&   r'   r&   r   )__name__
__module____qualname____doc__r,   r>   rT   __classcell__r   r   r;   r   r!   8   s    r!   c                   @  s<  e Zd ZU dZded< ddddddWddZdXddZedYddZdZddZ	e
dXddZdZddZdZddZdXddZd[d\d"d#Z	d]d^d(d)Z	d]d_d,d-Zd[d`d.d/Zdadbd3d4Zd0d0d5dcd8d9Zddd=d>Zded?d@ZdedAdBZdfdCdDZdgdEdFZ	0dhdidIdJZ	0dadjdMdNZdkdQdRZdedSdTZdZdUdVZd0S )lHTTP2ProtocolHyperImplh2strimplementationFT)validate_outbound_headersvalidate_inbound_headersnormalize_outbound_headersnormalize_inbound_headersr_   r   r`   ra   rb   r&   r'   c                C  s   t tjjd||||d| d| _d| _| j  tr!| jd n| jd t	 | _
d| _d| _| jjj| _| jjj| _t | _d S )NT)client_sider_   ra   r`   rb   )r$   r   i   i  F)r!   r-   r"   H2Configuration_connectionr=   initiate_connectionr    increment_flow_control_windowr   _events_terminated_goaway_to_honorremote_settingsmax_concurrent_streams_max_stream_countmax_frame_size_max_frame_sizer   _pending_ping_ack)r9   r_   r`   ra   rb   r   r   r   r,      s,   

zHTTP2ProtocolHyperImpl.__init__r   c                 C     | j S r   )ro   r9   r   r   r   rn      s   z%HTTP2ProtocolHyperImpl.max_frame_sizetuple[type[BaseException], ...]c                   C  s   t jjt jjfS r   )r-   
exceptionsProtocolErrorH2Errorr   r   r   r   rt      s   z!HTTP2ProtocolHyperImpl.exceptionsc                 C  s   | j s| jrdS | j| jkS )NF)ri   rj   rm   r=   rr   r   r   r   is_available   s   z#HTTP2ProtocolHyperImpl.is_availablec                 C  rq   r   )rm   rr   r   r   r   max_stream_count   s   z'HTTP2ProtocolHyperImpl.max_stream_countc                 C  s   | j jrdS | j o| jdkS )NFr   )rh   stream_countri   r=   rr   r   r   r   is_idle   s   zHTTP2ProtocolHyperImpl.is_idlec                 C  s4   t | jdkr| j  d| _| jp| jo| jj S )Nr   T)lenrp   re   close_connectionri   rj   rh   ry   rr   r   r   r   has_expired   s   

z"HTTP2ProtocolHyperImpl.has_expiredc                 C  
   | j  S r   )re   get_next_available_stream_idrr   r   r   r   get_available_stream_id      
z.HTTP2ProtocolHyperImpl.get_available_stream_idr   rH   c                 C  s   | j | d S r   )re   r|   )r9   rH   r   r   r   submit_close   s   z#HTTP2ProtocolHyperImpl.submit_close	stream_idheadersr	   
end_streamc                 C  s"   | j ||| |  jd7  _d S )N   )re   send_headersr=   )r9   r   r   r   r   r   r   submit_headers   s   z%HTTP2ProtocolHyperImpl.submit_headersdatabytesc                 C  s   | j ||| d S r   )re   	send_data)r9   r   r   r   r   r   r   submit_data   s   z"HTTP2ProtocolHyperImpl.submit_datac                 C  s   | j || d S r   )re   reset_stream)r9   r   rH   r   r   r   submit_stream_reset   s   z*HTTP2ProtocolHyperImpl.submit_stream_resetN
int | NoneEvent | Nonec                 C  s   | j j|dS )N)r   )rh   popleft)r9   r   r   r   r   
next_event   s   z!HTTP2ProtocolHyperImpl.next_eventr   
excl_eventr   tuple[type[Event], ...] | Nonec                C  s   | j j||dS )Nr   )rh   has)r9   r   r   r   r   r   has_pending_event   s   z(HTTP2ProtocolHyperImpl.has_pending_event	h2_eventslist[jh2.events.Event]Iterator[Event]c           	      c  s   | j }|D ]}|j}|tv r6|j}|jd u}|r,|  jd8  _|j|}|j|j	|< t
||j|dV  q|tjju rj|j}|jd u}|rY|  jd8  _|j|}|j|j	|< ||j| t||j|dV  q|tjju ryt|j|jV  q|tjju r|  jd8  _|j}||jv r|j|}|j|j	|< t||jV  q|tjju r|jdkrd| _t|j|jV  qd| _t|jd V  q|tv rtddV  q|tjju r|j}|| j v r| j !| qd S )Nr   )r   r   Tr\   )alpn_protocol)"re   r<   HEADER_OR_TRAILER_TYPE_SETr   stream_endedr=   streamspop	closed_by_closed_streamsr   r   r-   rM   r   acknowledge_received_dataflow_controlled_lengthr   InformationalResponseReceivedr   StreamResetr   rH   r
   rj   r   rN   ri   SETTINGS_EVENT_SETr   PingAckReceived	ping_datarp   remove)	r9   r   conneev_typer   r   streamuidr   r   r   _map_events   s^   




z"HTTP2ProtocolHyperImpl._map_eventsc                 C     |    d S r   _connection_terminatedrr   r   r   r   connection_lost'     z&HTTP2ProtocolHyperImpl.connection_lostc                 C  r   r   r   rr   r   r   r   eof_received*  r   z#HTTP2ProtocolHyperImpl.eof_receivedc              
   C  s   |sd S z| j |}W n tjjy) } z| |jt| W Y d }~nd }~ww | j	| 
| | jr:| j| _| j jjrP| jsH| j jj| _| j jj| _d S d S r   )re   receive_datar-   rt   ru   r   rH   r]   rh   extendr   rj   r=   rm   rk   
has_updaterl   rn   ro   )r9   r   r   r   r   r   r   bytes_received-  s"   
z%HTTP2ProtocolHyperImpl.bytes_receivedc                 C  r~   r   )re   data_to_sendrr   r   r   r   bytes_to_sendC  r   z$HTTP2ProtocolHyperImpl.bytes_to_sendmessage
str | Nonec                 C  s.   | j rd S t|}d| _ | jt|| d S )NT)ri   r   rh   rP   r
   )r9   rH   r   r   r   r   r   F  s
   z-HTTP2ProtocolHyperImpl._connection_terminatedamtbool | Nonec                 C  s$   | j |}|d u r|dkS ||kS )Nr   )re   local_flow_control_window)r9   r   r   flow_remaining_bytesr   r   r   should_wait_remote_flow_controlO  s   z6HTTP2ProtocolHyperImpl.should_wait_remote_flow_controlrM   r   c                 G  s   t |D ]}| j| qd S r   )reversedrh   
appendleft)r9   rM   evr   r   r   reshelve[  s   zHTTP2ProtocolHyperImpl.reshelvec                 C  s$   t d}| j| | j| d S )N   )r   rp   rP   re   ping)r9   r   r   r   r   r   _  s   zHTTP2ProtocolHyperImpl.pingc                 C  s
   t | jS r   )r   rp   rr   r   r   r   expect_pongd  r   z"HTTP2ProtocolHyperImpl.expect_pong)
r_   r   r`   r   ra   r   rb   r   r&   r'   rU   )r&   rs   )r&   r   )r   )rH   r   r&   r'   )F)r   r   r   r	   r   r   r&   r'   )r   r   r   r   r   r   r&   r'   )r   r   rH   r   r&   r'   r   )r   r   r&   r   )r   r   r   r   r&   r   )r   r   r&   r   )r&   r'   )r   r   r&   r'   )r&   r   )r   N)rH   r   r   r   r&   r'   )r   r   r   r   r&   r   )rM   r   r&   r'   )rV   rW   rX   r^   __annotations__r,   rn   staticmethodrt   rw   propertyrx   rz   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r[      sL   
 
"





8





r[   ).
__future__r   collectionsr   secretsr   typingr   
jh2.configr-   jh2.connection
jh2.errors
jh2.eventsjh2.exceptionsjh2.settingsr   jh2_version_stream_matrixr   _typingr	   rM   r
   r   r   r   r   r   r   r   
_protocolsr   tuplesplitr    r   rC   H2Connectionr!   ResponseReceivedTrailersReceivedr   SettingsAcknowledgedRemoteSettingsChangedr   r[   r   r   r   r   <module>   s8   (
L