| 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::PSGI; | ||
| 10 | use vars '$VERSION'; | ||
| 11 | $VERSION = '3.14'; | ||
| 12 | |||
| 13 | use parent 'XML::Compile::SOAP::Daemon', 'Plack::Component'; | ||
| 14 | |||
| 15 | use warnings; | ||
| 16 | use strict; | ||
| 17 | |||
| 18 | use Log::Report 'xml-compile-soap-daemon'; | ||
| 19 | |||
| 20 | use Encode; | ||
| 21 | use Plack::Request; | ||
| 22 | use Plack::Response; | ||
| 23 | |||
| 24 | |||
| 25 | use constant | ||
| 26 | { RC_OK => 200 | ||
| 27 | , RC_METHOD_NOT_ALLOWED => 405 | ||
| 28 | , RC_NOT_ACCEPTABLE => 406 | ||
| 29 | , RC_SERVER_ERROR => 500 | ||
| 30 | }; | ||
| 31 | |||
| 32 | #-------------------- | ||
| 33 | |||
| 34 | |||
| 35 | sub init($) | ||
| 36 | { my ($self, $args) = @_; | ||
| 37 | $self->SUPER::init($args); | ||
| 38 | $self->_init($args); | ||
| 39 | $self; | ||
| 40 | } | ||
| 41 | |||
| 42 | #------------------------------ | ||
| 43 | |||
| 44 | sub run(@) | ||
| 45 | { my ($self, $args) = @_; | ||
| 46 | $self->_init($args); | ||
| 47 | $self->to_app; | ||
| 48 | } | ||
| 49 | |||
| 50 | sub _init($) | ||
| 51 | { my ($self, $args) = @_; | ||
| 52 | $self->{preprocess} = $args->{preprocess}; | ||
| 53 | $self->{postprocess} = $args->{postprocess}; | ||
| 54 | $self; | ||
| 55 | } | ||
| 56 | |||
| 57 | |||
| 58 | # PSGI request handler | ||
| 59 | sub call($) | ||
| 60 | { my ($self, $env) = @_; | ||
| 61 | my $res = eval { $self->_call($env) }; | ||
| 62 | $res ||= Plack::Response->new | ||
| 63 | ( RC_SERVER_ERROR | ||
| 64 | , [Content_Type => 'text/plain'] | ||
| 65 | , [$@] | ||
| 66 | ); | ||
| 67 | $res->finalize; | ||
| 68 | } | ||
| 69 | |||
| 70 | sub _call($;$) | ||
| 71 | { my ($self, $env, $test_env) = @_; | ||
| 72 | |||
| 73 | notice __x"WSA module loaded, but not used" | ||
| 74 | if XML::Compile::SOAP::WSA->can('new') && !keys %{$self->{wsa_input}}; | ||
| 75 | $self->{wsa_input_rev} = +{ reverse %{$self->{wsa_input}} }; | ||
| 76 | |||
| 77 | my $req = Plack::Request->new($test_env || $env); | ||
| 78 | |||
| 79 | return $self->sendWsdl($req) | ||
| 80 | if $req->method eq 'GET' && uc($req->uri->query || '') eq 'WSDL'; | ||
| 81 | |||
| 82 | if(my $pp = $self->{preprocess}) | ||
| 83 | { $pp->($req); | ||
| 84 | } | ||
| 85 | |||
| 86 | my $method = $req->method; | ||
| 87 | my $ct = $req->content_type || 'text/plain'; | ||
| 88 | $ct =~ s/\;\s.*//; | ||
| 89 | |||
| 90 | my ($rc, $msg, $err, $mime, $bytes); | ||
| 91 | if($method ne 'POST' && $method ne 'M-POST') | ||
| 92 | { ($rc, $msg) = (RC_METHOD_NOT_ALLOWED, 'only POST or M-POST'); | ||
| 93 | $err = 'attempt to connect via GET'; | ||
| 94 | } | ||
| 95 | elsif($ct !~ m/\bxml\b/) | ||
| 96 | { ($rc, $msg) = (RC_NOT_ACCEPTABLE, 'required is XML'); | ||
| 97 | $err = 'content-type seems to be text/plain, must be some XML'; | ||
| 98 | } | ||
| 99 | else | ||
| 100 | { my $charset = $req->headers->content_type_charset || 'ascii'; | ||
| 101 | my $xmlin = decode $charset, $req->content; | ||
| 102 | my $action = $req->header('SOAPAction') || ''; | ||
| 103 | $action =~ s/["'\s]//g; # sometimes illegal quoting and blanks " | ||
| 104 | ($rc, $msg, my $xmlout) = $self->process(\$xmlin, $req, $action); | ||
| 105 | |||
| 106 | if(UNIVERSAL::isa($xmlout, 'XML::LibXML::Document')) | ||
| 107 | { $bytes = $xmlout->toString($rc == RC_OK ? 0 : 1); | ||
| 108 | $mime = 'text/xml; charset="utf-8"'; | ||
| 109 | } | ||
| 110 | else | ||
| 111 | { $err = $xmlout; | ||
| 112 | } | ||
| 113 | } | ||
| 114 | |||
| 115 | unless($bytes) | ||
| 116 | { $bytes = "[$rc] $err\n"; | ||
| 117 | $mime = 'text/plain'; | ||
| 118 | } | ||
| 119 | |||
| 120 | my $res = $req->new_response($rc, | ||
| 121 | { Warning => "199 $msg" | ||
| 122 | , Content_Type => $mime | ||
| 123 | }, $bytes); | ||
| 124 | |||
| 125 | if(my $pp = $self->{postprocess}) | ||
| 126 | { $pp->($req, $res); | ||
| 127 | } | ||
| 128 | |||
| 129 | $res->content_length(length $bytes); | ||
| 130 | $res; | ||
| 131 | } | ||
| 132 | |||
| 133 | sub setWsdlResponse($;$) | ||
| 134 | { my ($self, $fn, $ft) = @_; | ||
| 135 | local *WSDL; | ||
| 136 | open WSDL, '<:raw', $fn | ||
| 137 | or fault __x"cannot read WSDL from {file}", file => $fn; | ||
| 138 | local $/; | ||
| 139 | $self->{wsdl_data} = <WSDL>; | ||
| 140 | $self->{wsdl_type} = $ft || 'application/wsdl+xml'; | ||
| 141 | close WSDL; | ||
| 142 | } | ||
| 143 | |||
| 144 | sub sendWsdl($) | ||
| 145 | { my ($self, $req) = @_; | ||
| 146 | |||
| 147 | my $res = $req->new_response(RC_OK, | ||
| 148 | { Warning => '199 WSDL specification' | ||
| 149 | , Content_Type => $self->{wsdl_type}.'; charset=utf-8' | ||
| 150 | , Content_Length => length($self->{wsdl_data}) | ||
| 151 | }, $self->{wsdl_data}); | ||
| 152 | |||
| 153 | $res; | ||
| 154 | } | ||
| 155 | |||
| 156 | #----------------------------- | ||
| 157 | |||
| 158 | 1; | ||
| 159 | |||
| 160 |