File Coverage

blib/lib/XML/Compile/SOAP/Daemon/CGI.pm
Criterion Covered Total %
statement 54 67 80.6
branch 12 20 60.0
condition 10 21 47.6
subroutine 9 12 75.0
pod 2 3 66.6
total 87 123 70.7


line stmt bran cond sub pod time code
1             # Copyrights 2007-2018 by [Mark Overmeer ].
2             # For other contributors see ChangeLog.
3             # See the manual pages for details on the licensing terms.
4             # Pod stripped from pm file by OODoc 2.02.
5             # This code is part of distribution XML-Compile-SOAP-Daemon. Meta-POD
6             # processed with OODoc into POD and HTML manual-pages. See README.md
7             # Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
8              
9             package XML::Compile::SOAP::Daemon::CGI;
10 2     2   37908 use vars '$VERSION';
  2         6  
  2         127  
11             $VERSION = '3.14';
12              
13 2     2   13 use parent 'XML::Compile::SOAP::Daemon';
  2         5  
  2         13  
14              
15 2     2   118 use warnings;
  2         4  
  2         52  
16 2     2   13 use strict;
  2         10  
  2         48  
17              
18 2     2   11 use Log::Report 'xml-compile-soap-daemon';
  2         4  
  2         25  
19              
20 2     2   618 use CGI 3.53, ':cgi';
  2         6  
  2         15  
21 2     2   2498 use Encode;
  2         33  
  2         222  
22              
23             # do not depend on LWP
24             use constant
25 2         1664 { RC_OK => 200
26             , RC_METHOD_NOT_ALLOWED => 405
27             , RC_NOT_ACCEPTABLE => 406
28 2     2   13 };
  2         4  
29              
30              
31             #--------------------
32              
33 0     0 1 0 sub runCgiRequest(@) {shift->run(@_)}
34              
35              
36             # called by SUPER::run()
37             sub _run($;$)
38 6     6   7530 { my ($self, $args, $test_cgi) = @_;
39              
40 6   0     18 my $q = $test_cgi || $args->{query} || CGI->new;
41 6   50     17 my $method = $ENV{REQUEST_METHOD} || 'POST';
42 6   50     26 my $qs = $ENV{QUERY_STRING} || '';
43 6   100     15 my $ct = $ENV{CONTENT_TYPE} || 'text/plain';
44 6         12 $ct =~ s/\;\s.*//;
45              
46 6 50 66     19 return $self->sendWsdl($q)
47             if $method eq 'GET' && uc($qs) eq 'WSDL';
48              
49 6         11 my ($rc, $msg, $err, $mime, $bytes);
50 6 100 66     34 if($method ne 'POST' && $method ne 'M-POST')
    100          
51 1         2 { ($rc, $msg) = (RC_METHOD_NOT_ALLOWED, 'only POST or M-POST');
52 1         2 $err = 'attempt to connect via GET';
53             }
54             elsif($ct !~ m/\bxml\b/)
55 1         3 { ($rc, $msg) = (RC_NOT_ACCEPTABLE, 'required is XML');
56 1         2 $err = 'content-type seems to be text/plain, must be some XML';
57             }
58             else
59 4   50     13 { my $charset = $q->charset || 'ascii';
60 4         53 my $xmlin = decode $charset, $q->param('POSTDATA');
61 4   50     250 my $action = $ENV{HTTP_SOAPACTION} || $ENV{SOAPACTION} || '';
62 4         6 $action =~ s/["'\s]//g; # sometimes illegal quoting and blanks
63 4         19 ($rc, $msg, my $xmlout) = $self->process(\$xmlin, $q, $action);
64              
65 4 100       31184 if(UNIVERSAL::isa($xmlout, 'XML::LibXML::Document'))
66 1 50       9 { $bytes = $xmlout->toString($rc == RC_OK ? 0 : 1);
67 1         110 $mime = 'text/xml; charset="utf-8"';
68             }
69             else
70 3         10 { $err = $xmlout;
71             }
72             }
73              
74 6 100       15 unless($bytes)
75 5         86 { $bytes = "[$rc] $err\n";
76 5         1832 $mime = 'text/plain';
77             }
78              
79             my %headers =
80             ( -status => "$rc $msg"
81             , -type => $mime
82             , -charset => 'utf-8'
83 6 50       39 , -nph => ($args->{nph} ? 1 : 0)
84             );
85              
86 6 50       18 if(my $pp = $args->{postprocess})
87 0         0 { $pp->($args, \%headers, $rc, \$bytes);
88             }
89              
90 6         14 $headers{-Content_length} = length $bytes;
91 6         23 print $q->header(\%headers);
92 6         2252 print $bytes;
93             }
94              
95             sub setWsdlResponse($;$)
96 0     0 1   { my ($self, $fn, $ft) = @_;
97 0 0         $fn or return;
98 0           local *WSDL;
99 0 0         open WSDL, '<:raw', $fn
100             or fault __x"cannot read WSDL from {file}", file => $fn;
101 0           local $/;
102 0           $self->{wsdl_data} = ;
103 0   0       $self->{wsdl_type} = $ft || 'application/wsdl+xml';
104 0           close WSDL;
105             }
106              
107             sub sendWsdl($)
108 0     0 0   { my ($self, $q) = @_;
109              
110             print $q->header
111             ( -status => RC_OK.' WSDL specification'
112             , -type => $self->{wsdl_type}
113             , -charset => 'utf-8'
114             , -nph => 1
115              
116             , -Content_length => length($self->{wsdl_data})
117 0           );
118              
119 0           print $self->{wsdl_data};
120             }
121            
122             #-----------------------------
123              
124              
125             1;