| 1 | # $Id: XMLRPC.pm,v 1.4 2003/03/20 23:26:02 mah Exp $ | ||||||||||||||||||||||||||||
| 2 | # License and documentation are after __END__. | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | package POE::Component::Server::XMLRPC; | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | $VERSION = '0.05'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use POE; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use POE::Component::Server::HTTP; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use XMLRPC::Lite; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 1 | my %public_interfaces; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 20 | 0 | my $type = shift; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 0 | ▶ | croak "Must specify an even number of parameters to $type\->new()" if @_ % 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 0 | my %params = @_; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 0 | my $alias = delete $params{alias}; | |||||||||||||||||||||||||||
| 26 | 0 | ▶ | croak "Must specify an alias in $type\->new()" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $alias and length $alias
MC/DC0%0/2
defined $alias and length $alias
defined $alias
length $alias
Truth table0%0/3
A and B
A= defined $aliasB= length $alias
| |||||||||||||||||||||||||||||
| 27 | unless defined $alias and length $alias; | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 0 | my $interface = delete $params{interface}; | |||||||||||||||||||||||||||
| 30 | 0 | ▶ | croak "$type\->new() currently does not support the interface parameter" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | if defined $interface; | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 0 | my $port = delete $params{port}; | |||||||||||||||||||||||||||
| 34 | 0 | ▶ | $port = 80 unless $port; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | POE::Session->create | ||||||||||||||||||||||||||||
| 37 | ( inline_states => | ||||||||||||||||||||||||||||
| 38 | 0 | ▶ | { _start => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 39 | 0 | $_[KERNEL]->alias_set($alias); | |||||||||||||||||||||||||||
| 40 | }, | ||||||||||||||||||||||||||||
| 41 | 0 | ▶ | publish => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 42 | 0 | my ($alias, $event) = @_[ARG0, ARG1]; | |||||||||||||||||||||||||||
| 43 | 0 | $public_interfaces{$alias}{$event} = 1; | |||||||||||||||||||||||||||
| 44 | }, | ||||||||||||||||||||||||||||
| 45 | 0 | ▶ | rescind => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 46 | 0 | my ($alias, $event) = @_[ARG0, ARG1]; | |||||||||||||||||||||||||||
| 47 | 0 | delete $public_interfaces{$alias}{$event}; | |||||||||||||||||||||||||||
| 48 | }, | ||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | 0 | ); | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 0 | POE::Component::Server::HTTP->new | |||||||||||||||||||||||||||
| 53 | ( Port => $port, | ||||||||||||||||||||||||||||
| 54 | Headers => | ||||||||||||||||||||||||||||
| 55 | { Server => "POE::Component::Server::XMLRPC/$VERSION", | ||||||||||||||||||||||||||||
| 56 | }, | ||||||||||||||||||||||||||||
| 57 | ContentHandler => { "/" => \&web_handler }, | ||||||||||||||||||||||||||||
| 58 | ); | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | undef; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | ### Handle web requests by farming them out to other sessions. | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | ▶ | sub web_handler { | ||||||||||||||||||||||||||
|
Subroutine
web_handler: not called CC=10 SCAR=47.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 66 | 0 | my ($request, $response) = @_; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | # Parse useful things from the request. | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | my $query_string = $request->uri->query(); | |||||||||||||||||||||||||||
| 71 | 0 | ▶ | unless (defined($query_string) and $query_string =~ /\bsession=(.+ $ )/x) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $query_string and $query_string =~ /\bsession=(.+ $ )/x
MC/DC0%0/2
defined $query_string and $query_string =~ /\bsession=(.+ $ )/x
defined $query_string
$query_string =~ /\bsession=(.+ $ )/x
Truth table0%0/3
A and B
A= defined $query_stringB= $query_string =~ /\bsession=(.+ $ )/x
| |||||||||||||||||||||||||||||
| 72 | 0 | $response->code(400); | |||||||||||||||||||||||||||
| 73 | 0 | return RC_OK; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | 0 | my $session = $1; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | my $http_method = $request->method(); | |||||||||||||||||||||||||||
| 78 | 0 | my $request_content_type = $request->header('Content-Type'); | |||||||||||||||||||||||||||
| 79 | 0 | my $request_content_length = $request->header('Content-Length'); | |||||||||||||||||||||||||||
| 80 | 0 | my $debug_request = $request->header('DebugRequest'); | |||||||||||||||||||||||||||
| 81 | 0 | my $request_content = $request->content(); | |||||||||||||||||||||||||||
| 82 | 0 | my $data = XMLRPC::Deserializer | |||||||||||||||||||||||||||
| 83 | ->deserialize($request_content); | ||||||||||||||||||||||||||||
| 84 | 0 | my $method_name = $data->valueof("methodName"); | |||||||||||||||||||||||||||
| 85 | 0 | my $args = $data->valueof("params"); | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | ▶ | unless ($request_content_type =~ /^text\/xml(;.*)?$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 0 | _request_failed( $response, | |||||||||||||||||||||||||||
| 89 | 403, | ||||||||||||||||||||||||||||
| 90 | "Bad Request", | ||||||||||||||||||||||||||||
| 91 | "Content-Type must be text/xml.", | ||||||||||||||||||||||||||||
| 92 | ); | ||||||||||||||||||||||||||||
| 93 | 0 | return RC_OK; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 0 | ▶ | unless (defined($method_name) and length($method_name)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $method_name and length $method_name
MC/DC0%0/2
defined $method_name and length $method_name
defined $method_name
length $method_name
Truth table0%0/3
A and B
A= defined $method_nameB= length $method_name
| |||||||||||||||||||||||||||||
| 97 | 0 | _request_failed( $response, | |||||||||||||||||||||||||||
| 98 | 403, | ||||||||||||||||||||||||||||
| 99 | "Bad Request", | ||||||||||||||||||||||||||||
| 100 | "methodName is required.", | ||||||||||||||||||||||||||||
| 101 | ); | ||||||||||||||||||||||||||||
| 102 | 0 | return RC_OK; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | ▶ | unless ($method_name =~ /^(\S+)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 0 | _request_failed( $response, | |||||||||||||||||||||||||||
| 107 | 403, | ||||||||||||||||||||||||||||
| 108 | "Bad Request", | ||||||||||||||||||||||||||||
| 109 | "Unrecognized methodName: $method_name", | ||||||||||||||||||||||||||||
| 110 | ); | ||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | ▶ | unless (exists $public_interfaces{$session}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 0 | _request_failed( $response, | |||||||||||||||||||||||||||
| 115 | 500, | ||||||||||||||||||||||||||||
| 116 | "Bad Request", | ||||||||||||||||||||||||||||
| 117 | "Unknown session: $session", | ||||||||||||||||||||||||||||
| 118 | ); | ||||||||||||||||||||||||||||
| 119 | 0 | return RC_OK; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 0 | ▶ | unless (exists $public_interfaces{$session}{$method_name}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 0 | _request_failed( $response, | |||||||||||||||||||||||||||
| 124 | 500, | ||||||||||||||||||||||||||||
| 125 | "Bad Request", | ||||||||||||||||||||||||||||
| 126 | "Unknown method: $method_name", | ||||||||||||||||||||||||||||
| 127 | ); | ||||||||||||||||||||||||||||
| 128 | 0 | return RC_OK; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 0 | eval { | |||||||||||||||||||||||||||
| 132 | 0 | XMLRPCTransaction->start($response, $session, $method_name, $args); | |||||||||||||||||||||||||||
| 133 | }; | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 0 | _request_failed( $response, | |||||||||||||||||||||||||||
| 137 | 500, | ||||||||||||||||||||||||||||
| 138 | "Application Faulted", | ||||||||||||||||||||||||||||
| 139 | "An exception fired while processing the request: $@", | ||||||||||||||||||||||||||||
| 140 | ); | ||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 0 | return RC_WAIT; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 0 | ▶ | sub _request_failed { | ||||||||||||||||||||||||||
|
Subroutine
_request_failed: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 147 | 0 | my ($response, $fault_code, $fault_string, $result_description) = @_; | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 0 | my $response_content = qq{<?xml version="1.0"?> | |||||||||||||||||||||||||||
| 150 | <methodResponse> | ||||||||||||||||||||||||||||
| 151 | <fault><value><struct> | ||||||||||||||||||||||||||||
| 152 | <member><name>faultCode</name><value><int>$fault_code</int></value></member> | ||||||||||||||||||||||||||||
| 153 | <member><name>faultString</name><value><string>$fault_string</string></value> | ||||||||||||||||||||||||||||
| 154 | </member> | ||||||||||||||||||||||||||||
| 155 | </struct></value></fault> | ||||||||||||||||||||||||||||
| 156 | </methodResponse>}; | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 0 | $response->code(200); | |||||||||||||||||||||||||||
| 159 | 0 | $response->header("Content-Type", "text/xml"); | |||||||||||||||||||||||||||
| 160 | 0 | $response->header("Content-Length", length($response_content)); | |||||||||||||||||||||||||||
| 161 | 0 | $response->content($response_content); | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | package XMLRPCTransaction; | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | sub TR_RESPONSE () { 0 } | ||||||||||||||||||||||||||||
| 167 | sub TR_SESSION () { 1 } | ||||||||||||||||||||||||||||
| 168 | sub TR_EVENT () { 2 } | ||||||||||||||||||||||||||||
| 169 | sub TR_ARGS () { 3 } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 0 | ▶ | sub start { | ||||||||||||||||||||||||||
|
Subroutine
start: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 172 | 0 | my ($type, $response, $session, $event, $args) = @_; | |||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 0 | my $self = bless | |||||||||||||||||||||||||||
| 175 | [ $response, | ||||||||||||||||||||||||||||
| 176 | $session, | ||||||||||||||||||||||||||||
| 177 | $event, | ||||||||||||||||||||||||||||
| 178 | $args, | ||||||||||||||||||||||||||||
| 179 | ], $type; | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 0 | $POE::Kernel::poe_kernel->post($session, $event, $self); | |||||||||||||||||||||||||||
| 182 | 0 | undef; | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 0 | ▶ | sub params { | ||||||||||||||||||||||||||
|
Subroutine
params: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 186 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 187 | 0 | return $self->[TR_ARGS]; | |||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 0 | ▶ | sub return { | ||||||||||||||||||||||||||
|
Subroutine
return: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 191 | 0 | my ($self, $retval) = @_; | |||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 0 | my $content = XMLRPC::Serializer->envelope(response => 'toMethod', $retval); | |||||||||||||||||||||||||||
| 194 | 0 | my $response = $self->[TR_RESPONSE]; | |||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 0 | $response->code(200); | |||||||||||||||||||||||||||
| 197 | 0 | $response->header("Content-Type", "text/xml"); | |||||||||||||||||||||||||||
| 198 | 0 | $response->header("Content-Length", length($content)); | |||||||||||||||||||||||||||
| 199 | 0 | $response->content($content); | |||||||||||||||||||||||||||
| 200 | 0 | $response->continue(); | |||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 1 | 1; | |||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | __END__ | ||||||||||||||||||||||||||||