U
    sÍ@gë  ã                   @   sª   d Z ddlmZmZ zddlmZ W n  ek
rD   ddlmZ Y nX ddl	m
Z
 ddlmZ G dd„ de
jƒZG dd	„ d	ejƒZG d
d„ dejƒZG dd„ dejƒZdS )z&
I am a virtual hosts implementation.
é    )ÚdivisionÚabsolute_import)Úescape)Úroots)Úresourcec                   @   s>   e Zd ZdZejZdd„ Zdd„ Zdd„ Z	dd	„ Z
d
d„ ZdS )ÚVirtualHostCollectionzWWrapper for virtual hosts collection.

    This exists for configuration purposes.
    c                 C   s
   || _ d S ©N)Únvh)Úselfr	   © r   ú3/usr/lib/python3/dist-packages/twisted/web/vhost.pyÚ__init__   s    zVirtualHostCollection.__init__c                 C   s   | j j ¡ S r   )r	   ÚhostsÚitems©r
   r   r   r   ÚlistStaticEntities   s    z(VirtualHostCollection.listStaticEntitiesc                 C   s   | j j | ¡S r   )r	   r   Úget©r
   Únamer   r   r   ÚgetStaticEntity"   s    z%VirtualHostCollection.getStaticEntityc                 C   s   | j  ||¡ d S r   )r	   ÚaddHost)r
   r   Zentityr   r   r   ÚreallyPutEntity%   s    z%VirtualHostCollection.reallyPutEntityc                 C   s   | j  |¡ d S r   )r	   Ú
removeHostr   r   r   r   Ú	delEntity(   s    zVirtualHostCollection.delEntityN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚResourceZ
entityTyper   r   r   r   r   r   r   r   r   r      s   r   c                   @   sT   e Zd ZdZdZdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Z	dd„ Z
dd„ Zdd„ ZdS )ÚNameVirtualHostz:I am a resource which represents named virtual hosts.
    Nc                 C   s   t j | ¡ i | _dS )zInitialize.
        N)r   r   r   r   r   r   r   r   r   2   s    zNameVirtualHost.__init__c                 C   s   t j | ¡dt| ƒfg S ©NzVirtual Hosts)r   r   r   r   r   r   r   r   r   8   s    z"NameVirtualHost.listStaticEntitiesc                 C   s"   |dkrt | ƒS tj | |¡S d S r    )r   r   r   r   r   r   r   r   r   ;   s    zNameVirtualHost.getStaticEntityc                 C   s   || j |< dS )a¼  Add a host to this virtual host.

        This will take a host named `name', and map it to a resource
        `resrc'.  For example, a setup for our virtual hosts would be::

            nvh.addHost('divunal.com', divunalDirectory)
            nvh.addHost('www.divunal.com', divunalDirectory)
            nvh.addHost('twistedmatrix.com', twistedMatrixDirectory)
            nvh.addHost('www.twistedmatrix.com', twistedMatrixDirectory)
        N©r   )r
   r   Úresrcr   r   r   r   A   s    zNameVirtualHost.addHostc                 C   s   | j |= dS )zRemove a host.Nr!   r   r   r   r   r   N   s    zNameVirtualHost.removeHostc                 C   sZ   |  d¡}|dkr | jpt ¡ S | ¡  dd¡d }| j || j¡pXt dtt	|ƒƒ ¡S )zD(Internal) Get the appropriate resource for the given host.
        s   hostNó   :é   r   zhost %s not in vhost map)
Z	getHeaderÚdefaultr   Z
NoResourceÚlowerÚsplitr   r   Úhtml_escapeÚrepr)r
   ÚrequestZ
hostHeaderÚhostr   r   r   Ú_getResourceForRequestR   s    
ÿz&NameVirtualHost._getResourceForRequestc                 C   s   |   |¡}| |¡S )z=Implementation of resource.Resource's render method.
        )r,   Úrender)r
   r*   r"   r   r   r   r-   ]   s    
zNameVirtualHost.renderc                 C   s:   |   |¡}|jr*|j d|j d¡¡ |S | ||¡S dS )z?Implementation of resource.Resource's getChild method.
        r   éÿÿÿÿN)r,   ZisLeafÚpostpathÚinsertÚprepathÚpopZgetChildWithDefault)r
   Úpathr*   r"   r   r   r   ÚgetChildc   s
    
zNameVirtualHost.getChild)r   r   r   r   r%   r   r   r   r   r   r,   r-   r4   r   r   r   r   r   ,   s   r   c                   @   s   e Zd Zdd„ ZdS )Ú_HostResourcec                 C   s˜   d|kr"|  dd¡\}}t|ƒ}n
|d }}| ||¡ d| ¡  d t|ƒ t|jd ƒ }dd |j¡ |_|j	|d … |_	|jd d…= |j
 |¡S )Nr#   r$   éP   é   é   éýÿÿÿó   /)r'   ÚintZsetHostÚisSecureÚlenr1   Újoinr/   r3   ZuriZsiteZgetResourceFor)r
   r3   r*   r+   ZportZ	prefixLenr   r   r   r4   o   s    

ÿz_HostResource.getChildN)r   r   r   r4   r   r   r   r   r5   m   s   r5   c                   @   s   e Zd ZdZdd„ ZdS )ÚVHostMonsterResourcea.  
    Use this to be able to record the hostname and method (http vs. https)
    in the URL without disturbing your web site. If you put this resource
    in a URL http://foo.com/bar then requests to
    http://foo.com/bar/http/baz.com/something will be equivalent to
    http://foo.com/something, except that the hostname the request will
    appear to be accessing will be "baz.com". So if "baz.com" is redirecting
    all requests for to foo.com, while foo.com is inaccessible from the outside,
    then redirect and url generation will work correctly
    c                 C   s,   |dkrdd„ |_ n|dkr&dd„ |_ tƒ S )Ns   httpc                   S   s   dS )Nr   r   r   r   r   r   Ú<lambda>Œ   ó    z/VHostMonsterResource.getChild.<locals>.<lambda>s   httpsc                   S   s   dS )Nr$   r   r   r   r   r   r@   Ž   rA   )r<   r5   )r
   r3   r*   r   r   r   r4   Š   s
    
zVHostMonsterResource.getChildN)r   r   r   r   r4   r   r   r   r   r?   ~   s   
r?   N)r   Z
__future__r   r   Zhtmlr   r(   ÚImportErrorZcgiZtwisted.pythonr   Ztwisted.webr   Z
Homogenousr   r   r   r5   r?   r   r   r   r   Ú<module>   s   A