HTTP2/Client.pm

filter: Statementstmt 84.4% 65 / 77 Branchbran 50.0% 12 / 24 Conditioncond 35.7% 5 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 2 totaltotal 68.7% 97 / 141 SCAR 32.4
CC
18
Cov
71%
_consume_location
8.6
request
6.2
_get_conn
5.4
CRAP 25.7
« HTTP2/Client/Connection/IOAsync.pm ↑ Summary HTTP2/Response.pm »
1 package Net::HTTP2::Client;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 Net::HTTP2::Client - Full-featured HTTP/2 client base class
9  
10 =cut
11  
12 # perl -I ../p5-X-Tiny/lib -MData::Dumper -MAnyEvent -I ../p5-IO-SigGuard/lib -I ../p5-Promise-ES6/lib -Ilib -MNet::HTTP2::Client -e'my $h2 = Net::HTTP2::Client->new(); my $cv = AnyEvent->condvar(); $h2->request("GET", "https://google.com")->then( sub { print Dumper shift } )->finally($cv); $cv->recv();'
13  
14 #----------------------------------------------------------------------
15  
16 1 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use URI::Split ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 ▶use Net::HTTP2::Constants ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 use constant _SIMPLE_REDIRECTS => (
22 301, 308,
23 302, 307,
24 1 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 #----------------------------------------------------------------------
27  
28 sub new {
29 1 ▶ return bless {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
30 host_port_client => { },
31 }, shift;
32 }
33  
34 sub _split_uri_auth {
35 2 ▶ my $auth = shift;
Subroutine _split_uri_auth: called CC=2 SCAR=7.6
36  
37 2 ▶ if ( $auth =~ m<\A([^:]+):(.+)> ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($auth =~ /\A([^:]+):(.+)/) 0 2 2
38 0 return ($1, $2);
39 }
40  
41 2 return ($auth, Net::HTTP2::Constants::HTTPS_PORT);
42 }
43  
44 sub request {
45 1 ▶ my ($self, $method, $url, @opts_kv) = @_;
Subroutine request: called CC=5 SCAR=18.2
Pod undocumented
46  
47 # Omit the fragment:
48 1 my ($scheme, $auth, $path, $query) = URI::Split::uri_split($url);
49  
50 1 ▶ if (!$scheme) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($scheme) 0 1 1
51 0 Carp::croak "Need absolute URL, not “$url”";
52 }
53  
54 1 ▶ if ($scheme ne 'https') {
Branch: 1 branch
BranchTrueFalseTotal
if ($scheme ne "https") 0 1 1
55 0 Carp::croak "https only, not $scheme!";
56 }
57  
58 1 my ($host, $port) = _split_uri_auth($auth);
59  
60 1 my $host_port_conn_hr = $self->{'host_port_client'};
61  
62 1 my $conn_ns = $self->_get_conn_namespace();
63  
64 1 my $path_and_query = $path;
65 1 ▶ if (defined $query && length $query) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $query and length $query) 0 1 1
Condition33%1/3
defined $query and length $query
!ll&&!rl&&r
100
MC/DC0%0/2
defined $query and length $query
defined $query length $query
Truth table33%1/3
A and B
A= defined $queryB= length $query
ABresult
0X0
100
111
66 0 $path_and_query .= "?$query";
67 }
68  
69 1 return _request_recurse(
70 $conn_ns,
71 $host_port_conn_hr,
72 $method,
73 $host,
74 $port,
75 $path_and_query,
76 @opts_kv,
77 );
78 }
79  
80 sub _request_recurse {
81 2 ▶ my ($conn_ns, $host_port_conn_hr, $method, $host, $port, $path_and_query, @opts_kv) = @_;
Subroutine _request_recurse: called CC=1 SCAR=0.2
82  
83 2 my $conn = _get_conn( $conn_ns, $host_port_conn_hr, $host, $port, @opts_kv );
84  
85 return _request_once( $conn, $method, $path_and_query )->then(
86 sub {
87 1 ▶ my $resp = shift;
Subroutine __ANON__: called CC=3 SCAR=12.0
88  
89 1 my $status = $resp->status();
90 1 my $redirect_yn = grep { $_ == $status } _SIMPLE_REDIRECTS;
91  
92 1 ▶ if ($status == 303) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status == 303) 0 1 1
93 0 $redirect_yn = 1;
94  
95 0 $method = 'GET';
96 0 push @opts_kv, body => q<>;
97 }
98  
99 1 ▶ if ($redirect_yn) {
Branch: 1 branch
BranchTrueFalseTotal
if ($redirect_yn) 1 0 1
100 my ($new_host, $new_port, $path_and_query) = _consume_location(
101 1 $resp->headers()->{'location'},
102 $host, $port, $path_and_query,
103 );
104  
105 1 $host = $new_host;
106 1 $port = $new_port;
107  
108 1 return _request_recurse( $conn_ns, $host_port_conn_hr, $method, $host, $port, $path_and_query, @opts_kv );
109 }
110  
111 0 return $resp;
112 }
113 2 );
114 }
115  
116 sub _consume_location {
117 1 ▶ my ($location, $host, $port, $old_path) = @_;
Subroutine _consume_location: called CC=6 SCAR=21.5
118  
119 1 my ($scheme, $auth, $path, $query) = URI::Split::uri_split($location);
120  
121 1 my $path_and_query = $path;
122 1 ▶ if (defined $query && length $query) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $query and length $query) 0 1 1
Condition33%1/3
defined $query and length $query
!ll&&!rl&&r
100
MC/DC0%0/2
defined $query and length $query
defined $query length $query
Truth table33%1/3
A and B
A= defined $queryB= length $query
ABresult
0X0
100
111
123 0 $path_and_query .= "?$query";
124 }
125  
126 1 ▶ if ($scheme) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scheme) 1 0 1
127 1 ▶ if ($scheme ne 'https') {
Branch: 1 branch
BranchTrueFalseTotal
if ($scheme ne "https") 0 1 1
128 0 Carp::croak "Invalid scheme in redirect: $location";
129 }
130  
131 1 ($host, $port) = _split_uri_auth($auth);
132 }
133  
134 1 ▶ if (rindex($path, '/', 0) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rindex($path, "/", 0) != 0) 0 1 1
135 0 $old_path =~ s<(.*)/><$1>;
136 0 substr( $path_and_query, 0, 0, "$old_path/" );
137 }
138  
139 1 return ($host, $port, $path_and_query);
140 }
141  
142 sub _get_conn {
143 2 ▶ my ($conn_ns, $host_port_conn_hr, $host, $port) = @_;
Subroutine _get_conn: called CC=4 SCAR=16.9
144  
145 2 ▶ return $host_port_conn_hr->{$host}{$port || q<>} ||= $conn_ns->new(
Branch: 1 branch
BranchTrueFalseTotal
$port == 443 ? : 2 0 2
Condition40%2/5
$host_port_conn_hr->{$host}{$port or ""} ||= $conn_ns->new($host, $port == 443 ? () : ("port", $port))
l!l&&r!l&&!r
020
$port || ''
l!l
20
MC/DC0%0/2
$host_port_conn_hr->{$host}{$port or ""} ||= $conn_ns->new($host, $port == 443 ? () : ("port", $port))
$host_port_conn_hr->{$host}{$port or ""} $conn_ns->new($host, $port == 443 ? () : ("port", $port))
MC/DC0%0/1
$port || ''
$port
Truth table33%1/3
A ||= B
A= $host_port_conn_hr->{$host}{$port or ""}B= $conn_ns->new($host, $port == 443 ? () : ("port", $port))
ABresult
1X1
011
000
Truth table50%1/2
A
A= $port
Aresult
11
00
146 $host,
147 ($port == Net::HTTP2::Constants::HTTPS_PORT ? () : (port => $port)),
148 );
149 }
150  
151 sub _request_once {
152 2 ▶ my ($conn, $method, $path_and_query, @opts_kv) = @_;
Subroutine _request_once: called CC=1 SCAR=0.0
153  
154 2 return $conn->request($method, $path_and_query);
155 }
156  
157 sub _get_conn_namespace {
158 1 ▶ my $self = shift;
Subroutine _get_conn_namespace: called CC=3 SCAR=11.6
159  
160 1 ▶ return $self->{'_conn_ns'} ||= do {
Condition33%1/3
$self->{"_conn_ns"} ||= do { my $ns = 'Net::HTTP2::Client::Connection::' . $self->_CLIENT_IO; local $@; Carp::croak($@) unless eval "require $ns"; $ns }
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->{"_conn_ns"} ||= do { my $ns = 'Net::HTTP2::Client::Connection::' . $self->_CLIENT_IO; local $@; Carp::croak($@) unless eval "require $ns"; $ns }
$self->{"_conn_ns"} do { my $ns = 'Net::HTTP2::Client::Connection::' . $self->_CLIENT_IO; local $@; Carp::croak($@) unless eval "require $ns"; $ns }
Truth table33%1/3
A ||= B
A= $self->{"_conn_ns"}B= do { my $ns = 'Net::HTTP2::Client::Connection::' . $self->_CLIENT_IO; local $@; Carp::croak($@) unless eval "require $ns"; $ns }
ABresult
1X1
011
000
161 1 my $ns = "Net::HTTP2::Client::Connection::" . $self->_CLIENT_IO();
162  
163 1 local $@;
164 1 ▶ Carp::croak $@ if !eval "require $ns";
Branch: 1 branch
BranchTrueFalseTotal
unless eval "require $ns" 0 1 1
165  
166 1 $ns;
167 };
168 }
169  
170 1 1;
« HTTP2/Client/Connection/IOAsync.pm ↑ Summary HTTP2/Response.pm »