DRI/Transport/HTTP.pm

filter: Statementstmt 13.1% 19 / 144 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 33.3% 6 / 18 Podpod 12.5% 1 / 8 totaltotal 9.5% 26 / 271 CC 38 SCAR 52.4
CC
38
Cov
8%
new
462.0
send_login
56.0
send_logout
30.0
CRAP 188.7
« DRI/Protocol/EPP/Extensions/LU/Contact.pm ↑ Summary DRI/Protocol/EPP/Extensions/LU/Poll.pm »
1 ## Domain Registry Interface, HTTP/HTTPS Transport
2 ##
3 ## Copyright (c) 2008-2011,2013,2015 Patrick Mevzek <netdri@dotandco.com>. All rights reserved.
4 ##
5 ## This file is part of Net::DRI
6 ##
7 ## Net::DRI is free software; you can redistribute it and/or modify
8 ## it under the terms of the GNU General Public License as published by
9 ## the Free Software Foundation; either version 2 of the License, or
10 ## (at your option) any later version.
11 ##
12 ## See the LICENSE file that comes with this distribution for more details.
13 ####################################################################################################
14  
15 package Net::DRI::Transport::HTTP;
16  
17 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 ▶use base qw(Net::DRI::Transport);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 ▶use Net::DRI::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use Net::DRI::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 1 ▶use LWP::UserAgent 6.02;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 =pod
28  
29 =head1 NAME
30  
31 Net::DRI::Transport::HTTP - HTTP/HTTPS Transport for Net::DRI
32  
33 =head1 DESCRIPTION
34  
35 This module implements an HTTP/HTTPS transport for establishing connections in Net::DRI
36  
37 =head1 METHODS
38  
39 At creation (see Net::DRI C<new_profile>) you pass a reference to an hash, with the following available keys:
40  
41 =head2 timeout
42  
43 time to wait (in seconds) for server reply
44  
45 =head2 ssl_key_file ssl_cert_file ssl_ca_file ssl_ca_path ssl_cipher_list ssl_version ssl_passwd_cb
46  
47 if C<remote_url> begins with https://, all key materials, see IO::Socket::SSL documentation for corresponding options
48  
49 =head2 ssl_verify
50  
51 see IO::Socket::SSL documentation about verify_mode (by default 0x00 here)
52  
53 =head2 ssl_verify_callback
54  
55 see IO::Socket::SSL documentation about verify_callback, it gets here as first parameter the transport object
56 then all parameter given by IO::Socket::SSL; it is explicitly verified that the subroutine returns a true value,
57 and if not the connection is aborted.
58  
59 =head2 remote_url
60  
61 URL to access
62  
63 =head2 client_login client_password
64  
65 protocol login & password
66  
67 =head2 client_newpassword
68  
69 (optional) new password if you want to change password on login for registries handling that at connection
70  
71 =head2 protocol_connection
72  
73 Net::DRI class handling protocol connection details. Specifying it should not be needed, as the registry driver should have correct default values.
74  
75 =head2 protocol_data
76  
77 (optional) opaque data given to protocol_connection class.
78 For EPP, a key login_service_filter may exist, whose value is a code ref. It will be given an array of services, and should give back a
79 similar array; it can be used to filter out some services from those given by the registry.
80  
81 =head2 verify_response
82  
83 (optional) a callback (code ref) executed after each exchange with the registry, being called with the following parameters: the transport object,
84 the phase (1 for greeting+login, 2 for all normal operations, 3 for logout), the count (if we retried multiple times to send the same message),
85 the message sent (HTTP::Request object) and the response received (HTTP::Response object). This can be used to verify/diagnose SSL details,
86 see example in file t/remote/opensrs_xcp.t
87  
88 =head2 local_host
89  
90 (optional) the local address (hostname or IP) you want to use to connect (if you are multihomed)
91  
92 =head1 SUPPORT
93  
94 For now, support questions should be sent to:
95  
96 E<lt>netdri@dotandco.comE<gt>
97  
98 Please also see the SUPPORT file in the distribution.
99  
100 =head1 SEE ALSO
101  
102 E<lt>http://www.dotandco.com/services/software/Net-DRI/E<gt>
103  
104 =head1 AUTHOR
105  
106 Patrick Mevzek, E<lt>netdri@dotandco.comE<gt>
107  
108 =head1 COPYRIGHT
109  
110 Copyright (c) 2008-2011,2013,2015 Patrick Mevzek <netdri@dotandco.com>.
111 All rights reserved.
112  
113 This program is free software; you can redistribute it and/or modify
114 it under the terms of the GNU General Public License as published by
115 the Free Software Foundation; either version 2 of the License, or
116 (at your option) any later version.
117  
118 See the LICENSE file that comes with this distribution for more details.
119  
120 =cut
121  
122 ####################################################################################################
123  
124 sub new
125 {
126 0 ▶ my ($class,$ctx,$rp)=@_;
Subroutine new: not called CC=21 SCAR=61.4
Pod documented
127 0 my %opts=%$rp;
128 0 my $ndr=$ctx->{registry};
129 0 my $pname=$ctx->{profile};
130 0 my $po=$ctx->{protocol};
131  
132 0 my %t=(message_factory => $po->factories()->{message});
133 0 ▶ Net::DRI::Exception::usererr_insufficient_parameters('protocol_connection') unless (exists($opts{protocol_connection}) && $opts{protocol_connection});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"protocol_connection"} and $opts{"protocol_connection"} 0 0 0
Condition0%0/3
exists $opts{"protocol_connection"} and $opts{"protocol_connection"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $opts{"protocol_connection"} and $opts{"protocol_connection"}
exists $opts{"protocol_connection"} $opts{"protocol_connection"}
Truth table0%0/3
A and B
A= exists $opts{"protocol_connection"}B= $opts{"protocol_connection"}
ABresult
0X0
100
111
134 0 $t{pc}=$opts{protocol_connection};
135 0 Net::DRI::Util::load_module($t{pc},'transport/http');
136 0 ▶ if ($t{pc}->can('transport_default'))
Branch: 1 branch
BranchTrueFalseTotal
if ($t{"pc"}->can("transport_default")) 0 0 0
137 {
138 0 %opts=($t{pc}->transport_default('http'),%opts);
139 }
140  
141 0 my $self=$class->SUPER::new($ctx,\%opts); ## We are now officially a Net::DRI::Transport instance
142 0 $self->has_state(1); ## some registries need login (like .PL) some not (like .ES) ; see end of method & call to open_connection()
143 0 $self->is_sync(1);
144 0 $self->name('http');
145 0 $self->version('0.2');
146  
147 0 foreach my $k (qw/client_login client_password client_newpassword protocol_data/)
148 {
149 0 ▶ $t{$k}=$opts{$k} if exists($opts{$k});
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{$k} 0 0 0
150 }
151  
152 0 my @need=qw/read_data write_message/;
153 0 ▶ Net::DRI::Exception::usererr_invalid_parameters('protocol_connection class must have: '.join(' ',@need)) if (grep { ! $t{pc}->can($_) } @need);
Branch: 1 branch
BranchTrueFalseTotal
if grep {not $t{"pc"}->can($_);} @need 0 0 0
154 0 ▶ $t{protocol_data}=$opts{protocol_data} if (exists($opts{protocol_data}) && $opts{protocol_data});
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"protocol_data"} and $opts{"protocol_data"} 0 0 0
Condition0%0/3
exists $opts{"protocol_data"} and $opts{"protocol_data"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $opts{"protocol_data"} and $opts{"protocol_data"}
exists $opts{"protocol_data"} $opts{"protocol_data"}
Truth table0%0/3
A and B
A= exists $opts{"protocol_data"}B= $opts{"protocol_data"}
ABresult
0X0
100
111
155 0 ▶ Net::DRI::Exception::usererr_insufficient_parameters('remote_url must be defined') unless (exists $opts{'remote_url'} && defined $opts{'remote_url'});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"remote_url"} and defined $opts{"remote_url"} 0 0 0
Condition0%0/3
exists $opts{"remote_url"} and defined $opts{"remote_url"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $opts{"remote_url"} and defined $opts{"remote_url"}
exists $opts{"remote_url"} defined $opts{"remote_url"}
Truth table0%0/3
A and B
A= exists $opts{"remote_url"}B= defined $opts{"remote_url"}
ABresult
0X0
100
111
156 0 ▶ Net::DRI::Exception::usererr_invalid_parameters('remote_url must be an uri starting with http:// or https:// with a proper path') unless $opts{remote_url}=~m!^https?://\S+/\S*$!;
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"remote_url"} =~ m[^https?://\S+/\S*$] 0 0 0
157 0 $t{remote_url}=$opts{remote_url};
158 0 $t{remote_uri}=$t{remote_url}; ## only used for error messages
159  
160 0 my $ua=LWP::UserAgent->new();
161 0 $ua->agent(sprintf('Net::DRI/%s ',$Net::DRI::VERSION)); ## the final space triggers LWP::UserAgent to add its own string
162 0 $ua->cookie_jar({}); ## Cookies needed by some registries, like .PL (how strange !)
163 ## Now some security settings
164 0 $ua->max_redirect(0);
165 0 $ua->parse_head(0);
166 0 $ua->protocols_allowed(['http','https']);
167 0 ▶ $ua->timeout($self->timeout()) if $self->timeout(); ## problem with our own alarm ?
Branch: 1 branch
BranchTrueFalseTotal
if $self->timeout 0 0 0
168 0 ▶ $ua->local_address($opts{local_host}) if exists $opts{local_host} && defined $opts{local_host};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"local_host"} and defined $opts{"local_host"} 0 0 0
Condition0%0/3
exists $opts{"local_host"} and defined $opts{"local_host"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $opts{"local_host"} and defined $opts{"local_host"}
exists $opts{"local_host"} defined $opts{"local_host"}
Truth table0%0/3
A and B
A= exists $opts{"local_host"}B= defined $opts{"local_host"}
ABresult
0X0
100
111
169  
170 0 ▶ if ($t{remote_url}=~m!^https://!)
Branch: 1 branch
BranchTrueFalseTotal
if ($t{"remote_url"} =~ m[^https://]) 0 0 0
171 {
172 0 my %ssl=%{$self->parse_ssl_options(\%opts)};
173 0 ▶ while(my ($k,$v)=each %ssl)
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %ssl) 0 0 0
174 {
175 0 $ua->ssl_opts($k,$v);
176 }
177 }
178  
179 0 $t{ua}=$ua;
180 0 ▶ $t{verify_response}=$opts{verify_response} if (exists($opts{verify_response}) && defined($opts{verify_response}) && (ref($opts{verify_response}) eq 'CODE'));
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"verify_response"} and defined $opts{"verify_response"} and ref $opts{"verify_response"} eq "CODE" 0 0 0
Condition0%0/6
exists $opts{"verify_response"} and defined $opts{"verify_response"} and ref $opts{"verify_response"} eq "CODE"
!ll&&!rl&&r
000
exists $opts{"verify_response"} and defined $opts{"verify_response"}
!ll&&!rl&&r
000
MC/DC0%0/3
exists $opts{"verify_response"} and defined $opts{"verify_response"} and ref $opts{"verify_response"} eq "CODE"
exists $opts{"verify_response"} defined $opts{"verify_response"} ref $opts{"verify_response"} eq "CODE"
Truth table0%0/4
A and B and C
A= exists $opts{"verify_response"}B= defined $opts{"verify_response"}C= ref $opts{"verify_response"} eq "CODE"
ABCresult
0XX0
10X0
1100
1111
181 0 $self->{transport}=\%t;
182 0 ▶ $t{pc}->init($self) if $t{pc}->can('init');
Branch: 1 branch
BranchTrueFalseTotal
if $t{"pc"}->can("init") 0 0 0
183  
184 0 $self->open_connection($ctx); ## noop for registries without login, will properly setup has_state()
185 0 return $self;
186 }
187  
188 sub send_login
189 {
190 0 ▶ my ($self,$ctx)=@_;
Subroutine send_login: not called CC=7 SCAR=40.3
Pod undocumented
191 0 my $t=$self->transport_data();
192 0 my $pc=$t->{pc};
193 0 my ($cltrid,$dr);
194  
195 ## Get registry greeting, if available
196 0 ▶ if ($pc->can('greeting') && $pc->can('parse_greeting'))
Branch: 1 branch
BranchTrueFalseTotal
if ($pc->can("greeting") and $pc->can("parse_greeting")) 0 0 0
Condition0%0/3
$pc->can("greeting") and $pc->can("parse_greeting")
!ll&&!rl&&r
000
MC/DC0%0/2
$pc->can("greeting") and $pc->can("parse_greeting")
$pc->can("greeting") $pc->can("parse_greeting")
Truth table0%0/3
A and B
A= $pc->can("greeting")B= $pc->can("parse_greeting")
ABresult
0X0
100
111
197 {
198 0 $cltrid=$self->generate_trid($self->{logging_ctx}->{registry}); ## not used for greeting (<hello> has no clTRID), but used in logging
199 0 my $greeting=$pc->greeting($t->{message_factory});
200 0 $self->log_output('notice','transport',$ctx,{trid=>$cltrid,phase=>'opening',direction=>'out',message=>$greeting});
201 0 ▶ Net::DRI::Exception->die(0,'transport/http',4,'Unable to send greeting message to '.$t->{remote_uri}) unless $self->_http_send(1,$greeting,1);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_http_send(1, $greeting, 1) 0 0 0
202 0 $dr=$self->_http_receive(1);
203 0 $self->log_output('notice','transport',$ctx,{trid=>$cltrid,phase=>'opening',direction=>'in',message=>$dr});
204 0 my $rc1=$pc->parse_greeting($dr); ## gives back a Net::DRI::Protocol::ResultStatus
205 0 ▶ die($rc1) unless $rc1->is_success();
Branch: 1 branch
BranchTrueFalseTotal
unless $rc1->is_success 0 0 0
206 }
207  
208 0 my $login=$pc->login($t->{message_factory},$t->{client_login},$t->{client_password},$cltrid,$dr,$t->{client_newpassword},$t->{protocol_data});
209 0 $self->log_output('notice','transport',$ctx,{trid=>$cltrid,phase=>'opening',direction=>'out',message=>$login});
210 0 ▶ Net::DRI::Exception->die(0,'transport/http',4,'Unable to send login message to '.$t->{remote_uri}) unless $self->_http_send(1,$login,1);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_http_send(1, $login, 1) 0 0 0
211 0 $dr=$self->_http_receive(1);
212 0 $self->log_output('notice','transport',$ctx,{trid=>$cltrid,phase=>'opening',direction=>'in',message=>$dr});
213 0 my $rc2=$pc->parse_login($dr); ## gives back a Net::DRI::Protocol::ResultStatus
214 0 ▶ die($rc2) unless $rc2->is_success();
Branch: 1 branch
BranchTrueFalseTotal
unless $rc2->is_success 0 0 0
215 0 return;
216 }
217  
218 sub open_connection
219 {
220 0 ▶ my ($self,$ctx)=@_;
Subroutine open_connection: not called CC=3 SCAR=24.8
Pod undocumented
221 0 my $t=$self->transport_data();
222 0 my $pc=$t->{pc};
223 0 $self->has_state(0);
224  
225 0 ▶ if ($pc->can('login') && $pc->can('parse_login'))
Branch: 1 branch
BranchTrueFalseTotal
if ($pc->can("login") and $pc->can("parse_login")) 0 0 0
Condition0%0/3
$pc->can("login") and $pc->can("parse_login")
!ll&&!rl&&r
000
MC/DC0%0/2
$pc->can("login") and $pc->can("parse_login")
$pc->can("login") $pc->can("parse_login")
Truth table0%0/3
A and B
A= $pc->can("login")B= $pc->can("parse_login")
ABresult
0X0
100
111
226 {
227 0 $self->send_login($ctx);
228 0 $self->has_state(1);
229 0 $self->current_state(1);
230 }
231  
232 0 $self->time_open(time());
233 0 $self->time_used(time());
234 0 $self->transport_data()->{exchanges_done}=0;
235 0 return;
236 }
237  
238 sub send_logout
239 {
240 0 ▶ my ($self)=@_;
Subroutine send_logout: not called CC=5 SCAR=34.0
Pod undocumented
241 0 my $t=$self->transport_data();
242 0 my $pc=$t->{pc};
243  
244 0 ▶ return unless ($pc->can('logout') && $pc->can('parse_logout'));
Branch: 1 branch
BranchTrueFalseTotal
unless $pc->can("logout") and $pc->can("parse_logout") 0 0 0
Condition0%0/3
$pc->can("logout") and $pc->can("parse_logout")
!ll&&!rl&&r
000
MC/DC0%0/2
$pc->can("logout") and $pc->can("parse_logout")
$pc->can("logout") $pc->can("parse_logout")
Truth table0%0/3
A and B
A= $pc->can("logout")B= $pc->can("parse_logout")
ABresult
0X0
100
111
245  
246 0 my $cltrid=$self->generate_trid($self->{logging_ctx}->{registry});
247 0 my $logout=$pc->logout($t->{message_factory},$cltrid);
248 0 $self->log_output('notice','transport',{otype=>'session',oaction=>'logout'},{trid=>$cltrid,phase=>'closing',direction=>'out',message=>$logout});
249 0 ▶ Net::DRI::Exception->die(0,'transport/http',4,'Unable to send logout message to '.$t->{remote_uri}) unless $self->_http_send(1,$logout,3);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_http_send(1, $logout, 3) 0 0 0
250 0 my $dr=$self->_http_receive(1);
251 0 $self->log_output('notice','transport',{otype=>'session',oaction=>'logout'},{trid=>$cltrid,phase=>'closing',direction=>'in',message=>$dr});
252 0 my $rc1=$pc->parse_logout($dr);
253 0 ▶ die($rc1) unless $rc1->is_success();
Branch: 1 branch
BranchTrueFalseTotal
unless $rc1->is_success 0 0 0
254 0 return;
255 }
256  
257 sub close_connection
258 {
259 0 ▶ my ($self)=@_;
Subroutine close_connection: not called CC=3 SCAR=24.8
Pod undocumented
260 0 ▶ $self->send_logout() if ($self->has_state() && $self->current_state());
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_state and $self->current_state 0 0 0
Condition0%0/3
$self->has_state and $self->current_state
!ll&&!rl&&r
000
MC/DC0%0/2
$self->has_state and $self->current_state
$self->has_state $self->current_state
Truth table0%0/3
A and B
A= $self->has_stateB= $self->current_state
ABresult
0X0
100
111
261 0 $self->transport_data()->{ua}->cookie_jar({});
262 0 $self->current_state(0);
263 0 return;
264 }
265  
266 sub end
267 {
268 0 ▶ my ($self)=@_;
Subroutine end: not called CC=2 SCAR=17.9
Pod undocumented
269 0 ▶ if ($self->current_state())
Branch: 1 branch
BranchTrueFalseTotal
if ($self->current_state) 0 0 0
270 {
271 eval
272 0 {
273 0 ▶ local $SIG{ALRM}=sub { die 'timeout' };
Subroutine __ANON__: not called CC=1 SCAR=6.9
274 0 alarm(10);
275 0 $self->close_connection();
276 };
277 0 alarm(0); ## since close_connection may die, this must be outside of eval to be executed in all cases
278 }
279 0 return;
280 }
281  
282 sub send ## no critic (Subroutines::ProhibitBuiltinHomonyms)
283 {
284 0 ▶ my ($self,$ctx,$tosend)=@_;
Subroutine send: not called CC=1 SCAR=6.9
Pod undocumented
285 0 ▶ return $self->SUPER::send($ctx,$tosend,\&_http_send,sub {});
Subroutine __ANON__: not called CC=1 SCAR=6.9
286 }
287  
288 sub _http_send
289 {
290 0 ▶ my ($self,$count,$tosend,$phase)=@_;
Subroutine _http_send: not called CC=3 SCAR=24.8
291 0 ▶ $phase=2 unless defined($phase); ## Phase 2 = normal operations (1=greeting+login, 3=logout)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $phase 0 0 0
292 0 my $t=$self->transport_data();
293  
294 ## Content-Length is automatically computed and added during the request() call, no need to do it before
295 0 my $req=$t->{pc}->write_message($self,$tosend); ## gives back an HTTP::Request object
296 0 Net::DRI::Util::check_isa($req,'HTTP::Request');
297 0 my $ans=$t->{ua}->request($req);
298 0 ▶ $t->{verify_response}->($self,$phase,$count,$req,$ans) if exists($t->{verify_response});
Branch: 1 branch
BranchTrueFalseTotal
if exists $t->{"verify_response"} 0 0 0
299 0 $t->{last_reply}=$ans;
300 0 return 1; ## very important
301 }
302  
303 sub receive
304 {
305 0 ▶ my ($self,$ctx,$count)=@_;
Subroutine receive: not called CC=1 SCAR=6.9
Pod undocumented
306 0 return $self->SUPER::receive($ctx,\&_http_receive);
307 }
308  
309 sub _http_receive
310 {
311 0 ▶ my ($self,$count)=@_;
Subroutine _http_receive: not called CC=1 SCAR=6.9
312 0 my $t=$self->transport_data();
313  
314 ## Convert answer in a Net::DRI::Data::Raw object
315 0 my $dr=$t->{pc}->read_data($self,$t->{last_reply});
316 0 Net::DRI::Util::check_isa($dr,'Net::DRI::Data::Raw');
317 0 $t->{last_reply}=undef;
318 0 $t->{exchanges_done}++;
319 0 return $dr;
320 }
321  
322 #####################################################################################################
323 1 1;
« DRI/Protocol/EPP/Extensions/LU/Contact.pm ↑ Summary DRI/Protocol/EPP/Extensions/LU/Poll.pm »