Пример #1
0
 protected function prepareFileStream(ResponseInterface $response)
 {
     if ($response->getBody() instanceof FileStream) {
         $fileName = $response->getBody()->getFileName();
         if (empty($fileName)) {
             $fileName = 'file';
         }
         $contentType = $response->getBody()->getContentType();
         if (empty($contentType)) {
             $contentType = 'application/octet-stream';
         }
         $response->setHeader('Content-Type', $contentType);
         $response->setHeader('Content-Disposition', 'attachment; filename="' . addcslashes($fileName, '"') . '"');
         $response->setHeader('Transfer-Encoding', 'chunked');
     }
 }
Пример #2
0
 /**
  * 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);
 }
Пример #3
0
 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);
     }
 }
Пример #4
0
 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);
 }
Пример #5
0
 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);
 }
Пример #6
0
 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);
             }
         }
     }
 }