import os
from engineio.static_files import get_static_file
class WSGIApp(object):
"""WSGI application middleware for [Link].
This middleware dispatches traffic to an [Link] 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 [Link] server. Must be an instance of the
``[Link]`` 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 [Link] application should
be installed. The default value is appropriate for
most cases.
Example usage::
import engineio
import eventlet
eio = [Link]()
app = [Link](eio, static_files={
'/': {'content_type': 'text/html', 'filename': '[Link]'},
'/[Link]': {'content_type': 'text/html',
'filename': '[Link]'},
})
[Link]([Link](('', 8000)), app)
"""
def __init__(self, engineio_app, wsgi_app=None, static_files=None,
engineio_path='[Link]'):
self.engineio_app = engineio_app
self.wsgi_app = wsgi_app
self.engineio_path = engineio_path.strip('/')
self.static_files = static_files or {}
def __call__(self, environ, start_response):
if '[Link]' in environ:
# gunicorn saves the socket under environ['[Link]'], while
# eventlet saves it under environ['[Link]']. Eventlet also
# stores the socket inside a wrapper class, while gunicon writes it
# directly into the environment. To give eventlet's WebSocket
# module access to this socket when running under gunicorn, here we
# copy the socket to the eventlet format.
class Input(object):
def __init__(self, socket):
[Link] = socket
def get_socket(self):
return [Link]
environ['[Link]'] = Input(environ['[Link]'])
path = environ['PATH_INFO']
if path is not None and \
[Link]('/{0}/'.format(self.engineio_path)):
return self.engineio_app.handle_request(environ, start_response)
else:
static_file = get_static_file(path, self.static_files) \
if self.static_files else None
if static_file:
if [Link](static_file['filename']):
start_response(
'200 OK',
[('Content-Type', static_file['content_type'])])
with open(static_file['filename'], 'rb') as f:
return [[Link]()]
else:
return self.not_found(start_response)
elif self.wsgi_app is not None:
return self.wsgi_app(environ, start_response)
return self.not_found(start_response)
def not_found(self, start_response):
start_response("404 Not Found", [('Content-Type', 'text/plain')])
return [b'Not Found']
class Middleware(WSGIApp):
"""This class has been renamed to ``WSGIApp`` and is now deprecated."""
def __init__(self, engineio_app, wsgi_app=None,
engineio_path='[Link]'):
super(Middleware, self).__init__(engineio_app, wsgi_app,
engineio_path=engineio_path)