| 1 | ## Domain Registry Interface, XML-RPC Transport | ||
| 2 | ## | ||
| 3 | ## Copyright (c) 2008-2011,2013 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::XMLRPCLite; | ||
| 16 | |||
| 17 | use strict; | ||
| 18 | use warnings; | ||
| 19 | |||
| 20 | use base qw(Net::DRI::Transport); | ||
| 21 | |||
| 22 | use Net::DRI::Exception; | ||
| 23 | use Net::DRI::Data::Raw; | ||
| 24 | use Net::DRI::Util; | ||
| 25 | use XMLRPC::Lite; | ||
| 26 | |||
| 27 | =pod | ||
| 28 | |||
| 29 | =head1 NAME | ||
| 30 | |||
| 31 | Net::DRI::Transport::HTTP::XMLRPCLite - XML-RPC Transport for Net::DRI | ||
| 32 | |||
| 33 | =head1 DESCRIPTION | ||
| 34 | |||
| 35 | Please see the README file for details. | ||
| 36 | |||
| 37 | =head1 SUPPORT | ||
| 38 | |||
| 39 | For now, support questions should be sent to: | ||
| 40 | |||
| 41 | E<lt>netdri@dotandco.comE<gt> | ||
| 42 | |||
| 43 | Please also see the SUPPORT file in the distribution. | ||
| 44 | |||
| 45 | =head1 SEE ALSO | ||
| 46 | |||
| 47 | E<lt>http://www.dotandco.com/services/software/Net-DRI/E<gt> | ||
| 48 | |||
| 49 | =head1 AUTHOR | ||
| 50 | |||
| 51 | Patrick Mevzek, E<lt>netdri@dotandco.comE<gt> | ||
| 52 | |||
| 53 | =head1 COPYRIGHT | ||
| 54 | |||
| 55 | Copyright (c) 2008-2011,2013 Patrick Mevzek <netdri@dotandco.com>. | ||
| 56 | All rights reserved. | ||
| 57 | |||
| 58 | This program is free software; you can redistribute it and/or modify | ||
| 59 | it under the terms of the GNU General Public License as published by | ||
| 60 | the Free Software Foundation; either version 2 of the License, or | ||
| 61 | (at your option) any later version. | ||
| 62 | |||
| 63 | See the LICENSE file that comes with this distribution for more details. | ||
| 64 | |||
| 65 | =cut | ||
| 66 | |||
| 67 | #################################################################################################### | ||
| 68 | sub new | ||
| 69 | { | ||
| 70 | my ($class,$ctx,$rp)=@_; | ||
| 71 | my %opts=%$rp; | ||
| 72 | my $po=$ctx->{protocol}; | ||
| 73 | |||
| 74 | my %t=(message_factory => $po->factories()->{message}); | ||
| 75 | if (exists($opts{protocol_connection}) && $opts{protocol_connection}) | ||
| 76 | { | ||
| 77 | $t{protocol_connection}=$opts{protocol_connection}; | ||
| 78 | Net::DRI::Util::load_module($t{protocol_connection},'transport/xmlrpclite'); | ||
| 79 | if ($t{protocol_connection}->can('transport_default')) | ||
| 80 | { | ||
| 81 | %opts=($t{protocol_connection}->transport_default('xmlrpclite'),%opts); | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | my $self=$class->SUPER::new($ctx,\%opts); ## We are now officially a Net::DRI::Transport instance | ||
| 86 | $self->is_sync(1); | ||
| 87 | $self->name('xmlrpclite'); | ||
| 88 | $self->version('0.1'); | ||
| 89 | |||
| 90 | $t{has_login}=(exists($opts{has_login}) && defined($opts{has_login}))? $opts{has_login} : 0; | ||
| 91 | $t{has_logout}=(exists($opts{has_logout}) && defined($opts{has_logout}))? $opts{has_logout} : 0; | ||
| 92 | $self->has_state($t{has_login}); | ||
| 93 | if ($t{has_login}) | ||
| 94 | { | ||
| 95 | foreach my $p (qw/client_login client_password/) | ||
| 96 | { | ||
| 97 | Net::DRI::Exception::usererr_insufficient_parameters($p.' must be provided') unless (exists($opts{$p}) && defined($opts{$p})); | ||
| 98 | $t{$p}=$opts{$p}; | ||
| 99 | } | ||
| 100 | $t{session_data}={}; | ||
| 101 | } | ||
| 102 | |||
| 103 | foreach my $p (qw/protocol_connection proxy_uri/) | ||
| 104 | { | ||
| 105 | Net::DRI::Exception::usererr_insufficient_parameters($p.' must be provided') unless (exists($opts{$p}) && defined($opts{$p})); | ||
| 106 | $t{$p}=$opts{$p}; | ||
| 107 | } | ||
| 108 | Net::DRI::Exception::usererr_invalid_parameters('proxy_uri must be http:// or https://') unless ($t{proxy_uri}=~m!^https?://!); | ||
| 109 | |||
| 110 | my $pc=$t{protocol_connection}; | ||
| 111 | if ($t{has_login}) | ||
| 112 | { | ||
| 113 | foreach my $m (qw/login parse_login extract_session/) | ||
| 114 | { | ||
| 115 | Net::DRI::Exception::usererr_invalid_parameters('Protocol connection class '.$pc.' must have a '.$m.'() method, since has_login=1') unless ($pc->can($m)); | ||
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | if ($t{has_logout}) | ||
| 120 | { | ||
| 121 | foreach my $m (qw/logout parse_logout/) | ||
| 122 | { | ||
| 123 | Net::DRI::Exception::usererr_invalid_parameters('Protocol connection class '.$pc.' must have a '.$m.'() method, since has_logout=1') unless ($pc->can($m)); | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | $self->{transport}=\%t; | ||
| 128 | |||
| 129 | if ($self->has_state()) | ||
| 130 | { | ||
| 131 | if ($self->defer()) ## we will open, but later | ||
| 132 | { | ||
| 133 | $self->current_state(0); | ||
| 134 | } else ## we will open NOW | ||
| 135 | { | ||
| 136 | $self->open_connection($ctx); | ||
| 137 | } | ||
| 138 | } else | ||
| 139 | { | ||
| 140 | $self->init(); | ||
| 141 | $self->time_open(time()); | ||
| 142 | } | ||
| 143 | |||
| 144 | return $self; | ||
| 145 | } | ||
| 146 | |||
| 147 | sub soap { my ($self,$v)=@_; $self->{transport}->{soap}=$v if defined $v; return $self->{transport}->{soap}; } | ||
| 148 | sub session_data { my ($self,$v)=@_; $self->{transport}->{session_data}=$v if defined $v; return $self->{transport}->{session_data}; } | ||
| 149 | |||
| 150 | sub init | ||
| 151 | { | ||
| 152 | my ($self)=@_; | ||
| 153 | return if defined($self->soap()); | ||
| 154 | my $soap=XMLRPC::Lite->new(); | ||
| 155 | $soap->proxy($self->{transport}->{proxy_uri}); | ||
| 156 | $soap->transport()->agent(sprintf('Net::DRI/%s ',$Net::DRI::VERSION).$soap->transport()->agent()); | ||
| 157 | $self->soap($soap); | ||
| 158 | return; | ||
| 159 | } | ||
| 160 | |||
| 161 | sub send_login | ||
| 162 | { | ||
| 163 | my ($self,$ctx)=@_; | ||
| 164 | my $t=$self->{transport}; | ||
| 165 | return unless $t->{has_login}; | ||
| 166 | foreach my $p (qw/client_login client_password/) | ||
| 167 | { | ||
| 168 | Net::DRI::Exception::usererr_insufficient_parameters($p.' must be defined') unless (exists($t->{$p}) && $t->{$p}); | ||
| 169 | } | ||
| 170 | |||
| 171 | my $pc=$t->{protocol_connection}; | ||
| 172 | my $cltrid=$self->generate_trid($self->{logging_ctx}->{registry}); | ||
| 173 | my $login=$pc->login($t->{message_factory},$t->{client_login},$t->{client_password},$cltrid); | ||
| 174 | my $res=$self->_send_receive({otype=>'session',oaction=>'login',trid=>$cltrid,phase=>'opening'},$login); | ||
| 175 | my $msg=$t->{message_factory}->(); | ||
| 176 | $msg->parse(Net::DRI::Data::Raw->new(1,[$res])); | ||
| 177 | my $rc=$pc->parse_login($msg); | ||
| 178 | die($rc) unless $rc->is_success(); | ||
| 179 | |||
| 180 | $self->session_data($pc->extract_session($msg)); | ||
| 181 | return; | ||
| 182 | } | ||
| 183 | |||
| 184 | sub send_logout | ||
| 185 | { | ||
| 186 | my ($self)=@_; | ||
| 187 | my $t=$self->{transport}; | ||
| 188 | return unless $t->{has_logout}; | ||
| 189 | |||
| 190 | my $pc=$t->{protocol_connection}; | ||
| 191 | my $cltrid=$self->generate_trid($self->{logging_ctx}->{registry}); | ||
| 192 | my $logout=$pc->logout($t->{message_factory},$cltrid,$t->{session_data}); | ||
| 193 | my $res=$self->_send_receive({otype=>'session',oaction=>'logout',trid=>$cltrid,phase=>'closing'},$logout); | ||
| 194 | my $msg=$t->{message_factory}->(); | ||
| 195 | $msg->parse(Net::DRI::Data::Raw->new(1,[$res])); | ||
| 196 | my $rc=$pc->parse_logout($msg); | ||
| 197 | die($rc) unless $rc->is_success(); | ||
| 198 | |||
| 199 | $self->session_data({}); | ||
| 200 | return; | ||
| 201 | } | ||
| 202 | |||
| 203 | sub _send_receive ## no critic (Subroutines::RequireFinalReturn) | ||
| 204 | { | ||
| 205 | my ($self,$ctx,$msg)=@_; | ||
| 206 | my $soap=$self->soap(); | ||
| 207 | my $err; | ||
| 208 | my $res=$soap->on_fault(sub { (undef,$err)=@_; return; })->call($msg->method(),@{$msg->params()}); | ||
| 209 | if (my $httpres=$soap->transport()->http_response()) | ||
| 210 | { | ||
| 211 | $self->log_output('notice','transport',$ctx,{direction=>'out',message=>$httpres->request()}); | ||
| 212 | $self->log_output('notice','transport',$ctx,{direction=>'in', message=>$httpres}); | ||
| 213 | } else | ||
| 214 | { | ||
| 215 | $self->log_output('error','transport',$ctx,{direction=>'out',message=>'No response for message '.$soap->serializer()->envelope(method => $msg->method(), @{$msg->params()})}); | ||
| 216 | } | ||
| 217 | return $res if defined $res && ref $res && ! $res->fault() && ! defined $err; | ||
| 218 | |||
| 219 | Net::DRI::Exception->die(1,'transport/soaplite',4,'Unable to send message due to SOAP fault: '.$err->faultcode().' '.$err->faultstring()) if defined $err && ref $err; | ||
| 220 | Net::DRI::Exception->die(1,'transport/soaplite',4,'Unable to send message due to SOAP transport error: '.$soap->transport()->status()) unless $soap->transport()->is_success(); | ||
| 221 | Net::DRI::Exception->die(1,'transport/soaplite',4,'Unable to send message due to SOAP deserialization error: '.$err); | ||
| 222 | } | ||
| 223 | |||
| 224 | sub open_connection | ||
| 225 | { | ||
| 226 | my ($self,$ctx)=@_; | ||
| 227 | $self->init(); | ||
| 228 | $self->send_login($ctx); | ||
| 229 | $self->current_state(1); | ||
| 230 | $self->time_open(time()); | ||
| 231 | $self->time_used(time()); | ||
| 232 | return; | ||
| 233 | } | ||
| 234 | |||
| 235 | sub close_connection | ||
| 236 | { | ||
| 237 | my ($self)=@_; | ||
| 238 | $self->send_logout(); | ||
| 239 | $self->soap(undef); | ||
| 240 | $self->current_state(0); | ||
| 241 | return; | ||
| 242 | } | ||
| 243 | |||
| 244 | sub end | ||
| 245 | { | ||
| 246 | my ($self)=@_; | ||
| 247 | if ($self->has_state() && $self->current_state()) | ||
| 248 | { | ||
| 249 | eval | ||
| 250 | { | ||
| 251 | local $SIG{ALRM}=sub { die 'timeout' }; | ||
| 252 | alarm(10); | ||
| 253 | $self->close_connection(); | ||
| 254 | }; | ||
| 255 | alarm(0); ## since close_connection may die, this must be outside of eval to be executed in all cases | ||
| 256 | } | ||
| 257 | return; | ||
| 258 | } | ||
| 259 | |||
| 260 | #################################################################################################### | ||
| 261 | |||
| 262 | sub send ## no critic (Subroutines::ProhibitBuiltinHomonyms) | ||
| 263 | { | ||
| 264 | my ($self,$ctx,$tosend)=@_; | ||
| 265 | return $self->SUPER::send($ctx,$tosend,\&_soap_send,sub {}); | ||
| 266 | } | ||
| 267 | |||
| 268 | sub _soap_send | ||
| 269 | { | ||
| 270 | my ($self,$count,$tosend,$ctx)=@_; | ||
| 271 | my $t=$self->{transport}; | ||
| 272 | $tosend->add_session($self->session_data()) if $tosend->can('add_session'); | ||
| 273 | my $res=$self->_send_receive($ctx,$tosend); | ||
| 274 | $t->{last_reply}=$res; | ||
| 275 | return 1; ## very important | ||
| 276 | } | ||
| 277 | |||
| 278 | sub receive | ||
| 279 | { | ||
| 280 | my ($self,$ctx,$count)=@_; | ||
| 281 | return $self->SUPER::receive($ctx,\&_soap_receive); | ||
| 282 | } | ||
| 283 | |||
| 284 | sub _soap_receive | ||
| 285 | { | ||
| 286 | my ($self,$count)=@_; | ||
| 287 | my $t=$self->{transport}; | ||
| 288 | my $r=$t->{last_reply}; | ||
| 289 | $t->{last_reply}=undef; | ||
| 290 | return Net::DRI::Data::Raw->new(6,[$r]); | ||
| 291 | } | ||
| 292 | |||
| 293 | #################################################################################################### | ||
| 294 | 1; |