a
     ÑOcW  ã                   @   s  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 d dlmZ d dlmZmZmZmZmZmZmZmZmZmZmZ d dlZd dlZdd	lmZ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( ddl)m*Z*m+Z+ ddl,m-Z-m.Z. ddl/m0Z0 ddl1m2Z2 ddl3m4Z4 ddl5m6Z6 ddl7m8Z8m9Z9 dZ:e�rZddl;m<Z< ee'e.dedge6f Z=ee6gee9 f Z>e'dde&i i dddde ?d¡ƒ
Z@G dd„ deAƒZBG dd„ deAƒZCejDdddd �G d!d"„ d"ƒƒZEeee'eEf e.f ZFG d#d„ de!ƒZGdS )$é    N)Údeque)Úsuppress)Úescape)Ú
HTTPStatus)ÚLogger)ÚTYPE_CHECKINGÚAnyÚ	AwaitableÚCallableÚDequeÚOptionalÚSequenceÚTupleÚTypeÚUnionÚcasté   )ÚAbstractAccessLoggerÚAbstractStreamWriter)ÚBaseProtocol)Úceil_timeout)ÚHttpProcessingErrorÚHttpRequestParserÚHttpVersion10ÚRawRequestMessageÚStreamWriter)Úaccess_loggerÚserver_logger)ÚEMPTY_PAYLOADÚStreamReader)Útcp_keepalive)ÚHTTPException)ÚAccessLogger)ÚBaseRequest)ÚResponseÚStreamResponse)ÚRequestHandlerÚRequestPayloadErrorÚPayloadAccessError)ÚServerr&   zasyncio.Task[None]ÚUNKNOWNú/TFc                   @   s   e Zd ZdZdS )r'   zPayload parsing error.N©Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r1   r1   úK/var/www/html/gsl.tools/lib/python3.9/site-packages/aiohttp/web_protocol.pyr'   N   s   r'   c                   @   s   e Zd ZdZdS )r(   z-Payload was accessed after response was sent.Nr,   r1   r1   r1   r2   r(   R   s   r(   )Zauto_attribsÚfrozenÚslotsc                   @   s&   e Zd ZU eed< eed< eed< dS )Ú_ErrInfoÚstatusÚexcÚmessageN)r-   r.   r/   ÚintÚ__annotations__ÚBaseExceptionÚstrr1   r1   r1   r2   r5   V   s   
r5   c                       sþ  e Zd ZdZdZdZddeeeej	ddddd	d
ddœde
jeeeee eeeeeeeeedœ‡ fdd„Zedœdd„Zeedœdd„ƒZdHee ddœdd„Ze
jddœ‡ fdd„Zee ddœ‡ fdd„Zedd œd!d"„Zddœd#d$„Ze dd%œd&d'„Z!edd(œd)d*„Z"ddœd+d,„Z#ddœd-d.„Z$e%e&edd/œd0d1„Z'eedd2œd3d4„Z(eedd2œd5d6„Z)ddœd7d8„Z*e%ee+e%ge,e& f e-e&ef d9œd:d;„Z.ddœd<d=„Z/e%e&eed>œd?d@„Z0dIe%eee ee e&dBœdCdD„Z1e2e+e%ge,e& f dEœdFdG„Z3‡  Z4S )Jr&   aÒ  HTTP protocol implementation.

    RequestHandler handles incoming HTTP request. It reads request line,
    request headers and request payload and calls handle_request() method.
    By default it always returns with 404 response.

    RequestHandler handles errors in incoming request, like bad
    status line, bad headers or incomplete payload. If any error occurs,
    connection gets closed.

    keepalive_timeout -- number of seconds before closing
                         keep-alive connection

    tcp_keepalive -- TCP keep-alive is on, default is on

    debug -- enable debug mode

    logger -- custom logger object

    access_log_class -- custom class for access_logger

    access_log -- custom logging object

    access_log_format -- access log format string

    loop -- Optional event loop

    max_line_size -- Optional maximum header line size

    max_field_size -- Optional maximum header field size

    max_headers -- Optional maximum header size

    r   )Ú_request_countÚ
