Skip to content

Commit 43d8bf1

Browse files
lovebug356asvetlov
authored andcommitted
Fix static location in index when prefix is used (#1662)
When the prefix is used (e.g. not /), the links generated in the index page are wrong. In this patch the relative path is correctly calculated by the pathlib module and a absolute url is used as link in the index page. The original bug can be demonstrated with following example: --- import pathlib from aiohttp import web app = web.Application() app.router.add_static('/static', pathlib.Path(__file__).parent, show_index=True) web.run_app(app) ---
1 parent 13681f8 commit 43d8bf1

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed

CONTRIBUTORS.txt

+1
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ Taha Jahangir
152152
Taras Voinarovskyi
153153
Terence Honles
154154
Thanos Lefteris
155+
Thijs Vermeir
155156
Thomas Grainger
156157
Tolga Tezel
157158
Vaibhav Sagar

aiohttp/web_urldispatcher.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -492,11 +492,7 @@ def _directory_as_html(self, filepath):
492492
# sanity check
493493
assert filepath.is_dir()
494494

495-
posix_dir_len = len(self._directory.as_posix())
496-
497-
# remove the beginning of posix path, so it would be relative
498-
# to our added static path
499-
relative_path_to_dir = filepath.as_posix()[posix_dir_len:]
495+
relative_path_to_dir = filepath.relative_to(self._directory).as_posix()
500496
index_of = "Index of /{}".format(relative_path_to_dir)
501497
head = "<head>\n<title>{}</title>\n</head>".format(index_of)
502498
h1 = "<h1>{}</h1>".format(index_of)
@@ -505,7 +501,8 @@ def _directory_as_html(self, filepath):
505501
dir_index = filepath.iterdir()
506502
for _file in sorted(dir_index):
507503
# show file url as relative to static path
508-
file_url = _file.as_posix()[posix_dir_len:]
504+
file_url = self._prefix + '/' + \
505+
_file.relative_to(self._directory).as_posix()
509506

510507
# if file is a directory, add '/' to the end of the name
511508
if _file.is_dir():

tests/test_web_urldispatcher.py

+14-8
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,23 @@ def teardown():
2929
return tmp_dir
3030

3131

32-
@pytest.mark.parametrize("show_index,status,data",
33-
[(False, 403, None),
34-
(True, 200,
35-
b'<html>\n<head>\n<title>Index of /</title>\n'
36-
b'</head>\n<body>\n<h1>Index of /</h1>\n<ul>\n'
32+
@pytest.mark.parametrize("show_index,status,prefix,data",
33+
[(False, 403, '/', None),
34+
(True, 200, '/',
35+
b'<html>\n<head>\n<title>Index of /.</title>\n'
36+
b'</head>\n<body>\n<h1>Index of /.</h1>\n<ul>\n'
3737
b'<li><a href="/my_dir">my_dir/</a></li>\n'
3838
b'<li><a href="/my_file">my_file</a></li>\n'
39+
b'</ul>\n</body>\n</html>'),
40+
(True, 200, '/static',
41+
b'<html>\n<head>\n<title>Index of /.</title>\n'
42+
b'</head>\n<body>\n<h1>Index of /.</h1>\n<ul>\n'
43+
b'<li><a href="/static/my_dir">my_dir/</a></li>\n'
44+
b'<li><a href="/static/my_file">my_file</a></li>\n'
3945
b'</ul>\n</body>\n</html>')])
4046
@asyncio.coroutine
4147
def test_access_root_of_static_handler(tmp_dir_path, loop, test_client,
42-
show_index, status, data):
48+
show_index, status, prefix, data):
4349
"""
4450
Tests the operation of static file server.
4551
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,
6167
app = web.Application()
6268

6369
# Register global static route:
64-
app.router.add_static('/', tmp_dir_path, show_index=show_index)
70+
app.router.add_static(prefix, tmp_dir_path, show_index=show_index)
6571
client = yield from test_client(app)
6672

6773
# Request the root of the static directory.
68-
r = yield from client.get('/')
74+
r = yield from client.get(prefix)
6975
assert r.status == status
7076

7177
if data:

0 commit comments

Comments
 (0)