
     O=d                      h    d Z ddlmZmZmZmZmZmZ er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
    )TYPE_CHECKING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     JD:\Sites\api_v1\venv\Lib\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                 :  K   |d         dv rt          t          d         |          }|                    d          }|r|d         nd }| j        s	|| j        v rt          |d                   }d|v r2|d                             d          }|                                |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latin1schemes   x-forwarded-forc                 6    g | ]}|                                 S r   r   r   s     r   
<listcomp>z3ProxyHeadersMiddleware.__call__.<locals>.<listcomp>G   s-     - - -)-

- - -r   r    )r   r   getr&   r   dictdecoder   r$   r-   r   )r'   r.   r/   r0   client_addrclient_hostr6   x_forwarded_protox_forwarded_forr)   r,   ports               r   __call__zProxyHeadersMiddleware.__call__1   sY      =111<=uEEE5:YYx5H5HK,7A+a..TK  3K43E$E$EuY/00'722 )00D(E(L(LX(V(V%&7&=&=&?&?E(O%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
   r0   r   r   N)
__name__
__module____qualname__r   r   r#   r(   r   r-   rC   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   r   asgiref.typingr	   r
   r   r   r   r   r   r   r   r   <module>rJ      s   	 	 E D D D D D D D D D D D D D D D                64 64 64 64 64 64 64 64 64 64r   