o
    NK&h,                     @  s^  d dl mZ d dlmZmZ d dlmZ d dlmZ d dl	m
Z
 er(d dlmZ d dlmZ d dlmZ d d	lmZmZ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  d dl!m"Z" d dl#m$Z$ e%ddi Z&zd dl'm(Z( d dl)m*Z*m+Z+ d dl,m-Z-m.Z.m/Z/ e(Z&W n	 e0y   Y nw G dd dZ1G dd de1e$e
dZ2G dd de1e&Z3dS )    )annotations)TYPE_CHECKINGOptional)HTTP)Http3)TouchUpMeta)Sanic)CancelledError)	monotonic)RequestCancelledRequestTimeoutServiceUnavailable)HttpStage)Colorsaccess_loggererror_loggerloggerwebsockets_logger)ConnInfo)Request)SanicProtocolConnectionProtocol )QuicConnectionProtocol)H3_ALPNH3Connection)DatagramFrameReceivedProtocolNegotiated	QuicEventc                   @  sB   e Zd ZU dZded< dd Zdd Zedd	 Zed
d Z	dS )HttpProtocolMixinr   r   __version__c                 O  sF   | j | g|R i || _t | _z|   W d S  ty"   Y d S w N)
HTTP_CLASS_httpcurrent_time_timecheck_timeoutsAttributeError)selfargskwargsr   r   W/var/www/html/venv/lib/python3.10/site-packages/sanic/server/protocols/http_protocol.py_setup_connection6   s   z#HttpProtocolMixin._setup_connectionc                 C  sh   d | _ | jjj| _| jj| _| jj| _| jjj| _	| jjj
| _| jjj| _| jjj| _| jjp0t| _d S r"   )requestappconfig
ACCESS_LOG
access_loghandle_requestrequest_handlererror_handlerREQUEST_TIMEOUTrequest_timeoutRESPONSE_TIMEOUTresponse_timeoutKEEP_ALIVE_TIMEOUTkeep_alive_timeoutREQUEST_MAX_SIZErequest_max_sizerequest_classr   r)   r   r   r,   _setup>   s   

zHttpProtocolMixin._setupc                 C  s   t | dsd S | jS )Nr$   )hasattrr$   r?   r   r   r,   httpI   s   
zHttpProtocolMixin.httpc                 C  s   | j jS r"   )	__class__r!   r?   r   r   r,   versionO   s   zHttpProtocolMixin.versionN)
__name__
__module____qualname__	__slots____annotations__r-   r@   propertyrB   rD   r   r   r   r,   r    2   s   
 
r    c                      s   e Zd ZdZeZdZejZ	dZ
dddddd  fdd	Zd
d Zdd Zdd Zd!d" fddZdd Zd#ddZdd Zd$ddZ  ZS )%HttpProtocolzg
    This class provides implements the HTTP 1.1 protocol on top of our
    Sanic Server transport
    )sendconnection_task)r.   r4   r7   r9   r;   r=   r>   r5   r2   stateurl_handler_taskr$   
_exceptionrecv_buffer_callback_check_timeoutsN)signalconnectionsrN   unixr/   r   c                  sV   t  j|||||d d | _|r|ni | _|   d| jvr#d| jd< d | _d | _d S )N)loopr/   rT   rU   rV   requests_countr   )super__init__rO   rN   r@   rQ   rS   )r)   rW   r/   rT   rU   rN   rV   r+   rC   r   r,   rZ   x   s   


zHttpProtocol.__init__c                   s  zz|    | jjddd| jidI dH  | j I dH  W n ty(   Y n ty5   t	d Y nw W | j
rH| jrH| jrH| jjsH|   d| _d| _z9z|   W n tyc   t	d Y nw W | jjddd| jidI dH  dS W | jjddd| jidI dH  dS | jjddd| jidI dH  w | j
r| jr| jr| jjs|   d| _d| _z7z|   W n ty   t	d Y nw W | jjddd| jidI dH  w W | jjddd| jidI dH  w | jjddd| jidI dH  w )	z
        Run a HTTP connection.

        Timeouts and some additional error handling occur here, while most of
        everything else happens in class Http or in code called from there.
        zhttp.lifecycle.beginT	conn_infoinlinecontextNz!protocol.connection_task uncaughtzClosing failedzhttp.lifecycle.complete)r-   r/   dispatchr\   r$   http1r	   	Exceptionr   	exceptionr2   	transportupgrade_websocketlog_disconnect_taskcloseBaseExceptionr?   r   r   r,   rM      s   zHttpProtocol.connection_taskc                 C  s   | j d}| jj}| jj}|r|jnt| jjdt| ddd d ddd	}|d urG|j	 }r<| d
|j
 |d< |j d|j |d< tjd|d d S )NpeernameDISCONNECTEDXunxnil )statusbytehostr.   duration:rt    r.   extra)rd   get_extra_infor$   r.   responserr   strstageid	client_ipportmethodrO   r   info)r)   ipreqresry   r   r   r,   rf      s   
