diff --git a/aspen/algorithms/website.py b/aspen/algorithms/website.py index d90a37e9b..3d35a6b4b 100644 --- a/aspen/algorithms/website.py +++ b/aspen/algorithms/website.py @@ -62,19 +62,30 @@ def raise_200_for_OPTIONS(request): def dispatch_request_to_filesystem(website, request): + + def handle_directory(result): + if not website.list_directories: + raise Response(404) + result.extra['autoindexdir'] = result.match + result.match = website.ours_or_theirs('autoindex.html.spt') + assert result.match is not None # sanity check + return result + result = dispatcher.dispatch( website - , indices=website.indices - , media_type_default=website.media_type_default - , pathparts=request.line.uri.path.parts - , uripath=request.line.uri.path.raw - , querystring=request.line.uri.querystring.raw - , startdir=website.www_root + , indices = website.indices + , media_type_default = website.media_type_default + , pathparts = request.line.uri.path.parts + , uripath = request.line.uri.path.raw + , querystring = request.line.uri.querystring.raw + , startdir = website.www_root + , handle_directory = handle_directory ) request.fs = result.match for k, v in result.wildcards.iteritems(): request.line.uri.path[k] = v return {'dispatch_result': result} + def apply_typecasters_to_path(website, request): typecasting.apply_typecasters(website.typecasters, request.line.uri.path) diff --git a/aspen/dispatcher.py b/aspen/dispatcher.py index a6e3c6061..656969578 100644 --- a/aspen/dispatcher.py +++ b/aspen/dispatcher.py @@ -265,7 +265,7 @@ def update_neg_type(media_type_default, capture_accept, filename): def dispatch(website, indices, media_type_default, pathparts, uripath, querystring, - startdir, pure_dispatch=False): + startdir, handle_directory, pure_dispatch=False): """Concretize dispatch_abstract. """ @@ -342,12 +342,7 @@ def dispatch(website, indices, media_type_default, pathparts, uripath, querystri if result.status == DispatchStatus.okay: if result.match.endswith('/'): # autoindex - if not website.list_directories: - raise Response(404) - result.extra['autoindexdir'] = result.match - result.match = website.ours_or_theirs('autoindex.html.spt') - assert result.match is not None # sanity check - return result # return so we skip the no-escape check + return handle_directory(result) # return so we skip the no-escape check elif result.status == DispatchStatus.non_leaf: # trailing-slash redirect location = uripath + '/' diff --git a/tests/test_dispatcher.py b/tests/test_dispatcher.py index 3e1d9ad17..e36b29a14 100644 --- a/tests/test_dispatcher.py +++ b/tests/test_dispatcher.py @@ -5,11 +5,9 @@ import os from pytest import raises -from StringIO import StringIO import aspen from aspen import dispatcher, Response -from aspen.http.request import Request # Helpers @@ -58,6 +56,7 @@ def test_dispatcher_returns_a_result(harness): , '/' , '' , harness.fs.www.root + , lambda result: result ) assert result.status == dispatcher.DispatchStatus.okay assert result.match == os.path.join(harness.fs.www.root, 'index.html') @@ -72,6 +71,7 @@ def test_dispatcher_returns_a_result_for_favicon(harness): , '/favicon.ico' , '' , harness.fs.www.root + , lambda result: result ) assert result.status == dispatcher.DispatchStatus.okay assert result.match == harness.client.website.find_ours('favicon.ico') @@ -80,14 +80,21 @@ def test_dispatcher_returns_a_result_for_favicon(harness): def test_dispatcher_returns_a_result_for_autoindex(harness): harness.client.website.list_directories = True - result = dispatcher.dispatch( harness.client.website + tracer = object() + actual = dispatcher.dispatch( harness.client.website , [] , '' , [''] , '/' , '' , harness.fs.www.root + , lambda result: tracer ) + assert actual is tracer + +def test_dispatcher_in_algorithm_returns_a_better_result_for_autoindex(harness): + harness.client.website.list_directories = True + result = harness.simple(filepath=None, uripath='/', want='dispatch_result') assert result.status == dispatcher.DispatchStatus.okay assert result.match == harness.client.website.find_ours('autoindex.html.spt') assert result.wildcards == {}