| 1 | package Linux::Event::HTTP::_HTTP2::ClientSelector; | ||
| 2 | use v5.36; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Carp qw(croak); | ||
| 7 | use Scalar::Util qw(blessed refaddr weaken); | ||
| 8 | |||
| 9 | use Linux::Event::HTTP::Transaction; | ||
| 10 | |||
| 11 | our $VERSION = '0.003'; | ||
| 12 | |||
| 13 | my $BODY_HIGH_WATER = 65_536; | ||
| 14 | |||
| 15 | sub new ($class, %option) { | ||
| 16 | my $loop = delete $option{loop}; | ||
| 17 | my $host = delete $option{host}; | ||
| 18 | my $port = delete $option{port}; | ||
| 19 | my $transport = delete $option{transport}; | ||
| 20 | my $timeout = delete $option{timeout}; | ||
| 21 | my $scheme = delete($option{scheme}) // 'https'; | ||
| 22 | my $authority = delete $option{authority}; | ||
| 23 | my $on_selected = delete $option{on_selected}; | ||
| 24 | my $http1_connection_factory = | ||
| 25 | delete $option{http1_connection_factory}; | ||
| 26 | my $max_header_list_size = | ||
| 27 | delete($option{max_header_list_size}) // 65_536; | ||
| 28 | my $max_buffered_response_bytes = | ||
| 29 | delete($option{max_buffered_response_bytes}) // 67_108_864; | ||
| 30 | |||
| 31 | croak 'new(): loop is required' if !blessed($loop); | ||
| 32 | croak 'new(): host is required' | ||
| 33 | if !defined($host) || ref($host) || $host eq ''; | ||
| 34 | croak 'new(): port is required' | ||
| 35 | if !defined($port) || ref($port); | ||
| 36 | croak 'new(): transport is required' if !blessed($transport); | ||
| 37 | croak 'new(): authority is required' | ||
| 38 | if !defined($authority) || ref($authority) || $authority eq ''; | ||
| 39 | croak 'new(): on_selected must be a coderef' | ||
| 40 | if defined($on_selected) && ref($on_selected) ne 'CODE'; | ||
| 41 | croak 'new(): http1_connection_factory must be a coderef' | ||
| 42 | if defined($http1_connection_factory) | ||
| 43 | && ref($http1_connection_factory) ne 'CODE'; | ||
| 44 | croak 'new(): max_header_list_size must be a positive integer' | ||
| 45 | if ref($max_header_list_size) | ||
| 46 | || "$max_header_list_size" !~ /\A[0-9]+\z/ | ||
| 47 | || $max_header_list_size < 1; | ||
| 48 | croak 'new(): max_buffered_response_bytes must be a positive integer' | ||
| 49 | if ref($max_buffered_response_bytes) | ||
| 50 | || "$max_buffered_response_bytes" !~ /\A[0-9]+\z/ | ||
| 51 | || $max_buffered_response_bytes < 1; | ||
| 52 | croak 'new(): unknown options: ' . join(', ', sort keys %option) | ||
| 53 | if %option; | ||
| 54 | |||
| 55 | require Linux::Event::HTTP::_HTTP2::ClientSelectorConnection; | ||
| 56 | require Linux::Event::HTTP::_HTTP2::Client; | ||
| 57 | require Linux::Event::HTTP::_HTTP2::ClientConnection; | ||
| 58 | |||
| 59 | my $self = bless { | ||
| 60 | protocol => 'negotiating', | ||
| 61 | scheme => "$scheme", | ||
| 62 | authority => "$authority", | ||
| 63 | stream => undef, | ||
| 64 | executor => undef, | ||
| 65 | transaction => undef, | ||
| 66 | pending => [], | ||
| 67 | pending_by_tx => {}, | ||
| 68 | closed => 0, | ||
| 69 | on_selected => $on_selected, | ||
| 70 | http1_connection_factory => $http1_connection_factory, | ||
| 71 | max_header_list_size => 0 + $max_header_list_size, | ||
| 72 | max_buffered_response_bytes => 0 + $max_buffered_response_bytes, | ||
| 73 | }, $class; | ||
| 74 | |||
| 75 | my %connect = ( | ||
| 76 | loop => $loop, | ||
| 77 | host => $host, | ||
| 78 | port => $port, | ||
| 79 | transport => $transport, | ||
| 80 | ); | ||
| 81 | $connect{timeout} = $timeout if defined $timeout; | ||
| 82 | |||
| 83 | my $stream = Linux::Event::HTTP::_HTTP2::ClientSelectorConnection | ||
| 84 | ->connect(%connect); | ||
| 85 | $stream->{_http2_selector} = $self; | ||
| 86 | weaken($stream->{_http2_selector}); | ||
| 87 | $self->{stream} = $stream; | ||
| 88 | |||
| 89 | return $self; | ||
| 90 | } | ||
| 91 | |||
| 92 | sub stream ($self) { $self->{stream} } | ||
| 93 | |||
| 94 | sub is_closed ($self) { | ||
| 95 | return 1 if $self->{closed}; | ||
| 96 | return !$self->{stream} || $self->{stream}->is_closed ? 1 : 0; | ||
| 97 | } | ||
| 98 | |||
| 99 | sub protocol ($self) { $self->{protocol} } | ||
| 100 | |||
| 101 | sub _http2_capable ($self) { 1 } | ||
| 102 | |||
| 103 | sub can_accept_transaction ($self) { | ||
| 104 | return 0 if $self->is_closed; | ||
| 105 | return 0 if $self->{protocol} ne 'h2'; | ||
| 106 | my $executor = $self->{executor} or return 0; | ||
| 107 | return $executor->can_accept_transaction; | ||
| 108 | } | ||
| 109 | |||
| 110 | sub active_streams ($self) { | ||
| 111 | return 0 if $self->{protocol} ne 'h2' || !$self->{executor}; | ||
| 112 | return $self->{executor}->stream_count; | ||
| 113 | } | ||
| 114 | |||
| 115 | sub can_queue_transaction ($self) { | ||
| 116 | return 0 if $self->is_closed; | ||
| 117 | return 0 if $self->{protocol} ne 'negotiating' | ||
| 118 | && $self->{protocol} ne 'selecting-h2'; | ||
| 119 | return @{$self->{pending}} < 100 ? 1 : 0; | ||
| 120 | } | ||
| 121 | |||
| 122 | sub _notify_selected ($self) { | ||
| 123 | my $callback = $self->{on_selected} or return; | ||
| 124 | $callback->($self, $self->{protocol}); | ||
| 125 | return; | ||
| 126 | } | ||
| 127 | |||
| 128 | sub transaction ($self) { | ||
| 129 | if ($self->{protocol} eq 'negotiating' | ||
| 130 | || $self->{protocol} eq 'selecting-h2') { | ||
| 131 | for my $pending (@{$self->{pending}}) { | ||
| 132 | my $tx = $pending->{transaction}; | ||
| 133 | return $tx if $tx && !$tx->is_terminal; | ||
| 134 | } | ||
| 135 | return undef; | ||
| 136 | } | ||
| 137 | |||
| 138 | my $tx = $self->{transaction} or return undef; | ||
| 139 | return undef if $tx->is_terminal; | ||
| 140 | return $tx; | ||
| 141 | } | ||
| 142 | |||
| 143 | sub request ($self, $request, %option) { | ||
| 144 | croak 'request(): selector is closed' if $self->is_closed; | ||
| 145 | croak 'request(): requires a Linux::Event::HTTP::Request' | ||
| 146 | if !blessed($request) | ||
| 147 | || !$request->isa('Linux::Event::HTTP::Request'); | ||
| 148 | |||
| 149 | my $http1_reassign = delete $option{_http1_reassign}; | ||
| 150 | croak 'request(): _http1_reassign must be a coderef' | ||
| 151 | if defined($http1_reassign) && ref($http1_reassign) ne 'CODE'; | ||
| 152 | |||
| 153 | if ($self->{protocol} eq 'http/1.1') { | ||
| 154 | my $tx = $self->{stream}->request($request, %option); | ||
| 155 | $self->{transaction} = $tx; | ||
| 156 | return $tx; | ||
| 157 | } | ||
| 158 | |||
| 159 | if ($self->{protocol} eq 'h2') { | ||
| 160 | $self->_prepare_h2_request($request); | ||
| 161 | my $tx = $self->{executor}->request($request, %option); | ||
| 162 | $self->{transaction} = $tx; | ||
| 163 | return $tx; | ||
| 164 | } | ||
| 165 | |||
| 166 | croak 'request(): selector cannot queue another Transaction' | ||
| 167 | if !$self->can_queue_transaction; | ||
| 168 | |||
| 169 | croak 'request(): HTTP/1 Upgrade cannot be negotiated through an HTTP/2-capable selector' | ||
| 170 | if exists $option{upgrade_to}; | ||
| 171 | croak 'request(): CONNECT tunnel handoff cannot be negotiated through an HTTP/2-capable selector' | ||
| 172 | if exists $option{tunnel_to}; | ||
| 173 | |||
| 174 | my $stream_body; | ||
| 175 | if (exists $option{stream_body}) { | ||
| 176 | $stream_body = $option{stream_body}; | ||
| 177 | croak 'request(): stream_body must be a hash reference' | ||
| 178 | if ref($stream_body) ne 'HASH'; | ||
| 179 | $stream_body = { %$stream_body }; | ||
| 180 | $option{stream_body} = $stream_body; | ||
| 181 | } | ||
| 182 | |||
| 183 | my $tx = Linux::Event::HTTP::Transaction->_new( | ||
| 184 | request => $request, | ||
| 185 | controller => $self, | ||
| 186 | ); | ||
| 187 | $tx->_activate; | ||
| 188 | |||
| 189 | my $pending_body; | ||
| 190 | if ($stream_body) { | ||
| 191 | $request->_begin_stream_body; | ||
| 192 | $tx->request_body(%$stream_body); | ||
| 193 | $pending_body = { | ||
| 194 | queue => '', | ||
| 195 | expected => $request->content_length, | ||
| 196 | sent => 0, | ||
| 197 | eof => 0, | ||
| 198 | }; | ||
| 199 | } | ||
| 200 | |||
| 201 | my $pending = { | ||
| 202 | request => $request, | ||
| 203 | transaction => $tx, | ||
| 204 | option => { %option }, | ||
| 205 | body => $pending_body, | ||
| 206 | http1_reassign => $http1_reassign, | ||
| 207 | }; | ||
| 208 | push @{$self->{pending}}, $pending; | ||
| 209 | $self->{pending_by_tx}{refaddr($tx)} = $pending; | ||
| 210 | return $tx; | ||
| 211 | } | ||
| 212 | |||
| 213 | sub _prepare_h2_request ($self, $request) { | ||
| 214 | croak 'HTTP/2 selector cannot change a committed Request' | ||
| 215 | if !$request->is_mutable; | ||
| 216 | |||
| 217 | my $host = $request->header_values('Host'); | ||
| 218 | croak 'HTTP/2 selector requires at most one Host field' | ||
| 219 | if @$host > 1; | ||
| 220 | my $authority = @$host ? $host->[0] : $self->{authority}; | ||
| 221 | |||
| 222 | $request->version('2') if ($request->version // '') ne '2'; | ||
| 223 | $request->scheme($self->{scheme}); | ||
| 224 | $request->authority($authority); | ||
| 225 | $request->remove_header('Host'); | ||
| 226 | return; | ||
| 227 | } | ||
| 228 | |||
| 229 | sub _transport_ready ($self, $stream) { | ||
| 230 | return if $self->{closed}; | ||
| 231 | |||
| 232 | my $alpn = $stream->selected_alpn // ''; | ||
| 233 | if ($alpn eq 'http/1.1') { | ||
| 234 | $self->{protocol} = 'http/1.1'; | ||
| 235 | $self->_notify_selected; | ||
| 236 | $self->_submit_pending_http1; | ||
| 237 | return; | ||
| 238 | } | ||
| 239 | |||
| 240 | if ($alpn ne 'h2') { | ||
| 241 | $self->_fail_pending( | ||
| 242 | "TLS selected unsupported HTTP ALPN '$alpn'", | ||
| 243 | ); | ||
| 244 | $stream->close if !$stream->is_closed; | ||
| 245 | return; | ||
| 246 | } | ||
| 247 | |||
| 248 | $self->{protocol} = 'selecting-h2'; | ||
| 249 | $stream->pause_read; | ||
| 250 | $stream->loop->defer(sub { | ||
| 251 | return if $self->{closed} || $stream->is_closed; | ||
| 252 | |||
| 253 | my $executor = Linux::Event::HTTP::_HTTP2::Client->new( | ||
| 254 | stream => $stream, | ||
| 255 | autostart => 0, | ||
| 256 | max_header_list_size => $self->{max_header_list_size}, | ||
| 257 | max_buffered_response_bytes => | ||
| 258 | $self->{max_buffered_response_bytes}, | ||
| 259 | ); | ||
| 260 | $self->{executor} = $executor; | ||
| 261 | $stream->{_http2_executor} = $executor; | ||
| 262 | |||
| 263 | $stream->transition_to( | ||
| 264 | 'Linux::Event::HTTP::_HTTP2::ClientConnection', | ||
| 265 | ); | ||
| 266 | $executor->start; | ||
| 267 | $self->{protocol} = 'h2'; | ||
| 268 | $self->_notify_selected; | ||
| 269 | |||
| 270 | $self->_submit_pending_h2; | ||
| 271 | $stream->resume_read if $stream->is_read_paused; | ||
| 272 | }); | ||
| 273 | return; | ||
| 274 | } | ||
| 275 | |||
| 276 | sub _remove_pending ($self, $pending) { | ||
| 277 | my $tx = $pending->{transaction}; | ||
| 278 | delete $self->{pending_by_tx}{refaddr($tx)}; | ||
| 279 | @{$self->{pending}} = grep { | ||
| 280 | refaddr($_->{transaction}) != refaddr($tx) | ||
| 281 | } @{$self->{pending}}; | ||
| 282 | return; | ||
| 283 | } | ||
| 284 | |||
| 285 | sub _submit_pending_http1_on ($self, $connection, $pending, $reassign = 0) { | ||
| 286 | my $tx = $pending->{transaction}; | ||
| 287 | return 1 if $tx->is_terminal; | ||
| 288 | |||
| 289 | if ($reassign && $pending->{http1_reassign}) { | ||
| 290 | $pending->{http1_reassign}->($connection); | ||
| 291 | } | ||
| 292 | |||
| 293 | $connection->request( | ||
| 294 | $pending->{request}, | ||
| 295 | %{$pending->{option}}, | ||
| 296 | _transaction => $tx, | ||
| 297 | ); | ||
| 298 | $self->_transfer_pending_body($connection, $pending); | ||
| 299 | $self->_remove_pending($pending); | ||
| 300 | return 1; | ||
| 301 | } | ||
| 302 | |||
| 303 | sub _submit_pending_http1 ($self) { | ||
| 304 | my @pending = @{$self->{pending}}; | ||
| 305 | return if !@pending; | ||
| 306 | |||
| 307 | my $leader_used = 0; | ||
| 308 | for my $pending (@pending) { | ||
| 309 | my $tx = $pending->{transaction}; | ||
| 310 | if ($tx->is_terminal) { | ||
| 311 | $self->_remove_pending($pending); | ||
| 312 | next; | ||
| 313 | } | ||
| 314 | |||
| 315 | my $connection; | ||
| 316 | my $reassign = 0; | ||
| 317 | if (!$leader_used) { | ||
| 318 | $connection = $self->{stream}; | ||
| 319 | $leader_used = 1; | ||
| 320 | $self->{transaction} = $tx; | ||
| 321 | } else { | ||
| 322 | my $factory = $self->{http1_connection_factory}; | ||
| 323 | if (!$factory) { | ||
| 324 | $self->_fail_one_pending( | ||
| 325 | $pending, | ||
| 326 | 'HTTP/1.1 fallback requires another connection', | ||
| 327 | ); | ||
| 328 | next; | ||
| 329 | } | ||
| 330 | $connection = eval { $factory->($self) }; | ||
| 331 | if (!$connection || $@) { | ||
| 332 | my $error = "$@"; | ||
| 333 | $error =~ s/\s+\z//; | ||
| 334 | $error ||= 'HTTP/1.1 fallback connection creation failed'; | ||
| 335 | $self->_fail_one_pending($pending, $error); | ||
| 336 | next; | ||
| 337 | } | ||
| 338 | $reassign = 1; | ||
| 339 | } | ||
| 340 | |||
| 341 | my $ok = eval { | ||
| 342 | $self->_submit_pending_http1_on( | ||
| 343 | $connection, $pending, $reassign, | ||
| 344 | ); | ||
| 345 | 1; | ||
| 346 | }; | ||
| 347 | if (!$ok) { | ||
| 348 | $self->_fail_one_pending($pending, "$@"); | ||
| 349 | $connection->close | ||
| 350 | if $reassign && $connection && !$connection->is_closed; | ||
| 351 | } | ||
| 352 | } | ||
| 353 | return; | ||
| 354 | } | ||
| 355 | |||
| 356 | sub _submit_pending_h2 ($self) { | ||
| 357 | my @pending = @{$self->{pending}}; | ||
| 358 | for my $pending (@pending) { | ||
| 359 | my $tx = $pending->{transaction}; | ||
| 360 | if ($tx->is_terminal) { | ||
| 361 | $self->_remove_pending($pending); | ||
| 362 | next; | ||
| 363 | } | ||
| 364 | |||
| 365 | my $ok = eval { | ||
| 366 | $self->_prepare_h2_request($pending->{request}); | ||
| 367 | $self->{executor}->request( | ||
| 368 | $pending->{request}, | ||
| 369 | %{$pending->{option}}, | ||
| 370 | _transaction => $tx, | ||
| 371 | ); | ||
| 372 | $self->_transfer_pending_body( | ||
| 373 | $self->{executor}, $pending, | ||
| 374 | ); | ||
| 375 | $self->_remove_pending($pending); | ||
| 376 | 1; | ||
| 377 | }; | ||
| 378 | $self->_fail_one_pending($pending, "$@") if !$ok; | ||
| 379 | } | ||
| 380 | return; | ||
| 381 | } | ||
| 382 | |||
| 383 | sub _fail_one_pending ($self, $pending, $error) { | ||
| 384 | $error = 'HTTP connection failed during protocol selection' | ||
| 385 | if !defined($error) || $error eq ''; | ||
| 386 | $error =~ s/\s+\z//; | ||
| 387 | |||
| 388 | $self->_remove_pending($pending); | ||
| 389 | my $tx = $pending->{transaction}; | ||
| 390 | $tx->_fail($error) if !$tx->is_terminal; | ||
| 391 | |||
| 392 | my $callback = $pending->{option}{on_error}; | ||
| 393 | $callback->($tx, $error) if $callback && $tx; | ||
| 394 | return; | ||
| 395 | } | ||
| 396 | |||
| 397 | sub _fail_pending ($self, $error) { | ||
| 398 | my @pending = @{$self->{pending}}; | ||
| 399 | $self->_fail_one_pending($_, $error) for @pending; | ||
| 400 | return; | ||
| 401 | } | ||
| 402 | |||
| 403 | sub _transport_error ($self, $error) { | ||
| 404 | return if $self->{protocol} ne 'negotiating' | ||
| 405 | && $self->{protocol} ne 'selecting-h2'; | ||
| 406 | $self->_fail_pending($error); | ||
| 407 | return; | ||
| 408 | } | ||
| 409 | |||
| 410 | sub end ($self) { | ||
| 411 | my $stream = $self->{stream} or return $self; | ||
| 412 | $stream->end if !$stream->is_closed; | ||
| 413 | return $self; | ||
| 414 | } | ||
| 415 | |||
| 416 | sub close ($self) { | ||
| 417 | return $self if $self->{closed}; | ||
| 418 | $self->{closed} = 1; | ||
| 419 | |||
| 420 | my @pending = @{$self->{pending}}; | ||
| 421 | $self->_fail_one_pending( | ||
| 422 | $_, 'HTTP connection closed before protocol selection', | ||
| 423 | ) for @pending; | ||
| 424 | |||
| 425 | if (my $stream = $self->{stream}) { | ||
| 426 | $stream->close if !$stream->is_closed; | ||
| 427 | } | ||
| 428 | return $self; | ||
| 429 | } | ||
| 430 | |||
| 431 | sub _cancel_http_transaction ($self, $tx) { | ||
| 432 | my $pending = $self->{pending_by_tx}{refaddr($tx)} | ||
| 433 | or croak 'cancel(): Transaction is not pending on this HTTP selector'; | ||
| 434 | |||
| 435 | $self->_remove_pending($pending); | ||
| 436 | $tx->_mark_cancelled if !$tx->is_terminal; | ||
| 437 | return; | ||
| 438 | } | ||
| 439 | |||
| 440 | sub _write_http_request_body ($self, $tx, $bytes, $final, $operation) { | ||
| 441 | my $pending = $self->{pending_by_tx}{refaddr($tx)} | ||
| 442 | or croak "$operation(): Request is no longer pending protocol selection"; | ||
| 443 | |||
| 444 | my $state = $pending->{body} | ||
| 445 | or croak "$operation(): Request does not have a streaming body"; | ||
| 446 | croak "$operation(): body must be a scalar" if ref $bytes; | ||
| 447 | $bytes = '' if !defined $bytes; | ||
| 448 | croak "$operation(): streaming Request body is already complete" | ||
| 449 | if $state->{eof}; | ||
| 450 | |||
| 451 | my $new_sent = $state->{sent} + length($bytes); | ||
| 452 | if (defined($state->{expected}) && $new_sent > $state->{expected}) { | ||
| 453 | croak "$operation(): Request body exceeds Content-Length"; | ||
| 454 | } | ||
| 455 | if ($final && defined($state->{expected}) | ||
| 456 | && $new_sent != $state->{expected}) { | ||
| 457 | croak "$operation(): Request body length does not match Content-Length"; | ||
| 458 | } | ||
| 459 | |||
| 460 | $state->{queue} .= $bytes; | ||
| 461 | $state->{sent} = $new_sent; | ||
| 462 | $state->{eof} = 1 if $final; | ||
| 463 | |||
| 464 | return length($state->{queue}) >= $BODY_HIGH_WATER ? 0 : 1; | ||
| 465 | } | ||
| 466 | |||
| 467 | sub _transfer_pending_body ($self, $controller, $pending) { | ||
| 468 | my $state = $pending->{body} or return; | ||
| 469 | my $tx = $pending->{transaction}; | ||
| 470 | my $body = $tx->_request_body_object | ||
| 471 | or croak 'streaming Request lost its Body::Stream producer'; | ||
| 472 | |||
| 473 | my $bytes = $state->{queue}; | ||
| 474 | my $final = $state->{eof} ? 1 : 0; | ||
| 475 | return if !length($bytes) && !$final; | ||
| 476 | |||
| 477 | my $accepted = $controller->_write_http_request_body( | ||
| 478 | $tx, | ||
| 479 | $bytes, | ||
| 480 | $final, | ||
| 481 | $final ? 'request_body->complete' : 'request_body->write', | ||
| 482 | ); | ||
| 483 | |||
| 484 | if (!$body->is_complete) { | ||
| 485 | if ($accepted) { | ||
| 486 | $body->_drain; | ||
| 487 | } else { | ||
| 488 | $body->_block; | ||
| 489 | } | ||
| 490 | } | ||
| 491 | |||
| 492 | $state->{queue} = ''; | ||
| 493 | return; | ||
| 494 | } | ||
| 495 | |||
| 496 | sub _write_http_response_body ($self, $tx, $bytes, $final, $operation) { | ||
| 497 | croak "$operation(): client selector cannot write a Response body"; | ||
| 498 | } | ||
| 499 | |||
| 500 | sub _upgrade_http_transaction ($self, $tx, $target_class) { | ||
| 501 | croak 'upgrade(): unavailable while HTTP protocol selection is pending'; | ||
| 502 | } | ||
| 503 | |||
| 504 | sub _tunnel_http_transaction ($self, $tx, $target_class) { | ||
| 505 | croak 'tunnel(): unavailable while HTTP protocol selection is pending'; | ||
| 506 | } | ||
| 507 | |||
| 508 | 1; | ||
| 509 | |||
| 510 | __END__ |