o
    >h(                     @   s   d dl mZ d dlmZ zd dlmZ W n! ey5   zd dlmZ W n ey2   d dl	mZ Y nw Y nw 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)selfwsgiResourcer    r   y/var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-packages/autobahn/twisted/resource.pyr   F   s   

zWSGIRootResource.__init__c                 C   s   |j   |jd| | jS )Nr   )prepathpop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   sJ  |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|jtrT||j_n||_|| t|drg|j  n	t|drp|  |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   
)channel	transportautobahn.websocketr%   setResponseCode	setHeader_SERVER_STATUS_TEMPLATE__version__encoder   buildProtocolgetPeer
isinstancer   wrappedProtocolmakeConnectionhasattrr)   r*   methodurirequestHeadersgetAllRawHeadersjoincontentreaddataReceivedr	   )r   r   r%   htmlr/   datahr   r   r   renders   s8   	






$
zWebSocketResource.renderN)	r   r   r   r   isLeafr   r"   r$   rG   r   r   r   r   r
   X   s    	r
   N)zope.interfacer   twisted.protocols.policiesr   twisted.web.pagesr   ImportErrortwisted.web.resourcer   twisted.web.errorr   r   twisted.web.serverr	   __all__r   objectr
   r   r   r   r   <module>   s$   "