|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Web\RateLimiter; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\Http\Message\ResponseFactoryInterface; |
|
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
9
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
10
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
|
11
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* RateLimiter limits the number of consequential requests ({@see CacheCounter::$limit}) that could be processed per |
|
15
|
|
|
* {@see CacheCounter::$interval}. If the number is reached, middleware responds with HTTP code 429, "Too Many Requests" |
|
16
|
|
|
* until limit expires. |
|
17
|
|
|
*/ |
|
18
|
|
|
final class RateLimiter implements MiddlewareInterface |
|
19
|
|
|
{ |
|
20
|
|
|
private CacheCounter $counter; |
|
21
|
|
|
|
|
22
|
|
|
private ResponseFactoryInterface $responseFactory; |
|
23
|
|
|
|
|
24
|
|
|
private string $counterId; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var callable |
|
28
|
|
|
*/ |
|
29
|
|
|
private $counterIdCallback; |
|
30
|
|
|
|
|
31
|
|
|
public function __construct(CacheCounter $counter, ResponseFactoryInterface $responseFactory) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->counter = $counter; |
|
34
|
|
|
$this->responseFactory = $responseFactory; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
|
38
|
|
|
{ |
|
39
|
|
|
$this->counter->setId($this->generateId($request)); |
|
40
|
|
|
|
|
41
|
|
|
if ($this->counter->limitIsReached()) { |
|
42
|
|
|
return $this->createErrorResponse(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return $handler->handle($request); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function withCounterIdCallback(callable $callback): self |
|
49
|
|
|
{ |
|
50
|
|
|
$this->counterIdCallback = $callback; |
|
51
|
|
|
|
|
52
|
|
|
return $this; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function withCounterId(string $id): self |
|
56
|
|
|
{ |
|
57
|
|
|
$this->counterId = $id; |
|
58
|
|
|
|
|
59
|
|
|
return $this; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
private function createErrorResponse(): ResponseInterface |
|
63
|
|
|
{ |
|
64
|
|
|
$response = $this->responseFactory->createResponse(429); |
|
65
|
|
|
$response->getBody()->write('Too Many Requests'); |
|
66
|
|
|
|
|
67
|
|
|
return $response; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
private function generateId(ServerRequestInterface $request): string |
|
71
|
|
|
{ |
|
72
|
|
|
if ($this->counterIdCallback !== null) { |
|
73
|
|
|
return \call_user_func($this->counterIdCallback, $request); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $this->counterId ?? $this->generateIdFromRequest($request); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
private function generateIdFromRequest(ServerRequestInterface $request): string |
|
80
|
|
|
{ |
|
81
|
|
|
return strtolower('rate-limiter-' . $request->getMethod() . '-' . $request->getUri()->getPath()); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|