| 1 | ##-*- Mode: CPerl -*- | ||
| 2 | |||
| 3 | ## File: DTA::CAB::Server::HTTP::Handler::XmlRpc.pm | ||
| 4 | ## Author: Bryan Jurish <moocow@cpan.org> | ||
| 5 | ## Description: | ||
| 6 | ## + CAB HTTP Server: request handler: XML-RPC queries (backwards-compatible) | ||
| 7 | ##====================================================================== | ||
| 8 | |||
| 9 | package DTA::CAB::Server::HTTP::Handler::XmlRpc; | ||
| 10 | use DTA::CAB::Server::HTTP::Handler; | ||
| 11 | use DTA::CAB::Server::XmlRpc; | ||
| 12 | use HTTP::Status; | ||
| 13 | use CGI; | ||
| 14 | use Carp; | ||
| 15 | use strict; | ||
| 16 | |||
| 17 | our @ISA = qw(DTA::CAB::Server::HTTP::Handler); | ||
| 18 | |||
| 19 | ##-------------------------------------------------------------- | ||
| 20 | ## Aliases | ||
| 21 | BEGIN { | ||
| 22 | DTA::CAB::Server::HTTP::Handler->registerAlias( | ||
| 23 | 'DTA::CAB::Server::Server::HTTP::Handler::XmlRpc' => __PACKAGE__, | ||
| 24 | 'XmlRpc' => __PACKAGE__, | ||
| 25 | 'xmlrpc' => __PACKAGE__, | ||
| 26 | ); | ||
| 27 | } | ||
| 28 | |||
| 29 | ##-------------------------------------------------------------- | ||
| 30 | ## Methods | ||
| 31 | |||
| 32 | ## $h = $class_or_obj->new(%options) | ||
| 33 | ## + %options: | ||
| 34 | ## cxsrv => $cxsrv, ##-- underlying DTA::CAB::Server::XmlRpc object | ||
| 35 | ## | ||
| 36 | ## + runtime %$h data: | ||
| 37 | sub new { | ||
| 38 | my $that = shift; | ||
| 39 | my $h = bless { | ||
| 40 | cxsrv => undef, | ||
| 41 | @_ | ||
| 42 | }, ref($that)||$that; | ||
| 43 | return $h; | ||
| 44 | } | ||
| 45 | |||
| 46 | ## $bool = $h->prepare($server) | ||
| 47 | sub prepare { | ||
| 48 | my ($h,$srv,$path) = @_; | ||
| 49 | |||
| 50 | ##-- get underlying DTA::CAB::Server::XmlRpc object | ||
| 51 | my ($cxsrv); | ||
| 52 | if (!defined($cxsrv=$srv->{cxsrv})) { | ||
| 53 | $cxsrv = $srv->{cxsrv} = DTA::CAB::Server::XmlRpc->new(%$srv) | ||
| 54 | or $h->logdie("prepare(): could not create DTA::CAB::Server::XmlRpc object: $!"); | ||
| 55 | } | ||
| 56 | elsif (!UNIVERSAL::isa($cxsrv,'DTA::CAB::Server::XmlRpc')) { | ||
| 57 | $cxsrv = $srv->{cxsrv} = DTA::CAB::Server::XmlRpc->loadPerlRef({ %$srv, %$cxsrv }) | ||
| 58 | or $h->logdie("prepare(): loadPerlRef() failed for DTA::CAB::Server::XmlRpc object: $!"); | ||
| 59 | } | ||
| 60 | $h->{cxsrv} = $cxsrv; | ||
| 61 | |||
| 62 | ##-- prepare underlying server | ||
| 63 | return $cxsrv->prepareLocal() if (!defined($cxsrv->{xsrv})); | ||
| 64 | |||
| 65 | return 1; | ||
| 66 | } | ||
| 67 | |||
| 68 | ## $rsp = $path->run($server, $localPath, $clientConn, $httpRequest) | ||
| 69 | ## + perform local processing | ||
| 70 | ## + should return a HTTP::Response object to pass to the client | ||
| 71 | ## + if the call die()s or returns undef, an error response will be | ||
| 72 | ## sent to the client instead if it the connection is still open | ||
| 73 | ## + this method may return the data to the client itself; if so, | ||
| 74 | ## it should close the client connection ($csock->shutdown(2); $csock->close()) | ||
| 75 | ## and return undef to prevent bogus error messages. | ||
| 76 | sub run { | ||
| 77 | my ($h, $srv, $path, $csock, $hreq) = @_; | ||
| 78 | my $xmlsrv = $h->{cxsrv}{xsrv}; ##-- underlying RPC::XML::Server object | ||
| 79 | |||
| 80 | if ($hreq->method eq 'HEAD') { | ||
| 81 | # The HEAD method will be answered with our return headers, | ||
| 82 | # both as a means of self-identification and a verification | ||
| 83 | # of live-status. All the headers were pre-set in the cached | ||
| 84 | # HTTP::Response object. Also, we don't count this for stats. | ||
| 85 | return $xmlsrv->response; | ||
| 86 | } | ||
| 87 | elsif ($hreq->method eq 'POST') { | ||
| 88 | # Extract & serve XML-RPC request | ||
| 89 | my ($xmlreq); | ||
| 90 | $xmlreq = $xmlsrv->parser->parse($hreq->content); | ||
| 91 | return $h->cerror($csock, RC_INTERNAL_SERVER_ERROR, "Error parsing XML-RPC request") | ||
| 92 | if (!defined($xmlreq)); | ||
| 93 | |||
| 94 | # Dispatch will always return a RPC::XML::response. | ||
| 95 | # RT29351: If there was an error from RPC::XML::Parser (such as | ||
| 96 | # a message that didn't conform to spec), then return it directly | ||
| 97 | # as a fault, don't have dispatch() try and handle it. | ||
| 98 | my ($xmlrsp); | ||
| 99 | if (ref($xmlreq)) { | ||
| 100 | local $xmlsrv->{peeraddr} = $csock->peeraddr; | ||
| 101 | local $xmlsrv->{peerport} = $csock->peerport; | ||
| 102 | local $xmlsrv->{peerhost} = $csock->peerhost; | ||
| 103 | $xmlrsp = $xmlsrv->dispatch($xmlreq); | ||
| 104 | } else { | ||
| 105 | $xmlrsp = RPC::XML::fault->new(RC_INTERNAL_SERVER_ERROR, $xmlreq); | ||
| 106 | $xmlrsp = RPC::XML::response->new($xmlrsp); | ||
| 107 | return $h->cerror($csock, RC_INTERNAL_SERVER_ERROR, $xmlrsp->as_string) | ||
| 108 | } | ||
| 109 | |||
| 110 | # Clone the pre-fab response and set headers | ||
| 111 | my $resp = $xmlsrv->response->clone; | ||
| 112 | my $content = $xmlrsp->as_string; | ||
| 113 | $resp->content($content); | ||
| 114 | $resp->content_length(bytes::length($content)); | ||
| 115 | $csock->send_response($resp); | ||
| 116 | $csock->shutdown(2); | ||
| 117 | $csock->close(); | ||
| 118 | } | ||
| 119 | else { | ||
| 120 | return $h->cerror($csock, RC_FORBIDDEN) | ||
| 121 | } | ||
| 122 | |||
| 123 | return undef; | ||
| 124 | } | ||
| 125 | |||
| 126 | ## undef = $h->finish($server, $clientConn) | ||
| 127 | ## + clean up handler state after run() | ||
| 128 | ## + default implementation does nothing | ||
| 129 | sub finish { | ||
| 130 | return; | ||
| 131 | } | ||
| 132 | |||
| 133 | |||
| 134 | 1; ##-- be happy | ||
| 135 | |||
| 136 | __END__ |