private function sendHeaders()
 {
     foreach ($this->response->getHeaders() as $name => $values) {
         foreach ($values as $value) {
             header($name . ': ' . $value);
         }
     }
 }
Esempio n. 2
0
 /**
  * @param ResponseInterface $response
  *
  * @return ApiResult
  */
 protected function _getResult($response)
 {
     if (!$response instanceof ResponseInterface) {
         throw new \InvalidArgumentException("{$response} should be an instance of ResponseInterface");
     }
     $result = new ApiResult();
     $result->setStatusCode($response->getStatusCode());
     $callId = $response->getHeader('X-Call-Id');
     if (!empty($callId)) {
         $result->setCallId($callId);
     }
     $decoded = json_decode((string) $response->getBody());
     if (isset($decoded->meta) && isset($decoded->data) && isset($decoded->meta->code) && $decoded->meta->code == $response->getStatusCode()) {
         $meta = $decoded->meta;
         $data = $decoded->data;
         if (isset($meta->message)) {
             $result->setStatusMessage($meta->message);
         }
         $result->setContent(json_encode($data));
     } else {
         $result->setContent((string) $response->getBody());
     }
     $result->setHeaders($response->getHeaders());
     return $result;
 }
Esempio n. 3
0
 /**
  * @param ResponseInterface $response
  */
 public function send(ResponseInterface $response)
 {
     /**
      * build response header
      */
     foreach ($response->getHeaders() as $key => $value) {
         $filter_header = function ($header) {
             $filtered = str_replace('-', ' ', $header);
             $filtered = ucwords($filtered);
             return str_replace(' ', '-', $filtered);
         };
         $name = $filter_header($key);
         foreach ($value as $v) {
             $this->app['SwooleResponder']->header($name, $v);
         }
     }
     /**
      * compress content
      */
     if (!empty($this->app['Config']['server.gzip'])) {
         $this->app['SwooleResponder']->gzip($this->app['Config']['server.gzip']);
     }
     $this->app['SwooleResponder']->status($response->getStatusCode());
     $this->app['SwooleResponder']->header('Server', 'vinnige-app-server');
     $this->app['SwooleResponder']->end((string) $response->getBody());
 }
 /**
  * Stops the profiling.
  * @param ResponseInterface $response
  */
 public function leave(ResponseInterface $response = null)
 {
     $this->ends = ['wt' => microtime(true), 'mu' => memory_get_usage(), 'pmu' => memory_get_peak_usage()];
     if ($response) {
         $this->response = ['headers' => $response->getHeaders(), 'statusCode' => $response->getStatusCode(), 'body' => (string) $response->getBody()];
     }
 }
Esempio n. 5
0
 /**
  * @param ResponseInterface $response
  * @param string $type
  */
 protected function logResponse(ResponseInterface $response, $type = 'http')
 {
     $args = [ucfirst($type), $response->getStatusCode(), $response->getReasonPhrase()];
     /** @noinspection PrintfScanfArgumentsInspection */
     $this->log(sprintf(' <== (%s) %s %s', ...$args));
     $headers = $this->removeTokenFromLogs($response->getHeaders());
     $this->log('  Response headers: ' . json_encode($headers), Logger::DEBUG);
 }
 /**
  * Convert the PSR-7 headers to string
  *
  * @param ResponseInterface $psr7Response
  * @return string
  */
 private static function psr7HeadersToString(ResponseInterface $psr7Response)
 {
     $headers = '';
     foreach ($psr7Response->getHeaders() as $name => $value) {
         $headers .= $name . ": " . implode(", ", $value) . "\r\n";
     }
     return $headers;
 }
 public function formatResponse(ResponseInterface $response)
 {
     $format = ['code' => $response->getStatusCode(), 'headers' => $response->getHeaders(), 'body' => (string) $response->getBody()];
     if ($this->markerHeader) {
         $format['headers']['X-Guzzle-Stereo'] = true;
     }
     return $format;
 }
 /**
  * {@inheritdoc}
  */
 public function formatResponse(ResponseInterface $response)
 {
     $message = sprintf("HTTP/%s %s %s\n", $response->getProtocolVersion(), $response->getStatusCode(), $response->getReasonPhrase());
     foreach ($response->getHeaders() as $name => $values) {
         $message .= $name . ': ' . implode(', ', $values) . "\n";
     }
     return $this->addBody($response, $message);
 }
