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

Allow disabling sendfile w/ env 'AIOHTTP_NOSENDFILE=1' #629

Merged
merged 1 commit into from
Nov 10, 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
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":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm concerned that definition of this environment variable switch is differs from other environment variable switches used in asyncio/aiohttp.

AIOHTTP_NO_EXTENSIONS is checked in the following way:

if bool(os.environ.get('AIOHTTP_NO_EXTENSIONS')):

At least inside aiohttp library environment variables switches should work in the same way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was fixed after the merge: 9dc529b

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, thanks!

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