a
    8öj“/  ã                   @   sÂ   d dl Z d dlmZ d dlmZmZmZmZ ddlm	Z	 ddl
mZmZmZmZmZ ddlmZmZmZmZmZ ddlmZmZ dd	lmZ dd
lmZmZmZ G dd„ de	eeeef  ƒZdS )é    N)Úsuppress)ÚAnyÚOptionalÚTupleÚUnioné   )ÚBaseProtocol)ÚClientConnectionErrorÚClientOSErrorÚClientPayloadErrorÚServerDisconnectedErrorÚSocketTimeoutError)Ú_EXC_SENTINELÚEMPTY_BODY_STATUS_CODESÚBaseTimerContextÚset_exceptionÚ
set_result)ÚHttpResponseParserÚRawResponseMessage)ÚHttpProcessingError)ÚEMPTY_PAYLOADÚ	DataQueueÚStreamReaderc                       sÒ  e Zd ZdZejddœdd„Zeedej	d f dœdd„ƒZ
eedœd	d
„ƒZeedœdd„ƒZddœdd„Zddœdd„Zddœdd„Zedœdd„Zee ddœ‡ fdd„Zddœdd„Zddœ‡ fdd„Zddœ‡ fdd„Zefeeddœ‡ fdd „Zeedd!œd"d#„Zdd$d$d%dd&d'd(d(d)d*œ
ee eeeee eeeeedd+œd,d-„Zddœd.d/„Z ddœd0d1„Z!ddœd2d3„Z"eee dœd4d5„ƒZ#e#j$ee dd6œd7d5„ƒZ#ddœd8d9„Z%e&dd:œd;d<„Z'‡  Z(S )=ÚResponseHandlerz8Helper class to adapt between Protocol and StreamReader.N)ÚloopÚreturnc                 C   sl   t j| |d� t | |¡ d| _d | _d| _d | _d | _d| _d| _	d | _
d | _d | _d| _d | _d| _d S )N)r   Fó    é   )r   Ú__init__r   Ú_should_closeÚ_payloadÚ_skip_payloadÚ_payload_parserZ_timerÚ_tailÚ	_upgradedÚ_parserÚ_read_timeoutÚ_read_timeout_handleÚ_timeout_ceil_thresholdÚ_closedÚ_connection_lost_called)Úselfr   © r,   úS/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/aiohttp/client_proto.pyr      s    zResponseHandler.__init__)r   c                 C   s"   | j du r| js| j ¡ | _ | j S )a6  Future that is set when the connection is closed.

        This property returns a Future that will be completed when the connection
        is closed. The Future is created lazily on first access to avoid creating
        futures that will never be awaited.

        Returns:
            - A Future[None] if the connection is still open or was closed after
              this property was accessed
            - None if connection_lost() was already called before this property
              was ever accessed (indicating no one is waiting for the closure)
        N)r)   r*   Ú_loopZcreate_future©r+   r,   r,   r-   Úclosed4   s    zResponseHandler.closedc                 C   s   | j S ©N)r$   r/   r,   r,   r-   ÚupgradedF   s    zResponseHandler.upgradedc                 C   sF   t | jpB| jd ur| j ¡  pB| jpB| jd upB| jd upB| jpB| jƒS r1   )	Úboolr   r    Úis_eofr$   Ú
_exceptionr"   Ú_bufferr#   r/   r,   r,   r-   Úshould_closeJ   s    ÿþýüûùzResponseHandler.should_closec                 C   s
   d| _ d S ©NT)r   r/   r,   r,   r-   Úforce_closeV   s    zResponseHandler.force_closec                 C   s4   d | _ | j}|d ur0| ¡  d | _d | _|  ¡  d S r1   )r5   Ú	transportÚcloser    Ú_drop_timeout©r+   r:   r,   r,   r-   r;   Y   s    zResponseHandler.closec                 C   s4   d | _ | j}|d ur0| ¡  d | _d | _|  ¡  d S r1   )r5   r:   Úabortr    r<   r=   r,   r,   r-   r>   b   s    zResponseHandler.abortc                 C   s   | j d uo| j  ¡  S r1   )r:   Z
is_closingr/   r,   r,   r-   Úis_connectedk   s    zResponseHandler.is_connected)Úexcr   c           	   
      s¬  d| _ |  ¡  |}|}|d u }| jd ur^|r:t| jd ƒ n$|d usFJ ‚t| jtd|›�ƒ|ƒ | jd uršttƒ� | j 	¡  W d   ƒ n1 s�0    Y  d }| j
