U
    	f                     @  sR   d 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mZ G dd dZdS )a  
This middleware can be used when a known proxy is fronting the application,
and is trusted to be properly setting the `X-Forwarded-Proto` and
`X-Forwarded-For` headers with the connecting client information.

Modifies the `client` and `scheme` information so that they reference
the connecting client, rather that the connecting proxy.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers#Proxies
    )annotations)Unioncast)ASGI3ApplicationASGIReceiveCallableASGISendCallable	HTTPScopeScopeWebSocketScopec                   @  sD   e Zd ZdddddddZdd	d
ddZdddddddZdS )ProxyHeadersMiddleware	127.0.0.1r   zlist[str] | strNone)apptrusted_hostsreturnc                 C  sB   || _ t|tr(dd |dD | _n
t|| _d| jk| _d S )Nc                 S  s   h | ]}|  qS  strip.0itemr   r   c/var/www/html/apigroqsegura/apigroq/lib/python3.8/site-packages/uvicorn/middleware/proxy_headers.py	<setcomp>   s     z2ProxyHeadersMiddleware.__init__.<locals>.<setcomp>,*)r   
isinstancestrsplitr   setalways_trust)selfr   r   r   r   r   __init__   s
    

zProxyHeadersMiddleware.__init__z	list[str]z
str | None)x_forwarded_for_hostsr   c                 C  s2   | j r|d S t|D ]}|| jkr|  S qd S )Nr   )r   reversedr   )r    r"   hostr   r   r   get_trusted_client_host   s    

z.ProxyHeadersMiddleware.get_trusted_client_hostr	   r   r   )scopereceivesendr   c                   s   |d dkrt td |}|d}|r0|d nd }| jsD|| jkrt|d }d|kr|d d }|d d	kr|d
d|d< n||d< d|kr|d d}dd |	dD }	| 
|	}
d}|
|f|d< | |||I d H S )Ntype)http	websocket)r   r
   clientr   headerss   x-forwarded-protolatin1r+   r*   wsschemes   x-forwarded-forc                 S  s   g | ]}|  qS r   r   r   r   r   r   
<listcomp>@   s     z3ProxyHeadersMiddleware.__call__.<locals>.<listcomp>r   )r   r   getr   r   dictdecoder   replacer   r%   r   )r    r&   r'   r(   client_addrZclient_hostr-   Zx_forwarded_protoZx_forwarded_forr"   r$   portr   r   r   __call__)   s$    

zProxyHeadersMiddleware.__call__N)r   )__name__
__module____qualname__r!   r%   r8   r   r   r   r   r      s    
r   N)__doc__
__future__r   typingr   r   uvicorn._typesr   r   r   r   r	   r
   r   r   r   r   r   <module>   s   
 