Esempio n. 9
0
 /**
  * Save data
  *
  * @author  Florian Preusner
  * @version 2.1
  * @since   2015-05
  *
  * @param   ResponseInterface $response
  */
 public function save(ResponseInterface $response)
 {
     $this->setStatusCode($response->getStatusCode());
     $this->setStatusPhrase($response->getReasonPhrase());
     $this->setBody($response->getBody()->__toString());
     $this->setHeaders($response->getHeaders());
     $this->setProtocolVersion($response->getProtocolVersion());
 }
Esempio n. 10
0
 /**
  * @param ResponseInterface $response
  */
 public function send(ResponseInterface $response)
 {
     header('HTTP/' . $response->getProtocolVersion() . ' ' . $response->getStatusCode() . ' ' . $response->getReasonPhrase());
     foreach ($response->getHeaders() as $header => $values) {
         header($header . ': ' . implode(', ', $values));
     }
     parent::send($response);
 }
Esempio n. 11
0
 /**
  * Returns the Guzzle array of headers as a string.
  *
  * @param ResponseInterface $response The Guzzle response.
  *
  * @return string
  */
 public function getHeadersAsString(ResponseInterface $response)
 {
     $headers = $response->getHeaders();
     $rawHeaders = [];
     foreach ($headers as $name => $values) {
         $rawHeaders[] = $name . ": " . implode(", ", $values);
     }
     return implode("\r\n", $rawHeaders);
 }
Esempio n. 12
0
 /**
  * {@inheritdoc}
  */
 public function createResponse(ResponseInterface $psrResponse)
 {
     $response = new Response($psrResponse->getBody()->__toString(), $psrResponse->getStatusCode(), $psrResponse->getHeaders());
     $response->setProtocolVersion($psrResponse->getProtocolVersion());
     foreach ($psrResponse->getHeader('Set-Cookie') as $cookie) {
         $response->headers->setCookie($this->createCookie($cookie));
     }
     return $response;
 }
Esempio n. 13
0
 private function emitHeaders(ResponseInterface $response)
 {
     foreach ($response->getHeaders() as $header => $values) {
         $name = str_replace(' ', '-', ucwords(str_replace('-', ' ', $header)));
         foreach ($values as $value) {
             $this->output->writeln(sprintf('%s: %s', $name, $value));
         }
     }
 }
Esempio n. 14
0
 /**
  * Response constructor.
  *
  * @param ResponseInterface $response
  */
 public function __construct($response)
 {
     if ($response instanceof ResponseInterface) {
         $this->statusCode = $response->getStatusCode();
         $this->body = $response->getBody();
         $this->headers = $response->getHeaders();
     }
     $this->decodedBody = json_decode($this->body, true);
 }
Esempio n. 15
0
 /**
  * Constructs a representable document
  *
  * @param ResponseInterface $response
  */
 public function __construct(ResponseInterface $response)
 {
     $headers = $response->getHeaders();
     // What better to get the mime type than what the Telegram servers already send us?
     $this->mime_type = $headers['Content-Type'][0];
     // Same with file length
     $this->file_size = (int) $headers['Content-Length'][0];
     $this->contents = (string) $response->getBody();
 }
Esempio n. 16
0
 /**
  * Send response to the client
  *
  * @return void
  */
 protected function sendResponse()
 {
     http_response_code($this->response->getStatusCode());
     foreach ($this->response->getHeaders() as $name => $values) {
         foreach ($values as $value) {
             header(sprintf('%s: %s', $name, $value), false);
         }
     }
     echo $this->response->getBody();
 }
