webserver.php
changeset 26 300d374d89b0
parent 24 d275dc8f4203
child 29 8210fdc7c317
child 32 86140ed9087d
--- a/webserver.php	Wed Jul 02 11:57:13 2008 -0400
+++ b/webserver.php	Tue Aug 05 13:17:37 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 = <<<EOF
 <html>
   <head>
@@ -1135,6 +1161,17 @@
   </body>
 </html>
 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);
   }