Skip to content
Closed
Show file tree
Hide file tree
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
20 changes: 20 additions & 0 deletions src/Illuminate/Cache/RateLimiting/Limit.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ class Limit
*/
public $decaySeconds;

/**
* Whether to only record a hit after a successful response.
*
* @var bool
*/
public $onSuccess = false;

/**
* The response generator callback.
*
Expand Down Expand Up @@ -129,6 +136,19 @@ public function by($key)
return $this;
}

/**
* Set whether to only record a hit after a successful response.
*
* @param bool $onSuccess
* @return $this
*/
public function onSuccess(bool $onSuccess = true)
{
$this->onSuccess = $onSuccess;

return $this;
}

/**
* Set the callback that should generate the response when the limit is exceeded.
*
Expand Down
40 changes: 39 additions & 1 deletion src/Illuminate/Routing/Middleware/ThrottleRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public function handle($request, Closure $next, $maxAttempts = 60, $decayMinutes
'key' => $prefix.$this->resolveRequestSignature($request),
'maxAttempts' => $this->resolveMaxAttempts($request, $maxAttempts),
'decaySeconds' => 60 * $decayMinutes,
'onSuccess' => false,
'responseCallback' => null,
],
]
Expand Down Expand Up @@ -131,6 +132,7 @@ protected function handleRequestUsingNamedLimiter($request, Closure $next, $limi
'key' => self::$shouldHashKeys ? md5($limiterName.$limit->key) : $limiterName.':'.$limit->key,
'maxAttempts' => $limit->maxAttempts,
'decaySeconds' => $limit->decaySeconds,
'onSuccess' => $limit->onSuccess,
'responseCallback' => $limit->responseCallback,
];
})->all()
Expand All @@ -154,12 +156,20 @@ protected function handleRequest($request, Closure $next, array $limits)
throw $this->buildException($request, $limit->key, $limit->maxAttempts, $limit->responseCallback);
}

$this->limiter->hit($limit->key, $limit->decaySeconds);
if (! $limit->onSuccess) {
$this->limiter->hit($limit->key, $limit->decaySeconds);
}
}

$response = $next($request);

$isSuccess = $this->isSuccessfulResponse($request, $response);

foreach ($limits as $limit) {
if ($limit->onSuccess && $isSuccess) {
$this->limiter->hit($limit->key, $limit->decaySeconds);
}

$response = $this->addHeaders(
$response,
$limit->maxAttempts,
Expand All @@ -170,6 +180,34 @@ protected function handleRequest($request, Closure $next, array $limits)
return $response;
}

/**
* Determine whether we should consider the response successful.
*
* @param \Illuminate\Http\Request $request
* @param \Symfony\Component\HttpFoundation\Response $response
* @return bool
*/
protected function isSuccessfulResponse($request, Response $response)
{
if ($request->isPrecognitive()) {
return false;
}

if ($response->isSuccessful()) {
return true;
}

if ($response->getStatusCode() >= 400) {
return false;
}

if (data_get($response, 'exception.status') >= 400) {
return false;
}

return $response->getStatusCode() == 302;
}

/**
* Resolve the number of attempts if the user is authenticated or not.
*
Expand Down