|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace GreenTurtle\Middleware; |
| 4 | + |
| 5 | +use Closure; |
| 6 | +use GreenTurtle\Middleware\Encoder\ContentEncoder; |
| 7 | +use GreenTurtle\Middleware\Exception\FailedToEncode; |
| 8 | +use Illuminate\Http\Request; |
| 9 | +use Symfony\Component\HttpFoundation\Response; |
| 10 | + |
| 11 | +class ContentEncoding |
| 12 | +{ |
| 13 | + /** @var string[] */ |
| 14 | + private readonly array $types; |
| 15 | + /** @var ContentEncoder[] */ |
| 16 | + private readonly array $encoders; |
| 17 | + |
| 18 | + /** |
| 19 | + * @param string[] $allowedTypes |
| 20 | + * @param ContentEncoder[] $encoders |
| 21 | + */ |
| 22 | + public function __construct( |
| 23 | + private readonly bool $encodeUnknownType, |
| 24 | + array $allowedTypes, |
| 25 | + array $encoders |
| 26 | + ) { |
| 27 | + (fn(string ...$p) => true)(...$allowedTypes); |
| 28 | + (fn(ContentEncoder ...$p) => true)(...$encoders); |
| 29 | + |
| 30 | + $this->types = $allowedTypes; |
| 31 | + $this->encoders = $encoders; |
| 32 | + } |
| 33 | + |
| 34 | + public function handle(Request $request, Closure $next): Response |
| 35 | + { |
| 36 | + $response = $next($request); |
| 37 | + assert($response instanceof Response); |
| 38 | + |
| 39 | + if ( |
| 40 | + !$response->getContent() |
| 41 | + || $response->headers->has('Content-Encoding') |
| 42 | + || !is_string($request->header('Accept-Encoding')) |
| 43 | + || !$this->isEncodable($request->header('Content-Type')) |
| 44 | + ) { |
| 45 | + return $response; |
| 46 | + } |
| 47 | + |
| 48 | + [$encoding, $encoder] = $this->findEncoder($request->header('Accept-Encoding')); |
| 49 | + if (is_null($encoding) || is_null($encoder)) { |
| 50 | + return $response; |
| 51 | + } |
| 52 | + |
| 53 | + try { |
| 54 | + $encodedContent = $encoder->encode($response->getContent()); |
| 55 | + } catch (FailedToEncode) { |
| 56 | + return $response; |
| 57 | + } |
| 58 | + |
| 59 | + $response->headers->set('Content-Encoding', $encoding); |
| 60 | + $response->setContent($encodedContent); |
| 61 | + return $response; |
| 62 | + } |
| 63 | + |
| 64 | + private function isEncodable(mixed $contentType): bool |
| 65 | + { |
| 66 | + if (!is_string($contentType)) { |
| 67 | + return $this->encodeUnknownType; |
| 68 | + } |
| 69 | + |
| 70 | + foreach ($this->types as $type) { |
| 71 | + if (preg_match($type, $contentType) === 1) { |
| 72 | + return true; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return false; |
| 77 | + } |
| 78 | + |
| 79 | + /** @return array{?string, ?ContentEncoder} */ |
| 80 | + private function findEncoder(string $acceptEncoding): array |
| 81 | + { |
| 82 | + $acceptEncodings = explode(',', str_replace(' ', '', $acceptEncoding)); |
| 83 | + |
| 84 | + foreach ($this->encoders as $encoder) { |
| 85 | + foreach ($acceptEncodings as $acceptEncoding) { |
| 86 | + if ($encoder->supports($acceptEncoding)) { |
| 87 | + return [$acceptEncoding, $encoder]; |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return [null, null]; |
| 93 | + } |
| 94 | +} |
0 commit comments