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

add_static_view redirects requests for directories #67

Closed
jkrebs opened this issue Dec 15, 2010 · 8 comments

Comments

Projects
None yet
4 participants
@jkrebs
Copy link
Contributor

commented Dec 15, 2010

With the following

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

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

@mcdonc

This comment has been minimized.

Copy link
Member

commented Jan 25, 2011

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

@deefour

This comment has been minimized.

Copy link

commented Apr 13, 2011

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

@mcdonc

This comment has been minimized.

Copy link
Member

commented Apr 13, 2011

What would fixed mean to you?

@deefour

This comment has been minimized.

Copy link

commented Apr 13, 2011

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.

@mcdonc

This comment has been minimized.

Copy link
Member

commented Apr 13, 2011

Yes, that makes sense.

@jgonera

This comment has been minimized.

Copy link
Contributor

commented Apr 14, 2011

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)

@mcdonc mcdonc closed this in 29a8506 Apr 18, 2011

@mcdonc

This comment has been minimized.

Copy link
Member

commented Apr 18, 2011

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

@deefour

This comment has been minimized.

Copy link

commented Apr 18, 2011

@mcdonc, thank you very much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.