_keepaliveÚ_managerÚ_request_handlerÚ_request_factoryÚ_tcp_keepaliveÚ_keepalive_timeÚ_keepalive_handleÚ_keepalive_timeoutÚ_lingering_timeÚ	_messagesÚ_message_tailÚ_waiterÚ_task_handlerÚ_upgradeÚ_payload_parserÚ_request_parserZ_reading_pausedÚloggerÚdebugÚ
access_logr   Ú_closeÚ_force_closeÚ_current_requestg     ÀR@TFiþ  i €  g      $@i   )Úkeepalive_timeoutr    rN   Úaccess_log_classrP   Úaccess_log_formatrO   Úmax_line_sizeÚmax_headersÚmax_field_sizeÚlingering_timeÚread_bufsizeÚauto_decompressr)   )ÚmanagerÚlooprT   r    rN   rU   rP   rV   rO   rW   rX   rY   rZ   r[   r\   c             
      sÐ   t ƒ  |¡ d| _d| _d | _|| _|j| _|j| _	|| _
d| _d | _|| _t|ƒ| _tƒ | _d| _d | _d | _d| _d | _t| |||
||t|d�| _|| _|	| _|| _|rº|||ƒ| _nd | _d| _d| _d S )Nr   Fg        ó    )rW   rY   rX   Zpayload_exceptionr\   ) ÚsuperÚ__init__r=   r>   rS   r?   Úrequest_handlerr@   Zrequest_factoryrA   rB   rC   rD   rE   ÚfloatrF   r   rG   rH   rI   rJ   rK   rL   r   r'   rM   rN   rO   rP   r   rQ   rR   )Úselfr]   r^   rT   r    rN   rU   rP   rV   rO   rW   rX   rY   rZ   r[   r\   ©Ú	__class__r1   r2   ra   ¢   sL    
øÿzRequestHandler.__init__)Úreturnc                 C   s   d  | jj| jd urdnd¡S )Nz<{} {}>Ú	connectedZdisconnected)Úformatrf   r-   Ú	transport©rd   r1   r1   r2   Ú__repr__å   s    þzRequestHandler.__repr__c                 C   s   | j S ©N)rE   rk   r1   r1   r2   rT   ë   s    z RequestHandler.keepalive_timeoutç      .@N)Útimeoutrg   c              
   Ã   s   d| _ | jdur| j ¡  | jr*| j ¡  ttjtjƒ�† t|ƒ4 I dH šP | j	durf| j	 
t ¡ ¡ | jdur†| j ¡ s†| jI dH  W d  ƒI dH  q°1 I dH s¦0    Y  W d  ƒ n1 sÄ0    Y  | jdurâ| j ¡  | jdurü| j ¡  d| _dS )z±Do worker process exit preparations.

        We need to clean up everything and stop accepting requests.
        It is especially important for keep-alive connections.
        TN)rR   rD   ÚcancelrI   r   ÚasyncioÚCancelledErrorÚTimeoutErrorr   rS   Ú_cancelrJ   Údonerj   Úclose)rd   ro   r1   r1   r2   Úshutdownï   s     



T



zRequestHandler.shutdown)rj   rg   c                    sX   t ƒ  |¡ ttj|ƒ}| jr&t|ƒ | j |  	¡ ¡| _
| jd usFJ ‚| j | |¡ d S rm   )r`   Úconnection_mader   rq   Ú	TransportrB   r    Ú_loopÚcreate_taskÚstartrJ   r?   )rd   rj   Zreal_transportre   r1   r2   rx     s    zRequestHandler.connection_made)r7   rg   c                    s¸   | j d u rd S | j  | |¡ tƒ  |¡ d | _ d| _d | _d | _d | _| jd urZ| j ¡  | j	d ur€|d u rtt
dƒ}| j	 |¡ | jd ur”| j ¡  d | _| jd ur´| j ¡  d | _d S )NTzConnection lost)r?   Úconnection_lostr`   rR   rA   r@   rM   rD   rp   rS   ÚConnectionResetErrorrt   rI   rJ   rL   Zfeed_eof)rd   r7   re   r1   r2   r}     s*    







