1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
package XML::RPC;
use strict;
use XML::TreePP;
use Data::Dumper;
use vars qw($VERSION $faultCode);
no strict 'refs';
$VERSION = 0.5;
sub new {
my $package = shift;
my $self = { };
bless $self, $package;
$self->{url} = shift;
$self->{tpp} = XML::TreePP->new(@_);
return $self;
}
sub call {
my $self = shift;
my ( $methodname, @params ) = @_;
die 'no url' if ( !$self->{url} );
$faultCode = 0;
my $xml = $self->create_call_xml( $methodname, @params );
#print STDERR $xml;
my $result = $self->{tpp}->parsehttp(
POST => $self->{url},
$xml,
{
'Content-Type' => 'text/xml',
'User-Agent' => 'XML-RPC/' . $VERSION,
'Content-Length' => length($xml)
}
);
my @data = $self->unparse_response($result);
return @data == 1 ? $data[0] : @data;
}
sub receive {
my $self = shift;
my $result = eval {
my $xml = shift || die 'no xml';
my $handler = shift || die 'no handler';
my $hash = $self->{tpp}->parse($xml);
my ( $methodname, @params ) = $self->unparse_call($hash);
$self->create_response_xml( $handler->( $methodname, @params ) );
};
return $self->create_fault_xml($@) if ($@);
return $result;
}
sub create_fault_xml {
my $self = shift;
my $error = shift;
chomp($error);
return $self->{tpp}
->write( { methodResponse => { fault => $self->parse( { faultString => $error, faultCode => $faultCode } ) } } );
}
sub create_call_xml {
my $self = shift;
my ( $methodname, @params ) = @_;
return $self->{tpp}->write(
{
methodCall => {
methodName => $methodname,
params => { param => [ map { $self->parse($_) } @params ] }
}
}
);
}
sub create_response_xml {
my $self = shift;
my @params = @_;
return $self->{tpp}->write( { methodResponse => { params => { param => [ map { $self->parse($_) } @params ] } } } );
}
sub parse {
my $self = shift;
my $p = shift;
my $result;
if ( ref($p) eq 'HASH' ) {
$result = $self->parse_struct($p);
}
elsif ( ref($p) eq 'ARRAY' ) {
$result = $self->parse_array($p);
}
else {
$result = $self->parse_scalar($p);
}
return { value => $result };
}
sub parse_scalar {
my $self = shift;
my $scalar = shift;
local $^W = undef;
if ( ( $scalar =~ m/^[\-+]?\d+$/ )
&& ( abs($scalar) <= ( 0xffffffff >> 1 ) ) )
{
return { i4 => $scalar };
}
elsif ( $scalar =~ m/^[\-+]?\d+\.\d+$/ ) {
return { double => $scalar };
}
else {
return { string => \$scalar };
}
}
sub parse_struct {
my $self = shift;
my $hash = shift;
my @members;
while ( my ( $k, $v ) = each(%$hash) ) {
push @members, { name => $k, %{ $self->parse($v) } };
}
return { struct => { member => \@members } };
}
sub parse_array {
my $self = shift;
my $array = shift;
return { array => { data => { value => [ map { $self->parse($_)->{value} } $self->list($array) ] } } };
}
sub unparse_response {
my $self = shift;
my $hash = shift;
my $response = $hash->{methodResponse} || die 'no data';
if ( $response->{fault} ) {
return $self->unparse_value( $response->{fault}->{value} );
}
else {
return map { $self->unparse_value( $_->{value} ) } $self->list( $response->{params}->{param} );
}
}
sub unparse_call {
my $self = shift;
my $hash = shift;
my $response = $hash->{methodCall} || die 'no data';
my $methodname = $response->{methodName};
my @args =
map { $self->unparse_value( $_->{value} ) } $self->list( $response->{params}->{param} );
return ( $methodname, @args );
}
sub unparse_value {
my $self = shift;
my $value = shift;
my $result;
return $value if ( ref($value) ne 'HASH' ); # for unspecified params
if ( $value->{struct} ) {
$result = $self->unparse_struct( $value->{struct} );
return !%$result
? undef
: $result; # fix for empty hashrefs from XML::TreePP
}
elsif ( $value->{array} ) {
return $self->unparse_array( $value->{array} );
}
else {
return $self->unparse_scalar($value);
}
}
sub unparse_scalar {
my $self = shift;
my $scalar = shift;
my ($result) = values(%$scalar);
return ( ref($result) eq 'HASH' && !%$result )
? undef
: $result; # fix for empty hashrefs from XML::TreePP
}
sub unparse_struct {
my $self = shift;
my $struct = shift;
return { map { $_->{name} => $self->unparse_value( $_->{value} ) } $self->list( $struct->{member} ) };
}
sub unparse_array {
my $self = shift;
my $array = shift;
my $data = $array->{data};
return [ map { $self->unparse_value($_) } $self->list( $data->{value} ) ];
}
sub list {
my $self = shift;
my $param = shift;
return () if ( !$param );
return @$param if ( ref($param) eq 'ARRAY' );
return ($param);
}
1;
|