Example #1
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;
 }