[mod_python] Input filter content-length problem

Graham Dumpleton graham.dumpleton at gmail.com
Tue Oct 9 18:53:28 EDT 2007


On 10/10/2007, Amos Latteier <amos at latteier.com> wrote:
> Hi,
>
> I'm creating an input filter to rewrite POSTed form data before
> handling. It works great with one exception:
>
>   * My filter changes the length of the request body (since it writes
> more than it reads), but I cannot figure out how to change the
> Content-Length header to match.
>
> So the program that handles for form data sees the changes that my
> input filter makes, but it sees the original Content-Length header,
> and thus misses some of the data that my filter adds to the request
> body.
>
> I've tried changing the content length by doing:
>
> filter.req.headers_in['Content-Length'] = str(len(my_new_request_body))
>
> before I make any calls to filter.write(). However this doesn't affect
> the Content-Length header.
>
> I'd be grateful for any suggestions. Thanks!

You can't change the content length.

First off, there are problems in mod_python that can result in loss of
input data if the request content is mutated and thus ends up being
more than originally posted. This is because mod_python wrongly pays
attention to the Content-Length header and doesn't read more than
that. Details in:

  http://issues.apache.org/jira/browse/MODPYTHON-215

I can't remember if this is the full answer, but the only way around
it is to wipe the Content-Length header entirely. This though can only
be done if you are using Apache 2.2 and is done using the mod_filter
directives described in:

  http://httpd.apache.org/docs/2.2/mod/mod_filter.html

to designate that your input filter changes the content length of the
input. By doing this, Apache will wipe the Content-Length header for
you so that applications will not get an incorrect value.

This means though that an application must be written simply to read
all available input and not expect that content length can be used.
This can cause problems though with certain applications. For example,
if using any WSGI based application you are effectively stuffed as the
WSGI protocol doesn't support the concept of mutating input filters
that change the incoming content length. Instead, WSGI applications
expect Content-Length to be defined and that will read that amount of
data and nothing more. That WSGI is deficient in this respect is known
and raised as an issue to be solved in WSGI 2.0. See:

  http://www.wsgi.org/wsgi/WSGI_2.0

As documented in the referenced issues, mod_python also has issues in
its form processing support.

Graham


More information about the Mod_python mailing list