/** * Handle a given request and return the response. * * @param \Symfony\Component\HttpFoundation\Request $request * @param int $type * @param bool $catch * @return \Symfony\Component\HttpFoundation\Response * @throws \Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException */ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true) { // Our middleware needs to ensure that Laravel is booted before we // can do anything. This gives us access to all the booted // service providers and other container bindings. $this->container->boot(); if ($request instanceof InternalRequest || $this->auth->user()) { return $this->app->handle($request, $type, $catch); } // If a collection exists for the request and we can match a route // from the request then we'll check to see if the route is // protected and, if it is, we'll attempt to authenticate. if ($this->router->requestTargettingApi($request) && ($collection = $this->getApiRouteCollection($request))) { try { $route = $this->controllerReviser->revise($collection->match($request)); if ($this->routeIsProtected($route)) { return $this->authenticate($request, $route) ?: $this->app->handle($request, $type, $catch); } } catch (HttpExceptionInterface $exception) { // If we catch an HTTP exception then we'll simply let // the wrapping kernel do its thing. } } return $this->app->handle($request, $type, $catch); }
/** * Handle a given request and return the response. * * @param \Symfony\Component\HttpFoundation\Request $request * @param int $type * @param bool $catch * @return \Symfony\Component\HttpFoundation\Response * @throws \Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException */ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true) { $this->container->boot(); $this->prepareConfig($request); // Internal requests as well as requests that are not targetting the // API will not be rate limited. We'll also be sure not to perform // any rate limiting if it has been disabled. if ($request instanceof InternalRequest or !$this->router->requestTargettingApi($request) or $this->rateLimitingDisabled()) { return $this->app->handle($request, $type, $catch); } $this->cache->add($this->config['keys']['requests'], 0, $this->config['reset']); $this->cache->add($this->config['keys']['reset'], time() + $this->config['reset'] * 60, $this->config['reset']); $this->cache->increment($this->config['keys']['requests']); if ($this->exceededRateLimit()) { list($version, $format) = $this->router->parseAcceptHeader($request); $response = (new Response(['message' => $this->config['exceeded']], 403))->morph($format); } else { $response = $this->app->handle($request, $type, $catch); } return $this->adjustResponseHeaders($response); }