o
    NK&h                      @   s   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 d dlmZmZ d dlmZmZ d dlmZ G d	d
 d
edZdS )    )deque)partial)
attrgetter)CallableUnionoverload)	SanicMeta)
MiddlewareMiddlewareLocation)FutureMiddlewareMiddlewareType)Routerc                   @   s  e Zd ZU eed< dddZdefddZe			
dddde	de
dedede	f
ddZe			
dddde
de
dededee	ge	f f
ddZ			
ddddee	e
f de
dededee	ee	ge	f f f
ddZdddde	fddZdddddZdddZdS )MiddlewareMixinrouterreturnNc                 O   s
   g | _ d S N)_future_middleware)selfargskwargs r   J/var/www/html/venv/lib/python3.10/site-packages/sanic/mixins/middleware.py__init__   s   
zMiddlewareMixin.__init__
middlewarec                 C   s   t r   )NotImplementedError)r   r   r   r   r   _apply_middleware   s   z!MiddlewareMixin._apply_middlewarerequestTr   prioritymiddleware_or_request	attach_toapplyr   c                C      d S r   r   r   r   r    r!   r   r   r   r   r         zMiddlewareMixin.middlewarec                C   r"   r   r   r#   r   r   r   r      r$   c                   s2   d fdd	}t |r|||dS t||dS )a  Decorator for registering middleware.

        Decorate and register middleware to be called before a request is
        handled or after a response is created. Can either be called as
        *@app.middleware* or *@app.middleware('request')*. Although, it is
        recommended to use *@app.on_request* or *@app.on_response* instead
        for clarity and convenience.

        See [Middleware](/guide/basics/middleware) for more information.

        Args:
            middleware_or_request (Union[Callable, str]): Middleware function
                or the keyword 'request' or 'response'.
            attach_to (str, optional): When to apply the middleware;
                either 'request' (before the request is handled) or 'response'
                (after the response is created). Defaults to `'request'`.
            apply (bool, optional): Whether the middleware should be applied.
                Defaults to `True`.
            priority (int, optional): The priority level of the middleware.
                Lower numbers are executed first. Defaults to `0`.

        Returns:
            Union[Callable, Callable[[Callable], Callable]]: The decorated
                middleware function or a partial function depending on how
                the method was called.

        Example:
            ```python
            @app.middleware('request')
            async def custom_middleware(request):
                ...
            ```
        r   c                    sJ   |dkrt jnt j}t| |d} t| |}j|  r#| | S )Nr   r   )r
   REQUESTRESPONSEr	   r   r   appendr   )r   r    locationfuture_middlewarer!   r   r   r   r   register_middlewareS   s   

z7MiddlewareMixin.middleware.<locals>.register_middleware)r    N)r   )callabler   )r   r   r    r!   r   r+   r   r*   r   r   )   s   *c                C   (   t |r| j|d|dS t| jd|dS )aS  Register a middleware to be called before a request is handled.

        This is the same as *@app.middleware('request')*.

        Args:
            middleware (Callable, optional): A callable that takes in a
                request. Defaults to `None`.

        Returns:
            Callable: The decorated middleware function or a partial function
                depending on how the method was called.

        Examples:
            ```python
            @app.on_request
            async def custom_middleware(request):
                request.ctx.custom = 'value'
            ```
        r   r   r    r   r,   r   r   r   r   r   r   r   r   
on_requestl   
   zMiddlewareMixin.on_requestc                C   r-   )av  Register a middleware to be called after a response is created.

        This is the same as *@app.middleware('response')*.

        Args:
            middleware (Callable, optional): A callable that takes in a
                request and response. Defaults to `None`.

        Returns:
            Callable: The decorated middleware function or a partial function
                depending on how the method was called.

        Examples:
            ```python
            @app.on_response
            async def custom_middleware(request, response):
                response.headers['X-Server'] = 'Sanic'
            ```
        responser   r.   r/   r0   r   r   r   on_response   r2   zMiddlewareMixin.on_responsec                 C   s   | j jD ]C}tj| j| j|jt t	j
d}tj| j| j|jt t	jd}tt|tddd|j_tt|tdddddd |j_qtj| jt	j
d}tj| jt	jd}tt|tddd| _tt|tdddddd | _dS )au  Finalize the middleware configuration for the Sanic application.

        This method completes the middleware setup for the application.
        Middleware in Sanic is used to process requests globally before they
        reach individual routes or after routes have been processed.

        Finalization consists of identifying defined routes and optimizing
        Sanic's performance to meet the application's specific needs. If
        you are manually adding routes, after Sanic has started, you will
        typically want to use the `amend` context manager rather than
        calling this method directly.

        .. note::
            This method is usually called internally during the server setup
            process and does not typically need to be invoked manually.

        Example:
            ```python
            app.finalize_middleware()
            ```
        )r(   orderT)keyreverseN)r   routesr	   convertrequest_middlewarenamed_request_middlewaregetnamer   r
   r%   response_middlewarenamed_response_middlewarer&   sortedr   extra)r   router;   r?   r   r   r   finalize_middleware   sf   

z#MiddlewareMixin.finalize_middleware)r   N)r   Tr   )__name__
__module____qualname__r   __annotations__r   r   r   r   r   strboolintr   r   r   r1   r4   rD   r   r   r   r   r      sl   
 
	

Cr   )	metaclassN)collectionsr   	functoolsr   operatorr   typingr   r   r   sanic.base.metar   sanic.middlewarer	   r
   sanic.models.futuresr   r   sanic.routerr   r   r   r   r   r   <module>   s    