|
| 1 | +/** |
| 2 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 3 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 4 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 5 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 6 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 7 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 8 | + * THE SOFTWARE. |
| 9 | + * |
| 10 | + * This software consists of voluntary contributions made by many individuals |
| 11 | + * and is licensed under the MIT license. |
| 12 | + * |
| 13 | + * Copyright (c) 2017-2020 Yuuki Takezawa |
| 14 | + * |
| 15 | + */ |
| 16 | +namespace Nazg\Middleware; |
| 17 | + |
| 18 | +use type Facebook\HackRouter\HttpMethod; |
| 19 | +use type Facebook\Experimental\Http\Message\ResponseInterface; |
| 20 | +use type Facebook\Experimental\Http\Message\ServerRequestInterface; |
| 21 | +use type HH\Lib\Experimental\IO\CloseableWriteHandle; |
| 22 | +use type Nazg\Http\Server\AsyncMiddlewareInterface; |
| 23 | +use type Nazg\Http\Server\AsyncRequestHandlerInterface; |
| 24 | +use function implode; |
| 25 | + |
| 26 | +type CorsSetting = shape( |
| 27 | + ?'origin' => string, |
| 28 | + ?'header' => string, |
| 29 | + 'methods' => Vector<HttpMethod>, |
| 30 | +); |
| 31 | + |
| 32 | +enum AccessControl : string as string { |
| 33 | + AllowOrigin = 'Access-Control-Allow-Origin'; |
| 34 | + AllowHeaders = 'Access-Control-Allow-Headers'; |
| 35 | + AllowMethods = 'Access-Control-Allow-Methods'; |
| 36 | +} |
| 37 | + |
| 38 | +class SimpleCorsMiddleware implements AsyncMiddlewareInterface { |
| 39 | + |
| 40 | + protected string $allowOrigin = '*'; |
| 41 | + protected string |
| 42 | + $allowHeaders = 'X-Requested-With, Content-Type, Accept, Origin, Authorization'; |
| 43 | + protected Vector<HttpMethod> |
| 44 | + $allowMethods = Vector { |
| 45 | + HttpMethod::GET, |
| 46 | + HttpMethod::HEAD, |
| 47 | + HttpMethod::POST, |
| 48 | + }; |
| 49 | + |
| 50 | + public function __construct(protected CorsSetting $cors) {} |
| 51 | + |
| 52 | + public async function processAsync( |
| 53 | + CloseableWriteHandle $writeHandle, |
| 54 | + ServerRequestInterface $request, |
| 55 | + AsyncRequestHandlerInterface $handler, |
| 56 | + ): Awaitable<ResponseInterface> { |
| 57 | + $response = await $handler->handleAsync($writeHandle, $request); |
| 58 | + $origin = ($this->cors['origin']) ?? $this->allowOrigin; |
| 59 | + $header = ($this->cors['header']) ?? $this->allowHeaders; |
| 60 | + $methods = |
| 61 | + ($this->cors['methods']->isEmpty()) |
| 62 | + ? $this->allowMethods |
| 63 | + : $this->cors['methods']; |
| 64 | + return |
| 65 | + $response->withHeader(AccessControl::AllowOrigin, vec[$origin]) |
| 66 | + ->withHeader(AccessControl::AllowHeaders, vec[$header]) |
| 67 | + ->withHeader( |
| 68 | + AccessControl::AllowMethods, |
| 69 | + vec[$this->implodeMethods($methods)], |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | +<<__Rx>> |
| 74 | + protected function implodeMethods(Vector<HttpMethod> $methods): string { |
| 75 | + return implode(",", $methods); |
| 76 | + } |
| 77 | +} |
0 commit comments