lib/SOAP/Clean/CGI.pm untested

filter: Statementstmt 0.0% 0 / 255 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 74 MC/DCmcdc 0.0% 0 / 48 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 458 CC 29 SCAR 37.1
CC
29
Cov
0%
handle_post
110.0
handle_get
42.0
BEGIN
30.0
CRAP 40.8
1 # Copyright (c) 2003, Cornell University
2 # See the file COPYING for the status of this software
3  
4 ########################################################################
5 # CGI Server
6 ########################################################################
7  
8 # Determine invocation method
9 # Extract method name and arguments
10 # call appropriate method
11  
12 package SOAP::Clean::CGI;
13  
14 use warnings;
15 use strict;
16  
17 use Getopt::Long;
18 use File::Basename;
19  
20 use SOAP::Clean::Misc;
21 use SOAP::Clean::XML;
22 use SOAP::Clean::Internal;
23 use SOAP::Clean::Server;
24  
25 use CGI qw/:standard/;
26  
27 # Inheritance
28 our @ISA = qw(SOAP::Clean::Server::Base);
29  
30 BEGIN {
31  
32 # if $REQUEST_METHOD isn't set, then assume that the script was
33 # called interactively. Parse the command line and set the
34 # environment variables accordingly.
35 if ( !defined($ENV{REQUEST_METHOD}) ) {
36  
37 $ENV{SERVER_PROTOCOL}='HTTP/1.1';
38 $ENV{HTTP_HOST}='localhost.localdomain:80';
39 $ENV{REQUEST_METHOD} = 'GET';
40 $ENV{REQUEST_URI} = $0;
41 $ENV{VERBOSE_SERVER} = 0;
42  
43 my $help = 0;
44 my $verbose = 0;
45 my $stat =
46 GetOptions('help' => \$help,
47 'get|G' => sub { $ENV{REQUEST_METHOD} = 'GET' },
48 'post|P' => sub { $ENV{REQUEST_METHOD} = 'POST' },
49 'type=s' => sub { $ENV{CONTENT_TYPE} = $_[1] },
50 'soap_action:s' => sub {
51 if (defined($ENV{HTTP_SOAPACTION})) {
52 $ENV{HTTP_SOAPACTION} = $_[1];
53 } else {
54 $ENV{HTTP_SOAPACTION} = "urn:something/bogus";
55 }
56 },
57 'query=s' => sub { $ENV{QUERY_STRING} = $_[1] },
58 'verbose+' => \$ENV{VERBOSE_SERVER},
59 );
60  
61 if ( !$stat || $help ) {
62 print "Usage: ...\n";
63 exit($stat == 0);
64 }
65  
66 if ( defined($ENV{HTTP_SOAPACTION}) && !defined($ENV{CONTENT_TYPE}) ) {
67 $ENV{CONTENT_TYPE} = 'text/xml';
68 }
69  
70 }
71 }
72  
73 ####################################################################
74 # Constructor and getter/setter methods
75 ####################################################################
76  
77 ########################################################################
78  
79 # How to die gracefully within SOAP.
80 sub soap_die_handler {
81 my ($string) = @_;
82  
83 my $d =
84 document
85 (element("SOAP-ENV:Envelope",
86 namespace("SOAP-ENV",$SOAP_ENV),
87 namespace("SOAP-ENC",$SOAP_ENC),
88 namespace("xsi",$xsi),
89 namespace("xsd",$xsd),
90 namespace("wsse",$wsse),
91 element("SOAP-ENV:Body",
92 element("SOAP-ENV:Fault",
93 element("faultcode",text("SOAP-ENV:Server")),
94 element("faultstring",text($string))
95 ))
96 ));
97  
98 print header(-status=>'500 Internal Server Error',
99 -type=>'text/xml',
100 -charset=>'utf-8');
101 xml_to_fh($d,\*STDOUT);
102  
103 exit 0;
104 }
105  
106  
107 sub html_die_handler {
108 my ($string) = @_;
109  
110 print h2("Server Error!"),p;
111 print pre($string),p;
112 exit 0;
113 }
114  
115  
116 ####################################################################
117 # Go! Figure out the environment in which we are running. The options
118 # are,
119 #
120 # - SOAP request
121 # - POST
122 # - GET
123 # - GET?wsdl
124 # - run from the command line for debugging
125 ####################################################################
126  
127 sub go {
128 my ($self) = @_;
129  
130 $self->_print(1,"Go!\n");
131  
132 $self->_ensure_interface();
133  
134 defined($self->{urn}) || assert(0,"Server URN not set.");
135 defined($self->{name}) || assert(0,"Server name not set.");
136  
137 my $request_method = $ENV{REQUEST_METHOD} || 0;
138 my $soapaction = $ENV{HTTP_SOAPACTION} || 0;
139 my $content_type = $ENV{CONTENT_TYPE} || 0;
140 my $query_string = $ENV{QUERY_STRING} || 0;
141  
142  
143 my $result;
144 # Determine how the request should be handled.
145 if ( $soapaction || $content_type =~ 'application/soap\+xml' ) {
146 # if the SOAPAction: header appears or the Content-type is
147 # application/soap+xml, then it process it as a soap request, not
148 # matter what the request method was.
149 $result = $self->handle_soap($content_type =~ 'text/xml');
150 } else {
151 # use the request method to decide how to handle it.
152 if ( $request_method eq "POST" ) {
153 # POST method
154 $result = $self->handle_post();
155 } elsif ( $request_method eq "GET" ) {
156 # GET method. Use the query string to decide how to handle.
157 if ( lc($query_string) eq "wsdl" ) {
158 $result = $self->handle_wsdl();
159 } else {
160 $result = $self->handle_get();
161 }
162 } else {
163 # No request method? The script must be being called interactively
164 # for debugging purposes. Handle it as SOAP.
165 $result = $self->handle_soap(0);
166 }
167 }
168  
169 return $result;
170  
171 }
172  
173 ########################################################################
174  
175 sub handle_soap {
176 my ($self,$content_type_is_text_xml) = @_;
177  
178 # Set the handler for die calls.
179 $SIG{__DIE__} = \&soap_die_handler;
180  
181 my $server_urn = $self->{urn};
182 defined($server_urn) || assert(0,"No server URN");
183 my $server_name = $self->{name};
184 defined($server_name) || assert(0,"No server name");
185  
186 # Read the request from STDIN
187 my $request = xml_from_fh(\*STDIN);
188 defined($request) || assert(0,"Can't parse request?!?");
189  
190 # Take a SOAP request. Parse it and extract the method name and
191 # arguments. Call the appropriate function for the method. Package the
192 # results into a SOAP response and return it.
193  
194 # Here we verify the digital signature if the server wants to check
195 # digital signatures
196 if (defined($self->{dsig})) { verify_envelope($self,$request); }
197  
198 #### DECRYPTION HERE #########
199 if (defined($self->{enc})) { decrypt_body($self,$request); }
200  
201 my $envelope = xml_get_child($request,$SOAP_ENV,'Envelope');
202 assert(defined($envelope));
203 my $body = xml_get_child($envelope,$SOAP_ENV,'Body');
204 assert(defined($body));
205  
206 # The body contains a single element which is the message.
207 my @children = xml_get_children($body);
208 $#children == 0 ||
209 assert(0,"Error! file \"".__FILE__."\", line ".__LINE__);
210 my $message = $children[0];
211  
212 # The method name is the node name of the message.
213 my ($method_urn,$method_name) = xml_get_name($message);
214 # The method name must appear in the right namespace.
215 ($method_urn eq $server_urn) ||
216 assert(0,"Method name not in the server namespace");
217  
218 # The method arguments are the children of the message.
219 my %request_args = ();
220 foreach my $arg ( xml_get_children($message) ) {
221 my ($arg_ns,$arg_name) = xml_get_name($arg);
222 # The arg name must appear in the right namespace.
223 ($arg_ns eq $server_urn) ||
224 assert(0,"Arg name not in the server namespace");
225  
226 $request_args{$arg_name} = $arg;
227 }
228  
229 # %response_args = call $method_name with %request_args;
230 my %response_args = $self->_dispatch_method($method_name, %request_args);
231  
232 # Put the response arguments together in a list.
233 my @response_args_list = ();
234 foreach my $k (keys %response_args) {
235 push @response_args_list, $response_args{$k};
236 }
237  
238 # Put together $method_name."Response" with %responses;
239 my $response =
240 document(element("SOAP-ENV:Envelope",
241 namespace("SOAP-ENV",$SOAP_ENV),
242 namespace("SOAP-ENC",$SOAP_ENC),
243 namespace("xsi",$xsi),
244 namespace("xsd",$xsd),
245 namespace("server",$server_urn),
246 element("SOAP-ENV:Header"),
247 element("SOAP-ENV:Body",
248 element("server:".$method_name."Result",
249 @response_args_list))));
250  
251 # fixme: encrypt if need be
252  
253 # Use whatever content type that this client used.
254 my $type;
255 if ( $content_type_is_text_xml ) {
256 # .NET requires this,...
257 $type = 'text/xml';
258 } else {
259 # but SOAP 1.2 requires this,
260 $type = 'application/soap+xml';
261 }
262 $type = 'text/xml';
263 print header(-type=>$type,
264 -charset=>'utf-8');
265 xml_to_fh($response,\*STDOUT);
266  
267 return 0;
268 }
269  
270 ########################################################################
271  
272 sub handle_post {
273 my ($self) = @_;
274  
275 # Set the handler for die calls.
276 $SIG{__DIE__} = \&html_die_handler;
277  
278 my $server_urn = $self->{urn};
279 defined($server_urn) || assert(0,"No server URN");
280 my $server_name = $self->{name};
281 defined($server_name) || assert(0,"No server name");
282  
283 my $method_name = param("_method_");
284  
285 print header,
286 start_html($server_name.": Results"),
287 h1($server_name.": Results"), p;
288  
289 if (!param()) {
290 print "No params? How did you manage that?", hr;
291 print end_html;
292 return 1;
293 }
294  
295 # Throw an error if either dsig or enc was expected.
296 if (defined($self->{dsig})) {
297 assert(0,"No digitial signatures for POST's");
298 }
299 if (defined($self->{enc})) {
300 assert(0,"No encryption for POST's");
301 }
302  
303 # Determine the formal parameters based on the method name
304 my @formal_params;
305 if ( $method_name eq "Call" || $method_name eq "Spawn" ) {
306 my %h = ();
307 foreach my $p ( @{$self->{params}} ) {
308 $h{$$p{name}} = $p;
309 }
310 foreach my $n ( @{$self->{in_order}} ) {
311 my $p = $h{$n};
312 assert(defined($p));
313 if ( $$p{direc} eq "out" ) {
314 # nothing
315 } else {
316 push @formal_params, $p;
317 }
318 }
319 } elsif ( $method_name eq "Running" || $method_name eq "Results" ) {
320 @formal_params = ({ name=> 'uid',
321 direc=> 'in',
322 mech=> 'val',
323 type=> 'string',
324 });
325 } else {
326 die;
327 }
328  
329 # Parse the actual parameters.
330  
331 my %request_args = ();
332 foreach my $p ( @formal_params ) {
333 my $name = $$p{name};
334 assert(defined($p),"No value for $name");
335 my $mech = $$p{mech};
336 my $type = $$p{type};
337 my $value;
338 if ( $mech eq 'val' ) {
339 $value = param($name);
340 } elsif ( $mech eq 'file' ) {
341 my $fh = param($name); #upload($name);
342 assert($fh,"File $name is missing");
343 $value = '';
344 # deleteme
345 #print pre("+".$fh."\n"),p;
346 while (<$fh>) {
347 # deleteme
348 #print pre("*\n"),p;
349 $value .= $_;
350 }
351 } else {
352 assert(0);
353 }
354 if ( $value ne '') {
355 $request_args{$name} = $value;
356 }
357 }
358  
359 print "Invoking method $method_name. Please wait.",p;
360 # %response_args = call $method_name with %request_args;
361 my %response_args = $self->_dispatch_method($method_name, %request_args);
362 print "Done.",p;
363  
364 # print the results
365 foreach my $k ( keys %response_args ) {
366 print h2($k,":"),hr;
367 my $value = $response_args{$k};
368 # evil hack : fixme
369 my $env = { xsi => "$xsi", xsd => "$xsd" };
370 $$value[1] = $env;
371 print pre(escape_HTML(arg_decode_to_string($value))),p;
372 print hr;
373 }
374  
375 # # Additional buttons for some methods
376 # if ($method_name eq "Spawn") {
377 # print start_form;
378 # print hidden('_method_','Running');
379 # print hidden('uid',arg_decode_to_string($response_args{uid}));
380 # print submit('Running');
381 # print end_form;
382 # } elsif ($method_name eq "Running") {
383 # }
384 # print hr;
385  
386 print end_html;
387  
388 return 0;
389 }
390  
391  
392  
393 ########################################################################
394  
395 # Generate WSDL for the server.
396 # ....
397 sub handle_wsdl {
398 my ($self) = @_;
399  
400 # Set the handler for die calls.
401 $SIG{__DIE__} = \&html_die_handler;
402  
403 my $server_name = $self->{name};
404 defined($server_name) || assert(0,"No server name");
405  
406 # the service element
407  
408 my $location;
409 if (!defined($ENV{SERVER_PROTOCOL})) {
410 assert(0,"Undefined protocol");
411 } elsif ($ENV{SERVER_PROTOCOL} =~ "^HTTP/1.") {
412 $location = sprintf("http://%s%s",
413 $ENV{HTTP_HOST},
414 $ENV{REQUEST_URI});
415 } elsif ($ENV{SERVER_PROTOCOL} eq "CGIFILE") {
416 $location = sprintf("cgifile:%s", $ENV{REQUEST_URI});
417 } else {
418 assert(0,"What kind of SERVER_PROTOCOL is ".$ENV{SERVER_PROTOCOL});
419 }
420  
421 $location =~ s/\?wsdl$//;
422  
423 my $port_name = $server_name."Port";
424 my $service_name = $server_name."Service";
425  
426 # Generate the WSDL
427 my $d = $self->_wsdl_generate
428 (sub {
429 my ($binding_name) = @_;
430 my $service =
431 element("service",
432 attr("name",$server_name),
433 element("documentation",
434 text("WSDL File for ".$server_name)),
435 element("port",
436 attr("name",$port_name),
437 attr("binding","server:".$binding_name),
438 element("soap:address",
439 attr("location",$location))));
440 return $service;
441 });
442  
443 # Print it.
444 print header(-type=>'text/xml',
445 -charset=>'utf-8');
446 xml_to_fh($d,\*STDOUT);
447  
448 # Done!
449  
450 return 0;
451 }
452  
453 ########################################################################
454  
455 sub handle_get {
456 my ($self) = @_;
457  
458 # Set the handler for die calls.
459 $SIG{__DIE__} = \&html_die_handler;
460  
461 my $server_urn = $self->{urn};
462 defined($server_urn) || assert(0,"No server URN");
463 my $server_name = $self->{name};
464 defined($server_name) || assert(0,"No server name");
465  
466 my $method_name = $ENV{QUERY_STRING};
467  
468 if (!defined($method_name) || $method_name eq "") {
469 print header,"\n";
470 print start_html($server_name),"\n";
471 print h1($server_name),"\n";
472  
473 my $script_name = basename($ENV{SCRIPT_NAME});
474 assert(defined($script_name));
475  
476 print h2('Methods'),"\n";
477 foreach my $method ("Call","Spawn","Running","Results") {
478 print a({-href=>$script_name."?".$method},$method),p,"\n";
479 }
480  
481 print h2('Interface'),"\n";
482 print a({-href=>$script_name."?wsdl"},"WSDL"),p,"\n";
483  
484 print hr,"\n";
485 print end_html,"\n";
486 } else {
487 print header,"\n";
488 print start_html($server_name.":$method_name"),"\n";
489 print h1($server_name.":$method_name"),"\n";
490  
491 # Chose the input parameters for the method
492 my @params;
493 if ( $method_name eq "Call" || $method_name eq "Spawn" ) {
494 my %h = ();
495 foreach my $p ( @{$self->{params}} ) {
496 $h{$$p{name}} = $p;
497 }
498 foreach my $n ( @{$self->{in_order}} ) {
499 my $p = $h{$n};
500 assert(defined($p));
501 if ( $$p{direc} eq "out" ) {
502 # nothing
503 } else {
504 push @params, $p;
505 }
506 }
507 } elsif ( $method_name eq "Running" || $method_name eq "Results" ) {
508 @params = ({ name=> 'uid',
509 direc=> 'in',
510 mech=> 'val',
511 type=> 'string',
512 });
513 } else {
514 die;
515 }
516  
517 # Generate the form
518 print start_multipart_form,"\n";
519 print hidden('_method_',$method_name),"\n";
520 foreach my $p ( @params ) {
521 my $name = $$p{name};
522 my $direc = $$p{direc};
523 my $mech = $$p{mech};
524 my $type = $$p{type};
525 my $optional = $$p{optional};
526 my $default = $$p{default};
527 print $name, "[", $type, "]:";
528 if ( $type eq "xml" || $type eq "raw" ) {
529 print filefield(-name=>$name);
530 } else {
531 print textfield($name);
532 }
533 print p,"\n";
534  
535 }
536 print submit, end_form, "\n";
537 print hr,"\n";
538 print end_html,"\n";
539 }
540 return 0;
541 }
542  
543 ########################################################################
544  
545 1;