o
    >h                     @   sZ   d dl mZ d dlmZ d dlmZ ddlmZ G dd dZdd	 Z	G d
d deZ
dS )    )urlparse)settings)is_same_domain   )AsyncWebsocketConsumerc                   @   s@   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dS )OriginValidatorze
    Validates that the incoming connection has an Origin header that
    is in an allowed list.
    c                 C   s   || _ || _d S N)applicationallowed_origins)selfr	   r
    r   {/var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-packages/channels/security/websocket.py__init__   s   
zOriginValidator.__init__c              	      s   |d dkrt dd }|dg D ]\}}|dkr.z	t|d}W q ty-   Y qw q| |r>| |||I d H S t }||||I d H 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denierr   r   r   __call__   s$   
zOriginValidator.__call__c                 C   s    |du r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   r   r   r   r   +   s   	
zOriginValidator.valid_originc                    s   t  fd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   s$    | ]}|d kp  |V  qdS )r!   N)match_allowed_origin).0patternr   r   r   r   	<genexpr>I   s
    
z2OriginValidator.validate_origin.<locals>.<genexpr>)anyr
   r#   r   r'   r   r"   8   s   zOriginValidator.validate_originc                 C   s   |du rdS t | }|jdu rdS |js%t d| jp|}t|j|S | |}| |}|j|jkrB||krBt|j|jrBd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_portr   r   r   r$   N   s    


z$OriginValidator.match_allowed_originc                 C   sD   |j dur|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  )portr,   )r   originr   r   r   r-   u   s   
zOriginValidator.get_origin_portN)
__name__
__module____qualname____doc__r   r    r   r"   r$   r-   r   r   r   r   r   	   s    'r   c                 C   s"   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_hostsr   r   r   AllowedHostsOriginValidator   s   

rA   c                   @   s   e Zd ZdZdd ZdS )r   z=
    Simple application which denies all requests to it.
    c                    s   |   I d H  d S r   )close)r   r   r   r   connect   s   zWebsocketDenier.connectN)r9   r:   r;   r<   rC   r   r   r   r   r      s    r   N)urllib.parser   django.confr   django.http.requestr   generic.websocketr   r   rA   r   r   r   r   r   <module>   s    