
    g'              	           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eeeeef            Zy)    N)suppress)AnyOptionalTuple   )BaseProtocol)ClientOSErrorClientPayloadErrorServerDisconnectedErrorSocketTimeoutError)_EXC_SENTINELEMPTY_BODY_STATUS_CODESBaseTimerContextset_exception)HttpResponseParserRawResponseMessage)HttpProcessingError)EMPTY_PAYLOAD	DataQueueStreamReaderc                       e Zd ZdZdej
                  ddfdZedefd       Z	edefd       Z
d-dZd-d	Zdefd
Zdee   ddf fdZd-dZd- fdZd- fdZefdededdf fdZdededdf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d$Zd-d%Zd-d&Zd-d'Zedee   fd(       Zej@                  dee   ddfd)       Zd-d*Z!d+e"ddfd,Z# xZ$S ).ResponseHandlerz8Helper class to adapt between Protocol and StreamReader.loopreturnNc                     t        j                  | |       t        j                  | |       d| _        d | _        d| _        d | _        d | _        d| _        d| _	        d | _
        d | _        d | _        d| _        y )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   s     I/var/www/openai/venv/lib/python3.12/site-packages/aiohttp/client_proto.pyr   zResponseHandler.__init__   su    d.4&"04"#
59.2CG!89$r   c                     | j                   S N)r%   r*   s    r+   upgradedzResponseHandler.upgraded/   s    ~~r   c                    t        | j                  xsu | j                  d uxr | j                  j                          xsH | j                  xs: | j
                  d uxs* | j                  d uxs | j                  xs | j                        S r-   )	boolr   r    is_eofr%   
_exceptionr"   _bufferr$   r.   s    r+   should_closezResponseHandler.should_close3   s     T)H$--2F2F2H.H~~ d* ##4/	
 || zz
 	
r   c                     d| _         y NT)r   r.   s    r+   force_closezResponseHandler.force_close?   s
    !r   c                 ~    | j                   }|/|j                          d | _         d | _        | j                          y y r-   )	transportcloser    _drop_timeout)r*   r:   s     r+   r;   zResponseHandler.closeB   s:    NN	 OO!DN DM 	 !r   c                 X    | j                   d uxr | j                   j                          S r-   )r:   
is_closingr.   s    r+   is_connectedzResponseHandler.is_connectedJ   s&    ~~T)M$..2K2K2M.MMr   excc                    | j                          |}|}|d u }| j                  2t        t              5  | j                  j	                          d d d        d }| j
                  	 | j
                  j	                         }| j                         sSt        |t              rt        |j                   }|rt        |      }|rt        n|}|J |J | j                  ||       d| _        d | _        d | _        d | _        d| _        t$        	| M  |       y # 1 sw Y   xY w# t        $ rD}| j                  .d|}|s|d|}t        | j                  t        |      |       Y d }~d }~ww xY w)Nz#Response payload is not completed: z. TF)r<   r"   r   	Exceptionfeed_eofr&   r    r   r
   r2   
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__s
            r+   rI   zResponseHandler.connection_lostM   s   $'!0$=$E!+)$$$--/ % <<#"ll335" {{}3W=,.G.L.LM(6{C
 - . #
 *555+++|-CD!#$-[ %$  ==,=n=PQ + 5598<> / "*+AB&s#   D'D# D #	E0,:E++E0c                 $    | j                          y r-   )r<   r.   s    r+   eof_receivedzResponseHandler.eof_received   s    r   c                 B    t         |           | j                          y r-   )rH   pause_readingr<   r*   rQ   s    r+   rU   zResponseHandler.pause_reading   s    r   c                 B    t         |           | j                          y r-   )rH   resume_reading_reschedule_timeoutrV   s    r+   rX   zResponseHandler.resume_reading   s       "r   	exc_causec                 T    d| _         | j                          t        |   ||       y r7   )r   r<   rH   r   )r*   r@   rZ   rQ   s      r+   r   zResponseHandler.set_exception   s(    
 "c9-r   parserpayloadc                     || _         || _        | j                          | j                  r&| j                  dc}| _        | j	                  |       y y )Nr   )r    r"   r<   r$   data_received)r*   r\   r]   datas       r+   
