How to ignore repeated slashes in Pyramid

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.

Answers


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/"


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.


Need Your Help

Check Oracle Database connectivity from Application server

oracle oracle11g database-connection windows-server application-server

We have Oracle Database DB1 hosted on Unix Server and Application on Windows server 2012.