U
    •uÄc(  ã                   @   s¼   d dl mZ d dlmZ zd dlmZ W nF ek
rn   zd dlmZ W n  ek
rh   d dl	mZ Y nX Y nX d dlm
Z
mZ d dlmZ dZG dd	„ d	eƒZee
ƒG d
d„ deƒƒZdS )é    )Úimplementer)ÚProtocolWrapper©ÚnotFound)Ú
NoResource)Ú	IResourceÚResource)ÚNOT_DONE_YET)ÚWebSocketResourceÚWSGIRootResourcec                   @   s    e Zd ZdZdd„ Zdd„ ZdS )r   a«  
    Root resource when you want a WSGI resource be the default serving
    resource for a Twisted Web site, but have subpaths served by
    different resources.

    This is a hack needed since
    `twisted.web.wsgi.WSGIResource <http://twistedmatrix.com/documents/current/api/twisted.web.wsgi.WSGIResource.html>`_.
    does not provide a ``putChild()`` method.

    .. seealso::

       * `Autobahn Twisted Web WSGI example <https://github.com/crossbario/autobahn-python/tree/master/examples/twisted/websocket/echo_wsgi>`_
       * `Original hack <http://blog.vrplumber.com/index.php?/archives/2426-Making-your-Twisted-resources-a-url-sub-tree-of-your-WSGI-resource....html>`_
    c                 C   s   t  | ¡ || _|| _dS )a}  

        :param wsgiResource: The WSGI to serve as root resource.
        :type wsgiResource: Instance of `twisted.web.wsgi.WSGIResource <http://twistedmatrix.com/documents/current/api/twisted.web.wsgi.WSGIResource.html>`_.
        :param children: A dictionary with string keys constituting URL subpaths, and Twisted Web resources as values.
        :type children: dict
        N)r   Ú__init__Ú_wsgiResourceÚchildren)ÚselfZwsgiResourcer   © r   úZ/var/www/html/HOTSTAR/school/venv/lib/python3.8/site-packages/autobahn/twisted/resource.pyr   F   s    
zWSGIRootResource.__init__c                 C   s   |j  ¡  |j d|¡ | jS )Nr   )ZprepathÚpopZpostpathÚinsertr   )r   ÚpathÚrequestr   r   r   ÚgetChildR   s    
zWSGIRootResource.getChildN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   6   s   r   c                   @   s4   e Zd ZdZdZdd„ Zdd„ Zdd„ Zd	d
„ ZdS )r
   z/
    A Twisted Web resource for WebSocket.
    Tc                 C   s
   || _ dS )zˆ

        :param factory: An instance of :class:`autobahn.twisted.websocket.WebSocketServerFactory`.
        :type factory: obj
        N)Ú_factory)r   Úfactoryr   r   r   r   _   s    zWebSocketResource.__init__c                 C   s
   t dd�S )zR
        This resource cannot have children, hence this will always fail.
        zNo such child resource.)Úmessager   )r   Únamer   r   r   r   ÚgetChildWithDefaulth   s    z%WebSocketResource.getChildWithDefaultc                 C   s   dS )zS
        This resource cannot have children, hence this is always ignored.
        Nr   )r   r   Úchildr   r   r   ÚputChildn   s    zWebSocketResource.putChildc                 C   sL  |j jdkrJddlm} | dd¡ | dd¡ |jd|jf }| d	¡S | j	 
|j ¡ ¡}|sn| d
¡ dS |j jd }|j _t|tƒr’||_nt|jtƒr¨||j_n||_| |¡ t|dƒrÎ|j ¡  nt|dƒrà| ¡  |jd |j d }|j ¡ D ](}||d d d |d ¡ d 7 }� qþ|d7 }||j ¡ 7 }| |¡ tS )zâ
        Render the resource. This will takeover the transport underlying
        the request, create a :class:`autobahn.twisted.websocket.WebSocketServerProtocol`
        and let that do any subsequent communication.
        Nr   )Úprotocoliª  s   Upgrade requireds   Upgrades	   WebSocketÚ Úutf8iô  ó    Ú_networkProducerÚresumeProducingó    s    HTTP/1.1
s   : ó   ,é   s   
)ZchannelÚ	transportZautobahn.websocketr"   ZsetResponseCodeZ	setHeaderZ_SERVER_STATUS_TEMPLATEÚ__version__Úencoder   ZbuildProtocolZgetPeerÚ
isinstancer   ZwrappedProtocolZmakeConnectionÚhasattrr&   r'   ÚmethodZuriZrequestHeadersZgetAllRawHeadersÚjoinÚcontentÚreadZdataReceivedr	   )r   r   r"   Úhtmlr+   ÚdataÚhr   r   r   Úrenders   s8    	






&
zWebSocketResource.renderN)	r   r   r   r   ZisLeafr   r   r!   r7   r   r   r   r   r
   X   s   	r
   N)Zzope.interfacer   Ztwisted.protocols.policiesr   Ztwisted.web.pagesr   ÚImportErrorZtwisted.web.resourcer   Ztwisted.web.errorr   r   Ztwisted.web.serverr	   Ú__all__r   Úobjectr
   r   r   r   r   Ú<module>   s   "