HTTP2/Client.pm

filter: Statementstmt 84.4% 65 / 77 Branchbran 50.0% 11 / 22 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 14 / 14 Podpod 0.0% 0 / 2 totaltotal 73.6% 92 / 125 CC 14 SCAR 14.1
CC
14
Cov
74%
_consume_location
8.6
request
6.4
__ANON__
3.3
CRAP 4.1
« HTTP2/Response.pm ↑ Summary HTTP2/IOSocketSSL.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 - HTTP/2 client base class
9  
10 =head1 SYNOPSIS
11  
12 use Net::HTTP2::Client::Mojo;
13  
14 Net::HTTP2::Client::Mojo->new()->request(
15 GET => 'https://perl.com',
16 )->then(
17 sub ($response) {
18 # Handle the Net::HTTP2::Response
19 }
20 )->wait();
21  
22 L<Net::HTTP2::Client::AnyEvent> and L<Net::HTTP2::Client::IOAsync>
23 exist as well for L<AnyEvent> and L<IO::Async>, respectively.
24  
25 =head1 DESCRIPTION
26  
27 This class implements base functionality for an HTTP/2 client in Perl.
28  
29 =cut
30  
31 # perl -I ../p5-X-Tiny/lib -MData::Dumper -MAnyEvent -I ../p5-IO-SigGuard/lib -I ../p5-Promise-ES6/lib -Ilib -MNet::HTTP2::Client::Mojo -e'my $h2 = Net::HTTP2::Client::Mojo->new(); $h2->request("GET", "https://google.com")->then( sub { print Dumper shift } )->wait();'
32  
33 #----------------------------------------------------------------------
34  
35 1 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶use URI::Split ();
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 1 ▶use Net::HTTP2::Constants ();
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 ▶use Net::HTTP2::Client::ConnectionPool ();
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 1 ▶use constant _SIMPLE_REDIRECTS => (
Subroutine BEGIN: called CC=1 SCAR=0.0
42 301, 308,
43 302, 307,
44 1 );
45  
46 1 ▶use constant _FORBIDDEN => ('port');
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 #----------------------------------------------------------------------
49  
50 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
51 1 my ($class, %opts) = @_;
52  
53 1 my @bad = grep { defined $opts{$_} } _FORBIDDEN;
54 1 ▶ Carp::croak "Forbidden: @bad" if @bad;
Branch: 1 branch
BranchTrueFalseTotal
if @bad 0 1 1
55  
56 1 delete @opts{ _FORBIDDEN() };
57  
58 1 return bless {
59 host_port_conn => { },
60 conn_pool => Net::HTTP2::Client::ConnectionPool->new(
61 $class->_CLIENT_IO(),
62 \%opts,
63 ),
64 }, $class;
65 }
66  
67 2 ▶sub _split_uri_auth {
Subroutine _split_uri_auth: called CC=2 SCAR=7.6
68 2 my $auth = shift;
69  
70 2 ▶ if ( $auth =~ m<\A([^:]+):(.+)> ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($auth =~ /\A([^:]+):(.+)/) 0 2 2
71 0 return ($1, $2);
72 }
73  
74 2 return ($auth, Net::HTTP2::Constants::HTTPS_PORT);
75 }
76  
77 1 ▶sub request {
Subroutine request: called CC=5 SCAR=18.5
Pod undocumented
78 1 my ($self, $method, $url, @opts_kv) = @_;
79  
80 # Omit the fragment:
81 1 my ($scheme, $auth, $path, $query) = URI::Split::uri_split($url);
82  
83 1 ▶ if (!$scheme) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($scheme) 0 1 1
84 0 Carp::croak "Need absolute URL, not “$url”";
85 }
86  
87 1 ▶ if ($scheme ne 'https') {
Branch: 1 branch
BranchTrueFalseTotal
if ($scheme ne "https") 0 1 1
88 0 Carp::croak "https only, not $scheme!";
89 }
90  
91 1 my ($host, $port) = _split_uri_auth($auth);
92  
93 1 my $host_port_conn_hr = $self->{'host_port_conn'};
94  
95 1 my $path_and_query = $path;
96 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
97 0 $path_and_query .= "?$query";
98 }
99  
100 return _request_recurse(
101 1 $self->{'conn_pool'},
102 $method,
103 $host,
104 $port,
105 $path_and_query,
106 @opts_kv,
107 );
108 }
109  
110 2 ▶sub _request_recurse {
Subroutine _request_recurse: called CC=1 SCAR=0.2
111 2 my ($conn_pool, $method, $host, $port, $path_and_query, @opts_kv) = @_;
112  
113 2 my $conn = $conn_pool->get_connection($host, $port);
114  
115 return $conn->request($method, $path_and_query, @opts_kv)->then(
116 1 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=12.0
117 1 my $resp = shift;
118  
119 1 my $status = $resp->status();
120 1 my $redirect_yn = grep { $_ == $status } _SIMPLE_REDIRECTS;
121  
122 1 ▶ if ($status == 303) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status == 303) 0 1 1
123 0 $redirect_yn = 1;
124  
125 0 $method = 'GET';
126 0 push @opts_kv, body => q<>;
127 }
128  
129 1 ▶ if ($redirect_yn) {
Branch: 1 branch
BranchTrueFalseTotal
if ($redirect_yn) 1 0 1
130 my ($new_host, $new_port, $path_and_query) = _consume_location(
131 1 $resp->headers()->{'location'},
132 $host, $port, $path_and_query,
133 );
134  
135 1 $host = $new_host;
136 1 $port = $new_port;
137  
138 1 return _request_recurse( $conn_pool, $method, $host, $port, $path_and_query, @opts_kv );
139 }
140  
141 0 return $resp;
142 }
143 2 );
144 }
145  
146 1 ▶sub _consume_location {
Subroutine _consume_location: called CC=6 SCAR=21.5
147 1 my ($location, $host, $port, $old_path) = @_;
148  
149 1 my ($scheme, $auth, $path, $query) = URI::Split::uri_split($location);
150  
151 1 my $path_and_query = $path;
152 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
153 0 $path_and_query .= "?$query";
154 }
155  
156 1 ▶ if ($scheme) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scheme) 1 0 1
157 1 ▶ if ($scheme ne 'https') {
Branch: 1 branch
BranchTrueFalseTotal
if ($scheme ne "https") 0 1 1
158 0 Carp::croak "Invalid scheme in redirect: $location";
159 }
160  
161 1 ($host, $port) = _split_uri_auth($auth);
162 }
163  
164 1 ▶ if (rindex($path, '/', 0) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rindex($path, "/", 0) != 0) 0 1 1
165 0 $old_path =~ s<(.*)/><$1>;
166 0 substr( $path_and_query, 0, 0, "$old_path/" );
167 }
168  
169 1 return ($host, $port, $path_and_query);
170 }
171  
172 1 1;
« HTTP2/Response.pm ↑ Summary HTTP2/IOSocketSSL.pm »