| 1 | package SOAP::WSDL::Server::Plack::Transport; | ||
| 2 | use Moose; | ||
| 3 | |||
| 4 | # ABSTRACT: Plack Server Transport for SOAP::WSDL | ||
| 5 | |||
| 6 | use Carp; | ||
| 7 | use Try::Tiny; | ||
| 8 | |||
| 9 | # As SOAP::WSDL::Server is a Class::Std::Fast inside-out class we can't | ||
| 10 | # reliable inherit from it without using Class::Std::Fast too. Instead | ||
| 11 | # of inheritance we use a delegate pattern | ||
| 12 | use SOAP::WSDL::Server; | ||
| 13 | |||
| 14 | |||
| 15 | has 'action_map_ref' => ( | ||
| 16 | is => 'rw', | ||
| 17 | isa => 'HashRef', | ||
| 18 | ); | ||
| 19 | |||
| 20 | |||
| 21 | has 'class_resolver' => ( | ||
| 22 | is => 'rw', | ||
| 23 | isa => 'Str', | ||
| 24 | ); | ||
| 25 | |||
| 26 | |||
| 27 | has 'dispatch_to' => ( | ||
| 28 | is => 'rw', | ||
| 29 | isa => 'Str', | ||
| 30 | ); | ||
| 31 | |||
| 32 | |||
| 33 | # private server instance for delegate | ||
| 34 | has '_soap_wsdl_server' => ( | ||
| 35 | is => 'ro', | ||
| 36 | isa => 'SOAP::WSDL::Server', | ||
| 37 | lazy => 1, | ||
| 38 | default => sub { | ||
| 39 | my $self = $_[0]; | ||
| 40 | return SOAP::WSDL::Server->new({ | ||
| 41 | action_map_ref => $self->action_map_ref(), | ||
| 42 | class_resolver => $self->class_resolver(), | ||
| 43 | dispatch_to => $self->dispatch_to(), | ||
| 44 | }); | ||
| 45 | }, | ||
| 46 | handles => [qw( | ||
| 47 | get_action_map_ref | ||
| 48 | set_action_map_ref | ||
| 49 | get_class_resolver | ||
| 50 | set_class_resolver | ||
| 51 | get_dispatch_to | ||
| 52 | set_dispatch_to | ||
| 53 | )], | ||
| 54 | documentation => 'Carry SOAP::WSDL::Server instance to delegate to', | ||
| 55 | ); | ||
| 56 | |||
| 57 | |||
| 58 | sub handle { | ||
| 59 | my ($self, $req) = @_; | ||
| 60 | |||
| 61 | my $logger = $req->logger(); | ||
| 62 | $logger = sub { } unless defined $logger; | ||
| 63 | |||
| 64 | my $length = $req->headers->header('Content-Length'); | ||
| 65 | if (!$length) { | ||
| 66 | $logger->({ | ||
| 67 | level => 'error', | ||
| 68 | message => "No Content-Length provided", | ||
| 69 | }); | ||
| 70 | # TODO maybe throw instead of returning a HTTP code? | ||
| 71 | return 411; # Length required | ||
| 72 | } | ||
| 73 | |||
| 74 | # read may return less than requested - read until there's no more... | ||
| 75 | my ($buffer, $read_length); | ||
| 76 | my $body_psgi_input = $req->body(); | ||
| 77 | my $content = q{}; | ||
| 78 | while ($read_length = $body_psgi_input->read($buffer, $length)) { | ||
| 79 | $content .= $buffer; | ||
| 80 | } | ||
| 81 | |||
| 82 | if ($length != length($content)) { | ||
| 83 | $logger->({ | ||
| 84 | level => 'error', | ||
| 85 | message => sprintf( | ||
| 86 | "Read length mismatch; read [%d] bytes but received [%d] bytes", | ||
| 87 | length($content), $length | ||
| 88 | ), | ||
| 89 | }); | ||
| 90 | return 500; | ||
| 91 | } | ||
| 92 | |||
| 93 | # Shamelessly copied (with mild tweaks) from SOAP::WSDL::Server::Mod_Perl2 | ||
| 94 | # which was as shamelessly copied from SOAP::WSDL::Server::CGI which was | ||
| 95 | # as shamelessly copied from SOAP::Transport::HTTP... | ||
| 96 | my $request = HTTP::Request->new( | ||
| 97 | $req->method() => $req->uri(), | ||
| 98 | $req->headers->clone(), | ||
| 99 | $content | ||
| 100 | ); | ||
| 101 | #HTTP::Headers->new( SOAPAction => $req->headers->header('SOAPAction') ), | ||
| 102 | |||
| 103 | my $response_message; | ||
| 104 | try { | ||
| 105 | #$response_message = $self->SUPER::handle($request); | ||
| 106 | $response_message = $self->_soap_wsdl_server->handle($request); | ||
| 107 | } catch { | ||
| 108 | my $exception = $_; | ||
| 109 | $logger->({ | ||
| 110 | level => 'error', | ||
| 111 | message => "Failed to handle request: $exception", | ||
| 112 | }); | ||
| 113 | return 500; | ||
| 114 | }; | ||
| 115 | |||
| 116 | return $response_message; | ||
| 117 | }; | ||
| 118 | |||
| 119 | __PACKAGE__->meta->make_immutable(); | ||
| 120 | |||
| 121 | |||
| 122 | |||
| 123 | __END__ |