
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
U
    '7`Z                     @   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 d dlZdd	lmZmZ dd
lmZ ddl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*m+Z+ ddl,m-Z- ddl.m/Z/ ddl0m1Z1 ddl2m3Z3 ddl4m5Z5m6Z6 dZ7erJddl8m9Z9 ee$e+dedge3f Z:ee3gee6 f Z;e$dde#i i dddde<d
Z=G dd de>Z?G dd de>Z@G d d deZAdS )!    N)deque)suppress)escape)
HTTPStatus)Logger)TYPE_CHECKINGAny	AwaitableCallableOptionalTupleTypecast   )AbstractAccessLoggerAbstractStreamWriter)BaseProtocol)CeilTimeoutcurrent_task)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__ r/   r/   G/opt/alt/python38/lib64/python3.8/site-packages/aiohttp/web_protocol.pyr%   9   s   r%   c                   @   s   e Zd ZdZdS )r&   z-Payload was accessed after response was sent.Nr*   r/   r/   r/   r0   r&   =   s   r&   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e
j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&ef d9d:d;Z,ddd<d=Z-e%e&eed>d?d@Z.dIe%eee ee e&dBdCdDZ/dJe0eee ee ddEdFdGZ1  Z2S )Kr$   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.

    :param keepalive_timeout: number of seconds before closing
                              keep-alive connection
    :type keepalive_timeout: int or None

    :param bool tcp_keepalive: TCP keep-alive is on, default is on

    :param bool debug: enable debug mode

    :param logger: custom logger object
    :type logger: aiohttp.log.server_logger

    :param access_log_class: custom class for access_logger
    :type access_log_class: aiohttp.abc.AbstractAccessLogger

    :param access_log: custom logging object
    :type access_log: aiohttp.log.server_logger

    :param str access_log_format: access log format string

    :param loop: Optional event loop

    :param int max_line_size: Optional maximum header line size

    :param int max_field_size: Optional maximum header field size

    :param int 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_error_handler_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   rC   access_log_classrE   access_log_formatrD   max_line_sizemax_headersmax_field_sizelingering_timeread_bufsizer'   )managerlooprI   r   rC   rJ   rE   rK   rD   rL   rM   rN   rO   rP   c             	      s   t  | d| _d| _d | _|| _|j| _|j| _	|| _
