lib/Catalyst/Plugin/Server/XMLRPC.pm

filter: Statementstmt 22.5% 18 / 80 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 22 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 54.5% 6 / 11 Podpod n/a totaltotal 15.4% 24 / 155 CC 23 SCAR 43.0
CC
23
Cov
14%
finalize
132.0
prepare_action
90.0
dispatch
30.0
CRAP 73.4
↑ Summary blib/lib/Catalyst/Plugin/Server/XMLRPC/DispatchType/XMLRPCPath.pm »
1 =head1 NAME
2  
3 Catalyst::Plugin::Server::XMLRPC -- Catalyst XMLRPC Server Plugin
4  
5 =head1 SYNOPSIS
6  
7 package MyApp;
8 use Catalyst qw/Server Server::XMLRPC/;
9  
10 package MyApp::Controller::Example;
11 use base 'Catalyst::Controller';
12  
13 sub echo : XMLRPC { # available as: example.echo
14 my ( $self, $c, @args ) = @_;
15 $c->stash->{xmlrpc} = join ', ', @args;
16 }
17  
18 sub ping : XMLRPCPath('/ping') { # available as: ping
19 my ( $self, $c ) = @_;
20 $c->stash->{xmlrpc} = 'Pong';
21 }
22  
23 sub world : XMLRPCRegex(/hello/) { # available as: *hello*
24 my ($self, $c) = @_;
25 $c->stash->{xmlrpc} = 'World';
26 }
27  
28 sub echo : XMLRPCLocal { # available as: example.echo
29 my ( $self, $c, @args ) = @_;
30 $c->stash->{xmlrpc} = join ', ', @args;
31 }
32  
33 sub ping : XMLRPCGlobal { # available as: ping
34 my ( $self, $c ) = @_;
35 $c->stash->{xmlrpc} = 'Pong';
36 }
37  
38 =head1 DESCRIPTION
39  
40 XMLRPC Plugin for Catalyst which we tried to make compatible with the
41 way Catalyst works with URLS. Main features are:
42  
43 =over 4
44  
45 =item * Split XMLRPC methodNames by STRING to find out Controller.
46  
47 =item * Single entrypoint for XMLRPC calls, like http://host.tld/rpc
48  
49 =item * DispatchTypes (attributes) which work much the same as Catalyst attrs
50  
51 =item * XMLRPC Parameter handling transparent to Catalyst parameter handling
52  
53 =back
54  
55 =head1 HOW IT WORKS
56  
57 The default behaviour will handle XMLRPC Requests sent to C</rpc> by creating
58 an OBJECT containing XMLRPC specific parameters in C<< $c->req->xmlrpc >>.
59  
60 Directly after, it will find out the Path of the Action to dispatch to, by
61 splitting methodName by C<.>:
62  
63 methodName: hello.world
64 path : /hello/world
65  
66 From this point, it will dispatch to '/hello/world' when it exists,
67 like Catalyst Urls would do. What means: you will be able to set Regexes,
68 Paths etc on subroutines to define the endpoint.
69  
70 We discuss these custom XMLRPC attributes below.
71  
72 When the request is dispatched, we will return $c->stash->{xmlrpc} to the
73 xmlrpc client, or, when it is not available, it will return $c->stash to
74 the client. There is also a way of defining $c->stash keys to be send back
75 to the client.
76  
77 =head1 ATTRIBUTES
78  
79 You can mark any method in your Catalyst application as being
80 available remotely by using one of the following attributes,
81 which can be added to any existing attributes, except Private.
82 Remember that one of the mentioned attributes below are automatically
83 also Privates...
84  
85 =over 4
86  
87 =item XMLRPC
88  
89 Make this method accessible via XMLRPC, the same way as Local does
90 when using catalyst by URL.
91  
92 The following example will be accessible by method C<< hello.world >>:
93  
94 package Catalyst::Controller::Hello
95 sub world : XMLRPC {}
96  
97 =item XMLRPCLocal
98  
99 Identical version of attribute C<XMLRPC>
100  
101 =item XMLRPCGlobal
102  
103 Make this method accessible via XMLRPC, the same way as GLOBAL does
104 when using catalyst by URL.
105  
106 The following example will be accessible by method C<< ping >>:
107  
108 package Catalyst::Controller::Hello
109 sub ping : XMLRPCGlobal {}
110  
111 =item XMLRPCPath('/say/hello')
112  
113 Make this method accessible via XMLRPC, the same way as Path does
114 when using catalyst by URL.
115  
116 The following example will be accessible by method C<< say.hello >>:
117  
118 package Catalyst::Controller::Hello
119 sub hello : XMLRPCPath('/say/hello') {}
120  
121 =item XMLRPCRegex('foo')
122  
123 Make this method accessible via XMLRPC, the same way as Regex does
124 when using catalyst by URL.
125  
126 The following example will be accessible by example methods:
127 C<< a.foo.method >>
128 C<< wedoofoohere >>
129 C<< foo.getaround >>
130  
131 package Catalyst::Controller::Hello
132 sub hello : XMLRPCPath('foo') {}
133  
134 =back
135  
136 =head1 ACCESSORS
137  
138 Once you've used the plugin, you'll have an $c->request->xmlrpc accessor
139 which will return an C<Catalyst::Plugin::Server::XMLRPC> object.
140  
141 You can query this object as follows:
142  
143 =over 4
144  
145 =item $c->req->xmlrpc->is_xmlrpc_request
146  
147 Boolean indicating whether the current request has been initiated
148 via XMLRPC
149  
150 =item $c->req->xmlrpc->config
151  
152 Returns a C<Catalyst::Plugin::Server::XMLRPC::Config> object. See the
153 C<CONFIGURATION> below on how to use and configure it.
154  
155 =item $c->req->xmlrpc->body
156  
157 The body of the original XMLRPC call
158  
159 =item $c->req->xmlrpc->method
160  
161 The name of the original method called via XMLRPC
162  
163 =item $c->req->xmlrpc->args
164  
165 A list of parameters supplied by the XMLRPC call
166  
167 =item $c->req->xmlrpc->result_as_string
168  
169 The XML body that will be sent back to the XMLRPC client
170  
171 =item $c->req->xmlrpc->error
172  
173 Allows you to set xmlrpc fault code and message
174  
175 Example:
176  
177 $c->req->xmlrpc->error( [ 401 => 'Unauthorized' ] )
178  
179 To return status code C<401> with message C<Unauthorized>
180  
181 The default is to return error code C<500> on error.
182  
183 =back
184  
185 =head1 Server Accessors
186  
187 The following accessors are always available, whether you're in a xmlrpc
188 specific request or not
189  
190 =over 4
191  
192 =item $c->server->xmlrpc->list_methods
193  
194 Returns a HASHREF containing the available xmlrpc methods in Catalyst as
195 a key, and the C<Catalyst::Action> object as a value.
196  
197 =back
198  
199 =head1 CATALYST REQUEST
200  
201 To make things transparent, we try to put XMLRPC params into the Request
202 object of Catalyst. But first we will explain something about the XMLRPC
203 specifications.
204  
205 A full draft of these specifications can be found on:
206 C<http://www.xmlrpc.com/spec>
207  
208 In short, a xmlrpc-request consists of a methodName, like a subroutine
209 name, and a list of parameters. This list of parameters may contain strings
210 (STRING), arrays (LIST) and structs (HASH). Off course, these can be nested.
211  
212 =over 4
213  
214 =item $c->req->arguments
215  
216 We will put the list of arguments into $c->req->arguments, thisway you can
217 fetch this list within your dispatched-to-subroutine:
218  
219 sub echo : XMLRPC {
220 my ($self, $c, @args) = @_;
221 $c->log->debug($arg[0]); # Prints first XMLRPC parameter
222 # to debug log
223 }
224  
225 =item $c->req->parameters
226  
227 Because XMLRPC parameters are a LIST, we can't B<just> fill
228 $c->req->paremeters. To keep things transparent, we made an extra config
229 option what tells the XMLRPC server we can assume the following conditions
230 on all XMLRPC requests:
231 - There is only one XMLRPC parameter
232 - This XMLRPC parameter is a struct (HASH)
233  
234 We will put this STRUCT as key-value pairs into $c->req->parameters.
235  
236 =item $c->req->params
237  
238 Alias of $c->req->parameters
239  
240 =item $c->req->param
241  
242 Alias of $c->req->parameters
243  
244 =back
245  
246 =cut
247  
248 { package Catalyst::Plugin::Server::XMLRPC;
249  
250 7 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
251 7 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
252 7 ▶ use attributes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
253 7 ▶ use MRO::Compat;
Subroutine BEGIN: called CC=1 SCAR=0.0
254 7 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
255  
256 my $ServerClass = 'Catalyst::Plugin::Server::XMLRPC::Backend';
257  
258 ### only for development dumps!
259 my $Debug = 0;
260  
261 ###
262 ### Catalyst loading and dispatching
263 ###
264  
265 ### Loads our xmlrpc backend class in $c->server->xmlrpc
266 0 ▶ sub setup_engine {
Subroutine setup_engine: not called CC=1 SCAR=6.9
267 0 my $class = shift;
268 0 $class->server->register_server(
269 'xmlrpc' => $ServerClass->new($class)
270 );
271 0 $class->next::method(@_);
272 }
273  
274 ### Will load our customized DispatchTypes into Catalyst
275 0 ▶ sub setup_dispatcher {
Subroutine setup_dispatcher: not called CC=1 SCAR=6.9
276 0 my $class = shift;
277  
278 ### Load custom DispatchTypes
279 0 $class->next::method( @_ );
280 $class->dispatcher->preload_dispatch_types(
281 0 @{$class->dispatcher->preload_dispatch_types},
282 qw/ +Catalyst::Plugin::Server::XMLRPC::DispatchType::XMLRPCPath
283 +Catalyst::Plugin::Server::XMLRPC::DispatchType::XMLRPCRegex/
284 );
285  
286 0 return $class;
287 }
288  
289 ### Loads the xmlrpc-server object, redispatch to the method
290 0 ▶ sub prepare_action {
Subroutine prepare_action: not called CC=9 SCAR=45.0
291 0 my $c = shift;
292 0 my @args = @_;
293  
294 ### set up the accessor to hold an xmlrpc server instance
295 0 $c->req->register_server(
296 'xmlrpc' => Catalyst::Plugin::Server::XMLRPC::Request->new()
297 );
298  
299 ### are we an xmlrpc call? check the path against a regex
300 0 my $path = $c->server->xmlrpc->config->path;
301 0 ▶ if( $c->req->path =~ /$path/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->req->path =~ /$path/) { } 0 0 0
302  
303 PREPARE: {
304 ### mark us as an xmlrpc request
305 0 $c->req->xmlrpc->is_xmlrpc_request(1);
306  
307 0 ▶ $c->log->debug( 'PREPARE WITH $c ' . Dumper ($c ) ) if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Debug 0 0 0
308  
309 0 ▶ $c->req->xmlrpc->_deserialize_xml( $c ) or last PREPARE;
Branch: 1 branch
BranchTrueFalseTotal
$c->req->xmlrpc->_deserialize_xml($c) or last PREPARE 0 0 0
310  
311 ### CAVEAT: we consider backing up to a default for a
312 ### xml-rpc method when the method doesn't exist a security
313 ### risk. So when the exact method doesn't exist, we return
314 ### an error.
315 ### TODO ARGH Because of regex methods, this won't work
316  
317 ### set the new request path, the one we will forward to
318 0 $c->req->path( $c->req->xmlrpc->forward_path );
319  
320 ### filter change dispatch types to our OWN
321 0 ▶ { my $saved_dt = $c->dispatcher->dispatch_types || [];
Condition0%0/2
$c->dispatcher->dispatch_types || []
l!l
00
MC/DC0%0/1
$c->dispatcher->dispatch_types || []
$c->dispatcher->dispatch_types
Truth table0%0/2
A
A= $c->dispatcher->dispatch_types
Aresult
11
00
322 0 my $dp_ns
323 = 'Catalyst::Plugin::Server::XMLRPC::DispatchType::';
324  
325 $c->dispatcher->dispatch_types(
326 [ grep {
327 0 ▶ $_->isa($dp_ns . 'XMLRPCPath')
Condition0%0/3
$_->isa($dp_ns . 'XMLRPCPath') || $_->isa($dp_ns . 'XMLRPCRegex')
l!l&&r!l&&!r
000
MC/DC0%0/2
$_->isa($dp_ns . 'XMLRPCPath') || $_->isa($dp_ns . 'XMLRPCRegex')
$_->isa($dp_ns . 'XMLRPCPath') $_->isa($dp_ns . 'XMLRPCRegex')
Truth table0%0/3
A || B
A= $_->isa($dp_ns . 'XMLRPCPath')B= $_->isa($dp_ns . 'XMLRPCRegex')
ABresult
1X1
011
000
328 or
329 $_->isa($dp_ns . 'XMLRPCRegex')
330 } @$saved_dt
331 ]
332 );
333  
334 ### run the rest of the prepare actions, we should have
335 ### an action object now
336 0 $c->next::method( @_ );
337  
338 ### restore the saved dispatchtypes
339 0 $c->dispatcher->dispatch_types( $saved_dt );
340 }
341  
342 ### check if we have a c->action now
343 ### check if the NEW action isn't hte same as the
344 ### OLD action -- which mean no method was found
345 ### Not needed, don't have an action until we NEXT
346 0 ▶ if( (not $c->action) &&
Branch: 1 branch
BranchTrueFalseTotal
unless ($c->action or $c->server->xmlrpc->private_methods->{$c->req->xmlrpc->method}) 0 0 0
Condition0%0/3
$c->action or $c->server->xmlrpc->private_methods->{$c->req->xmlrpc->method}
l!l&&r!l&&!r
000
MC/DC0%0/2
$c->action or $c->server->xmlrpc->private_methods->{$c->req->xmlrpc->method}
$c->action $c->server->xmlrpc->private_methods->{$c->req->xmlrpc->method}
Truth table0%0/3
A or B
A= $c->actionB= $c->server->xmlrpc->private_methods->{$c->req->xmlrpc->method}
ABresult
1X1
011
000
347 !$c->server->xmlrpc->private_methods->{
348 $c->req->xmlrpc->method
349 }
350 ) {
351 0 $c->req->xmlrpc->_error(
352 $c, qq[Invalid XMLRPC request: No such method]
353 );
354 0 last PREPARE;
355 }
356 }
357  
358 ### XMLRPC parameters and argument processing, see the Request
359 ### class below for information why we can't do it there.
360 0 ▶ $c->req->parameters( $c->req->xmlrpc->params )
Branch: 1 branch
BranchTrueFalseTotal
if $c->server->xmlrpc->config->convert_params 0 0 0
361 if $c->server->xmlrpc->config->convert_params;
362  
363 0 $c->req->args($c->req->xmlrpc->args );
364  
365 ### we're no xmlrpc request, so just let others handle it
366 } else {
367 0 $c->next::method( @_ );
368 }
369 }
370  
371 ### before we dispatch, make sure no xmlrpc errors have happened already,
372 ### or an internal method has been called.
373 0 ▶ sub dispatch {
Subroutine dispatch: not called CC=5 SCAR=34.0
374 0 my $c = shift;
375  
376 0 ▶ if( $c->req->xmlrpc->is_xmlrpc_request and
Branch: 2 branches
BranchTrueFalseTotal
if ($c->req->xmlrpc->is_xmlrpc_request and scalar @{$c->error;}) { } 0 0 0
elsif ($c->req->xmlrpc->is_xmlrpc_request and $c->server->xmlrpc->private_methods->{$c->req->xmlrpc->method}) { } 0 0 0
Condition0%0/2
$c->req->xmlrpc->is_xmlrpc_request and scalar @{$c->error;}
!ll
00
MC/DC0%0/1
$c->req->xmlrpc->is_xmlrpc_request and scalar @{$c->error;}
$c->req->xmlrpc->is_xmlrpc_request
Truth table0%0/2
A
A= $c->req->xmlrpc->is_xmlrpc_request
Aresult
00
11
377 0 scalar( @{ $c->error } )
378 ) {
379 0 1;
380 ▶ } elsif (
Condition0%0/3
$c->req->xmlrpc->is_xmlrpc_request and $c->server->xmlrpc->private_methods->{$c->req->xmlrpc->method}
!ll&&!rl&&r
000
MC/DC0%0/2
$c->req->xmlrpc->is_xmlrpc_request and $c->server->xmlrpc->private_methods->{$c->req->xmlrpc->method}
$c->req->xmlrpc->is_xmlrpc_request $c->server->xmlrpc->private_methods->{$c->req->xmlrpc->method}
Truth table0%0/3
A and B
A= $c->req->xmlrpc->is_xmlrpc_requestB= $c->server->xmlrpc->private_methods->{$c->req->xmlrpc->method}
ABresult
0X0
100
111
381 $c->req->xmlrpc->is_xmlrpc_request and
382 $c->server->xmlrpc->private_methods->{$c->req->xmlrpc->method}
383 ) {
384 0 $c->req->xmlrpc->run_method($c);
385 } else {
386 0 $c->next::method( @_ );
387 }
388 }
389  
390 0 ▶ sub finalize {
Subroutine finalize: not called CC=11 SCAR=48.8
391 0 my $c = shift;
392  
393 0 ▶ if( $c->req->xmlrpc->is_xmlrpc_request ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->req->xmlrpc->is_xmlrpc_request) 0 0 0
394  
395 ### if we got an error anywhere, we'll return a fault
396 ### othwerise, the resultset will be returned
397 ### XXX TODO make error codes configurable ( done )
398 ### XXX TODO make messages customizable ( done )
399 0 my $res;
400 0 my $req_error = $c->req->xmlrpc->error;
401 0 ▶ if( scalar @{ $c->error } or $req_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$c->error;} or $req_error) { } 0 0 0
Condition0%0/3
scalar @{$c->error;} or $req_error
l!l&&r!l&&!r
000
MC/DC0%0/2
scalar @{$c->error;} or $req_error
scalar @{$c->error;} $req_error
Truth table0%0/3
A or B
A= scalar @{$c->error;}B= $req_error
ABresult
1X1
011
000
402 0 ▶ if ($c->server->xmlrpc->config->show_errors) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->server->xmlrpc->config->show_errors) { } 0 0 0
403 0 ▶ if ( $req_error && ref $req_error eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req_error and ref $req_error eq 'ARRAY') { } 0 0 0
Condition0%0/3
$req_error and ref $req_error eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
$req_error and ref $req_error eq "ARRAY"
$req_error ref $req_error eq "ARRAY"
Truth table0%0/3
A and B
A= $req_errorB= ref $req_error eq "ARRAY"
ABresult
0X0
100
111
404 0 $res = RPC::XML::fault->new( @{ $req_error } );
405 } else {
406 $res = RPC::XML::fault->new( -1,
407 0 join $/, @{ $c->error }
408 );
409 }
410 } else {
411 0 ▶ if ( $req_error && ref $req_error eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req_error and ref $req_error eq 'ARRAY') { } 0 0 0
Condition0%0/3
$req_error and ref $req_error eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
$req_error and ref $req_error eq "ARRAY"
$req_error ref $req_error eq "ARRAY"
Truth table0%0/3
A and B
A= $req_errorB= ref $req_error eq "ARRAY"
ABresult
0X0
100
111
412 0 $res = RPC::XML::fault->new( @{ $req_error } );
413 } else {
414 $c->log->debug("XMLRPC 500 Errors:\n" .
415 0 join("\n", @{ $c->error })
416 );
417 0 $res = RPC::XML::fault->new(
418 500,
419 'Internal Server Error'
420 );
421 }
422 }
423 } else {
424 0 ▶ if( exists $c->stash->{xmlrpc} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $c->stash->{'xmlrpc'}) { } 0 0 0
425 0 $res = $c->stash->{xmlrpc};
426 } else {
427 0 $res = $c->stash;
428 }
429 }
430  
431 0 $c->res->body(
432 $c->req->xmlrpc->_serialize_xmlrpc( $c, $res )
433 );
434  
435 ### make sure to clear the error, so catalyst doesn't try
436 ### to deal with it
437 0 $c->error( 0 );
438 }
439  
440 0 ▶ $c->log->debug( 'FINALIZE ' . Dumper ( $c, \@_ ) ) if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Debug 0 0 0
441  
442 ### always call finalize at the end, so Catalyst's final handler
443 ### gets called as well
444 0 $c->next::method( @_ );
445 }
446 }
447  
448 ### The server implementation
449 { package Catalyst::Plugin::Server::XMLRPC::Backend;
450  
451 7 ▶ use base qw/Class::Accessor::Fast/;
Subroutine BEGIN: called CC=1 SCAR=0.0
452 use Data::Dumper;
453 use Scalar::Util 'reftype';
454  
455 __PACKAGE__->mk_accessors( qw/
456 dispatcher
457 private_methods
458 c
459 config
460 /
461 );
462  
463 sub new {
464 my $class = shift;
465 my $c = shift;
466 my $self = $class->next::method( @_ );
467  
468 $self->c($c);
469 $self->config( Catalyst::Plugin::Server::XMLRPC::Config->new( $c ) );
470 $self->private_methods({});
471 $self->dispatcher({});
472  
473 ### Internal function
474 $self->add_private_method(
475 'system.listMethods' => sub {
476 my ($c_ob, @args) = @_;
477 return [ keys %{
478 $c_ob->server->xmlrpc->list_methods;
479 } ];
480 }
481 );
482  
483 return $self;
484 }
485  
486 sub add_private_method {
487 my ($self, $name, $sub) = @_;
488  
489 return unless ($name && (reftype($sub) eq 'CODE'));
490 $self->private_methods->{$name} = $sub;
491 return 1;
492 }
493  
494 sub list_methods {
495 my ($self) = @_;
496 return $self->dispatcher->{Path}->methods($self->c);
497 }
498 }
499  
500 ### the config implementation ###
501 { package Catalyst::Plugin::Server::XMLRPC::Config;
502 use base 'Class::Accessor::Fast';
503  
504 ### XXX change me to an ENTRYPOINT!
505 my $DefaultPath = qr!^(/?)rpc(/|$)!i;
506 my $DefaultAttr = 'XMLRPC';
507 my $DefaultPrefix = '';
508 my $DefaultSep = '.';
509 my $DefaultShowErrors = 0;
510  
511 ### XXX add: stash_fields (to encode) stash_exclude_fields (grep -v)
512  
513 __PACKAGE__->mk_accessors(
514 qw/ path prefix separator attribute convert_params
515 show_errors xml_encoding
516 /
517 );
518  
519 ### return the cached version where possible
520 my $Obj;
521 sub new {
522 return $Obj if $Obj;
523  
524 my $class = shift;
525 my $c = shift;
526 my $self = $class->next::method;
527  
528 $self->prefix( $c->config->{xmlrpc}->{prefix} || $DefaultPrefix);
529 $self->separator($c->config->{xmlrpc}->{separator} || $DefaultSep);
530 $self->path( $c->config->{xmlrpc}->{path} || $DefaultPath);
531 $self->show_errors( $c->config->{xmlrpc}->{show_errors}
532 || $DefaultShowErrors );
533 $self->xml_encoding( $c->config->{xmlrpc}->{xml_encoding} )
534 if $c->config->{xmlrpc}->{xml_encoding};
535 $self->attribute($DefaultAttr);
536 $self->convert_params( 1 );
537  
538 ### cache it
539 return $Obj = $self;
540 }
541 }
542  
543 ### the server class implementation ###
544 { package Catalyst::Plugin::Server::XMLRPC::Request;
545  
546 use strict;
547 use warnings;
548  
549 use RPC::XML;
550 use RPC::XML::Parser;
551 use Scalar::Util 'reftype';
552 use Clone::Fast qw/clone/;
553  
554 use Data::Dumper;
555 use Text::SimpleTable;
556  
557 use base 'Class::Data::Inheritable';
558 use base 'Class::Accessor::Fast';
559  
560 __PACKAGE__->mk_accessors( qw[ forward_path args method body result
561 is_xmlrpc_request params
562 result_as_string internal_methods error
563 ] );
564  
565 __PACKAGE__->mk_classdata( qw[_xmlrpc_parser]);
566 __PACKAGE__->_xmlrpc_parser( RPC::XML::Parser->new );
567  
568 *parameters = *params;
569  
570 sub run_method {
571 my ($self, $c) = @_;
572  
573 $c->stash->{xmlrpc} =
574 &{$c->server->xmlrpc->private_methods->{$self->method}}($c, @{ $c->req->args });
575 }
576  
577 sub _deserialize_xml {
578 my ($self, $c) = @_;
579  
580 ### the parser will die on failure, make sure we catch it
581 my $content; my $req;
582 eval {
583 ## Make sure we do not read from empty filehandle,
584 ## by sending empty string
585 $content = do { local $/; my $b = $c->req->body; $b ? <$b> : ''};
586 $req = $self->_xmlrpc_parser->parse( $content );
587  
588 ### RPC::XML::Parser *returns* the error string on error
589 ### OR an object... *sigh*
590 die $req unless ref $req;
591  
592 ### Because we will die when request is not valid XMLRPC,
593 ### we simply test it. XXX TODO This results in a malformed
594 ### xml detected error, maybe we should catch it.
595 $req->name;
596 $req->args;
597 };
598  
599 ### parsing the request went fine
600 if ( not $@ and defined $req->name ) {
601  
602 $self->body( $content ); # original xml message
603 $self->method( $req->name ); # name of the method
604  
605 ### allow the args to be encoded as a HASH when requested
606 ### xmlrpc only knows a top level 'list', and we can not tell
607 ### if that is meant to be a hash or not
608 ### make sure to store args as an ARRAY REF! to be compatible
609 ### with catalyst
610 my @args = map { $_->value } @{ $req->args };
611 $self->args( \@args ); # parsed arguments
612  
613 ### HEURISTIC! IF @args == 1 AND it's a HASHREF,
614 ### then we can assume it's key => value pairs in there
615 ### and we will map them to $c->req->params
616 $self->params(
617 (@args == 1 && (reftype($args[0]) eq 'HASH'))
618 ? $args[0]
619 : {}
620 );
621 ### build the relevant namespace, action and path
622 { ### construct the forward path -- this allows catalyst to
623 ### do the hard work of dispatching for us
624 my $prefix = $c->server->xmlrpc->config->prefix;
625 my ($sep) = map { qr/$_/ }
626 map { quotemeta $_ }
627 $c->server->xmlrpc->config->separator;
628  
629 ### error checks here
630 if( $prefix =~ m|^/| ) {
631 $c->log->debug( __PACKAGE__ . ": Your prefix starts with".
632 " a / -- This is not recommended"
633 ) if $c->debug;
634 }
635  
636 unless( ref($sep) eq 'Regexp' ) {
637 $c->log->debug( __PACKAGE__ . ": Your separator is not a ".
638 "Regexp object -- This is not recommended"
639 ) if $c->debug;
640 }
641  
642 ### foo.bar => $prefix/foo/bar
643 ### DO NOT add a leading slash! uri.pm gets very upset
644 my @parts = split( $sep, $self->method );
645 my $fwd_path = join '/',
646 grep { defined && length } $prefix, @parts;
647  
648  
649 ### Complete our object-instance
650 $self->forward_path( $fwd_path );
651  
652 ### Notify system of called rpc method and arguments
653 $c->log->debug('XML-RPC: Method called: ' . $self->method)
654 if $c->debug;
655 if ($c->server->xmlrpc->config->convert_params &&
656 $self->params
657 ) {
658 my $params = Text::SimpleTable->new( [ 36, 'Key' ], [ 37, 'Value' ] );
659 foreach my $key (sort keys %{$self->params}) {
660 my $value = $self->params->{$key};
661 $value = ref($value) || $value;
662 $params->row($key, $value);
663 }
664 $c->log->debug("XML-RPC: Parameters:\n" . $params->draw)
665 if ($c->debug && %{$self->params});
666 }
667 }
668  
669 ### an error in parsing the request
670 } elsif ( $@ ) {
671 $self->_error( $c, qq[Invalid XMLRPC request "$@"] );
672 return;
673  
674 ### something is wrong, but who knows what...
675 } else {
676 $self->_error( $c, qq[Invalid XMLRPC request: Unknown error] );
677 return;
678 }
679  
680 return $self;
681 }
682  
683 ### alias arguments to args
684 *arguments = *args;
685  
686 ### Serializes the response to $c->res->body
687 sub _serialize_xmlrpc {
688 my ( $self, $c, $status ) = @_;
689  
690 local $RPC::XML::ENCODING = $c->server->xmlrpc->config->xml_encoding
691 if $c->server->xmlrpc->config->xml_encoding;
692
693 local $Clone::Fast::BREAK_REFS = 1;
694  
695 my $res = RPC::XML::response->new(clone($status));
696 $c->res->content_type('text/xml');
697  
698 return $self->result_as_string( $res->as_string );
699 }
700  
701 ### record errors in the error and debug log -- just for convenience
702 sub _error {
703 my($self, $c, $msg) = @_;
704  
705 $c->log->debug( $msg ) if $c->debug;
706 $c->error( $msg );
707 }
708 }
709  
710  
711 1;
712  
713 __END__
↑ Summary blib/lib/Catalyst/Plugin/Server/XMLRPC/DispatchType/XMLRPCPath.pm »