Skip to content

Commit

Permalink
Fix static location in index when prefix is used (#1662)
Browse files Browse the repository at this point in the history
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)

---
  • Loading branch information
lovebug356 authored and asvetlov committed May 14, 2017
1 parent 13681f8 commit 43d8bf1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ Taha Jahangir
Taras Voinarovskyi
Terence Honles
Thanos Lefteris
Thijs Vermeir
Thomas Grainger
Tolga Tezel
Vaibhav Sagar
Expand Down
9 changes: 3 additions & 6 deletions aiohttp/web_urldispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,11 +492,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 = "<head>\n<title>{}</title>\n</head>".format(index_of)
h1 = "<h1>{}</h1>".format(index_of)
Expand All @@ -505,7 +501,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():
Expand Down
22 changes: 14 additions & 8 deletions tests/test_web_urldispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,23 @@ def teardown():
return tmp_dir


@pytest.mark.parametrize("show_index,status,data",
[(False, 403, None),
(True, 200,
b'<html>\n<head>\n<title>Index of /</title>\n'
b'</head>\n<body>\n<h1>Index of /</h1>\n<ul>\n'
@pytest.mark.parametrize("show_index,status,prefix,data",
[(False, 403, '/', None),
(True, 200, '/',
b'<html>\n<head>\n<title>Index of /.</title>\n'
b'</head>\n<body>\n<h1>Index of /.</h1>\n<ul>\n'
b'<li><a href="/my_dir">my_dir/</a></li>\n'
b'<li><a href="/my_file">my_file</a></li>\n'
b'</ul>\n</body>\n</html>'),
(True, 200, '/static',
b'<html>\n<head>\n<title>Index of /.</title>\n'
b'</head>\n<body>\n<h1>Index of /.</h1>\n<ul>\n'
b'<li><a href="/static/my_dir">my_dir/</a></li>\n'
b'<li><a href="/static/my_file">my_file</a></li>\n'
b'</ul>\n</body>\n</html>')])
@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
Expand All @@ -61,11 +67,11 @@ def test_access_root_of_static_handler(tmp_dir_path, loop, test_client,
app = web.Application()

# 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:
Expand Down

0 comments on commit 43d8bf1

Please sign in to comment.