11

I'm using Django with Google's App Engine.

I want to send information to the server with percent encoded slashes. A request like http:/localhost/turtle/waxy%2Fsmooth that would match against a URL like r'^/turtle/(?P<type>([A-Za-z]|%2F)+)$'. The request gets to the server intact, but sometime before it is compared against the regex the %2F is converted into a forward slash.

What can I do to stop the %2Fs from being converted into forward slashes? Thanks!

7
  • 2
    Why can't you just change the regexp to use the non-encoded version? Jun 14, 2010 at 20:32
  • 4
    We're using a RESTful design and have other regexes that look like r'^/turtle/(?P<type>([A-Za-z]|%2F)+)/shell$'. We want to distinguish between slashes that are part of the URL hierarchy and slashes that are part of a turtle name. Jun 14, 2010 at 20:48
  • 1
    I will add this to my list of requirements that I use when evaluating candidate Python web frameworks. Jun 15, 2010 at 5:01
  • You say it's "sometimes" converted - under what circumstances? Jun 15, 2010 at 16:04
  • It says "sometime". I meant that at some point after it gets to the server but before it is compared to the regex the %2F is converted to a forward slash. Jun 15, 2010 at 16:27

1 Answer 1

4

os.environ['PATH_INFO'] is decoded, so you lose that information. Probably os.environ['REQUEST_URI'] is available, and if it is available it is not decoded. Django only reads PATH_INFO. You could probably do something like:

request_uri = environ['REQUEST_URI']
request_uri = re.sub(r'%2f', '****', request_uri, re.I)
environ['PATH_INFO'] = urllib.unquote(request_uri)

Then all cases of %2f are replaced with **** (or whatever you want to use).

8
  • What sets REQUEST_URI? I don't see it in pep 333's required wsgi variable list.
    – ʇsәɹoɈ
    Jun 15, 2010 at 1:37
  • It's not required in PEP 333, but it is widely set as part of CGI (or CGI-like) requests. On GAE if it exists, then it is certain to keep existing. It is the complete request path, with no URL unencoding done to it. Jun 15, 2010 at 2:18
  • 2
    os.environ['REQUEST_URI'] is not available on GAE. Jun 15, 2010 at 3:05
  • 2
    Another technique that works for GData is that you have things like /{http:%2f%2ffoo}attr/ (which by the time it gets to WSGI looks like /{http://foo}attr/), but because it uses nested braces you can parse out the chunks. You could design your URLs similarly. Jun 16, 2010 at 4:16
  • 1
    I think that the suggestion of bracketing the names is probably the best solution short of hacking Django. Thanks for your help, Ian! Jun 17, 2010 at 23:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.