zRequestHandler.connection_lost)Úparserrg   c                 C   s2   | j d u sJ ‚|| _ | jr.| j  | j¡ d| _d S )Nr_   )rL   rH   Ú	feed_data)rd   r   r1   r1   r2   Ú
set_parser7  s
    zRequestHandler.set_parserc                 C   s   d S rm   r1   rk   r1   r1   r2   Úeof_receivedA  s    zRequestHandler.eof_received)Údatarg   c           
   
   C   sD  | j s| jrd S | jd u rò| jsò| jd us.J ‚z| j |¡\}}}W nB ty† } z*td||jd�t	fg}d}d}W Y d }~n
d }~0 0 |pŽdD ]&\}}|  j
d7  _
| j ||f¡ q�| j}|rÜ|d urÜ| ¡ sÜ| d ¡ || _|rð|rð|| _nN| jd u �r| j�r|�r|  j|7  _n$|�r@| j |¡\}	}|	�r@|  ¡  d S )Ni�  )r6   r7   r8   Fr_   r1   r   )rR   rQ   rL   rK   rM   r€   r   r5   r8   r   r=   rG   ÚappendrI   ru   Z
set_resultrH   rv   )
rd   rƒ   ÚmessagesZupgradedÚtailr7   ÚmsgÚpayloadÚwaiterÚeofr1   r1   r2   Údata_receivedD  s4    ÿ
zRequestHandler.data_received)Úvalrg   c                 C   s    || _ | jr| j ¡  d| _dS )zMSet keep-alive connection mode.

        :param bool val: new state.
        N)r>   rD   rp   )rd   rŒ   r1   r1   r2   Ú
keep_alivek  s    
zRequestHandler.keep_alivec                 C   s   d| _ | jr| j ¡  dS )z�Close connection.

        Stop accepting new pipelining messages and close
        connection when handlers done processing messages.
        TN)rQ   rI   rp   rk   r1   r1   r2   rv   u  s    zRequestHandler.closec                 C   s4   d| _ | jr| j ¡  | jdur0| j ¡  d| _dS )zForcefully close connection.TN)rR   rI   rp   rj   rv   rk   r1   r1   r2   Úforce_close  s    


zRequestHandler.force_close)ÚrequestÚresponseÚtimerg   c                 C   s(   | j d ur$| j  ||| j ¡ | ¡ d S rm   )r   Úlogrz   r‘   )rd   r�   r�   r‘   r1   r1   r2   Ú
log_accessˆ  s    
zRequestHandler.log_access)ÚargsÚkwrg   c                 O   s   | j r| jj |i |¤Ž d S rm   )rO   rN   ©rd   r”   r•   r1   r1   r2   Ú	log_debugŽ  s    zRequestHandler.log_debugc                 O   s   | j j|i |¤Ž d S rm   )rN   Ú	exceptionr–   r1   r1   r2   Úlog_exception’  s    zRequestHandler.log_exceptionc                 C   sT   | j s| jsd S | j| j }| jr<| j ¡ |kr<|  ¡  d S | j | j	| j
¡| _d S rm   )rR   r>   rC   rE   rI   rz   r‘   rŽ   Z
call_laterÚKEEPALIVE_RESCHEDULE_DELAYÚ_process_keepaliverD   )rd   Únextr1   r1   r2   r›   •  s    ÿz!RequestHandler._process_keepalive)r�   Ú
start_timerb   rg   c              
   Ã   sZ  | j d usJ ‚z,z|| _||ƒI d H }W d | _nd | _0 W nè tyv } z$|}|  |||¡I d H }W Y d }~näd }~0  tjyŠ   ‚ Y nÈ tjyÜ } z:| jd|d� |  |d¡}|  |||¡I d H }W Y d }~n~d }~0  t	�y" } z.|  |d|¡}|  |||¡I d H }W Y d }~n8d }~0 0 t
