| 1 | package Linux::Event::HTTP::_HTTP2::Server; | ||
| 2 | use v5.36; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Scalar::Util qw(blessed refaddr weaken); | ||
| 7 | |||
| 8 | use Linux::Event::HTTP::_HTTP2; | ||
| 9 | |||
| 10 | our $VERSION = '0.003'; | ||
| 11 | |||
| 12 | use constant { | ||
| 13 | H2_DATA => 0, | ||
| 14 | H2_HEADERS => 1, | ||
| 15 | H2_GOAWAY => 7, | ||
| 16 | H2_END_STREAM => 0x1, | ||
| 17 | H2_INTERNAL_ERROR => 2, | ||
| 18 | H2_CANCEL => 8, | ||
| 19 | H2_ENHANCE_YOUR_CALM => 11, | ||
| 20 | }; | ||
| 21 | |||
| 22 | my $BODY_HIGH_WATER = 65_536; | ||
| 23 | my $BODY_LOW_WATER = 32_768; | ||
| 24 | |||
| 25 | sub new ($class, %option) { | ||
| 26 | my $stream = delete $option{stream}; | ||
| 27 | die 'new(): stream must be an object with write()' | ||
| 28 | if !blessed($stream) || !$stream->can('write'); | ||
| 29 | |||
| 30 | my $connection = delete $option{connection}; | ||
| 31 | my $autostart = exists($option{autostart}) | ||
| 32 | ? delete($option{autostart}) : 1; | ||
| 33 | die 'new(): autostart must be zero or one' | ||
| 34 | if !defined($autostart) || ref($autostart) | ||
| 35 | || "$autostart" !~ /\A[01]\z/; | ||
| 36 | |||
| 37 | die 'new(): connection must be an object' | ||
| 38 | if defined($connection) && !blessed($connection); | ||
| 39 | |||
| 40 | my %callback; | ||
| 41 | for my $name (qw(on_request on_body on_request_end on_error)) { | ||
| 42 | next if !exists $option{$name}; | ||
| 43 | my $cb = delete $option{$name}; | ||
| 44 | die "new(): $name must be a coderef" | ||
| 45 | if defined($cb) && ref($cb) ne 'CODE'; | ||
| 46 | $callback{$name} = $cb if $cb; | ||
| 47 | } | ||
| 48 | |||
| 49 | my $max_concurrent_streams = | ||
| 50 | delete($option{max_concurrent_streams}) // 100; | ||
| 51 | my $max_header_list_size = | ||
| 52 | delete($option{max_header_list_size}) // 65_536; | ||
| 53 | die 'new(): max_concurrent_streams must be a positive integer' | ||
| 54 | if ref($max_concurrent_streams) | ||
| 55 | || "$max_concurrent_streams" !~ /\A[0-9]+\z/ | ||
| 56 | || $max_concurrent_streams < 1; | ||
| 57 | die 'new(): max_header_list_size must be a positive integer' | ||
| 58 | if ref($max_header_list_size) | ||
| 59 | || "$max_header_list_size" !~ /\A[0-9]+\z/ | ||
| 60 | || $max_header_list_size < 1; | ||
| 61 | |||
| 62 | die 'new(): unknown options: ' . join(', ', sort keys %option) | ||
| 63 | if %option; | ||
| 64 | |||
| 65 | require Net::HTTP2::nghttp2; | ||
| 66 | Net::HTTP2::nghttp2->VERSION('0.011'); | ||
| 67 | require Net::HTTP2::nghttp2::Session; | ||
| 68 | die 'new(): nghttp2 library is unavailable' | ||
| 69 | if !Net::HTTP2::nghttp2->available; | ||
| 70 | |||
| 71 | my $self = bless { | ||
| 72 | stream => $stream, | ||
| 73 | connection => $connection, | ||
| 74 | callback => \%callback, | ||
| 75 | session => undef, | ||
| 76 | streams => {}, | ||
| 77 | tx_stream => {}, | ||
| 78 | current_tx => undef, | ||
| 79 | in_session_call => 0, | ||
| 80 | closing => 0, | ||
| 81 | close_pending => 0, | ||
| 82 | transport_blocked => 0, | ||
| 83 | transport_ending => 0, | ||
| 84 | closed => 0, | ||
| 85 | started => 0, | ||
| 86 | }, $class; | ||
| 87 | |||
| 88 | my $weak = $self; | ||
| 89 | weaken($weak); | ||
| 90 | |||
| 91 | my $session = Net::HTTP2::nghttp2::Session->new_server( | ||
| 92 | callbacks => { | ||
| 93 | on_begin_headers => sub (@args) { | ||
| 94 | my $self = $weak or return 0; | ||
| 95 | return 0 if $self->{closing}; | ||
| 96 | return $self->_on_begin_headers(@args); | ||
| 97 | }, | ||
| 98 | on_header => sub (@args) { | ||
| 99 | my $self = $weak or return 0; | ||
| 100 | return 0 if $self->{closing}; | ||
| 101 | return $self->_on_header(@args); | ||
| 102 | }, | ||
| 103 | on_frame_recv => sub (@args) { | ||
| 104 | my $self = $weak or return 0; | ||
| 105 | return 0 if $self->{closing}; | ||
| 106 | return $self->_on_frame_recv(@args); | ||
| 107 | }, | ||
| 108 | on_data_chunk_recv => sub (@args) { | ||
| 109 | my $self = $weak or return 0; | ||
| 110 | return 0 if $self->{closing}; | ||
| 111 | return $self->_on_data_chunk_recv(@args); | ||
| 112 | }, | ||
| 113 | on_stream_close => sub (@args) { | ||
| 114 | my $self = $weak or return 0; | ||
| 115 | return 0 if $self->{closing}; | ||
| 116 | return $self->_on_stream_close(@args); | ||
| 117 | }, | ||
| 118 | on_error => sub (@args) { | ||
| 119 | my $self = $weak or return 0; | ||
| 120 | return 0 if $self->{closing}; | ||
| 121 | return $self->_on_session_error(@args); | ||
| 122 | }, | ||
| 123 | }, | ||
| 124 | ); | ||
| 125 | |||
| 126 | $self->{session} = $session; | ||
| 127 | $self->{max_concurrent_streams} = 0 + $max_concurrent_streams; | ||
| 128 | $self->{max_header_list_size} = 0 + $max_header_list_size; | ||
| 129 | $self->start if $autostart; | ||
| 130 | return $self; | ||
| 131 | } | ||
| 132 | |||
| 133 | sub start ($self) { | ||
| 134 | die 'start(): executor is closed' if $self->{closed}; | ||
| 135 | return $self if $self->{started}; | ||
| 136 | $self->{started} = 1; | ||
| 137 | $self->{session}->send_connection_preface( | ||
| 138 | max_concurrent_streams => $self->{max_concurrent_streams}, | ||
| 139 | max_header_list_size => $self->{max_header_list_size}, | ||
| 140 | ); | ||
| 141 | $self->flush; | ||
| 142 | return $self; | ||
| 143 | } | ||
| 144 | |||
| 145 | sub started ($self) { !!$self->{started} } | ||
| 146 | |||
| 147 | sub session ($self) { $self->{session} } | ||
| 148 | sub stream ($self) { $self->{stream} } | ||
| 149 | |||
| 150 | sub transaction ($self) { | ||
| 151 | return $self->{current_tx}; | ||
| 152 | } | ||
| 153 | |||
| 154 | sub transaction_for_stream ($self, $stream_id) { | ||
| 155 | my $state = $self->{streams}{$stream_id} or return undef; | ||
| 156 | return $state->{transaction}; | ||
| 157 | } | ||
| 158 | |||
| 159 | sub stream_count ($self) { | ||
| 160 | return scalar keys %{$self->{streams}}; | ||
| 161 | } | ||
| 162 | |||
| 163 | sub input ($self, $bytes) { | ||
| 164 | die 'input(): executor is closed' if $self->{closed}; | ||
| 165 | die 'input(): executor is not started' if !$self->{started}; | ||
| 166 | die 'input(): bytes must be a scalar' if ref $bytes; | ||
| 167 | return 0 if !defined($bytes) || $bytes eq ''; | ||
| 168 | |||
| 169 | my $consumed; | ||
| 170 | { | ||
| 171 | local $self->{in_session_call} = 1; | ||
| 172 | $consumed = $self->{session}->mem_recv($bytes); | ||
| 173 | } | ||
| 174 | die 'input(): nghttp2 did not consume complete input' | ||
| 175 | if !defined($consumed) || $consumed != length($bytes); | ||
| 176 | |||
| 177 | return $consumed if $self->_finish_pending_close; | ||
| 178 | |||
| 179 | $self->flush; | ||
| 180 | return $consumed; | ||
| 181 | } | ||
| 182 | |||
| 183 | sub flush ($self) { | ||
| 184 | return if $self->{closed} || $self->{closing}; | ||
| 185 | return if !$self->{started}; | ||
| 186 | return if $self->{in_session_call}; | ||
| 187 | return if $self->{transport_blocked}; | ||
| 188 | |||
| 189 | { | ||
| 190 | local $self->{in_session_call} = 1; | ||
| 191 | while ($self->{session}->want_write) { | ||
| 192 | my $bytes = $self->{session}->mem_send; | ||
| 193 | last if $self->{close_pending}; | ||
| 194 | last if !defined($bytes) || $bytes eq ''; | ||
| 195 | |||
| 196 | my $accepted = $self->{stream}->write($bytes); | ||
| 197 | if (!$accepted) { | ||
| 198 | $self->{transport_blocked} = 1; | ||
| 199 | last; | ||
| 200 | } | ||
| 201 | } | ||
| 202 | } | ||
| 203 | |||
| 204 | return if $self->_finish_pending_close; | ||
| 205 | |||
| 206 | $self->_maybe_end_transport; | ||
| 207 | return; | ||
| 208 | } | ||
| 209 | |||
| 210 | sub _maybe_end_transport ($self) { | ||
| 211 | return if $self->{closed} || $self->{transport_ending}; | ||
| 212 | return if $self->{transport_blocked}; | ||
| 213 | |||
| 214 | my $session = $self->{session} or return; | ||
| 215 | if ($self->{peer_goaway}) { | ||
| 216 | return if $self->stream_count; | ||
| 217 | } else { | ||
| 218 | return if $session->want_read || $session->want_write; | ||
| 219 | } | ||
| 220 | |||
| 221 | my $stream = $self->{stream} or return; | ||
| 222 | return if $stream->is_closed; | ||
| 223 | |||
| 224 | $self->{transport_ending} = 1; | ||
| 225 | $stream->end; | ||
| 226 | return; | ||
| 227 | } | ||
| 228 | |||
| 229 | sub transport_drain ($self) { | ||
| 230 | return if $self->{closed}; | ||
| 231 | $self->{transport_blocked} = 0; | ||
| 232 | $self->flush; | ||
| 233 | |||
| 234 | for my $state (values %{$self->{streams}}) { | ||
| 235 | my $provider = $state->{response_provider} or next; | ||
| 236 | next if length($provider->{queue}) >= $BODY_LOW_WATER; | ||
| 237 | my $tx = $state->{transaction} or next; | ||
| 238 | my $body = $tx->_response_body_object or next; | ||
| 239 | $body->_drain; | ||
| 240 | } | ||
| 241 | return; | ||
| 242 | } | ||
| 243 | |||
| 244 | sub close ($self) { | ||
| 245 | return if $self->{closed}; | ||
| 246 | |||
| 247 | if ($self->{in_session_call}) { | ||
| 248 | $self->{closing} = 1; | ||
| 249 | $self->{close_pending} = 1; | ||
| 250 | return; | ||
| 251 | } | ||
| 252 | |||
| 253 | return $self->_finish_close; | ||
| 254 | } | ||
| 255 | |||
| 256 | sub _finish_pending_close ($self) { | ||
| 257 | return 0 if !$self->{close_pending}; | ||
| 258 | $self->{close_pending} = 0; | ||
| 259 | $self->_finish_close; | ||
| 260 | return 1; | ||
| 261 | } | ||
| 262 | |||
| 263 | sub _finish_close ($self) { | ||
| 264 | return if $self->{closed}; | ||
| 265 | $self->{closed} = 1; | ||
| 266 | $self->{closing} = 0; | ||
| 267 | |||
| 268 | for my $state (values %{$self->{streams}}) { | ||
| 269 | my $tx = $state->{transaction} or next; | ||
| 270 | next if $tx->is_terminal; | ||
| 271 | $tx->_fail('HTTP/2 connection closed'); | ||
| 272 | } | ||
| 273 | |||
| 274 | $self->{streams} = {}; | ||
| 275 | $self->{tx_stream} = {}; | ||
| 276 | $self->{session} = undef; | ||
| 277 | return; | ||
| 278 | } | ||
| 279 | |||
| 280 | sub _state ($self, $stream_id) { | ||
| 281 | return $self->{streams}{$stream_id} //= { | ||
| 282 | header_block => [], | ||
| 283 | collecting => 'initial', | ||
| 284 | transaction => undef, | ||
| 285 | response_provider => undef, | ||
| 286 | request_end_called => 0, | ||
| 287 | }; | ||
| 288 | } | ||
| 289 | |||
| 290 | sub _on_begin_headers ($self, $stream_id, $frame_type, $flags) { | ||
| 291 | my $state = $self->_state($stream_id); | ||
| 292 | $state->{header_limit_exceeded} = 0; | ||
| 293 | $state->{header_list_size} = 0; | ||
| 294 | if ($state->{transaction}) { | ||
| 295 | $state->{trailer_block} = []; | ||
| 296 | $state->{collecting} = 'trailer'; | ||
| 297 | } else { | ||
| 298 | $state->{header_block} = []; | ||
| 299 | $state->{collecting} = 'initial'; | ||
| 300 | } | ||
| 301 | return 0; | ||
| 302 | } | ||
| 303 | |||
| 304 | sub _on_header ($self, $stream_id, $name, $value, $flags) { | ||
| 305 | my $state = $self->_state($stream_id); | ||
| 306 | return 0 if $state->{header_limit_exceeded}; | ||
| 307 | |||
| 308 | my $size = $state->{header_list_size} | ||
| 309 | + length($name) + length($value) + 32; | ||
| 310 | if ($size > $self->{max_header_list_size}) { | ||
| 311 | $state->{header_limit_exceeded} = 1; | ||
| 312 | $self->_stream_failure( | ||
| 313 | $stream_id, | ||
| 314 | 'HTTP/2 request header list exceeds configured limit', | ||
| 315 | H2_ENHANCE_YOUR_CALM, | ||
| 316 | ); | ||
| 317 | return 0; | ||
| 318 | } | ||
| 319 | $state->{header_list_size} = $size; | ||
| 320 | |||
| 321 | my $key = $state->{collecting} eq 'trailer' | ||
| 322 | ? 'trailer_block' | ||
| 323 | : 'header_block'; | ||
| 324 | $state->{$key} //= []; | ||
| 325 | push @{$state->{$key}}, [ $name, $value ]; | ||
| 326 | return 0; | ||
| 327 | } | ||
| 328 | |||
| 329 | sub _on_frame_recv ($self, $frame) { | ||
| 330 | if (($frame->{type} // -1) == H2_GOAWAY) { | ||
| 331 | # GOAWAY is a peer drain signal, not a reason to abort the transport. | ||
| 332 | # Keep reading until the peer closes so unread TLS/TCP bytes cannot | ||
| 333 | # turn an otherwise graceful shutdown into an RST. | ||
| 334 | $self->{peer_goaway} = 1; | ||
| 335 | return 0; | ||
| 336 | } | ||
| 337 | |||
| 338 | my $stream_id = $frame->{stream_id} // 0; | ||
| 339 | return 0 if !$stream_id; | ||
| 340 | |||
| 341 | if (($frame->{type} // -1) == H2_HEADERS) { | ||
| 342 | my $state = $self->_state($stream_id); | ||
| 343 | return 0 if $state->{header_limit_exceeded}; | ||
| 344 | |||
| 345 | if (!$state->{transaction}) { | ||
| 346 | my $ok = eval { | ||
| 347 | my $tx = Linux::Event::HTTP::_HTTP2 | ||
| 348 | ->server_transaction_from_headers( | ||
| 349 | $state->{header_block}, | ||
| 350 | $self, | ||
| 351 | end_stream => (($frame->{flags} // 0) & H2_END_STREAM) | ||
| 352 | ? 1 : 0, | ||
| 353 | ); | ||
| 354 | |||
| 355 | $state->{transaction} = $tx; | ||
| 356 | $self->{tx_stream}{refaddr($tx)} = $stream_id; | ||
| 357 | $self->_invoke('on_request', $stream_id, $tx); | ||
| 358 | $self->_maybe_auto_send($tx); | ||
| 359 | |||
| 360 | if (($frame->{flags} // 0) & H2_END_STREAM) { | ||
| 361 | $self->_request_end($stream_id, $tx); | ||
| 362 | } | ||
| 363 | 1; | ||
| 364 | }; | ||
| 365 | if (!$ok) { | ||
| 366 | $self->_stream_failure($stream_id, "$@"); | ||
| 367 | } | ||
| 368 | return 0; | ||
| 369 | } | ||
| 370 | |||
| 371 | # Request trailers are valid HTTP/2 but the public message API does not | ||
| 372 | # yet expose trailers. Preserve stream completion without folding them | ||
| 373 | # into the initial lossless field list. | ||
| 374 | if (($frame->{flags} // 0) & H2_END_STREAM) { | ||
| 375 | $self->_request_end($stream_id, $state->{transaction}); | ||
| 376 | } | ||
| 377 | return 0; | ||
| 378 | } | ||
| 379 | |||
| 380 | if (($frame->{type} // -1) == H2_DATA | ||
| 381 | && (($frame->{flags} // 0) & H2_END_STREAM)) { | ||
| 382 | my $state = $self->{streams}{$stream_id}; | ||
| 383 | if ($state && $state->{transaction}) { | ||
| 384 | $self->_request_end($stream_id, $state->{transaction}); | ||
| 385 | } | ||
| 386 | } | ||
| 387 | |||
| 388 | return 0; | ||
| 389 | } | ||
| 390 | |||
| 391 | sub _on_data_chunk_recv ($self, $stream_id, $data, $flags) { | ||
| 392 | my $state = $self->{streams}{$stream_id}; | ||
| 393 | return 0 if !$state || !$state->{transaction}; | ||
| 394 | |||
| 395 | my $tx = $state->{transaction}; | ||
| 396 | my $cb = $self->{callback}{on_body}; | ||
| 397 | if ($cb) { | ||
| 398 | my $ok = $self->_invoke('on_body', $stream_id, $tx, $data); | ||
| 399 | return 0 if !$ok; | ||
| 400 | } | ||
| 401 | |||
| 402 | $self->_maybe_auto_send($tx); | ||
| 403 | return 0; | ||
| 404 | } | ||
| 405 | |||
| 406 | sub _request_end ($self, $stream_id, $tx) { | ||
| 407 | my $state = $self->{streams}{$stream_id} or return; | ||
| 408 | return if $state->{request_end_called}++; | ||
| 409 | |||
| 410 | $tx->request->_mark_complete if !$tx->request->is_complete; | ||
| 411 | $self->_invoke('on_request_end', $stream_id, $tx) | ||
| 412 | if $self->{callback}{on_request_end}; | ||
| 413 | $self->_maybe_auto_send($tx); | ||
| 414 | return; | ||
| 415 | } | ||
| 416 | |||
| 417 | sub _invoke ($self, $name, $stream_id, $tx, @extra) { | ||
| 418 | my $cb = $self->{callback}{$name} or return 1; | ||
| 419 | my $connection = $self->{connection} // $self; | ||
| 420 | my $ok = eval { | ||
| 421 | local $self->{current_tx} = $tx; | ||
| 422 | if ($name eq 'on_body') { | ||
| 423 | $cb->($connection, $tx->request, $tx->response, $extra[0]); | ||
| 424 | } else { | ||
| 425 | $cb->($connection, $tx->request, $tx->response); | ||
| 426 | } | ||
| 427 | 1; | ||
| 428 | }; | ||
| 429 | if (!$ok) { | ||
| 430 | $self->_stream_failure($stream_id, "$@"); | ||
| 431 | return 0; | ||
| 432 | } | ||
| 433 | return 1; | ||
| 434 | } | ||
| 435 | |||
| 436 | sub _stream_failure ( | ||
| 437 | $self, $stream_id, $error, $code = H2_INTERNAL_ERROR, | ||
| 438 | ) { | ||
| 439 | $error = 'HTTP/2 stream failure' if !defined($error) || $error eq ''; | ||
| 440 | |||
| 441 | my $state = $self->{streams}{$stream_id}; | ||
| 442 | if ($state && (my $tx = $state->{transaction})) { | ||
| 443 | $tx->_fail($error) if !$tx->is_terminal; | ||
| 444 | } | ||
| 445 | |||
| 446 | eval { $self->{session}->submit_rst_stream($stream_id, $code) }; | ||
| 447 | |||
| 448 | if (my $cb = $self->{callback}{on_error}) { | ||
| 449 | eval { $cb->($self, $stream_id, $error) }; | ||
| 450 | } | ||
| 451 | return; | ||
| 452 | } | ||
| 453 | |||
| 454 | sub _on_session_error ($self, $lib_error_code, $message) { | ||
| 455 | if (my $cb = $self->{callback}{on_error}) { | ||
| 456 | eval { $cb->($self, 0, "$message") }; | ||
| 457 | } | ||
| 458 | return 0; | ||
| 459 | } | ||
| 460 | |||
| 461 | sub _on_stream_close ($self, $stream_id, $error_code) { | ||
| 462 | my $state = delete $self->{streams}{$stream_id}; | ||
| 463 | return 0 if !$state; | ||
| 464 | |||
| 465 | my $tx = $state->{transaction}; | ||
| 466 | return 0 if !$tx; | ||
| 467 | |||
| 468 | delete $self->{tx_stream}{refaddr($tx)}; | ||
| 469 | |||
| 470 | if (!$tx->is_terminal) { | ||
| 471 | if ($error_code) { | ||
| 472 | $tx->_fail("HTTP/2 stream closed with error $error_code"); | ||
| 473 | } else { | ||
| 474 | $tx->_mark_complete; | ||
| 475 | } | ||
| 476 | } | ||
| 477 | |||
| 478 | return 0; | ||
| 479 | } | ||
| 480 | |||
| 481 | sub _stream_id_for_tx ($self, $tx, $operation) { | ||
| 482 | my $stream_id = $self->{tx_stream}{refaddr($tx)}; | ||
| 483 | die "$operation: Transaction is not active on this HTTP/2 connection" | ||
| 484 | if !defined $stream_id; | ||
| 485 | return $stream_id; | ||
| 486 | } | ||
| 487 | |||
| 488 | sub _normal_response_headers ($self, $response) { | ||
| 489 | my $block = Linux::Event::HTTP::_HTTP2->response_headers($response); | ||
| 490 | return [ @$block[1 .. $#$block] ]; | ||
| 491 | } | ||
| 492 | |||
| 493 | sub _maybe_auto_send ($self, $tx) { | ||
| 494 | return if $tx->is_terminal || $tx->is_response_started; | ||
| 495 | my $response = $tx->response or return; | ||
| 496 | return if !$response->_has_scalar_body; | ||
| 497 | $self->_send_http_response($tx); | ||
| 498 | return; | ||
| 499 | } | ||
| 500 | |||
| 501 | sub _send_http_response ($self, $tx) { | ||
| 502 | my $stream_id = $self->_stream_id_for_tx( | ||
| 503 | $tx, '_send_http_response()', | ||
| 504 | ); | ||
| 505 | die '_send_http_response(): response output already started' | ||
| 506 | if $tx->is_response_started; | ||
| 507 | |||
| 508 | my $response = $tx->response | ||
| 509 | or die '_send_http_response(): Transaction has no Response'; | ||
| 510 | my $body = $response->_scalar_body; | ||
| 511 | die '_send_http_response(): Response has no complete scalar body' | ||
| 512 | if !defined $body; | ||
| 513 | |||
| 514 | my $headers = $self->_normal_response_headers($response); | ||
| 515 | $response->_commit; | ||
| 516 | $self->{session}->submit_response( | ||
| 517 | $stream_id, | ||
| 518 | status => $response->status, | ||
| 519 | headers => $headers, | ||
| 520 | body => $body, | ||
| 521 | ); | ||
| 522 | |||
| 523 | $tx->_mark_response_started; | ||
| 524 | $tx->_mark_response_output_complete; | ||
| 525 | $self->flush; | ||
| 526 | return 1; | ||
| 527 | } | ||
| 528 | |||
| 529 | sub _response_data_provider ($stream_id, $max_length, $provider) { | ||
| 530 | my $queue = $provider->{queue}; | ||
| 531 | |||
| 532 | if (!length($queue)) { | ||
| 533 | return ('', 1) if $provider->{eof}; | ||
| 534 | return; | ||
| 535 | } | ||
| 536 | |||
| 537 | my $take = length($queue) < $max_length | ||
| 538 | ? length($queue) | ||
| 539 | : $max_length; | ||
| 540 | my $chunk = substr($provider->{queue}, 0, $take, ''); | ||
| 541 | my $eof = $provider->{eof} && !length($provider->{queue}) ? 1 : 0; | ||
| 542 | |||
| 543 | if ($provider->{blocked} | ||
| 544 | && length($provider->{queue}) < $BODY_LOW_WATER) { | ||
| 545 | $provider->{blocked} = 0; | ||
| 546 | if (my $tx = $provider->{transaction}) { | ||
| 547 | if (my $body = $tx->_response_body_object) { | ||
| 548 | $body->_drain; | ||
| 549 | } | ||
| 550 | } | ||
| 551 | } | ||
| 552 | |||
| 553 | return ($chunk, $eof); | ||
| 554 | } | ||
| 555 | |||
| 556 | sub _start_response_provider ($self, $tx, $stream_id) { | ||
| 557 | my $response = $tx->response | ||
| 558 | or die 'response_body: Transaction has no Response'; | ||
| 559 | |||
| 560 | my $provider = { | ||
| 561 | queue => '', | ||
| 562 | eof => 0, | ||
| 563 | blocked => 0, | ||
| 564 | transaction => $tx, | ||
| 565 | }; | ||
| 566 | weaken($provider->{transaction}); | ||
| 567 | |||
| 568 | my $headers = $self->_normal_response_headers($response); | ||
| 569 | $response->_commit; | ||
| 570 | $self->{session}->submit_response( | ||
| 571 | $stream_id, | ||
| 572 | status => $response->status, | ||
| 573 | headers => $headers, | ||
| 574 | data_callback => \&_response_data_provider, | ||
| 575 | callback_data => $provider, | ||
| 576 | ); | ||
| 577 | |||
| 578 | my $state = $self->{streams}{$stream_id} | ||
| 579 | or die 'response_body: HTTP/2 stream state disappeared'; | ||
| 580 | $state->{response_provider} = $provider; | ||
| 581 | $tx->_mark_response_started; | ||
| 582 | return $provider; | ||
| 583 | } | ||
| 584 | |||
| 585 | sub _write_http_response_body ($self, $tx, $bytes, $final, $operation) { | ||
| 586 | my $stream_id = $self->_stream_id_for_tx($tx, "$operation()"); | ||
| 587 | die "$operation(): body must be a scalar" if ref $bytes; | ||
| 588 | $bytes = '' if !defined $bytes; | ||
| 589 | |||
| 590 | my $state = $self->{streams}{$stream_id} | ||
| 591 | or die "$operation(): HTTP/2 stream state disappeared"; | ||
| 592 | my $provider = $state->{response_provider} | ||
| 593 | // $self->_start_response_provider($tx, $stream_id); | ||
| 594 | |||
| 595 | $provider->{queue} .= $bytes; | ||
| 596 | $provider->{eof} = 1 if $final; | ||
| 597 | |||
| 598 | if ($self->{session}->is_stream_deferred($stream_id)) { | ||
| 599 | $self->{session}->resume_stream($stream_id); | ||
| 600 | } | ||
| 601 | |||
| 602 | if ($final) { | ||
| 603 | $tx->_mark_response_output_complete; | ||
| 604 | } | ||
| 605 | |||
| 606 | my $blocked = $self->{transport_blocked} | ||
| 607 | || length($provider->{queue}) >= $BODY_HIGH_WATER; | ||
| 608 | $provider->{blocked} = 1 if $blocked; | ||
| 609 | |||
| 610 | $self->flush; | ||
| 611 | return $blocked ? 0 : 1; | ||
| 612 | } | ||
| 613 | |||
| 614 | sub _write_http_request_body ($self, $tx, $bytes, $final, $operation) { | ||
| 615 | die "$operation(): server HTTP/2 executor cannot write a Request body"; | ||
| 616 | } | ||
| 617 | |||
| 618 | sub _cancel_http_transaction ($self, $tx) { | ||
| 619 | my $stream_id = $self->{tx_stream}{refaddr($tx)}; | ||
| 620 | return if !defined $stream_id; | ||
| 621 | |||
| 622 | $self->{session}->submit_rst_stream($stream_id, H2_CANCEL); | ||
| 623 | $tx->_mark_cancelled if !$tx->is_terminal; | ||
| 624 | $self->flush; | ||
| 625 | return; | ||
| 626 | } | ||
| 627 | |||
| 628 | sub _upgrade_http_transaction ($self, $tx, $target_class) { | ||
| 629 | die 'upgrade(): HTTP/1 whole-transport Upgrade is not available on HTTP/2'; | ||
| 630 | } | ||
| 631 | |||
| 632 | sub _tunnel_http_transaction ($self, $tx, $target_class) { | ||
| 633 | die 'tunnel(): HTTP/2 CONNECT requires a stream-level tunnel abstraction'; | ||
| 634 | } | ||
| 635 | |||
| 636 | 1; | ||
| 637 | |||
| 638 | __END__ |