blib/lib/Langertha/HTTP/ConnectCheck.pm

filter: Statementstmt 100% 30 / 30 Branchbran 90.0% 9 / 10 Conditioncond 45.4% 5 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 4 / 4 Podpod 100% 1 / 1 totaltotal 79.0% 49 / 62 CC 11 SCAR 22.5
CC
11
Cov
86%
connect_error
11.8
BEGIN
1.0
BEGIN
1.0
CRAP 9.5
« blib/lib/Langertha/ToolResult.pm ↑ Summary blib/lib/Langertha/Request/SyncHTTP.pm »
1 package Langertha::HTTP::ConnectCheck;
2 # ABSTRACT: Check that the modules Net::Async::HTTP loads at connect time load
3 23 our $VERSION = '0.503';
4 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 23 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8  
9 # Private to IO::Async (IO::Async::Loop->connect loads it via __new_feature);
10 # see the POD above for the assumption and when this check can go.
11 23 my @CONNECTOR = ( 'IO::Async::Internals::Connector', undef,
12 'every HTTP connection needs it' );
13 # Net::Async::HTTP 0.50 requires 0.12 (the ->connect(handle) fix) after loading.
14 23 my @SSL = ( 'IO::Async::SSL', '0.12',
15 'an SSL (https) connection needs it, with IO::Socket::SSL, Net::SSLeay and the system libssl' );
16  
17 259 ▶sub connect_error {
Subroutine connect_error: called CC=11 SCAR=24.6
Pod documented
18 259 my ( $uri, $ssl ) = @_;
19 259 ▶ $uri = URI->new("$uri") unless ref $uri;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $uri 38 221 259
20 259 ▶ my $scheme = lc( $uri->scheme // 'http' );
Condition50%1/2
$uri->scheme // "http"
l!l
2590
MC/DC0%0/1
$uri->scheme // "http"
$uri->scheme
Truth table50%1/2
A
A= $uri->scheme
Aresult
11
00
21 # Net::Async::HTTP 0.50 (_do_request) derives SSL from the scheme, but passes
22 # the caller's own SSL option after it, so an explicit one wins.
23 259 ▶ $ssl //= $scheme eq 'https';
Condition66%2/3
$ssl //= $scheme eq "https"
l!l&&r!l&&!r
42550
MC/DC0%0/2
$ssl //= $scheme eq "https"
$ssl $scheme eq "https"
Truth table66%2/3
A //= B
A= $sslB= $scheme eq "https"
ABresult
1X1
011
000
24 259 ▶ my $target = $uri->can('host_port') && defined $uri->host && length $uri->host
Branch: 1 branch
BranchTrueFalseTotal
$uri->can('host_port') && defined $uri->host && length $uri->host ? : 259 0 259
Condition33%2/6
$uri->can("host_port") and defined $uri->host and length $uri->host
!ll&&!rl&&r
00259
$uri->can("host_port") and defined $uri->host
!ll&&!rl&&r
00259
MC/DC0%0/3
$uri->can("host_port") and defined $uri->host and length $uri->host
$uri->can("host_port") defined $uri->host length $uri->host
Truth table25%1/4
A and B and C
A= $uri->can("host_port")B= defined $uri->hostC= length $uri->host
ABCresult
0XX0
10X0
1100
1111
25 ? $scheme.'://'.$uri->host_port
26 : $scheme.' URL';
27 259 ▶ for my $need ( \@CONNECTOR, $ssl ? \@SSL : () ) {
Branch: 1 branch
BranchTrueFalseTotal
$ssl ? : 17 242 259
28 275 my ( $module, $min_version, $why ) = @{$need};
29 275 ( my $file = "$module.pm" ) =~ s{::}{/}g;
30 275 local $@;
31 275 ▶ next if eval { require $file; $module->VERSION($min_version) if defined $min_version; 1 };
Branch: 2 branches
BranchTrueFalseTotal
if eval { do { require $file; $module->VERSION($min_version) if defined $min_version; 1 } } 269 6 275
if defined $min_version 13 256 269
32 6 my ($reason) = split /\n/, $@;
33 6 return "cannot connect to $target: $module failed to load ($reason); $why";
34 }
35 253 return;
36 }
37  
38 23 1;
39  
40 __END__
41  
42 =pod
43  
44 =encoding UTF-8
45  
46 =head1 NAME
47  
48 Langertha::HTTP::ConnectCheck - Check that the modules Net::Async::HTTP loads at connect time load
49  
50 =head1 VERSION
51  
52 version 0.503
53  
54 =head1 SYNOPSIS
55  
56 use Langertha::HTTP::ConnectCheck;
57  
58 if ( my $error = Langertha::HTTP::ConnectCheck::connect_error( $request->uri ) ) {
59 return Future->fail( "$class: $error\n", 'connect' );
60 }
61 return $net_async_http->do_request( request => $request );
62  
63 =head1 DESCRIPTION
64  
65 B<Internal module.> Its interface may change without notice.
66  
67 L<Net::Async::HTTP> loads some modules only when it opens a connection:
68 L<IO::Async::Internals::Connector> for every connection, L<IO::Async::SSL>
69 (with L<IO::Socket::SSL>, L<Net::SSLeay> and the system's libssl) for https.
70 When such a load dies, Net::Async::HTTP 0.50 has already counted the
71 connection against the host, and the dead connection keeps the host's slot:
72 every later request to that host waits forever, even once the module loads
73 (C<max_connections_per_host> is one by default). Core asks this module before
74 it hands a request to a L<Net::Async::HTTP> client
75 (L<Langertha::Role::AsyncHTTP>, L<Langertha::Content::Image>), so a broken
76 install is an error that names the module instead of a hang (karr k353).
77  
78 =head2 connect_error
79  
80 my $error = Langertha::HTTP::ConnectCheck::connect_error( $uri );
81 my $error = Langertha::HTTP::ConnectCheck::connect_error( $uri, $ssl ); # an explicit SSL option
82  
83 Returns C<undef> when the modules a connection to C<$uri> (a L<URI> or a
84 string) needs load, else a one-line message without a trailing newline:
85 C<< cannot connect to <scheme>://<host>:<port>: <module> failed to load
86 (<first line of the load error>); <why it is needed> >>. Only scheme, host and
87 port name the target: a query or userinfo may carry a key.
88  
89 Whether the connection needs SSL follows L<Net::Async::HTTP> 0.50: a C<SSL>
90 request option, passed here as C<$ssl>, wins when given (C<_do_request> puts
91 the caller's options after the C<SSL> it derives from the scheme), so C<http>
92 with C<< SSL => 1 >> connects with SSL and C<https> with C<< SSL => 0 >>
93 without; otherwise the scheme decides (C<https> is SSL).
94  
95 A module that loads is not loaded again (perl keeps it in C<%INC>), so once
96 the modules are in, the check costs a few microseconds per request.
97  
98 C<IO::Async::Internals::Connector> is a B<private> module name of L<IO::Async>,
99 hard-coded here on the assumption that L<IO::Async::Loop> keeps loading it
100 lazily under that name on connect (true for IO::Async 0.805). If IO::Async
101 renames it, the check reports a module that cannot load on every request, and
102 this module must follow the rename. The whole check can go once
103 L<Net::Async::HTTP> releases the host's slot when a connect-time load dies.
104  
105 =head1 SUPPORT
106  
107 =head2 Issues
108  
109 Please report bugs and feature requests on GitHub at
110 L<https://github.com/Getty/langertha/issues>.
111  
112 =head2 IRC
113  
114 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
115  
116 =head1 CONTRIBUTING
117  
118 Contributions are welcome! Please fork the repository and submit a pull request.
119  
120 =head1 AUTHOR
121  
122 Torsten Raudssus <getty@cpan.org>
123  
124 =head1 COPYRIGHT AND LICENSE
125  
126 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
127  
128 This is free software; you can redistribute it and/or modify it under
129 the same terms as the Perl 5 programming language system itself.
130  
131 =cut
« blib/lib/Langertha/ToolResult.pm ↑ Summary blib/lib/Langertha/Request/SyncHTTP.pm »