Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add response factory to StaticRoute. #456

Merged
merged 1 commit into from
Aug 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions aiohttp/web_urldispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ def __repr__(self):
class StaticRoute(Route):

def __init__(self, name, prefix, directory, *,
expect_handler=None, chunk_size=256*1024):
expect_handler=None, chunk_size=256*1024,
response_factory=None):
assert prefix.startswith('/'), prefix
assert prefix.endswith('/'), prefix
super().__init__(
Expand All @@ -152,6 +153,10 @@ def __init__(self, name, prefix, directory, *,
self._prefix_len = len(self._prefix)
self._directory = os.path.abspath(directory) + os.sep
self._chunk_size = chunk_size
if response_factory is None:
self._response_factory = StreamResponse
else:
self._response_factory = response_factory

if not os.path.isdir(self._directory):
raise ValueError(
Expand Down Expand Up @@ -187,7 +192,7 @@ def handle(self, request):
if not ct:
ct = 'application/octet-stream'

resp = StreamResponse()
resp = self._response_factory()
resp.content_type = ct
if encoding:
resp.headers[hdrs.CONTENT_ENCODING] = encoding
Expand Down Expand Up @@ -407,7 +412,7 @@ def add_route(self, method, path, handler,
return route

def add_static(self, prefix, path, *, name=None, expect_handler=None,
chunk_size=256*1024):
chunk_size=256*1024, response_factory=None):
"""
Adds static files view
:param prefix - url prefix
Expand All @@ -418,6 +423,7 @@ def add_static(self, prefix, path, *, name=None, expect_handler=None,
prefix += '/'
route = StaticRoute(name, prefix, path,
expect_handler=expect_handler,
chunk_size=chunk_size)
chunk_size=chunk_size,
response_factory=response_factory)
self.register_route(route)
return route
9 changes: 8 additions & 1 deletion docs/web_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1014,7 +1014,7 @@ Router is any object that implements :class:`AbstractRouter` interface.
:returns: new :class:`PlainRoute` or :class:`DynamicRoute` instance.

.. method:: add_static(prefix, path, *, name=None, expect_handler=None, \
chunk_size=256*1024)
chunk_size=256*1024, response_factory=None)

Adds router for returning static files.

Expand Down Expand Up @@ -1044,6 +1044,13 @@ Router is any object that implements :class:`AbstractRouter` interface.

.. versionadded:: 0.16

:param callable response_factory: factory to use to generate a new
response, defaults to
:class:`StreamResponse` and should
expose a compatible API.

.. versionadded:: 0.17

:returns: new :class:`StaticRoute` instance.

.. coroutinemethod:: resolve(requst)
Expand Down