[mod_python] Headers not being sent when input/output is over a certain size

Sidnei da Silva sidnei at enfoldsystems.com
Wed Sep 5 12:21:27 EDT 2007

Ok, I was finally able to figure out part of the problem and a solution for 

The issue is that if the response fits in 'io_buffer_size' which defaults to 
8k, then the output filters get all called in the same iteration. In 
particular, there's one filter called 'HTTP_HEADER' which is responsible for 
emitting the headers. Obviously, to make things work right, it should be the 
last one to be called, which it is apparently.

However, when the response is larger than io_buffer_size, the filters get 
called multiple times. Unfortunately, HTTP_HEADER is one such filter, so the 
first time around the mod_python filter gets called, but hasn't seen EOS, so 
it doesn't change the headers. Then the HTTP_HEADER gets called and sends 
the headers. Then after that you can't change the headers anymore.

My initial hack was to set 'req.assbackwards', which prevented the 
HTTP_HEADER from doing it's thing, and then when the mod_python filter sees 
EOS then I set 'req.assbackwards' to 1 and call 
req.add_output_filter('HTTP_HEADER'). That works, but depends on filter 
implementation internals, so I felt I needed a better fix.

Then I stumble upon 'mod_filter', which has lots of interesting 
configuration knobs. The interesting one is 'FilterProtocol', which lets you 
specify if your filter will change the content length and if it supports 
byte ranges, and also that you want your filter to be the first one in the 
chain. So I toggled those settings, and it magically works.

Here's my configuration file:

It would be great to document this somewhere, so that the next person 
doesn't have to suffer that much. Maybe add some notes to mod_python docs? I 
will certainly write a blog entry.

Sidnei da Silva
Enfold Systems, Inc. 

More information about the Mod_python mailing list