zHttpProtocol.log_disconnectc                 C  s6  z| j sW dS t | j }| jj}|tju r!|| jkr!t	d n^|tj
u r7|| jkr7t	d td| j_nH|tju rH| jjrHt	d W dS |tjtjtjfv rc|| jkrct	d td| j_nt| j| j| jd }td	|}| j|| j| _W dS d
}d}| j j|  W dS  ty   td Y dS w )zK
        Runs itself periodically to enforce any expired timeouts.
        Nz&KeepAlive Timeout. Closing connection.z$Request Timeout. Closing connection.zRequest Timeoutz&Handling websocket. Timeouts disabled.z%Response Timeout. Closing connection.zResponse Timeout   g?r   )z%Cancel connection task with a timeoutzprotocol.check_timeouts)rg   r%   r&   r$   r}   r   IDLEr;   r   debugREQUESTr7   r   rc   HANDLERre   r   RESPONSEFAILEDr9   r   minmaxrW   
call_laterr'   rS   cancelrb   r   )r)   ru   r}   interval	_intervalcancel_msg_argsr   r   r,   r'      sL   



zHttpProtocol.check_timeoutstimeoutOptional[float]c                   s   | j r| j   t j|dS )zN
        Requires to prevent checking timeouts for closed connections
        )r   )rS   r   rY   rh   )r)   r   r[   r   r,   rh      s   
zHttpProtocol.closec                   sT   | j  I dH  | j rt| jjddd|idI dH  | j| t | _	dS )z=
        Writes HTTP data with backpressure control.
        Nzhttp.lifecycle.sendTdatar]   )

_can_writewaitrd   
is_closingr   r/   r`   writer%   r&   r)   r   r   r   r,   rL     s   
zHttpProtocol.sendreturnboolc                 C  s(   | j du s| j jtju r|   dS dS )z
        Close the connection if a request is not being sent or received

        :return: boolean - True if closed, false if staying open
        NTF)r$   r}   r   r   rh   r?   r   r   r,   close_if_idle  s   zHttpProtocol.close_if_idlec                 C  sr   z)|j ddd | j|  || _| j|  | _t | _	t
| j| jd| _W dS  ty8   td Y dS w )z?
        HTTP-protocol-specific new connection handler
        i @  i   )lowhigh)rV   zprotocol.connect_madeN)set_write_buffer_limitsrU   addrd   rW   create_taskrM   rg   	bytearrayrR   r   _unixr\   rb   r   rc   )r)   rd   r   r   r,   connection_made"  s   zHttpProtocol.connection_mader   bytesc                 C  s   z2t  | _|s|  W S |  j|7  _t| j| jjjkr%| jr%| j	  | j
r0| j
  W d S W d S  tyA   td Y d S w )Nzprotocol.data_received)r%   r&   rh   rR   lenr/   r0   REQUEST_BUFFER_SIZErd   pause_reading_data_receivedsetrb   r   rc   r   r   r   r,   data_received1  s   

zHttpProtocol.data_received)r/   r   r"   )r   r   )r   r   )r   r   )rE   rF   rG   __doc__r   r#   __touchup__r   	VERSION_1r!   rH   rZ   rM   rf   r'   rh   rL   r   r   r   __classcell__r   r   r[   r,   rK   T   s&    *-	
rK   )	metaclassc                      s@   e Zd ZeZejZd fddZdd	d
Z	e
dddZ  ZS )Http3Protocolr/   r   r   Nonec                  s*   || _ t j|i | |   d | _d S r"   )r/   rY   rZ   r@   _connection)r)   r/   r*   r+   r[   r   r,   rZ   H  s   
zHttp3Protocol.__init__eventr   c                 C  s   t jtj dtj | tj ddid t|tr/| j| j	d |j
tv r.t| jdd| _nt|tr?|jdkr?| jd	 | jd urS| j|D ]
}| j| qJd S d S )
Nz[quic_event_received]: 	verbosityr   rx   )transmitT)enable_webtransports   quacks	   quack-ack)r   r   r   BLUEPURPLEEND
isinstancer   r-   r   alpn_protocolr   r   _quicr   r   r   send_datagram_framehandle_eventr$   http_event_received)r)   r   
http_eventr   r   r,   quic_event_receivedN  s2   




z!Http3Protocol.quic_event_receivedOptional[H3Connection]c                 C  s   | j S r"   )r   r?   r   r   r,   
connectionc  s   zHttp3Protocol.connection)r/   r   r   r   )r   r   r   r   )r   r   )rE   rF   rG   r   r#   r   	VERSION_3r!   rZ   r   rJ   r   r   r   r   r[   r,   r   D  s    
r   N)4
__future__r   typingr   r   sanic.http.constantsr   sanic.http.http3r   sanic.touchup.metar   	sanic.appr   asyncior	   timer
   r%   sanic.exceptionsr   r   r   
sanic.httpr   r   	sanic.logr   r   r   r   r   sanic.models.server_typesr   sanic.requestr   $sanic.server.protocols.base_protocolr   typer   aioquic.asyncior   aioquic.h3.connectionr   r   aioquic.quic.eventsr   r   r   ModuleNotFoundErrorr    rK   r   r   r   r   r,   <module>   s8    " q