Esempio n. 17
0
 /**
  * Emit response headers.
  *
  * Loops through each header, emitting each; if the header value
  * is an array with multiple values, ensures that each is sent
  * in such a way as to create aggregate headers (instead of replace
  * the previous).
  *
  * @param ResponseInterface $response
  */
 private function emitHeaders(ResponseInterface $response)
 {
     foreach ($response->getHeaders() as $header => $values) {
         $name = $this->filterHeader($header);
         foreach ($values as $value) {
             $header = sprintf("%s: %s\r\n", $name, $value);
             $this->conn->write($header);
         }
     }
 }
Esempio n. 18
0
 /**
  * Sends the response headers.
  *
  * @param \Psr\Http\Message\ResponseInterface $response Response instance
  */
 private function sendHeaders(ResponseInterface $response)
 {
     $statusCode = $response->getStatusCode();
     header(sprintf('HTTP/%s %d %s', $response->getProtocolVersion(), $statusCode, $response->getReasonPhrase()), true, $statusCode);
     foreach ($response->getHeaders() as $name => $values) {
         foreach ($values as $value) {
             header("{$name}: {$value}", false, $statusCode);
         }
     }
 }
Esempio n. 19
0
 public function send(ResponseInterface $response)
 {
     http_response_code($response->getStatusCode());
     foreach ($response->getHeaders() as $header => $values) {
         foreach ($values as $value) {
             header($header . ':' . $value, false);
         }
     }
     Psr7\copy_to_stream($response->getBody(), $this->environment->getStdOut());
 }
 /**
  * Asserts response match with the response schema.
  *
  * @param ResponseInterface $response
  * @param SchemaManager $schemaManager
  * @param string $path percent-encoded path used on the request.
  * @param string $httpMethod
  * @param string $message
  */
 public function assertResponseMatch(ResponseInterface $response, SchemaManager $schemaManager, $path, $httpMethod, $message = '')
 {
     $this->assertResponseMediaTypeMatch($response->getHeaderLine('Content-Type'), $schemaManager, $path, $httpMethod, $message);
     $httpCode = $response->getStatusCode();
     $headers = $response->getHeaders();
     foreach ($headers as &$value) {
         $value = implode(', ', $value);
     }
     $this->assertResponseHeadersMatch($headers, $schemaManager, $path, $httpMethod, $httpCode, $message);
     $this->assertResponseBodyMatch(json_decode($response->getBody()), $schemaManager, $path, $httpMethod, $httpCode, $message);
 }
Esempio n. 21
0
 public function sendResponse(ResponseInterface $response)
 {
     $headers = [];
     foreach ($response->getHeaders() as $name => $values) {
         $headers[$name] = implode(', ', $values);
     }
     $response->getBody()->rewind();
     $size = $response->getBody()->getSize();
     $this->writeHead($size > 0, $response->getStatusCode(), $response->getReasonPhrase(), $headers);
     $this->end($response->getBody()->getContents());
 }
Esempio n. 22
0
 /**
  * @param ServerRequestInterface $request
  * @param ResponseInterface $response
  * @param callable|null $next
  * @return ResponseInterface
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
 {
     $rowset = $request->getAttribute('Response-Body');
     $status = $response->getStatusCode();
     $headers = $response->getHeaders();
     $response = new JsonResponse($rowset, $status, $headers);
     if ($next) {
         return $next($request, $response);
     }
     return $response;
 }
Esempio n. 23
0
 public function parseMetadata(ResponseInterface $response) : array
 {
     $metadata = [];
     foreach ($response->getHeaders() as $header => $value) {
         $position = strpos($header, static::METADATA_PREFIX);
         if ($position === 0) {
             $metadata[ltrim($header, static::METADATA_PREFIX)] = $response->getHeader($header)[0];
         }
     }
     return $metadata;
 }
Esempio n. 24
0
 /**
  * Emit response headers.
  *
  * Loops through each header, emitting each; if the header value
  * is an array with multiple values, ensures that each is sent
  * in such a way as to create aggregate headers (instead of replace
  * the previous).
  *
  * @param ResponseInterface $response
  */
 private function emitHeaders(ResponseInterface $response)
 {
     foreach ($response->getHeaders() as $header => $values) {
         $name = $this->filterHeader($header);
         $first = true;
         foreach ($values as $value) {
             header(sprintf('%s: %s', $name, $value), $first);
             $first = false;
         }
     }
 }
 /**
  * Convert a PSR7 Response headers into a flat array
  *
  * @param PsrResponse $response The response to convert.
  * @return CakeResponse The equivalent CakePHP response
  */
 protected static function collapseHeaders(PsrResponse $response)
 {
     $out = [];
     foreach ($response->getHeaders() as $name => $value) {
         if (count($value) === 1) {
             $out[$name] = $value[0];
         } else {
             $out[$name] = $value;
         }
     }
     return $out;
 }
