RSS

(root)/bugzilla/trunk : /Bugzilla/WebService/Server/XMLRPC.pm (revision 7061)

To get this branch, use:
bzr branch /bugzilla/trunk
Line Revision Contents
1 6475
# -*- Mode: perl; indent-tabs-mode: nil -*-
2
#
3
# The contents of this file are subject to the Mozilla Public
4
# License Version 1.1 (the "License"); you may not use this file
5
# except in compliance with the License. You may obtain a copy of
6
# the License at http://www.mozilla.org/MPL/
7
#
8
# Software distributed under the License is distributed on an "AS
9
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
10
# implied. See the License for the specific language governing
11
# rights and limitations under the License.
12
#
13
# The Original Code is the Bugzilla Bug Tracking System.
14
#
15
# Contributor(s): Marc Schumann <wurblzap@gmail.com>
16
#                 Max Kanat-Alexander <mkanat@bugzilla.org>
17 6575
#                 Rosie Clarkson <rosie.clarkson@planningportal.gov.uk>
18
#                 
19
# Portions © Crown copyright 2009 - Rosie Clarkson (development@planningportal.gov.uk) for the Planning Portal
20 6475
21
package Bugzilla::WebService::Server::XMLRPC;
22
23
use strict;
24
use XMLRPC::Transport::HTTP;
25
use Bugzilla::WebService::Server;
26
our @ISA = qw(XMLRPC::Transport::HTTP::CGI Bugzilla::WebService::Server);
27
28
use Bugzilla::WebService::Constants;
29
30
sub initialize {
31
    my $self = shift;
32
    my %retval = $self->SUPER::initialize(@_);
33
    $retval{'serializer'}   = Bugzilla::XMLRPC::Serializer->new;
34
    $retval{'deserializer'} = Bugzilla::XMLRPC::Deserializer->new;
35
    $retval{'dispatch_with'} = WS_DISPATCH;
36
    return %retval;
37
}
38
39
sub make_response {
40
    my $self = shift;
41
42
    $self->SUPER::make_response(@_);
43
44
    # XMLRPC::Transport::HTTP::CGI doesn't know about Bugzilla carrying around
45
    # its cookies in Bugzilla::CGI, so we need to copy them over.
46
    foreach (@{Bugzilla->cgi->{'Bugzilla_cookie_list'}}) {
47
        $self->response->headers->push_header('Set-Cookie', $_);
48
    }
49
}
50
51
sub handle_login {
52
    my ($self, $classes, $action, $uri, $method) = @_;
53
    my $class = $classes->{$uri};
54
    my $full_method = $uri . "." . $method;
55
    $self->SUPER::handle_login($class, $method, $full_method);
56
    return;
57
}
58
59
1;
60
61
# This exists to validate input parameters (which XMLRPC::Lite doesn't do)
62
# and also, in some cases, to more-usefully decode them.
63
package Bugzilla::XMLRPC::Deserializer;
64
use strict;
65
# We can't use "use base" because XMLRPC::Serializer doesn't return
66
# a true value.
67 6819
use XMLRPC::Lite;
68 6475
our @ISA = qw(XMLRPC::Deserializer);
69
70
use Bugzilla::Error;
71 6808
use Scalar::Util qw(tainted);
72
73
sub deserialize {
74
    my $self = shift;
75
    my ($xml) = @_;
76
    my $som = $self->SUPER::deserialize(@_);
77
    if (tainted($xml)) {
78
        $som->{_bz_do_taint} = 1;
79
    }
80
    bless $som, 'Bugzilla::XMLRPC::SOM';
81 6962
    Bugzilla->input_params($som->paramsin || {}); 
82 6808
    return $som;
83
}
84 6475
85
# Some method arguments need to be converted in some way, when they are input.
86
sub decode_value {
87
    my $self = shift;
88
    my ($type) = @{ $_[0] };
89
    my $value = $self->SUPER::decode_value(@_);
90
    
91
    # We only validate/convert certain types here.
92
    return $value if $type !~ /^(?:int|i4|boolean|double|dateTime\.iso8601)$/;
93
    
94
    # Though the XML-RPC standard doesn't allow an empty <int>,
95
    # <double>,or <dateTime.iso8601>,  we do, and we just say
96
    # "that's undef".
97
    if (grep($type eq $_, qw(int double dateTime))) {
98
        return undef if $value eq '';
99
    }
100
    
101
    my $validator = $self->_validation_subs->{$type};
102
    if (!$validator->($value)) {
103
        ThrowUserError('xmlrpc_invalid_value',
104
                       { type => $type, value => $value });
105
    }
106
    
107
    # We convert dateTimes to a DB-friendly date format.
108
    if ($type eq 'dateTime.iso8601') {
109 7041.1.1
        if ($value !~ /T.*[\-+Z]/i) {
110
           # The caller did not specify a timezone, so we assume UTC.
111
           # pass 'Z' specifier to datetime_from to force it
112
           $value = $value . 'Z';
113
        }
114 7061
        $value = Bugzilla::WebService::Server::XMLRPC->datetime_format_inbound($value);
115 6475
    }
116
117
    return $value;
118
}
119
120
sub _validation_subs {
121
    my $self = shift;
122
    return $self->{_validation_subs} if $self->{_validation_subs};
123
    # The only place that XMLRPC::Lite stores any sort of validation
124
    # regex is in XMLRPC::Serializer. We want to re-use those regexes here.
125
    my $lookup = Bugzilla::XMLRPC::Serializer->new->typelookup;
126
    
127
    # $lookup is a hash whose values are arrayrefs, and whose keys are the
128
    # names of types. The second item of each arrayref is a subroutine
129
    # that will do our validation for us.
130
    my %validators = map { $_ => $lookup->{$_}->[1] } (keys %$lookup);
131
    # Add a boolean validator
132
    $validators{'boolean'} = sub {$_[0] =~ /^[01]$/};
133
    # Some types have multiple names, or have a different name in
134
    # XMLRPC::Serializer than their standard XML-RPC name.
135
    $validators{'dateTime.iso8601'} = $validators{'dateTime'};
136
    $validators{'i4'} = $validators{'int'};
137
    
138
    $self->{_validation_subs} = \%validators;
139
    return \%validators;
140
}
141
142
1;
143
144 6808
package Bugzilla::XMLRPC::SOM;
145
use strict;
146 6819
use XMLRPC::Lite;
147 6808
our @ISA = qw(XMLRPC::SOM);
148
use Bugzilla::WebService::Util qw(taint_data);
149
150
sub paramsin {
151
    my $self = shift;
152 6813
    return $self->{bz_params_in} if $self->{bz_params_in};
153 6808
    my $params = $self->SUPER::paramsin(@_);
154
    if ($self->{_bz_do_taint}) {
155
        taint_data($params);
156
    }
157 6813
    $self->{bz_params_in} = $params;
158
    return $self->{bz_params_in};
159 6808
}
160
161
1;
162
163 6475
# This package exists to fix a UTF-8 bug in SOAP::Lite.
164
# See http://rt.cpan.org/Public/Bug/Display.html?id=32952.
165
package Bugzilla::XMLRPC::Serializer;
166 6614
use Scalar::Util qw(blessed);
167 6475
use strict;
168
# We can't use "use base" because XMLRPC::Serializer doesn't return
169
# a true value.
170 6819
use XMLRPC::Lite;
171 6475
our @ISA = qw(XMLRPC::Serializer);
172
173
sub new {
174
    my $class = shift;
175
    my $self = $class->SUPER::new(@_);
176
    # This fixes UTF-8.
177
    $self->{'_typelookup'}->{'base64'} =
178
        [10, sub { !utf8::is_utf8($_[0]) && $_[0] =~ /[^\x09\x0a\x0d\x20-\x7f]/},
179
        'as_base64'];
180
    # This makes arrays work right even though we're a subclass.
181
    # (See http://rt.cpan.org//Ticket/Display.html?id=34514)
182
    $self->{'_encodingStyle'} = '';
183
    return $self;
184
}
185
186 6575
# Here the XMLRPC::Serializer is extended to use the XMLRPC nil extension.
187
sub encode_object {
188
    my $self = shift;
189
    my @encoded = $self->SUPER::encode_object(@_);
190
191
    return $encoded[0]->[0] eq 'nil'
192
        ? ['value', {}, [@encoded]]
193
        : @encoded;
194
}
195
196 6614
# Removes undefined values so they do not produce invalid XMLRPC.
197
sub envelope {
198
    my $self = shift;
199
    my ($type, $method, $data) = @_;
200
    # If the type isn't a successful response we don't want to change the values.
201
    if ($type eq 'response'){
202
        $data = _strip_undefs($data);
203
    }
204
    return $self->SUPER::envelope($type, $method, $data);
205
}
206
207
# In an XMLRPC response we have to handle hashes of arrays, hashes, scalars,
208
# Bugzilla objects (reftype = 'HASH') and XMLRPC::Data objects.
209
# The whole XMLRPC::Data object must be removed if its value key is undefined
210
# so it cannot be recursed like the other hash type objects.
211
sub _strip_undefs {
212
    my ($initial) = @_;
213
    if (ref $initial eq "HASH" || (blessed $initial && $initial->isa("HASH"))) {
214
        while (my ($key, $value) = each(%$initial)) {
215
            if ( !defined $value
216
                 || (blessed $value && $value->isa('XMLRPC::Data') && !defined $value->value) )
217
            {
218
                # If the value is undefined remove it from the hash.
219
                delete $initial->{$key};
220
            }
221
            else {
222
                $initial->{$key} = _strip_undefs($value);
223
            }
224
        }
225
    }
226
    if (ref $initial eq "ARRAY" || (blessed $initial && $initial->isa("ARRAY"))) {
227
        for (my $count = 0; $count < scalar @{$initial}; $count++) {
228
            my $value = $initial->[$count];
229
            if ( !defined $value
230
                 || (blessed $value && $value->isa('XMLRPC::Data') && !defined $value->value) )
231
            {
232
                # If the value is undefined remove it from the array.
233
                splice(@$initial, $count, 1);
234
                $count--;
235
            }
236
            else {
237
                $initial->[$count] = _strip_undefs($value);
238
            }
239
        }
240
    }
241
    return $initial;
242
}
243
244 6575
sub BEGIN {
245
    no strict 'refs';
246
    for my $type (qw(double i4 int dateTime)) {
247
        my $method = 'as_' . $type;
248
        *$method = sub {
249
            my ($self, $value) = @_;
250
            if (!defined($value)) {
251
                return as_nil();
252
            }
253
            else {
254
                my $super_method = "SUPER::$method"; 
255
                return $self->$super_method($value);
256
            }
257
        }
258
    }
259
}
260
261
sub as_nil {
262
    return ['nil', {}];
263
}
264
265 6475
1;
266 6534
267
__END__
268
269
=head1 NAME
270
271
Bugzilla::WebService::Server::XMLRPC - The XML-RPC Interface to Bugzilla
272
273
=head1 DESCRIPTION
274
275
This documentation describes things about the Bugzilla WebService that
276
are specific to XML-RPC. For a general overview of the Bugzilla WebServices,
277
see L<Bugzilla::WebService>.
278
279
=head1 XML-RPC
280
281
The XML-RPC standard is described here: L<http://www.xmlrpc.com/spec>
282
283
=head1 CONNECTING
284
285
The endpoint for the XML-RPC interface is the C<xmlrpc.cgi> script in
286
your Bugzilla installation. For example, if your Bugzilla is at
287
C<bugzilla.yourdomain.com>, then your XML-RPC client would access the
288
API via: C<http://bugzilla.yourdomain.com/xmlrpc.cgi>
289
290
=head1 PARAMETERS
291
292
C<dateTime> fields are the standard C<dateTime.iso8601> XML-RPC field. They
293 7042
should be in C<YYYY-MM-DDTHH:MM:SS> format (where C<T> is a literal T). As
294
of Bugzilla B<3.6>, Bugzilla always expects C<dateTime> fields to be in the
295
UTC timezone, and all returned C<dateTime> values are in the UTC timezone.
296 6534
297
All other fields are standard XML-RPC types.
298
299
=head2 How XML-RPC WebService Methods Take Parameters
300
301
All functions take a single argument, a C<< <struct> >> that contains all parameters.
302
The names of the parameters listed in the API docs for each function are the
303
C<< <name> >> element for the struct C<< <member> >>s.
304
305
=head1 EXTENSIONS TO THE XML-RPC STANDARD
306
307
=head2 Undefined Values
308
309
Normally, XML-RPC does not allow empty values for C<int>, C<double>, or
310
C<dateTime.iso8601> fields. Bugzilla does--it treats empty values as
311
C<undef> (called C<NULL> or C<None> in some programming languages).
312
313 7042
Bugzilla accepts a timezone specifier at the end of C<dateTime.iso8601>
314
fields that are specified as method arguments. The format of the timezone
315
specifier is specified in the ISO-8601 standard. If no timezone specifier
316
is included, the passed-in time is assumed to be in the UTC timezone.
317
Bugzilla will never output a timezone specifier on returned data, because
318
doing so would violate the XML-RPC specification. All returned times are in
319
the UTC timezone.
320
321 6575
Bugzilla also accepts an element called C<< <nil> >>, as specified by the
322
XML-RPC extension here: L<http://ontosys.com/xml-rpc/extensions.php>, which
323
is always considered to be C<undef>, no matter what it contains.
324
325 6614
Bugzilla does not use C<< <nil> >> values in returned data, because currently
326
most clients do not support C<< <nil> >>. Instead, any fields with C<undef>
327
values will be stripped from the response completely. Therefore
328
B<the client must handle the fact that some expected fields may not be 
329
returned>.
330 6534
331
=begin private
332
333 6575
nil is implemented by XMLRPC::Lite, in XMLRPC::Deserializer::decode_value
334
in the CPAN SVN since 14th Dec 2008
335
L<http://rt.cpan.org/Public/Bug/Display.html?id=20569> and in Fedora's
336
perl-SOAP-Lite package in versions 0.68-1 and above.
337 6534
338 6573
=end private
339 7020
340
=head1 SEE ALSO
341
342
L<Bugzilla::WebService>

Loggerhead 1.18.1 is a web-based interface for Bazaar branches