Skip to content

Commit

Permalink
Support setting socketio_path to the root URL (Fixes #242)
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Jul 26, 2021
1 parent 583c7db commit 47ada56
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
8 changes: 6 additions & 2 deletions src/engineio/async_drivers/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,18 @@ def __init__(self, engineio_server, other_asgi_app=None,
on_startup=None, on_shutdown=None):
self.engineio_server = engineio_server
self.other_asgi_app = other_asgi_app
self.engineio_path = engineio_path.strip('/')
self.engineio_path = engineio_path
if not self.engineio_path.startswith('/'):
self.engineio_path = '/' + self.engineio_path
if not self.engineio_path.endswith('/'):
self.engineio_path += '/'
self.static_files = static_files or {}
self.on_startup = on_startup
self.on_shutdown = on_shutdown

async def __call__(self, scope, receive, send):
if scope['type'] in ['http', 'websocket'] and \
scope['path'].startswith('/{0}/'.format(self.engineio_path)):
scope['path'].startswith(self.engineio_path):
await self.engineio_server.handle_request(scope, receive, send)
else:
static_file = get_static_file(scope['path'], self.static_files) \
Expand Down
9 changes: 6 additions & 3 deletions src/engineio/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ def __init__(self, engineio_app, wsgi_app=None, static_files=None,
engineio_path='engine.io'):
self.engineio_app = engineio_app
self.wsgi_app = wsgi_app
self.engineio_path = engineio_path.strip('/')
self.engineio_path = engineio_path
if not self.engineio_path.startswith('/'):
self.engineio_path = '/' + self.engineio_path
if not self.engineio_path.endswith('/'):
self.engineio_path += '/'
self.static_files = static_files or {}

def __call__(self, environ, start_response):
Expand All @@ -55,8 +59,7 @@ def get_socket(self):

environ['eventlet.input'] = Input(environ['gunicorn.socket'])
path = environ['PATH_INFO']
if path is not None and \
path.startswith('/{0}/'.format(self.engineio_path)):
if path is not None and path.startswith(self.engineio_path):
return self.engineio_app.handle_request(environ, start_response)
else:
static_file = get_static_file(path, self.static_files) \
Expand Down
4 changes: 2 additions & 2 deletions tests/asyncio/test_async_asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ def test_create_app(self):
'eio',
'other_app',
static_files='static_files',
engineio_path='/foo',
engineio_path='/foo/',
)
assert app.engineio_server == 'eio'
assert app.other_asgi_app == 'other_app'
assert app.static_files == 'static_files'
assert app.engineio_path == 'foo'
assert app.engineio_path == '/foo/'

def test_engineio_routing(self):
mock_server = mock.MagicMock()
Expand Down
2 changes: 1 addition & 1 deletion tests/common/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,4 @@ def test_legacy_middleware_class(self):
assert m.engineio_app == 'eio'
assert m.wsgi_app == 'wsgi'
assert m.static_files == {}
assert m.engineio_path == 'eio_path'
assert m.engineio_path == '/eio_path/'

0 comments on commit 47ada56

Please sign in to comment.