예제 #1
0
 /**
  * Add response content
  *
  * @param mixed $content
  *
  * @return $this
  */
 public function addContent($content)
 {
     if (!$this->contentSend) {
         $this->dispatchHeaders();
         $this->contentSend = true;
     }
     $this->reactResponse->write($content);
     return $this;
 }
예제 #2
0
 public function testResponseBodyShouldBeChunkedCorrectly()
 {
     $conn = $this->getMock('React\\Socket\\ConnectionInterface');
     $conn->expects($this->at(4))->method('write')->with("5\r\nHello\r\n");
     $conn->expects($this->at(5))->method('write')->with("1\r\n \r\n");
     $conn->expects($this->at(6))->method('write')->with("6\r\nWorld\n\r\n");
     $conn->expects($this->at(7))->method('write')->with("0\r\n\r\n");
     $response = new Response($conn);
     $response->writeHead();
     $response->write('Hello');
     $response->write(' ');
     $response->write("World\n");
     $response->end();
 }
예제 #3
0
 /**
  * It performs the setup of a reactPHP response from a SlimpPHP response
  * object and finishes the communication
  *
  * @param \React\Http\Response $reactResp    ReactPHP native response object
  * @param boolean              $endRequest   If true, response flush will be finished
  *
  * @return void
  */
 public function setReactResponse(\React\Http\Response $reactResp, $endRequest = false)
 {
     $reactResp->writeHead($this->getStatusCode(), $this->getHeaders());
     $reactResp->write($this->getBody());
     if ($endRequest === true) {
         $reactResp->end();
     }
 }
예제 #4
0
 /**
  * It performs the setup of a reactPHP response from a SlimpPHP response
  * object and finishes the communication
  *
  * @param \React\Http\Response $reactResp    ReactPHP native response object
  * @param \Slim\Http\Response  $slimResponse SlimPHP native response object
  * @param boolean              $endRequest   If true, response flush will be finished
  *
  * @return void
  */
 static function setReactResponse(\React\Http\Response $reactResp, \Slim\Http\Response $slimResponse, $endRequest = false)
 {
     $headers = static::reduceHeaders($slimResponse->getHeaders());
     $reactResp->writeHead($slimResponse->getStatusCode(), $headers);
     $reactResp->write($slimResponse->getBody());
     if ($endRequest === true) {
         $reactResp->end();
     }
 }
예제 #5
0
 /**
  * Dispatch headers and buffered data to response
  * 
  * @param int $code [false]
  * @param string $data [null]
  * 
  * @return void
  */
 public function dispatch($code = false)
 {
     $code = $code ?: $this->getCode();
     $headers = $this->headers()->all();
     $this->original->writeHead($code, $headers);
     $data = $this->getBufferEnd();
     if (!empty($data)) {
         $this->original->write($data);
     }
 }
예제 #6
0
 /**
  * Sends content for the current web response.
  *
  * @param ReactResponse   $response
  * @param SymfonyResponse $sf_response
  */
 private function sendContent(ReactResponse $response, SymfonyResponse $sf_response)
 {
     if ($sf_response instanceof StreamedResponse || $sf_response instanceof BinaryFileResponse) {
         ob_start(function ($buffer) use($response) {
             $response->write($buffer);
         });
         $sf_response->sendContent();
         ob_get_clean();
         $response->end();
     } else {
         $response->end($sf_response->getContent());
     }
 }
 /**
  * @param ResponseInterface $response
  * @param ReactResponse     $reactResponse
  */
 public function reverseTransform(ResponseInterface $response, ReactResponse $reactResponse)
 {
     $body = $response->getBody();
     $body->rewind();
     $headers = array();
     foreach (array_keys($response->getHeaders()) as $name) {
         $headers[$name] = $response->getHeaderLine($name);
     }
     if (!isset($headers['Content-Length']) && $body->getSize() > 0) {
         $headers['Content-Length'] = $body->getSize();
     }
     $reactResponse->writeHead($response->getStatusCode(), $headers);
     while (false === $body->eof()) {
         $reactResponse->write($body->read($this->responseBuffer));
     }
     $reactResponse->end();
 }
예제 #8
0
 /**
  * Close the connexion
  */
 public function close()
 {
     $this->sendHeaders();
     $this->httpResponse->write($this->data);
     $this->httpResponse->close();
 }
예제 #9
0
 public function handleRequest(Request $request, Response $response)
 {
     $this->request = $request;
     $this->response = $response;
     $dispatcher = new Dispatcher($this->router->getData());
     try {
         $content = $dispatcher->dispatch($request->getMethod(), $request->getPath());
         $response->writeHead(200, ['Content-Type' => $this->responseContentType]);
         $response->write($content);
     } catch (HttpRouteNotFoundException $e) {
         $response->writeHead(404, ['Content-Type' => 'text/plain']);
     } catch (HttpMethodNotAllowedException $e) {
         $response->writeHead(403, ['Content-Type' => 'text/plain']);
     } catch (Exception $e) {
         $this->getLogger()->error($e->getMessage());
         $response->writeHead(500, ['Content-Type' => 'text/plain']);
     }
     $response->end();
 }
예제 #10
0
 public function write($data)
 {
     $this->bytesSent += strlen($data);
     parent::write($data);
 }
예제 #11
0
 /**
  * Manager Internal server error
  *
  * @param ReactResponse $response
  * @param Exception|Throwable $error
  */
 protected function fatalError(ReactResponse $response, $error)
 {
     $response->writeHead(500);
     $response->write(sprintf("Internal server error : %s", $error->getMessage()));
     $response->end();
 }