« blib/lib/SOAP/WSDL/XSD/Typelib/Element.pm
↑ Summary
blib/lib/SOAP/WSDL/Generator/Template/XSD.pm »
| 1 | package SOAP::WSDL::Server::CGI; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | use Encode; | ||
| 6 | |||
| 7 | use HTTP::Request; | ||
| 8 | use HTTP::Response; | ||
| 9 | use HTTP::Status; | ||
| 10 | use HTTP::Headers; | ||
| 11 | use Scalar::Util qw(blessed); | ||
| 12 | |||
| 13 | use Class::Std::Fast::Storable; | ||
| 14 | |||
| 15 | use base qw(SOAP::WSDL::Server); | ||
| 16 | |||
| 17 | use version; our $VERSION = qv('3.00.0_2'); | ||
| 18 | |||
| 19 | # mostly copied from SOAP::Lite. Unfortunately we can't use SOAP::Lite's CGI | ||
| 20 | # server directly - we would have to swap out it's base class... | ||
| 21 | # | ||
| 22 | # This should be a warning for us: We should not handle methods via inheritance, | ||
| 23 | # but via some plugin mechanism, to allow alternative handlers to be plugged | ||
| 24 | # in. | ||
| 25 | |||
| 26 | sub handle { | ||
| 27 | my $self = shift; | ||
| 28 | my $response; | ||
| 29 | my $length = $ENV{'CONTENT_LENGTH'} || 0; | ||
| 30 | |||
| 31 | if (!$length) { | ||
| 32 | $response = HTTP::Response->new(411); # LENGTH REQUIRED | ||
| 33 | $self->_output($response); | ||
| 34 | return; | ||
| 35 | } | ||
| 36 | |||
| 37 | if (exists $ENV{EXPECT} && $ENV{EXPECT} =~ /\b100-Continue\b/i) { | ||
| 38 | print "HTTP/1.1 100 Continue\r\n\r\n"; | ||
| 39 | } | ||
| 40 | |||
| 41 | my $content = q{}; | ||
| 42 | my $buffer; | ||
| 43 | |||
| 44 | # do we need to use bytes; here ? | ||
| 45 | binmode(STDIN); | ||
| 46 | while (read(STDIN,$buffer,$length - length($content))) { | ||
| 47 | $content .= $buffer; | ||
| 48 | } | ||
| 49 | |||
| 50 | my $request = HTTP::Request->new( | ||
| 51 | $ENV{'REQUEST_METHOD'} || '' => $ENV{'SCRIPT_NAME'}, | ||
| 52 | HTTP::Headers->new( | ||
| 53 | map { | ||
| 54 | (/^HTTP_(.+)/i | ||
| 55 | ? ($1=~m/SOAPACTION/) | ||
| 56 | ?('SOAPAction') | ||
| 57 | :($1) | ||
| 58 | : $_ | ||
| 59 | ) => $ENV{$_} | ||
| 60 | } keys %ENV), | ||
| 61 | $content, | ||
| 62 | ); | ||
| 63 | |||
| 64 | # we copy the response message around here. | ||
| 65 | # Passing by reference would be much better... | ||
| 66 | my $response_message = eval { $self->SUPER::handle($request) }; | ||
| 67 | # caveat: SOAP::WSDL::SOAP::Typelib::Fault11 is false in bool context... | ||
| 68 | if ($@ || blessed $@) { | ||
| 69 | my $exception = $@; | ||
| 70 | $response = HTTP::Response->new(500); | ||
| 71 | $response->header('Content-type' => 'text/xml; charset="utf-8"'); | ||
| 72 | if (blessed($exception)) { | ||
| 73 | $response->content( $self->get_serializer->serialize({ | ||
| 74 | body => $exception | ||
| 75 | }) | ||
| 76 | ); | ||
| 77 | } | ||
| 78 | else { | ||
| 79 | $response->content($exception); | ||
| 80 | } | ||
| 81 | } | ||
| 82 | else { | ||
| 83 | $response = HTTP::Response->new(200); | ||
| 84 | $response->header('Content-type' => 'text/xml; charset="utf-8"'); | ||
| 85 | $response->content( encode('utf8', $response_message ) ); | ||
| 86 | { | ||
| 87 | use bytes; | ||
| 88 | $response->header('Content-length', length $response_message); | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | $self->_output($response); | ||
| 93 | return; | ||
| 94 | } | ||
| 95 | |||
| 96 | sub _output :PRIVATE { | ||
| 97 | my ($self, $response) = @_; | ||
| 98 | # imitate nph- cgi for IIS (pointed by Murray Nesbitt) | ||
| 99 | my $status = defined($ENV{'SERVER_SOFTWARE'}) && $ENV{'SERVER_SOFTWARE'}=~/IIS/ | ||
| 100 | ? $ENV{SERVER_PROTOCOL} || 'HTTP/1.0' | ||
| 101 | : 'Status:'; | ||
| 102 | |||
| 103 | my $code = $response->code; | ||
| 104 | binmode(STDOUT); | ||
| 105 | print STDOUT "$status $code ", HTTP::Status::status_message($code) | ||
| 106 | , "\015\012", $response->headers_as_string("\015\012") | ||
| 107 | , "\015\012", $response->content; | ||
| 108 | } | ||
| 109 | |||
| 110 | 1; | ||
| 111 | |||
| 112 | =pod | ||
| 113 | |||
| 114 | =head1 NAME | ||
| 115 | |||
| 116 | SOAP::WSDL::Server::CGI - CGI based SOAP server | ||
| 117 | |||
| 118 | =head1 SYNOPSIS | ||
| 119 | |||
| 120 | use MyServer::TestService::TestPort; | ||
| 121 | my $server = MyServer::TestService::TestPort->new({ | ||
| 122 | dispatch_to => 'main', | ||
| 123 | transport_class => 'SOAP::WSDL::Server::CGI', # optional, default | ||
| 124 | }); | ||
| 125 | $server->handle(); | ||
| 126 | |||
| 127 | =head1 USAGE | ||
| 128 | |||
| 129 | To use SOAP::WSDL::Server::CGI efficiently, you should first create a server | ||
| 130 | interface using L<wsdl2perl.pl|wsdl2perl.pl>. | ||
| 131 | |||
| 132 | SOAP::WSDL::Server dispatches all calls to appropriately named methods in the | ||
| 133 | class or object set via C<dispatch_to>. | ||
| 134 | |||
| 135 | See the generated server class on details. | ||
| 136 | |||
| 137 | =head1 DESCRIPTION | ||
| 138 | |||
| 139 | Lightweight CGI based SOAP server. SOAP::WSDL::Server::CGI does not provide | ||
| 140 | the fancier things of CGI handling, like URL parsing, parameter extraction | ||
| 141 | or the like, but provides a basic SOAP server using SOAP::WSDL::Server. | ||
| 142 | |||
| 143 | =head1 METHODS | ||
| 144 | |||
| 145 | =head2 handle | ||
| 146 | |||
| 147 | See synopsis above. | ||
| 148 | |||
| 149 | =head1 LICENSE AND COPYRIGHT | ||
| 150 | |||
| 151 | Copyright 2004-2008 Martin Kutter. | ||
| 152 | |||
| 153 | This file is part of SOAP-WSDL. You may distribute/modify it under the same | ||
| 154 | terms as perl itself | ||
| 155 | |||
| 156 | =head1 AUTHOR | ||
| 157 | |||
| 158 | Martin Kutter E<lt>martin.kutter fen-net.deE<gt> | ||
| 159 | |||
| 160 | =head1 REPOSITORY INFORMATION | ||
| 161 | |||
| 162 | $Rev: 391 $ | ||
| 163 | $LastChangedBy: kutterma $ | ||
| 164 | $Id: Client.pm 391 2007-11-17 21:56:13Z kutterma $ | ||
| 165 | $HeadURL: https://soap-wsdl.svn.sourceforge.net/svnroot/soap-wsdl/SOAP-WSDL/trunk/lib/SOAP/WSDL/Client.pm $ | ||
| 166 | |||
| 167 | =cut |