Stream/chunk PHP response to browser

LSUser12

Well-Known Member
#1
Hi,

It seems like in the default configuration for the litespeed server, it will wait till the entire PHP result is generated, and then it will reply with a Content-Length.

I would expect that for a long running script, which streams its response over a number of seconds, that Litespeed would reply with a chunked encoding.

In this topic mistwang (the LS employee) replied stating chunked encoding should be avoided. I can appreciate that chunked encoding has overhead and that it's more efficient to use a Content-Length when sending/receiving HTTP messages; however, for a slow PHP page this isn't ideal.

For an e-commerce site, that may take a few seconds to load, waiting the entire page load time means the browser can't begin processing css/javascript includes. This results in a worse experience for the user, regardless of overhead.

An example PHP script to produce this with the LSAPI is:

Code:
<?php

for($i = 1; $i < 10; $i++){

        print "$i<br>\n";
        sleep(1);

}

?>
If you run this against the LSPHP binary, it will appropriately print 1 through 9 as they are generated (every 1 second); however, if you run this through litespeed, you'll have to wait for the entire page to load.

Is there any way to make it so that Litespeed "streams" or chunks the reply, so it doesn't wait to generate the content-length based response?

Thanks!
 

LSUser12

Well-Known Member
#2
I was able to get this to work in perl with:

Code:
#!/usr/bin/perl

use strict;
use warnings;
use CGI;

$| = 1;
my $q = new CGI;
print $q->header(-type => 'text/html');

for(my $i = 1; $i < 10; $i++){

print "$i<br>\n";
sleep 1;

}
So I guess it's not a limitation of litespeed, maybe with my PHP configuration.
 
Last edited:

NiteWave

Administrator
#4
I did a few quick tests under apache and litespeed.

Code:
        print "$i<br>\n";
apache: "Content-Length: 54"
litespeed: "Content-Length:47"

Code:
        print "$i<br>\n";
flush();
this will force apache "Transfer-Encoding: chunked" but lsws still output "Content-Length:..."

Code:
        print "$i<br>\n";
ob_flush();
apache and litespeed will output "Content-Length:..."

Code:
        print "$i<br>\n";
ob_flush();
flush();
both apache and litespeed will output "Transfer-Encoding: chunked"

since this is a quick test, just run it on my local box, so php is not the latest version.
Server: Apache/2.2.3 (CentOS)
X-Powered-By: PHP/5.1.6

Server: LiteSpeed -> 4.2.12
X-Powered-By: PHP/5.4.18 ( lsapi V6.3)

not sure if it'll be same results with your environment
 
Top