File Coverage

blib/lib/XML/Compile/SOAP/Daemon/CGI.pm
Criterion Covered Total %
statement 55 68 80.8
branch 11 18 61.1
condition 10 21 47.6
subroutine 9 12 75.0
pod 2 3 66.6
total 87 122 71.3


line stmt bran cond sub pod time code
1             # Copyrights 2007-2022 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.03.
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   38870 use vars '$VERSION';
  2         7  
  2         127  
11             $VERSION = '3.15';
12              
13 2     2   18 use parent 'XML::Compile::SOAP::Daemon';
  2         6  
  2         15  
14              
15 2     2   134 use warnings;
  2         6  
  2         56  
16 2     2   10 use strict;
  2         10  
  2         65  
17              
18 2     2   23 use Log::Report 'xml-compile-soap-daemon';
  2         4  
  2         13  
19              
20 2     2   613 use CGI 3.53, ':cgi';
  2         8  
  2         28  
21 2     2   2505 use Encode;
  2         24  
  2         599  
22              
23             # do not depend on LWP
24             use constant
25 2         1729 { RC_OK => 200
26             , RC_METHOD_NOT_ALLOWED => 405
27             , RC_NOT_ACCEPTABLE => 406
28 2     2   15 };
  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   8252 { my ($self, $args, $test_cgi) = @_;
39 6         11 my $nph = $args->{nph};
40              
41 6   0     18 my $q = $test_cgi || $args->{query} || CGI->new;
42 6   50     18 my $method = $ENV{REQUEST_METHOD} || 'POST';
43 6   50     22 my $qs = $ENV{QUERY_STRING} || '';
44 6   100     16 my $ct = $ENV{CONTENT_TYPE} || 'text/plain';
45 6         15 $ct =~ s/\;\s.*//;
46              
47 6 50 66     16 return $self->sendWsdl($q, $nph)
48             if $method eq 'GET' && uc($qs) eq 'WSDL';
49              
50 6         12 my ($rc, $msg, $err, $mime, $bytes);
51 6 100 66     65 if($method ne 'POST' && $method ne 'M-POST')
    100          
52 1         2 { ($rc, $msg) = (RC_METHOD_NOT_ALLOWED, 'only POST or M-POST');
53 1         2 $err = 'attempt to connect via GET';
54             }
55             elsif($ct !~ m/\bxml\b/)
56 1         4 { ($rc, $msg) = (RC_NOT_ACCEPTABLE, 'required is XML');
57 1         2 $err = 'content-type seems to be text/plain, must be some XML';
58             }
59             else
60 4   50     13 { my $charset = $q->charset || 'ascii';
61 4         72 my $xmlin = decode $charset, $q->param('POSTDATA');
62 4   50     213 my $action = $ENV{HTTP_SOAPACTION} || $ENV{SOAPACTION} || '';
63 4         8 $action =~ s/["'\s]//g; # sometimes illegal quoting and blanks
64 4         18 ($rc, $msg, my $xmlout) = $self->process(\$xmlin, $q, $action);
65              
66 4 100       34152 if(UNIVERSAL::isa($xmlout, 'XML::LibXML::Document'))
67 1 50       9 { $bytes = $xmlout->toString($rc == RC_OK ? 0 : 1);
68 1         79 $mime = 'text/xml; charset="utf-8"';
69             }
70             else
71 3         8 { $err = $xmlout;
72             }
73             }
74              
75 6 100       18 unless($bytes)
76 5         59 { $bytes = "[$rc] $err\n";
77 5         1949 $mime = 'text/plain';
78             }
79              
80 6         37 my %headers =
81             ( -status => "$rc $msg"
82             , -type => $mime
83             , -charset => 'utf-8'
84             , -nph => $nph
85             );
86              
87 6 50       19 if(my $pp = $args->{postprocess})
88 0         0 { $pp->($args, \%headers, $rc, \$bytes);
89             }
90              
91 6         17 $headers{-Content_length} = length $bytes;
92 6         30 print $q->header(\%headers);
93 6         2486 print $bytes;
94             }
95              
96             sub setWsdlResponse($;$)
97 0     0 1   { my ($self, $fn, $ft) = @_;
98 0 0         $fn or return;
99 0           local *WSDL;
100 0 0         open WSDL, '<:raw', $fn
101             or fault __x"cannot read WSDL from {file}", file => $fn;
102 0           local $/;
103 0           $self->{wsdl_data} = ;
104 0   0       $self->{wsdl_type} = $ft || 'application/wsdl+xml';
105 0           close WSDL;
106             }
107              
108             sub sendWsdl($;$)
109 0     0 0   { my ($self, $q, $nph) = @_;
110              
111             print $q->header
112             ( -status => RC_OK.' WSDL specification'
113             , -type => $self->{wsdl_type}
114             , -charset => 'utf-8'
115             , -nph => $nph
116              
117             , -Content_length => length($self->{wsdl_data})
118 0           );
119              
120 0           print $self->{wsdl_data};
121             }
122            
123             #-----------------------------
124              
125              
126             1;