Skip to content

Commit

Permalink
Fix static location in index when prefix is used
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 committed Feb 19, 2017
1 parent e1c682c commit 3ade2d0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ Sviatoslav Bulbakha
Taha Jahangir
Taras Voinarovskyi
Terence Honles
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 @@ -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 = "<head>\n<title>{}</title>\n</head>".format(index_of)
h1 = "<h1>{}</h1>".format(index_of)
Expand All @@ -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():
Expand Down
25 changes: 17 additions & 8 deletions tests/test_web_urldispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,24 @@ 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,16 +68,18 @@ 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:
assert r.headers['Content-Type'] == "text/html; charset=utf-8"
read_ = (yield from r.read())
print(read_)
print(data)
assert read_ == data
yield from r.release()

Expand Down

0 comments on commit 3ade2d0

Please sign in to comment.