Exemplo n.º 1
0
 public function testResponse()
 {
     $response = new Response();
     $response->setHeader('Content-Type', 'text/plain')->setBody('hello')->setCode(200);
     $this->assertEquals('text/plain', $response->getHeader('Content-Type'));
     $this->assertEquals('hello', $response->getBody());
     $this->assertEquals(200, $response->getCode());
     $this->assertEquals("HTTP/1.1 200 OK\nContent-Type: text/plain\nContent-Length: 5\n\nhello", (string) $response);
 }
Exemplo n.º 2
0
 /**
  * @param        $output
  * @param string $errorOutput
  *
  * @return Response
  */
 protected function parseOutput($output, $errorOutput = '')
 {
     $response = new Response();
     list($header, $body) = explode("\r\n\r\n", $output, 2);
     $response->setBody($body);
     $response->setError($errorOutput);
     $header = new Text($header);
     $cookies = [];
     foreach ($header->lines() as $line) {
         list($name, $value) = explode(':', $line, 2);
         if ('Set-Cookie' == $name) {
             $cookies[] = trim($value);
         } else {
             $response->setHeader(trim($name), trim($value));
         }
     }
     if (count($cookies)) {
         $response->setHeader('Set-Cookie', $cookies);
     }
     return $response;
 }
Exemplo n.º 3
0
 /**
  * @param Request               $request
  * @param ProtectedStreamSocket $socket
  *
  * @throws ResourceNotFoundException
  *
  * @return Response
  */
 protected function handleRequest(Request $request, ProtectedStreamSocket $socket)
 {
     try {
         $file = $this->findFile($request);
         if ($processor = $this->findProcessor($file)) {
             $response = $processor->execute($file, $request, $this->env);
         } else {
             $response = new Response(file_get_contents($file));
             $response->setHeader('Content-Type', ContentType::getType($file->getExtension()));
         }
     } catch (ResourceNotFoundException $e) {
         $response = $this->handleError(404, $e);
     } catch (Exception $e) {
         $response = $this->handleError(500, $e);
     }
     $this->logger->log($response->getCode(), (string) $request);
     if ($error = $response->getError()) {
         $this->logger->log(-1, $error);
     }
     return $response;
 }