Forum Moderators: coopster

Message Too Old, No Replies

Compress a manually closed connection AND continue processing

I can compress or close the connection early though how do I do both?

         

JAB Creations

2:09 am on Nov 17, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



- I've managed to close a PHP connection early and continue processing.
- I've managed to send a compressed response to a client.
- I have not managed to do both at the same time.

How do send a compressed response and continue processing?

Currently my somewhat minimal test case compresses the server response though it doesn't close the connection:

<?php
ob_start();
echo '<style type="text/css">* {background-color: #000; color: #fff;}</style>';
echo '<p>Testing response: '.time().'.</p>';
$c = ob_get_contents();
ob_end_clean();

ob_start('ob_gzhandler');
echo $c;
$size = ob_get_length();
ob_end_flush();


// Set the content length of the response.
header("Content-Length: {$size}");

// Close the connection.
header('Connection: close');

// Flush all output.
ob_end_flush();

// Close current session (if it exists).
if (session_id()) {session_write_close();}

sleep(2);
file_put_contents('test.txt', 'testing: '.time().'; size: '.$size);
?>

JAB Creations

3:31 am on Nov 17, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Got it...

John

<?php
ob_start();
ob_start('ob_gzhandler');

// Send your response.
echo '<style>* {background-color: #000; color: #fff;}</style>';
echo '<p>Testing response: '.time().'.</p>';

// The ob_gzhandler one
ob_end_flush();

header('Content-Length: '.ob_get_length());

// The main one
ob_end_flush();
ob_flush();
flush();

// Close current session (if it exists).
if (session_id()) {session_write_close();}

sleep(4);
file_put_contents('test.txt', 'testing: '.time().'; size: '.$size);
?>

JorgeV

1:26 pm on Nov 17, 2020 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member Top Contributors Of The Month



Hello,

Just to add that, if you use PHP-FPM , you can use :

fastcgi_finish_request
[php.net...]

This will send all the buffers, and close the connection to the client. Then, the remain of the PHP script will be executed without impact on the client. (no wait)

w3dk

10:16 pm on Nov 17, 2020 (gmt 0)

10+ Year Member Top Contributors Of The Month




// The main one
ob_end_flush();
ob_flush();
flush();


Do I take it that the final flush() was the missing piece to the puzzle?

The ob_flush() would seem to be superfluous? You're starting ob twice, but closing 3 times?

JAB Creations

10:52 pm on Nov 18, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Jorge, I don't use PHP-FPM though I'm sure someone at some point will come across that and be happy for it.

W3dk, I commented out each one and they're all apparently necessary. I come across really dumb stuff like a numeric variable set to 9 and it's NOT greater than 0. Literally almost every single time I post a bug report I encounter, "I don't want to fix this and everyone thinks like me even though every time I drive everyone else is the idiot so no way anyone will think this is even a bug." response. I also deal with a lot of poor documentation. A lot of developers will talk about how you can do X or Y with A or B though not why it is necessary or important. While I'm not one of those developers I can not be the entirety of society to myself. So until other people start giving a damn what works is what works. I am however open to non-solicited improvements.

John