Skip to content

Commit

Permalink
optimization to static file serving
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Jun 3, 2019
1 parent 7709b8c commit 5b87010
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion engineio/async_drivers/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ async def __call__(self, scope, receive, send):
await self.engineio_server.handle_request(scope, receive, send)
else:
static_file = get_static_file(scope['path'], self.static_files) \
if scope['type'] == 'http' else None
if scope['type'] == 'http' and self.static_files else None
if static_file:
await self.serve_static_file(static_file, receive, send)
elif self.other_asgi_app is not None:
Expand Down
3 changes: 2 additions & 1 deletion engineio/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ def get_socket(self):
path.startswith('/{0}/'.format(self.engineio_path)):
return self.engineio_app.handle_request(environ, start_response)
else:
static_file = get_static_file(path, self.static_files)
static_file = get_static_file(path, self.static_files) \
if self.static_files else None
if static_file:
if os.path.exists(static_file['filename']):
start_response(
Expand Down
5 changes: 5 additions & 0 deletions tests/asyncio/test_async_asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ def check_path(path, status_code, content_type, body):
check_path('/static/foo.bar', 404, 'text/plain', 'Not Found')
check_path('/static/test/index.html', 200, 'text/html',
'<html></html>\n')
check_path('/bar/foo', 404, 'text/plain', 'Not Found')
check_path('', 404, 'text/plain', 'Not Found')

app.static_files[''] = 'index.html'
check_path('/static/test/', 200, 'text/html',
Expand All @@ -102,6 +104,9 @@ def check_path(path, status_code, content_type, body):
app.static_files[''] = {'filename': 'test.gif'}
check_path('/static/test/', 404, 'text/plain', 'Not Found')

app.static_files = {}
check_path('/static/test/index.html', 404, 'text/plain', 'Not Found')

def test_lifespan_startup(self):
app = async_asgi.ASGIApp('eio')
scope = {'type': 'lifespan'}
Expand Down
9 changes: 7 additions & 2 deletions tests/common/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ def check_path(path, status_code, content_type, body):
'Not Found')
check_path('/static/test/index.html', '200 OK', 'text/html',
'<html></html>\n')
check_path('/static/test/', '200 OK', 'text/html',
'<html></html>\n')
check_path('/static/test/', '200 OK', 'text/html', '<html></html>\n')
check_path('/bar/foo', '404 Not Found', 'text/plain', 'Not Found')
check_path('', '404 Not Found', 'text/plain', 'Not Found')

m.static_files[''] = 'index.html'
check_path('/static/test/', '200 OK', 'text/html',
Expand All @@ -77,6 +78,10 @@ def check_path(path, status_code, content_type, body):
check_path('/static/test/', '404 Not Found', 'text/plain',
'Not Found')

m.static_files = {}
check_path('/static/test/index.html', '404 Not Found', 'text/plain',
'Not Found')

def test_404(self):
mock_wsgi_app = None
mock_eio_app = mock.Mock()
Expand Down

0 comments on commit 5b87010

Please sign in to comment.