Disable output buffering in CGI scripts?


#1

How do I disable output buffering in CGI scripts so the browser prints text as it’s available (as opposed to waiting for all text output then rendering it all in one shot) ?

I currently have this in my code:

$|=1;

… which works on other hosting providers. But it doesn’t work on my dreamhost account on the greengoblin server. I initially thought it was a problem with the mod_deflate module since that’s known to buffer all output first, but I had a support rep turn that off for my account but it didn’t help.

Anyone have any other tricks to make this work?

Take this example:

#!/usr/bin/perl
use strict;
$|=1;
print “Content-type: text/plain\n\n” ;
print “I was here\n” ;
sleep 3 ;
print “I was here again…\n” ;
exit ;
#eof

… If this were written as a CGI script, it should print the first line of text (“I was here\n”), then sleep 3 seconds, and then render the second line of text (“I was here again…\n”)

In fact, here are two URL’s to test:
One hosting provider where the output buffering works as expected:
http://iandouglas.com/cgi-bin/sleep-test.cgi

Here at dreamhost, where output buffering is turned on:
http://warchestgame.com/cgi-bin/sleep-test.cgi

This works for me on other hosting providers, but not here at Dreamhost, and I’m not sure what else to ask support to try. (Daniel J, the support rep helping me didn’t know what else to try either).

Thanks!


#2

You could try turning off DEFLATE in htaccess.

The site you mention above - it shows the same symptoms on first load from a clean cache, but works correctly after hitting reload.

Maximum Cash Discount on any plan with MAXCASH

How To Install PHP.INI / ionCube on DreamHost


#3

Hey there, thanks for the reply. Daniel J in DH Support disabled mod_deflate completely in my domain’s httpd.conf and it still didn’t help. I had also disabled it in .htaccess as well, and got the same result – even after several runs of the script in case it was cached.

Any other ideas?


#4

It’s still serving up with Content-Encoding: gzip

Maximum Cash Discount on any plan with MAXCASH

How To Install PHP.INI / ionCube on DreamHost


#5

Just tried it out. Whack this in your .htaccess:

Shazam! :wink:

Maximum Cash Discount on any plan with MAXCASH

How To Install PHP.INI / ionCube on DreamHost


#6

This worked by adding it to the http headers:

print (“Content-encoding: none\nContent-type: text/plain\n\n”) ;

The .htaccess directive “SetEnv no-gzip” didn’t seem to work for me.


#7

That’s odd. You can bang the livehttpheaders tool in to Firefox to quickly check if the SetEnv directive isn’t available for some reason.

Maximum Cash Discount on any plan with MAXCASH

How To Install PHP.INI / ionCube on DreamHost


#8

THIS TOTALLY WORKED FOR ME! Thank you!!

One slight mod for the scripters out there, just add the content-encoding bit to your header printer.

In perl CGI this looks like:
$_query->header(“Content-encoding: none”);

Oh and I had $| = 1;