Unblock-HTTP
view release on metacpan or search on metacpan
work on a later event-loop turn. The Server advertises Extended CONNECT
only for explicitly enabled protocols. WebSocket acceptance affects
one multiplexed stream on H2/H3, but transfers a whole HTTP1 socket.
`on_headers($tx, $request)` runs first. By default the engine buffers up to
1 MiB, then calls `on_request($tx, $request)` with the complete frozen request.
Set `max_buffered_body_bytes` in a protocol hash to change that positive limit.
For streaming, register `$tx->on_data(sub { my ($tx, $bytes) = @_; ... })`
during `on_headers`. This suppresses full-body accumulation. `on_request`
still runs after successful body completion. Failed or cancelled bodies do
not reach it. Register `$tx->on_finish(...)` before the transaction is terminal
to observe completion, error, or cancellation exactly once.
For enabled protocols this requires Unblock::HTTP1 0.13+, Unblock::HTTP2
0.13+, or Unblock::HTTP3 0.14+. Attachment also checks required engine methods
and gives an explicit error for an incompatible older engine.
## Framework attachment
For each incoming TCP/TLS connection, the adapter attaches its stream:
```perl
my $engine = $server->attach_stream(
transport => $stream,
alpn => $negotiated_alpn,
);
$engine->input($bytes);
```
The returned object is the actual HTTP1 or HTTP2 Server engine. Transactions
also come directly from that engine. There is no forwarding connection object.
The framework retains the engine and continues to own its socket and buffers.
The stream supplies `unblock_send`, `unblock_finish`, and `unblock_abort`.
`unblock_send` accepts the entire buffer or throws. A defined false result
means the bytes were accepted but the host is congested. The framework calls
`resume_output()` when it has capacity again.
Configure TLS using `$server->alpn_protocols`. This returns enabled TCP choices,
with H2 preferred. Empty ALPN selects H1 only when H1 is enabled. Unknown or
disabled protocols fail. For cleartext H2 prior knowledge, pass
`protocol => 'h2'` instead of `alpn`.
A ready HTTP/3 QUIC connection uses:
```perl
my $engine = $server->attach_quic(connection => $ready_h3_connection);
```
It returns a started HTTP3 Server engine. UDP and timer events continue to go
to `Net::QUIC::Driver`. The framework is responsible for supplying a connection
negotiated for H3. A QUIC manager can attach all ready connections to the same
definition:
```perl
my $endpoint = Unblock::HTTP::QUIC->server(
server => $server,
certificate_file => $cert,
private_key_file => $key,
send => $send_datagram,
set_timeout => $replace_timer,
);
$endpoint->start;
```
Read `perldoc Unblock::HTTP::Integration` for the adapter contract.
## Shared Client
The Client now owns reusable HTTP policy, request scheduling, and per-origin
connection pools. An application makes one Client for many destinations:
```perl
my $op = $client->request(
method => 'GET',
url => 'https://example.com/data',
on_response => sub {
my ($tx, $response) = @_;
print $response->status, "\n";
},
);
```
The request returns an operation handle right away. Its `transaction()` is
undefined while waiting for a connection, then becomes the **real engine
Transaction** after dispatch. `cancel()`, `state()`, and `error()` work
without polling. A strict `http_version => '3'` requires HTTP/3, not a
fallback to HTTP/2. HTTPS cold requests use H2/H1 ALPN; later requests may use H3
through an authenticated, fresh, same-host Alt-Svc advertisement.
An integration supplies `connect_stream`, `connect_quic` and a `defer`
callback. The factory opens TCP/TLS or QUIC through its framework and calls
`$ready->(...)`. **The ready callback returns the real connected engine.**
The adapter keeps that engine on its framework-owned stream or connection and
feeds input directly to it. The `defer` callback schedules work in a later
event-loop turn. See `perldoc Unblock::HTTP::Client` and
`perldoc Unblock::HTTP::Integration` for the exact contracts.
By default the Client buffers up to 1 MiB of response data in the chosen
engine, then calls `on_response` once after body completion. Supplying
`on_data` automatically streams the response: `on_response` fires when
headers arrive, then `on_data($tx, $chunk)`, `on_end($tx)`, and terminal
`on_finish($tx)`. Client buffering does not add another body accumulator.
A request may set `timeout => $seconds` to cover queuing, establishment,
and its active HTTP transaction. The framework adapter supplies an optional
`schedule_timer($seconds, $callback)` constructor callback returning a timer
cancellation coderef. A timed-out active request cancels its real engine
transaction and reports a timeout on the operation. A factory can return
a cancellation coderef so a timed-out or manually cancelled connection
attempt releases its pool slot; late factory readiness is ignored.
When HTTP/3 QUIC bidi stream credit is exhausted, an unsubmitted
request stays in the bounded Client queue; the H3 engine notifies when
request stream availability changes, and the Client retries admission
only after framework deferral. No submitted request is replayed.
The framework may call `$client->network_changed` when its network
identity changes. This discards idle old-network sockets and Alt-Svc
( run in 0.692 second using v1.01-cache-2.11-cpan-8379b9b8ec2 )