
    h                     d    d dl mZ d dlmZ d dlmZ ddlmZ  G d d      Zd Z	 G d	 d
e      Z
y)    )urlparse)settings)is_same_domain   )AsyncWebsocketConsumerc                   4    e Zd ZdZd Zd Zd Zd Zd Zd Z	y)	OriginValidatorze
    Validates that the incoming connection has an Origin header that
    is in an allowed list.
    c                      || _         || _        y N)applicationallowed_origins)selfr   r   s      W/var/www/Befach/backend/env/lib/python3.12/site-packages/channels/security/websocket.py__init__zOriginValidator.__init__   s    &.    c                 `  K   |d   dk7  rt        d      d }|j                  dg       D ]&  \  }}|dk(  s	 t        |j                  d            }( | j                  |      r| j                  |||       d {   S t               } ||||       d {   S # t        $ r Y |w xY w7 /7 w)Ntype	websocketz<You cannot use OriginValidator on a non-WebSocket connectionheaderss   originlatin1)
ValueErrorgetr   decodeUnicodeDecodeErrorvalid_originr   WebsocketDenier)r   scopereceivesendparsed_originheader_nameheader_valuedeniers           r   __call__zOriginValidator.__call__   s     =K'N  ).9b)A%Ki'$,\-@-@-J$KM	 *B ]+))%$??? %&Fw555 * 
 @ 6sF   3B.B)B.:B*;B.B,B.	B'$B.&B''B.,B.c                 F    |d| j                   vry| j                  |      S )z
        Checks parsed origin is None.

        Pass control to the validate_origin function.

        Returns ``True`` if validation function was successful, ``False`` otherwise.
        *F)r   validate_originr   r    s     r   r   zOriginValidator.valid_origin+   s+      S0D0D%D##M22r   c                 D     t         fd j                  D              S )a  
        Validate the given origin for this site.

        Check than the origin looks valid and matches the origin pattern in
        specified list ``allowed_origins``. Any pattern begins with a scheme.
        After the scheme there must be a domain. Any domain beginning with a
        period corresponds to the domain and all its subdomains (for example,
        ``http://.example.com``). After the domain there must be a port,
        but it can be omitted. ``*`` matches anything and anything
        else must match exactly.

        Note. This function assumes that the given origin has a schema, domain
        and port, but port is optional.

        Returns ``True`` for a valid host, ``False`` otherwise.
        c              3   P   K   | ]  }|d k(  xs j                  |        yw)r&   N)match_allowed_origin).0patternr    r   s     r   	<genexpr>z2OriginValidator.validate_origin.<locals>.<genexpr>I   s2      
 sNOd77wOO
s   #&)anyr   r(   s   ``r   r'   zOriginValidator.validate_origin8   s%    "  
//
 
 	
r   c                    |yt        |j                               }|j                  y|j                  s2t        d|z         j                  xs |}t	        |j                  |      S | j                  |      }| j                  |      }|j                  |j                  k(  r&||k(  r!t	        |j                  |j                        ryy)a  
        Returns ``True`` if the origin is either an exact match or a match
        to the wildcard pattern. Compares scheme, domain, port of origin and pattern.

        Any pattern can be begins with a scheme. After the scheme must be a domain,
        or just domain without scheme.
        Any domain beginning with a period corresponds to the domain and all
        its subdomains (for example, ``.example.com`` ``example.com``
        and any subdomain). Also with scheme (for example, ``http://.example.com``
        ``http://example.com``). After the domain there must be a port,
        but it can be omitted.

        Note. This function assumes that the given origin is either None, a
        schema-domain-port string, or just a domain string
        Fz//T)r   lowerhostnameschemer   get_origin_port)r   r    r-   parsed_patternpattern_hostnameorigin_portpattern_ports          r   r+   z$OriginValidator.match_allowed_originN   s        "'--/2!!)$$'w7@@KG!-"8"8:JKK**=9++N; !!]%9%99|+}55~7N7NOr   c                     |j                   |j                   S |j                  dk(  s|j                  dk(  ry|j                  dk(  s|j                  dk(  ryy)zq
        Returns the origin.port or port for this schema by default.
        Otherwise, it returns None.
        NhttpwsP   httpswssi  )portr3   )r   origins     r   r4   zOriginValidator.get_origin_portu   sO    
 ;;";;==F"fmmt&;]]g%%)?r   N)
__name__
__module____qualname____doc__r   r$   r   r'   r+   r4    r   r   r	   r	   	   s&    
/603
,%Nr   r	   c                 f    t         j                  }t         j                  r|sg d}t        | |      S )zi
    Factory function which returns an OriginValidator configured to use
    settings.ALLOWED_HOSTS.
    )	localhostz	127.0.0.1z[::1])r   ALLOWED_HOSTSDEBUGr	   )r   allowed_hostss     r   AllowedHostsOriginValidatorrK      s*    
 **M~~m;;66r   c                       e Zd ZdZd Zy)r   z=
    Simple application which denies all requests to it.
    c                 @   K   | j                          d {    y 7 wr   )close)r   s    r   connectzWebsocketDenier.connect   s     jjls   N)rA   rB   rC   rD   rO   rE   r   r   r   r      s    r   r   N)urllib.parser   django.confr   django.http.requestr   generic.websocketr   r	   rK   r   rE   r   r   <module>rT      s0    !   . 6| |~7, r   