corpus / http-message-utils
Psr7 Http 消息处理的实用工具
Requires
- php: >=7.2
- psr/http-message: ^1 || ^2
Requires (Dev)
- corpus/coding-standard: ^0.6.0
- donatj/drop: ^1.0
- donatj/mddoc: ^0.7.0
- friendsofphp/php-cs-fixer: ^3.3
- guzzlehttp/psr7: ^2.4
- php-mock/php-mock-phpunit: ^2.7
- phpunit/phpunit: ^6.5 || ^9.3
- squizlabs/php_codesniffer: ^3.7
This package is auto-updated.
Last update: 2024-09-03 17:06:46 UTC
README
处理 PSR-7 Http 消息 对象的实用工具。
要求
- psr/http-message: ^1 || ^2
- php: >=7.3
安装
使用以下命令安装最新版本
composer require 'corpus/http-message-utils'
文档
类:\Corpus\HttpMessageUtils\Authorization\AuthorizationHeaderParser
将授权头拆分为 <type>
和 <credentials>
的实用工具,类似于: Authorization: <type> <credentials>
解析器本身不依赖于授权类型,可以与任何符合 RFC7235 的授权类型一起工作。
示例
$serverRequest = \GuzzleHttp\Psr7\ServerRequest::fromGlobals(); $parsedAuth = (new \Corpus\HttpMessageUtils\Authorization\AuthorizationHeaderParser)->parseServerRequest($serverRequest); if( $parsedAuth ) { echo 'type: ' . $parsedAuth->getType(); echo 'cred: ' . $parsedAuth->getCredentials(); }
方法:AuthorizationHeaderParser->__construct
function __construct([ ?\Corpus\HttpMessageUtils\Authorization\AuthorizationPartsFactory $factory = null])
参数
- \Corpus\HttpMessageUtils\Authorization\AuthorizationPartsFactory | null
$factory
- 结果对象的构造的可选工厂
方法:AuthorizationHeaderParser->parseString
function parseString(string $headerValue) : ?\Corpus\HttpMessageUtils\Authorization\AuthorizationPartsInterface
将授权头解析为 <type>
和 <credentials>
参数
- string
$headerValue
- 要解析的头值
返回
- \Corpus\HttpMessageUtils\Authorization\AuthorizationPartsInterface | null - 成功时返回 AuthorizationParts,失败时返回 null。失败原因包括空字符串和非 RFC7235 兼容的头值。
方法:AuthorizationHeaderParser->parseServerRequest
function parseServerRequest(\Psr\Http\Message\ServerRequestInterface $request [, string $headerName = self::DEFAULT_HEADER]) : ?\Corpus\HttpMessageUtils\Authorization\AuthorizationPartsInterface
从 PSR ServerRequestInterface 中轻松解析的辅助工具
参数
- \Psr\Http\Message\ServerRequestInterface
$request
- 要从中读取的 PSR ServerRequestInterface - string
$headerName
- 可选的头名称进行解析。默认为 Authorization。
返回
- \Corpus\HttpMessageUtils\Authorization\AuthorizationPartsInterface | null - 成功时返回 AuthorizationParts,失败时返回 null。
类:\Corpus\HttpMessageUtils\Authorization\AuthorizationPartsInterface
授权头部分的表示: Authorization: <type> <credentials>
方法:AuthorizationPartsInterface->getType
function getType() : string
指定的授权类型
方法:AuthorizationPartsInterface->getCredentials
function getCredentials() : string
指定的授权凭证
类:\Corpus\HttpMessageUtils\ProxyAwareSchemer
将 Uri 或 ServerRequestInterface 的 Uri 映射到从代理(如 AWS 负载均衡器)检测到的外部方案。
仅升级到 https,从不降级。
示例
$serverRequest = \GuzzleHttp\Psr7\ServerRequest::fromGlobals(); $serverRequest = (new \Corpus\HttpMessageUtils\ProxyAwareSchemer)->withUriWithDetectedScheme($serverRequest);
<?php namespace Corpus\HttpMessageUtils; class ProxyAwareSchemer { public const HTTPS_EXPECTED_SERVER_VALUES = ['HTTP_X_FORWARDED_PROTOCOL' => 'https', 'HTTP_X_FORWARDED_PROTO' => 'https', 'HTTP_X_FORWARDED_SSL' => 'on', 'HTTP_FRONT_END_HTTPS' => 'on', 'HTTP_X_URL_SCHEME' => 'https', 'HTTPS' => 'on']; public const PORT_EXPECTED_SERVER_KEYS = ['HTTP_X_FORWARDED_PORT']; /** Value for `default port` arguments to remove the port from the URI */ public const REMOVE_PORT = -65536; }
方法:ProxyAwareSchemer->__construct
function __construct([ ?array $server = null [, ?array $proxyServerHttpsKeyValues = null [, ?array $proxyServerPortKeys = null]]])
参数
- array<string,scalar>
$server
- 要检查的服务器数组。默认为 $_SERVER。 - array<string,string> | null
$proxyServerHttpsKeyValues
- $_SERVER 键到其预期的 https-positive 值的映射。默认为 ProxyAwareSchemer::HTTPS_EXPECTED_SERVER_VALUES - string[] | null
$proxyServerPortKeys
- 要检查转发端口的 $_SERVER 键的数组。
方法:ProxyAwareSchemer->withUriWithDetectedScheme
function withUriWithDetectedScheme(\Psr\Http\Message\ServerRequestInterface $serverRequest [, bool $detectPort = true [, ?int $defaultOnHttps = self::REMOVE_PORT]]) : \Psr\Http\Message\ServerRequestInterface
给定一个 \Psr\Http\Message\ServerRequestInterface,返回一个新的 ServerRequestInterface 实例,具有新的 Uri
其方案已调整为与代理头定义的外部方案相匹配。
参数
- bool
$detectPort
- 启用/禁用代理端口嗅探。 - int | null
$defaultOnHttps
- 如果嗅探失败但检测到 HTTPS 代理,则使用的默认端口。默认为 ProxyAwareSchemer::REMOVE_PORT,它从 URI 中删除端口信息。传递 null 将保留端口。
方法:ProxyAwareSchemer->withDetectedScheme
function withDetectedScheme(\Psr\Http\Message\UriInterface $uri [, bool $detectPort = true [, ?int $defaultOnHttps = self::REMOVE_PORT]]) : \Psr\Http\Message\UriInterface
给定一个 \Psr\Http\Message\UriInterface,返回一个实例化的 UriInterface,其方案已调整为与
代理头定义的外部方案相匹配。
参数
- bool
$detectPort
- 启用/禁用代理端口嗅探。 - int | null
$defaultOnHttps
- 如果嗅探失败但检测到 HTTPS 代理,则使用的默认端口。默认为 ProxyAwareSchemer::REMOVE_PORT,它从 URI 中删除端口信息。传递 null 将保留端口。
方法:ProxyAwareSchemer->withDetectedPort
function withDetectedPort(\Psr\Http\Message\UriInterface $uri [, ?int $default = null]) : \Psr\Http\Message\UriInterface
给定一个 \Psr\Http\Message\UriInterface,返回一个UriInterface实例,其端口号已调整为匹配
代理头定义的外部方案相匹配。
参数
- int | null
$default
- 定义默认回退端口。传递 ProxyAwareSchemer::REMOVE_PORT 将默认删除端口信息。默认为 null - null 保持端口不变。
类:\Corpus\HttpMessageUtils\ResponseSender
用于实现PSR7 ResponseInterface的工具
发送头和体。
示例
$response = new \GuzzleHttp\Psr7\Response(); (new \Corpus\HttpMessageUtils\ResponseSender)->send($response);
灵感来自 http-interop/response-sender
,MIT许可证,版权所有(c)2017 Woody Gilk
方法:ResponseSender->__construct
function __construct([ bool $fullHttpStmtHeader = false [, bool $rewindBody = true]])
ResponseSender构造函数。
参数
- bool
$fullHttpStmtHeader
- 设置为true
启用完整的HTTP语句构造,允许非标准原因短语和可能不匹配的协议版本。请谨慎使用。 - bool
$rewindBody
- 设置为false
允许您在传输之前禁用响应体的重绕。
方法:ResponseSender->send
function send(\Psr\Http\Message\ResponseInterface $response) : void
触发给定 \Psr\Http\Message\ResponseInterface 的传输。