blib/lib/App/MatrixTool/HTTPClient.pm

filter: Statementstmt 33.6% 38 / 113 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 44.4% 12 / 27 Podpod 66.6% 4 / 6 totaltotal 27.0% 54 / 200 SCAR 55.3
CC
23
Cov
24%
request
156.0
__ANON__
20.0
__ANON__
12.0
CRAP 251.9
« blib/lib/App/MatrixTool/Command/server_key.pm ↑ Summary blib/lib/App/MatrixTool/ServerIdStore.pm »
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2015 -- leonerd@leonerd.org.uk
5  
6 package App::MatrixTool::HTTPClient;
7  
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $VERSION = '0.06';
12  
13 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Future 0.33; # ->catch
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Future::Utils qw( repeat_until_success );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use IO::Async::Loop;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use IO::Async::Resolver 0.68; # failure details
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use IO::Async::Resolver::DNS 0.06 qw( ERR_NO_HOST ERR_NO_ADDRESS ); # Future return with failure details
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use JSON qw( encode_json decode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 ▶use Socket qw( getnameinfo NI_NUMERICHOST SOCK_RAW );
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 1 ▶use constant DEFAULT_MATRIX_PORT => 8448;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 =head1 NAME
27  
28 C<App::MatrixTool::HTTPClient> - HTTP client helper for L<App::MatrixTool>
29  
30 =head1 DESCRIPTION
31  
32 Provides helper methods to perform HTTP client operations that may be required
33 by commands of L<App::MatrixTool>.
34  
35 =cut
36  
37 sub new
38 {
39 0 ▶ my $class = shift;
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
40  
41 0 my $loop = IO::Async::Loop->new; # magic constructor
42  
43 0 return bless {
44 @_,
45 resolver => $loop->resolver,
46 loop => $loop,
47 }, $class;
48 }
49  
50 =head1 METHODS
51  
52 =cut
53  
54 sub ua
55 {
56 0 ▶ my $self = shift;
Subroutine ua: not called CC=2 SCAR=17.9
Pod undocumented
57 0 ▶ return $self->{ua} ||= do {
Condition0%0/3
$self->{"ua"} ||= do { require IO::Async::SSL; require Net::Async::HTTP; require HTTP::Request; my $ua = "Net::Async::HTTP"->new("SSL_verify_mode", 0, "fail_on_error", 1); $self->{"loop"}->add($ua); $ua }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"ua"} ||= do { require IO::Async::SSL; require Net::Async::HTTP; require HTTP::Request; my $ua = "Net::Async::HTTP"->new("SSL_verify_mode", 0, "fail_on_error", 1); $self->{"loop"}->add($ua); $ua }
$self->{"ua"} do { require IO::Async::SSL; require Net::Async::HTTP; require HTTP::Request; my $ua = "Net::Async::HTTP"->new("SSL_verify_mode", 0, "fail_on_error", 1); $self->{"loop"}->add($ua); $ua }
Truth table0%0/3
A ||= B
A= $self->{"ua"}B= do { require IO::Async::SSL; require Net::Async::HTTP; require HTTP::Request; my $ua = "Net::Async::HTTP"->new("SSL_verify_mode", 0, "fail_on_error", 1); $self->{"loop"}->add($ua); $ua }
ABresult
1X1
011
000
58 0 require IO::Async::SSL;
59 0 require Net::Async::HTTP;
60 0 require HTTP::Request;
61  
62 0 my $ua = Net::Async::HTTP->new(
63 SSL_verify_mode => 0,
64 fail_on_error => 1, # turn 4xx/5xx errors into Future failures
65 );
66  
67 0 $self->{loop}->add( $ua );
68 0 $ua;
69 };
70 }
71  
72 =head2 resolve_matrix
73  
74 @res = $client->resolve_matrix( $server_name )->get
75  
76 Returns a list of C<HASH> references. Each has at least the keys C<target>
77 and C<port>. These should be tried in order until one succeeds.
78  
79 =cut
80  
81 sub resolve_matrix
82 {
83 0 ▶ my $self = shift;
Subroutine resolve_matrix: not called CC=2 SCAR=17.9
Pod documented
84 0 my ( $server_name ) = @_;
85  
86 0 ▶ if( my ( $host, $port ) = $server_name =~ m/^(\S+):(\d+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $server_name =~ /^(\S+):(\d+)$/) 0 0 0
87 0 return Future->done( { target => $host, port => $port } );
88 }
89  
90 $self->{resolver}->res_query(
91 dname => "_matrix._tcp.$server_name",
92 type => "SRV",
93 )->then( sub {
94 0 ▶ my ( undef, @srvs ) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
95 0 Future->done( @srvs );
96 })->catch_with_f(
97 resolve => sub {
98 0 ▶ my ( $f, $message, undef, undef, $errnum ) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
99 0 ▶ return $f unless $errnum == ERR_NO_HOST or $errnum == ERR_NO_ADDRESS;
Branch: 1 branch
BranchTrueFalseTotal
unless $errnum == 1 or $errnum == 2 0 0 0
Condition0%0/3
$errnum == 1 or $errnum == 2
l!l&&r!l&&!r
000
MC/DC0%0/2
$errnum == 1 or $errnum == 2
$errnum == 1 $errnum == 2
Truth table0%0/3
A or B
A= $errnum == 1B= $errnum == 2
ABresult
1X1
011
000
100  
101 0 Future->done( { target => $server_name, port => DEFAULT_MATRIX_PORT } );
102 }
103 0 );
104 }
105  
106 =head2 resolve_addr
107  
108 @addrs = $client->resolve_addr( $hostname )->get
109  
110 Returns a list of human-readable string representations of the IP addresses
111 resolved by the given hostname.
112  
113 =cut
114  
115 sub resolve_addr
116 {
117 0 ▶ my $self = shift;
Subroutine resolve_addr: not called CC=1 SCAR=6.9
Pod documented
118 0 my ( $host ) = @_;
119  
120 $self->{resolver}->getaddrinfo(
121 host => $host,
122 service => "",
123 family => $self->{family},
124 socktype => SOCK_RAW,
125 )->then( sub {
126 0 ▶ my @res = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
127 return Future->done(
128 0 map { ( getnameinfo( $_->{addr}, NI_NUMERICHOST ) )[1] } @res
129 );
130 0 });
131 }
132  
133 =head2 request
134  
135 $response = $client->request( server => $name, method => $method, path => $path, ... )->get
136  
137 Performs an HTTPS request to the given server, by resolving the server name
138 using the C<resolve_matrix> method first, thus obeying its published C<SRV>
139 records.
140  
141 =cut
142  
143 sub request
144 {
145 0 ▶ my $self = shift;
Subroutine request: not called CC=12 SCAR=50.5
Pod documented
146 0 my %params = @_;
147  
148 0 my $uri = URI->new;
149 0 $uri->path( $params{path} );
150 0 ▶ $uri->query_form( %{ $params{params} } ) if $params{params};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"params"} 0 0 0
151  
152 0 my $ua = $self->ua;
153 my $req = $params{request} // HTTP::Request->new( $params{method} => $uri,
154 0 ▶ [ Host => $params{server} ],
Condition0%0/3
$params{"request"} // "HTTP::Request"->new($params{"method"}, $uri, ["Host", $params{"server"}])
l!l&&r!l&&!r
000
MC/DC0%0/2
$params{"request"} // "HTTP::Request"->new($params{"method"}, $uri, ["Host", $params{"server"}])
$params{"request"} "HTTP::Request"->new($params{"method"}, $uri, ["Host", $params{"server"}])
Truth table0%0/3
A // B
A= $params{"request"}B= "HTTP::Request"->new($params{"method"}, $uri, ["Host", $params{"server"}])
ABresult
1X1
011
000
155 );
156 0 $req->protocol( "HTTP/1.1" );
157  
158 0 ▶ if( defined $params{content} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"content"}) 0 0 0
159 0 ▶ if( ref $params{content} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params{'content'}) { } 0 0 0
160 0 $req->content( encode_json( delete $params{content} ) );
161 0 $req->header( Content_type => "application/json" );
162 }
163 else {
164 0 $req->content( delete $params{content} );
165 $req->header( Content_type => delete $params{content_type} //
166 0 ▶ croak "Non-reference content needs 'content_type'" );
Condition0%0/3
delete $params{"content_type"} // croak("Non-reference content needs 'content_type'")
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $params{"content_type"} // croak("Non-reference content needs 'content_type'")
delete $params{"content_type"} croak("Non-reference content needs 'content_type'")
Truth table0%0/3
A // B
A= delete $params{"content_type"}B= croak("Non-reference content needs 'content_type'")
ABresult
1X1
011
000
167 }
168 0 $req->header( Content_length => length $req->content );
169 }
170  
171 0 ▶ if( $self->{print_request} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"print_request"}) 0 0 0
172 0 print STDERR "Sending HTTP request to $params{server}\n";
173 0 print STDERR " $_\n" for split m/\n/, $req->as_string( "\n" );
174 }
175  
176 0 my $path = $req->uri->path;
177  
178 # Different kinds of request need resolving either as a client or as a
179 # federated server
180 0 my $resolve_f;
181 0 ▶ if( $path =~ m{^/_matrix/key/} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($path =~ m[^/_matrix/key/]) { } 0 0 0
elsif ($path =~ m[^/_matrix/(?:client|media)/]) { } 0 0 0
182 $resolve_f = $self->resolve_matrix( $params{server} )->then( sub {
183 0 ▶ my @res = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
184 Future->done( map {
185 0 { SSL => 1, host => $_->{target}, port => $_->{port}, family => $self->{family} }
186 0 } @res );
187 0 });
188 }
189 elsif( $path =~ m{^/_matrix/(?:client|media)/} ) {
190 0 ▶ my ( $server, $port ) = $params{server} =~ m/^([^:]+)(?::(\d+))?$/ or
Branch: 1 branch
BranchTrueFalseTotal
() = $params{"server"} =~ /^([^:]+)(?::(\d+))?$/ or die qq[Unable to parse server '$params{"server"}'\n] 0 0 0
191 die "Unable to parse server '$params{server}'\n";
192 $resolve_f = Future->done(
193 { SSL => 1, port => $port // 443, host => $server, family => $self->{family} }
194 0 ▶ );
Condition0%0/2
$port // 443
l!l
00
MC/DC0%0/1
$port // 443
$port
Truth table0%0/2
A
A= $port
Aresult
11
00
195 }
196 else {
197 0 die "Unsure how to resolve server for path $path\n";
198 }
199  
200 $resolve_f->then( sub {
201 0 ▶ my @res = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
202  
203 repeat_until_success {
204 0 ▶ my $res = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
205 0 ▶ print STDERR "Using target $res->{host} port $res->{port}\n" if $self->{print_request};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"print_request"} 0 0 0
206  
207 $ua->do_request(
208 %params,
209 %$res,
210 request => $req,
211 )->on_done( sub {
212 0 ▶ my ( $response ) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
213 0 ▶ if( $self->{print_response} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"print_response"}) 0 0 0
214 0 print STDERR "Received HTTP response:\n";
215 0 print STDERR " $_\n" for split m/\n/, $response->as_string( "\n" );
216 }
217 })->on_fail( sub {
218 0 ▶ my ( undef, $name, $response ) = @_;
Subroutine __ANON__: not called CC=4 SCAR=30.0
219 0 ▶ if( $name eq "http" and $self->{print_response} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "http" and $self->{"print_response"}) 0 0 0
Condition0%0/3
$name eq "http" and $self->{"print_response"}
!ll&&!rl&&r
000
MC/DC0%0/2
$name eq "http" and $self->{"print_response"}
$name eq "http" $self->{"print_response"}
Truth table0%0/3
A and B
A= $name eq "http"B= $self->{"print_response"}
ABresult
0X0
100
111
220 0 print STDERR "Received HTTP response:\n";
221 0 print STDERR " $_\n" for split m/\n/, $response->as_string( "\n" );
222 }
223 0 });
224 0 } foreach => \@res;
225 0 });
226 }
227  
228 =head2 request_json
229  
230 ( $body, $response ) = $client->request_json( ... )
231  
232 A small wrapper around C<request> that decodes the returned body as JSON.
233  
234 =cut
235  
236 sub request_json
237 {
238 0 ▶ my $self = shift;
Subroutine request_json: not called CC=1 SCAR=6.9
Pod documented
239 $self->request( @_ )->then( sub {
240 0 ▶ my ( $response ) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
241  
242 0 ▶ $response->content_type eq "application/json" or
Branch: 1 branch
BranchTrueFalseTotal
$response->content_type eq "application/json" or return "Future"->fail("Expected an application/json response body", "matrix") 0 0 0
243 return Future->fail( "Expected an application/json response body", matrix => );
244  
245 0 Future->done( decode_json( $response->decoded_content ), $response );
246 0 });
247 }
248  
249 =head1 AUTHOR
250  
251 Paul Evans <leonerd@leonerd.org.uk>
252  
253 =cut
254  
255 1 0x55AA;
« blib/lib/App/MatrixTool/Command/server_key.pm ↑ Summary blib/lib/App/MatrixTool/ServerIdStore.pm »