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

Auto-redirect to canonical URLs #2

Closed
ghost opened this issue Sep 19, 2004 · 5 comments
Closed

Auto-redirect to canonical URLs #2

ghost opened this issue Sep 19, 2004 · 5 comments

Comments

@ghost
Copy link

ghost commented Sep 19, 2004

Originally reported by: Anonymous


The canonical URL scheme looks like this:

  • / -> cpg.root.index()
  • /index -> cpg.root.index()
  • /a/ -> cpg.root.a.index()
  • /a/b -> cpg.root.a.b()

In other words, objects always have trailing slashes, methods never have trailing slashes.

CP2 should enforce this scheme by automatically doing an external redirect if the incoming request URI isn't "correct". For example, if the user requests /a, CP2 should redirect to /a/ before actually calling cpg.root.a.index().

If we do this, request handler classes can safely use relative HTML links to refer to their own (or their parent/child objects') methods.

See: http://sourceforge.net/mailarchive/message.php?msg_id=9558209

Reported by hmans


@ghost
Copy link
Author

ghost commented Sep 20, 2004

Original comment by Anonymous:


After some discussion on cherrypy-devel, we're considering making this a configuration option.

@ghost
Copy link
Author

ghost commented Sep 26, 2004

Original comment by sja (Bitbucket: sja, GitHub: sja):


See #4 for a patch that fixes this.

@ghost
Copy link
Author

ghost commented Sep 26, 2004

Original comment by sja (Bitbucket: sja, GitHub: sja):


Should URIs with virtual path parameters have a trailing slash?

My patch strips trailing slashes from such URLs, because I thought the most common case would be someting like /user/12, where "12" logically represents a file, or a leaf in the tree.

But what should happen when we have urls such as /articles/2004/, which returns a list of articles from the given year? Is this an index/folder? Should it have a trailing slash? If so, how do we distinguish between the former and the latter case?

Perhaps we could add a trailing slash when the list of parameters is incomplete. For example:

#!python
class Articles:
    def default(self, year, month=None, day=None, number=None):
        ...

Given the previous method definition, these are the possible valid urls:

/articles/2004/
/articles/2004/12/
/articles/2004/12/5/
/articles/2004/12/5/2

@ghost
Copy link
Author

ghost commented Sep 26, 2004

Original comment by sja (Bitbucket: sja, GitHub: sja):


I've posted an updated patch which implements the behavior I've just described in #4.

@ghost
Copy link
Author

ghost commented Sep 26, 2004

Original comment by sja (Bitbucket: sja, GitHub: sja):


There are a couple open issues with my latest implementation.

#!python
class A:
    def b(self, c=None):
        ...

In this case, b is a method, hence it will not have a trailing slash in the url (/a/b). But b allows a virtual path parameter (/a/b/45), so it's logically not a leaf, but an index... should it have a trailing slash when the parameter is unspecified?

#!python
class Articles:
    # colorScheme is meant as a query string parameter
    def default(self, year, month=None, day=None, number=None, colorScheme=1):
        ...

If a method is defined as in this example, the URL of an article will have a trailing slash (/2004/12/5/3/), though it problably should not.

A workaround is to define the method thus:

#!python
    def default(self, year, month=None, day=None, number=None, **parameters):
        ...

Personally I like this better because it creates a cleaner distinction between normal parameters and query string arguments.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

0 participants