set_parserzResponseHandler.set_parser   sK      %::#zz3D$*t$ r   FTi   r   i  )	timerskip_payloadread_until_eofauto_decompressread_timeoutread_bufsizetimeout_ceil_thresholdmax_line_sizemax_field_sizerb   rc   rd   re   rf   rg   rh   ri   rj   c       	             || _         || _        || _        t        | | j                  ||t
        | ||||	
      | _        | j                  r&| j                  dc}
| _        | j                  |
       y y )N)rb   payload_exceptionresponse_with_bodyrd   re   ri   rj   r   )	r!   r'   r)   r   _loopr
   r&   r$   r_   )r*   rb   rc   rd   re   rf   rg   rh   ri   rj   r`   s              r+   set_response_paramsz#ResponseHandler.set_response_params   s{     *)'=$)JJ0#//)+')
 ::#zz3D$*t$ r   c                 `    | j                   "| j                   j                          d | _         y y r-   )r(   cancelr.   s    r+   r<   zResponseHandler._drop_timeout   s-    $$0%%,,.(,D% 1r   c                     | j                   }| j                  | j                  j                          |r,| j                  j	                  || j
                        | _        y d | _        y r-   )r'   r(   rq   rn   
call_later_on_read_timeout)r*   timeouts     r+   rY   z#ResponseHandler._reschedule_timeout   sZ    $$$$0%%,,.(,

(=(=..)D% )-D%r   c                 $    | j                          y r-   )rY   r.   s    r+   start_timeoutzResponseHandler.start_timeout   s      "r   c                     | j                   S r-   r'   r.   s    r+   rf   zResponseHandler.read_timeout   s    !!!r   c                     || _         y r-   ry   )r*   rf   s     r+   rf   zResponseHandler.read_timeout   s
    )r   c                     t        d      }| j                  |       | j                  t        | j                  |       y y )Nz#Timeout on reading data from socket)r   r   r    )r*   r@   s     r+   rt   z ResponseHandler._on_read_timeout   s8     !FG3==$$--- %r   r`   c                    | j                          |sy | j                  B| j                  j                  |      \  }}|r!d | _        d | _        |r| j	                  |       y | j
                  s| j                  | xj                  |z  c_        y 	 | j                  j                  |      \  }}}|| _        d }|D ]j  \  }	}|	j"                  rd| _        || _        | j&                  s|	j                  t(        v r| j                  |	t*        fd       W| j                  |	|fd       l |4|t*        ur|j-                  | j.                         n| j/                          |r|r| j	                  |       y y y # t        $ r}| j                  | j                  j                          t        |t              r-t        |j                  |j                  |j                        }n
t               }| j!                  ||       Y d }~y d }~ww xY w)N)codemessageheadersTr   )rY   r"   	feed_datar    r_   r%   r&   r$   BaseExceptionr:   r;   rD   r   r}   r~   r   r   r5   r   r!   r   r   on_eofr<   )
r*   r`   eoftailmessagesr/   rN   r@   r]   r~   s
             r+   r_   zResponseHandler.data_received   s     " +,,66t<IC $'+$&&t,>>T\\1JJ$J	'+||'='=d'C$Hh& "*. (GW##%)"#DM!!W\\5L%L7;115 !) 
 m+t112""$t$ 8U  	~~) $$&.*=>)',,*22*22 *+sN3!	s   E6 6	H?A?HH)r   N)%__name__
__module____qualname____doc__asyncioAbstractEventLoopr   propertyr1   r/   r5   r8   r;   r?   r   r   rI   rS   rU   rX   r   r   r   ra   r   floatintro   r<   rY   rw   rf   setterrt   bytesr_   __classcell__)rQ   s   @r+   r   r      s   B:W66 :4 :* $   	
d 	
 	
"!Nd N6.8M#: 6.t 6.p# $1.. !. 
	.% %s %t %$ -1"$ $(,!()!""% ()"% 	"%
 "% "% uo"% "% !&"% "% "% 
"%H-

-# "huo " " *% *T * *.D%% D%D D%r   r   )r   
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+   <module>r      sV      ' ' '   9 0 ; ;\%lIe4F4T.U$V \%r   