Steve Hay wrote: > I'm in the process of converting some old software from mod_perl1 to > mod_perl2, and I'm finding that $r->bytes_sent() doesn't work as it used > to.
yeah. IIRC the issue is that now we have filters, so in order to actually calculate bytes_sent() the filters need to run in their entirety. > > The code is written in a very out-moded style: it is compatible with > mod_cgi, runs via ModPerl::Registry (formerly Apache::Registry), and > produces all its output via explicit print() statements scattered left, > right and centre, including a print() statement to send the response > headers. An error-handling subroutine calls $r->bytes_sent() to find out > if anything has been sent yet, so that it knows whether or not it needs > to send response headers before sending an error page (it could be > invoked either before or after the headers have been sent in the normal > course of events). > > This works fine under mod_perl1, but under mod_perl2 I find that the > error-handler always emits response headers itself (i.e. it always > thinks that nothing has been sent yet). Therefore, in those cases where > response headers have already been sent, I now have a second set being > sent, which the user ends up seeing in the web browser. The code in > question is simply: > > unless (exists $ENV{MOD_PERL} > ? Apache2::RequestUtil->request()->bytes_sent() > : tell STDOUT) > { > #... send response headers since you no longer send response headers in mp2, isn't this all moot? > } > #... send error page > > There is a suggestion in > > http://perl.apache.org/docs/2.0/api/Apache2/RequestRec.html#C_bytes_sent > _ > > that bytes_sent() doesn't work quite like it used to, so I assume that > this is a known problem / intended functionality. There is mention there > of buffering issues, so I've tried calling $r->rflush() first but > $r->bytes_sent() still returns 0. Setting $|=1 on STDOUT also doesn't > help. right, if this is all from the response phase then the header filter, content length filter, includes filter, etc all haven't run yet, so no bytes have actually been sent. > > Is there any way to get this to work under mod_perl2? If the data is > indeed buffered somewhere then is there any way to test whether or not > the buffer is empty? there probably is using the buckets api. but again, I'm not sure that you need to check for the presence of error headers before sending the error page anymore, since httpd sends headers for you. HTH --Geoff