public function handle(RequestInterface $request, ResponseInterface $response, FilterChainInterface $filterChain) { $accept = $request->getHeader('Accept'); if (stripos($accept, 'text/html') !== false && is_file($this->file)) { $response->setHeader('Content-Type', 'text/html'); $response->getBody()->write(file_get_contents($this->file)); } else { $filterChain->handle($request, $response); } }
public function handle(RequestInterface $request, ResponseInterface $response, FilterChainInterface $filterChain) { if ($request->hasHeader('Accept-Encoding')) { $acceptEncoding = $request->getHeader('Accept-Encoding'); if (strpos($acceptEncoding, 'gzip') !== false) { // the sender will compress the response if the content encoding // header is available $response->setHeader('Content-Encoding', 'gzip'); } } $filterChain->handle($request, $response); }
/** * Writes the $record with the writer $writerType or depending on the get * parameter format or of the mime type of the Accept header * * @param \PSX\Data\RecordInterface $record * @param string $writerType * @return void */ private function setResponse(RecordInterface $record, $writerType = null) { // find best writer type $writer = $this->getResponseWriter($writerType); // set writer specific settings $this->configureWriter($writer); // write the response $response = $writer->write($record); // the response may have multiple presentations based on the Accept // header field if (!$this->response->hasHeader('Vary')) { $this->response->setHeader('Vary', 'Accept'); } // set content type header if not available if (!$this->response->hasHeader('Content-Type')) { $contentType = $writer->getContentType(); if ($contentType !== null) { $this->response->setHeader('Content-Type', $contentType); } } // for iframe file uploads we need an text/html content type header even // if we want serve json content. If all browsers support the FormData // api we can send file uploads per ajax but for now we use this hack. // Note do not rely on this param it will be removed as soon as possible if (isset($_GET['htmlMime'])) { $this->response->setHeader('Content-Type', 'text/html'); } $this->response->getBody()->write($response); }
public function handle(RequestInterface $request, ResponseInterface $response, FilterChainInterface $filterChain) { $cacheControl = array(); if ($this->flags & self::TYPE_PUBLIC) { $cacheControl[] = 'public'; } if ($this->flags & self::TYPE_PRIVATE) { $cacheControl[] = 'private'; } if ($this->flags & self::NO_CACHE) { $cacheControl[] = 'no-cache'; } if ($this->flags & self::NO_STORE) { $cacheControl[] = 'no-store'; } if ($this->flags & self::NO_TRANSFORM) { $cacheControl[] = 'no-transform'; } if ($this->flags & self::MUST_REVALIDATE) { $cacheControl[] = 'must-revalidate'; } if ($this->flags & self::PROXY_REVALIDATE) { $cacheControl[] = 'proxy-revalidate'; } if ($this->maxAge !== null) { $cacheControl[] = 'max-age=' . intval($this->maxAge); } if ($this->sMaxAge !== null) { $cacheControl[] = 's-maxage=' . intval($this->sMaxAge); } if (!empty($cacheControl)) { $response->setHeader('Cache-Control', implode(', ', $cacheControl)); } if ($this->expires !== null) { $response->setHeader('Expires', $this->expires->format(DateTime::HTTP)); } $filterChain->handle($request, $response); }
/** * @param \PSX\Http\ResponseInterface $response * @return string */ public static function buildStatusLine(ResponseInterface $response) { $protocol = $response->getProtocolVersion(); $code = $response->getStatusCode(); $phrase = $response->getReasonPhrase(); if (empty($code)) { throw new Exception('Status code not set'); } $protocol = !empty($protocol) ? $protocol : 'HTTP/1.1'; if (empty($phrase) && isset(Http::$codes[$code])) { $phrase = Http::$codes[$code]; } if (empty($phrase)) { throw new Exception('No reason phrase provided'); } return $protocol . ' ' . $code . ' ' . $phrase; }
protected function sendContentChunked(ResponseInterface $response) { $body = $response->getBody(); $body->seek(0); while (!$body->eof()) { $chunk = $body->read($this->chunkSize); $len = strlen($chunk); if ($len > 0) { echo dechex($len) . "\r\n" . $chunk . "\r\n"; flush(); } } echo '0' . "\r\n" . "\r\n"; flush(); $body->close(); }
/** * Method which checks whether this is an valid response from an google * server */ protected function assertGoogleResponse(ResponseInterface $response) { $this->assertTrue($response->getStatusCode() >= 200 && $response->getStatusCode() < 400); // google server always response with an Server header $this->assertTrue($response->hasHeader('Server')); // we assume that the response should be more the 128 bytes $this->assertTrue(strlen((string) $response->getBody()) > 128); }
public static function createResponse(ResponseInterface $response) { return new PsrResponse($response->getBody(), $response->getStatusCode(), $response->getHeaders()); }
/** * Returns an array containing all headers which gets saved in the cache * * @param \PSX\Http\ResponseInterface $response * @return array */ protected function getCacheHeaders(ResponseInterface $response) { $headers = array('Last-Modified' => date(DateTime::HTTP)); if ($response->hasHeader('Content-Type')) { $headers['Content-Type'] = $response->getHeader('Content-Type'); } return $headers; }
public function send(ResponseInterface $response) { $this->response = (string) $response->getBody(); }
protected function handleStatusCodeException(StatusCode\StatusCodeException $e, ResponseInterface $response) { $response->setStatus($e->getStatusCode()); if ($e instanceof StatusCode\MethodNotAllowedException) { $allowedMethods = $e->getAllowedMethods(); if (!empty($allowedMethods)) { $response->setHeader('Allow', implode(', ', $allowedMethods)); } } elseif ($e instanceof StatusCode\UnauthorizedException) { $type = $e->getType(); $parameters = $e->getParameters(); if (!empty($type)) { if (!empty($parameters)) { $response->setHeader('WWW-Authenticate', $type . ' ' . Authentication::encodeParameters($parameters)); } else { $response->setHeader('WWW-Authenticate', $type); } } } }