d| _d | _|| _t|| _t | _d| _d | _d | _d | _d| _d | _t| |||
||td| _|| _|	| _|| _|r|||| _nd | _d| _d| _ d S )Nr   Fg            )rL   rN   rM   Zpayload_exception)!super__init__r1   r2   rH   r3   Zrequest_handlerr4   Zrequest_factoryr5   r6   r7   r8   r9   floatr:   r   r;   r<   r=   r>   r?   r@   rA   r   r%   rB   rC   rD   rE   r   rF   rG   )selfrQ   rR   rI   r   rC   rJ   rE   rK   rD   rL   rM   rN   rO   rP   	__class__r/   r0   rU      sR    

 zRequestHandler.__init__)returnc                 C   s   d | jj| jd k	rdndS )Nz<{} {}>Z	connectedZdisconnected)formatrY   r+   	transportrW   r/   r/   r0   __repr__   s    zRequestHandler.__repr__c                 C   s   | j S N)r9   r]   r/   r/   r0   rI      s    z RequestHandler.keepalive_timeout      .@N)timeoutrZ   c              
      s   d| _ | jdk	r| j  | jr*| j  ttjtjz t|| j	d` | j
dk	rj| j
 sj| j
I dH  | jdk	r| jt  | jdk	r| j s| jI dH  W 5 Q R X W 5 Q R X | jdk	r| j  | jdk	r| j  d| _dS )zWorker process is about to exit, we need cleanup everything and
        stop accepting requests. It is especially important for keep-alive
        connections.TNrR   )rG   r8   cancelr=   r   asyncioCancelledErrorTimeoutErrorr   _loopr>   donerH   _cancelr?   r\   close)rW   ra   r/   r/   r0   shutdown   s$    



 



zRequestHandler.shutdown)r\   rZ   c                    sX   t  | ttj|}| jr&t| | j| 	 | _
| jd k	sFt| j| | d S r_   )rT   connection_mader   rd   Z	Transportr6   r   rg   create_taskstartr?   r3   AssertionError)rW   r\   Zreal_transportrX   r/   r0   rl      s    zRequestHandler.connection_made)excrZ   c                    s   | j d krd S | j | | t | d | _ d| _d | _d | _d | _| jd k	rZ| j  | j	d k	r|d krtt
d}| j	| | jd k	r| j  | jd k	r| j  | jd k	r| j  d | _| jd k	r| j  d | _d S )NTzConnection lost)r3   connection_lostrT   rG   r5   r4   rB   r8   rc   rH   ConnectionResetErrorri   r>   r?   r=   rA   Zfeed_eof)rW   rp   rX   r/   r0   rq     s2    











zRequestHandler.connection_lost)parserrZ   c                 C   s2   | j d kst|| _ | jr.| j | j d| _d S )NrS   )rA   ro   r<   	feed_data)rW   rs   r/   r/   r0   
set_parser&  s
    zRequestHandler.set_parserc                 C   s   d S r_   r/   r]   r/   r/   r0   eof_received0  s    zRequestHandler.eof_received)datarZ   c           
      C   s  | j s| jrd S | jd krP| jsP| jd k	s2tz| j|\}}}W n tk
r } z0| j	| 
t| | jd||j| _|   W 5 d }~X Y n tk
r } z,| j	| 
t| | jd|| _|   W 5 d }~X Y npX |r6|D ]&\}}|  jd7  _| j||f q| j}|d k	r6| s6|d  || _|r|r|| _nN| jd krz| jrz|rz|  j|7  _n$|r| j|\}	}|	r|   d S )Ni    r   )rG   rF   rA   r@   rB   ro   rt   r   rg   rm   handle_parse_errorr   messager>   rj   	Exceptionr1   r;   appendr=   rh   
set_resultr<   )
rW   rw   messagesZupgradedtailrp   msgpayloadwaitereofr/   r/   r0   data_received3  sL    
   


zRequestHandler.data_received)valrZ   c                 C   s    || _ | jr| j  d| _dS )zMSet keep-alive connection mode.

        :param bool val: new state.
        N)r2   r8   rc   )rW   r   r/   r/   r0   
keep_alived  s    
zRequestHandler.keep_alivec                 C   s   d| _ | jr| j  dS )ziStop accepting new pipelinig messages and close
        connection when handlers done processing messagesTN)rF   r=   rc   r]   r/   r/   r0   rj   n  s    zRequestHandler.closec                 C   s4   d| _ | jr| j  | jdk	r0| j  d| _dS )zForce close connectionTN)rG   r=   rc   r\   rj   r]   r/   r/   r0   force_closeu  s    


zRequestHandler.force_close)requestresponsetimerZ   c                 C   s(   | j d k	r$| j ||| j |  d S r_   )r   logrg   r   )rW   r   r   r   r/   r/   r0   
log_access~  s    
zRequestHandler.log_access)argskwrZ   c                 O   s   | j r| jj || d S r_   )rD   rC   rW   r   r   r/   r/   r0   	log_debug  s    zRequestHandler.log_debugc                 O   s   | j j|| d S r_   )rC   	exceptionr   r/   r/   r0   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 r_   )rG   r2   r7   r9   r=   rg   r   r   Z
call_laterKEEPALIVE_RESCHEDULE_DELAY_process_keepaliver8   )rW   nextr/   r/   r0   r     s     z!RequestHandler._process_keepalive)r   
start_timerZ   c              
      sL  | j d k	stz(z|| _|  |I d H }W 5 d | _X W n tk
r } z0t|j|j|j|jd}| 	|||I d H }W 5 d }~X Y n t
jk
r    Y n t
jk
r } z2| jd|d | |d}| 	|||I d H }W 5 d }~X Y nZ tk
r. } z&| |d|}| 	|||I d H }W 5 d }~X Y nX | 	|||I d H }||fS )N)statusreasontextheaderszRequest handler timed out.exc_infoi  rx   )r4   ro   rH   r   r"   r   r   r   r   finish_responserd   re   rf   r   handle_errorr{   )rW   r   r   resprp   resetr/   r/   r0   _handle_request  s2       $$&zRequestHandler._handle_requestc                    s  | j }| j}|dk	st| j}|dk	s*t| j}d}| jdk	sBt| jdk	sPt| js| jsz@z|
 | _	| j	I dH  W n  tjk
r   Y W qY nX W 5 d| _	X | j \}}| }| jd7  _t| |}	| ||| |	|}
dz$zv| j | |
|}z|I dH \}}W n6 tjtfk
rN   | d Y W W  qY nX t|ddrjtdt ~|r| d W W  qt |j!| _|" sX| j#}| js4|r4| d	| | }|| }t$tj%tjP |" s*||k r*t&|| |d
 |' I dH  W 5 Q R X | }qW 5 Q R X |" sX| jsX| d | (  |)t*  W n tjk
r   | d Y W  qY nz t+k
r } z"| j,r| j-d|d | .  W 5 d}~X Y n: t/k
r } z| j-d|d | .  W 5 d}~X Y nX W 5 | jdkr4|dk	r4| d n\| js| jr| js|dk	r| j  }|| _| jdkr||| | j| _n  qX  qP| jsd| _| jdk	r| j0dk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 disconnection.z&Ignored premature client disconnectionZ__http_exception__Fzlreturning HTTPException object is deprecated (#2415) and will be removed, please raise the exception insteadz(Ignored premature client disconnection 2z'Start lingering close timer for %s sec.rb   zUncompleted request.z'Ignored premature client disconnection zUnhandled runtime exceptionr   zUnhandled exception)1rg   r?   ro   r3   r9   r5   r4   rG   r;   r=   Zcreate_futurerd   re   popleftr   Zrequests_countr   r\   r   r2   rF   r7   r8   Zcall_atr   rm   r   ConnectionErrorgetattrwarningswarnDeprecationWarningboolr   Zis_eofr:   r   rf   r   Zreadanyrj   set_exceptionr&   RuntimeErrorrD   r   r   r{   r>   )rW   rR   handlerrQ   rI   r   rz   r   rn   writerr   nowtaskr   rO   Zend_trp   r/   r/   r0   rn     s    	





 



 zRequestHandler.start)r   r   r   rZ   c                    s   | j dk	r6| j d d| _| jr6| j | j d| _z
|j}W n4 tk
rt   |dkrbtdntd|Y nX z ||I dH  |	 I dH  W n$ t
k
r   | ||| Y dS X | ||| 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.
        NFrS   z+Missing return statement on request handlerz7Web-handler should return a response instance, got {!r}T)rB   Zset_upgradedr@   r<   rt   prepareAttributeErrorr   r[   	write_eofr   r   )rW   r   r   r   Zprepare_methr/   r/   r0   r   4  s0    	



zRequestHandler.finish_responserx   )r   r   rp   rz   rZ   c           
   	   C   s   | j d|d d}|tjkrdtj}tjj}d}| jrVtt t	 }W 5 Q R X d|j
ddkr|rt|}d	| d
}dj||d}d}n|r|}|d | }t|||d}	|	  |jjdks| jdkr|   |	S )zHandle errors.

        Returns HTTP response with specific status code. Logs additional
        information. It always closes current connection.zError handling requestr   z
text/plainz{0.value} {0.phrase}Nz	text/htmlZAccept z<h2>Traceback:</h2>
<pre>z</pre>zV<html><head><title>{title}</title></head><body>
<h1>{title}</h1>
{msg}
</body></html>
)titler   z

)r   r   content_typer   )r   r   ZINTERNAL_SERVER_ERRORr[   descriptionrD   r   r{   	traceback
format_excr   gethtml_escaper"   r   r   Zoutput_sizer\   )
rW   r   r   rp   rz   ctr   r   tbr   r/   r/   r0   r   X  s6    

 zRequestHandler.handle_error)r   r   rp   rz   rZ   c                    sr   t  }|d k	stttt| ||| j}| ||||}||I d H  | I d H  | j	d k	rh| j	
  d | _d S r_   )r   ro   r!   ERRORr   rg   r   r   r   r\   rj   r>   )rW   r   r   rp   rz   r   r   r   r/   r/   r0   ry     s          

z!RequestHandler.handle_parse_error)r`   )rx   NN)NN)3r+   r,   r-   r.   r   	__slots__r   r    r   Z
LOG_FORMATrd   ZAbstractEventLooprV   r   r   r   r   strintrU   r^   propertyrI   r   rk   ZBaseTransportrl   BaseExceptionrq   r   ru   rv   bytesr   r   rj   r   r!   r#   r   r   r   r   r   r   rn   r   r   r   ry   __classcell__r/   r/   rX   r0   r$   A   s   '"G "
1

  
y  '   3  )Brd   Zasyncio.streamsr   r   collectionsr   
contextlibr   htmlr   r   httpr   loggingr   typingr   r   r	   r
   r   r   r   r   Zyarlabcr   r   Zbase_protocolr   Zhelpersr   r   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&   r$   r/   r/   r/   r0   <module>   sd   (         