diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt
index e59077aef22..804ec72e634 100644
--- a/CONTRIBUTORS.txt
+++ b/CONTRIBUTORS.txt
@@ -140,6 +140,7 @@ Sviatoslav Bulbakha
Taha Jahangir
Taras Voinarovskyi
Terence Honles
+Thijs Vermeir
Thomas Grainger
Tolga Tezel
Vaibhav Sagar
diff --git a/aiohttp/web_urldispatcher.py b/aiohttp/web_urldispatcher.py
index 3fbf0742eb4..956d05b70b8 100644
--- a/aiohttp/web_urldispatcher.py
+++ b/aiohttp/web_urldispatcher.py
@@ -500,11 +500,7 @@ def _directory_as_html(self, filepath):
# sanity check
assert filepath.is_dir()
- posix_dir_len = len(self._directory.as_posix())
-
- # remove the beginning of posix path, so it would be relative
- # to our added static path
- relative_path_to_dir = filepath.as_posix()[posix_dir_len:]
+ relative_path_to_dir = filepath.relative_to(self._directory).as_posix()
index_of = "Index of /{}".format(relative_path_to_dir)
head = "
\n{}\n".format(index_of)
h1 = "{}
".format(index_of)
@@ -513,7 +509,8 @@ def _directory_as_html(self, filepath):
dir_index = filepath.iterdir()
for _file in sorted(dir_index):
# show file url as relative to static path
- file_url = _file.as_posix()[posix_dir_len:]
+ file_url = self._prefix + '/' + \
+ _file.relative_to(self._directory).as_posix()
# if file is a directory, add '/' to the end of the name
if _file.is_dir():
diff --git a/tests/test_web_urldispatcher.py b/tests/test_web_urldispatcher.py
index 3cd7ecd10e0..e852fff69b5 100644
--- a/tests/test_web_urldispatcher.py
+++ b/tests/test_web_urldispatcher.py
@@ -29,17 +29,23 @@ def teardown():
return tmp_dir
-@pytest.mark.parametrize("show_index,status,data",
- [(False, 403, None),
- (True, 200,
- b'\n\nIndex of /\n'
- b'\n\nIndex of /
\n\n'
+@pytest.mark.parametrize("show_index,status,prefix,data",
+ [(False, 403, '/', None),
+ (True, 200, '/',
+ b'\n\nIndex of /.\n'
+ b'\n\nIndex of /.
\n\n\n'),
+ (True, 200, '/static',
+ b'\n\nIndex of /.\n'
+ b'\n\nIndex of /.
\n\n\n')])
@asyncio.coroutine
def test_access_root_of_static_handler(tmp_dir_path, loop, test_client,
- show_index, status, data):
+ show_index, status, prefix, data):
"""
Tests the operation of static file server.
Try to access the root of static file server, and make
@@ -61,11 +67,11 @@ def test_access_root_of_static_handler(tmp_dir_path, loop, test_client,
app = web.Application(loop=loop)
# Register global static route:
- app.router.add_static('/', tmp_dir_path, show_index=show_index)
+ app.router.add_static(prefix, tmp_dir_path, show_index=show_index)
client = yield from test_client(app)
# Request the root of the static directory.
- r = yield from client.get('/')
+ r = yield from client.get(prefix)
assert r.status == status
if data: