Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.5] Throttles request - throw a 429 HTTP exception. #19860

Merged
merged 2 commits into from
Jul 4, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 28 additions & 11 deletions src/Illuminate/Routing/Middleware/ThrottleRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Support\Carbon;
use Illuminate\Cache\RateLimiter;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpException;

class ThrottleRequests
{
Expand Down Expand Up @@ -37,6 +38,7 @@ public function __construct(RateLimiter $limiter)
* @param int|string $maxAttempts
* @param float|int $decayMinutes
* @return mixed
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
*/
public function handle($request, Closure $next, $maxAttempts = 60, $decayMinutes = 1)
{
Expand All @@ -45,7 +47,7 @@ public function handle($request, Closure $next, $maxAttempts = 60, $decayMinutes
$maxAttempts = $this->resolveMaxAttempts($request, $maxAttempts);

if ($this->limiter->tooManyAttempts($key, $maxAttempts, $decayMinutes)) {
return $this->buildResponse($key, $maxAttempts);
throw $this->buildException($key, $maxAttempts);
}

$this->limiter->hit($key, $decayMinutes);
Expand Down Expand Up @@ -95,23 +97,23 @@ protected function resolveRequestSignature($request)
}

/**
* Create a 'too many attempts' response.
* Create a 'too many attempts' exception.
*
* @param string $key
* @param int $maxAttempts
* @return \Symfony\Component\HttpFoundation\Response
* @return \Symfony\Component\HttpKernel\Exception\HttpException
*/
protected function buildResponse($key, $maxAttempts)
protected function buildException($key, $maxAttempts)
{
$response = new Response('Too Many Attempts.', 429);

$retryAfter = $this->limiter->availableIn($key);

return $this->addHeaders(
$response, $maxAttempts,
$headers = $this->getHeaders(
$maxAttempts,
$this->calculateRemainingAttempts($key, $maxAttempts, $retryAfter),
$retryAfter
);

return new HttpException(429, 'Too Many Attempts.', null, $headers);
}

/**
Expand All @@ -124,6 +126,23 @@ protected function buildResponse($key, $maxAttempts)
* @return \Symfony\Component\HttpFoundation\Response
*/
protected function addHeaders(Response $response, $maxAttempts, $remainingAttempts, $retryAfter = null)
{
$response->headers->add(
$this->getHeaders($maxAttempts, $remainingAttempts, $retryAfter)
);

return $response;
}

/**
* Get the limit headers information.
*
* @param int $maxAttempts
* @param int $remainingAttempts
* @param int|null $retryAfter
* @return array
*/
protected function getHeaders($maxAttempts, $remainingAttempts, $retryAfter = null)
{
$headers = [
'X-RateLimit-Limit' => $maxAttempts,
Expand All @@ -135,9 +154,7 @@ protected function addHeaders(Response $response, $maxAttempts, $remainingAttemp
$headers['X-RateLimit-Reset'] = Carbon::now()->getTimestamp() + $retryAfter;
}

$response->headers->add($headers);

return $response;
return $headers;
}

/**
Expand Down