Skip to content
This repository

add_static_view redirects requests for directories #67

Closed
jkrebs opened this Issue December 15, 2010 · 8 comments

4 participants

Joachim Krebs Chris McDonough Jason Daly Juliusz Gonera
Joachim Krebs

With the following

config.add_static_view('static', 'myapp:static/')

requests for /static/css/ are HTTP redirected to /css/.

Chris McDonough
Owner

I've confirmed this behavior (leaving this note mostly for myself). Not really sure how (or whether) to fix.

Jason Daly

I'm doing the same, adding to the list who'd like to see this fixed.

Chris McDonough
Owner

What would fixed mean to you?

Jason Daly

With a static route like

config.add_static_view('docs', 'myapp:docs/build/htmldir/')

for Sphinx documentation (for example), a file at path myapp:docs/build/htmldir/foo/index.html should be accessible via

http://localhost/docs/foo/

No redirection should happen from /docs/foo/ to /foo/, but does.

The base /docs/ path already works this way, properly mapping

http://localhost/docs/

to myapp:docs/build/htmldir/index.html.

Chris McDonough
Owner

Yes, that makes sense.

Juliusz Gonera

I think I found the reason for such behaviour or at least a way to fix it. After going through all the process of calling a static view callable, I found that the information about the correct path is lost in pyramid.traversal.traversal_path

This function strips trailing slash and because of this docs/foo/ becomes docs/foo. Making it leave the trailing slash (strip -> lstrip) and not ignore empty segments (if not segment or segment=='.' -> if segment=='.'), solves the problem.

However, I'm not sure if it's a good fix (it breaks a bunch of traversal tests) and this is not the only issue. Why does Pyramid redirect to /foo/ URL anyway? When paste.urlparser.StaticURLParser is called and detects that the static resource is a directory, but lacks trailing slash, it invokes its add_slash method. The first line in this method is:

url = request.construct_url(environ, with_query_string=False)

and it returns the changed URL (/foo instead of /docs/foo). It's because of how path is divided in static parser (root_directory and PATH_INFO) and construct_url uses only PATH_INFO from environ and has no idea about the root directory. I have a feeling that the whole idea of putting only a part of the url in PATH_INFO is not very correct (but again, maybe I'm wrong).

On a side note, fixing this would probably also make urls of directories without trailing slash work (like http://mydomain.com/docs; right now they don't).

If something is not clear, let me know and I'll try to think about it tomorrow. I'm too sleepy now ;)

Edit: To sum up, I think there are two things that need to be fixed:

  • traversal should not strip the ending slash
  • paste.urlparser.StaticURLParser (and pyramid.static.PackageURLParser) should somehow know the full original URL to make a proper redirect in case the ending slash is missing (or in any other case if necessary)
Chris McDonough mcdonc closed this issue from a commit April 18, 2011
Chris McDonough - When visiting a URL that represented a static view which resolved to a
  subdirectory, the ``index.html`` of that subdirectory would not be served
  properly.  Instead, a redirect to ``/subdir`` would be issued.  This has
  been fixed, and now visiting a subdirectory that contains an ``index.html``
  within a static view returns the index.html properly.  See also
  #67.

- Redirects issued by a static view did not take into account any existing
  ``SCRIPT_NAME`` (such as one set by a url mapping composite).  Now they do.

Closes #67.
29a8506
Chris McDonough mcdonc closed this in 29a8506 April 17, 2011
Chris McDonough
Owner

I solved this on the trunk, thanks for the analysis jgonera.

Jason Daly

@mcdonc, thank you very much

Joey Smith tml referenced this issue from a commit April 18, 2011
Chris McDonough - When visiting a URL that represented a static view which resolved to a
  subdirectory, the ``index.html`` of that subdirectory would not be served
  properly.  Instead, a redirect to ``/subdir`` would be issued.  This has
  been fixed, and now visiting a subdirectory that contains an ``index.html``
  within a static view returns the index.html properly.  See also
  #67.

- Redirects issued by a static view did not take into account any existing
  ``SCRIPT_NAME`` (such as one set by a url mapping composite).  Now they do.

Closes #67.
29a8506
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.