
     @g                     `    d Z ddlmZmZmZmZmZ ddlmZm	Z	m
Z
mZmZmZ  G 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
    )ListOptionalTupleUnioncast)ASGI3ApplicationASGIReceiveCallableASGISendCallable	HTTPScopeScopeWebSocketScopec                       e Zd Z	 ddddeee         ef         ddfdZdee         dee         fd	Z	 	 	 	 	 	 	 	 ddZ	dS )ProxyHeadersMiddleware	127.0.0.1appr   trusted_hostsreturnNc                     || _         t          |t                    r%d |                    d          D             | _        nt          |          | _        d| j        v | _        d S )Nc                 6    h | ]}|                                 S  strip.0items     T/var/www/piapp/venv/lib/python3.11/site-packages/uvicorn/middleware/proxy_headers.py	<setcomp>z2ProxyHeadersMiddleware.__init__.<locals>.<setcomp>   s     !T!T!T4$**,,!T!T!T    ,*)r   
isinstancestrsplitr   setalways_trust)selfr   r   s      r   __init__zProxyHeadersMiddleware.__init__   sj    
 mS)) 	4!T!T=;N;Ns;S;S!T!T!TD!$]!3!3D4#55r   x_forwarded_for_hostsc                 b    | j         r|d         S t          |          D ]}|| j        vr|c S d S )Nr   )r%   reversedr   )r&   r(   hosts      r   get_trusted_client_hostz.ProxyHeadersMiddleware.get_trusted_client_host$   sU      	,(++233 	 	D4--- . tr   scoper   receiver	   sendr
   c                 p  K   |d         dv rt          t          d         |          }|                    d          }|r|d         nd }| j        s	|| j        v rt          |d                   }d|v rL|d                             d                                          }|d         d	k    r|d
k    rdnd|d<   n||d<   d|v rX|d                             d          }d |                    d          D             }	| 	                    |	          }
d}|
|f|d<   | 
                    |||           d {V S )Ntype)http	websocket)r   r   clientr   headerss   x-forwarded-protolatin1r3   httpswsswsschemes   x-forwarded-forc                 6    g | ]}|                                 S r   r   r   s     r   
<listcomp>z3ProxyHeadersMiddleware.__call__.<locals>.<listcomp>M   s-     - - -)-

- - -r   r   )r   r   getr%   r   dictdecoder   r#   r,   r   )r&   r-   r.   r/   client_addrclient_hostr5   x_forwarded_protox_forwarded_forr(   r+   ports               r   __call__zProxyHeadersMiddleware.__call__0   s      =111<=uEEE5:YYx5H5HK,7A+a..TK  3K43E$E$EuY/00'722   45<<XFFLLNN & V}33%6'%A%AEEt h +<h%00 '..@&A&H&H&R&RO- -1@1F1Fs1K1K- - -)  778MNNDD'+TlE(OXXeWd333333333r   )r   )r-   r   r.   r	   r/   r
   r   N)
__name__
__module____qualname__r   r   r"   r'   r   r,   rE   r   r   r   r   r      s         0;
6 
6
6 T#Y^,
6 
	
6 
6 
6 
6
%)#Y
	#
 
 
 
$4$4'<$4DV$4	$4 $4 $4 $4 $4 $4r   r   N)__doc__typingr   r   r   r   r   uvicorn._typesr   r	   r
   r   r   r   r   r   r   r   <module>rL      s   	 	 6 5 5 5 5 5 5 5 5 5 5 5 5 5               =4 =4 =4 =4 =4 =4 =4 =4 =4 =4r   