o
    p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                   @  s.   e Zd Z	dddd	ZdddZdddZdS )ProxyHeadersMiddleware	127.0.0.1appr   trusted_hostslist[str] | strreturnNonec                 C  sB   || _ t|trdd |dD | _nt|| _d| jv | _d S )Nc                 S  s   h | ]}|  qS  strip.0itemr   r   Y/home/ertert/spirit/venv/lib/python3.10/site-packages/uvicorn/middleware/proxy_headers.py	<setcomp>       z2ProxyHeadersMiddleware.__init__.<locals>.<setcomp>,*)r   
isinstancestrsplitr   setalways_trust)selfr   r   r   r   r   __init__   s
   

zProxyHeadersMiddleware.__init__x_forwarded_for_hosts	list[str]
str | Nonec                 C  s2   | j r|d S t|D ]}|| jvr|  S qd S )Nr   )r!   reversedr   )r"   r$   hostr   r   r   get_trusted_client_host   s   
z.ProxyHeadersMiddleware.get_trusted_client_hostscoper	   receiver   sendr   c                   s   |d dv rkt td |}|d}|r|d nd }| js#|| jv rkt|d }d|v rI|d d }|d d	krE|d
d|d< n||d< d|v rk|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>@   r   z3ProxyHeadersMiddleware.__call__.<locals>.<listcomp>r   )r   r   getr!   r   dictdecoder   replacer   r)   r   )r"   r*   r+   r,   client_addrclient_hostr1   x_forwarded_protox_forwarded_forr$   r(   portr   r   r   __call__)   s&   

zProxyHeadersMiddleware.__call__N)r   )r   r   r   r   r   r   )r$   r%   r   r&   )r*   r	   r+   r   r,   r   r   r   )__name__
__module____qualname__r#   r)   r?   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
    
 