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

Fix static location in index when prefix is used #1662

Merged
merged 2 commits into from
May 14, 2017
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
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