| 1 | package Langertha::Role::AsyncHTTP; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Async HTTP backend selection (injected > Net::Async::HTTP > sync LWP fallback) | ||||||||||||||||||||||||||||||||||||||||||
| 3 | 181 | our $VERSION = '0.503'; | |||||||||||||||||||||||||||||||||||||||||
| 4 | 181 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 181 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 181 | ▶ | use Future; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 181 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 181 | ▶ | use Scalar::Util qw( blessed reftype ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 181 | ▶ | use URI; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 181 | requires 'user_agent'; | |||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 181 | my $WARNED = 0; | |||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 181 | has _async_loop => ( | |||||||||||||||||||||||||||||||||||||||||
| 16 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 17 | lazy_build => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 18 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 144 | ▶ | sub _build__async_loop { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build__async_loop: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 144 | require IO::Async::Loop; | |||||||||||||||||||||||||||||||||||||||||
| 22 | 144 | return IO::Async::Loop->new; | |||||||||||||||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 181 | has _async_http => ( | |||||||||||||||||||||||||||||||||||||||||
| 26 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 27 | lazy_build => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 28 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | 156 | ▶ | sub _build__async_http { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build__async_http: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | 156 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 32 | 156 | my $loaded = eval { require Net::Async::HTTP; 1 }; | |||||||||||||||||||||||||||||||||||||||||
| 33 | 156 | my $load_error = $@; | |||||||||||||||||||||||||||||||||||||||||
| 34 | 156 | ▶ | if ($loaded) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | # No HTTP/1.1 pipelining: a request pipelined behind a long LLM stream | ||||||||||||||||||||||||||||||||||||||||||
| 36 | # waits for it anyway, and fails with "Connection closed" when that stream | ||||||||||||||||||||||||||||||||||||||||||
| 37 | # is aborted (karr k199, ADR 0027). | ||||||||||||||||||||||||||||||||||||||||||
| 38 | 151 | my $http = Net::Async::HTTP->new( pipeline => 0 ); | |||||||||||||||||||||||||||||||||||||||||
| 39 | 151 | $self->_async_loop->add($http); | |||||||||||||||||||||||||||||||||||||||||
| 40 | 151 | return $http; | |||||||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||||||
| 42 | 5 | ▶ | unless ($WARNED) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 4 | $WARNED = 1; | |||||||||||||||||||||||||||||||||||||||||
| 44 | # A missing module is the expected, documented case. Anything else (a | ||||||||||||||||||||||||||||||||||||||||||
| 45 | # missing IO::Async sub-dependency after a partial upgrade, a compile | ||||||||||||||||||||||||||||||||||||||||||
| 46 | # error) is a broken install the user should see, not "not available". | ||||||||||||||||||||||||||||||||||||||||||
| 47 | 4 | ▶ | my $reason = $load_error =~ m{\ACan't locate Net/Async/HTTP\.pm } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | ? 'Net::Async::HTTP not available' | ||||||||||||||||||||||||||||||||||||||||||
| 49 | : 'Net::Async::HTTP failed to load (' . ( split /\n/, $load_error )[0] . ')'; | ||||||||||||||||||||||||||||||||||||||||||
| 50 | 4 | warn "$reason; Langertha is running HTTP synchronously " | |||||||||||||||||||||||||||||||||||||||||
| 51 | . "(no concurrency). Install Net::Async::HTTP + IO::Async for real async." | ||||||||||||||||||||||||||||||||||||||||||
| 52 | . _caller_location() . "\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||
| 54 | 5 | require Langertha::Request::SyncHTTP; | |||||||||||||||||||||||||||||||||||||||||
| 55 | 5 | return Langertha::Request::SyncHTTP->new( user_agent => $self->user_agent ); | |||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | # The builder runs inside a generated Moose accessor called from a Langertha | ||||||||||||||||||||||||||||||||||||||||||
| 59 | # role (chat_f, poll_metrics_f, ...), so carp would point into that plumbing. | ||||||||||||||||||||||||||||||||||||||||||
| 60 | # Report the first frame outside Langertha, Moose and the Future machinery: | ||||||||||||||||||||||||||||||||||||||||||
| 61 | # the user's own call site. | ||||||||||||||||||||||||||||||||||||||||||
| 62 | 4 | ▶ | sub _caller_location { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_caller_location: called CC=3 SCAR=11.3
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 4 | my $level = 0; | |||||||||||||||||||||||||||||||||||||||||
| 64 | 4 | ▶ | while ( my ( $package, $file, $line ) = caller $level++ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 19 | ▶ | next if $package =~ /\A(?:Langertha|Moose|Class::MOP|Eval::Closure|Future)(?:::|\z)/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 4 | return " at $file line $line."; | |||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | return ''; | |||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 66 | ▶ | async sub async_request_f { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
async_request_f: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 66 | my ( $self, $request, %opts ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 74 | 66 | return await $self->_async_do_request_f( request => $request, %opts ); | |||||||||||||||||||||||||||||||||||||||||
| 75 | 181 | } | |||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | # Every async request core sends goes through here (karr k278, ADR 0027). | ||||||||||||||||||||||||||||||||||||||||||
| 78 | # Net::Async::HTTP sets no timeout of its own, so a provider that accepts and | ||||||||||||||||||||||||||||||||||||||||||
| 79 | # never answers left the Future pending forever. When the engine has a | ||||||||||||||||||||||||||||||||||||||||||
| 80 | # user_agent_timeout it becomes Net::Async::HTTP's per-request timeout: the | ||||||||||||||||||||||||||||||||||||||||||
| 81 | # total time for a plain request, the time without a byte (stall_timeout) for | ||||||||||||||||||||||||||||||||||||||||||
| 82 | # a streaming one (on_header), where a long steady stream is legitimate. The | ||||||||||||||||||||||||||||||||||||||||||
| 83 | # failure is rewritten to name the engine and the URL (query and userinfo | ||||||||||||||||||||||||||||||||||||||||||
| 84 | # dropped: Gemini carries its key in the query). A caller's own timeout / | ||||||||||||||||||||||||||||||||||||||||||
| 85 | # stall_timeout option wins. The sync shim already has the timeout on its | ||||||||||||||||||||||||||||||||||||||||||
| 86 | # LWP user agent, and an injected client of another class keeps its own. | ||||||||||||||||||||||||||||||||||||||||||
| 87 | 863 | ▶ | sub _async_do_request_f { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_async_do_request_f: called CC=22 SCAR=33.3
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 863 | my ( $self, %args ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 89 | 863 | my $http = $self->_async_http; | |||||||||||||||||||||||||||||||||||||||||
| 90 | 863 | ▶ | my $is_nahttp = blessed($http) && $http->isa('Net::Async::HTTP'); | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
builtin::blessed($http) && $http->isa('Net::Async::HTTP')
MC/DC50%1/2
builtin::blessed($http) && $http->isa('Net::Async::HTTP')
builtin::blessed($http)
$http->isa('Net::Async::HTTP')
Truth table66%2/3
A && B
A= builtin::blessed($http)B= $http->isa('Net::Async::HTTP')
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | # connect_address (karr k375): only Net::Async::HTTP (per-request host and | ||||||||||||||||||||||||||||||||||||||||||
| 92 | # SSL_* options, _async_one_request_f) and the sync shim over a pinned | ||||||||||||||||||||||||||||||||||||||||||
| 93 | # Langertha::HTTP::UserAgent can pin; any other client would resolve the | ||||||||||||||||||||||||||||||||||||||||||
| 94 | # name itself, so a request to the pinned host fails instead of going out. | ||||||||||||||||||||||||||||||||||||||||||
| 95 | 863 | ▶ | if ( my $pin_host = $self->can('_connect_host') ? $self->_connect_host : undef ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 36 | ▶ | my $target = $args{request} ? $args{request}->uri : $args{uri}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 36 | ▶ | my $to_pinned = defined $target && URI->new("$target")->can('host') | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%4/8
defined $target && 'URI'->new("$target")->can('host') && lc('URI'->new("$target")->host // '') eq $pin_host
defined $target and "URI"->new("$target")->can("host")
"URI"->new("$target")->host // ""
MC/DC0%0/3
defined $target && 'URI'->new("$target")->can('host') && lc('URI'->new("$target")->host // '') eq $pin_host
defined $target
"URI"->new("$target")->can("host")
"URI"->new("$target")->host
Truth table25%1/4
A and B && C
A= defined $targetB= "URI"->new("$target")->can("host")C= "URI"->new("$target")->host
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | && lc( URI->new("$target")->host // '' ) eq $pin_host; | ||||||||||||||||||||||||||||||||||||||||||
| 99 | 36 | ▶ | if ($to_pinned) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 35 | ▶ | my $error = $is_nahttp ? $self->_nahttp_pin_error( $http, \%args ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition60%3/5
builtin::blessed($http) and $http->isa("Langertha::Request::SyncHTTP")
builtin::blessed($http) // "unblessed"
MC/DC50%1/2
builtin::blessed($http) and $http->isa("Langertha::Request::SyncHTTP")
builtin::blessed($http)
$http->isa("Langertha::Request::SyncHTTP")
MC/DC0%0/1
builtin::blessed($http) // "unblessed"
builtin::blessed($http)
Truth table66%2/3
A and B
A= builtin::blessed($http)B= $http->isa("Langertha::Request::SyncHTTP")
Truth table50%1/2
A
A= builtin::blessed($http)
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | : blessed($http) && $http->isa('Langertha::Request::SyncHTTP') | ||||||||||||||||||||||||||||||||||||||||||
| 102 | ? $self->_connect_pin_error( $http->user_agent ) | ||||||||||||||||||||||||||||||||||||||||||
| 103 | : 'connect_address ' . $self->connect_address . ' cannot be applied through an injected ' | ||||||||||||||||||||||||||||||||||||||||||
| 104 | . ( blessed($http) // 'unblessed' ) . ' client (only Net::Async::HTTP or Langertha::Request::SyncHTTP can pin)'; | ||||||||||||||||||||||||||||||||||||||||||
| 105 | 35 | ▶ | return Future->fail( ref($self) . ": $error\n", 'connect_address' ) if $error; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | # Only Net::Async::HTTP gets the timeout, the body cap and the redirect policy | ||||||||||||||||||||||||||||||||||||||||||
| 109 | # below. The sync shim and injected clients hand back the still-encoded body, | ||||||||||||||||||||||||||||||||||||||||||
| 110 | # which Role::HTTP::_bounded_decoded_content bounds downstream, and they carry | ||||||||||||||||||||||||||||||||||||||||||
| 111 | # their own timeout (ADR 0027); the sync shim runs the engine's user_agent, | ||||||||||||||||||||||||||||||||||||||||||
| 112 | # a Langertha::HTTP::UserAgent with the same redirect policy (karr k374). | ||||||||||||||||||||||||||||||||||||||||||
| 113 | 859 | ▶ | return $http->do_request(%args) unless $is_nahttp; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | # Redirects (karr k374). Net::Async::HTTP follows a GET redirect with a fresh | ||||||||||||||||||||||||||||||||||||||||||
| 116 | # request carrying the Location's query as it is, so a server echoing the | ||||||||||||||||||||||||||||||||||||||||||
| 117 | # request URI sent Gemini's ?key= to the new host, while on the same origin | ||||||||||||||||||||||||||||||||||||||||||
| 118 | # the credential header was lost. It follows nothing here (max_redirects => 0); | ||||||||||||||||||||||||||||||||||||||||||
| 119 | # each hop is decided by Langertha::HTTP::Redirect, the policy the sync LWP | ||||||||||||||||||||||||||||||||||||||||||
| 120 | # agent follows too. A caller's max_redirects is the hop limit, else the | ||||||||||||||||||||||||||||||||||||||||||
| 121 | # client's own (the max_redirects it was built with, default 3). Only GET and | ||||||||||||||||||||||||||||||||||||||||||
| 122 | # HEAD are ever followed, so any other request is the single hop it was. | ||||||||||||||||||||||||||||||||||||||||||
| 123 | # The client's own limit is Net::Async::HTTP private state (checked against | ||||||||||||||||||||||||||||||||||||||||||
| 124 | # 0.50, where configure stores it in $self->{max_redirects}); read defensively | ||||||||||||||||||||||||||||||||||||||||||
| 125 | # and fall back to that version's default of 3. | ||||||||||||||||||||||||||||||||||||||||||
| 126 | my $hops = exists $args{max_redirects} ? delete $args{max_redirects} | ||||||||||||||||||||||||||||||||||||||||||
| 127 | : ( reftype($http) // '' ) eq 'HASH' && defined $http->{max_redirects} ? $http->{max_redirects} | ||||||||||||||||||||||||||||||||||||||||||
| 128 | 205 | ▶ | : 3; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition40%2/5
(builtin::reftype($http) // "") eq "HASH" and defined $http->{"max_redirects"}
builtin::reftype($http) // ""
MC/DC0%0/2
(builtin::reftype($http) // "") eq "HASH" and defined $http->{"max_redirects"}
builtin::reftype($http)
defined $http->{"max_redirects"}
Truth table33%1/3
A and B
A= builtin::reftype($http)B= defined $http->{"max_redirects"}
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | 205 | $args{max_redirects} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 130 | # A caller passing uri => instead of request => (no core caller does) gets no | ||||||||||||||||||||||||||||||||||||||||||
| 131 | # redirect following: Net::Async::HTTP builds that request itself, so it is | ||||||||||||||||||||||||||||||||||||||||||
| 132 | # the single hop max_redirects => 0 allows and a 3xx comes back as it is. | ||||||||||||||||||||||||||||||||||||||||||
| 133 | 205 | my $request = $args{request}; | |||||||||||||||||||||||||||||||||||||||||
| 134 | 205 | ▶ | return $self->_async_one_request_f( $http, %args ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
$hops and $request and uc($request->method) =~ /\A(?:GET|HEAD)\z/
$hops and $request
MC/DC33%1/3
$hops and $request and uc($request->method) =~ /\A(?:GET|HEAD)\z/
$hops
$request
uc($request->method) =~ /\A(?:GET|HEAD)\z/
Truth table50%2/4
A and B and C
A= $hopsB= $requestC= uc($request->method) =~ /\A(?:GET|HEAD)\z/
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | unless $hops && $request && ( uc $request->method ) =~ /\A(?:GET|HEAD)\z/; | ||||||||||||||||||||||||||||||||||||||||||
| 136 | 84 | require Langertha::HTTP::Redirect; | |||||||||||||||||||||||||||||||||||||||||
| 137 | 84 | return $self->_async_follow_f( $http, \%args, $hops, undef ); | |||||||||||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | # One hop of a followed chain, then the next one if the policy follows it. A | ||||||||||||||||||||||||||||||||||||||||||
| 141 | # caller's on_header must not see a redirect that is followed (its body is | ||||||||||||||||||||||||||||||||||||||||||
| 142 | # discarded, as Net::Async::HTTP does); a redirect that is not followed reaches | ||||||||||||||||||||||||||||||||||||||||||
| 143 | # it like any other response. Future->then propagates a cancel of the returned | ||||||||||||||||||||||||||||||||||||||||||
| 144 | # future to the hop in flight. | ||||||||||||||||||||||||||||||||||||||||||
| 145 | 100 | ▶ | sub _async_follow_f { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_async_follow_f: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | 100 | my ( $self, $http, $args, $hops, $previous ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 147 | 100 | my %hop = %{$args}; | |||||||||||||||||||||||||||||||||||||||||
| 148 | # Decided once per response (on_header and the completed hop both ask), so a | ||||||||||||||||||||||||||||||||||||||||||
| 149 | # refusal's Client-Warning is added once. | ||||||||||||||||||||||||||||||||||||||||||
| 150 | 100 | my ( $decided, $next ); | |||||||||||||||||||||||||||||||||||||||||
| 151 | 85 | ▶ | my $follows = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=22.8
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | 85 | my ($response) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 153 | 85 | ▶ | return $next if $decided && $decided == $response; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$decided and $decided == $response
MC/DC50%1/2
$decided and $decided == $response
$decided
$decided == $response
Truth table66%2/3
A and B
A= $decidedB= $decided == $response
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | 81 | $decided = $response; | |||||||||||||||||||||||||||||||||||||||||
| 155 | 81 | ▶ | $response->previous($previous) if $previous && !$response->previous; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$previous and not $response->previous
MC/DC50%1/2
$previous and not $response->previous
$previous
not $response->previous
Truth table66%2/3
A and not(B)
A= $previousB= not $response->previous
| |||||||||||||||||||||||||||||||||||||||||||
| 156 | 81 | ▶ | if ( $hops > 0 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | 79 | ▶ | $next = Langertha::HTTP::Redirect::next_request( $hop{request}, $response, | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 158 | $self->can('_connect_host') ? $self->_connect_host : undef ); | ||||||||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||||||
| 160 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 161 | 2 | $next = undef; | |||||||||||||||||||||||||||||||||||||||||
| 162 | 2 | ▶ | $response->push_header( 'Client-Warning' => | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$response->is_redirect and defined $response->header("Location")
MC/DC0%0/2
$response->is_redirect and defined $response->header("Location")
$response->is_redirect
defined $response->header("Location")
Truth table33%1/3
A and B
A= $response->is_redirectB= defined $response->header("Location")
| |||||||||||||||||||||||||||||||||||||||||||
| 163 | 'redirect not followed: Langertha::HTTP::Redirect: hop limit reached' ) | ||||||||||||||||||||||||||||||||||||||||||
| 164 | if $response->is_redirect && defined $response->header('Location'); | ||||||||||||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||||||||||
| 166 | 81 | return $next; | |||||||||||||||||||||||||||||||||||||||||
| 167 | 100 | }; | |||||||||||||||||||||||||||||||||||||||||
| 168 | 100 | ▶ | if ( my $on_header = $hop{on_header} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 169 | 4 | ▶ | $hop{on_header} = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 170 | 4 | my ($response) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 171 | 4 | ▶ | return sub { return @_ ? () : $response } if $follows->($response); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 172 | 3 | return $on_header->($response); | |||||||||||||||||||||||||||||||||||||||||
| 173 | 4 | }; | |||||||||||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||||||||||||
| 175 | 81 | ▶ | return $self->_async_one_request_f( $http, %hop )->then( sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 176 | 81 | my ($response) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 177 | 81 | my $next = $follows->($response); | |||||||||||||||||||||||||||||||||||||||||
| 178 | 81 | ▶ | return Future->done($response) unless $next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 179 | 16 | return $self->_async_follow_f( $http, { %{$args}, request => $next }, $hops - 1, $response ); | |||||||||||||||||||||||||||||||||||||||||
| 180 | 100 | } ); | |||||||||||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | # One Net::Async::HTTP request with the connect check, the body cap and the | ||||||||||||||||||||||||||||||||||||||||||
| 184 | # timeout applied. | ||||||||||||||||||||||||||||||||||||||||||
| 185 | 221 | ▶ | sub _async_one_request_f { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_async_one_request_f: called CC=26 SCAR=32.9
| |||||||||||||||||||||||||||||||||||||||||||
| 186 | 221 | my ( $self, $http, %args ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||
| 188 | # Net::Async::HTTP 0.50 loads IO::Async::Internals::Connector (and | ||||||||||||||||||||||||||||||||||||||||||
| 189 | # IO::Async::SSL for https) only when it connects; when that load dies, the | ||||||||||||||||||||||||||||||||||||||||||
| 190 | # dead connection keeps the host's slot and every later request to the host | ||||||||||||||||||||||||||||||||||||||||||
| 191 | # hangs. Check first, so it is a failed Future naming the module (karr k353). | ||||||||||||||||||||||||||||||||||||||||||
| 192 | 221 | ▶ | my $target = $args{request} ? $args{request}->uri : $args{uri}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 193 | 221 | ▶ | if ( defined $target ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 194 | 221 | require Langertha::HTTP::ConnectCheck; | |||||||||||||||||||||||||||||||||||||||||
| 195 | 221 | my $error = Langertha::HTTP::ConnectCheck::connect_error( $target, $args{SSL} ); | |||||||||||||||||||||||||||||||||||||||||
| 196 | 221 | ▶ | return Future->fail( ref($self).": $error\n", 'connect' ) if $error; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||
| 199 | # connect_address (karr k375): connect to the checked address, not to a | ||||||||||||||||||||||||||||||||||||||||||
| 200 | # fresh resolution of the name. Net::Async::HTTP 0.50 uses host/port only as | ||||||||||||||||||||||||||||||||||||||||||
| 201 | # the connection target (and its pool key); the Host header comes from the | ||||||||||||||||||||||||||||||||||||||||||
| 202 | # request URI, and for SSL it sets SSL_hostname => host before the request's | ||||||||||||||||||||||||||||||||||||||||||
| 203 | # own SSL_* options, so these name the host for SNI and the certificate check. | ||||||||||||||||||||||||||||||||||||||||||
| 204 | # A redirect is never followed by the client here (max_redirects => 0), so | ||||||||||||||||||||||||||||||||||||||||||
| 205 | # the target cannot leak into another hop. | ||||||||||||||||||||||||||||||||||||||||||
| 206 | 217 | ▶ | if ( defined $target and my $pin_host = $self->can('_connect_host') ? $self->_connect_host : undef ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
defined $target and my $pin_host = $self->can("_connect_host") ? $self->_connect_host : undef
MC/DC50%1/2
defined $target and my $pin_host = $self->can("_connect_host") ? $self->_connect_host : undef
defined $target
my $pin_host = $self->can("_connect_host") ? $self->_connect_host : undef
Truth table66%2/3
A and B
A= defined $targetB= my $pin_host = $self->can("_connect_host") ? $self->_connect_host : undef
| |||||||||||||||||||||||||||||||||||||||||||
| 207 | 24 | my $uri = URI->new("$target"); | |||||||||||||||||||||||||||||||||||||||||
| 208 | 24 | ▶ | if ( $uri->can('host') && lc( $uri->host // '' ) eq $pin_host ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition60%3/5
$uri->can("host") and lc($uri->host // "") eq $pin_host
$uri->host // ""
MC/DC0%0/2
$uri->can("host") and lc($uri->host // "") eq $pin_host
$uri->can("host")
$uri->host
Truth table33%1/3
A and B
A= $uri->can("host")B= $uri->host
| |||||||||||||||||||||||||||||||||||||||||||
| 209 | 23 | %args = ( %args, _connect_pin_args( $uri, $self->connect_address ) ); | |||||||||||||||||||||||||||||||||||||||||
| 210 | 23 | $args{on_ready} = $self->_connect_pin_on_ready( $http, $uri, \%args ); | |||||||||||||||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||
| 214 | 217 | ▶ | my $stream = $args{on_header} ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||
| 216 | # Bound the decoded response body against a decompression bomb (karr k346). | ||||||||||||||||||||||||||||||||||||||||||
| 217 | # Unlike LWP, Net::Async::HTTP inflates a Content-Encoding while it streams and | ||||||||||||||||||||||||||||||||||||||||||
| 218 | # moves the header to X-Original-Content-Encoding, so by the time the finished | ||||||||||||||||||||||||||||||||||||||||||
| 219 | # response reaches _bounded_decoded_content there is no encoding left to bound | ||||||||||||||||||||||||||||||||||||||||||
| 220 | # and a bomb has already been inflated in full. Count the decoded bytes as they | ||||||||||||||||||||||||||||||||||||||||||
| 221 | # arrive (Net::Async::HTTP decodes before this on_header callback) and abort | ||||||||||||||||||||||||||||||||||||||||||
| 222 | # past response_max_bytes -- the same streamed-byte guard the inline-image fetch | ||||||||||||||||||||||||||||||||||||||||||
| 223 | # uses (Langertha::Content::Image::_fetch_net_async_f). Only for a non-streaming | ||||||||||||||||||||||||||||||||||||||||||
| 224 | # request: a caller on_header is a stream, unbounded by design. | ||||||||||||||||||||||||||||||||||||||||||
| 225 | 217 | ▶ | my $max = ( !$stream && $self->can('response_max_bytes') ) ? $self->response_max_bytes : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $stream and $self->can("response_max_bytes")
MC/DC50%1/2
not $stream and $self->can("response_max_bytes")
not $stream
$self->can("response_max_bytes")
Truth table66%2/3
not(A) and B
A= not $streamB= $self->can("response_max_bytes")
| |||||||||||||||||||||||||||||||||||||||||||
| 226 | 217 | my ( $abort, $too_big ); | |||||||||||||||||||||||||||||||||||||||||
| 227 | 217 | ▶ | if ($max) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 228 | 164 | $abort = Future->new; | |||||||||||||||||||||||||||||||||||||||||
| 229 | 164 | $args{on_header} = $self->_body_cap_on_header( $http, $max, \$too_big, $abort ); | |||||||||||||||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||
| 232 | # user_agent_timeout -> Net::Async::HTTP's own per-request option, unless the | ||||||||||||||||||||||||||||||||||||||||||
| 233 | # caller set one (karr k278). | ||||||||||||||||||||||||||||||||||||||||||
| 234 | 217 | ▶ | my $secs = $self->can('has_user_agent_timeout') && $self->has_user_agent_timeout | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->can("has_user_agent_timeout") and $self->has_user_agent_timeout
MC/DC50%1/2
$self->can("has_user_agent_timeout") and $self->has_user_agent_timeout
$self->can("has_user_agent_timeout")
$self->has_user_agent_timeout
Truth table66%2/3
A and B
A= $self->can("has_user_agent_timeout")B= $self->has_user_agent_timeout
| |||||||||||||||||||||||||||||||||||||||||||
| 235 | ? $self->user_agent_timeout : 0; | ||||||||||||||||||||||||||||||||||||||||||
| 236 | 217 | ▶ | my $timed = $secs && !exists $args{timeout} && !exists $args{stall_timeout}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition83%5/6
$secs && !exists $args{'timeout'} && !exists $args{'stall_timeout'}
$secs and not exists $args{"timeout"}
MC/DC66%2/3
$secs && !exists $args{'timeout'} && !exists $args{'stall_timeout'}
$secs
not exists $args{"timeout"}
!exists $args{'stall_timeout'}
Truth table75%3/4
A and not(B) && not(C)
A= $secsB= not exists $args{"timeout"}C= !exists $args{'stall_timeout'}
| |||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||
| 238 | 217 | ▶ | return $http->do_request(%args) unless $timed || $max; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$timed or $max
MC/DC100%2/2
$timed or $max
$timed
$max
Truth table100%3/3
A or B
A= $timedB= $max
| |||||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||
| 240 | 166 | ▶ | my $request_f = $http->do_request( %args, | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 241 | $timed ? ( ( $stream ? 'stall_timeout' : 'timeout' ) => $secs ) : () ); | ||||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||
| 243 | 166 | ▶ | if ($timed) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 244 | 15 | my $uri = $args{request}->uri->clone; | |||||||||||||||||||||||||||||||||||||||||
| 245 | 15 | $uri->query(undef); | |||||||||||||||||||||||||||||||||||||||||
| 246 | 15 | $uri->fragment(undef); | |||||||||||||||||||||||||||||||||||||||||
| 247 | 15 | ▶ | $uri->userinfo(undef) if $uri->can('userinfo'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 248 | 15 | ▶ | my $what = ref($self) . ': ' . ( $stream ? 'streaming request' : 'request' ) . " to $uri"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 249 | 14 | ▶ | $request_f = $request_f->else( sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||||||||||||||||
| 250 | 14 | my ( $message, $category, @details ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 251 | 14 | ▶ | return Future->fail(@_) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
defined $category and $category eq "timeout" || $category eq "stall_timeout"
$category eq "timeout" or $category eq "stall_timeout"
MC/DC33%1/3
defined $category and $category eq "timeout" || $category eq "stall_timeout"
defined $category
$category eq "timeout"
$category eq "stall_timeout"
Truth table75%3/4
A and B or C
A= defined $categoryB= $category eq "timeout"C= $category eq "stall_timeout"
| |||||||||||||||||||||||||||||||||||||||||||
| 252 | unless defined $category && ( $category eq 'timeout' || $category eq 'stall_timeout' ); | ||||||||||||||||||||||||||||||||||||||||||
| 253 | 13 | ▶ | my $text = $category eq 'timeout' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 254 | ? "$what timed out after ${secs}s" | ||||||||||||||||||||||||||||||||||||||||||
| 255 | : "$what timed out after ${secs}s without data ($message)"; | ||||||||||||||||||||||||||||||||||||||||||
| 256 | 13 | return Future->fail( "$text\n", $category, @details ); | |||||||||||||||||||||||||||||||||||||||||
| 257 | 15 | } ); | |||||||||||||||||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||
| 260 | 166 | ▶ | return $request_f unless $max; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||
| 262 | # Over the cap, whatever ends the fetch first -- the abort cancelling the | ||||||||||||||||||||||||||||||||||||||||||
| 263 | # request, or the server closing the connection -- becomes the size error. | ||||||||||||||||||||||||||||||||||||||||||
| 264 | 4 | ▶ | my $too_big_croak = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 265 | 4 | croak "".( ref $self )." response body exceeds response_max_bytes ($max)"; | |||||||||||||||||||||||||||||||||||||||||
| 266 | 164 | }; | |||||||||||||||||||||||||||||||||||||||||
| 267 | return Future->wait_any( $request_f, $abort ) | ||||||||||||||||||||||||||||||||||||||||||
| 268 | 137 | ▶ | ->then( sub { $too_big ? $too_big_croak->() : Future->done(@_) } ) | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 269 | 164 | ▶ | ->else( sub { $too_big ? $too_big_croak->() : Future->fail(@_) } ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||
| 272 | # The Net::Async::HTTP request options that connect a request for $uri to | ||||||||||||||||||||||||||||||||||||||||||
| 273 | # $address while naming the host for TLS (karr k375). | ||||||||||||||||||||||||||||||||||||||||||
| 274 | 23 | ▶ | sub _connect_pin_args { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_connect_pin_args: called CC=5 SCAR=17.5
| |||||||||||||||||||||||||||||||||||||||||||
| 275 | 23 | my ( $uri, $address ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 276 | 23 | my $host = $uri->host; | |||||||||||||||||||||||||||||||||||||||||
| 277 | 23 | ▶ | my $literal = $host =~ /:/ || $host =~ /\A[0-9.]+\z/; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$host =~ /:/ || $host =~ /\A[0-9.]+\z/
MC/DC0%0/2
$host =~ /:/ || $host =~ /\A[0-9.]+\z/
$host =~ /:/
$host =~ /\A[0-9.]+\z/
Truth table33%1/3
A || B
A= $host =~ /:/B= $host =~ /\A[0-9.]+\z/
| |||||||||||||||||||||||||||||||||||||||||||
| 278 | return ( | ||||||||||||||||||||||||||||||||||||||||||
| 279 | 23 | ▶ | host => $address, | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition50%1/2
$uri->scheme // ""
MC/DC0%0/1
$uri->scheme // ""
$uri->scheme
Truth table50%1/2
A
A= $uri->scheme
| |||||||||||||||||||||||||||||||||||||||||||
| 280 | port => $uri->port, | ||||||||||||||||||||||||||||||||||||||||||
| 281 | ( lc( $uri->scheme // '' ) eq 'https' | ||||||||||||||||||||||||||||||||||||||||||
| 282 | ? ( SSL_hostname => ( $literal ? undef : $host ), SSL_verifycn_name => $host ) | ||||||||||||||||||||||||||||||||||||||||||
| 283 | : () ), | ||||||||||||||||||||||||||||||||||||||||||
| 284 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||
| 287 | # The on_ready (Net::Async::HTTP 0.50 runs it with the connection before it | ||||||||||||||||||||||||||||||||||||||||||
| 288 | # writes the request, for a new and for a pooled connection alike) that checks | ||||||||||||||||||||||||||||||||||||||||||
| 289 | # the connection really is to the pinned address and, over TLS, was verified | ||||||||||||||||||||||||||||||||||||||||||
| 290 | # for this host. The client pools connections by host:port, and host is now | ||||||||||||||||||||||||||||||||||||||||||
| 291 | # the address: two engines pinning different names to one address on a shared | ||||||||||||||||||||||||||||||||||||||||||
| 292 | # client would otherwise share a TLS session verified for the first name | ||||||||||||||||||||||||||||||||||||||||||
| 293 | # (karr k375). A refused connection is closed once idle (never under a request | ||||||||||||||||||||||||||||||||||||||||||
| 294 | # that is using it), so it leaves the pool and a request queued behind it gets | ||||||||||||||||||||||||||||||||||||||||||
| 295 | # a fresh one. | ||||||||||||||||||||||||||||||||||||||||||
| 296 | 24 | ▶ | sub _connect_pin_on_ready { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_connect_pin_on_ready: called CC=10 SCAR=24.5
| |||||||||||||||||||||||||||||||||||||||||||
| 297 | 24 | my ( $self, $http, $uri, $args ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 298 | 24 | my $address = $self->connect_address; | |||||||||||||||||||||||||||||||||||||||||
| 299 | 24 | my $host = $uri->host; | |||||||||||||||||||||||||||||||||||||||||
| 300 | 24 | ▶ | my $https = lc( $uri->scheme // '' ) eq 'https'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$uri->scheme // ""
MC/DC0%0/1
$uri->scheme // ""
$uri->scheme
Truth table50%1/2
A
A= $uri->scheme
| |||||||||||||||||||||||||||||||||||||||||||
| 301 | 24 | ▶ | my $params = ( reftype($http) // '' ) eq 'HASH' && ref $http->{ssl_params} eq 'HASH' ? $http->{ssl_params} : {}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition40%2/5
(builtin::reftype($http) // "") eq "HASH" and ref $http->{"ssl_params"} eq "HASH"
builtin::reftype($http) // ""
MC/DC0%0/2
(builtin::reftype($http) // "") eq "HASH" and ref $http->{"ssl_params"} eq "HASH"
builtin::reftype($http)
ref $http->{"ssl_params"} eq "HASH"
Truth table33%1/3
A and B
A= builtin::reftype($http)B= ref $http->{"ssl_params"} eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||
| 302 | # Verification switched off for this request or client: SSL_verify_mode | ||||||||||||||||||||||||||||||||||||||||||
| 303 | # => 0 skips both checks, SSL_verifycn_scheme => 'none' the name check | ||||||||||||||||||||||||||||||||||||||||||
| 304 | # (what the connection itself would have skipped; LWP's verify_hostname | ||||||||||||||||||||||||||||||||||||||||||
| 305 | # => 0 is the same pair on the sync side). | ||||||||||||||||||||||||||||||||||||||||||
| 306 | 24 | ▶ | my %tls = map { $_ => ( exists $args->{$_} ? $args->{$_} : $params->{$_} ) } qw( SSL_verify_mode SSL_verifycn_scheme ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 307 | 24 | ▶ | my $check_chain = $https && !( defined $tls{SSL_verify_mode} && !$tls{SSL_verify_mode} ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%3/6
$https && !(defined $tls{'SSL_verify_mode'} && !$tls{'SSL_verify_mode'})
defined $tls{'SSL_verify_mode'} && !$tls{'SSL_verify_mode'}
MC/DC33%1/3
$https && !(defined $tls{'SSL_verify_mode'} && !$tls{'SSL_verify_mode'})
$https
defined $tls{'SSL_verify_mode'}
!$tls{'SSL_verify_mode'}
Truth table50%2/4
A && not(B && not(C))
A= $httpsB= defined $tls{'SSL_verify_mode'}C= !$tls{'SSL_verify_mode'}
| |||||||||||||||||||||||||||||||||||||||||||
| 308 | 24 | ▶ | my $check_name = $check_chain && !( defined $tls{SSL_verifycn_scheme} && $tls{SSL_verifycn_scheme} eq 'none' ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%3/6
$check_chain && !(defined $tls{'SSL_verifycn_scheme'} && $tls{'SSL_verifycn_scheme'} eq 'none')
defined $tls{'SSL_verifycn_scheme'} && $tls{'SSL_verifycn_scheme'} eq 'none'
MC/DC33%1/3
$check_chain && !(defined $tls{'SSL_verifycn_scheme'} && $tls{'SSL_verifycn_scheme'} eq 'none')
$check_chain
defined $tls{'SSL_verifycn_scheme'}
$tls{'SSL_verifycn_scheme'} eq 'none'
Truth table50%2/4
A && not(B && C)
A= $check_chainB= defined $tls{'SSL_verifycn_scheme'}C= $tls{'SSL_verifycn_scheme'} eq 'none'
| |||||||||||||||||||||||||||||||||||||||||||
| 309 | 24 | my $caller_on_ready = $args->{on_ready}; | |||||||||||||||||||||||||||||||||||||||||
| 310 | 24 | my $class = ref $self; | |||||||||||||||||||||||||||||||||||||||||
| 311 | 23 | ▶ | return sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=11 SCAR=24.5
| |||||||||||||||||||||||||||||||||||||||||||
| 312 | 23 | my ($conn) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 313 | 23 | my $handle = $conn->read_handle; | |||||||||||||||||||||||||||||||||||||||||
| 314 | 23 | ▶ | my $peer = $handle ? eval { $handle->peerhost } : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 315 | 23 | require Langertha::HTTP::UserAgent; | |||||||||||||||||||||||||||||||||||||||||
| 316 | 23 | ▶ | my $error = !( defined $peer && Langertha::HTTP::UserAgent::same_address( $peer, $address ) ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition60%3/5
defined $peer && Langertha::HTTP::UserAgent::same_address($peer, $address)
$peer // "an unknown peer"
MC/DC50%1/2
defined $peer && Langertha::HTTP::UserAgent::same_address($peer, $address)
defined $peer
Langertha::HTTP::UserAgent::same_address($peer, $address)
MC/DC0%0/1
$peer // "an unknown peer"
$peer
Truth table66%2/3
A && B
A= defined $peerB= Langertha::HTTP::UserAgent::same_address($peer, $address)
Truth table50%1/2
A
A= $peer
| |||||||||||||||||||||||||||||||||||||||||||
| 317 | ? "connect_address $address was not used: the connection goes to " . ( $peer // 'an unknown peer' ) | ||||||||||||||||||||||||||||||||||||||||||
| 318 | : undef; | ||||||||||||||||||||||||||||||||||||||||||
| 319 | 23 | ▶ | if ( !$error && $check_chain ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $error and $check_chain
MC/DC100%2/2
not $error and $check_chain
not $error
$check_chain
Truth table100%3/3
not(A) and B
A= not $errorB= $check_chain
| |||||||||||||||||||||||||||||||||||||||||||
| 320 | 8 | my $tls = Langertha::HTTP::UserAgent::tls_identity_error( $handle, $host, | |||||||||||||||||||||||||||||||||||||||||
| 321 | chain => 1, name => $check_name ); | ||||||||||||||||||||||||||||||||||||||||||
| 322 | 8 | ▶ | $error = "connect_address $address: $tls" if $tls; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||||||||||||||||
| 324 | 23 | ▶ | if ($error) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 325 | 4 | ▶ | $conn->loop->later( sub { $conn->close if $conn->read_handle && $conn->is_idle } ) if $conn->loop; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=12.0
Branch: 2 branches
Condition33%1/3
$conn->read_handle and $conn->is_idle
MC/DC0%0/2
$conn->read_handle and $conn->is_idle
$conn->read_handle
$conn->is_idle
Truth table33%1/3
A and B
A= $conn->read_handleB= $conn->is_idle
| |||||||||||||||||||||||||||||||||||||||||||
| 326 | 4 | return Future->fail( "$class: $error\n", 'connect_address' ); | |||||||||||||||||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||||||||||||||||
| 328 | 19 | ▶ | return $caller_on_ready ? $caller_on_ready->($conn) : Future->done; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 329 | 24 | }; | |||||||||||||||||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||||
| 332 | # Why this Net::Async::HTTP request could not be pinned, or undef (karr k375). | ||||||||||||||||||||||||||||||||||||||||||
| 333 | 23 | ▶ | sub _nahttp_pin_error { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_nahttp_pin_error: called CC=7 SCAR=19.8
| |||||||||||||||||||||||||||||||||||||||||||
| 334 | 23 | my ( $self, $http, $args ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 335 | 23 | my $address = $self->connect_address; | |||||||||||||||||||||||||||||||||||||||||
| 336 | # With uri => Net::Async::HTTP sets host from the URI itself, over ours. | ||||||||||||||||||||||||||||||||||||||||||
| 337 | return "connect_address $address needs a request => HTTP::Request, not uri =>" | ||||||||||||||||||||||||||||||||||||||||||
| 338 | 23 | ▶ | unless $args->{request}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 339 | # A proxy connection goes to the proxy, which resolves the name itself. The | ||||||||||||||||||||||||||||||||||||||||||
| 340 | # client's own proxy settings are private state (hash fields in 0.50). | ||||||||||||||||||||||||||||||||||||||||||
| 341 | 22 | ▶ | my $client = ( reftype($http) // '' ) eq 'HASH' ? $http : {}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
builtin::reftype($http) // ""
MC/DC0%0/1
builtin::reftype($http) // ""
builtin::reftype($http)
Truth table50%1/2
A
A= builtin::reftype($http)
| |||||||||||||||||||||||||||||||||||||||||||
| 342 | 22 | for my $key (qw( proxy_host proxy_path )) { | |||||||||||||||||||||||||||||||||||||||||
| 343 | return "connect_address $address cannot be used through a proxy ($key)" | ||||||||||||||||||||||||||||||||||||||||||
| 344 | 43 | ▶ | if defined $args->{$key} || defined $client->{$key}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $args->{$key} or defined $client->{$key}
MC/DC50%1/2
defined $args->{$key} or defined $client->{$key}
defined $args->{$key}
defined $client->{$key}
Truth table66%2/3
A or B
A= defined $args->{$key}B= defined $client->{$key}
| |||||||||||||||||||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||||||||||||||||
| 346 | 21 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||||||||||
| 349 | # The on_header (Net::Async::HTTP contract) that counts a non-streaming response | ||||||||||||||||||||||||||||||||||||||||||
| 350 | # body's decoded bytes and trips the abort once they pass $max (karr k346). | ||||||||||||||||||||||||||||||||||||||||||
| 351 | # Mirrors the inline-image counter (Langertha::Content::Image::_fetch_net_async_f): | ||||||||||||||||||||||||||||||||||||||||||
| 352 | # the body accumulates on the header response so the finished HTTP::Response still | ||||||||||||||||||||||||||||||||||||||||||
| 353 | # carries it, and the abort runs on the loop's next tick because closing the | ||||||||||||||||||||||||||||||||||||||||||
| 354 | # connection from inside the read handler is not safe. | ||||||||||||||||||||||||||||||||||||||||||
| 355 | 164 | ▶ | sub _body_cap_on_header { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_body_cap_on_header: called CC=1 SCAR=0.1
| |||||||||||||||||||||||||||||||||||||||||||
| 356 | 164 | my ( $self, $http, $max, $too_big_ref, $abort ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 357 | 137 | ▶ | return sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.2
| |||||||||||||||||||||||||||||||||||||||||||
| 358 | 137 | my ($header) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 359 | 2 | ▶ | my $trip = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||||||||||||||||
| 360 | 2 | ▶ | return if ${$too_big_ref}++; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 361 | 2 | ▶ | $http->loop->later( sub { $abort->done unless $abort->is_ready } ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 362 | 137 | }; | |||||||||||||||||||||||||||||||||||||||||
| 363 | 137 | my $length = $header->content_length; | |||||||||||||||||||||||||||||||||||||||||
| 364 | 137 | ▶ | $trip->() if $header->is_success && defined $length && $length > $max; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$header->is_success and defined $length and $length > $max
$header->is_success and defined $length
MC/DC0%0/3
$header->is_success and defined $length and $length > $max
$header->is_success
defined $length
$length > $max
Truth table50%2/4
A and B and C
A= $header->is_successB= defined $lengthC= $length > $max
| |||||||||||||||||||||||||||||||||||||||||||
| 365 | 274 | ▶ | return sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||||||||||||||||
| 366 | 274 | ▶ | return $header unless @_; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 367 | 137 | ▶ | return if ${$too_big_ref}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 368 | 137 | ▶ | $header->add_content( $_[0] ) if defined $_[0]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 369 | 137 | ▶ | $trip->() if length( ${ $header->content_ref } ) > $max; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 370 | 137 | return; | |||||||||||||||||||||||||||||||||||||||||
| 371 | 137 | }; | |||||||||||||||||||||||||||||||||||||||||
| 372 | 164 | }; | |||||||||||||||||||||||||||||||||||||||||
| 373 | } | ||||||||||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||||||
| 376 | 32 | ▶ | sub async_loop { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
async_loop: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 377 | 32 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 378 | 32 | my $http = $self->_async_http; | |||||||||||||||||||||||||||||||||||||||||
| 379 | 32 | ▶ | return $http->can('loop') ? $http->loop : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||||||||||
| 384 | 181 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||||||||||||
| 386 | __END__ | ||||||||||||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||||||||||||
| 388 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||||||||||
| 390 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||||||||||
| 392 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||||||||||||||||
| 394 | Langertha::Role::AsyncHTTP - Async HTTP backend selection (injected > Net::Async::HTTP > sync LWP fallback) | ||||||||||||||||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||||||||||||||||
| 396 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||||||||||||||||
| 398 | version 0.503 | ||||||||||||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||||||||||||
| 400 | =head2 _async_http | ||||||||||||||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||||||||||||
| 402 | The backend that satisfies the async C<do_request> contract: | ||||||||||||||||||||||||||||||||||||||||||
| 403 | C<< do_request( request => $req [, on_header => sub {...}] ) >> returning a | ||||||||||||||||||||||||||||||||||||||||||
| 404 | L<Future> that resolves to an L<HTTP::Response>. It is the injection seam — | ||||||||||||||||||||||||||||||||||||||||||
| 405 | pass C<< _async_http => $client >> at construction to bring your own client | ||||||||||||||||||||||||||||||||||||||||||
| 406 | (any object with that method) and it is used verbatim. | ||||||||||||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||||||||||||
| 408 | When not injected the builder selects, in order: L<Net::Async::HTTP> if it can | ||||||||||||||||||||||||||||||||||||||||||
| 409 | be loaded (a real async client added to L</_async_loop>, built with | ||||||||||||||||||||||||||||||||||||||||||
| 410 | C<< pipeline => 0 >>); otherwise | ||||||||||||||||||||||||||||||||||||||||||
| 411 | L<Langertha::Request::SyncHTTP> over the engine's C<user_agent>, warning once | ||||||||||||||||||||||||||||||||||||||||||
| 412 | per process. The warning names the caller's own C<_f> call site; if | ||||||||||||||||||||||||||||||||||||||||||
| 413 | L<Net::Async::HTTP> is installed but fails to load (for example a missing | ||||||||||||||||||||||||||||||||||||||||||
| 414 | L<IO::Async> sub-dependency) it says so and includes the first line of the | ||||||||||||||||||||||||||||||||||||||||||
| 415 | load error instead of claiming the module is unavailable. The sync fallback runs HTTP B<synchronously and sequentially> | ||||||||||||||||||||||||||||||||||||||||||
| 416 | (blocking, no concurrency) — every C<_f> call still works and returns a | ||||||||||||||||||||||||||||||||||||||||||
| 417 | L<Future>, but multiple calls awaited "in parallel" run one after another. | ||||||||||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||||||||||
| 419 | The L<Net::Async::HTTP> client does not pipeline HTTP/1.1 requests: a request | ||||||||||||||||||||||||||||||||||||||||||
| 420 | pipelined behind a long LLM stream would wait for it anyway, and would fail | ||||||||||||||||||||||||||||||||||||||||||
| 421 | with C<Connection closed> if that stream were cancelled or aborted. It keeps | ||||||||||||||||||||||||||||||||||||||||||
| 422 | the library's other defaults, including C<max_connections_per_host> (one | ||||||||||||||||||||||||||||||||||||||||||
| 423 | keep-alive connection per host; the C<NET_ASYNC_HTTP_MAXCONNS> environment | ||||||||||||||||||||||||||||||||||||||||||
| 424 | variable changes it), so concurrent requests on one engine are sent one after | ||||||||||||||||||||||||||||||||||||||||||
| 425 | another on that connection. Concurrent requests through different engines use | ||||||||||||||||||||||||||||||||||||||||||
| 426 | their own clients. Inject a client configured otherwise to change either. | ||||||||||||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||||||||||||
| 428 | =head2 async_request_f | ||||||||||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||||||||||
| 430 | my $response = await $engine->async_request_f($http_request); | ||||||||||||||||||||||||||||||||||||||||||
| 431 | die $response->status_line unless $response->is_success; | ||||||||||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||||||||||
| 433 | # streaming: on_header passes through to the backend | ||||||||||||||||||||||||||||||||||||||||||
| 434 | await $engine->async_request_f($http_request, on_header => sub { ... }); | ||||||||||||||||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||||||||||||||||
| 436 | Sends a prepared L<HTTP::Request> (for example from C<chat_request> or | ||||||||||||||||||||||||||||||||||||||||||
| 437 | C<build_tool_chat_request>) through the engine's selected backend | ||||||||||||||||||||||||||||||||||||||||||
| 438 | (L</_async_http>) and returns a L<Future> that resolves to the | ||||||||||||||||||||||||||||||||||||||||||
| 439 | L<HTTP::Response>. This is the public face of the C<do_request> contract, for | ||||||||||||||||||||||||||||||||||||||||||
| 440 | callers outside core that assemble their own requests. On the synchronous | ||||||||||||||||||||||||||||||||||||||||||
| 441 | fallback the returned future is already complete. | ||||||||||||||||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||||||||||||||||
| 443 | An HTTP error status (4xx/5xx) B<resolves> the future on every backend: check | ||||||||||||||||||||||||||||||||||||||||||
| 444 | C<is_success> on the response. A B<transport-level> failure (connection | ||||||||||||||||||||||||||||||||||||||||||
| 445 | refused, DNS, timeout) is B<not> uniform across backends: on | ||||||||||||||||||||||||||||||||||||||||||
| 446 | L<Net::Async::HTTP> it B<fails> the future with the socket error, while on the | ||||||||||||||||||||||||||||||||||||||||||
| 447 | synchronous fallback it B<resolves> with the 500 response LWP synthesizes | ||||||||||||||||||||||||||||||||||||||||||
| 448 | (C<500 Can't connect ...>, header C<Client-Warning: Internal response>) — the | ||||||||||||||||||||||||||||||||||||||||||
| 449 | future does not fail. Either way the call did not succeed, so always check | ||||||||||||||||||||||||||||||||||||||||||
| 450 | C<is_success>; do not rely on a failed future alone to catch a dead endpoint. | ||||||||||||||||||||||||||||||||||||||||||
| 451 | See ADR 0027 for the parity scope. | ||||||||||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||||||||
| 453 | When the engine has a L<Langertha::Role::HTTP/user_agent_timeout> and the | ||||||||||||||||||||||||||||||||||||||||||
| 454 | backend is a L<Net::Async::HTTP>, it is applied here like on the engine's own | ||||||||||||||||||||||||||||||||||||||||||
| 455 | C<_f> calls: as the total C<timeout> for a plain request, as the | ||||||||||||||||||||||||||||||||||||||||||
| 456 | C<stall_timeout> (time without a byte) when C<on_header> is given. On expiry | ||||||||||||||||||||||||||||||||||||||||||
| 457 | the future B<fails> with C<< <engine class>: request to <url> timed out after | ||||||||||||||||||||||||||||||||||||||||||
| 458 | Ns >> (query string and userinfo left out of the URL) and the category | ||||||||||||||||||||||||||||||||||||||||||
| 459 | C<timeout> or C<stall_timeout>. Passing your own C<timeout> or | ||||||||||||||||||||||||||||||||||||||||||
| 460 | C<stall_timeout> option overrides it. | ||||||||||||||||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||||||||||||||||
| 462 | On the L<Net::Async::HTTP> backend the modules it loads only when it connects | ||||||||||||||||||||||||||||||||||||||||||
| 463 | are checked before the request is handed to it: L<IO::Async::Internals::Connector>, | ||||||||||||||||||||||||||||||||||||||||||
| 464 | and L<IO::Async::SSL> for C<https>. If one fails to load, the future B<fails> | ||||||||||||||||||||||||||||||||||||||||||
| 465 | with C<< <engine class>: cannot connect to <scheme>://<host>:<port>: <module> | ||||||||||||||||||||||||||||||||||||||||||
| 466 | failed to load (<reason>); ... >> and the category C<connect>. Without the check | ||||||||||||||||||||||||||||||||||||||||||
| 467 | L<Net::Async::HTTP> 0.50 would keep the host's connection slot taken by the | ||||||||||||||||||||||||||||||||||||||||||
| 468 | connection that never opened, and every later request to that host would wait | ||||||||||||||||||||||||||||||||||||||||||
| 469 | forever (karr k353). Inline image fetches through such a client | ||||||||||||||||||||||||||||||||||||||||||
| 470 | (L<Langertha::Content::Image/ensure_base64_f>) are checked the same way. | ||||||||||||||||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||||||||||||||||
| 472 | Redirects follow L<Langertha::HTTP::Redirect> on every backend core builds: | ||||||||||||||||||||||||||||||||||||||||||
| 473 | on L<Net::Async::HTTP> they are followed here one hop at a time (the client | ||||||||||||||||||||||||||||||||||||||||||
| 474 | itself is told C<< max_redirects => 0 >>), on the synchronous fallback by the | ||||||||||||||||||||||||||||||||||||||||||
| 475 | engine's L<Langertha::HTTP::UserAgent>. Only C<GET> and C<HEAD> are followed, | ||||||||||||||||||||||||||||||||||||||||||
| 476 | never from C<https> to C<http>; a redirect on the same origin keeps the request | ||||||||||||||||||||||||||||||||||||||||||
| 477 | as it was, one to another origin drops every header but the representation | ||||||||||||||||||||||||||||||||||||||||||
| 478 | ones (so no credential header of any name goes along), the URL's userinfo, and | ||||||||||||||||||||||||||||||||||||||||||
| 479 | any query value the request carried as a credential. C<http://host> to | ||||||||||||||||||||||||||||||||||||||||||
| 480 | C<https://host> counts as another origin, so a keyed GET behind such a redirect | ||||||||||||||||||||||||||||||||||||||||||
| 481 | arrives without its key and gets a 401: configure the C<https> URL. A C<POST> | ||||||||||||||||||||||||||||||||||||||||||
| 482 | is never followed. A redirect that is not followed resolves the future with | ||||||||||||||||||||||||||||||||||||||||||
| 483 | the 3xx response, with a C<Client-Warning> header naming the reason | ||||||||||||||||||||||||||||||||||||||||||
| 484 | (C<redirect not followed: Langertha::HTTP::Redirect: ...>, also when the hop | ||||||||||||||||||||||||||||||||||||||||||
| 485 | limit ran out). The hop limit is a C<max_redirects> option if given, else the | ||||||||||||||||||||||||||||||||||||||||||
| 486 | client's own (3 by default); the C<timeout> applies per hop. With | ||||||||||||||||||||||||||||||||||||||||||
| 487 | C<on_header> the callback sees only the response the chain ends on, a | ||||||||||||||||||||||||||||||||||||||||||
| 488 | redirect that was not followed included (karr k374). A request passed as | ||||||||||||||||||||||||||||||||||||||||||
| 489 | C<< uri => >> instead of C<< request => >> is not followed at all. An injected | ||||||||||||||||||||||||||||||||||||||||||
| 490 | client of another class follows redirects on its own terms. | ||||||||||||||||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||||||||||||||||
| 492 | With a L<Langertha::Role::HTTP/connect_address>, a request to the host of | ||||||||||||||||||||||||||||||||||||||||||
| 493 | the engine's C<url> connects to that address: on L<Net::Async::HTTP> the | ||||||||||||||||||||||||||||||||||||||||||
| 494 | request is given C<host> / C<port> as the connection target and, for | ||||||||||||||||||||||||||||||||||||||||||
| 495 | C<https>, C<SSL_hostname> and C<SSL_verifycn_name> naming the host (the | ||||||||||||||||||||||||||||||||||||||||||
| 496 | C<Host> header comes from the request URL as always), and an C<on_ready> | ||||||||||||||||||||||||||||||||||||||||||
| 497 | check that the connection (new or pooled) goes to the address and, over TLS, | ||||||||||||||||||||||||||||||||||||||||||
| 498 | was verified for the host; on the synchronous fallback the engine's pinned | ||||||||||||||||||||||||||||||||||||||||||
| 499 | L<Langertha::HTTP::UserAgent> does it. A | ||||||||||||||||||||||||||||||||||||||||||
| 500 | redirect from the pinned host to another host is not followed. A client that | ||||||||||||||||||||||||||||||||||||||||||
| 501 | cannot pin (an injected client of another class, the shim over an agent | ||||||||||||||||||||||||||||||||||||||||||
| 502 | without the same pin, a L<Net::Async::HTTP> with a proxy, a C<< uri => >> | ||||||||||||||||||||||||||||||||||||||||||
| 503 | request) fails the future with category C<connect_address> instead of | ||||||||||||||||||||||||||||||||||||||||||
| 504 | sending the request (karr k375). | ||||||||||||||||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||||||||||||||||
| 506 | Any extra named options (such as C<on_header> for streaming) are passed to | ||||||||||||||||||||||||||||||||||||||||||
| 507 | C<do_request> unchanged. The backend object itself is not exposed; see | ||||||||||||||||||||||||||||||||||||||||||
| 508 | L</async_loop> for the event loop. | ||||||||||||||||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||||||||||||||||
| 510 | On the L<Net::Async::HTTP> backend a B<non-streaming> request (no C<on_header>) | ||||||||||||||||||||||||||||||||||||||||||
| 511 | has its decoded response body bounded by | ||||||||||||||||||||||||||||||||||||||||||
| 512 | L<Langertha::Role::HTTP/response_max_bytes>: the client inflates a | ||||||||||||||||||||||||||||||||||||||||||
| 513 | C<Content-Encoding> as it streams, so the decoded bytes are counted and the | ||||||||||||||||||||||||||||||||||||||||||
| 514 | request is aborted past the ceiling, failing the future with C<< <engine class> | ||||||||||||||||||||||||||||||||||||||||||
| 515 | response body exceeds response_max_bytes (<n>) >> (a decompression-bomb guard, | ||||||||||||||||||||||||||||||||||||||||||
| 516 | karr k346). A streaming request (with C<on_header>) is not bounded. Set | ||||||||||||||||||||||||||||||||||||||||||
| 517 | C<response_max_bytes> to C<0> to disable. | ||||||||||||||||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||||||||||||||||
| 519 | =head2 async_loop | ||||||||||||||||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||||||||||||||||
| 521 | my $loop = $engine->async_loop // IO::Async::Loop->new; | ||||||||||||||||||||||||||||||||||||||||||
| 522 | $loop->add($notifier); | ||||||||||||||||||||||||||||||||||||||||||
| 523 | await $loop->delay_future( after => 2 ); | ||||||||||||||||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||||||||||||||||
| 525 | Returns the event loop of the active async backend (L</_async_http>), or | ||||||||||||||||||||||||||||||||||||||||||
| 526 | C<undef> — a C<Maybe[loop]>. Core promises no loop (see L</EVENT LOOP>): | ||||||||||||||||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||||||||||||||||
| 528 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||||||||||||||||
| 530 | =item * an injected client that has a C<loop> method: that client's loop, | ||||||||||||||||||||||||||||||||||||||||||
| 531 | whatever loop the caller put it on; | ||||||||||||||||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||||||||||||||||
| 533 | =item * the default L<Net::Async::HTTP> backend: the loop it was added to | ||||||||||||||||||||||||||||||||||||||||||
| 534 | (L</_async_loop>); | ||||||||||||||||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||||||||||||||||
| 536 | =item * the synchronous fallback (L<Langertha::Request::SyncHTTP>) or an | ||||||||||||||||||||||||||||||||||||||||||
| 537 | injected client without a C<loop> method: C<undef>. | ||||||||||||||||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||||||||||||||||
| 539 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||||||||||||||||
| 541 | Calling it selects the backend if that has not happened yet (so on a clean | ||||||||||||||||||||||||||||||||||||||||||
| 542 | install it may emit the one-time fallback warning). Code that needs a loop for | ||||||||||||||||||||||||||||||||||||||||||
| 543 | its own notifiers or timers should use this loop when it is defined, so its | ||||||||||||||||||||||||||||||||||||||||||
| 544 | futures and the engine's HTTP futures are driven by the same loop; awaiting | ||||||||||||||||||||||||||||||||||||||||||
| 545 | futures from two different loops in one chain can hang. | ||||||||||||||||||||||||||||||||||||||||||
| 546 | |||||||||||||||||||||||||||||||||||||||||||
| 547 | =head1 EVENT LOOP | ||||||||||||||||||||||||||||||||||||||||||
| 548 | |||||||||||||||||||||||||||||||||||||||||||
| 549 | Core promises no event loop: L<IO::Async> is only recommended, an injected | ||||||||||||||||||||||||||||||||||||||||||
| 550 | client may run on any loop, and the synchronous fallback runs on none. | ||||||||||||||||||||||||||||||||||||||||||
| 551 | L</async_loop> reports the backend's loop when there is one and C<undef> | ||||||||||||||||||||||||||||||||||||||||||
| 552 | otherwise. When it is C<undef> a caller that needs a loop brings its own, | ||||||||||||||||||||||||||||||||||||||||||
| 553 | typically C<< IO::Async::Loop->new >> (the process-wide loop). | ||||||||||||||||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||||||||||||||||
| 555 | The backend's loop is not necessarily the process-wide one: an injected | ||||||||||||||||||||||||||||||||||||||||||
| 556 | L</_async_http> client can live on any loop, and L</_async_loop> is itself a | ||||||||||||||||||||||||||||||||||||||||||
| 557 | constructor argument (C<< _async_loop => $my_loop >>), in which case the | ||||||||||||||||||||||||||||||||||||||||||
| 558 | default L<Net::Async::HTTP> backend is added to that loop. L</async_loop> | ||||||||||||||||||||||||||||||||||||||||||
| 559 | returns the right loop in all of these cases. | ||||||||||||||||||||||||||||||||||||||||||
| 560 | |||||||||||||||||||||||||||||||||||||||||||
| 561 | =head2 _async_loop | ||||||||||||||||||||||||||||||||||||||||||
| 562 | |||||||||||||||||||||||||||||||||||||||||||
| 563 | The L<IO::Async::Loop> the real-async client is added to. Built lazily and | ||||||||||||||||||||||||||||||||||||||||||
| 564 | B<only> on the L<Net::Async::HTTP> path; the sync fallback never touches it, | ||||||||||||||||||||||||||||||||||||||||||
| 565 | so no event loop is created when running synchronously. The default is | ||||||||||||||||||||||||||||||||||||||||||
| 566 | C<< IO::Async::Loop->new >>, the process-wide loop; it can be passed at | ||||||||||||||||||||||||||||||||||||||||||
| 567 | construction to put the default backend on another loop (see L</EVENT LOOP>). | ||||||||||||||||||||||||||||||||||||||||||
| 568 | Use L</async_loop> to read the backend's loop. | ||||||||||||||||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||||||||||||||||
| 570 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||||||||||||||||
| 572 | =head2 Issues | ||||||||||||||||||||||||||||||||||||||||||
| 573 | |||||||||||||||||||||||||||||||||||||||||||
| 574 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||||||||||||||||
| 575 | L<https://github.com/Getty/langertha/issues>. | ||||||||||||||||||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||||||||||||||||
| 577 | =head2 IRC | ||||||||||||||||||||||||||||||||||||||||||
| 578 | |||||||||||||||||||||||||||||||||||||||||||
| 579 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||||||||||||||||
| 580 | |||||||||||||||||||||||||||||||||||||||||||
| 581 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||||||||||||||||
| 583 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||||||||||||||||
| 585 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||||||||||||||||
| 587 | Torsten Raudssus <getty@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||||||||||||||||
| 589 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 590 | |||||||||||||||||||||||||||||||||||||||||||
| 591 | This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>. | ||||||||||||||||||||||||||||||||||||||||||
| 592 | |||||||||||||||||||||||||||||||||||||||||||
| 593 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||
| 594 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||||||||||||||||
| 596 | =cut | ||||||||||||||||||||||||||||||||||||||||||