U
    RWjö  ã                   @   sD   d dl Z d dlZd dlmZ e d¡Zdd„ Zdd„ Zddd„ZdS )é    N)ÚHeaderss   [-!#$%&'*+.^_`|~0-9a-zA-Z]+c                 C   s8   |   dd¡\}}t |¡}|  d¡D ]}t||ƒ}q$|S )z{
    Given a dotted/colon path, like project.module:ClassName.callable,
    returns the object at the end of the path.
    ú:é   Ú.)ÚsplitÚ	importlibÚimport_moduleÚgetattr)ÚpathÚmodule_pathZobject_pathÚtargetÚbit© r   úWc:\users\psant\downloads\hotstarbackend\school\venv38\Lib\site-packages\daphne/utils.pyÚimport_by_path   s
    
r   c                 C   s$   | | }t |tƒr|d }| d¡S )Nr   úutf-8)Ú
isinstanceÚlistÚdecode)ÚheadersÚheader_nameÚvaluer   r   r   Úheader_value   s    
r   úX-Forwarded-ForúX-Forwarded-PortúX-Forwarded-Protoc           
      C   s  |s||fS t | tƒr"t|  ¡ ƒ} dd„ |  ¡ D ƒ} tdd„ |  ¡ D ƒƒsNt‚| ¡  	d¡}|}|}|| k�rt
| |ƒ}d|kr’| d¡d  ¡ }|dg}|rä| ¡  	d¡}|| krät
| |ƒ}	zt|	ƒ|d< W n tk
râ   Y nX |�r| ¡  	d¡}|| k�rt
| |ƒ}||fS )	a˜  
    Parses an X-Forwarded-For header and returns a host/port pair as a list.

    @param headers: The twisted-style object containing a request's headers
    @param address_header_name: The name of the expected host header
    @param port_header_name: The name of the expected port header
    @param proto_header_name: The name of the expected proto header
    @param original_addr: A host/port pair that should be returned if the headers are not in the request
    @param original_scheme: A scheme that should be returned if the headers are not in the request
    @return: A list containing a host (string) as the first entry and a port (int) as the second.
    c                 S   s   i | ]\}}|  ¡ |“qS r   )Úlower)Ú.0ÚnameÚvaluesr   r   r   Ú
<dictcomp>9   s      z)parse_x_forwarded_for.<locals>.<dictcomp>c                 s   s   | ]}t |tƒV  qd S )N)r   Úbytes)r   r   r   r   r   Ú	<genexpr><   s     z(parse_x_forwarded_for.<locals>.<genexpr>r   ú,r   r   )r   r   ÚdictZgetAllRawHeadersÚitemsÚallÚkeysÚAssertionErrorr   Úencoder   r   ÚstripÚintÚ
ValueError)
r   Zaddress_header_nameZport_header_nameZproto_header_nameZoriginal_addrZoriginal_schemeZresult_addrZresult_schemeZaddress_valueZ
port_valuer   r   r   Úparse_x_forwarded_for   s6    





r-   )r   r   r   NN)	r   ÚreZtwisted.web.http_headersr   ÚcompileZHEADER_NAME_REr   r   r-   r   r   r   r   Ú<module>   s   
	     ú