1

I'd like to make sure that the following two urls are handled in the same way (there's an existing client which sends requests this way):

/resource
//resource

Unfortunately I cannot add both urls to the same route and even if I try using the //resource format, url matching doesn't work correctly with this case.

Is there any way to work around it so that I don't have to rewrite requests in the server in front of WSGI?

edit: I did find pyramid.event which allows me access to the NewRequest event, so I think I can rewrite the path, but with the number of different functions that get the path in various ways, I'm not sure how exactly should I rewrite it in an existing request.

edit2: It seems the whole event doesn't have information about //resource anymore - no element I query shows it. Everything claims I got a request for just /.

edit3: It seems to be actually related to paste or some other module in between, rather than Pyramid itself - uwsgi passes the original path_info with double slash without any problems.

Charles
  • 50,943
  • 13
  • 104
  • 142
viraptor
  • 33,322
  • 10
  • 107
  • 191
  • Just an idea here. You could create a middleware that will modify the PATH_INFO variable before passing it to pyramid – Loïc Faure-Lacroix Jul 16 '12 at 11:55
  • btw, seems like you found a bug in waitress. double slashes strip the first path part apparently `//something/fun` gets transformed to `/fun`, While using triple slashes gives me the right result – Loïc Faure-Lacroix Jul 16 '12 at 20:31

2 Answers2

0
import re

class RemoveDoubleSlashes(object):
    def __init__(self, application):
        self.application = application

    def __call__(self, environ, start_response):

        environ['PATH_INFO'] = re.sub('/+', '/', environ['PATH_INFO'])                                                                                                                      

        return self.application(environ, start_response)

At the end of your main:

return RemoveDoubleSlashes(config.make_wsgi_app())

I have no idea how hard it might be to do a regex replace on each request but it will do the trick if you really need that.

edit

Keep in mind that this is a way to edit the PATH_INFO before it even get to pyramid. You should not do that. You should create valid urls and let the wrong url fail.

It should be a quickfix until you find a way to fix wrongly created urls in your app.. this middleware isn't a cure, it's just a patch. It makes no sense to check all urls for repeated slashes.

Just reread your question and you could test if it starts with double slashes instead of using regex. And to just a simple replace('//', '/') if the PATH_INFO starts with double slashes.

regex makes sense only you'll have something like "//fdf//3dfdf/"

Loïc Faure-Lacroix
  • 13,220
  • 6
  • 67
  • 99
0

This seems to be a duplicate of this question, which has a useful answer.

Essentially double slashes are not supported by the WSGI and thus are not supported by Pyramid.

Community
  • 1
  • 1
Mikko Koho
  • 754
  • 6
  • 14