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