/** * 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); }
/** * Transform the image. * * @param ResponseInterface $response * @param string $transform * * @return ResponseInterface */ private function transform(ResponseInterface $response, $transform) { $image = Image::createFromString((string) $response->getBody()); $image->transform($transform); $body = Middleware::createStream(); $body->write($image->getString()); return $response->withBody($body)->withHeader('Content-Type', $image->getMimeType()); }
/** * Execute the middleware. * * @param RequestInterface $request * @param ResponseInterface $response * @param callable $next * * @return ResponseInterface */ public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next) { if ($request->getUri()->getPath() === '/robots.txt') { $body = Middleware::createStream(); $body->write("User-Agent: *\nDisallow: /"); return $response->withBody($body); } $response = $next($request, $response); return $response->withHeader(self::HEADER, 'noindex, nofollow, noarchive'); }
/** * Execute the middleware. * * @param RequestInterface $request * @param ResponseInterface $response * @param callable $next * * @return ResponseInterface */ public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next) { if ($request->getMethod() !== 'GET') { return $response->withStatus(405); } $file = $this->getFilename($request); if (!is_file($file)) { return $response->withStatus(404); } return $next($request, $response->withBody(Middleware::createStream($file))); }
/** * Insert content into all POST forms. * * @param ResponseInterface $response * @param callable $replace * * @return ResponseInterface */ private function insertIntoPostForms(ResponseInterface $response, callable $replace) { $html = (string) $response->getBody(); $html = preg_replace_callback('/(<form\\s[^>]*method=["\']?POST["\']?[^>]*>)/i', $replace, $html, -1, $count); if (!empty($count)) { $body = Middleware::createStream(); $body->write($html); return $response->withBody($body); } return $response; }
/** * Inject some code just before any tag. * * @param ResponseInterface $response * @param string $code * @param string $tag * * @return ResponseInterface */ private function inject(ResponseInterface $response, $code, $tag = 'body') { $html = (string) $response->getBody(); $pos = strripos($html, "</{$tag}>"); if ($pos === false) { $response->getBody()->write($code); return $response; } $body = Middleware::createStream(); $body->write(substr($html, 0, $pos) . $code . substr($html, $pos)); return $response->withBody($body); }
/** * Insert content into all POST forms. * * @param ResponseInterface $response * @param string $input * * @return ResponseInterface */ protected function insertIntoPostForms(ResponseInterface $response, $input) { $html = (string) $response->getBody(); $html = preg_replace_callback('/(<form\\s[^>]*method="?POST"?[^>]*>)/i', function ($match) use($input) { return $match[0] . $input; }, $html, -1, $count); if ($count) { $body = Middleware::createStream(); $body->write($html); return $response->withBody($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 (!Middleware::hasAttribute($request, FormatNegotiator::KEY)) { throw new RuntimeException('This middleware needs FormatNegotiator executed before'); } $renderer = $this->debugBar->getJavascriptRenderer(); //Is an asset? $path = $request->getUri()->getPath(); $renderPath = $renderer->getBaseUrl(); if (strpos($path, $renderPath) === 0) { $file = $renderer->getBasePath() . substr($path, strlen($renderPath)); if (file_exists($file)) { $body = Middleware::createStream(); $body->write(file_get_contents($file)); return $response->withBody($body); } } $response = $next($request, $response); //Fix the render baseUrl $renderPath = Utils\Helpers::joinPath(BasePath::getBasePath($request), $renderer->getBaseUrl()); $renderer->setBaseUrl($renderPath); $ajax = Utils\Helpers::isAjax($request); //Redirection response if (Utils\Helpers::isRedirect($response)) { if ($this->debugBar->isDataPersisted() || session_status() === PHP_SESSION_ACTIVE) { $this->debugBar->stackData(); } //Html response } elseif (FormatNegotiator::getFormat($request) === 'html') { if (!$ajax) { $response = $this->inject($response, $renderer->renderHead(), 'head'); } $response = $this->inject($response, $renderer->render(!$ajax), 'body'); //Ajax response } elseif ($ajax && $this->captureAjax) { $headers = $this->debugBar->getDataAsHeaders(); foreach ($headers as $name => $value) { $response = $response->withHeader($name, $value); } } 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) { list($headers_file, $stream_file) = $this->getCacheFilename($request); if (is_file($stream_file) && is_file($headers_file)) { $headers = (include $headers_file); if (static::cacheIsFresh($headers, $stream_file)) { $response = $response->withBody(Factory::createStream($stream_file)); foreach ($headers as $name => $header) { $response = $response->withHeader($name, $header); } return $response; } } $response = $next($request, $response); if (static::isCacheable($request, $response)) { static::writeStream($response->getBody(), $stream_file); file_put_contents($headers_file, '<?php return ' . var_export($response->getHeaders(), true) . ';'); } return $response; }
/** * Handle the payload. * * @param ServerRequestInterface $request * * @return ServerRequestInterface */ protected function handlePayload(ServerRequestInterface $request) { if ($request->getParsedBody() || !in_array($request->getMethod(), ['POST', 'PUT', 'DELETE'], true)) { return $request; } $contentType = trim($request->getHeaderLine('Content-Type')); //json if (stripos($contentType, 'application/json') === 0) { return $request->withParsedBody($this->parseJson($request->getBody()))->withBody(Middleware::createStream()); } //urlencoded if (stripos($contentType, 'application/x-www-form-urlencoded') === 0) { return $request->withParsedBody($this->parseUrlEncoded($request->getBody()))->withBody(Middleware::createStream()); } //csv if (stripos($contentType, 'text/csv') === 0) { return $request->withParsedBody($this->parseCsv($request->getBody()))->withBody(Middleware::createStream()); } return $request; }
/** * Execute the middleware. * * @param ServerRequestInterface $request * @param ResponseInterface $response * @param callable $next * * @return ResponseInterface */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) { $whoops = $this->getWhoopsInstance($request); //Catch errors means register whoops globally if ($this->catchErrors) { $whoops->register(); } try { $response = $next($request, $response); } catch (\Exception $exception) { $method = Run::EXCEPTION_HANDLER; $whoops->allowQuit(false); $whoops->writeToOutput(false); $body = Middleware::createStream(); $body->write($whoops->{$method}($exception)); $response = $response->withStatus(500)->withBody($body); } $whoops->unregister(); return $response; }
/** * Execute the middleware. * * @param RequestInterface $request * @param ResponseInterface $response * @param callable $next * * @return ResponseInterface */ public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next) { $item = $this->cache->getItem(self::getCacheKey($request)); if ($item->isHit()) { list($headers, $body) = $item->get(); $response = $response->withBody(Middleware::createStream()); $response->getBody()->write($body); foreach ($headers as $name => $header) { $response = $response->withHeader($name, $header); } return $response; } $response = $next($request, $response); if (self::isCacheable($request, $response)) { $item->set([$response->getHeaders(), (string) $response->getBody()]); if (($time = self::getExpiration($response)) !== null) { $item->expiresAt($time); } $this->cache->save($item); } return $response; }
/** * Returns a redirect response. * * @param int $redirectStatus * @param UriInterface $uri * @param ResponseInterface $response */ private static function getRedirectResponse($redirectStatus, UriInterface $uri, ResponseInterface $response) { return $response->withStatus($redirectStatus)->withHeader('Location', (string) $uri)->withBody(Middleware::createStream()); }
/** * Gzip minifier using gzdeflate(). * * @param ResponseInterface $response * * @return ResponseInterface */ public static function deflate(ResponseInterface $response) { $stream = Middleware::createStream(); $stream->write(gzdeflate((string) $response->getBody())); return $response->withHeader('Content-Encoding', 'deflate')->withBody($stream); }
/** * Minify js code. * * @param ResponseInterface $response * * @return ResponseInterface */ protected function minifyJs(ResponseInterface $response) { $stream = Middleware::createStream(); $stream->write(JsMinify::minify((string) $response->getBody())); return $response->withBody($stream); }
/** * Transform the image. * * @param ServerRequestInterface $request * @param ResponseInterface $response * @param string $transform * * @return ResponseInterface */ private function transform(ServerRequestInterface $request, ResponseInterface $response, $transform) { $image = Image::fromString((string) $response->getBody()); $hints = $this->getClientHints($request); if ($hints) { $image->setClientHints($hints); $response = $response->withHeader('Vary', implode(', ', $hints)); } $image->transform($transform); $body = Middleware::createStream(); $body->write($image->getString()); return $response->withBody($body)->withHeader('Content-Type', $image->getMimeType()); }
/** * HTML minifier. * * @param ResponseInterface $response * * @return ResponseInterface */ public function html(ResponseInterface $response) { $stream = Middleware::createStream(); $stream->write(Minify_HTML::minify((string) $response->getBody(), ['jsCleanComments' => true])); return $response->withBody($stream); }