blib/lib/Langertha/Role/AsyncHTTP.pm

filter: Statementstmt 84.5% 202 / 239 Branchbran 76.2% 122 / 160 Conditioncond 56.1% 69 / 123 MC/DCmcdc 30.8% 21 / 68 Subroutinesub 87.1% 34 / 39 Podpod 50.0% 2 / 4 totaltotal 71.0% 450 / 633 CC 225 SCAR 26.2
CC
225
Cov
75%
_async_do_request_f
27.9
_async_do_request_f
27.9
_async_one_request_f
26.9
CRAP 13.7
« blib/lib/Langertha/Role/Transcription.pm ↑ Summary blib/lib/Langertha/Response.pm »
1 package Langertha::Role::AsyncHTTP;
2 # ABSTRACT: Async HTTP backend selection (injected > Net::Async::HTTP > sync LWP fallback)
3 196 our $VERSION = '0.505';
4 196 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 196 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 196 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 196 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 196 ▶use Scalar::Util qw( blessed reftype );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 196 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 196 requires 'user_agent';
12  
13 196 my $WARNED = 0;
14  
15 196 has _async_loop => (
16 is => 'ro',
17 lazy_build => 1,
18 );
19  
20 224 ▶sub _build__async_loop {
Subroutine _build__async_loop: called CC=1 SCAR=1.2
21 0 ▶ require IO::Async::Loop;
Subroutine _build__async_loop: not called CC=1 SCAR=1.2
22 0 return IO::Async::Loop->new;
23 }
24  
25 196 has _async_http => (
26 is => 'ro',
27 lazy_build => 1,
28 );
29  
30 237 ▶sub _build__async_http {
Subroutine _build__async_http: called CC=4 SCAR=17.9
31 0 ▶ my ($self) = @_;
Subroutine _build__async_http: not called CC=4 SCAR=17.9
32 0 my $loaded = eval { require Net::Async::HTTP; 1 };
33 0 my $load_error = $@;
34 0 ▶ if ($loaded) {
Branch: 2 branches
BranchTrueFalseTotal
if ($loaded) 232 5 237
if ($loaded) 0 0 0
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 0 my $http = Net::Async::HTTP->new( pipeline => 0 );
39 0 $self->_async_loop->add($http);
40 0 $self->_set_async_http_owned(1);
41 0 return $http;
42 }
43 0 ▶ unless ($WARNED) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($WARNED) 4 1 5
unless ($WARNED) 0 0 0
44 0 $WARNED = 1;
45 # A missing module is the expected, documented case. Anything else (a
46 # missing IO::Async sub-dependency after a partial upgrade, a compile
47 # error) is a broken install the user should see, not "not available".
48 0 ▶ my $reason = $load_error =~ m{\ACan't locate Net/Async/HTTP\.pm }
Branch: 2 branches
BranchTrueFalseTotal
$load_error =~ m[\ACan't locate Net/Async/HTTP\.pm ] ? : 1 3 4
$load_error =~ m[\ACan't locate Net/Async/HTTP\.pm ] ? : 0 0 0
49 ? 'Net::Async::HTTP not available'
50 : 'Net::Async::HTTP failed to load (' . ( split /\n/, $load_error )[0] . ')';
51 0 warn "$reason; Langertha is running HTTP synchronously "
52 . "(no concurrency). Install Net::Async::HTTP + IO::Async for real async."
53 . _caller_location() . "\n";
54 }
55 0 require Langertha::Request::SyncHTTP;
56 0 return Langertha::Request::SyncHTTP->new( user_agent => $self->user_agent );
57 }
58  
59 # Set by the builder when it added its own Net::Async::HTTP client to the loop:
60 # that client is the engine's to take off again. An injected client is the
61 # caller's and never sets it (karr k379).
62 196 has _async_http_owned => (
63 is => 'ro',
64 init_arg => undef,
65 writer => '_set_async_http_owned',
66 );
67  
68 # The loop holds every notifier added to it until it is removed, so without
69 # this each engine left its client (and the client's keep-alive connections)
70 # on the loop for the rest of the process (karr k379). An engine with a
71 # request in flight is not destroyed: the suspended _f call holds it. At
72 # global destruction the loop may already be gone and the process is ending,
73 # so nothing is done. A class composing this role that defines its own
74 # DEMOLISH replaces this one (Moose role composition) and has to call
75 # _release_async_http itself; a subclass's DEMOLISH runs alongside it.
76 6024 ▶sub DEMOLISH {
Subroutine DEMOLISH: called CC=2 SCAR=10.5
Pod undocumented
77 0 ▶ my ( $self, $in_global_destruction ) = @_;
Subroutine DEMOLISH: not called CC=2 SCAR=10.5
Pod undocumented
78 0 ▶ $self->_release_async_http unless $in_global_destruction;
Branch: 2 branches
BranchTrueFalseTotal
unless $in_global_destruction 6024 0 6024
unless $in_global_destruction 0 0 0
79 0 return;
80 }
81  
82 6024 ▶sub _release_async_http {
Subroutine _release_async_http: called CC=5 SCAR=22.1
83 0 ▶ my ($self) = @_;
Subroutine _release_async_http: not called CC=5 SCAR=22.1
84 0 ▶ return unless $self->_async_http_owned && $self->_has_async_http;
Branch: 2 branches
BranchTrueFalseTotal
unless $self->_async_http_owned and $self->_has_async_http 5628 396 6024
unless $self->_async_http_owned and $self->_has_async_http 0 0 0
Condition33%2/6
$self->_async_http_owned and $self->_has_async_http
!ll&&!rl&&r
56280396
$self->_async_http_owned and $self->_has_async_http
!ll&&!rl&&r
000
MC/DC50%1/2
$self->_async_http_owned and $self->_has_async_http
$self->_async_http_owned $self->_has_async_http
MC/DC0%0/2
$self->_async_http_owned and $self->_has_async_http
$self->_async_http_owned $self->_has_async_http
Truth table66%2/3
A and B
A= $self->_async_http_ownedB= $self->_has_async_http
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $self->_async_http_ownedB= $self->_has_async_http
ABresult
0X0
100
111
85 0 my $http = $self->_async_http;
86 0 my $loop = $http->loop;
87 # Already off the loop, or made a child of another notifier by someone who
88 # reached for the private client: then it is not ours to remove.
89 0 ▶ return unless $loop && !$http->parent;
Branch: 2 branches
BranchTrueFalseTotal
unless $loop and not $http->parent 177 219 396
unless $loop and not $http->parent 0 0 0
Condition33%2/6
$loop and not $http->parent
!ll&&!rl&&r
1770219
$loop and not $http->parent
!ll&&!rl&&r
000
MC/DC50%1/2
$loop and not $http->parent
$loop not $http->parent
MC/DC0%0/2
$loop and not $http->parent
$loop not $http->parent
Truth table66%2/3
A and not(B)
A= $loopB= not $http->parent
ABresult
0X0
100
111
Truth table0%0/3
A and not(B)
A= $loopB= not $http->parent
ABresult
0X0
100
111
90 0 local $@;
91 0 eval { $loop->remove($http); 1 };
92 0 return;
93 }
94  
95 # The builder runs inside a generated Moose accessor called from a Langertha
96 # role (chat_f, poll_metrics_f, ...), so carp would point into that plumbing.
97 # Report the first frame outside Langertha, Moose and the Future machinery:
98 # the user's own call site.
99 4 ▶sub _caller_location {
Subroutine _caller_location: called CC=3 SCAR=16.2
100 0 ▶ my $level = 0;
Subroutine _caller_location: not called CC=3 SCAR=16.2
101 0 ▶ while ( my ( $package, $file, $line ) = caller $level++ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (() = caller $level++) 19 0 19
if (() = caller $level++) 0 0 0
102 0 ▶ next if $package =~ /\A(?:Langertha|Moose|Class::MOP|Eval::Closure|Future)(?:::|\z)/;
Branch: 2 branches
BranchTrueFalseTotal
if $package =~ /\A(?:Langertha|Moose|Class::MOP|Eval::Closure|Future)(?:::|\z)/ 15 4 19
if $package =~ /\A(?:Langertha|Moose|Class::MOP|Eval::Closure|Future)(?:::|\z)/ 0 0 0
103 0 return " at $file line $line.";
104 }
105 0 return '';
106 }
107  
108  
109 68 ▶async sub async_request_f {
Subroutine async_request_f: called CC=1 SCAR=0.0
Pod documented
110 68 my ( $self, $request, %opts ) = @_;
111 68 return await $self->_async_do_request_f( request => $request, %opts );
112 196 }
113  
114 # Every async request core sends goes through here (karr k278, ADR 0027).
115 # Net::Async::HTTP sets no timeout of its own, so a provider that accepts and
116 # never answers left the Future pending forever. When the engine has a
117 # user_agent_timeout it becomes Net::Async::HTTP's per-request timeout: the
118 # total time for a plain request, the time without a byte (stall_timeout) for
119 # a streaming one (on_header), where a long steady stream is legitimate. The
120 # failure is rewritten to name the engine and the URL (query and userinfo
121 # dropped: Gemini carries its key in the query). A caller's own timeout /
122 # stall_timeout option wins. The sync shim already has the timeout on its
123 # LWP user agent, and an injected client of another class keeps its own.
124 958 ▶sub _async_do_request_f {
Subroutine _async_do_request_f: called CC=22 SCAR=33.3
125 958 my ( $self, %args ) = @_;
126 958 my $http = $self->_async_http;
127 958 ▶ my $is_nahttp = blessed($http) && $http->isa('Net::Async::HTTP');
Condition66%2/3
builtin::blessed($http) && $http->isa('Net::Async::HTTP')
!ll&&!rl&&r
0710248
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')
ABresult
0X0
100
111
128 # connect_address (karr k375): only Net::Async::HTTP (per-request host and
129 # SSL_* options, _async_one_request_f) and the sync shim over a pinned
130 # Langertha::HTTP::UserAgent can pin; any other client would resolve the
131 # name itself, so a request to the pinned host fails instead of going out.
132 958 ▶ if ( my $pin_host = $self->can('_connect_host') ? $self->_connect_host : undef ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $pin_host = $self->can("_connect_host") ? $self->_connect_host : undef) 36 922 958
$self->can('_connect_host') ? : 958 0 958
133 36 ▶ my $target = $args{request} ? $args{request}->uri : $args{uri};
Branch: 1 branch
BranchTrueFalseTotal
$args{'request'} ? : 35 1 36
134 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
!ll&&!rl&&r
0135
defined $target and "URI"->new("$target")->can("host")
!ll&&!rl&&r
0036
"URI"->new("$target")->host // ""
l!l
360
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
ABCresult
0XX0
10X0
1100
1111
135 && lc( URI->new("$target")->host // '' ) eq $pin_host;
136 36 ▶ if ($to_pinned) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to_pinned) 35 1 36
137 35 ▶ my $error = $is_nahttp ? $self->_nahttp_pin_error( $http, \%args )
Branch: 2 branches
BranchTrueFalseTotal
$is_nahttp ? : 23 12 35
builtin::blessed($http) && $http->isa('Langertha::Request::SyncHTTP') ? : 11 1 12
Condition60%3/5
builtin::blessed($http) and $http->isa("Langertha::Request::SyncHTTP")
!ll&&!rl&&r
0111
builtin::blessed($http) // "unblessed"
l!l
10
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")
ABresult
0X0
100
111
Truth table50%1/2
A
A= builtin::blessed($http)
Aresult
11
00
138 : blessed($http) && $http->isa('Langertha::Request::SyncHTTP')
139 ? $self->_connect_pin_error( $http->user_agent )
140 : 'connect_address ' . $self->connect_address . ' cannot be applied through an injected '
141 . ( blessed($http) // 'unblessed' ) . ' client (only Net::Async::HTTP or Langertha::Request::SyncHTTP can pin)';
142 35 ▶ return Future->fail( ref($self) . ": $error\n", 'connect_address' ) if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 4 31 35
143 }
144 }
145 # Only Net::Async::HTTP gets the timeout, the body cap and the redirect policy
146 # below. The sync shim and injected clients hand back the still-encoded body,
147 # which Role::HTTP::_bounded_decoded_content bounds downstream, and they carry
148 # their own timeout (ADR 0027); the sync shim runs the engine's user_agent,
149 # a Langertha::HTTP::UserAgent with the same redirect policy (karr k374).
150 954 ▶ return $http->do_request(%args) unless $is_nahttp;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_nahttp 708 246 954
151  
152 # Redirects (karr k374). Net::Async::HTTP follows a GET redirect with a fresh
153 # request carrying the Location's query as it is, so a server echoing the
154 # request URI sent Gemini's ?key= to the new host, while on the same origin
155 # the credential header was lost. It follows nothing here (max_redirects => 0);
156 # each hop is decided by Langertha::HTTP::Redirect, the policy the sync LWP
157 # agent follows too. A caller's max_redirects is the hop limit, else the
158 # client's own (the max_redirects it was built with, default 3). Only GET and
159 # HEAD are ever followed, so any other request is the single hop it was.
160 # The client's own limit is Net::Async::HTTP private state (checked against
161 # 0.50, where configure stores it in $self->{max_redirects}); read defensively
162 # and fall back to that version's default of 3.
163 my $hops = exists $args{max_redirects} ? delete $args{max_redirects}
164 : ( reftype($http) // '' ) eq 'HASH' && defined $http->{max_redirects} ? $http->{max_redirects}
165 246 ▶ : 3;
Branch: 2 branches
BranchTrueFalseTotal
exists $args{'max_redirects'} ? : 1 245 246
(builtin::reftype($http) // '') eq 'HASH' && defined $http->{'max_redirects'} ? : 245 0 245
Condition40%2/5
(builtin::reftype($http) // "") eq "HASH" and defined $http->{"max_redirects"}
!ll&&!rl&&r
00245
builtin::reftype($http) // ""
l!l
2450
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"}
ABresult
0X0
100
111
166 246 $args{max_redirects} = 0;
167 # A caller passing uri => instead of request => (no core caller does) gets no
168 # redirect following: Net::Async::HTTP builds that request itself, so it is
169 # the single hop max_redirects => 0 allows and a 3xx comes back as it is.
170 246 my $request = $args{request};
171 246 ▶ return $self->_async_one_request_f( $http, %args )
Branch: 1 branch
BranchTrueFalseTotal
unless $hops and $request and uc($request->method) =~ /\A(?:GET|HEAD)\z/ 162 84 246
Condition50%3/6
$hops and $request and uc($request->method) =~ /\A(?:GET|HEAD)\z/
!ll&&!rl&&r
016284
$hops and $request
!ll&&!rl&&r
00246
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/
ABCresult
0XX0
10X0
1100
1111
172 unless $hops && $request && ( uc $request->method ) =~ /\A(?:GET|HEAD)\z/;
173 84 require Langertha::HTTP::Redirect;
174 84 return $self->_async_follow_f( $http, \%args, $hops, undef );
175 }
176  
177 # One hop of a followed chain, then the next one if the policy follows it. A
178 # caller's on_header must not see a redirect that is followed (its body is
179 # discarded, as Net::Async::HTTP does); a redirect that is not followed reaches
180 # it like any other response. Future->then propagates a cancel of the returned
181 # future to the hop in flight.
182 100 ▶sub _async_follow_f {
Subroutine _async_follow_f: called CC=2 SCAR=7.0
183 100 my ( $self, $http, $args, $hops, $previous ) = @_;
184 100 my %hop = %{$args};
185 # Decided once per response (on_header and the completed hop both ask), so a
186 # refusal's Client-Warning is added once.
187 100 my ( $decided, $next );
188 85 ▶ my $follows = sub {
Subroutine __ANON__: called CC=9 SCAR=22.8
189 85 my ($response) = @_;
190 85 ▶ return $next if $decided && $decided == $response;
Branch: 1 branch
BranchTrueFalseTotal
if $decided and $decided == $response 4 81 85
Condition66%2/3
$decided and $decided == $response
!ll&&!rl&&r
8104
MC/DC50%1/2
$decided and $decided == $response
$decided $decided == $response
Truth table66%2/3
A and B
A= $decidedB= $decided == $response
ABresult
0X0
100
111
191 81 $decided = $response;
192 81 ▶ $response->previous($previous) if $previous && !$response->previous;
Branch: 1 branch
BranchTrueFalseTotal
if $previous and not $response->previous 16 65 81
Condition66%2/3
$previous and not $response->previous
!ll&&!rl&&r
65016
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
ABresult
0X0
100
111
193 81 ▶ if ( $hops > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hops > 0) { } 79 2 81
194 79 ▶ $next = Langertha::HTTP::Redirect::next_request( $hop{request}, $response,
Branch: 1 branch
BranchTrueFalseTotal
$self->can('_connect_host') ? : 79 0 79
195 $self->can('_connect_host') ? $self->_connect_host : undef );
196 }
197 else {
198 2 $next = undef;
199 2 ▶ $response->push_header( 'Client-Warning' =>
Branch: 1 branch
BranchTrueFalseTotal
if $response->is_redirect and defined $response->header("Location") 2 0 2
Condition33%1/3
$response->is_redirect and defined $response->header("Location")
!ll&&!rl&&r
002
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")
ABresult
0X0
100
111
200 'redirect not followed: Langertha::HTTP::Redirect: hop limit reached' )
201 if $response->is_redirect && defined $response->header('Location');
202 }
203 81 return $next;
204 100 };
205 100 ▶ if ( my $on_header = $hop{on_header} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $on_header = $hop{"on_header"}) 4 96 100
206 4 ▶ $hop{on_header} = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
207 4 my ($response) = @_;
208 4 ▶ return sub { return @_ ? () : $response } if $follows->($response);
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 2 branches
BranchTrueFalseTotal
if &$follows($response) 1 3 4
@_ ? : 1 1 2
209 3 return $on_header->($response);
210 4 };
211 }
212 81 ▶ return $self->_async_one_request_f( $http, %hop )->then( sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
213 81 my ($response) = @_;
214 81 my $next = $follows->($response);
215 81 ▶ return Future->done($response) unless $next;
Branch: 1 branch
BranchTrueFalseTotal
unless $next 65 16 81
216 16 return $self->_async_follow_f( $http, { %{$args}, request => $next }, $hops - 1, $response );
217 100 } );
218 }
219  
220 # One Net::Async::HTTP request with the connect check, the body cap and the
221 # timeout applied.
222 262 ▶sub _async_one_request_f {
Subroutine _async_one_request_f: called CC=26 SCAR=32.9
223 262 my ( $self, $http, %args ) = @_;
224  
225 # Net::Async::HTTP 0.50 loads IO::Async::Internals::Connector (and
226 # IO::Async::SSL for https) only when it connects; when that load dies, the
227 # dead connection keeps the host's slot and every later request to the host
228 # hangs. Check first, so it is a failed Future naming the module (karr k353).
229 262 ▶ my $target = $args{request} ? $args{request}->uri : $args{uri};
Branch: 1 branch
BranchTrueFalseTotal
$args{'request'} ? : 262 0 262
230 262 ▶ if ( defined $target ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $target) 262 0 262
231 262 require Langertha::HTTP::ConnectCheck;
232 262 my $error = Langertha::HTTP::ConnectCheck::connect_error( $target, $args{SSL} );
233 262 ▶ return Future->fail( ref($self).": $error\n", 'connect' ) if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 4 258 262
234 }
235  
236 # connect_address (karr k375): connect to the checked address, not to a
237 # fresh resolution of the name. Net::Async::HTTP 0.50 uses host/port only as
238 # the connection target (and its pool key); the Host header comes from the
239 # request URI, and for SSL it sets SSL_hostname => host before the request's
240 # own SSL_* options, so these name the host for SNI and the certificate check.
241 # A redirect is never followed by the client here (max_redirects => 0), so
242 # the target cannot leak into another hop.
243 258 ▶ if ( defined $target and my $pin_host = $self->can('_connect_host') ? $self->_connect_host : undef ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $target and my $pin_host = $self->can("_connect_host") ? $self->_connect_host : undef) 24 234 258
$self->can('_connect_host') ? : 258 0 258
Condition66%2/3
defined $target and my $pin_host = $self->can("_connect_host") ? $self->_connect_host : undef
!ll&&!rl&&r
023424
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
ABresult
0X0
100
111
244 24 my $uri = URI->new("$target");
245 24 ▶ if ( $uri->can('host') && lc( $uri->host // '' ) eq $pin_host ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri->can("host") and lc($uri->host // "") eq $pin_host) 23 1 24
Condition60%3/5
$uri->can("host") and lc($uri->host // "") eq $pin_host
!ll&&!rl&&r
0123
$uri->host // ""
l!l
240
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
ABresult
0X0
100
111
246 23 %args = ( %args, _connect_pin_args( $uri, $self->connect_address ) );
247 23 $args{on_ready} = $self->_connect_pin_on_ready( $http, $uri, \%args );
248 }
249 }
250  
251 258 ▶ my $stream = $args{on_header} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$args{'on_header'} ? : 56 202 258
252  
253 # Bound the decoded response body against a decompression bomb (karr k346).
254 # Unlike LWP, Net::Async::HTTP inflates a Content-Encoding while it streams and
255 # moves the header to X-Original-Content-Encoding, so by the time the finished
256 # response reaches _bounded_decoded_content there is no encoding left to bound
257 # and a bomb has already been inflated in full. Count the decoded bytes as they
258 # arrive (Net::Async::HTTP decodes before this on_header callback) and abort
259 # past response_max_bytes -- the same streamed-byte guard the inline-image fetch
260 # uses (Langertha::Content::Image::_fetch_net_async_f). Only for a non-streaming
261 # request: a caller on_header is a stream, unbounded by design.
262 258 ▶ my $max = ( !$stream && $self->can('response_max_bytes') ) ? $self->response_max_bytes : 0;
Branch: 1 branch
BranchTrueFalseTotal
!$stream && $self->can('response_max_bytes') ? : 202 56 258
Condition66%2/3
not $stream and $self->can("response_max_bytes")
!ll&&!rl&&r
560202
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")
ABresult
0X0
100
111
263 258 my ( $abort, $too_big );
264 258 ▶ if ($max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max) 202 56 258
265 202 $abort = Future->new;
266 202 $args{on_header} = $self->_body_cap_on_header( $http, $max, \$too_big, $abort );
267 }
268  
269 # user_agent_timeout -> Net::Async::HTTP's own per-request option, unless the
270 # caller set one (karr k278).
271 258 ▶ my $secs = $self->can('has_user_agent_timeout') && $self->has_user_agent_timeout
Branch: 1 branch
BranchTrueFalseTotal
$self->can('has_user_agent_timeout') && $self->has_user_agent_timeout ? : 16 242 258
Condition66%2/3
$self->can("has_user_agent_timeout") and $self->has_user_agent_timeout
!ll&&!rl&&r
024216
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
ABresult
0X0
100
111
272 ? $self->user_agent_timeout : 0;
273 258 ▶ my $timed = $secs && !exists $args{timeout} && !exists $args{stall_timeout};
Condition83%5/6
$secs && !exists $args{'timeout'} && !exists $args{'stall_timeout'}
!ll&&!rl&&r
243015
$secs and not exists $args{"timeout"}
!ll&&!rl&&r
242115
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'}
ABCresult
0XX0
10X0
1100
1111
274  
275 258 ▶ return $http->do_request(%args) unless $timed || $max;
Branch: 1 branch
BranchTrueFalseTotal
unless $timed or $max 54 204 258
Condition100%3/3
$timed or $max
l!l&&r!l&&!r
1518954
MC/DC100%2/2
$timed or $max
$timed $max
Truth table100%3/3
A or B
A= $timedB= $max
ABresult
1X1
011
000
276  
277 204 ▶ my $request_f = $http->do_request( %args,
Branch: 2 branches
BranchTrueFalseTotal
$timed ? : 15 189 204
$stream ? : 2 13 15
278 $timed ? ( ( $stream ? 'stall_timeout' : 'timeout' ) => $secs ) : () );
279  
280 204 ▶ if ($timed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timed) 15 189 204
281 15 my $uri = $args{request}->uri->clone;
282 15 $uri->query(undef);
283 15 $uri->fragment(undef);
284 15 ▶ $uri->userinfo(undef) if $uri->can('userinfo');
Branch: 1 branch
BranchTrueFalseTotal
if $uri->can("userinfo") 15 0 15
285 15 ▶ my $what = ref($self) . ': ' . ( $stream ? 'streaming request' : 'request' ) . " to $uri";
Branch: 1 branch
BranchTrueFalseTotal
$stream ? : 2 13 15
286 14 ▶ $request_f = $request_f->else( sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
287 14 my ( $message, $category, @details ) = @_;
288 14 ▶ return Future->fail(@_)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $category and $category eq "timeout" || $category eq "stall_timeout" 1 13 14
Condition66%4/6
defined $category and $category eq "timeout" || $category eq "stall_timeout"
!ll&&!rl&&r
1013
$category eq "timeout" or $category eq "stall_timeout"
l!l&&r!l&&!r
1210
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"
ABCresult
0XX0
1000
11X1
1011
289 unless defined $category && ( $category eq 'timeout' || $category eq 'stall_timeout' );
290 13 ▶ my $text = $category eq 'timeout'
Branch: 1 branch
BranchTrueFalseTotal
$category eq 'timeout' ? : 12 1 13
291 ? "$what timed out after ${secs}s"
292 : "$what timed out after ${secs}s without data ($message)";
293 13 return Future->fail( "$text\n", $category, @details );
294 15 } );
295 }
296  
297 204 ▶ return $request_f unless $max;
Branch: 1 branch
BranchTrueFalseTotal
unless $max 2 202 204
298  
299 # Over the cap, whatever ends the fetch first -- the abort cancelling the
300 # request, or the server closing the connection -- becomes the size error.
301 4 ▶ my $too_big_croak = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
302 4 croak "".( ref $self )." response body exceeds response_max_bytes ($max)";
303 202 };
304 return Future->wait_any( $request_f, $abort )
305 175 ▶ ->then( sub { $too_big ? $too_big_croak->() : Future->done(@_) } )
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$too_big ? : 2 173 175
306 202 ▶ ->else( sub { $too_big ? $too_big_croak->() : Future->fail(@_) } );
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$too_big ? : 2 24 26
307 }
308  
309 # The Net::Async::HTTP request options that connect a request for $uri to
310 # $address while naming the host for TLS (karr k375).
311 23 ▶sub _connect_pin_args {
Subroutine _connect_pin_args: called CC=5 SCAR=17.5
312 23 my ( $uri, $address ) = @_;
313 23 my $host = $uri->host;
314 23 ▶ my $literal = $host =~ /:/ || $host =~ /\A[0-9.]+\z/;
Condition33%1/3
$host =~ /:/ || $host =~ /\A[0-9.]+\z/
l!l&&r!l&&!r
0023
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/
ABresult
1X1
011
000
315 return (
316 23 ▶ host => $address,
Branch: 2 branches
BranchTrueFalseTotal
lc($uri->scheme // '') eq 'https' ? : 10 13 23
$literal ? : 0 10 10
Condition50%1/2
$uri->scheme // ""
l!l
230
MC/DC0%0/1
$uri->scheme // ""
$uri->scheme
Truth table50%1/2
A
A= $uri->scheme
Aresult
11
00
317 port => $uri->port,
318 ( lc( $uri->scheme // '' ) eq 'https'
319 ? ( SSL_hostname => ( $literal ? undef : $host ), SSL_verifycn_name => $host )
320 : () ),
321 );
322 }
323  
324 # The on_ready (Net::Async::HTTP 0.50 runs it with the connection before it
325 # writes the request, for a new and for a pooled connection alike) that checks
326 # the connection really is to the pinned address and, over TLS, was verified
327 # for this host. The client pools connections by host:port, and host is now
328 # the address: two engines pinning different names to one address on a shared
329 # client would otherwise share a TLS session verified for the first name
330 # (karr k375). A refused connection is closed once idle (never under a request
331 # that is using it), so it leaves the pool and a request queued behind it gets
332 # a fresh one.
333 24 ▶sub _connect_pin_on_ready {
Subroutine _connect_pin_on_ready: called CC=10 SCAR=24.5
334 24 my ( $self, $http, $uri, $args ) = @_;
335 24 my $address = $self->connect_address;
336 24 my $host = $uri->host;
337 24 ▶ my $https = lc( $uri->scheme // '' ) eq 'https';
Condition50%1/2
$uri->scheme // ""
l!l
240
MC/DC0%0/1
$uri->scheme // ""
$uri->scheme
Truth table50%1/2
A
A= $uri->scheme
Aresult
11
00
338 24 ▶ my $params = ( reftype($http) // '' ) eq 'HASH' && ref $http->{ssl_params} eq 'HASH' ? $http->{ssl_params} : {};
Branch: 1 branch
BranchTrueFalseTotal
(builtin::reftype($http) // '') eq 'HASH' && ref $http->{'ssl_params'} eq 'HASH' ? : 24 0 24
Condition40%2/5
(builtin::reftype($http) // "") eq "HASH" and ref $http->{"ssl_params"} eq "HASH"
!ll&&!rl&&r
0024
builtin::reftype($http) // ""
l!l
240
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"
ABresult
0X0
100
111
339 # Verification switched off for this request or client: SSL_verify_mode
340 # => 0 skips both checks, SSL_verifycn_scheme => 'none' the name check
341 # (what the connection itself would have skipped; LWP's verify_hostname
342 # => 0 is the same pair on the sync side).
343 24 ▶ my %tls = map { $_ => ( exists $args->{$_} ? $args->{$_} : $params->{$_} ) } qw( SSL_verify_mode SSL_verifycn_scheme );
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{$_} ? : 0 48 48
344 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'})
!ll&&!rl&&r
14010
defined $tls{'SSL_verify_mode'} && !$tls{'SSL_verify_mode'}
!ll&&!rl&&r
1000
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'}
ABCresult
0XX0
1110
10X1
1101
345 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')
!ll&&!rl&&r
14010
defined $tls{'SSL_verifycn_scheme'} && $tls{'SSL_verifycn_scheme'} eq 'none'
!ll&&!rl&&r
1000
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'
ABCresult
0XX0
1110
10X1
1101
346 24 my $caller_on_ready = $args->{on_ready};
347 24 my $class = ref $self;
348 23 ▶ return sub {
Subroutine __ANON__: called CC=11 SCAR=24.5
349 23 my ($conn) = @_;
350 23 my $handle = $conn->read_handle;
351 23 ▶ my $peer = $handle ? eval { $handle->peerhost } : undef;
Branch: 1 branch
BranchTrueFalseTotal
$handle ? : 23 0 23
352 23 require Langertha::HTTP::UserAgent;
353 23 ▶ my $error = !( defined $peer && Langertha::HTTP::UserAgent::same_address( $peer, $address ) )
Branch: 1 branch
BranchTrueFalseTotal
!(defined $peer && Langertha::HTTP::UserAgent::same_address($peer, $address)) ? : 1 22 23
Condition60%3/5
defined $peer && Langertha::HTTP::UserAgent::same_address($peer, $address)
!ll&&!rl&&r
0122
$peer // "an unknown peer"
l!l
10
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)
ABresult
0X0
100
111
Truth table50%1/2
A
A= $peer
Aresult
11
00
354 ? "connect_address $address was not used: the connection goes to " . ( $peer // 'an unknown peer' )
355 : undef;
356 23 ▶ if ( !$error && $check_chain ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $error and $check_chain) 8 15 23
Condition100%3/3
not $error and $check_chain
!ll&&!rl&&r
1148
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
ABresult
0X0
100
111
357 8 my $tls = Langertha::HTTP::UserAgent::tls_identity_error( $handle, $host,
358 chain => 1, name => $check_name );
359 8 ▶ $error = "connect_address $address: $tls" if $tls;
Branch: 1 branch
BranchTrueFalseTotal
if $tls 3 5 8
360 }
361 23 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 4 19 23
362 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
BranchTrueFalseTotal
if $conn->loop 3 1 4
if $conn->read_handle and $conn->is_idle 2 0 2
Condition33%1/3
$conn->read_handle and $conn->is_idle
!ll&&!rl&&r
002
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
ABresult
0X0
100
111
363 4 return Future->fail( "$class: $error\n", 'connect_address' );
364 }
365 19 ▶ return $caller_on_ready ? $caller_on_ready->($conn) : Future->done;
Branch: 1 branch
BranchTrueFalseTotal
$caller_on_ready ? : 0 19 19
366 24 };
367 }
368  
369 # Why this Net::Async::HTTP request could not be pinned, or undef (karr k375).
370 23 ▶sub _nahttp_pin_error {
Subroutine _nahttp_pin_error: called CC=7 SCAR=19.8
371 23 my ( $self, $http, $args ) = @_;
372 23 my $address = $self->connect_address;
373 # With uri => Net::Async::HTTP sets host from the URI itself, over ours.
374 return "connect_address $address needs a request => HTTP::Request, not uri =>"
375 23 ▶ unless $args->{request};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"request"} 1 22 23
376 # A proxy connection goes to the proxy, which resolves the name itself. The
377 # client's own proxy settings are private state (hash fields in 0.50).
378 22 ▶ my $client = ( reftype($http) // '' ) eq 'HASH' ? $http : {};
Branch: 1 branch
BranchTrueFalseTotal
(builtin::reftype($http) // '') eq 'HASH' ? : 22 0 22
Condition50%1/2
builtin::reftype($http) // ""
l!l
220
MC/DC0%0/1
builtin::reftype($http) // ""
builtin::reftype($http)
Truth table50%1/2
A
A= builtin::reftype($http)
Aresult
11
00
379 22 for my $key (qw( proxy_host proxy_path )) {
380 return "connect_address $address cannot be used through a proxy ($key)"
381 43 ▶ if defined $args->{$key} || defined $client->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args->{$key} or defined $client->{$key} 1 42 43
Condition66%2/3
defined $args->{$key} or defined $client->{$key}
l!l&&r!l&&!r
0142
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}
ABresult
1X1
011
000
382 }
383 21 return undef;
384 }
385  
386 # The on_header (Net::Async::HTTP contract) that counts a non-streaming response
387 # body's decoded bytes and trips the abort once they pass $max (karr k346).
388 # Mirrors the inline-image counter (Langertha::Content::Image::_fetch_net_async_f):
389 # the body accumulates on the header response so the finished HTTP::Response still
390 # carries it, and the abort runs on the loop's next tick because closing the
391 # connection from inside the read handler is not safe.
392 202 ▶sub _body_cap_on_header {
Subroutine _body_cap_on_header: called CC=1 SCAR=0.1
393 202 my ( $self, $http, $max, $too_big_ref, $abort ) = @_;
394 175 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=14.2
395 175 my ($header) = @_;
396 2 ▶ my $trip = sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
397 2 ▶ return if ${$too_big_ref}++;
Branch: 1 branch
BranchTrueFalseTotal
if ${$too_big_ref;}++ 0 2 2
398 2 ▶ $http->loop->later( sub { $abort->done unless $abort->is_ready } );
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
unless $abort->is_ready 0 2 2
399 175 };
400 175 my $length = $header->content_length;
401 175 ▶ $trip->() if $header->is_success && defined $length && $length > $max;
Branch: 1 branch
BranchTrueFalseTotal
if $header->is_success and defined $length and $length > $max 0 175 175
Condition66%4/6
$header->is_success and defined $length and $length > $max
!ll&&!rl&&r
421330
$header->is_success and defined $length
!ll&&!rl&&r
420133
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
ABCresult
0XX0
10X0
1100
1111
402 350 ▶ return sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
403 350 ▶ return $header unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 175 175 350
404 175 ▶ return if ${$too_big_ref};
Branch: 1 branch
BranchTrueFalseTotal
if ${$too_big_ref;} 0 175 175
405 175 ▶ $header->add_content( $_[0] ) if defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] 175 0 175
406 175 ▶ $trip->() if length( ${ $header->content_ref } ) > $max;
Branch: 1 branch
BranchTrueFalseTotal
if length ${$header->content_ref;} > $max 2 173 175
407 175 return;
408 175 };
409 202 };
410 }
411  
412  
413 34 ▶sub async_loop {
Subroutine async_loop: called CC=2 SCAR=6.9
Pod documented
414 34 my ($self) = @_;
415 34 my $http = $self->_async_http;
416 34 ▶ return $http->can('loop') ? $http->loop : undef;
Branch: 1 branch
BranchTrueFalseTotal
$http->can('loop') ? : 17 17 34
417 }
418  
419  
420  
421 196 1;
422  
423 __END__
« blib/lib/Langertha/Role/Transcription.pm ↑ Summary blib/lib/Langertha/Response.pm »