tianwen2976 / webapp-improved

Automatically exported from code.google.com/p/webapp-improved
Other
0 stars 0 forks source link

Routing does not handle patterns of the form '/thanks/?' #38

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Add a route: webapp.Route(r'/thanks/?', ThanksHandler, 'thanks').
2. On the dev server, try to navigate to 'localhost8080://thanks'.
3. On the dev server, try to navigate to 'localhost8080://thanks/'.

What is the expected output? What do you see instead?
I expect to see the output of the ThanksHandler.

In both steps 2 and above, I see a 404 page.

What version of the product are you using? On what operating system?
I am using the webapp2 package provided with the google appengine SDK v1.6.0, 
running with python 2.7.2.  I believe this is webapp2 v2.3.

Please provide any additional information below.
From what I see of the code, the problem is in webapp2.py, function 
_parse_route_template().  There are two places where this function passes a 
string through re.escape(), and this messes up the pattern.

For example, the pattern '/thanks/?' becomes r'\/thanks\/\?', which is is not 
at all the same.  If you remove the calls to re.escape() the problems does not 
occur.

What is the purpose of escaping the input template this way?

Original issue reported on code.google.com by ro...@tawacentral.net on 6 Dec 2011 at 2:29

GoogleCodeExporter commented 9 years ago
I'm not 100% sure of Rodrigo's intentions here but it seems Route templates can 
only contain regular expression in regex components (i.e. <>).

Why are you using a route here anyway?

Original comment by bquin...@google.com on 7 Dec 2011 at 3:37

GoogleCodeExporter commented 9 years ago
Brian is correct.

If you use Route, all regular expression must be enclosed by <>. In the 
example: webapp.Route(r'/thanks<:/?>', ThanksHandler, 'thanks'). It may not 
make much sense because you don't want to capture the slash, but it'll capture 
it. The alternative is to use a webapp-like tuple instead of a Route instance: 
(r'/thanks/?', ThanksHandler), but then URLs can't be built.

The purpose of the <> syntax is to set placeholders to build URLs. It doesn't 
use standard regexp named groups because it would be a lot complex to parse 
(avoiding nested groups and several edge cases; see Django which fights this 
since 2005). 

Several projects adopt a similar <> approach or variants (Pyramid in Python 
land and I saw it in Java and Ruby projects too) because of how tricky is to 
reverse regexps.

Original comment by rodrigo.moraes on 7 Dec 2011 at 11:53

GoogleCodeExporter commented 9 years ago
Sorry, my bad, and thanks for the explanation.  I had not realized that regex 
expressions are only allowed inside <>.  Maybe just me, but this constraint was 
not clear from the description at 
http://webapp-improved.appspot.com/guide/routing.html#guide-routing-the-regex-te
mplate.

The reason for using a route is uri building.  I used a simplified example 
above, so maybe uri building might not seem very appropriate.  Now that I 
understand correctly I'll keep playing with it.

Original comment by ro...@tawacentral.net on 7 Dec 2011 at 1:17

GoogleCodeExporter commented 9 years ago
Ok, fair enough. I'll try to make it more clear in the docs.

Original comment by rodrigo.moraes on 7 Dec 2011 at 4:12

GoogleCodeExporter commented 9 years ago
Oops. I pointed to issue 36 in the commit. It should be more clear now.

http://code.google.com/p/webapp-improved/source/detail?r=58cfeb9c718dfb8bf916a96
fcee719bb92aeaa2e

Original comment by rodrigo.moraes on 7 Dec 2011 at 4:13