| 1 | # Copyrights 2007-2021 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. Meta-POD processed | ||
| 6 | # 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::Server; | ||
| 10 | use vars '$VERSION'; | ||
| 11 | $VERSION = '3.27'; | ||
| 12 | |||
| 13 | |||
| 14 | use warnings; | ||
| 15 | use strict; | ||
| 16 | |||
| 17 | use Log::Report 'xml-compile-soap'; | ||
| 18 | |||
| 19 | use XML::Compile::Util qw/unpack_type/; | ||
| 20 | use XML::Compile::SOAP::Util qw/:soap11/; | ||
| 21 | use HTTP::Status qw/RC_OK RC_BAD_REQUEST RC_NOT_ACCEPTABLE | ||
| 22 | RC_INTERNAL_SERVER_ERROR/; | ||
| 23 | |||
| 24 | |||
| 25 | sub new(@) { panic __PACKAGE__." only secundary in multiple inheritance" } | ||
| 26 | |||
| 27 | sub init($) | ||
| 28 | { my ($self, $args) = @_; | ||
| 29 | $self->{role} = $self->roleURI($args->{role} || 'NEXT') || $args->{role}; | ||
| 30 | $self; | ||
| 31 | } | ||
| 32 | |||
| 33 | #--------------------------------- | ||
| 34 | |||
| 35 | |||
| 36 | sub role() {shift->{role}} | ||
| 37 | |||
| 38 | #--------------------------------- | ||
| 39 | |||
| 40 | |||
| 41 | sub compileHandler(@) | ||
| 42 | { my ($self, %args) = @_; | ||
| 43 | |||
| 44 | my $decode = $args{decode}; | ||
| 45 | my $encode = $args{encode} || $self->compileMessage('SENDER'); | ||
| 46 | my $name = $args{name} | ||
| 47 | or error __x"each server handler requires a name"; | ||
| 48 | my $selector = $args{selector} || sub {0}; | ||
| 49 | |||
| 50 | # even without callback, we will validate | ||
| 51 | my $callback = $args{callback}; | ||
| 52 | |||
| 53 | sub | ||
| 54 | { my ($name, $xmlin, $info, $session) = @_; | ||
| 55 | # info is used to help determine if the xmlin is of the type for | ||
| 56 | # this call. $session is passed in by the server and is in turn | ||
| 57 | # passed to the handlers | ||
| 58 | $selector->($xmlin, $info) or return; | ||
| 59 | trace __x"procedure {name} selected", name => $name; | ||
| 60 | |||
| 61 | my $data; | ||
| 62 | if($decode) | ||
| 63 | { $data = try { $decode->($xmlin) }; | ||
| 64 | if(my $err = $@->wasFatal) | ||
| 65 | { $err->throw(reason => 'INFO', is_fatal => 0); | ||
| 66 | return ( RC_NOT_ACCEPTABLE, 'input validation failed' | ||
| 67 | , $self->faultValidationFailed($name, $err)) | ||
| 68 | } | ||
| 69 | } | ||
| 70 | else | ||
| 71 | { $data = $xmlin; | ||
| 72 | } | ||
| 73 | |||
| 74 | my $answer = $callback->($self, $data, $session); | ||
| 75 | unless(defined $answer) | ||
| 76 | { notice __x"procedure {name} did not produce an answer", name=>$name; | ||
| 77 | return ( RC_INTERNAL_SERVER_ERROR, 'no answer produced' | ||
| 78 | , $self->faultNoAnswerProduced($name)); | ||
| 79 | } | ||
| 80 | |||
| 81 | if(ref $answer ne 'HASH') | ||
| 82 | { notice __x"procedure {name} did not return a HASH", name => $name; | ||
| 83 | return ( RC_INTERNAL_SERVER_ERROR, 'invalid answer produced' | ||
| 84 | , $self->faultNoAnswerProduced($name)); | ||
| 85 | } | ||
| 86 | |||
| 87 | my $rc = (delete $answer->{_RETURN_CODE}) || RC_OK; | ||
| 88 | my $rc_txt = delete $answer->{_RETURN_TEXT} || 'Answer included'; | ||
| 89 | |||
| 90 | my $xmlout = try { $encode->($answer) }; | ||
| 91 | $@ or return ($rc, $rc_txt, $xmlout); | ||
| 92 | |||
| 93 | my $fatal = $@->wasFatal; | ||
| 94 | $fatal->throw(reason => 'ALERT', is_fatal => 0); | ||
| 95 | |||
| 96 | ( RC_INTERNAL_SERVER_ERROR, 'created response not valid' | ||
| 97 | , $self->faultResponseInvalid($name, $fatal) | ||
| 98 | ); | ||
| 99 | }; | ||
| 100 | } | ||
| 101 | |||
| 102 | |||
| 103 | sub compileFilter(@) | ||
| 104 | { my ($self, %args) = @_; | ||
| 105 | |||
| 106 | my $need_node; | ||
| 107 | if($args{style} eq 'rpc') | ||
| 108 | { # RPC-style wraps the body parameters in the procedure name. That's | ||
| 109 | # a logical construction. | ||
| 110 | $need_node = $args{body}{procedure} or panic; | ||
| 111 | } | ||
| 112 | else | ||
| 113 | { # Document-style does *not* contain the procedure name anywhere! We | ||
| 114 | # can only base the selection on the type of the elements. Therefore, | ||
| 115 | # procedure selection is often based on HTTP header (which was created | ||
| 116 | # for other purposes. | ||
| 117 | my $first = $args{body}{parts}[0]; | ||
| 118 | $need_node = $first ? $first->{element} : undef; | ||
| 119 | } | ||
| 120 | |||
| 121 | $need_node | ||
| 122 | or return sub { !defined $_[1]->{body}[0] }; # empty body | ||
| 123 | |||
| 124 | my ($need_ns, $need_local) = unpack_type($need_node); | ||
| 125 | |||
| 126 | # The returned code-ref is called with (XML, INFO) | ||
| 127 | sub { | ||
| 128 | my ($xml, $info) = @_; | ||
| 129 | (my $body) = $xml->getChildrenByLocalName('Body'); | ||
| 130 | (my $has) = $body->getElementsByTagNameNS($need_ns, $need_local); | ||
| 131 | defined $has; | ||
| 132 | }; | ||
| 133 | } | ||
| 134 | |||
| 135 | |||
| 136 | sub faultWriter() | ||
| 137 | { my $thing = shift; | ||
| 138 | my $self = ref $thing ? $thing : $thing->new; | ||
| 139 | $self->{fault_writer} ||= $self->compileMessage('SENDER'); | ||
| 140 | } | ||
| 141 | |||
| 142 | 1; |