blib/lib/POE/Component/Server/SimpleXMLRPC.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package POE::Component::Server::SimpleXMLRPC;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 $VERSION = '0.02';
8  
9 1 ▶use POE qw(Component::Server::SimpleHTTP Wheel::Run);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 0 ▶use Frontier::RPC2;
Subroutine BEGIN: called CC=1 SCAR=2.6
11 use Encode;
12 use POSIX;
13  
14 sub new {
15 # Get the OOP's type
16 my $type = shift;
17 # Sanity checking
18 if ( @_ & 1 ) {
19 die( 'POE::Component::Server::SimpleXMLRPC->new needs even number of options' );
20 }
21 # The options hash
22 my %opt = @_;
23 my ($sesName, $rpcMap, $alias, $func_timeout, $logger);
24 if ( exists $opt{'ALIAS'} and defined $opt{'ALIAS'} and length( $opt{'ALIAS'} ) ) {
25 $alias = $opt{'ALIAS'};
26 } else {
27 # set default
28 $alias = 'HTTPD';
29 $opt{'ALIAS'} = 'HTTPD';
30 }
31 if ( exists $opt{'RECV_SESSION'} and defined $opt{'RECV_SESSION'} and length( $opt{'RECV_SESSION'} ) ) {
32 $sesName = $opt{'RECV_SESSION'};
33 delete $opt{'RECV_SESSION'};
34 } else {
35 # set default
36 $sesName = 'HTTP_GET';
37 }
38 if ( exists $opt{'FUNC_TIMEOUT'} and defined $opt{'FUNC_TIMEOUT'} and length( $opt{'FUNC_TIMEOUT'} ) ) {
39 $func_timeout = $opt{'FUNC_TIMEOUT'};
40 delete $opt{'FUNC_TIMEOUT'};
41 } else {
42 # set default
43 $func_timeout = 120;
44 }
45 if ( exists $opt{'RPC_MAP'} and defined $opt{'RPC_MAP'} and length( $opt{'RPC_MAP'} ) ) {
46 $rpcMap = $opt{'RPC_MAP'};
47 delete $opt{'RPC_MAP'};
48 } else {
49 die( 'RPC_MAP is required to create a new POE::Component::Server::SimpleXMLRPC instance!' );
50 }
51 if (exists $opt{'LOGGER'} and defined $opt{'LOGGER'} and ref $opt{'LOGGER'} eq 'HASH') {
52 if (defined $opt{LOGGER}->{SESSION} and defined $opt{LOGGER}->{EVENT}) {
53 $logger = delete $opt{LOGGER};
54 }
55 else {
56 die ('Malformed LOGGER option');
57 }
58 }
59 # Adding and checking our options:
60 # Replace HANDLERS
61 $opt{HANDLERS} = [ {DIR => '^.*$',
62 SESSION => $sesName,
63 EVENT => 'GOT_default'}];
64  
65 # Start server
66 POE::Component::Server::SimpleHTTP->new(%opt) or die 'Unable to create the HTTP Server';
67  
68 my $states;
69 $states->{GOT_default} = \&GOT_default;
70 $states->{_start} = sub { $poe_kernel->alias_set( $sesName ); $poe_kernel->sig(CHLD => "sigchild");};
71 $states->{SHUTDOWN} = sub {
72 my ( $kernel, $heap ) = @_[ KERNEL, HEAP ];
73 foreach my $wheel ( keys %{ $heap->{ wheels } } ) {
74 $kernel->call( $_[SESSION], 'wheel_check_' . $wheel );
75 $kernel->alarm_remove( $heap->{wheels}->{ $wheel }->{timer} );
76 }
77 delete $heap->{ 'wheels' };
78 $kernel->alias_remove( $sesName );
79 $kernel->call( $heap->{HTTPD}, 'SHUTDOWN' );
80 $kernel->yield( '_stop' );
81 };
82 $states->{remap} = sub { $_[HEAP]->{METHODS} = $_[ARG0]; };
83 $states->{sigchild} = sub {
84 #if ($_[HEAP]->{LOGGER}) {
85 # $_[KERNEL]->post($_[HEAP]->{LOGGER}->{SESSION}, $_[HEAP]->{LOGGER}->{EVENT}, 'Sigchild got with data: '.Dumper($_[ARG2]));
86 #}
87 return $poe_kernel->sig_handled(); # 1;
88 };
89 $states->{wheel_done} = sub {
90 my ($kernel, $heap, $wheel_id) = @_[KERNEL, HEAP, ARG0, ARG1];
91 my $content = eval ('my $VAR1; '.$heap->{wheels}->{$wheel_id}->{content});
92 #$content = $content || 'void content';
93 $content = $heap->{coder}->encode_response($content);
94 $heap->{wheels}->{$wheel_id}->{response}->code(200);
95 if (Encode::is_utf8($content)) {
96 $content = encode('utf8', $content);
97 }
98 if ($_[HEAP]->{LOGGER}) {
99 $kernel->call($heap->{LOGGER}->{SESSION}, $heap->{LOGGER}->{EVENT}, Dumper($content));
100 }
101 $heap->{wheels}->{$wheel_id}->{response}->content( $content );
102 $kernel->post( $heap->{HTTPD}, 'DONE', $heap->{wheels}->{$wheel_id}->{response} );
103 $kernel->alarm_remove( $heap->{wheels}->{$wheel_id}->{timer} );
104 delete $heap->{wheels}->{$wheel_id};
105 };
106 $states->{wheel_out} = sub {
107 my ($kernel, $heap, $content, $wheel_id) = @_[KERNEL, HEAP, ARG0, ARG1];
108 $heap->{wheels}->{$wheel_id}->{content} .= $content;
109 };
110 $states->{wheel_err} = sub {
111 my ($kernel, $heap, $content, $wheel_id) = @_[KERNEL, HEAP, ARG0, ARG1];
112 #$heap->{wheels}->{$wheel_id}->{content} .= $content;
113 if ($heap->{LOGGER}) {
114 $kernel->post($heap->{LOGGER}->{SESSION}, $heap->{LOGGER}->{EVENT}, 'stderr from function '.
115 $heap->{wheels}->{$wheel_id}->{procedure}.': '.$content);
116 }
117 };
118 $states->{_default} = sub {
119 my ($kernel, $heap, $event, $wheel_id) = @_[KERNEL, HEAP, ARG0, ARG1];
120 unless ($event =~ /^wheel_check_(\d+)$/) {
121 if ($heap->{LOGGER}) {
122 $kernel->post($heap->{LOGGER}->{SESSION}, $heap->{LOGGER}->{EVENT},'unhandled event '.$event);
123 }
124 return $poe_kernel->sig_handled();
125 }
126 $wheel_id = $1;
127 if (exists $heap->{wheels}->{$wheel_id}) {
128 $heap->{wheels}->{$wheel_id}->{wheel}->kill(9);
129 my $content = $heap->{coder}->encode_fault('500', "Request timed out");
130 $heap->{wheels}->{$wheel_id}->{response}->code( 200 );
131 if (!Encode::is_utf8($content)) {
132 $content = decode('utf8', $content);
133 }
134 $heap->{wheels}->{$wheel_id}->{response}->content( $content );
135 $_[KERNEL]->post( $heap->{HTTPD}, 'DONE', $heap->{wheels}->{$wheel_id}->{response} );
136 delete $heap->{wheels}->{$wheel_id};
137 }
138 return $poe_kernel->sig_handled();
139 };
140 # Start worker session
141 POE::Session->create(
142 inline_states => $states,
143 heap => {
144 METHODS => $rpcMap,
145 FUNC_TIMEOUT => $func_timeout,
146 coder => new Frontier::RPC2('encoding' => 'UTF-8'),
147 wheels => {},
148 LOGGER => $logger,
149 HTTPD => $alias,
150 },
151 );
152 }
153  
154 sub GOT_default {
155 my( $kernel, $heap, $request, $response, $dirmatch ) = @_[ KERNEL, HEAP, ARG0 .. ARG2 ];
156 my $params = _getParamsFromRequest($request, $heap->{coder});
157 my ($code, $content);
158 $code = 200;
159 if (ref $params->{__postdata} eq 'HASH' && $params->{__postdata}->{method_name}) {
160 # Try to find appropriate function to handle method
161 my $found = 0;
162 foreach my $handler ( keys %{ $_[HEAP]->{'METHODS'} } ) {
163 if ( $params->{__postdata}->{method_name} eq $handler ) {
164 $found = 1;
165 my $func = $_[HEAP]->{'METHODS'}->{$handler};
166 # fork wheel::run
167 my $wheel = POE::Wheel::Run->new(
168 Program => sub { use Data::Dumper;
169 $Data::Dumper::Purity = 1;
170 $Data::Dumper::Terse = 1;
171 print Dumper(&$func($params->{__postdata}->{value}))."\n"; return 1;},
172 StdoutEvent => 'wheel_out',
173 StderrEvent => 'wheel_err',
174 CloseEvent => 'wheel_done',
175 CloseOnCall => 1,
176 StderrFilter => POE::Filter::Stream->new(),
177 );
178 $heap->{wheels}->{$wheel->ID}->{wheel} = $wheel;
179 $heap->{wheels}->{$wheel->ID}->{response} = $response;
180 $heap->{wheels}->{$wheel->ID}->{content} = '';
181 $heap->{wheels}->{$wheel->ID}->{procedure} = $handler;
182 # set timeout
183 $heap->{wheels}->{$wheel->ID}->{timer} =
184 $kernel->delay_set('wheel_check_'.$wheel->ID => $heap->{FUNC_TIMEOUT}, $wheel->ID);
185 # and return
186 return 1;
187 }
188 }
189 if ($found == 0) {
190 $content = $heap->{coder}->encode_fault('404', 'No such method');
191  
192 }
193 }
194 else {
195 $code = 500;
196 $content = 'Malformed request: no method found';
197 }
198 $response->code( $code );
199 if (!Encode::is_utf8($content)) {
200 $content = decode('utf8', $content);
201 }
202 $response->content( $content );
203 $_[KERNEL]->post( $heap->{HTTPD}, 'DONE', $response );
204  
205 }
206  
207 sub _getParamsFromRequest {
208 my ($req, $coder) = @_;
209 my ($params);
210 $params->{__postdata} = $req->decoded_content(charset => 'UTF-8');
211 $params->{__postdata} ||= decode('utf8', $req->content());
212 if ($params->{__postdata} =~ /^<\?xml/) {
213 $params->{__postdata} = $coder->decode($params->{__postdata});
214 }
215 return $params;
216 }
217  
218 1;
219  
220 __END__
↑ Summary