/**
  * Runs the builder, and returns the web response.
  */
 public function run($options, $request)
 {
     // Run the preprocessor, if any.
     if ($this->preprocessor != null) {
         $this->log->debug("Preprocessing '{$request->getUri()}'...");
         $func = $this->preprocessor;
         $func($request);
         $this->log->debug("Done preprocessing '{$request->getUri()}'.");
     }
     // See if the request maps to an existing file on our VFS.
     $handled = false;
     $documentPath = $this->vfs->getDocumentPath($request->getUriPath());
     if ($documentPath != null) {
         if ($request->getMethod() == 'GET' and is_file($documentPath)) {
             // Serve existing file...
             $this->log->debug('Serving static file: ' . $documentPath);
             return $this->vfs->serveDocument($request, $documentPath);
         } else {
             if ($request->getMethod() == 'GET' and is_dir($documentPath)) {
                 $indexPath = $this->vfs->getIndexDocument($documentPath);
                 if ($indexPath != null) {
                     // Serve a directory's index file...
                     $this->log->debug('Serving static file: ' . $indexPath);
                     return $this->vfs->serveDocument($request, $indexPath);
                 } else {
                     if ($options['list_directories'] and ($options['list_root_directory'] or $request->getUriPath() != '/')) {
                         // Serve the directory's contents...
                         $this->log->debug('Serving directory: ' . $documentPath);
                         return $this->vfs->serveDirectory($request, $documentPath);
                     }
                 }
             }
         }
     }
     // No static file/directory matched. Run the registered handlers.
     if (isset($this->handlers[$request->getMethod()])) {
         // Run the request handlers.
         foreach ($this->handlers[$request->getMethod()] as $handler) {
             if ($handler->_isMatch($request->getUri())) {
                 $response = new StupidHttp_WebResponse();
                 $context = new StupidHttp_HandlerContext($request, $response, $this->log);
                 $this->log->debug('--- Starting request handler for ' . $request->getUri() . ' ---');
                 $this->log->_startBuffering();
                 ob_start();
                 try {
                     $handler->_run($context);
                 } catch (Exception $e) {
                     ob_end_clean();
                     $this->log->_endBuffering();
                     $this->log->debug('--- Finished request handler ---');
                     $this->log->error("Handler error for URI '" . $request->getUri() . "': " . strval($e));
                     return new StupidHttp_WebResponse(500);
                 }
                 $body = ob_get_clean();
                 $response->setBody($body);
                 $this->log->_endBuffering();
                 $this->log->debug('--- Finished request handler ---');
                 return $response;
             }
         }
     }
     if ($request->getMethod() == 'GET') {
         return new StupidHttp_WebResponse(404);
         // Not found.
     } else {
         return new StupidHttp_WebResponse(501);
         // Method not implemented.
     }
 }
Beispiel #2
0
 protected function processRequest(&$requestInfo)
 {
     // Create the request object.
     $requestInfo['profiling']['process.start'] = microtime(true);
     $request = new StupidHttp_WebRequest($this->buildServerInfo(), $requestInfo['headers'], $requestInfo['body']);
     $requestInfo['request'] = $request;
     // Process the request, get the response.
     try {
         $processor = new StupidHttp_ResponseBuilder($this->vfs, $this->preprocessor, $this->requestHandlers, $this->log);
         $response = $processor->run($this->options, $request);
     } catch (StupidHttp_WebException $e) {
         $this->log->error('Error processing request:');
         $this->log->error($e->getCode() . ': ' . $e->getMessage());
         if ($e->getCode() != 0) {
             $response = new StupidHttp_WebResponse($e->getCode());
         } else {
             $response = new StupidHttp_WebResponse(500);
         }
     } catch (Exception $e) {
         $this->log->error('Error processing request:');
         $this->log->error($e->getCode() . ': ' . $e->getMessage());
         $response = new StupidHttp_WebResponse(500);
     }
     $requestInfo['response'] = $response;
     $requestInfo['profiling']['process.end'] = microtime(true);
     // Figure out whether to close the connection with the client.
     $closeSocket = true;
     if ($this->options['keep_alive']) {
         switch ($request->getVersion()) {
             case 'HTTP/1.0':
             default:
                 // Always close, unless asked to keep alive.
                 $closeSocket = $request->getHeader('Connection') != 'keep-alive';
                 break;
             case 'HTTP/1.1':
                 // Always keep alive, unless asked to close.
                 $closeSocket = $request->getHeader('Connection') == 'close';
                 break;
         }
     } else {
         $closeSocket = true;
     }
     $requestInfo['close_socket'] = $closeSocket;
     // Adjust the headers.
     if ($closeSocket) {
         $response->setHeader('Connection', 'close');
     } else {
         $response->setHeader('Connection', 'keep-alive');
     }
     if ($response->getHeader('Content-Length') == null) {
         if ($response->getBody() != null) {
             $response->setHeader('Content-Length', strlen($response->getBody()));
         } else {
             $response->setHeader('Content-Length', 0);
         }
     }
     return $response;
 }