/** * Send the status * * @param ResponseInterface $response The response object * * @return void */ protected function sendStatus(ResponseInterface $response) { $version = $response->getProtocolVersion(); $status = $response->getStatusCode(); $phrase = $response->getReasonPhrase(); header("HTTP/{$version} {$status} {$phrase}"); }
/** * 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()); }
/** * {@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); }
/** * @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); }
/** * 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); } } }
/** * Emit the status line. * * Emits the status line using the protocol version and status code from * the response; if a reason phrase is availble, it, too, is emitted. * * @param ResponseInterface $response */ private function emitStatusLine(ResponseInterface $response) { $reasonPhrase = $response->getReasonPhrase(); header(sprintf( 'HTTP/%s %d%s', $response->getProtocolVersion(), $response->getStatusCode(), ($reasonPhrase ? ' ' . $reasonPhrase : '') )); }
private function getStatusLine(ResponseInterface $response) { $protocol = $response->getProtocolVersion(); $statusCode = $response->getStatusCode(); $reasonPhrase = $response->getReasonPhrase(); if ($reasonPhrase) { return "HTTP/{$protocol} {$statusCode} {$reasonPhrase}"; } else { return "HTTP/{$protocol} {$statusCode}"; } }
/** * 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()); }
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(); } }
/** * 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); }
/** * Takes a PSR-7 Response and outputs all headers and body. This should be the * very last thing done in request processing. */ function renderResponse(ResponseInterface $response) { // Send HTTP code header(sprintf("HTTP/%s %s %s", $response->getProtocolVersion(), $response->getStatusCode(), $response->getReasonPhrase())); // Additional headers foreach ($response->getHeaders() as $key => $values) { foreach ($values as $value) { header(sprintf("%s: %s", $key, $value), false); } } // And then the body echo $response->getBody(); }
public static function send(ResponseInterface $response) { $statusCode = $response->getStatusCode(); $reasonPhrase = $response->getReasonPhrase(); $protocolVersion = $response->getProtocolVersion(); header("HTTP/{$protocolVersion} {$statusCode} {$reasonPhrase}"); foreach ($response->getHeaders() as $name => $values) { foreach ($values as $value) { header(sprintf('%s: %s', $name, $value), false); } } echo $response->getBody(); }
public function sendHeaders(ResponseInterface $response) { header(sprintf('HTTP/%s %s %s', $response->getProtocolVersion(), $response->getStatusCode(), $response->getReasonPhrase()), true, $response->getStatusCode()); 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; } } $time = round(microtime(true) - $_SERVER['REQUEST_TIME_FLOAT'], 3); header("X-Processing-Time: {$time} seconds"); }
/** * Sends the response back to the client * * @param ResponseInterface $response */ public function sendResponse(ResponseInterface $response) { if (!headers_sent()) { $header = sprintf('HTTP/%s %s %s', $response->getProtocolVersion(), $response->getStatusCode(), $response->getReasonPhrase()); header($header); foreach ($response->getHeaders() as $name => $values) { foreach ($values as $value) { $header = sprintf('%s: %s', $name, $value); header($header, false); } } } echo (string) $response->getBody(); }
function send(ResponseInterface $response) { if (!headers_sent()) { // status header(sprintf('HTTP/%s %s %s', $response->getProtocolVersion(), $response->getStatusCode(), $response->getReasonPhrase()), true, $response->getStatusCode()); // headers foreach ($response->getHeaders() as $header => $values) { foreach ($values as $value) { header($header . ': ' . $value, false, $response->getStatusCode()); } } } echo $response->getBody(); }
/** * @param \Psr\Http\Message\ResponseInterface $response */ public function send(ResponseInterface $response) { $statusCode = $response->getStatusCode(); $reasonPhrase = $response->getReasonPhrase(); $protocolVersion = $response->getProtocolVersion(); header("HTTP/{$protocolVersion} {$statusCode} {$reasonPhrase}"); foreach ($response->getHeaders() as $name => $values) { if (strtolower($name) === 'set-cookie') { foreach ($values as $cookie) { header(sprintf('Set-Cookie: %s', $cookie), false); } break; } header(sprintf('%s: %s', $name, $response->getHeaderLine($name))); } echo $response->getBody(); }
/** * @param \Psr\Http\Message\ResponseInterface $response * @return string */ public function parseToSocketBody(ResponseInterface $response) { $lines = []; $statusCode = $response->getStatusCode(); $reasonPhrase = $response->getReasonPhrase(); $protocolVersion = $response->getProtocolVersion(); $lines[] = "HTTP/{$protocolVersion} {$statusCode} {$reasonPhrase}"; foreach ($response->getHeaders() as $name => $values) { if (strtolower($name) === 'set-cookie') { foreach ($values as $cookie) { $lines[] = sprintf('Set-Cookie: %s', $cookie); } break; } $lines[] = sprintf('%s: %s', $name, $response->getHeaderLine($name)); } return implode(" \r\n", $lines) . "\r\n\r\n" . $response->getBody()->__toString(); }
/** * 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)); } // If the response code was not changed by legacy code (still is 200) // then allow the PSR-7 response object to explicitly set it. // Otherwise let legacy code take precedence. // This code path can be deprecated once we expose the response object to third party code if (http_response_code() === 200) { header('HTTP/' . $this->response->getProtocolVersion() . ' ' . $this->response->getStatusCode() . ' ' . $this->response->getReasonPhrase()); } } echo $this->response->getBody()->__toString(); } return $this; }
/** * Get context fields to add to the time-line entry. * * @param \Psr\Http\Message\RequestInterface $request * @param \Psr\Http\Message\ResponseInterface $response * * @return array */ protected function getParameters(RequestInterface $request, ResponseInterface $response = null) { $params = []; $result = ''; $keys = array_intersect($this->context, $this->availableParameters); foreach ($keys as $key) { switch ($key) { case 'method': $result = $request->getMethod(); break; case 'url': $result = $request->getUri()->__toString(); break; case 'request_version': $result = $request->getProtocolVersion(); break; case 'response_version': $result = $response ? $response->getProtocolVersion() : 'NULL'; break; case 'host': $result = $request->getUri()->getHost(); break; case 'hostname': $result = gethostname(); break; case 'status_code': $result = $response ? $response->getStatusCode() : 'NULL'; break; case 'phrase': $result = $response ? $response->getReasonPhrase() : 'NULL'; break; } $params[$key] = (string) $result ?: ''; } return $params; }
/** * Proxy to PsrResponseInterface::getProtocolVersion() * * {@inheritdoc} */ public function getProtocolVersion() { return $this->psrResponse->getProtocolVersion(); }
/** * Send the response the client * * @param ResponseInterface $response */ public function respond(ResponseInterface $response) { // Send response if (!headers_sent()) { // Status header(sprintf('HTTP/%s %s %s', $response->getProtocolVersion(), $response->getStatusCode(), $response->getReasonPhrase())); // Headers foreach ($response->getHeaders() as $name => $values) { foreach ($values as $value) { header(sprintf('%s: %s', $name, $value), false); } } } // Body if (!$this->isEmptyResponse($response)) { $body = $response->getBody(); if ($body->isSeekable()) { $body->rewind(); } $settings = $this->container->get('settings'); $chunkSize = $settings['responseChunkSize']; $contentLength = $response->getHeaderLine('Content-Length'); if (!$contentLength) { $contentLength = $body->getSize(); } if (isset($contentLength)) { $amountToRead = $contentLength; while ($amountToRead > 0 && !$body->eof()) { $data = $body->read(min($chunkSize, $amountToRead)); echo $data; $amountToRead -= strlen($data); if (connection_status() != CONNECTION_NORMAL) { break; } } } else { while (!$body->eof()) { echo $body->read($chunkSize); if (connection_status() != CONNECTION_NORMAL) { break; } } } } }
public function yaml(ResponseInterface $response) { return new YamlResponse($response->getStatusCode(), $response->getHeaders(), $response->getBody(), $response->getProtocolVersion(), $response->getReasonPhrase()); }
/** * Send the response the client * * @param ResponseInterface $response */ public function respond(ResponseInterface $response) { static $responded = false; if (!$responded) { // Send response if (!headers_sent()) { // Status header(sprintf('HTTP/%s %s %s', $response->getProtocolVersion(), $response->getStatusCode(), $response->getReasonPhrase())); // Headers foreach ($response->getHeaders() as $name => $values) { foreach ($values as $value) { header(sprintf('%s: %s', $name, $value), false); } } } // Body if (!$this->isEmptyResponse($response)) { $body = $response->getBody(); if ($body->isSeekable()) { $body->rewind(); } $settings = $this->container->get('settings'); while (!$body->eof()) { echo $body->read($settings['responseChunkSize']); if (connection_status() != CONNECTION_NORMAL) { break; } } } $responded = true; } }
/** * Decodes a response into a standard formatted array. (See * ClientInterface for documentation). * * @param ResponseInterface $response Guzzle response * * @return array Response as array */ private function decodeResponse(ResponseInterface $response) { return array('headers' => $response->getHeaders(), 'status' => $response->getStatusCode(), 'reason' => $response->getReasonPhrase(), 'version' => $response->getProtocolVersion(), 'body' => (string) $response->getBody()); }
/** * Send the response the client * * @param ResponseInterface $response */ public function respond(ResponseInterface $response) { // Send response if (!headers_sent()) { // Status header(sprintf('HTTP/%s %s %s', $response->getProtocolVersion(), $response->getStatusCode(), $response->getReasonPhrase())); // Headers foreach ($response->getHeaders() as $name => $values) { foreach ($values as $value) { header(sprintf('%s: %s', $name, $value), false); } } } // Body if (!$this->isEmptyResponse($response)) { $body = $response->getBody(); if ($body->isSeekable()) { $body->rewind(); } $settings = $this->container->get('settings'); $chunkSize = $settings['responseChunkSize']; $contentLength = $response->getHeaderLine('Content-Length'); if (!$contentLength) { $contentLength = $body->getSize(); } $totalChunks = ceil($contentLength / $chunkSize); $lastChunkSize = $contentLength % $chunkSize; $currentChunk = 0; while (!$body->eof() && $currentChunk < $totalChunks) { if (++$currentChunk == $totalChunks && $lastChunkSize > 0) { $chunkSize = $lastChunkSize; } echo $body->read($chunkSize); if (connection_status() != CONNECTION_NORMAL) { break; } } } }
/** * Send response headers * * Sends the response status/reason, followed by all headers; * header names are filtered to be word-cased. * * @param ResponseInterface $response */ private function sendHeaders(ResponseInterface $response) { if ($response->getReasonPhrase()) { header(sprintf('HTTP/%s %d %s', $response->getProtocolVersion(), $response->getStatusCode(), $response->getReasonPhrase())); } else { header(sprintf('HTTP/%s %d', $response->getProtocolVersion(), $response->getStatusCode())); } 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; } } }
/** * {@inheritDoc} */ public function getProtocolVersion() { return $this->decorated->getProtocolVersion(); }
/** * Set the status line. * * @param ResponseInterface $response The response. */ protected function serveStatus(ResponseInterface $response) { $protocol = $response->getProtocolVersion(); $status = $response->getStatusCode(); $reason = $response->getReasonPhrase(); if ($reason != '') { $reason = ' ' . $reason; } header('HTTP/' . $protocol . ' ' . $status . $reason); }
/** * Retrieves the HTTP protocol version as a string. * * The string MUST contain only the HTTP version number (e.g., "1.1", "1.0"). * * @return string HTTP protocol version. */ function getProtocolVersion() { return $this->response->getProtocolVersion(); }
public function save(RequestInterface $request, ResponseInterface $response) { $data = ['status' => $response->getStatusCode(), 'headers' => $response->getHeaders(), 'body' => (string) $response->getBody(), 'version' => $response->getProtocolVersion(), 'reason' => $response->getReasonPhrase()]; $this->cache->save($this->getKey($request), $data, $this->ttl); }