[mod_python] Cookies & mod_rewrite

Graham Dumpleton grahamd at dscpl.com.au
Tue Sep 26 00:02:10 EDT 2006


CARTWRIGHT, Guy, GBM wrote ..
> >  From memory, you can set req.headers_in and it will go 
> > through to backend via proxy. Give it a try anyway. Simply go:
> > 
> >    req.headers_in['X-MyHeader'] = 'test'
> > 
> > and see if it gets through.
> > 
> 
> No luck with the above, or with:
> 
>     req.headers_out['X-MyHeader'] = 'test'
> 
> but the following does work and gets passed through to the back-end
> server...
> 
>     req.err_headers_out['X-MyHeader'] = 'test'
>     return apache.OK
> 
> Weird...

That doesn't make a great deal of sense and I wouldn't rely on it.

FWIW, after I got proxying in mod_python 3.3 working again (I had managed to
break it), I found the following worked fine.

import posixpath 

from mod_python import apache, Cookie

def fixuphandler(req): 

  if req.proxyreq: 
    return apache.DECLINED 

  normalised_uri = posixpath.normpath(req.uri) 

  if normalised_uri: 
    if normalised_uri != '/' and req.uri[-1] == '/': 
      normalised_uri += '/' 

  length = len(req.filename) 
  length -= len(req.hlist.directory) - 1 
  length += len(req.path_info or '') 

  baseurl = normalised_uri[:-length] 
  path = normalised_uri[len(baseurl):] 

  req.proxyreq = apache.PROXYREQ_REVERSE 
  req.uri = 'http://localhost:8082/~grahamd/headers' + path 
  req.filename = 'proxy:%s' % req.uri 
  req.handler = 'proxy-server' 

  req.headers_in['Cookie'] = str(Cookie.Cookie('XXX', 'YYY'))

  req.log_error('uri = %s' % req.uri)
  req.log_error('filename = %s' % req.filename)
  req.log_error('handler = %s' % req.handler)

  return apache.OK 

In the backend server, could then use:

from mod_python import apache, Cookie

def handler(req):
  req.content_type = 'text/plain'
  for name in req.headers_in:
    req.write('%s: %s\n' % (name, req.headers_in[name]))

  req.write('\n')
  req.write(str(Cookie.get_cookies(req)))
  return apache.OK

I still need to commit changes back into mod_python repository to fix what I
broke, but mod_python 3.3 certainly provides more flexibility than perhaps
using mod_rewrite.

I'll get around to trying to do it with mod_rewrite later.

Sorry, for late followup, have been away for a week.

Graham


More information about the Mod_python mailing list