|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of CodeIgniter 4 framework. |
| 7 | + * |
| 8 | + * (c) CodeIgniter Foundation <[email protected]> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view |
| 11 | + * the LICENSE file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CodeIgniter\Filters; |
| 15 | + |
| 16 | +use CodeIgniter\HTTP\Cors as CorsService; |
| 17 | +use CodeIgniter\HTTP\IncomingRequest; |
| 18 | +use CodeIgniter\HTTP\RequestInterface; |
| 19 | +use CodeIgniter\HTTP\ResponseInterface; |
| 20 | + |
| 21 | +/** |
| 22 | + * @see \CodeIgniter\Filters\CorsTest |
| 23 | + */ |
| 24 | +class Cors implements FilterInterface |
| 25 | +{ |
| 26 | + private ?CorsService $cors = null; |
| 27 | + |
| 28 | + /** |
| 29 | + * @testTag $config is used for testing purposes only. |
| 30 | + * |
| 31 | + * @param array{ |
| 32 | + * allowedOrigins?: list<string>, |
| 33 | + * allowedOriginsPatterns?: list<string>, |
| 34 | + * supportsCredentials?: bool, |
| 35 | + * allowedHeaders?: list<string>, |
| 36 | + * exposedHeaders?: list<string>, |
| 37 | + * allowedMethods?: list<string>, |
| 38 | + * maxAge?: int, |
| 39 | + * } $config |
| 40 | + */ |
| 41 | + public function __construct(array $config = []) |
| 42 | + { |
| 43 | + if ($config !== []) { |
| 44 | + $this->cors = new CorsService($config); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @param list<string>|null $arguments |
| 50 | + * |
| 51 | + * @return ResponseInterface|string|void |
| 52 | + */ |
| 53 | + public function before(RequestInterface $request, $arguments = null) |
| 54 | + { |
| 55 | + if (! $request instanceof IncomingRequest) { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + $this->createCorsService($arguments); |
| 60 | + |
| 61 | + if (! $this->cors->isPreflightRequest($request)) { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + /** @var ResponseInterface $response */ |
| 66 | + $response = service('response'); |
| 67 | + |
| 68 | + $response = $this->cors->handlePreflightRequest($request, $response); |
| 69 | + |
| 70 | + // Always adds `Vary: Access-Control-Request-Method` header for cacheability. |
| 71 | + // If there is an intermediate cache server such as a CDN, if a plain |
| 72 | + // OPTIONS request is sent, it may be cached. But valid preflight requests |
| 73 | + // have this header, so it will be cached separately. |
| 74 | + $response->appendHeader('Vary', 'Access-Control-Request-Method'); |
| 75 | + |
| 76 | + return $response; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @param list<string>|null $arguments |
| 81 | + */ |
| 82 | + private function createCorsService(?array $arguments): void |
| 83 | + { |
| 84 | + $this->cors ??= ($arguments === null) ? CorsService::factory() |
| 85 | + : CorsService::factory($arguments[0]); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * @param list<string>|null $arguments |
| 90 | + * |
| 91 | + * @return ResponseInterface|void |
| 92 | + */ |
| 93 | + public function after(RequestInterface $request, ResponseInterface $response, $arguments = null) |
| 94 | + { |
| 95 | + if (! $request instanceof IncomingRequest) { |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + $this->createCorsService($arguments); |
| 100 | + |
| 101 | + // Always adds `Vary: Access-Control-Request-Method` header for cacheability. |
| 102 | + // If there is an intermediate cache server such as a CDN, if a plain |
| 103 | + // OPTIONS request is sent, it may be cached. But valid preflight requests |
| 104 | + // have this header, so it will be cached separately. |
| 105 | + if ($request->is('OPTIONS')) { |
| 106 | + $response->appendHeader('Vary', 'Access-Control-Request-Method'); |
| 107 | + } |
| 108 | + |
| 109 | + return $this->cors->addResponseHeaders($request, $response); |
| 110 | + } |
| 111 | +} |
0 commit comments