| 1 | package SOAP::WSDL::Server::Mod_Perl2; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use base qw(SOAP::WSDL::Server); | ||
| 5 | use Scalar::Util qw(blessed); | ||
| 6 | |||
| 7 | use HTTP::Request (); | ||
| 8 | use Apache2::RequestIO (); # $r->read() | ||
| 9 | use Apache2::RequestRec (); # $r->headers_in | ||
| 10 | use Apache2::RequestUtil(); # $r->dir_config() | ||
| 11 | use APR::Table (); # $r->headers_in->get() | ||
| 12 | use Apache2::Log (); # $r->log | ||
| 13 | use Apache2::Const -compile => qw( | ||
| 14 | OK | ||
| 15 | SERVER_ERROR | ||
| 16 | HTTP_LENGTH_REQUIRED | ||
| 17 | ); | ||
| 18 | |||
| 19 | use version; our $VERSION = qv('3.00.0_2'); | ||
| 20 | |||
| 21 | my %LOADED_OF = (); | ||
| 22 | |||
| 23 | sub handler { | ||
| 24 | my $r = shift; | ||
| 25 | my $rlog = $r->log(); | ||
| 26 | |||
| 27 | # | ||
| 28 | # Set up section; import requested modules, throwing errors if we're | ||
| 29 | # unable to do so. For maximum performance, this should be re-worked | ||
| 30 | # to use perl-based Apache directives rather than dir_config(), since | ||
| 31 | # the former happens at startup time and the latter at request time. | ||
| 32 | |||
| 33 | # | ||
| 34 | # dispatch_to | ||
| 35 | my $dispatch_to = $r->dir_config('dispatch_to'); | ||
| 36 | if (! $dispatch_to) { | ||
| 37 | $rlog->error("No 'dispatch_to' variable set in httpd.conf"); | ||
| 38 | return Apache2::Const::SERVER_ERROR; | ||
| 39 | } | ||
| 40 | |||
| 41 | if (! exists $LOADED_OF{$dispatch_to}) { | ||
| 42 | eval "require $dispatch_to"; | ||
| 43 | if ($@) { | ||
| 44 | $rlog->error("Failed to require [$dispatch_to]: $@"); | ||
| 45 | return Apache2::Const::SERVER_ERROR; | ||
| 46 | } | ||
| 47 | $LOADED_OF{$dispatch_to} = undef; | ||
| 48 | } | ||
| 49 | |||
| 50 | # | ||
| 51 | # SOAP service | ||
| 52 | my $soap_service_package = $r->dir_config('soap_service'); | ||
| 53 | if (! $soap_service_package) { | ||
| 54 | $rlog->error("No 'soap_service' variable set in httpd.conf"); | ||
| 55 | return Apache2::Const::SERVER_ERROR; | ||
| 56 | } | ||
| 57 | |||
| 58 | if (! exists $LOADED_OF{$soap_service_package}) { | ||
| 59 | eval "require $soap_service_package"; | ||
| 60 | if ($@) { | ||
| 61 | $rlog->error("Failed to require [$soap_service_package]: $@"); | ||
| 62 | return Apache2::Const::SERVER_ERROR; | ||
| 63 | } | ||
| 64 | $LOADED_OF{$soap_service_package} = undef; | ||
| 65 | } | ||
| 66 | |||
| 67 | # | ||
| 68 | # transport_class (optional) | ||
| 69 | my $transport_class = $r->dir_config('transport_class'); | ||
| 70 | if ($transport_class) { | ||
| 71 | eval "require $transport_class"; | ||
| 72 | if ($@) { | ||
| 73 | $rlog->error("Failed to require [$transport_class]: $@"); | ||
| 74 | return Apache2::Const::SERVER_ERROR; | ||
| 75 | } | ||
| 76 | } | ||
| 77 | else { | ||
| 78 | # | ||
| 79 | # if no transport class was specified, use this package's handle() | ||
| 80 | # method | ||
| 81 | $transport_class = __PACKAGE__; | ||
| 82 | } | ||
| 83 | |||
| 84 | # | ||
| 85 | # instantiate SOAP server object | ||
| 86 | my $server = $soap_service_package->new({ | ||
| 87 | dispatch_to => $dispatch_to, # methods | ||
| 88 | transport_class => $transport_class, # handle() call | ||
| 89 | }); | ||
| 90 | |||
| 91 | my $response_msg = $server->handle($r); | ||
| 92 | if ($response_msg =~ /^\d{3}$/) { | ||
| 93 | # | ||
| 94 | # a 3-digit number is presumed to be an HTTP return status; since | ||
| 95 | # we got this and not a SOAP response, it's presumed to be an | ||
| 96 | # error; pass it back to the client as-is | ||
| 97 | $rlog->error("Dispatcher returned HTTP $response_msg"); | ||
| 98 | return $response_msg; | ||
| 99 | } | ||
| 100 | |||
| 101 | if ($response_msg) { | ||
| 102 | $r->content_type('text/xml; charset="utf-8"'); | ||
| 103 | $r->print($response_msg); | ||
| 104 | return Apache2::Const::OK; | ||
| 105 | } | ||
| 106 | else { | ||
| 107 | $rlog->error("No response returned from dispatcher"); | ||
| 108 | return Apache2::Const::SERVER_ERROR; | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | sub handle { | ||
| 113 | my ($self, $r) = @_; | ||
| 114 | my $rlog = $r->log(); | ||
| 115 | |||
| 116 | my $length = $r->headers_in->get('content-length'); | ||
| 117 | if (! $length) { | ||
| 118 | $rlog->error("No content-length provided"); | ||
| 119 | # TODO maybe throw instead of returning a HTTP code? | ||
| 120 | # ... it's an exception, anyway... | ||
| 121 | return Apache2::Const::HTTP_LENGTH_REQUIRED; | ||
| 122 | } | ||
| 123 | |||
| 124 | # read may return less than requested - read until there's no more... | ||
| 125 | # TODO: We should note that LimitRequestBody is a must in apache config | ||
| 126 | my ($buffer, $read_length); | ||
| 127 | my $content = q{}; | ||
| 128 | while ($read_length = $r->read($buffer, $length)) { | ||
| 129 | $content .= $buffer; | ||
| 130 | } | ||
| 131 | |||
| 132 | if ($length != length $content) { | ||
| 133 | $rlog->error("Read length mismatch; read [" . length($content) . "] bytes but received [$length] bytes"); | ||
| 134 | return Apache2::Const::SERVER_ERROR; | ||
| 135 | } | ||
| 136 | |||
| 137 | # Shamelessly copied (with mild tweaks) from SOAP::WSDL::Server::CGI | ||
| 138 | # ... which was as shamelessly copied from SOAP::Transport::HTTP... | ||
| 139 | my $request = HTTP::Request->new( | ||
| 140 | $r->method => $r->uri, | ||
| 141 | HTTP::Headers->new( | ||
| 142 | SOAPAction => $r->headers_in()->get('SOAPAction'), | ||
| 143 | ), | ||
| 144 | $content, | ||
| 145 | ); | ||
| 146 | |||
| 147 | my $response_message = eval { $self->SUPER::handle($request) }; | ||
| 148 | |||
| 149 | # TODO return response if @$ is a SOAP::WSDL::XSD::Typelib::Builtin::anyType object | ||
| 150 | if ($@ || blessed($@)) { | ||
| 151 | $rlog->error("Failed to handle request: $@"); | ||
| 152 | return Apache2::Const::SERVER_ERROR; | ||
| 153 | } | ||
| 154 | else { | ||
| 155 | return $response_message; | ||
| 156 | } | ||
| 157 | } | ||
| 158 | |||
| 159 | 1; | ||
| 160 | |||
| 161 | __END__ |