|ddƒ�r>t dt¡ |  |||¡I d H }||fS )NzRequest handler timed out.©Úexc_infoiø  éô  Z__http_exception__Fzlreturning HTTPException object is deprecated (#2415) and will be removed, please raise the exception instead)r@   rS   r!   Úfinish_responserq   rr   rs   r—   Úhandle_errorÚ	ExceptionÚgetattrÚwarningsÚwarnÚDeprecationWarning)rd   r�   r�   rb   Úrespr7   Úresetr1   r1   r2   Ú_handle_request§  s2    ((*üzRequestHandler._handle_requestc              
   Ã   sæ  | j }| j}|dusJ ‚| j}|dus*J ‚| j}d}| jdusBJ ‚| jdusPJ ‚| j�s¾| js°zHz| ¡ | _	| j	I dH  W n" t
jyœ   Y W d| _	�q¾Y n0 W d| _	nd| _	0 | j ¡ \}}| ¡ }| jd7  _t| |ƒ}	t|tƒrø|  |¡}
t}n| j}
|  ||| |	|¡}�z$�z„| j  |  |||
¡¡}z|I dH \}}W n¨ t
jtf�yè   |  d¡ Y W W | jdu �rˆ|du�rˆ|  d¡ nX| j�s¾| j�r¾| j�s¾|du�rà| j  ¡ }|| _| jdu �rà| || | j¡| _n�q¾�q¾Y n0 ~|�r~|  d¡ W W | jdu �r"|du�r"|  d¡ nX| j�s¾| j�r¾| j�s¾|du�rz| j  ¡ }|| _| jdu �rz| || | j¡| _n�q¾�q¾t|j ƒ| _| !¡ �sŠ| j"}| j�sf|�rf|  d|¡ | ¡ }|| }t#t
j$t
jƒ�‚ | !¡ �sF||k �rFt%|| ƒ4 I dH š& | &¡ I dH  W d  ƒI dH  �q:1 I dH �s00    Y  | ¡ }�qÔW d  ƒ n1 �s\0    Y  | !¡ �sŠ| j�sŠ|  d¡ |  '¡  | (t)ƒ ¡ W �n t
j�y<   |  d¡ Y W | jdu �rÜ|du�rÜ|  d¡ nX| j�s¾| j�r¾| j�s¾|du�r4| j  ¡ }|| _| jdu �r4| || | j¡| _n�q¾�q¾Y n~ t*�y~ } z*| j+�rb| j,d	|d
� |  -¡  W Y d}~nDd}~0  t.�y¸ } z"| j,d|d
� |  -¡  W Y d}~n
d}~0 0 W | jdu �rÞ|du�rÞ|  d¡ nX| j�s¼| j�r¾| j�s¾|du�r6| j  ¡ }|| _| jdu �r6| || | j¡| _n�q¾qP| jdu �rZ|du�rZ|  d¡ n`| j�sº| j�r®| j�s®|du�rº| j  ¡ }|| _| jdu �rº| || | j¡| _n   Y �q¾0 qP| j�sâd| _| jdu�râ| j '¡  dS )a_  Process incoming request.

        It reads request line, request headers and request payload, then
        calls handle_request() method. Subclass has to override
        handle_request(). start() handles various exceptions in request
        or response handling. Connection is being closed always unless
        keep_alive(True) specified.
        Nr   z&Ignored premature client disconnectionz'Ignored premature client disconnection.z(Ignored premature client disconnection 2z'Start lingering close timer for %s sec.zUncompleted request.z'Ignored premature client disconnection zUnhandled runtime exceptionrž   zUnhandled exception)/rz   rJ   r?   rE   rA   r@   rR   rG   Zcreate_futurerI   rq   rr   Úpopleftr‘   Zrequests_countr   Ú
isinstancer5   Ú_make_error_handlerÚERRORr{   rª   ÚConnectionErrorr—   rj   r>   rQ   rC   rD   Zcall_atr›   Úboolr�   Zis_eofrF   r   rs   r   Zreadanyrv   Zset_exceptionr(   ÚRuntimeErrorrO   r™   rŽ   r£   )rd   r^   Úhandlerr]   rT   r¨   r8   rˆ   r|   Úwriterrb   r�   Ztaskr©   ÚnowrZ   Zend_tr7   r1   r1   r2   r|   Î  s   	
þ


ÿ
/


ÿÄ

)


ÿÊ
ÿ<,

	


ÿê 


ÿó


ÿzRequestHandler.start)r�   r¨   r�   rg   c                 Ã   sÎ   | j dur6| j  d¡ d| _| jr6| j  | j¡ d| _z
|j}W n2 tyr   |du r`tdƒ‚ntd |¡ƒ‚Y n0 z ||ƒI dH  | 	¡ I dH  W n" t
y¶   |  |||¡ Y dS 0 |  |||¡ dS dS )a   Prepare the response and write_eof, then log access.

        This has to
        be called within the context of any exception so the access logger
        can get exception information. Returns True if the client disconnects
        prematurely.
        NFr_   z+Missing return statement on request handlerz7Web-handler should return a response instance, got {!r}T)rM   Zset_upgradedrK   rH   r€   ÚprepareÚAttributeErrorr±   ri   Ú	write_eofr¯   r“   )rd   r�   r¨   r�   Zprepare_methr1   r1   r2   r¡   G  s0    



