blib/lib/PAGI/Server/Protocol/HTTP2.pm

filter: Statementstmt 96.1% 175 / 182 Branchbran 70.5% 55 / 78 Conditioncond 64.0% 32 / 50 MC/DCmcdc 34.4% 10 / 29 Subroutinesub 96.7% 30 / 31 Podpod 100% 4 / 4 totaltotal 81.8% 306 / 374 CC 62 SCAR 26.7
CC
62
Cov
85%
__ANON__
35.3
__ANON__
9.3
new
8.0
CRAP 14.4
« blib/lib/PAGI/Server/Runner.pm ↑ Summary blib/lib/PAGI/Server/EventValidator.pm »
1 package PAGI::Server::Protocol::HTTP2;
2 155 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 155 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 155 our $VERSION = '0.002013';
6  
7 =encoding utf8
8  
9 =head1 NAME
10  
11 PAGI::Server::Protocol::HTTP2 - HTTP/2 protocol handler using nghttp2
12  
13 =head1 SYNOPSIS
14  
15 use PAGI::Server::Protocol::HTTP2;
16  
17 my $proto = PAGI::Server::Protocol::HTTP2->new;
18  
19 if ($proto->available) {
20 my $session = $proto->create_session(
21 on_request => sub { ... },
22 on_body => sub { ... },
23 on_close => sub { ... },
24 );
25 }
26  
27 =head1 DESCRIPTION
28  
29 PAGI::Server::Protocol::HTTP2 provides HTTP/2 support for PAGI::Server
30 using the nghttp2 C library via Net::HTTP2::nghttp2.
31  
32 Unlike HTTP/1.1, HTTP/2 uses binary framing, multiplexed streams on a
33 single connection, HPACK header compression, and per-stream flow control.
34  
35 This module bridges nghttp2's callback-based API to PAGI's event model.
36  
37 =cut
38  
39 # HTTP/2 client connection preface (RFC 9113 Section 3.4)
40 155 ▶use constant H2_CLIENT_PREFACE => "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n";
Subroutine BEGIN: called CC=1 SCAR=0.0
41 155 ▶use constant H2_PREFACE_LENGTH => 24;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 # Check for nghttp2 availability
44 155 our $AVAILABLE;
45 155 ▶use constant MIN_NGHTTP2_VERSION => '0.009';
Subroutine BEGIN: called CC=1 SCAR=0.0
46 155 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.0
47 155 ▶ $AVAILABLE = eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Net::HTTP2::nghttp2; 'Net::HTTP2::nghttp2'->VERSION('0.009'); require Net::HTTP2::nghttp2::Session; 'Net::HTTP2::nghttp2'->available } } ? : 155 0 155
48 155 require Net::HTTP2::nghttp2;
49 155 Net::HTTP2::nghttp2->VERSION(MIN_NGHTTP2_VERSION);
50 155 require Net::HTTP2::nghttp2::Session;
51 155 Net::HTTP2::nghttp2->available;
52 } ? 1 : 0;
53 }
54  
55 194 ▶sub available { return $AVAILABLE }
Subroutine available: called CC=1 SCAR=0.0
Pod documented
56  
57 =head2 available
58  
59 if (PAGI::Server::Protocol::HTTP2->available) { ... }
60  
61 Returns true if HTTP/2 support is usable — that is, if Net::HTTP2::nghttp2
62 (at least version C<MIN_NGHTTP2_VERSION>) and its Session class loaded and the
63 underlying nghttp2 library reports itself available. Returns false otherwise.
64 Checked once at module load.
65  
66 =head2 detect_preface
67  
68 if (PAGI::Server::Protocol::HTTP2->detect_preface($bytes)) { ... }
69  
70 Returns true if C<$bytes> starts with the HTTP/2 client connection preface.
71 Used for h2c (cleartext HTTP/2) detection.
72  
73 =cut
74  
75 148 ▶sub detect_preface {
Subroutine detect_preface: called CC=3 SCAR=11.0
Pod documented
76 148 my ($class, $bytes) = @_;
77 148 ▶ return 0 unless defined $bytes && length($bytes) >= H2_PREFACE_LENGTH;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes and length $bytes >= 24 3 145 148
Condition66%2/3
defined $bytes and length $bytes >= 24
!ll&&!rl&&r
03145
MC/DC50%1/2
defined $bytes and length $bytes >= 24
defined $bytes length $bytes >= 24
Truth table66%2/3
A and B
A= defined $bytesB= length $bytes >= 24
ABresult
0X0
100
111
78 145 return substr($bytes, 0, H2_PREFACE_LENGTH) eq H2_CLIENT_PREFACE;
79 }
80  
81 =head2 new
82  
83 my $proto = PAGI::Server::Protocol::HTTP2->new(
84 max_concurrent_streams => 100, # Default
85 initial_window_size => 65535, # Default
86 max_frame_size => 16384, # Default
87 enable_push => 0, # Default (disabled)
88 enable_connect_protocol => 1, # Default (enabled, RFC 8441)
89 max_header_list_size => 65536, # Default (64KB)
90 h2_rst_rate_limit => { burst => 1000, rate => 33 }, # Default (Rapid Reset defense)
91 );
92  
93 Creates a new HTTP/2 protocol handler with the specified settings.
94  
95 =cut
96  
97 245 ▶sub new {
Subroutine new: called CC=8 SCAR=20.8
Pod documented
98 245 my ($class, %args) = @_;
99  
100 my $self = bless {
101 max_concurrent_streams => $args{max_concurrent_streams} // 100,
102 initial_window_size => $args{initial_window_size} // 65535,
103 max_frame_size => $args{max_frame_size} // 16384,
104 enable_push => $args{enable_push} // 0,
105 enable_connect_protocol => $args{enable_connect_protocol} // 1,
106 max_header_list_size => $args{max_header_list_size} // 65536,
107 245 ▶ h2_rst_rate_limit => $args{h2_rst_rate_limit} // { burst => 1000, rate => 33 },
Condition100%14/14
$args{"max_concurrent_streams"} // 100
l!l
22817
$args{"initial_window_size"} // 65535
l!l
22817
$args{"max_frame_size"} // 16384
l!l
22817
$args{"enable_push"} // 0
l!l
22817
$args{"enable_connect_protocol"} // 1
l!l
22817
$args{"max_header_list_size"} // 65536
l!l
23015
$args{"h2_rst_rate_limit"} // {"burst", 1000, "rate", 33}
l!l
22916
MC/DC100%1/1
$args{"max_concurrent_streams"} // 100
$args{"max_concurrent_streams"}
MC/DC100%1/1
$args{"initial_window_size"} // 65535
$args{"initial_window_size"}
MC/DC100%1/1
$args{"max_frame_size"} // 16384
$args{"max_frame_size"}
MC/DC100%1/1
$args{"enable_push"} // 0
$args{"enable_push"}
MC/DC100%1/1
$args{"enable_connect_protocol"} // 1
$args{"enable_connect_protocol"}
MC/DC100%1/1
$args{"max_header_list_size"} // 65536
$args{"max_header_list_size"}
MC/DC100%1/1
$args{"h2_rst_rate_limit"} // {"burst", 1000, "rate", 33}
$args{"h2_rst_rate_limit"}
Truth table100%2/2
A
A= $args{"max_concurrent_streams"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"initial_window_size"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"max_frame_size"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"enable_push"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"enable_connect_protocol"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"max_header_list_size"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"h2_rst_rate_limit"}
Aresult
11
00
108 }, $class;
109  
110 245 return $self;
111 }
112  
113 =head2 create_session
114  
115 my $session = $proto->create_session(
116 on_request => sub { ($stream_id, $pseudo, $headers, $has_body) = @_ },
117 on_body => sub { ($stream_id, $data, $eof) = @_ },
118 on_close => sub { ($stream_id, $error_code) = @_ },
119 on_header_overflow => sub { ($stream_id) = @_ },
120 );
121  
122 Creates a new HTTP/2 session for a connection. Returns a
123 L<PAGI::Server::Protocol::HTTP2::Session> wrapper.
124  
125 C<on_header_overflow> is optional. It fires instead of C<on_request> when a
126 request's HEADERS block exceeds C<max_header_list_size> (RFC 9113 section
127 10.5.1): the caller is expected to submit a 431 response on the given
128 stream id (no request state was ever dispatched, so there is nothing to
129 clean up on the caller's side). If omitted, an oversized request is simply
130 never dispatched and no response is sent for it.
131  
132 =cut
133  
134 221 ▶sub create_session {
Subroutine create_session: called CC=2 SCAR=7.1
Pod documented
135 221 my ($self, %callbacks) = @_;
136  
137 221 ▶ die "HTTP/2 not available (nghttp2 not installed)\n" unless $AVAILABLE;
Branch: 1 branch
BranchTrueFalseTotal
unless $PAGI::Server::Protocol::HTTP2::AVAILABLE 0 221 221
138  
139 return PAGI::Server::Protocol::HTTP2::Session->new(
140 protocol => $self,
141 on_request => $callbacks{on_request},
142 on_body => $callbacks{on_body},
143 on_close => $callbacks{on_close},
144 on_header_overflow => $callbacks{on_header_overflow},
145 settings => {
146 max_concurrent_streams => $self->{max_concurrent_streams},
147 initial_window_size => $self->{initial_window_size},
148 max_frame_size => $self->{max_frame_size},
149 enable_push => $self->{enable_push},
150 enable_connect_protocol => $self->{enable_connect_protocol},
151 max_header_list_size => $self->{max_header_list_size},
152 },
153 h2_rst_rate_limit => $self->{h2_rst_rate_limit},
154 221 );
155 }
156  
157 # =============================================================================
158 # HTTP/2 Session Wrapper
159 # =============================================================================
160  
161 package PAGI::Server::Protocol::HTTP2::Session;
162 155 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
163 155 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
164 155 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
165  
166 155 our $VERSION = '0.002013';
167  
168 221 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
169 221 my ($class, %args) = @_;
170  
171 my $self = bless {
172 protocol => $args{protocol},
173 on_request => $args{on_request},
174 on_body => $args{on_body},
175 on_close => $args{on_close},
176 on_header_overflow => $args{on_header_overflow},
177 settings => $args{settings},
178 h2_rst_rate_limit => $args{h2_rst_rate_limit},
179 221 streams => {}, # stream_id => { headers => [], pseudo => {}, ... }
180 nghttp2 => undef,
181 }, $class;
182  
183 221 weaken($self->{protocol});
184  
185 221 $self->_init_nghttp2_session;
186  
187 221 return $self;
188 }
189  
190 221 ▶sub _init_nghttp2_session {
Subroutine _init_nghttp2_session: called CC=2 SCAR=7.1
191 221 my ($self) = @_;
192  
193 221 my $weak_self = $self;
194 221 weaken($weak_self);
195  
196 221 my $rl = $self->{h2_rst_rate_limit};
197  
198 $self->{nghttp2} = Net::HTTP2::nghttp2::Session->new_server(
199 callbacks => {
200 232 ▶ on_begin_headers => sub {
Subroutine __ANON__: called CC=5 SCAR=16.7
201 232 my ($stream_id, $type, $flags) = @_;
202 232 ▶ return 0 unless $weak_self;
Branch: 1 branch
BranchTrueFalseTotal
unless $weak_self 0 232 232
203  
204 # HEADERS frame: the first block on a stream establishes the
205 # request accumulator (as before). A LATER HEADERS block on
206 # an already-established stream (request trailers, RFC 9113
207 # section 8.1) must never reinitialize the committed
208 # request -- it accumulates into a separate {pending}
209 # sub-block instead, which on_frame_recv classifies by
210 # $frame->{headers_category} once the block is complete.
211 232 ▶ if (!defined $type || $type == Net::HTTP2::nghttp2::NGHTTP2_HEADERS()) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $type or $type == Net::HTTP2::nghttp2::NGHTTP2_HEADERS()) 232 0 232
Condition33%1/3
not defined $type or $type == Net::HTTP2::nghttp2::NGHTTP2_HEADERS()
l!l&&r!l&&!r
02320
MC/DC0%0/2
not defined $type or $type == Net::HTTP2::nghttp2::NGHTTP2_HEADERS()
not defined $type $type == Net::HTTP2::nghttp2::NGHTTP2_HEADERS()
Truth table33%1/3
not(A) or B
A= not defined $typeB= $type == Net::HTTP2::nghttp2::NGHTTP2_HEADERS()
ABresult
1X1
011
000
212 232 my $existing = $weak_self->{streams}{$stream_id};
213 232 ▶ if ($existing) {
Branch: 1 branch
BranchTrueFalseTotal
if ($existing) { } 5 227 232
214 $existing->{pending} = {
215 5 headers => [],
216 pseudo => {},
217 header_list_size => 0,
218 };
219 } else {
220 227 $weak_self->{streams}{$stream_id} = {
221 headers => [],
222 pseudo => {},
223 header_list_size => 0,
224 };
225 }
226 }
227 232 return 0;
228 },
229  
230 1084 ▶ on_header => sub {
Subroutine __ANON__: called CC=9 SCAR=22.3
231 1084 my ($stream_id, $name, $value, $flags) = @_;
232 1084 ▶ return 0 unless $weak_self;
Branch: 1 branch
BranchTrueFalseTotal
unless $weak_self 0 1084 1084
233  
234 1084 my $stream = $weak_self->{streams}{$stream_id};
235 1084 ▶ return 0 unless $stream;
Branch: 1 branch
BranchTrueFalseTotal
unless $stream 0 1084 1084
236  
237 # Accumulate into the pending (trailer) block when one is
238 # in progress; otherwise into the main (request) block.
239 1084 ▶ my $block = $stream->{pending} || $stream;
Condition66%2/3
$stream->{'pending'} || $stream
l!l&&r!l&&!r
610780
MC/DC0%0/2
$stream->{'pending'} || $stream
$stream->{'pending'} $stream
Truth table66%2/3
A || B
A= $stream->{'pending'}B= $stream
ABresult
1X1
011
000
240  
241 # Once the REQUEST block has been marked oversized (below),
242 # discard further fields for it instead of accumulating:
243 # HPACK decoding keeps running (nghttp2 already decoded and
244 # handed us this field), so dynamic-table state stays
245 # consistent, but PAGI stops storing fields for a block it
246 # will never dispatch. The overflow itself is reported as a
247 # 431 once the HEADERS frame commits (on_frame_recv), not
248 # here.
249 1084 ▶ if (!$stream->{pending} && $stream->{oversized}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $stream->{"pending"} and $stream->{"oversized"}) 0 1084 1084
Condition66%2/3
not $stream->{"pending"} and $stream->{"oversized"}
!ll&&!rl&&r
610780
MC/DC0%0/2
not $stream->{"pending"} and $stream->{"oversized"}
not $stream->{"pending"} $stream->{"oversized"}
Truth table66%2/3
not(A) and B
A= not $stream->{"pending"}B= $stream->{"oversized"}
ABresult
0X0
100
111
250 0 return 0;
251 }
252  
253 # RFC 7541: header entry size = name_len + value_len + 32
254 1084 $block->{header_list_size} += length($name) + length($value) + 32;
255 1084 ▶ if ($block->{header_list_size} > $weak_self->{settings}{max_header_list_size}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block->{"header_list_size"} > $weak_self->{"settings"}{"max_header_list_size"}) 4 1080 1084
256 4 ▶ if ($stream->{pending}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stream->{"pending"}) 1 3 4
257 # Trailer block overflow: unchanged behavior --
258 # PAGI defines no request-trailer error path, so
259 # abort the stream outright.
260 1 delete $stream->{pending};
261 1 return Net::HTTP2::nghttp2::NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE();
262 }
263 # REQUEST block overflow (RFC 9113 section 10.5.1 / Go
264 # net/http2 style): do NOT abort the callback -- a bare
265 # abort here turns into a client-visible RST_STREAM
266 # with no :status at all. Let HPACK decoding finish
267 # instead, and mark the stream for a real 431 response
268 # (synthesized by on_frame_recv once the HEADERS frame
269 # commits) rather than dispatching it.
270 3 $stream->{oversized} = 1;
271 3 return 0;
272 }
273  
274 # Pseudo-headers start with ':'
275 1080 ▶ if ($name =~ /^:/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^:/) { } 958 122 1080
276 958 $block->{pseudo}{$name} = $value;
277 } else {
278 122 push @{$block->{headers}}, [$name, $value];
279 }
280 1080 return 0;
281 },
282  
283 863 ▶ on_frame_recv => sub {
Subroutine __ANON__: called CC=30 SCAR=35.6
284 863 my ($frame) = @_;
285 863 ▶ return 0 unless $weak_self;
Branch: 1 branch
BranchTrueFalseTotal
unless $weak_self 0 863 863
286  
287 863 my $stream_id = $frame->{stream_id};
288 863 my $type = $frame->{type};
289 863 my $flags = $frame->{flags};
290  
291 # HEADERS frame = a request headers block completed
292 863 ▶ if ($type == Net::HTTP2::nghttp2::NGHTTP2_HEADERS()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == Net::HTTP2::nghttp2::NGHTTP2_HEADERS()) 230 633 863
293 230 my $stream = $weak_self->{streams}{$stream_id};
294  
295 # Reject HEADERS on a stream where client already sent END_STREAM
296 230 ▶ if ($stream && $stream->{client_end_stream}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stream and $stream->{"client_end_stream"}) 0 230 230
Condition33%1/3
$stream and $stream->{"client_end_stream"}
!ll&&!rl&&r
02300
MC/DC0%0/2
$stream and $stream->{"client_end_stream"}
$stream $stream->{"client_end_stream"}
Truth table33%1/3
A and B
A= $streamB= $stream->{"client_end_stream"}
ABresult
0X0
100
111
297 0 delete $stream->{pending};
298 0 return Net::HTTP2::nghttp2::NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE();
299 }
300  
301 230 my $category = $frame->{headers_category};
302  
303 # NGHTTP2_HCAT_HEADERS on an already-established stream
304 # is a later ordinary HEADERS block -- request trailers.
305 # The initial request is already committed on $stream;
306 # this block was accumulated into {pending} by
307 # on_begin_headers/on_header above.
308 230 ▶ if ($stream
Branch: 1 branch
BranchTrueFalseTotal
if ($stream and defined $category and $category == Net::HTTP2::nghttp2::NGHTTP2_HCAT_HEADERS()) 3 227 230
Condition50%3/6
$stream and defined $category and $category == Net::HTTP2::nghttp2::NGHTTP2_HCAT_HEADERS()
!ll&&!rl&&r
02273
$stream and defined $category
!ll&&!rl&&r
00230
MC/DC33%1/3
$stream and defined $category and $category == Net::HTTP2::nghttp2::NGHTTP2_HCAT_HEADERS()
$stream defined $category $category == Net::HTTP2::nghttp2::NGHTTP2_HCAT_HEADERS()
Truth table50%2/4
A and B and C
A= $streamB= defined $categoryC= $category == Net::HTTP2::nghttp2::NGHTTP2_HCAT_HEADERS()
ABCresult
0XX0
10X0
1100
1111
309 && defined $category
310 && $category == Net::HTTP2::nghttp2::NGHTTP2_HCAT_HEADERS()) {
311  
312 3 my $trailer = delete $stream->{pending};
313 3 ▶ $trailer //= { headers => [], pseudo => {}, header_list_size => 0 };
Condition50%1/2
$trailer //= {"headers", [], "pseudo", {}, "header_list_size", 0}
l!l
30
MC/DC0%0/1
$trailer //= {"headers", [], "pseudo", {}, "header_list_size", 0}
$trailer
Truth table50%1/2
A
A= $trailer
Aresult
11
00
314  
315 # RFC 9113 section 8.1: pseudo-header fields are
316 # forbidden in trailers -- malformed, close the stream.
317 3 ▶ if (%{$trailer->{pseudo}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (%{$trailer->{"pseudo"};}) 0 3 3
318 0 return Net::HTTP2::nghttp2::NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE();
319 }
320  
321 # PAGI defines no request-trailer receive event yet
322 # (tracked separately) -- discard the fields.
323  
324 3 my $end_stream = $flags & Net::HTTP2::nghttp2::NGHTTP2_FLAG_END_STREAM();
325 3 ▶ if ($end_stream) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end_stream) 3 0 3
326 3 $stream->{client_end_stream} = 1;
327 # Deliver body completion exactly as the
328 # DATA+END_STREAM arm below does: END_STREAM rode
329 # the trailer HEADERS, not DATA, so the original
330 # request's body_complete must still fire.
331 3 ▶ if ($weak_self->{on_body}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($weak_self->{"on_body"}) 3 0 3
332 3 $weak_self->{on_body}->($stream_id, '', 1);
333 }
334 }
335  
336 3 return 0;
337 }
338  
339 # REQUEST header block exceeded max_header_list_size
340 # (marked by on_header above): do not dispatch. Instead
341 # of deleting the stream entry outright, leave it in
342 # place -- on_begin_headers still needs it to classify
343 # a later trailer HEADERS block as {pending} rather
344 # than a second request, and on_stream_close still
345 # needs it for its ordinary cleanup. Only the
346 # partially-accumulated fields are dropped here to
347 # bound memory.
348 227 ▶ if ($stream && $stream->{oversized}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stream and $stream->{"oversized"}) 3 224 227
Condition66%2/3
$stream and $stream->{"oversized"}
!ll&&!rl&&r
02243
MC/DC50%1/2
$stream and $stream->{"oversized"}
$stream $stream->{"oversized"}
Truth table66%2/3
A and B
A= $streamB= $stream->{"oversized"}
ABresult
0X0
100
111
349 3 my $end_stream = $flags & Net::HTTP2::nghttp2::NGHTTP2_FLAG_END_STREAM();
350 3 ▶ $stream->{client_end_stream} = 1 if $end_stream;
Branch: 1 branch
BranchTrueFalseTotal
if $end_stream 2 1 3
351 3 $stream->{headers} = [];
352 3 $stream->{pseudo} = {};
353  
354 3 ▶ if ($weak_self->{on_header_overflow}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($weak_self->{"on_header_overflow"}) 3 0 3
355 3 $weak_self->{on_header_overflow}->($stream_id);
356 }
357 3 return 0;
358 }
359  
360 224 ▶ if ($stream && $weak_self->{on_request}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stream and $weak_self->{"on_request"}) 224 0 224
Condition33%1/3
$stream and $weak_self->{"on_request"}
!ll&&!rl&&r
00224
MC/DC0%0/2
$stream and $weak_self->{"on_request"}
$stream $weak_self->{"on_request"}
Truth table33%1/3
A and B
A= $streamB= $weak_self->{"on_request"}
ABresult
0X0
100
111
361 224 my $headers = $stream->{headers};
362 224 my $pseudo = $stream->{pseudo};
363  
364 # Convert :authority pseudo-header to host header
365 # (RFC 9113 Section 8.3.1: :authority takes precedence)
366 224 ▶ if (defined $pseudo->{':authority'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pseudo->{":authority"}) 224 0 224
367 224 my $authority = $pseudo->{':authority'};
368 224 my $found_host = 0;
369 224 for my $h (@$headers) {
370 117 ▶ if ($h->[0] eq 'host') {
Branch: 1 branch
BranchTrueFalseTotal
if ($h->[0] eq "host") 1 116 117
371 1 $h->[1] = $authority;
372 1 $found_host = 1;
373 1 last;
374 }
375 }
376 224 ▶ push @$headers, ['host', $authority] unless $found_host;
Branch: 1 branch
BranchTrueFalseTotal
unless $found_host 223 1 224
377 }
378  
379 # Normalize multiple cookie headers into one
380 # (matches HTTP/1.1 behavior in HTTP1.pm)
381 224 my @cookie_values;
382 my @non_cookie;
383 224 for my $h (@$headers) {
384 340 ▶ if ($h->[0] eq 'cookie') {
Branch: 1 branch
BranchTrueFalseTotal
if ($h->[0] eq 'cookie') { } 2 338 340
385 2 push @cookie_values, $h->[1];
386 } else {
387 338 push @non_cookie, $h;
388 }
389 }
390 224 ▶ if (@cookie_values > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@cookie_values > 1) 1 223 224
391 1 push @non_cookie, ['cookie', join('; ', @cookie_values)];
392 1 @$headers = @non_cookie;
393 }
394  
395 224 my $end_stream = $flags & Net::HTTP2::nghttp2::NGHTTP2_FLAG_END_STREAM();
396  
397 # Track that client has finished sending on this stream
398 224 ▶ if ($end_stream) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end_stream) 153 71 224
399 153 $stream->{client_end_stream} = 1;
400 }
401  
402 224 $weak_self->{on_request}->(
403 $stream_id,
404 $pseudo,
405 $headers,
406 !$end_stream, # has_body = not END_STREAM
407 );
408 }
409 }
410  
411 # DATA frame with END_STREAM = body complete
412 857 ▶ if ($type == Net::HTTP2::nghttp2::NGHTTP2_DATA()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == Net::HTTP2::nghttp2::NGHTTP2_DATA()) 71 786 857
413 71 my $end_stream = $flags & Net::HTTP2::nghttp2::NGHTTP2_FLAG_END_STREAM();
414 71 ▶ if ($end_stream) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end_stream) 10 61 71
415 10 my $stream = $weak_self->{streams}{$stream_id};
416 10 ▶ $stream->{client_end_stream} = 1 if $stream;
Branch: 1 branch
BranchTrueFalseTotal
if $stream 10 0 10
417 10 ▶ if ($weak_self->{on_body}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($weak_self->{"on_body"}) 10 0 10
418 10 $weak_self->{on_body}->($stream_id, '', 1);
419 }
420 }
421 }
422  
423 857 return 0;
424 },
425  
426 74 ▶ on_data_chunk_recv => sub {
Subroutine __ANON__: called CC=5 SCAR=18.1
427 74 my ($stream_id, $data, $flags) = @_;
428 74 ▶ return 0 unless $weak_self;
Branch: 1 branch
BranchTrueFalseTotal
unless $weak_self 0 74 74
429  
430 # Reject DATA on a stream where client already sent END_STREAM
431 74 my $stream = $weak_self->{streams}{$stream_id};
432 74 ▶ if ($stream && $stream->{client_end_stream}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stream and $stream->{"client_end_stream"}) 0 74 74
Condition33%1/3
$stream and $stream->{"client_end_stream"}
!ll&&!rl&&r
0740
MC/DC0%0/2
$stream and $stream->{"client_end_stream"}
$stream $stream->{"client_end_stream"}
Truth table33%1/3
A and B
A= $streamB= $stream->{"client_end_stream"}
ABresult
0X0
100
111
433 0 return Net::HTTP2::nghttp2::NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE();
434 }
435  
436 74 ▶ if ($weak_self->{on_body}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($weak_self->{"on_body"}) 74 0 74
437 # END_STREAM comes in frame_recv, not here
438 74 $weak_self->{on_body}->($stream_id, $data, 0);
439 }
440 73 return 0;
441 },
442  
443 148 ▶ on_stream_close => sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
444 148 my ($stream_id, $error_code) = @_;
445 148 ▶ return 0 unless $weak_self;
Branch: 1 branch
BranchTrueFalseTotal
unless $weak_self 0 148 148
446  
447 148 ▶ if ($weak_self->{on_close}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($weak_self->{"on_close"}) 148 0 148
448 148 $weak_self->{on_close}->($stream_id, $error_code);
449 }
450  
451 # Clean up stream state
452 148 delete $weak_self->{streams}{$stream_id};
453 148 return 0;
454 },
455 },
456 (defined $rl
457 ? (stream_reset_burst => $rl->{burst}, stream_reset_rate => $rl->{rate})
458 221 ▶ : ()),
Branch: 1 branch
BranchTrueFalseTotal
defined $rl ? : 221 0 221
459 );
460  
461 # Send initial SETTINGS
462 221 $self->{nghttp2}->send_connection_preface(%{$self->{settings}});
463 }
464  
465 =head2 feed
466  
467 my $consumed = $session->feed($data);
468  
469 Feed incoming data to the HTTP/2 session. Returns bytes consumed.
470  
471 =cut
472  
473 780 ▶sub feed {
Subroutine feed: called CC=1 SCAR=0.0
474 780 my ($self, $data) = @_;
475 780 return $self->{nghttp2}->mem_recv($data);
476 }
477  
478 =head2 extract
479  
480 my $data = $session->extract;
481  
482 Extract outgoing data from the session. Returns bytes to send.
483  
484 =cut
485  
486 2387 ▶sub extract {
Subroutine extract: called CC=1 SCAR=0.0
487 2387 my ($self) = @_;
488 2387 return $self->{nghttp2}->mem_send;
489 }
490  
491 =head2 want_read
492  
493 if ($session->want_read) { ... }
494  
495 Check if session wants to read.
496  
497 =cut
498  
499 740 ▶sub want_read {
Subroutine want_read: called CC=1 SCAR=0.0
500 740 my ($self) = @_;
501 740 return $self->{nghttp2}->want_read;
502 }
503  
504 =head2 want_write
505  
506 if ($session->want_write) { ... }
507  
508 Check if session has data to write.
509  
510 =cut
511  
512 1 ▶sub want_write {
Subroutine want_write: called CC=1 SCAR=0.0
513 1 my ($self) = @_;
514 1 return $self->{nghttp2}->want_write;
515 }
516  
517 =head2 submit_response
518  
519 $session->submit_response($stream_id,
520 status => 200,
521 headers => [['content-type', 'text/html']],
522 body => $body,
523 );
524  
525 Submit a response on a stream. C<body> can be a string (sent as single
526 response) or a coderef for streaming.
527  
528 =cut
529  
530 103 ▶sub submit_response {
Subroutine submit_response: called CC=1 SCAR=0.0
531 103 my ($self, $stream_id, %args) = @_;
532 103 return $self->{nghttp2}->submit_response($stream_id, %args);
533 }
534  
535 =head2 submit_response_streaming
536  
537 $session->submit_response_streaming($stream_id,
538 status => 200,
539 headers => [['content-type', 'text/event-stream']],
540 data_callback => sub {
541 my ($stream_id, $max_len) = @_;
542 return ($chunk, $is_eof);
543 },
544 );
545  
546 Submit a streaming response with a data provider callback.
547  
548 =cut
549  
550 114 ▶sub submit_response_streaming {
Subroutine submit_response_streaming: called CC=1 SCAR=0.0
551 114 my ($self, $stream_id, %args) = @_;
552 return $self->{nghttp2}->submit_response($stream_id,
553 status => $args{status},
554 headers => $args{headers},
555 data_callback => $args{data_callback},
556 callback_data => $args{callback_data},
557 114 );
558 }
559  
560 =head2 resume_stream
561  
562 $session->resume_stream($stream_id);
563  
564 Resume a deferred stream after data becomes available.
565  
566 =cut
567  
568 285 ▶sub resume_stream {
Subroutine resume_stream: called CC=1 SCAR=0.0
569 285 my ($self, $stream_id) = @_;
570 285 return $self->{nghttp2}->resume_stream($stream_id);
571 }
572  
573 =head2 submit_trailer
574  
575 $session->submit_trailer($stream_id, headers => [['x-checksum', 'abc']]);
576  
577 Submit the trailing HEADERS block for a response (design section 8.3):
578 queues C<NGHTTP2_FLAG_END_STREAM> on a HEADERS frame after any already-queued
579 DATA, completing a response whose data provider reserved END_STREAM for the
580 trailers (a three-value C<($chunk, $eof, $no_end_stream)> data-callback
581 return). C<headers> defaults to C<[]> for a declared-but-empty trailer
582 block. Throws (via the underlying C<Net::HTTP2::nghttp2::Session>) on a
583 malformed tuple or a pseudo-header name. An unknown or already-gone stream
584 id does not throw -- it returns a falsy nghttp2 status; C<stream_id == 0> is
585 the one id nghttp2 rejects with an immediate croak (invalid argument).
586  
587 =cut
588  
589 6 ▶sub submit_trailer {
Subroutine submit_trailer: called CC=2 SCAR=7.2
590 6 my ($self, $stream_id, %args) = @_;
591 6 ▶ return $self->{nghttp2}->submit_trailer($stream_id, headers => $args{headers} // []);
Condition50%1/2
$args{"headers"} // []
l!l
60
MC/DC0%0/1
$args{"headers"} // []
$args{"headers"}
Truth table50%1/2
A
A= $args{"headers"}
Aresult
11
00
592 }
593  
594 =head2 submit_data
595  
596 $session->submit_data($stream_id, $data, $eof);
597  
598 Push data directly onto a stream. Used for WebSocket frame delivery
599 over HTTP/2 where frames are sent as DATA payloads.
600  
601 =cut
602  
603 0 ▶sub submit_data {
Subroutine submit_data: not called CC=1 SCAR=6.9
604 0 my ($self, $stream_id, $data, $eof) = @_;
605 0 return $self->{nghttp2}->submit_data($stream_id, $data, $eof);
606 }
607  
608 =head2 terminate
609  
610 $session->terminate($error_code);
611  
612 Terminate the session with GOAWAY.
613  
614 =cut
615  
616 205 ▶sub terminate {
Subroutine terminate: called CC=2 SCAR=7.1
617 205 my ($self, $error_code) = @_;
618 205 ▶ $error_code //= 0; # NO_ERROR
Condition50%1/2
$error_code //= 0
l!l
2050
MC/DC0%0/1
$error_code //= 0
$error_code
Truth table50%1/2
A
A= $error_code
Aresult
11
00
619 205 return $self->{nghttp2}->terminate_session($error_code);
620 }
621  
622 =head2 submit_rst_stream
623  
624 $session->submit_rst_stream($stream_id, $error_code);
625  
626 Reset a single stream with RST_STREAM, leaving the rest of the session
627 untouched. C<$error_code> is an RFC 9113 section 7 error code (for example
628 C<2>, INTERNAL_ERROR).
629  
630 Used by the server's HTTP/2 dispatch to abort a stream whose response was
631 left started but incomplete -- the application returned early or threw after
632 C<http.response.start>, so there is no honest way to finish the body.
633  
634 =cut
635  
636 8 ▶sub submit_rst_stream {
Subroutine submit_rst_stream: called CC=1 SCAR=0.0
637 8 my ($self, $stream_id, $error_code) = @_;
638 8 return $self->{nghttp2}->submit_rst_stream($stream_id, $error_code);
639 }
640  
641 155 1;
642  
643 __END__
« blib/lib/PAGI/Server/Runner.pm ↑ Summary blib/lib/PAGI/Server/EventValidator.pm »