TrueAsync\HttpRequest
(PHP 8.6+, true_async_server 0.6+)
Read-only object passed as the first argument to the handler. Created by the server — not constructed by user code.
namespace TrueAsync;
final class HttpRequest
{
// --- general ---
public function getMethod(): string;
public function getUri(): string;
public function getPath(): string;
public function getHttpVersion(): string;
public function isKeepAlive(): bool;
// --- query ---
public function getQuery(): array;
public function getQueryParam(string $name, mixed $default = null): mixed;
// --- headers ---
public function hasHeader(string $name): bool;
public function getHeader(string $name): ?string;
public function getHeaderLine(string $name): string;
public function getHeaders(): array;
public function getContentType(): ?string;
public function getContentLength(): ?int;
// --- body ---
public function getBody(): string;
public function hasBody(): bool;
public function awaitBody(): static;
public function readBody(int $maxLen = 65536): ?string;
// --- multipart / form ---
public function getPost(): array;
public function getFiles(): array;
public function getFile(string $name): ?UploadedFile;
// --- W3C Trace Context ---
public function getTraceParent(): ?string;
public function getTraceState(): ?string;
public function getTraceId(): ?string;
public function getSpanId(): ?string;
public function getTraceFlags(): ?int;
}General
getMethod
public HttpRequest::getMethod(): string"GET", "POST", "PUT", "DELETE", etc.
getUri
public HttpRequest::getUri(): stringThe full request URI — path plus query string.
getPath
public HttpRequest::getPath(): stringThe path without the query string. For example, /search from /search?q=hello. Uniform across HTTP/1.1, HTTP/2 (:path pseudo-header), and HTTP/3. Together with getQuery() it uses a single lazy parse — the URI is split into path/query on first access and cached in the request struct.
getHttpVersion
public HttpRequest::getHttpVersion(): string"1.1", "2", "3".
isKeepAlive
public HttpRequest::isKeepAlive(): boolQuery
getQuery
public HttpRequest::getQuery(): arrayAll query parameters as an associative array — the equivalent of $_GET. Supports percent-decoding, +-as-space, and PHP array notation (foo[], foo[bar]). Parsing is delegated to php_default_treat_data(PARSE_STRING, ...) — the same function that populates $_GET.
getQueryParam
public HttpRequest::getQueryParam(string $name, mixed $default = null): mixedA single parameter by name, or $default (default null) when missing.
Headers
hasHeader
public HttpRequest::hasHeader(string $name): boolCase-insensitive.
getHeader
public HttpRequest::getHeader(string $name): ?stringA single value, case-insensitive. null when missing.
getHeaderLine
public HttpRequest::getHeaderLine(string $name): stringAll values, joined with commas. Empty string when missing.
getHeaders
public HttpRequest::getHeaders(): arrayAll headers. Names are lowercase.
getContentType
public HttpRequest::getContentType(): ?stringThe value of Content-Type, or null.
getContentLength
public HttpRequest::getContentLength(): ?intContent-Length or null (missing or invalid).
Body
getBody
public HttpRequest::getBody(): stringThe request body. Empty string when there is no body.
In streaming-body mode (
HttpServerConfig::setBodyStreamingEnabled(true))getBody()throws — read throughreadBody().
hasBody
public HttpRequest::hasBody(): boolawaitBody
public HttpRequest::awaitBody(): staticWait for the full body. Since Phase 6 Step 3+, the handler may be invoked immediately after the parsed headers, before the body is fully received. awaitBody() suspends the coroutine until message-complete.
When the body is already fully buffered (the current default), the call returns immediately without suspending.
readBody
public HttpRequest::readBody(int $maxLen = 65536): ?stringPull-based body streaming (issue #26). Returns one parser-supplied chunk per call:
- an H2 DATA frame (≈ 16 KiB);
- an llhttp
on_bodyslice (bounded by the H1 read buffer of 8 KiB).
Behaviour:
- Empty queue → the coroutine parks on a per-request trigger event.
- EOF →
null(idempotent). - Stream error (peer reset,
max_body_sizeexceeded) →\Exception. $maxLenis reserved for a future coalesce optimisation and is currently ignored. The signature stays binary-compatible with the upcoming polish.
Available only when HttpServerConfig::setBodyStreamingEnabled(true).
See Streaming.
Multipart / form
getPost
public HttpRequest::getPost(): arrayPOST data from multipart/form-data or application/x-www-form-urlencoded. Supports PHP-style arrays: name[], user[name], matrix[0][1].
getFiles
public HttpRequest::getFiles(): arrayAll uploaded files. Multiple files with the same name: ['photos' => [UploadedFile, UploadedFile, ...]].
getFile
public HttpRequest::getFile(string $name): ?UploadedFileA single file by name. For photos[], returns the first in the array. null when missing.
See UploadedFile.
W3C Trace Context
Requires HttpServerConfig::setTelemetryEnabled(true).
getTraceParent
public HttpRequest::getTraceParent(): ?stringRaw traceparent as received. null when missing / malformed / telemetry is disabled.
getTraceState
public HttpRequest::getTraceState(): ?stringRaw tracestate. null when missing / telemetry is disabled.
getTraceId
public HttpRequest::getTraceId(): ?stringThe decoded 32-character lower-hex trace id, or null.
getSpanId
public HttpRequest::getSpanId(): ?stringThe decoded 16-character lower-hex parent span id, or null.
getTraceFlags
public HttpRequest::getTraceFlags(): ?intThe decoded 8-bit flags byte (for example, 0x01 — sampled), or null.
Example
$server->addHttpHandler(function (HttpRequest $req, HttpResponse $res) {
error_log(sprintf(
"[%s] %s %s (HTTP/%s, body=%s, traceid=%s)",
$req->getMethod(),
$req->getPath(),
$req->getQuery() ? json_encode($req->getQuery()) : '-',
$req->getHttpVersion(),
$req->getContentLength() ?? 'n/a',
$req->getTraceId() ?? '-'
));
if ($req->getMethod() === 'POST' && $req->getContentType() === 'application/json') {
$body = json_decode($req->getBody(), true);
// ...
}
$res->json(['ok' => true]);
});