Inheritance: use trait Psr7Middlewares\Utils\NegotiateTrait, use trait Psr7Middlewares\Utils\AttributeTrait
Example #1
0
 /**
  * Execute the middleware.
  *
  * @param ServerRequestInterface $request
  * @param ResponseInterface      $response
  * @param callable               $next
  *
  * @return ResponseInterface
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
 {
     //If basePath does not match
     if (!$this->testBasePath($request->getUri()->getPath())) {
         return $next($request, $response);
     }
     //If the method is not allowed
     if ($request->getMethod() !== 'GET') {
         return $response->withStatus(405);
     }
     $body = Middleware::createStream();
     $file = $this->getFilename($request);
     //If the file does not exists, check if is gzipped
     if (!is_file($file)) {
         $file .= '.gz';
         if (EncodingNegotiator::getEncoding($request) !== 'gzip' || !is_file($file)) {
             return $response->withStatus(404);
         }
         $response = $response->withHeader('Content-Encoding', 'gzip');
     }
     self::readFile($file, $body);
     $response = $response->withBody($body);
     //Handle range header
     $response = $this->range($request, $response);
     return $next($request, $response);
 }
Example #2
0
 /**
  * Execute the middleware.
  *
  * @param ServerRequestInterface $request
  * @param ResponseInterface      $response
  * @param callable               $next
  *
  * @return ResponseInterface
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
 {
     if (!Middleware::hasAttribute($request, EncodingNegotiator::KEY)) {
         throw new RuntimeException('Gzip middleware needs EncodingNegotiator executed before');
     }
     $resolver = $this->resolver ?: new Transformers\Encoder();
     $transformer = $resolver->resolve(EncodingNegotiator::getEncoding($request));
     if ($transformer) {
         $response = $transformer($response);
     }
     return $next($request, $response);
 }
Example #3
0
 /**
  * Execute the middleware.
  *
  * @param ServerRequestInterface $request
  * @param ResponseInterface      $response
  * @param callable               $next
  *
  * @return ResponseInterface
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
 {
     if (!self::hasAttribute($request, EncodingNegotiator::KEY)) {
         throw new RuntimeException('Gzip middleware needs EncodingNegotiator executed before');
     }
     $response = $next($request, $response);
     $resolver = $this->resolver ?: new Transformers\Encoder();
     $encoding = EncodingNegotiator::getEncoding($request);
     $transformer = $resolver->resolve($encoding);
     if ($transformer) {
         $body = $response->getBody();
         return $response->withHeader('Content-Encoding', $encoding)->withBody($transformer($body, self::createStream($body)));
     }
     return $response;
 }
 /**
  * Execute the middleware.
  *
  * @param ServerRequestInterface $request
  * @param ResponseInterface      $response
  * @param callable               $next
  *
  * @return ResponseInterface
  */
 public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
 {
     //If the method is not allowed
     if ($request->getMethod() !== 'GET') {
         if ($this->continueOnError) {
             return $next($request, $response);
         }
         return $response->withStatus(405);
     }
     $file = $this->getFilename($request);
     //If the file does not exists, check if is gzipped
     if (!is_file($file)) {
         $file .= '.gz';
         if (EncodingNegotiator::getEncoding($request) !== 'gzip' || !is_file($file)) {
             if ($this->continueOnError) {
                 return $next($request, $response);
             }
             return $response->withStatus(404);
         }
         $response = $response->withHeader('Content-Encoding', 'gzip');
     }
     //Handle range header
     return $this->range($request, $response->withBody(self::createStream($file, 'r')));
 }