o
    @»Bi(  ã                   @   s¬   d dl mZ d dlmZ d dlmZ zd dlmZ W n ey)   d dl	mZ Y nw d dl	m
Z
mZ d dl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 )é    )Úabsolute_import)Úimplementer)ÚProtocolWrapper©Ú
NoResource)Ú	IResourceÚResource)ÚPY3)Ú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)ÚselfÚwsgiResourcer   © r   úY/var/www/html/Trade-python/venv/lib/python3.10/site-packages/autobahn/twisted/resource.pyr   D   s   

zWSGIRootResource.__init__c                 C   s   |j  ¡  |j d|¡ | jS )Nr   )ÚprepathÚpopÚpostpathÚinsertr   )r   ÚpathÚrequestr   r   r   ÚgetChildP   s   
zWSGIRootResource.getChildN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   4   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ƒS )zR
        This resource cannot have children, hence this will always fail.
        zNo such child resource.r   )r   Únamer   r   r   r   ÚgetChildWithDefaultf   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   ÚputChildl   s    zWebSocketResource.putChildc                 C   s€  |j jdu r%ddlm} | dd¡ | dd¡ |jd|jf }| d	¡S | j	 
|j ¡ ¡}|s7| d
¡ dS |j jd}|j _t|tƒrI||_n||_| |¡ t|dƒr\|j ¡  n	t|dƒre| ¡  tr–|jd |j d }|j ¡ D ]}||d d d |d ¡ d 7 }qv|d7 }||j ¡ 7 }n#d|j|jf }|j ¡ D ]}|d|d d |d ¡f 7 }q£|d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%s %s HTTP/1.1
z%s: %s
ú,z
)ÚchannelÚ	transportÚautobahn.websocketr%   ÚsetResponseCodeÚ	setHeaderÚ_SERVER_STATUS_TEMPLATEÚ__version__Úencoder   ÚbuildProtocolÚgetPeerÚ
isinstancer   ÚwrappedProtocolÚmakeConnectionÚhasattrr)   r*   r	   ÚmethodÚuriÚrequestHeadersÚgetAllRawHeadersÚjoinÚcontentÚreadÚdataReceivedr
   )r   r   r%   Úhtmlr0   ÚdataÚhr   r   r   Úrenderq   s>   	





$ 
zWebSocketResource.renderN)	r   r   r   r   ÚisLeafr   r"   r$   rH   r   r   r   r   r   V   s    	r   N)Ú
__future__r   Úzope.interfacer   Útwisted.protocols.policiesr   Útwisted.web.errorr   ÚImportErrorÚtwisted.web.resourcer   r   Úsixr	   Útwisted.web.serverr
   Ú__all__r   Úobjectr   r   r   r   r   Ú<module>   s   þ"