
    _f                     r    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
dS )    )urlparse)settings)is_same_domain   )AsyncWebsocketConsumerc                   6    e Zd ZdZd Zd Zd Zd Zd Zd Z	dS )	OriginValidatorze
    Validates that the incoming connection has an Origin header that
    is in an allowed list.
    c                 "    || _         || _        d S N)applicationallowed_origins)selfr   r   s      M/var/www/html/env/lib/python3.11/site-packages/channels/security/websocket.py__init__zOriginValidator.__init__   s    &.    c                   K   |d         dk    rt          d          d }|                    dg           D ]?\  }}|dk    r4	 t          |                    d                    }/# t          $ r Y ;w xY w@|                     |          r|                     |||           d {V S t                      } ||||           d {V S )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)A 	 	%Ki''$,\-@-@-J-J$K$KMM)   D	 ( ]++ 	6))%$????????? %&&Fw555555555s   "A%%
A21A2c                 F    |d| j         vrdS |                     |          S )z
        Checks parsed origin is None.

        Pass control to the validate_origin function.

        Returns ``True`` if validation function was successful, ``False`` otherwise.
        N*F)r   validate_originr   r    s     r   r   zOriginValidator.valid_origin+   s0      S0D%D%D5##M222r   c                 H     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    p                     |          V  !dS )r&   N)match_allowed_origin).0patternr    r   s     r   	<genexpr>z2OriginValidator.validate_origin.<locals>.<genexpr>I   sP       
 
 sNOd77wOO
 
 
 
 
 
r   )anyr   r(   s   ``r   r'   zOriginValidator.validate_origin8   sF    "  
 
 
 
 
/
 
 
 
 
 	
r   c                    |dS t          |                                          }|j        dS |j        s.t          d|z             j        p|}t	          |j        |          S |                     |          }|                     |          }|j        |j        k    r"||k    rt	          |j        |j                  rdS dS )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://exapmple.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
        NFz//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       5 "'--//22!)5$ 	L'w77@KG!-"8:JKKK**=99++N;; !]%999|++}5~7NOO , 4ur   c                     |j         |j         S |j        dk    s|j        dk    rdS |j        dk    s|j        dk    rdS dS )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   sU    
 ;";=F""fmt&;&;2]g%%%)?)?34r   N)
__name__
__module____qualname____doc__r   r$   r   r'   r+   r4    r   r   r	   r	   	   sy         
/ / /6 6 603 3 3
 
 
,% % %N    r   r	   c                 ^    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      s8    
 *M~ <m <;;;;666r   c                       e Zd ZdZd ZdS )r   z=
    Simple application which denies all requests to it.
    c                 >   K   |                                   d {V  d S r   )close)r   s    r   connectzWebsocketDenier.connect   s,      jjllr   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      s    ! ! ! ! ! !             . . . . . . 6 6 6 6 6 6| | | | | | | |~7 7 7    ,     r   