christoph-kluge/reactphp-http-cors-middleware

ReactPHP HTTP-Server 的 CORS 中间件

2.0.0 2020-09-19 20:41 UTC

This package is auto-updated.

Last update: 2024-09-21 22:06:11 UTC


README

此中间件实现了 ReactPHP 的跨源资源共享 (CORS)。此存储库主要受到 tuupola/cors-middleware 的启发。此外,从 barryvdh/laravel-corsnelmio/NelmioCorsBundle 中获取了一些配置灵感。内部重负载由 neomerx/cors-psr7 库完成。

Build Status Total Downloads License

安装

通过 Composer 安装,使用以下命令,它将自动检测最新版本并将其与 ^ 绑定。

composer require christoph-kluge/reactphp-http-cors-middleware

此中间件将检测 CORS 请求,并在检测到无效内容时拦截请求。

用法

$server = new HttpServer(
    new CorsMiddleware(),
    function (ServerRequestInterface $request, callable $next) {
        return new Response(200, ['Content-Type' => 'text/html'], 'We test CORS');
    },
);

配置

此中间件的默认值主要来自 enable-cors.org

可用的配置选项

感谢 expressjs/cors#configuring-cors。因为我从那里获取了大多数配置描述。

  • server_url:可用于设置启用严格的 Host 标头检查,以避免恶意使用我们的服务器。(默认值:null
  • response_code:可用于设置成功的 OPTIONS / 预检请求的 HTTP-StatusCode。(默认值:204
  • allow_credentials:配置 Access-Control-Allow-Credentials CORS 标头。期望一个布尔值(例如:true // 设置标头)
  • allow_origin:配置 Access-Control-Allow-Origin CORS 标头。期望一个数组(例如:['http://example.net', 'https://example.net'])。
  • allow_origin_callback:将 allow_origin 设置为空数组 [] 并在每次请求的基础上使用回调。第一个参数是 ParsedUrlInterface 的实例,而回调期望返回一个 boolean
  • allow_methods:配置 Access-Control-Allow-Methods CORS 标头。期望一个数组(例如:['GET', 'PUT', 'POST'])。
  • allow_headers:配置 Access-Control-Allow-Headers CORS 标头。期望一个数组(例如:['Content-Type', 'Authorization'])。
  • expose_headers:配置 Access-Control-Expose-Headers CORS 标头。期望一个数组(例如:['Content-Range', 'X-Content-Range'])。
  • max_age:配置 Access-Control-Max-Age CORS 标头。期望一个表示秒的整数(例如:1728000 // 20 天)

默认设置(允许所有 CORS 请求)

$settings = [
    'allow_credentials' => true,
    'allow_origin'      => ['*'],
    'allow_methods'     => ['GET', 'POST', 'PUT', 'DELETE', 'HEAD', 'OPTIONS'],
    'allow_headers'     => ['DNT','X-Custom-Header','Keep-Alive','User-Agent','X-Requested-With','If-Modified-Since','Cache-Control','Content-Type','Content-Range','Range'],
    'expose_headers'    => ['DNT','X-Custom-Header','Keep-Alive','User-Agent','X-Requested-With','If-Modified-Since','Cache-Control','Content-Type','Content-Range','Range'],
    'max_age'           => 60 * 60 * 24 * 20, // preflight request is valid for 20 days
];

允许特定源(Origin 需要方案、主机和可选端口)

$server = new HttpServer(
    new CorsMiddleware([
        'allow_origin' => [
            'http://www.example.net',
            'https://www.example.net',
            'http://www.example.net:8443',
        ],
    ])
);

按请求基础允许源(回调)

$server = new HttpServer(
    new CorsMiddleware([
        'allow_origin'          => [],
        'allow_origin_callback' => function(ParsedUrlInterface $origin) {
            // do some evaluation magic with origin ..
            return true;
        },
    ])
);

在预检请求上使用自定义响应代码

一些旧版浏览器在 204 上会出现问题。感谢 expressjs/cors#configuring-cors

$server = new HttpServer(
    new CorsMiddleware([
        'response_code' => 200,
    ])
);

使用严格的主机检查

此中间件的默认处理将允许任何 "Host"-标头。这意味着您可以使用任何主机名使用您的服务器。这可能是一种期望的行为,但也会允许滥用您的服务器。

为了防止这种行为,有一个 server_url 选项,它将启用严格的主机检查。在这种情况下,服务器将返回一个 403,内容为 "Origin not allowed"。

$server = new HttpServer(
    new CorsMiddleware([
        'server_url' => 'http://api.example.net:8080'
    ])
);

许可协议

MIT 许可协议(MIT)

版权所有 (c) 2017 Christoph Kluge

在此特此授予任何人免费获得本软件及其相关文档文件(以下简称“软件”)的副本的权利,不受限制地处理软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件的副本,并允许向软件提供副本的个人这样做,但须遵守以下条件:

上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。

软件按“原样”提供,不提供任何形式的保证,无论是明示的、暗示的还是其他形式的,包括但不限于适销性、特定用途的适用性和非侵权性保证。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任负责,无论是由合同行为、侵权行为或其他行为引起的,无论该索赔、损害或其他责任是否与软件或软件的使用或其他方式有关。