[mod_python] changing req.proxyreq on the fly

Graham Dumpleton grahamd at dscpl.com.au
Fri Mar 3 00:39:47 EST 2006

FWIW, if req.proxyreq is made writable, it would be good at the same
time to make req.uri writable as well. This would allow for example a
PythonTransHandler which did something like the following:

def transhandler(req):
    if req.proxyreq:
        return apache.DECLINED

    if req.uri.find('/search/') == 0:
        req.proxyreq = apache.PROXYREQ_PROXY
        req.uri = 'http://www.google.com/' + req.uri[len('/search/'):]
        req.filename= "proxy:%s' % req.uri
        req.handler= 'proxy-server'
        return apache.OK

    return apache.DECLINED

That is, you could from within a mod_python handler when detecting
a certain URL prefix, dynamically cause Apache to proxy the request to
another location.

This example is based on a mod_perl example which does something
similar. See:

I'll definitely create a JIRA issue for this, as can see a use for this
in some stuff I have to do. I was going to create a static snippet of
Apache configuration with lots of proxy stuff, but this would be more


Graham Dumpleton wrote ..
> Bill Fraser wrote ..
> > Hello, this is my first post to this list, and I'm rather new to
> > Python, so please excuse me if my question is a trivial one. :)
> > 
> > I'm using Apache2's mod_rewrite for a proxy system on my webserver,
> > and I've noticed that no matter what, mod_rewrite instructs mod_proxy
> > to add all the X-Forwarded-* headers, despite the fact that the proxy
> > request is forward proxy and not a reverse one. Looking at the Apache2
> > source, mod_proxy specifically mentions in the relevant comment block
> > that this should NOT be done for forward proxy requests (and in fact
> > they are messing up the application this is for), yet mod_rewrite will
> > ALWAYS add them, no matter what the nature of the proxy request is.
> > 
> > Looking further in the source, I found that the way to change this
> > behavior is to change the value of the r->proxyreq variable from
> > PROXYREQ_REVERSE (what mod_rewrite sets it to) to PROXYREQ_PROXY. A
> > nice thing about Python is its ability to attach itself to arbitrary
> > points in the request process, so I thought I could change this value.
> > No luck. req.proxyreq is marked as read-only.
> There are various members in the req object in mod_python which are
> read only when they should be writable as well. I have been slowly identifying
> them and making them writable as appropriate.
> In the case of req.proxyreq, because it is an integer value, I think only
> a one line change is required to make it writable.
> Index: src/requestobject.c
> ===================================================================
> --- src/requestobject.c (revision 382636)
> +++ src/requestobject.c (working copy)
> @@ -1675,7 +1675,7 @@
>      {"main",       (getter)getmakeobj, NULL, "If subrequest, pointer to
> the main request", "main"},
>      {"the_request", (getter)getreq_recmbr, NULL, "First line of request",
> "the_request"},
>      {"assbackwards", (getter)getreq_recmbr, (setter)setreq_recmbr, "HTTP/0.9
> \"simple\" request", "assbackwards"},
> -    {"proxyreq",     (getter)getreq_recmbr, NULL, "A proxy request: one
> of apache.PROXYREQ_* values", "proxyreq"},
> +    {"proxyreq",     (getter)getreq_recmbr, (setter)setreq_recmbr, "A
> proxy request: one of apache.PROXYREQ_* values", "proxyreq"},
>      {"header_only",  (getter)getreq_recmbr, NULL, "HEAD request, as oppsed
> to GET", "header_only"},
>      {"protocol",     (getter)getreq_recmbr, NULL, "Protocol as given to
> us, or HTTP/0.9", "protocol"},
>      {"proto_num",    (getter)getreq_recmbr, NULL, "Protocol version. 1.1
> = 1001", "proto_num"},
> I haven't tested this yet though.
> Can you possibly add an issue for this at:
>   http://issues.apache.org/jira/browse/MODPYTHON
> If not I'll add one later and include your email.
> Graham
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python

More information about the Mod_python mailing list