APIClient/Transport/NetCurlPromiser.pm untested

filter: Statementstmt 0.0% 0 / 110 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 171 CC 12 SCAR 27.7
CC
12
Cov
0%
_create_easy
42.0
cancel
20.0
new
12.0
CRAP 16.0
1 package cPanel::APIClient::Transport::NetCurlPromiser;
2  
3 use strict;
4 use warnings;
5  
6 =encoding utf-8
7  
8 =head1 NAME
9  
10 cPanel::APIClient::Transport::NetCurlPromiser
11  
12 =head1 SYNOPSIS
13  
14 my $promiser = Net::Curl::Promiser::AnyEvent->new();
15  
16 my $cp = cPanel::APIClient->create(
17 service => 'cpanel',
18 transport => [
19 'NetCurlPromiser',
20 promiser => $promiser,
21 hostname => 'greathosting.net',
22  
23 # For testing only:
24 # tls_verification => 'off',
25 ],
26  
27 credentials => {
28 username => 'hugh',
29 password -> 'mySecretPassword!',
30 },
31 );
32  
33 =head1 DESCRIPTION
34  
35 This module allows L<Net::Curl::Promiser> (i.e.,
36 L<libcurl|https://curl.haxx.se/libcurl/> via L<Net::Curl>)
37 to serve as transport for cPanel API calls.
38  
39 You can use this transport module to support any event interface that
40 Net::Curl::Promiser itself supports. See that module’s documentation
41 for more details.
42  
43 It supports C<username>/C<password> or C<username>/C<api_token>
44 C<credentials> schemes. See L<cPanel::APIClient> for more details.
45  
46 It expects these parameters:
47  
48 =over
49  
50 =item * C<promiser> - Required. Instance of a L<Net::Curl::Promiser>
51 subclass.
52  
53 =item * C<hostname> - Required. The remote hostname that will serve
54 the API calls.
55  
56 =item * C<tls_verification> - Optional. Either C<on> (default) or C<off>.
57  
58 =back
59  
60 This is probably the fastest way to call cPanel’s API via HTTP.
61  
62 =head1 INTERFACE
63  
64 When using this module, the immediate return from API-calling methods
65 (e.g., C<call_uapi()>) will be a L<cPanel::APIClient::Pending> instance.
66 See that module’s documentation for details of how to use it.
67  
68 =head2 Cancellation
69  
70 This class allows you to cancel an in-progress query, thus:
71  
72 $cp_service_obj->cancel($pending);
73  
74 Note that the L<cPanel::APIClient::Pending> object’s promise will remain
75 pending indefinitely.
76  
77 =cut
78  
79 #----------------------------------------------------------------------
80  
81 use parent qw(
82 cPanel::APIClient::TransportBase::HTTPBase
83 cPanel::APIClient::TransportBase::TLSVerificationBase
84 );
85  
86 use Net::Curl::Easy ();
87  
88 use cPanel::APIClient::Pending ();
89 use cPanel::APIClient::Utils::HTTPResponse ();
90 use cPanel::APIClient::X ();
91  
92 my @_REQUIRED = ('promiser');
93  
94 #----------------------------------------------------------------------
95  
96 sub new {
97 my ( $class, $authn, %opts ) = @_;
98  
99 my $self = $class->SUPER::new( $authn, %opts );
100  
101 my @missing = grep { !defined $opts{$_} } @_REQUIRED;
102 die( __PACKAGE__ . ": Missing @missing" ) if @missing;
103  
104 $self->{'promiser'} = $opts{'promiser'};
105  
106 $self->_parse_tls_verification( \%opts );
107  
108 if ( $self->_needs_session() ) {
109 require Net::Curl::Share;
110 $self->{'share'} = Net::Curl::Share->new();
111 $self->{'share'}->setopt( Net::Curl::Share::CURLSHOPT_SHARE(), Net::Curl::Share::CURL_LOCK_DATA_COOKIE() );
112 }
113  
114 return $self;
115 }
116  
117 # for testing
118 our $_TWEAK_EASY_CR;
119  
120 sub _create_easy {
121 my ( $self, $method, $url, $payload ) = @_;
122  
123 die "Unsupported HTTP method: $method" if $method ne 'POST';
124  
125 my $easy = Net::Curl::Easy->new();
126  
127 if ( 'off' eq $self->_get_tls_verification() ) {
128 $easy->setopt( Net::Curl::Easy::CURLOPT_SSL_VERIFYPEER, 0 );
129 $easy->setopt( Net::Curl::Easy::CURLOPT_SSL_VERIFYHOST, 0 );
130 }
131  
132 if ( $self->_needs_session() ) {
133 $easy->setopt( Net::Curl::Easy::CURLOPT_COOKIEFILE, q<> );
134 $easy->setopt( Net::Curl::Easy::CURLOPT_SHARE, $self->{'share'} );
135 }
136  
137 # We POST even if there’s no payload.
138 $payload = q<> if !defined $payload;
139 $easy->setopt( Net::Curl::Easy::CURLOPT_POSTFIELDSIZE, length $payload );
140 $easy->setopt( Net::Curl::Easy::CURLOPT_COPYPOSTFIELDS, $payload );
141  
142 $easy->setopt( Net::Curl::Easy::CURLOPT_URL, $url );
143  
144 @{$easy}{ 'head', 'body' } = ( q<>, q<> );
145  
146 $easy->setopt( Net::Curl::Easy::CURLOPT_HEADERDATA, \$easy->{'head'} );
147 $easy->setopt( Net::Curl::Easy::CURLOPT_FILE, \$easy->{'body'} );
148  
149 $_TWEAK_EASY_CR->($easy) if $_TWEAK_EASY_CR;
150  
151 return $easy;
152 }
153  
154 sub _get_session_promise {
155 my ( $self, $service_obj ) = @_;
156  
157 return $self->{'_session_promise'} ||= $self->_needs_session() && do {
158 my ( $method, $url, $payload ) = $self->{'authn'}->get_login_request_pieces();
159 substr( $url, 0, 0, $self->_get_url_base($service_obj) );
160  
161 my $session_easy = $self->_create_easy( $method, $url, $payload );
162  
163 $self->{'_session_easy'} = $session_easy;
164  
165 $self->{'promiser'}->add_handle($session_easy)->then(
166 sub {
167 my ($easy) = @_;
168  
169 my $resp_code = $easy->getinfo(Net::Curl::Easy::CURLINFO_RESPONSE_CODE);
170  
171 my $resp_obj = cPanel::APIClient::Utils::HTTPResponse->new(
172 $resp_code,
173 $easy->{'head'},
174 );
175  
176 $self->{'authn'}->consume_session_response($resp_obj);
177 }
178 );
179 };
180 }
181  
182 sub request {
183 my ( $self, $service_obj, $request_obj ) = @_;
184  
185 my $easy_sr = \do { my $v = undef };
186  
187 my $settled;
188  
189 my $get_promise_cr = sub {
190 my ( $method, $url, $headers_ar, $payload ) = $self->_assemble_request_pieces( $service_obj, $request_obj );
191  
192 my $easy = $self->_create_easy( $method, $url, $payload );
193  
194 $$easy_sr = $easy;
195  
196 if (@$headers_ar) {
197 my @header_strs = map { "$_->[0]: $_->[1]" } @$headers_ar;
198 $easy->pushopt( Net::Curl::Easy::CURLOPT_HTTPHEADER, \@header_strs );
199 }
200  
201 my $promise1 = $self->{'promiser'}->add_handle($easy);
202  
203 my $promise2 = $promise1->then(
204 sub {
205 return _xform_easy_response( $_[0], $request_obj );
206 },
207 sub {
208 my ($code) = @_;
209 my $str = "$code";
210  
211 die cPanel::APIClient::X->create( 'SubTransport', $str, code => $code );
212 },
213 );
214  
215 my $promise3 = $promise2->finally( sub {
216 $settled = 1;
217 } );
218  
219 return $promise3;
220 };
221  
222 my $promise = $self->_get_session_promise($service_obj);
223 $promise &&= $promise->then($get_promise_cr);
224 $promise ||= $get_promise_cr->();
225  
226 return cPanel::APIClient::Pending->new( $promise, $easy_sr, \$settled );
227 }
228  
229 sub _xform_easy_response {
230 my ( $easy, $request_obj ) = @_;
231  
232 my $resp_code = $easy->getinfo(Net::Curl::Easy::CURLINFO_RESPONSE_CODE);
233  
234 my $resp_obj = cPanel::APIClient::Utils::HTTPResponse->new(
235 $resp_code,
236 $easy->{'head'},
237 $easy->{'body'},
238 );
239  
240 return $request_obj->parse_http_response( $resp_obj, $easy->{'body'} );
241 }
242  
243 sub cancel {
244 my ( $self, $pending_obj ) = @_;
245  
246 my ( $easy_sr, $settled_sr ) = $pending_obj->get_details();
247  
248 if ($$settled_sr) {
249 die 'Request is already finished!';
250 }
251  
252 if ( !$easy_sr ) {
253 if ( $self->_needs_session() ) {
254 $easy_sr = \$self->{'_session_easy'};
255 }
256 else {
257 die 'non-session authn but session lacks detail!';
258 die 'pending object lacks detail!';
259 }
260 }
261  
262 $self->{'promiser'}->cancel_handle($$easy_sr);
263  
264 return $self;
265 }
266  
267 =head1 LICENSE
268  
269 Copyright 2020 cPanel, L. L. C. All rights reserved. L<http://cpanel.net>
270  
271 This is free software; you can redistribute it and/or modify it under the
272 same terms as Perl itself. See L<perlartistic>.
273  
274 =cut
275  
276 1;