a
    nJh&                     @   s   d dl Z d dlmZ d dlmZmZmZ ddlmZ ddl	m
Z
mZmZmZ ddl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   )BaseProtocol)ClientOSErrorClientPayloadErrorServerDisconnectedErrorServerTimeoutError)_EXC_SENTINELBaseTimerContextset_exceptionstatus_code_must_be_empty_body)HttpResponseParserRawResponseMessage)HttpProcessingError)EMPTY_PAYLOAD	DataQueueStreamReaderc                       sp  e Zd ZdZejd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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%	ee eeeee eeeedd&
d'd(Zddd)d*Zddd+d,Zddd-d.Zddd/d0Ze dd1d2d3Z!  Z"S )4ResponseHandlerz8Helper class to adapt between Protocol and StreamReader.N)loopreturnc                 C   s`   t j| |d t| | 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)selfr    r'   F/var/www/shaz/venv/lib/python3.9/site-packages/aiohttp/client_proto.pyr      s    zResponseHandler.__init__)r   c                 C   s   | j S N)r!   r&   r'   r'   r(   upgraded/   s    zResponseHandler.upgradedc                 C   sV   | j d ur| j  r| jrdS | jpT| jpT|  d upT| jd upTt| dkpTt| jS NTr   )	r   is_eofr!   r   	exceptionr   lenboolr    r*   r'   r'   r(   should_close3   s    

zResponseHandler.should_closec                 C   s
   d| _ d S NT)r   r*   r'   r'   r(   force_closeA   s    zResponseHandler.force_closec                 C   s.   | j }|d ur*|  d | _ d | _|   d S r)   )	transportcloser   _drop_timeout)r&   r4   r'   r'   r(   r5   D   s    zResponseHandler.closec                 C   s   | j d uo| j   S r)   )r4   Z
is_closingr*   r'   r'   r(   is_connectedL   s    zResponseHandler.is_connected)excr   c           	   
      s\  |    |}|}|d u }| jd urTtt | j  W d    n1 sJ0    Y  d }| jd urz| j }W n\ ty } zD| jd urd|}|s|d|}t| jt|| W Y d }~n
d }~0 0 | 	 s.t
|trt|j }|rt|}|rtn|}|d usJ |d us"J | || d| _d | _d | _d | _d| _t | d S )Nz#Response payload is not completed: z. TF)r6   r   r   	ExceptionZfeed_eofr"   r   r   r	   r-   
isinstanceOSErrorr   argsr
   r   r   Z_reading_pausedsuperconnection_lost)	r&   r8   Zoriginal_connection_errorZreraised_excZconnection_closed_cleanlyZuncompletedunderlying_excZclient_payload_exc_msgZunderlying_non_eof_exc	__class__r'   r(   r>   O   sV    

(




zResponseHandler.connection_lostc                 C   s   |    d S r)   )r6   r*   r'   r'   r(   eof_received   s    zResponseHandler.eof_receivedc                    s   t    |   d S r)   )r=   pause_readingr6   r*   r@   r'   r(   rC      s    
zResponseHandler.pause_readingc                    s   t    |   d S r)   )r=   resume_reading_reschedule_timeoutr*   r@   r'   r(   rD      s    
zResponseHandler.resume_reading)r8   	exc_causer   c                    s    d| _ |   t || d S r2   )r   r6   r=   r   )r&   r8   rF   r@   r'   r(   r      s    zResponseHandler.set_exception)parserpayloadr   c                 C   s6   || _ || _|   | jr2| jd }| _| | d S )Nr   )r   r   r6   r    data_received)r&   rG   rH   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)
rL   rM   rN   rO   rP   rQ   rR   rS   rT   r   c       	         C   sV   || _ || _|| _t| | j||t| ||||	d
| _| jrR| jd }
| _| |
 d S )N)rL   Zpayload_exceptionZresponse_with_bodyrN   rO   rS   rT   r   )	r   r#   r%   r   _loopr	   r"   r    rI   )r&   rL   rM   rN   rO   rP   rQ   rR   rS   rT   rJ   r'   r'   r(   set_response_params   s$    z#ResponseHandler.set_response_paramsc                 C   s   | j d ur| j   d | _ d S r)   )r$   cancelr*   r'   r'   r(   r6      s    

zResponseHandler._drop_timeoutc                 C   s<   | j }| jd ur| j  |r2| j|| j| _nd | _d S r)   )r#   r$   rW   rU   Z
call_later_on_read_timeout)r&   timeoutr'   r'   r(   rE      s    

z#ResponseHandler._reschedule_timeoutc                 C   s   |    d S r)   )rE   r*   r'   r'   r(   start_timeout   s    zResponseHandler.start_timeoutc                 C   s,   t d}| | | jd ur(t| j| d S )Nz#Timeout on reading data from socket)r   r   r   )r&   r8   r'   r'   r(   rX      s    

z ResponseHandler._on_read_timeout)rJ   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  _n
z| j|\}}}W nH ty } z0| j	d ur| j	
  | t | W Y d }~d S d }~0 0 || _d }|D ]R\}}|j rd| _|| _| jst|jr| |tfd q| ||fd q|d urZ|turR|| j n|   |rx|rr| | n|| _d S r,   )rE   r   Z	feed_datar   rI   r!   r"   r    BaseExceptionr4   r5   r   r   r1   r   r   r   coder   Zon_eofr6   )	r&   rJ   eoftailmessagesr+   r?   rH   messager'   r'   r(   rI      sN    






zResponseHandler.data_received)#__name__
__module____qualname____doc__asyncioZAbstractEventLoopr   propertyr0   r+   r1   r3   r5   r7   r   r[   r>   rB   rC   rD   r   r   r   rK   r   floatintrV   r6   rE   rZ   rX   bytesrI   __classcell__r'   r'   r@   r(   r      s\   8	$r   )re   
contextlibr   typingr   r   r   Zbase_protocolr   Zclient_exceptionsr   r	   r
   r   Zhelpersr   r   r   r   httpr   r   Zhttp_exceptionsr   streamsr   r   r   r   r'   r'   r'   r(   <module>   s   