þÿ
zRequestHandler.finish_responser    )r�   r6   r7   r8   rg   c           
      C   sê   | j d|d� |jjdkr"tdƒ‚d}|tjkrÐd tj¡}tjj}d}| jr~t	t
ƒ� t ¡ }W d  ƒ n1 st0    Y  d|j d	d
¡v r¼|r¨t|ƒ}d|› d�}dj||d�}d}n|rÄ|}|d | }t|||d�}	|	 ¡  |	S )z›Handle errors.

        Returns HTTP response with specific status code. Logs additional
        information. It always closes current connection.
        zError handling requestrž   r   zMResponse is sent already, cannot send another response with the error messagez
text/plainz{0.value} {0.phrase}Nz	text/htmlÚAcceptÚ z<h2>Traceback:</h2>
<pre>z</pre>zV<html><head><title>{title}</title></head><body>
<h1>{title}</h1>
{msg}
</body></html>
)Útitler‡   z

)r6   ÚtextÚcontent_type)r™   r³   Zoutput_sizer¯   r   ÚINTERNAL_SERVER_ERRORri   ÚdescriptionrO   r   r£   Ú	tracebackÚ
format_excÚheadersÚgetÚhtml_escaper$   rŽ   )
rd   r�   r6   r7   r8   Úctrº   r‡   Útbr¨   r1   r1   r2   r¢   l  s8    ÿ

&ÿûzRequestHandler.handle_error)Úerr_inforg   c                    s   t tdœ‡ ‡fdd„}|S )N)r�   rg   c                 “   s   ˆ  | ˆ jˆ jˆ j¡S rm   )r¢   r6   r7   r8   )r�   ©rÆ   rd   r1   r2   r²   ¢  s    ÿz3RequestHandler._make_error_handler.<locals>.handler)r#   r%   )rd   rÆ   r²   r1   rÇ   r2   r­   Ÿ  s    z"RequestHandler._make_error_handler)rn   )r    NN)5r-   r.   r/   r0   rš   Ú	__slots__r   r"   r   Z
LOG_FORMATrq   ZAbstractEventLooprc   r°   r   r   r   r<   r9   ra   rl   ÚpropertyrT   r   rw   ZBaseTransportrx   r;   r}   r   r�   r‚   Úbytesr‹   r�   rv   rŽ   r#   r%   r“   r—   r™   r›   r
   r	   r   rª   r|   r¡   r¢   r5   r­   Ú__classcell__r1   r1   re   r2   r&   `   s’   #!ïïC
'


þ
û'zþ(   ûú4þ)Hrq   Zasyncio.streamsr¿   r¥   Úcollectionsr   Ú
contextlibr   Úhtmlr   rÃ   Úhttpr   Úloggingr   Útypingr   r   r	   r
   r   r   r   r   r   r   r   ÚattrZyarlÚabcr   r   Zbase_protocolr   Zhelpersr   r   r   r   r   r   r’   r   r   Zstreamsr   r   Ztcp_helpersr    Zweb_exceptionsr!   Zweb_logr"   Zweb_requestr#   Zweb_responser$   r%   Ú__all__Z
web_serverr)   Z_RequestFactoryZ_RequestHandlerÚURLr®   r£   r'   r(   Úsr5   Z_MsgTyper&   r1   r1   r1   r2   Ú<module>   sl   4ûùÿö