Question

in my GAE app i use webapp2.RequestHandler.initialize to do custom stuff to the request.
up to a few days ago changing os.environ['PATH_INFO'] did influence calling self.request.path on the RequestHandler and it reflected the changed request path. (and this still works fine on the SDK)

now it does not work any more. and of course im having huge issues because of it. i understand that this might be an edge case but what are the reasons this changed?

the affected code:

class BaseHandler(webapp2.RequestHandler):

    def initialize(self, request, response):    
        ns, path = get_namespace(os.environ)
        namespace_manager.set_namespace(ns)
        os.environ['namespace'] = ns

        # request.path reflects the incoming path

        path = os.environ.get('PATH_INFO')
        prefix = '/%s'%ns

        if ns and path.startswith(prefix):
            # the request.path has to be changed here...

            newpath = path[len(prefix):]
            # here i change the path_info in os.environ to the new 
            # path
            os.environ['PATH_INFO'] = newpath or '/'

        super(BaseHandler, self).initialize(request, response)

        # request.path and self.request.path here are still unchanged.
        # up to a few days ago here the path was reflecting the changes
Was it helpful?

Solution

os.environ contains the CGI-style environment variables. WSGI applications such as webapp(2) may get their information from there (or not, depending on the container), but they're welcome - and even likely - to copy, not reference the original data. There's no reason to assume that manipulating os.environ will affect your WSGI app, and doing so is a bad idea for several reasons:

  1. It breaks abstraction
  2. It may not be threadsafe
  3. It's just nasty

Instead, modify the request object directly (eg, by assigning to request.path), if you must - or better, store the relevant information in attributes on the request handler that your subhandlers can access.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top