# HG changeset patch # User Dan # Date 1217956677 14400 # Node ID 20a36fe254c968a6e9a5921b5da8326790fa16c9 # Parent 300d374d89b0ad32350de9301e41f54d312aa050# Parent 5c377ceb0e4cd270ab47a3dc8ebefbfa2cc8150f Merging artwork (nighthawk) + keepalive (scribus) branches diff -r 5c377ceb0e4c -r 20a36fe254c9 webserver.php --- a/webserver.php Tue Aug 05 13:15:11 2008 -0400 +++ b/webserver.php Tue Aug 05 13:17:57 2008 -0400 @@ -21,6 +21,13 @@ define('HTTPD_VERSION', '0.1b1'); /** + * Length of keep-alive connections + * @const int + */ + +define('HTTPD_KEEP_ALIVE_TIMEOUT', 300); + +/** * Webserver system icons */ @@ -348,15 +355,39 @@ // read request $last_line = ''; $client_headers = ''; - while ( $line = @socket_read($remote, 1024, PHP_NORMAL_READ) ) + if ( defined('HTTPD_WS_CHILD') ) + { + if ( !@socket_set_timeout($remote, HTTPD_KEEP_ALIVE_TIMEOUT) ) + { + status('stream_set_timeout() on $remote failed.'); + var_dump($remote); + } + } + if ( $line = @socket_read($remote, 1024, PHP_NORMAL_READ) ) { - $line = str_replace("\r", "", $line); - if ( empty($line) ) - continue; - if ( $line == "\n" && $last_line == "\n" ) - break; - $client_headers .= $line; - $last_line = $line; + do + { + $line = str_replace("\r", "", $line); + if ( empty($line) ) + continue; + if ( $line == "\n" && $last_line == "\n" ) + break; + $client_headers .= $line; + $last_line = $line; + } + while ( $line = @socket_read($remote, 1024, PHP_NORMAL_READ) ); + } + else + { + if ( defined('HTTPD_WS_CHILD') ) + { + $md = @socket_get_status($remote); + if ( @$md['timed_out'] ) + { + status('[debug] keep-alive connection timed out'); + continue; // will jump back to the start of the loop and kill the child process + } + } } // parse request @@ -654,6 +685,7 @@ // if ( defined('HTTPD_WS_CHILD') ) // status('Continuing connection'); // @socket_write($remote, "\r\n\r\n"); + $last_finish_time = time(); } else { @@ -1115,13 +1147,7 @@ global $http_responses; $reason_code = ( isset($http_responses[$http_code]) ) ? $http_responses[$http_code] : 'Unknown'; - // if we're in a scriptlet, include custom headers - if ( $this->in_scriptlet ) - $headers = implode("\r\n", $this->response_headers); - else - $headers = ''; - - $this->send_client_headers($socket, $http_code, 'text/html', $headers); + // generate error page $html = << @@ -1135,6 +1161,17 @@ EOF; + + // length of the response (required if we want keep-alive to work) + $this->header('Content-length: ' . strlen($html)); + + // if we're in a scriptlet, include custom headers + if ( $this->in_scriptlet ) + $headers = implode("\r\n", $this->response_headers); + else + $headers = 'Content-length: ' . strlen($html); + + $this->send_client_headers($socket, $http_code, 'text/html', $headers); @socket_write($socket, $html); }