3
ò\ð]Ì  ã               @   s8   d dl Z d dlmZ G dd„ deƒZG dd„ deƒZdS )é    N)Úget_static_filec               @   s*   e Zd ZdZd
dd„Zdd„ Zdd	„ ZdS )ÚWSGIAppa§  WSGI application middleware for Engine.IO.

    This middleware dispatches traffic to an Engine.IO application. It can
    also serve a list of static files to the client, or forward unrelated
    HTTP traffic to another WSGI application.

    :param engineio_app: The Engine.IO server. Must be an instance of the
                         ``engineio.Server`` class.
    :param wsgi_app: The WSGI app that receives all other traffic.
    :param static_files: A dictionary with static file mapping rules. See the
                         documentation for details on this argument.
    :param engineio_path: The endpoint where the Engine.IO application should
                          be installed. The default value is appropriate for
                          most cases.

    Example usage::

        import engineio
        import eventlet

        eio = engineio.Server()
        app = engineio.WSGIApp(eio, static_files={
            '/': {'content_type': 'text/html', 'filename': 'index.html'},
            '/index.html': {'content_type': 'text/html',
                            'filename': 'index.html'},
        })
        eventlet.wsgi.server(eventlet.listen(('', 8000)), app)
    Nú	engine.ioc             C   s&   || _ || _|jdƒ| _|pi | _d S )Nú/)Úengineio_appÚwsgi_appÚstripÚengineio_pathÚstatic_files)Úselfr   r   r
   r	   © r   ú>/tmp/pip-build-mqc4i71p/python-engineio/engineio/middleware.pyÚ__init__"   s    zWSGIApp.__init__c          	   C   sä   d|kr(G dd„ dt ƒ}||d ƒ|d< |d }|d k	rX|jdj| jƒƒrX| jj||ƒS | jrjt|| jƒnd }|rÄtj	j
|d ƒr¸|dd	|d
 fgƒ t|d dƒ�}|jƒ gS Q R X qÚ| j|ƒS n| jd k	rÚ| j||ƒS | j|ƒS )Nzgunicorn.socketc               @   s   e Zd Zdd„ Zdd„ ZdS )zWSGIApp.__call__.<locals>.Inputc             S   s
   || _ d S )N)Úsocket)r   r   r   r   r   r   2   s    z(WSGIApp.__call__.<locals>.Input.__init__c             S   s   | j S )N)r   )r   r   r   r   Ú
get_socket5   s    z*WSGIApp.__call__.<locals>.Input.get_socketN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   ÚInput1   s   r   zeventlet.inputZ	PATH_INFOz/{0}/Úfilenamez200 OKzContent-TypeÚcontent_typeÚrb)ÚobjectÚ
startswithÚformatr	   r   Úhandle_requestr
   r   ÚosÚpathÚexistsÚopenÚreadÚ	not_foundr   )r   ÚenvironÚstart_responser   r   Zstatic_fileÚfr   r   r   Ú__call__)   s&    
zWSGIApp.__call__c             C   s   |ddgƒ dgS )Nz404 Not FoundúContent-Typeú
text/plains	   Not Found)r&   r'   r   )r   r#   r   r   r   r!   M   s    zWSGIApp.not_found)NNr   )r   r   r   Ú__doc__r   r%   r!   r   r   r   r   r      s
    
$r   c                   s"   e Zd ZdZd‡ fdd„	Z‡  ZS )Ú
MiddlewarezAThis class has been renamed to ``WSGIApp`` and is now deprecated.Nú	engine.ioc                s   t t| ƒj|||d� d S )N)r	   )Úsuperr)   r   )r   r   r   r	   )Ú	__class__r   r   r   T   s    zMiddleware.__init__)Nr*   )r   r   r   r(   r   Ú__classcell__r   r   )r,   r   r)   R   s    r)   )r   Zengineio.static_filesr   r   r   r)   r   r   r   r   Ú<module>   s   M