webserver.php
changeset 12 b3fcc21e557f
parent 10 d3059e20b0fa
child 13 b5db2345c397
equal deleted inserted replaced
11:0faea3a6c881 12:b3fcc21e557f
   135   function serve()
   135   function serve()
   136   {
   136   {
   137     while ( true )
   137     while ( true )
   138     {
   138     {
   139       // wait for connection...
   139       // wait for connection...
   140       $remote = socket_accept($this->sock);
   140       // trick from http://us.php.net/manual/en/function.socket-accept.php
       
   141       $remote = false;
       
   142       switch(@socket_select($r = array($this->sock), $w = array($this->sock), $e = array($this->sock), 5)) {
       
   143         case 2:
       
   144           break;
       
   145         case 1:
       
   146           $remote = @socket_accept($this->sock);
       
   147           break;
       
   148         case 0:
       
   149           break;
       
   150       }
       
   151          
       
   152       if ( !$remote )
       
   153       {
       
   154         continue;
       
   155       }
       
   156       
   141       // read request
   157       // read request
   142       $last_line = '';
   158       $last_line = '';
   143       $client_headers = '';
   159       $client_headers = '';
   144       while ( $line = socket_read($remote, 1024, PHP_NORMAL_READ) )
   160       while ( $line = @socket_read($remote, 1024, PHP_NORMAL_READ) )
   145       {
   161       {
   146         $line = str_replace("\r", "", $line);
   162         $line = str_replace("\r", "", $line);
   147         if ( empty($line) )
   163         if ( empty($line) )
   148           continue;
   164           continue;
   149         if ( $line == "\n" && $last_line == "\n" )
   165         if ( $line == "\n" && $last_line == "\n" )
   284     
   300     
   285     $headers = str_replace("\r\n", "\n", $headers);
   301     $headers = str_replace("\r\n", "\n", $headers);
   286     $headers = str_replace("\n", "\r\n", $headers);
   302     $headers = str_replace("\n", "\r\n", $headers);
   287     $headers = preg_replace("#[\r\n]+$#", '', $headers);
   303     $headers = preg_replace("#[\r\n]+$#", '', $headers);
   288     
   304     
   289     socket_write($socket, "HTTP/1.1 $http_code $reason_code\r\n");
   305     @socket_write($socket, "HTTP/1.1 $http_code $reason_code\r\n");
   290     socket_write($socket, "Server: $this->server_string");
   306     @socket_write($socket, "Server: $this->server_string");
   291     socket_write($socket, "Connection: close\r\n");
   307     @socket_write($socket, "Connection: close\r\n");
   292     socket_write($socket, "Content-Type: $contenttype\r\n");
   308     @socket_write($socket, "Content-Type: $contenttype\r\n");
   293     if ( !empty($headers) )
   309     if ( !empty($headers) )
   294     {
   310     {
   295       socket_write($socket, "$headers\r\n");
   311       @socket_write($socket, "$headers\r\n");
   296     }
   312     }
   297     socket_write($socket, "\r\n");
   313     @socket_write($socket, "\r\n");
   298   }
   314   }
   299   
   315   
   300   /**
   316   /**
   301    * Sends a normal response
   317    * Sends a normal response
   302    * @param resource Socket connection to client
   318    * @param resource Socket connection to client
   375             $contents .= "\n    </ul>\n    <address>Served by {$this->server_string}</address>\n</body>\n</html>\n\n";
   391             $contents .= "\n    </ul>\n    <address>Served by {$this->server_string}</address>\n</body>\n</html>\n\n";
   376             
   392             
   377             $sz = strlen($contents);
   393             $sz = strlen($contents);
   378             $this->send_client_headers($socket, 200, 'text/html', "Content-length: $sz\r\n");
   394             $this->send_client_headers($socket, 200, 'text/html', "Content-length: $sz\r\n");
   379             
   395             
   380             socket_write($socket, $contents);
   396             @socket_write($socket, $contents);
   381             
   397             
   382             return true;
   398             return true;
   383           }
   399           }
   384           
   400           
   385           // try to open the file
   401           // try to open the file
   402           $this->send_client_headers($socket, 200, $mimetype, "Content-length: $sz\r\nLast-Modified: $time\r\n");
   418           $this->send_client_headers($socket, 200, $mimetype, "Content-length: $sz\r\nLast-Modified: $time\r\n");
   403           
   419           
   404           // send body
   420           // send body
   405           while ( $blk = @fread($fh, 768000) )
   421           while ( $blk = @fread($fh, 768000) )
   406           {
   422           {
   407             socket_write($socket, $blk);
   423             @socket_write($socket, $blk);
   408           }
   424           }
   409           fclose($fh);
   425           fclose($fh);
   410           return true;
   426           return true;
   411         }
   427         }
   412         else
   428         else
   453           $this->send_client_headers($socket, 200, $mimetype, "Content-length: $sz\r\nLast-Modified: $time\r\n");
   469           $this->send_client_headers($socket, 200, $mimetype, "Content-length: $sz\r\nLast-Modified: $time\r\n");
   454           
   470           
   455           // send body
   471           // send body
   456           while ( $blk = @fread($fh, 768000) )
   472           while ( $blk = @fread($fh, 768000) )
   457           {
   473           {
   458             socket_write($socket, $blk);
   474             @socket_write($socket, $blk);
   459           }
   475           }
   460           fclose($fh);
   476           fclose($fh);
   461           return true;
   477           return true;
   462         }
   478         }
   463         else
   479         else
   500         
   516         
   501         // write headers
   517         // write headers
   502         $this->send_client_headers($socket, $this->response_code, $this->content_type, $headers);
   518         $this->send_client_headers($socket, $this->response_code, $this->content_type, $headers);
   503         
   519         
   504         // write body
   520         // write body
   505         socket_write($socket, $output);
   521         @socket_write($socket, $output);
   506         
   522         
   507         break;
   523         break;
   508     }
   524     }
   509   }
   525   }
   510   
   526   
   552     <hr />
   568     <hr />
   553     <address>Served by $this->server_string</address>
   569     <address>Served by $this->server_string</address>
   554   </body>
   570   </body>
   555 </html>
   571 </html>
   556 EOF;
   572 EOF;
   557     socket_write($socket, $html);
   573     @socket_write($socket, $html);
   558     @socket_close($socket);
   574     @socket_close($socket);
   559   }
   575   }
   560   
   576   
   561   /**
   577   /**
   562    * Adds a new handler
   578    * Adds a new handler