Skip to content

Commit

Permalink
Merge pull request #629 from jashandeep-sohi/env-nosendfile
Browse files Browse the repository at this point in the history
Allow disabling sendfile w/ env 'AIOHTTP_NOSENDFILE=1'
  • Loading branch information
asvetlov committed Nov 10, 2015
2 parents 483dd39 + 82451b6 commit 2834235
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
3 changes: 3 additions & 0 deletions aiohttp/web_urldispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ def __init__(self, name, prefix, directory, *,
raise ValueError(
"No directory exists at '{}'".format(self._directory))

if os.environ.get("AIOHTTP_NOSENDFILE") == "1":
self._sendfile = self._sendfile_fallback

def match(self, path):
if not path.startswith(self._prefix):
return None
Expand Down
19 changes: 17 additions & 2 deletions docs/web_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1125,16 +1125,31 @@ Router is any object that implements :class:`AbstractRouter` interface.
.. method:: add_static(prefix, path, *, name=None, expect_handler=None, \
chunk_size=256*1024, response_factory=StreamResponse)

Adds router for returning static files.
Adds a router and a handler for returning static files.

Useful for handling static content like images, javascript and css files.
Useful for serving static content like images, javascript and css files.

On platforms that support it, the handler will transfer files more
efficiently using the ``sendfile`` system call.

In some situations it might be necessary to avoid using the ``sendfile``
system call even if the platform supports it. This can be accomplished by
by setting environment variable ``AIOHTTP_NOSENDFILE=1``.

.. warning::

Use :meth:`add_static` for development only. In production,
static content should be processed by web servers like *nginx*
or *apache*.

.. versionchanged:: 0.18.0
Transfer files using the ``sendfile`` system call on supported
platforms.

.. versionchanged:: 0.19.0
Disable ``sendfile`` by setting environment variable
``AIOHTTP_NOSENDFILE=1``

:param str prefix: URL path prefix for handled static files

:param str path: path to the folder in file system that contains
Expand Down
7 changes: 7 additions & 0 deletions tests/test_web_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,13 @@ def go(dirname, filename):

self.loop.run_until_complete(go(here, filename))

def test_env_nosendfile(self):
directory = os.path.dirname(__file__)

with mock.patch.dict(os.environ, {'AIOHTTP_NOSENDFILE': '1'}):
route = web.StaticRoute(None, "/", directory)
self.assertEqual(route._sendfile, route._sendfile_fallback)


class TestStaticFileSendfileFallback(StaticFileMixin,
unittest.TestCase):
Expand Down

0 comments on commit 2834235

Please sign in to comment.