
    9Cg                    n    d dl mZ d dlZd dlmZmZmZmZ  G d d          Zdd
Z	 G d d          Z
dS )    )annotationsN)ASGI3ApplicationASGIReceiveCallableASGISendCallableScopec                  $    e Zd ZdZddd	ZddZdS )ProxyHeadersMiddlewareaB  Middleware for handling known proxy headers

    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.

    References:
    - <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers#Proxies>
    - <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For>
    	127.0.0.1appr   trusted_hostslist[str] | strreturnNonec                <    || _         t          |          | _        d S N)r   _TrustedHostsr   )selfr   r   s      R/var/www/api/venv/lib/python3.11/site-packages/uvicorn/middleware/proxy_headers.py__init__zProxyHeadersMiddleware.__init__   s    *=99    scoper   receiver   sendr   c                X  K   |d         dk    r|                      |||           d {V S |                    d          }|r|d         nd }|| j        v rt          |d                   }d|v r\|d                             d                                          }|dv r+|d         d	k    r|                    d
d          |d<   n||d<   d|v r@|d                             d          }| j                            |          }	|	r	d}
|	|
f|d<   |                      |||           d {V S )Ntypelifespanclientr   headerss   x-forwarded-protolatin1>   wswsshttphttps	websocketr"   r    schemes   x-forwarded-for)r   getr   dictdecodestripreplaceget_trusted_client_host)r   r   r   r   client_addrclient_hostr   x_forwarded_protox_forwarded_forhostports              r   __call__zProxyHeadersMiddleware.__call__   sn     =J&&%$777777777ii))(3=k!nn$,,,5+,,G#w..$+,@$A$H$H$R$R$X$X$Z$Z!$(FFFV}33*;*C*CFD*Q*Qh*;h!W,,")*<"="D"DX"N"N)AA/RR 3 D'+TlE(OXXeWd333333333r   N)r
   )r   r   r   r   r   r   )r   r   r   r   r   r   r   r   )__name__
__module____qualname____doc__r   r2    r   r   r	   r	      sK         : : : : :!4 !4 !4 !4 !4 !4r   r	   valuestrr   	list[str]c                @    d |                      d          D             S )Nc                6    g | ]}|                                 S r7   )r)   ).0items     r   
<listcomp>z$_parse_raw_hosts.<locals>.<listcomp>@   s     666TDJJLL666r   ,)split)r8   s    r   _parse_raw_hostsrB   ?   s"    66U[[%5%56666r   c                  *    e Zd ZdZddZdd
ZddZdS )r   z(Container for trusted hosts and networksr   r   r   r   c                Z   |ddgfv | _         t                      | _        t                      | _        t                      | _        | j         st          |t                    rt          |          }|D ]}d|v rX	 | j                            t          j
        |                     4# t          $ r | j                            |           Y Zw xY w	 | j                            t          j        |                     # t          $ r | j                            |           Y w xY wd S d S )N*/)always_trustsettrusted_literalsr   trusted_networks
isinstancer9   rB   add	ipaddress
ip_network
ValueError
ip_address)r   r   r0   s      r   r   z_TrustedHosts.__init__F   s\   "/C#<"?*-%%QTQVQVTWTYTY   	8--- @ 0 ? ?% 8 8
 $;;8-11)2Ft2L2LMMMM% 8 8 8-11$7777788*..y/CD/I/IJJJJ% 8 8 8-11$777778'	8 	88 8s$   :,B''$CC,C??$D&%D&r0   
str | Noneboolc                    | j         rdS |sdS 	 t          j        |          | j        v rdS t	          fd| j        D                       S # t          $ r || j        v cY S w xY w)NTFc              3      K   | ]}|v V  	d S r   r7   )r=   netips     r   	<genexpr>z-_TrustedHosts.__contains__.<locals>.<genexpr>x   s'      BBSrSyBBBBBBr   )rG   rM   rP   r   anyrJ   rO   rI   )r   r0   rV   s     @r   __contains__z_TrustedHosts.__contains__m   s     	4 	5	1%d++BT'''tBBBBD,ABBBBBB 	1 	1 	1400000	1s   A A A%$A%r/   r9   c                    t          |          }| j        r|d         S t          |          D ]
}|| vr|c S |d         S )zExtract the client host from x_forwarded_for header

        In general this is the first "untrusted" host in the forwarded for list.
        r   )rB   rG   reversed)r   r/   x_forwarded_for_hostsr0   s       r   r+   z%_TrustedHosts.get_trusted_client_host}   sh    
 !1 A A 	,(++ 233 	 	D4  
 %Q''r   N)r   r   r   r   )r0   rQ   r   rR   )r/   r9   r   r9   )r3   r4   r5   r6   r   rY   r+   r7   r   r   r   r   C   sW        22%8 %8 %8 %8N1 1 1 1 ( ( ( ( ( (r   r   )r8   r9   r   r:   )
__future__r   rM   uvicorn._typesr   r   r   r   r	   rB   r   r7   r   r   <module>r_      s    " " " " " "     Y Y Y Y Y Y Y Y Y Y Y Y44 44 44 44 44 44 44 44n7 7 7 7K( K( K( K( K( K( K( K( K( K(r   