Вход Регистрация
Файл: gapps/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php
Строк: 128
<?php

namespace IlluminateRoutingMiddleware;

use 
Closure;
use 
IlluminateCacheRateLimiter;
use 
SymfonyComponentHttpFoundationResponse;

class 
ThrottleRequests
{
    
/**
     * The rate limiter instance.
     *
     * @var IlluminateCacheRateLimiter
     */
    
protected $limiter;

    
/**
     * Create a new request throttler.
     *
     * @param  IlluminateCacheRateLimiter  $limiter
     * @return void
     */
    
public function __construct(RateLimiter $limiter)
    {
        
$this->limiter $limiter;
    }

    
/**
     * Handle an incoming request.
     *
     * @param  IlluminateHttpRequest  $request
     * @param  Closure  $next
     * @param  int  $maxAttempts
     * @param  int  $decayMinutes
     * @return mixed
     */
    
public function handle($requestClosure $next$maxAttempts 60$decayMinutes 1)
    {
        
$key $this->resolveRequestSignature($request);

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

        
$this->limiter->hit($key$decayMinutes);

        
$response $next($request);

        return 
$this->addHeaders(
            
$response$maxAttempts,
            
$this->calculateRemainingAttempts($key$maxAttempts)
        );
    }

    
/**
     * Resolve request signature.
     *
     * @param  IlluminateHttpRequest  $request
     * @return string
     */
    
protected function resolveRequestSignature($request)
    {
        return 
$request->fingerprint();
    }

    
/**
     * Create a 'too many attempts' response.
     *
     * @param  string  $key
     * @param  int  $maxAttempts
     * @return IlluminateHttpResponse
     */
    
protected function buildResponse($key$maxAttempts)
    {
        
$response = new Response('Too Many Attempts.'429);

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

        return 
$this->addHeaders(
            
$response$maxAttempts,
            
$this->calculateRemainingAttempts($key$maxAttempts$retryAfter),
            
$retryAfter
        
);
    }

    
/**
     * Add the limit header information to the given response.
     *
     * @param  SymfonyComponentHttpFoundationResponse  $response
     * @param  int  $maxAttempts
     * @param  int  $remainingAttempts
     * @param  int|null  $retryAfter
     * @return IlluminateHttpResponse
     */
    
protected function addHeaders(Response $response$maxAttempts$remainingAttempts$retryAfter null)
    {
        
$headers = [
            
'X-RateLimit-Limit' => $maxAttempts,
            
'X-RateLimit-Remaining' => $remainingAttempts,
        ];

        if (! 
is_null($retryAfter)) {
            
$headers['Retry-After'] = $retryAfter;
        }

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

        return 
$response;
    }

    
/**
     * Calculate the number of remaining attempts.
     *
     * @param  string  $key
     * @param  int  $maxAttempts
     * @param  int|null  $retryAfter
     * @return int
     */
    
protected function calculateRemainingAttempts($key$maxAttempts$retryAfter null)
    {
        if (! 
is_null($retryAfter)) {
            return 
0;
        }

        return 
$this->limiter->retriesLeft($key$maxAttempts);
    }
}
Онлайн: 2
Реклама