innmind / http-parser
2.1.0
2024-03-10 15:32 UTC
Requires
- php: ~8.2
- innmind/http: ~7.0
- innmind/immutable: ~5.2
- innmind/io: ~2.7
- innmind/stream: ~4.0
- innmind/time-continuum: ~3.2
Requires (Dev)
- innmind/black-box: ~5.5
- innmind/coding-standard: ~2.0
- vimeo/psalm: ~5.15
README
一组类,用于将任何流解析为 innmind/http
对象。
如果您想解析保存到文件中的请求或构建 HTTP 服务器,这很有用。
支持的功能
- 解码请求
- 读取带有
Transfer-Encoding: chunked
的流请求 - 提取
Cookie
- 提取表单数据
- 提取查询数据
- 读取多部分体
安装
composer require innmind/http-parser
用法
use Innmind\HttpParser\{ Request\Parse, ServerRequest\Transform, ServerRequest\DecodeCookie, ServerRequest\DecodeQuery, ServerRequest\DecodeForm, }; use Innmind\TimeContinuum\Earth\Clock; use Innmind\IO\IO; use Innmind\Stream\Streams; use Innmind\Http\Message\ServerRequest; use Innmind\Immutable\Str; // this data could come from anywhere $raw = <<<RAW POST /some-form HTTP/1.1 Host: innmind.com Content-Type: application/x-www-form-urlencoded Content-Length: 23 Accept-Language: fr-fr Cookie: PHPSESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43; _gat=1 some[key]=value&foo=bar RAW; $streams = Streams::fromAmbientAuthority(); $io = IO::of(static fn($timeout) => match ($timeout) { null => $streams->watch()->waitForever(), default => $streams->watch()->timeoutAfter($timeout), }); $parse = Parse::default(new Clock); $stream = $streams ->temporary() ->new() ->write(Str::of($raw)) ->flatMap(static fn($stream) => $stream->rewind()) ->match( static fn($stream) => $stream, static fn() => throw new \RuntimeException('Stream not writable'), ); $stream = $io ->readable() ->wrap($stream) ->watch(); $request = $parse($stream) ->map(Transform::of()) ->map(DecodeCookie::of()) ->map(DecodeQuery::of()) ->map(DecodeForm::of()) ->match( static fn($request) => $request, static fn() => throw new \RuntimeException, ); $request instanceof ServerRequest, // true