views:

104

answers:

2

Is there any straightforward to convert all incoming urls to lowercase before they get matched against urlpatterns in run_wsgi_app(webapp.WSGIApplication(urlpatterns))?

+3  A: 

You'd have to wrap the instance of WSGIApplication with your own WSGI app that lowercases the URL in the WSGI environment -- but then the environment would just stay modified, which may have other unpleasant effects. Why not just add (?i) to the regex patterns you use in urlpatterns instead?

Alex Martelli
Thanks.But how will this work in '/(?P<some_category>\w+)/edit/(?P<item_id>\w+)/?' where 'edit' might be 'EDIT'?
alex
@gmsd, as I said, just add `(?i)` to the pattern; for example, use `'(?i)/(?P<some_category>\w+)/edit/(?P<item_id>\w+)/?'` (just like your pattern plus `(?i)` in front, to tell `re` that the match is cases-insensitive. See http://docs.python.org/library/re.html?#regular-expression-syntax .
Alex Martelli
A: 

I wonder if you could modify your CGI environment variables before executing the WSGIApplication instance.

os.putenv(os.getenv('PATH_INFO').lower())

Something along those lines. I've done this myself for slight URL modifications, however I 301 redirected to the new URL; I didn't continue processing with WSGI.

jhs
This solution will work (with the caveats Alex mentioned), but WSGI middleware is a better idea.
Nick Johnson