
    .ig                    h    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	ddZ
 G d d	      Zy)    )annotationsN)ASGI3ApplicationASGIReceiveCallableASGISendCallableScopec                  "    e Zd ZdZdddZddZy)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>
    c                2    || _         t        |      | _        y N)app_TrustedHoststrusted_hosts)selfr   r   s      R/root/launch/venv/lib/python3.12/site-packages/uvicorn/middleware/proxy_headers.py__init__zProxyHeadersMiddleware.__init__   s    *=9    c                  K   |d   dk(  r| j                  |||       d {   S |j                  d      }|r|d   nd }|| j                  v rt        |d         }d|v rI|d   j	                  d      j                         }|dv r#|d   d	k(  r|j                  d
d      |d<   n||d<   d|v r;|d   j	                  d      }| j                  j                  |      \  }	}
|	r|	|
f|d<   | j                  |||       d {   S 7 7 w)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_address)r   scopereceivesendclient_addrclient_hostr   x_forwarded_protox_forwarded_forhostports              r   __call__zProxyHeadersMiddleware.__call__   s'    =J&%$777ii)(3k!n$,,,5+,G#w.$+,@$A$H$H$R$X$X$Z!$(FFV}3*;*C*CFD*Qh*;h!W,")*<"="D"DX"N!//JJ?[
d (,TlE(OXXeWd3337 86 4s"   DDCD>D?DDN)z	127.0.0.1)r   r   r   list[str] | strreturnNone)r%   r   r&   r   r'   r   r0   r1   )__name__
__module____qualname____doc__r   r.    r   r   r	   r	      s    :4r   r	   c                f    | j                  d      D cg c]  }|j                          c}S c c}w )N,)splitr"   )valueitems     r   _parse_raw_hostsr<   ;   s%    %*[[%56TDJJL666s   .c                   | j                  d      rS| j                  d      }|dk(  r| dfS | d| }| |dz   d }|s|dfS |j                  d      s| dfS 	 |t        |dd       fS | j	                  d      dk(  r#| j                  dd      \  }}	 |t        |      fS | dfS # t        $ r |dfcY S w xY w# t        $ r | dfcY S w xY w)a&  Parse a forwarded host value into host and optional port.

    Accepts bare IPs, IPv4 `host:port`, and bracketed IPv6 `[host]:port`.
    Any unrecognized or malformed value is treated conservatively and returned
    without a port so trust checks do not silently normalize arbitrary input.
    []r      N:)
startswithfindint
ValueErrorcountrsplit)r:   bracket_endr,   	remainderr-   s        r   _parse_host_portrK   ?   s    jjo"!8OQ{#+/+,	7N##C(!8O	Yqr]+++ {{31\\#q)
d	T?" !8O  	7N	  	!8O	s$   B  B3  B0/B03CCc                  (    e Zd ZdZddZddZddZy)	r   z(Container for trusted hosts and networksc                B   |ddgfv | _         t               | _        t               | _        t               | _        | j                   st        |t              rt        |      }|D ]e  }d|v r0	 | j                  j                  t        j                  |             7	 | j                  j                  t        j                  |             g y y # t        $ r | j                  j                  |       Y w xY w# t        $ r | j                  j                  |       Y w xY w)N*/)always_trustsettrusted_literalsr   trusted_networks
isinstancestrr<   add	ipaddress
ip_networkrF   
ip_address)r   r   r,   s      r   r   z_TrustedHosts.__init__e   s   "/C#<"?*-%QTQVTWTY   -- 0 ?% 8
 $;8--11)2F2Ft2LM
8**..y/C/CD/IJ8	 ! & 8--11$78 & 8--11$78s$   +.C.C7$C43C47$DDc                    | j                   ry|sy	 t        j                  |      | j                  v ryt	        fd| j
                  D              S # t        $ r || j                  v cY S w xY w)NTFc              3  &   K   | ]  }|v  
 y wr   r6   ).0netips     r   	<genexpr>z-_TrustedHosts.__contains__.<locals>.<genexpr>   s     BSrSyBs   )rP   rW   rY   r   anyrS   rF   rR   )r   r,   r^   s     @r   __contains__z_TrustedHosts.__contains__   sr    	1%%d+BT'''BD,A,ABBB 	140000	1s   #A A A/.A/c                    t        |      }| j                  rt        |d         S t        |      D ]  }t        |      \  }}|| vs||fc S  t        |d         S )zExtract the client address from x_forwarded_for header.

        In general this is the first "untrusted" host in the forwarded for list.
        r   )r<   rP   rK   reversed)r   r+   x_forwarded_for_hosts	host_portr,   r-   s         r   r$   z(_TrustedHosts.get_trusted_client_address   ss    
 !1 A#$9!$<== ""78 	"I))4JD$4Tz!	"   5a 899r   N)r   r/   r0   r1   )r,   z
str | Noner0   bool)r+   rU   r0   tuple[str, int])r2   r3   r4   r5   r   ra   r$   r6   r   r   r   r   b   s    2%8N1 :r   r   )r:   rU   r0   z	list[str])r:   rU   r0   rg   )
__future__r   rW   uvicorn._typesr   r   r   r   r	   r<   rK   r   r6   r   r   <module>rj      s3    "  Y Y04 04f7 FL: L:r   