Esempio n. 26
0
 /**
  * Create a string representation of a response.
  *
  * @param ResponseInterface $response
  * @return string
  */
 public static function toString(ResponseInterface $response)
 {
     $reasonPhrase = $response->getReasonPhrase();
     $headers = self::serializeHeaders($response->getHeaders());
     $body = (string) $response->getBody();
     $format = 'HTTP/%s %d%s%s%s';
     if (!empty($headers)) {
         $headers = "\r\n" . $headers;
     }
     $headers .= "\r\n\r\n";
     return sprintf($format, $response->getProtocolVersion(), $response->getStatusCode(), $reasonPhrase ? ' ' . $reasonPhrase : '', $headers, $body);
 }
 /**
  * @param RequestInterface $request
  * @param ResponseInterface $response
  * @throws ValidatorResponseException
  */
 private function assertNoMissingHeaders(RequestInterface $request, ResponseInterface $response)
 {
     $method = $request->getMethod();
     $path = $request->getUri()->getPath();
     $statusCode = $response->getStatusCode();
     $schemaHeaders = $this->schemaHelper->getResponseHeaders($method, $path, $statusCode, true);
     $missingHeaders = array_diff_key($schemaHeaders, $response->getHeaders());
     if (count($missingHeaders) === 0) {
         return;
     }
     throw new ValidatorResponseException(sprintf('Missing response headers required by the schema for %s %s with status code %s: %s', strtoupper($method), $path, $statusCode, $this->getNamedParametersAsString($missingHeaders)));
 }
 /**
  * Process a ResponseInterface into an output.
  *
  * @param ResponseInterface $httpResponse
  */
 public function execute(ResponseInterface $httpResponse)
 {
     $headerFunction = $this->headerFunction;
     $bodyFunction = $this->bodyFunction;
     $headerFunction('HTTP/' . $httpResponse->getProtocolVersion() . ' ' . $httpResponse->getStatusCode() . ' ' . $httpResponse->getReasonPhrase());
     foreach ($httpResponse->getHeaders() as $name => $values) {
         foreach ($values as $value) {
             $headerFunction(sprintf('%s: %s', $name, $value));
         }
     }
     $bodyFunction($httpResponse->getBody());
 }
Esempio n. 29
0
 public static function sendResponse(Response $response)
 {
     header(sprintf('HTTP/%s %s %s', $response->getProtocolVersion(), $response->getStatusCode(), $response->getReasonPhrase()));
     foreach ($response->getHeaders() as $name => $values) {
         foreach ($values as $value) {
             header(sprintf('%s: %s', $name, $value), false);
         }
     }
     if (!in_array($response->getStatusCode(), [204, 205, 304])) {
         echo $response->getBody();
     }
 }
Esempio n. 30
0
 /**
  * Outputs content if there is a proper Response object.
  *
  * @return Bootstrap
  */
 protected function sendResponse()
 {
     if ($this->response instanceof \Psr\Http\Message\ResponseInterface) {
         if (!headers_sent()) {
             foreach ($this->response->getHeaders() as $name => $values) {
                 header($name . ': ' . implode(', ', $values), FALSE);
             }
         }
         echo $this->response->getBody()->__toString();
     }
     return $this;
 }