d u�rz| j
 	¡ }W n` t�y } zF| jd u�rd|›�}|sò|›d|›�}t| jt|ƒ|ƒ W Y d }~n
d }~0 0 |  ¡ �s~t|tƒ�r:t|jŽ }|�rHt|ƒ}|�rRtn|}|d u�sdJ ‚|d u�srJ ‚|  ||¡ d| _d | _
d | _d | _d| _tƒ  |¡ d S )NTzConnection lost: z#Response payload is not completed: z. F)r*   r<   r)   r   r   r	   r"   r   Ú	ExceptionZfeed_eofr%   r    r   r4   Ú
isinstanceÚOSErrorr
   Úargsr   r   r   Z_reading_pausedÚsuperÚconnection_lost)	r+   r@   Zoriginal_connection_errorZreraised_excZconnection_closed_cleanlyZuncompletedÚunderlying_excZclient_payload_exc_msgZunderlying_non_eof_exc©Ú	__class__r,   r-   rF   n   sn    
ÿû

(ÿÿÿý

ÿýzResponseHandler.connection_lostc                 C   s   |   ¡  d S r1   )r<   r/   r,   r,   r-   Úeof_received¹   s    zResponseHandler.eof_receivedc                    s   t ƒ  ¡  |  ¡  d S r1   )rE   Úpause_readingr<   r/   rH   r,   r-   rK   ½   s    
zResponseHandler.pause_readingc                    s   t ƒ  ¡  |  ¡  d S r1   )rE   Úresume_readingÚ_reschedule_timeoutr/   rH   r,   r-   rL   Á   s    
zResponseHandler.resume_reading)r@   Ú	exc_causer   c                    s    d| _ |  ¡  tƒ  ||¡ d S r8   )r   r<   rE   r   )r+   r@   rN   rH   r,   r-   r   Å   s    zResponseHandler.set_exception)ÚparserÚpayloadr   c                 C   s6   || _ || _|  ¡  | jr2| jd }| _|  |¡ d S )Nr   )r    r"   r<   r#   Údata_received)r+   rO   rP   Údatar,   r,   r-   Ú
set_parserÎ   s    zResponseHandler.set_parserFTi   r   iþ  é€   )
ÚtimerÚskip_payloadÚread_until_eofÚauto_decompressÚread_timeoutÚread_bufsizeÚtimeout_ceil_thresholdÚmax_line_sizeÚmax_field_sizeÚmax_headers)rU   rV   rW   rX   rY   rZ   r[   r\   r]   r^   r   c       
         C   sX   || _ || _|| _t| | j||t| ||||	|
d�| _| jrT| jd }| _|  |¡ d S )N)rU   Zpayload_exceptionZresponse_with_bodyrW   rX   r\   r]   r^   r   )	r!   r&   r(   r   r.   r   r%   r#   rQ   )r+   rU   rV   rW   rX   rY   rZ   r[   r\   r]   r^   rR   r,   r,   r-   Úset_response_paramsÝ   s&    õz#ResponseHandler.set_response_paramsc                 C   s   | j d ur| j  ¡  d | _ d S r1   )r'   Úcancelr/   r,   r,   r-   r<     s    

zResponseHandler._drop_timeoutc                 C   s<   | j }| jd ur| j ¡  |r2| j || j¡| _nd | _d S r1   )r&   r'   r`   r.   Z
call_laterÚ_on_read_timeout)r+   Útimeoutr,   r,   r-   rM     s    

ÿz#ResponseHandler._reschedule_timeoutc                 C   s   |   ¡  d S r1   )rM   r/   r,   r,   r-   Ústart_timeout  s    zResponseHandler.start_timeoutc                 C   s   | j S r1   ©r&   r/   r,   r,   r-   rY     s    zResponseHandler.read_timeout)rY   r   c                 C   s
   || _ d S r1   rd   )r+   rY   r,   r,   r-   rY     s    c                 C   s,   t dƒ}|  |¡ | jd ur(t| j|ƒ d S )Nz#Timeout on reading data from socket)r   r   r    )r+   r@   r,   r,   r-   ra     s    

z ResponseHandler._on_read_timeout)rR   r   c           
   
   C   sš  |   ¡  |sd S | jd urL| j |¡\}}|rHd | _d | _|rH|  |¡ d S | js\| jd u rn|  j|7  _d S z| j |¡\}}}W nl tyð } zT| j	d ur¨| j	 
¡  t|tƒrÈt|j|j|jd�}ntƒ }|  ||¡ W Y d }~d S d }~0 0 || _d }|D ]T\}	}|	j�rd| _|| _| j�s0|	jtv �rB|  |	tfd¡ n|  |	|fd¡ �q |d u�r€|tu�rx| | j¡ n|  ¡  |�r–|�r–|  |¡ d S )N)ÚcodeÚmessageÚheadersTr   )rM   r"   Z	feed_datar    rQ   r$   r%   r#   ÚBaseExceptionr:   r;   rB   r   re   rf   rg   r   r7   r   r!   r   r   Zon_eofr<   )
r+   rR   ÚeofÚtailÚmessagesr2   rG   r@   rP   rf   r,   r,   r-   rQ   %  sV    




ý

zResponseHandler.data_received))Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚasyncioZAbstractEventLoopr   Úpropertyr   ÚFuturer0   r3   r2   r7   r9   r;   r>   r?   r   rh   rF   rJ   rK   rL   r   r   r   rS   r   ÚfloatÚintr_   r<   rM   rc   rY   Úsetterra   ÚbytesrQ   Ú__classcell__r,   r,   rH   r-   r      sn   		Kýü	ôó&r   ) rp   Ú
contextlibr   Útypingr   r   r   r   Zbase_protocolr   Zclient_exceptionsr	   r
   r   r   r   Zhelpersr   r   r   r   r   Úhttpr   r   Zhttp_exceptionsr   Ústreamsr   r   r   r   r,   r,   r,   r-   Ú<module>   s   