
    g                    `    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
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      U/var/www/openai/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	d}
|	|
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_host)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))AA/R D'+TlE(OXXeWd333? 8> 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6%5TDJJL%5666s   .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_network
ValueError
ip_address)r   r   r,   s      r   r   z_TrustedHosts.__init__F   s    "/C#<"?*-%QTQVTWTY   -- 0 ?%
 $;8--11)2F2Ft2LM
8**..y/C/CD/IJ &	 ! & 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>x   s     B,ASrSy,As   )rA   rH   rK   r   anyrD   rJ   rC   )r   r,   rP   s     @r   __contains__z_TrustedHosts.__contains__m   sr    	1%%d+BT'''BD,A,ABBB 	140000	1s   #A A A/.A/c                t    t        |      }| j                  r|d   S t        |      D ]
  }|| vs|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   )r<   rA   reversed)r   r+   x_forwarded_for_hostsr,   s       r   r$   z%_TrustedHosts.get_trusted_client_host}   sO    
 !1 A(++ 23D4 4 %Q''r   N)r   r/   r0   r1   )r,   z
str | Noner0   bool)r+   rF   r0   rF   )r2   r3   r4   r5   r   rS   r$   r6   r   r   r   r   C   s    2%8N1 (r   r   )r:   rF   r0   z	list[str])
__future__r   rH   uvicorn._typesr   r   r   r   r	   r<   r   r6   r   r   <module>rZ      s-    "  Y Y44 44n7K( K(r   