Exemplo n.º 1
0
 public function test_run()
 {
     $result = true;
     try {
         ob_start();
         \veneer\app::run();
         $output = ob_get_clean();
     } catch (\Exception $e) {
         $result = false;
     }
     $this->assertTrue($result);
 }
Exemplo n.º 2
0
 /**
  * Main server method. This will loop as long as there is a master server
  * socket present and handle the flow of incoming requests.
  *
  * @param integer $max_request_size  The maximum size (in bytes) of a request
  * @return bool
  */
 public function server($max_request_size = 4096)
 {
     $r = array($this->socket);
     while (socket_select($r, $w = NULL, $e = NULL, $this->timeout) !== false) {
         $client = socket_accept($this->socket);
         /**
          * Since this is HTTP over sockets, you can't just assume that all of the
          * data will be sent in one packet and gathered with one recv(). Here we
          * parse the HTTP request according to RFC2616 (HTTP Message / Hypertext
          * Transfer Protocol -- HTTP/1.1) to read chunked messages and length
          * headers to read continuously until all parts of the HTTP message have
          * been received.
          */
         $complete = false;
         $buf = $input = '';
         while (!$complete) {
             $buf = socket_read($client, (int) $max_request_size);
             $input .= $buf;
             $request = \veneer\http\request::from_message($input);
             if (!$request) {
                 continue;
             }
             if (array_key_exists('content-length', $request['headers'])) {
                 $complete = strlen($request['body']) == $request['headers']['content-length'];
             } else {
                 $complete = true;
             }
         }
         socket_getsockname($client, $request['http_host'], $request['server_port']);
         socket_getpeername($client, $request['remote_addr'], $request['remote_port']);
         self::set_environment($request);
         ob_start();
         \veneer\app::run();
         $response = ob_get_clean();
         socket_write($client, $response);
         $status = preg_replace('~^[^\\s]+\\s~', '', array_shift(explode("\n", $response)));
         self::debug("[{$_SERVER['REMOTE_ADDR']}] " . "{$_SERVER['REQUEST_METHOD']} " . "{$_SERVER['REQUEST_URI']} - {$status}");
         /**
          * Close the socket and add the master server socket back into the pool. The
          * socket_select() function modifies data passed to it so the master server
          * socket needs to be re-added.
          */
         socket_close($client);
         $r = array($this->socket);
         self::reset();
     }
 }