PAGI-Server
view release on metacpan or search on metacpan
lib/PAGI/Server/Protocol/HTTP2.pm view on Meta::CPAN
my $consumed = $session->feed($data);
Feed incoming data to the HTTP/2 session. Returns bytes consumed.
=cut
sub feed {
my ($self, $data) = @_;
return $self->{nghttp2}->mem_recv($data);
}
=head2 extract
my $data = $session->extract;
Extract outgoing data from the session. Returns bytes to send.
=cut
sub extract {
my ($self) = @_;
return $self->{nghttp2}->mem_send;
}
=head2 want_read
if ($session->want_read) { ... }
Check if session wants to read.
=cut
sub want_read {
my ($self) = @_;
return $self->{nghttp2}->want_read;
}
=head2 want_write
if ($session->want_write) { ... }
Check if session has data to write.
=cut
sub want_write {
my ($self) = @_;
return $self->{nghttp2}->want_write;
}
=head2 submit_response
$session->submit_response($stream_id,
status => 200,
headers => [['content-type', 'text/html']],
body => $body,
);
Submit a response on a stream. C<body> can be a string (sent as single
response) or a coderef for streaming.
=cut
sub submit_response {
my ($self, $stream_id, %args) = @_;
return $self->{nghttp2}->submit_response($stream_id, %args);
}
=head2 submit_response_streaming
$session->submit_response_streaming($stream_id,
status => 200,
headers => [['content-type', 'text/event-stream']],
data_callback => sub {
my ($stream_id, $max_len) = @_;
return ($chunk, $is_eof);
},
);
Submit a streaming response with a data provider callback.
=cut
sub submit_response_streaming {
my ($self, $stream_id, %args) = @_;
return $self->{nghttp2}->submit_response($stream_id,
status => $args{status},
headers => $args{headers},
data_callback => $args{data_callback},
callback_data => $args{callback_data},
);
}
=head2 resume_stream
$session->resume_stream($stream_id);
Resume a deferred stream after data becomes available.
=cut
sub resume_stream {
my ($self, $stream_id) = @_;
return $self->{nghttp2}->resume_stream($stream_id);
}
=head2 submit_trailer
$session->submit_trailer($stream_id, headers => [['x-checksum', 'abc']]);
Submit the trailing HEADERS block for a response (design section 8.3):
queues C<NGHTTP2_FLAG_END_STREAM> on a HEADERS frame after any already-queued
DATA, completing a response whose data provider reserved END_STREAM for the
trailers (a three-value C<($chunk, $eof, $no_end_stream)> data-callback
return). C<headers> defaults to C<[]> for a declared-but-empty trailer
block. Throws (via the underlying C<Net::HTTP2::nghttp2::Session>) on a
malformed tuple or a pseudo-header name. An unknown or already-gone stream
id does not throw -- it returns a falsy nghttp2 status; C<stream_id == 0> is
the one id nghttp2 rejects with an immediate croak (invalid argument).
=cut
sub submit_trailer {
my ($self, $stream_id, %args) = @_;
return $self->{nghttp2}->submit_trailer($stream_id, headers => $args{headers} // []);
}
=head2 submit_data
$session->submit_data($stream_id, $data, $eof);
Push data directly onto a stream. Used for WebSocket frame delivery
over HTTP/2 where frames are sent as DATA payloads.
=cut
sub submit_data {
my ($self, $stream_id, $data, $eof) = @_;
return $self->{nghttp2}->submit_data($stream_id, $data, $eof);
}
=head2 terminate
$session->terminate($error_code);
Terminate the session with GOAWAY.
=cut
sub terminate {
my ($self, $error_code) = @_;
$error_code //= 0; # NO_ERROR
return $self->{nghttp2}->terminate_session($error_code);
}
=head2 submit_rst_stream
$session->submit_rst_stream($stream_id, $error_code);
Reset a single stream with RST_STREAM, leaving the rest of the session
untouched. C<$error_code> is an RFC 9113 section 7 error code (for example
C<2>, INTERNAL_ERROR).
Used by the server's HTTP/2 dispatch to abort a stream whose response was
left started but incomplete -- the application returned early or threw after
C<http.response.start>, so there is no honest way to finish the body.
=cut
sub submit_rst_stream {
my ($self, $stream_id, $error_code) = @_;
return $self->{nghttp2}->submit_rst_stream($stream_id, $error_code);
}
1;
__END__
=head1 HTTP/2 vs HTTP/1.1
Key differences that affect PAGI integration:
=over 4
=item * Multiplexing - Multiple concurrent requests on one TCP connection
=item * Binary Framing - nghttp2 handles all framing; PAGI feeds/extracts bytes
=item * Header Compression - HPACK is built into nghttp2
=item * Flow Control - Per-stream and connection-level, via streaming callbacks
=back
=head1 SEE ALSO
L<Net::HTTP2::nghttp2>, L<PAGI::Server::Protocol::HTTP1>
=cut
( run in 0.727 second using v1.01-cache-2.11-cpan-5c0b1e786e0 )