o
    #`^h'                     @   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SocketTimeoutError)_EXC_SENTINELBaseTimerContextset_exceptionstatus_code_must_be_empty_body)HttpResponseParserRawResponseMessage)HttpProcessingError)EMPTY_PAYLOAD	DataQueueStreamReaderc                       s  e Zd ZdZdejddfddZedefddZ	edefd	d
Z
d@ddZd@ddZdefddZdee ddf fddZd@ddZd@ fddZd@ fddZefdededdf fddZdededdfdd Zdd!d!d"dd#d$d%d%d&	d'ee d(ed)ed*ed+ee d,ed-ed.ed/eddfd0d1Zd@d2d3Zd@d4d5Zd@d6d7Zedee fd8d9Zej d+ee ddfd:d9Zd@d;d<Z!d=e"ddfd>d?Z#  Z$S )AResponseHandlerz8Helper class to adapt between Protocol and StreamReader.loopreturnNc                 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_parser_timer_tail	_upgraded_parser_read_timeout_read_timeout_handle_timeout_ceil_threshold)selfr    r(   R/home/air/shanriGPT/back/venv/lib/python3.10/site-packages/aiohttp/client_proto.pyr      s   
zResponseHandler.__init__c                 C      | j S N)r"   r'   r(   r(   r)   upgraded/      zResponseHandler.upgradedc                 C   sV   | j d ur
| j  r| jrdS | jp*| jp*|  d up*| jd up*t| dkp*t| 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   s2   | j }|d ur|  d | _ d | _|   d S d S r+   )	transportcloser   _drop_timeout)r'   r7   r(   r(   r)   r8   D   s   zResponseHandler.closec                 C   s   | j d uo
| j   S r+   )r7   
is_closingr,   r(   r(   r)   is_connectedL   s   zResponseHandler.is_connectedexcc           	   
      sT  |    |}|}|d u }| jd ur*tt | j  W d    n1 s%w   Y  d }| jd urgz| j }W n. tyf } z"| jd ur\d|}|sS|d|}t| jt|| W Y d }~nd }~ww | 	 st
|trut|j }|r{t|}|rtn|}|d usJ |d usJ | || d| _d | _d | _d | _d| _t | d S )Nz#Response payload is not completed: z. TF)r9   r   r   	Exceptionfeed_eofr#   r   r   r	   r0   
isinstanceOSErrorr   argsr
   r   r   _reading_pausedsuperconnection_lost)	r'   r<   original_connection_errorreraised_excconnection_closed_cleanlyuncompletedunderlying_excclient_payload_exc_msgunderlying_non_eof_exc	__class__r(   r)   rD   O   s\   





zResponseHandler.connection_lostc                 C      |    d S r+   )r9   r,   r(   r(   r)   eof_received   s   zResponseHandler.eof_receivedc                       t    |   d S r+   )rC   pause_readingr9   r,   rL   r(   r)   rQ         
zResponseHandler.pause_readingc                    rP   r+   )rC   resume_reading_reschedule_timeoutr,   rL   r(   r)   rS      rR   zResponseHandler.resume_reading	exc_causec                    s    d| _ |   t || d S r5   )r   r9   rC   r   )r'   r<   rU   rL   r(   r)   r      s   zResponseHandler.set_exceptionparserpayloadc                 C   s:   || _ || _|   | jr| jd}| _| | d S d S )Nr   )r   r   r9   r!   data_received)r'   rV   rW   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_sizer[   r\   r]   r^   r_   r`   ra   rb   rc   c       	         C   sZ   || _ || _|| _t| | j||t| ||||	d
| _| jr+| jd}
| _| |
 d S d S )N)r[   payload_exceptionresponse_with_bodyr]   r^   rb   rc   r   )	r   r$   r&   r   _loopr	   r#   r!   rX   )r'   r[   r\   r]   r^   r_   r`   ra   rb   rc   rY   r(   r(   r)   set_response_params   s&   z#ResponseHandler.set_response_paramsc                 C   s"   | j d ur| j   d | _ d S d S r+   )r%   cancelr,   r(   r(   r)   r9      s   


zResponseHandler._drop_timeoutc                 C   s>   | j }| jd ur| j  |r| j|| j| _d S d | _d S r+   )r$   r%   rh   rf   
call_later_on_read_timeout)r'   timeoutr(   r(   r)   rT      s   



z#ResponseHandler._reschedule_timeoutc                 C   rN   r+   )rT   r,   r(   r(   r)   start_timeout   s   zResponseHandler.start_timeoutc                 C   r*   r+   r$   r,   r(   r(   r)   r_      r.   zResponseHandler.read_timeoutc                 C   s
   || _ d S r+   rm   )r'   r_   r(   r(   r)   r_      s   
c                 C   s0   t d}| | | jd urt| j| d S d S )Nz#Timeout on reading data from socket)r   r   r   )r'   r<   r(   r(   r)   rj      s
   

z ResponseHandler._on_read_timeoutrY   c           	   
   C   st  |    |sd S | jd ur&| j|\}}|r$d | _d | _|r$| | d S | js.| jd u r7|  j|7  _d S z| j|\}}}W n$ tyf } z| j	d urT| j	
  | t | W Y d }~d S d }~ww || _d }|D ]&\}}|jrxd| _|| _| jst|jr| |tfd qn| ||fd qn|d ur|tur|| j n|   |r|r| | d S || _d S d S r/   )rT   r   	feed_datar   rX   r"   r#   r!   BaseExceptionr7   r8   r   r   r4   r   r   r   coder   on_eofr9   )	r'   rY   eoftailmessagesr-   rI   rW   messager(   r(   r)   rX      sT   





zResponseHandler.data_received)r   N)%__name__
__module____qualname____doc__asyncioAbstractEventLoopr   propertyr3   r-   r4   r6   r8   r;   r   ro   rD   rO   rQ   rS   r   r   r   rZ   r   floatintrg   r9   rT   rl   r_   setterrj   bytesrX   __classcell__r(   r(   rL   r)   r      s|    


8		


$


r   )rz   
contextlibr   typingr   r   r   base_protocolr   client_exceptionsr   r	   r
   r   helpersr   r   r   r   httpr   r   http_exceptionsr   streamsr   r   r   r   r(   r(   r(   r)   <module>   s    "