Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Ticket #323 (closed enhancement: fixed)

Opened 14 years ago

Last modified 12 years ago

[PATCH] Redirect GETs to subobjects so that there is a trailing slash

Reported by: kevin Owned by: anonymous
Priority: normal Milestone: 0.9
Component: CherryPy Version:
Severity: normal Keywords:
Cc:

Description

Relative URLs don't work properly unless the browser is pointing to the desired parent level (which sometimes means that a trailing slash on the URL is needed). This should be added automatically for a GET (possible with an option to turn it off). The argument that is apparently made against this is for the sake of POSTing, which doesn't always redirect well.

Attachments

turbogears-redirect.diff Download (1.6 KB) - added by bbockelm@… 14 years ago.
Adds trailing slash for non-POST requests
turbogears-redirect.2.diff Download (1.7 KB) - added by bbockelm@… 14 years ago.
Fix to take queryString into consideration

Change History

comment:1 Changed 14 years ago by elvelind grandin

 http://www.cherrypy.org/wiki/ExposeAs has some code for this.

Changed 14 years ago by bbockelm@…

Adds trailing slash for non-POST requests

Changed 14 years ago by bbockelm@…

Fix to take queryString into consideration

comment:2 Changed 14 years ago by bbockelm@…

Fixed in the patch turbogears-redirect.2.diff .

This patch automatically adds a trailing slash whenever it won't mess up the request.

I added a keyword argument named "slash" to the expose method. Set it to True, and it adds trailing slash whenever possible. Set it to False, and it removes trailing slashes where possible. Set it to something like None, and it doesn't change the URL.

In the patch, it defaults to True, but I would recommend defaulting it to None to reduce the number of redirects.

comment:3 Changed 14 years ago by kevin

  • Summary changed from Redirect GETs to subobjects so that there is a trailing slash to [PATCH] Redirect GETs to subobjects so that there is a trailing slash

Yeah, you wouldn

comment:4 Changed 14 years ago by stephen@…

The problem with POSTs still exists, as the current patch makes the trailing slash 'fixed' for GETs, but accepts both /foo and /foo/ as being the same thing for the purposes of a POST.

I think the best thing to do in the case of a POST is to raise an error if the slash isn't used correctly. Both the programmer and the user have control over GETs, but the programmer is expected to get the POSTs correct.

To illustrate the behaviour I think is more appropriate:

@expose(slash=True)
def foo(self):
    return dict()

GET /foo will redirect to /foo/

GET /foo/ will render the page

POST /foo will be a 405 Method Not Allowed

POST /foo/ will render the page

comment:5 Changed 14 years ago by anonymous

Whoa guys. Why do we need to automatically add a / all the time?

GET /foo should only redirect if foo is an object. It shouldn't redirect if it's a method. However if you do POST /foo and foo is an object, then a 405 is a good idea.

I think the CherryPy? people handled it all right, and there is a ticket open at cherrypy.org to *remove* trailing /s whenever necessary:

 http://www.cherrypy.org/ticket/433

I can even work on that and have it submitted ASAP if everyone agrees that forcing /s everywhere is not the best way to go!

comment:6 Changed 14 years ago by kevin

  • Status changed from new to closed
  • Resolution set to fixed

It turns out that some code in startup.py was breaking CherryPy?'s behavior (add trailing slash when you hit an index method without one). I fixed this.

comment:7 Changed 14 years ago by michele

This also fixes the annoying problem you had with the trailing slash on your DataController devcast.

Thanks Kevin!

Note: See TracTickets for help on using tickets.