Frontier/Responder.pm untested

filter: Statementstmt 0.0% 0 / 30 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 47 CC 5 SCAR 18.9
CC
5
Cov
0%
get_cgi_request
12.0
new
6.0
answer
6.0
CRAP 6.6
1 # File: Repsonder.pm
2 # based heavily on Ken MacLeod's Frontier::Daemon
3 # Author: Joe Johnston 7/2000
4 # Revisions:
5 # 11/2000 - Cleaned/Add POD. Took out 'use CGI'.
6 #
7 # Meant to be called from a CGI process to answer client
8 # requests and emit the appropriate reponses. See POD for details.
9 #
10 # LICENSE: This code is released under the same licensing
11 # as Perl itself.
12 #
13 # Use the code where ever you want, but due credit is appreciated.
14  
15 package Frontier::Responder;
16  
17 use strict;
18 use vars qw/@ISA/;
19  
20 use Frontier::RPC2;
21  
22 my $snappy_answer = "Hey, I need to return true, don't I?";
23  
24 # Class constructor.
25 # Input: (expects parameters to be passed in as a hash)
26 # methods => hashref, keys are API procedure names, values are
27 # subroutine references
28 #
29 # Output: blessed reference
30 sub new {
31 my $class = shift;
32 my %args = @_;
33 my $self = bless {}, (ref $class ? ref $class : $class);
34  
35 # Store the dispatch table away for future use.
36 $self->{methods} = $args{methods};
37 $self->{_decode} = Frontier::RPC2->new();
38  
39 return $self;
40 }
41  
42 # Grabs input from CGI "stream", makes request
43 # if possible, packs up the response in purddy
44 # XML
45 # Input: None
46 # Output: A XML string suitable for printing from a CGI process
47 sub answer{
48 my $self = shift;
49  
50 # fetch the xml message sent
51 my $request = get_cgi_request();
52
53 unless( defined $request ){
54 print
55 "Content-Type: text/txt\n\n";
56 exit;
57 }
58  
59 # Let's figure out the method to execute
60 # along with its arguments
61 my $response = $self->{_decode}->serve( $request,
62 $self->{methods} );
63 # Ship it!
64 return
65 "Content-Type: text/xml \n\n" . $response;
66  
67 }
68  
69 # private function. No need to advertise this.
70 # Remember, this is just XML.
71 # CGI.pm doesn't grok this.
72 sub get_cgi_request{
73 my $in;
74 if( $ENV{REQUEST_METHOD} eq 'POST' ){
75 my $len = $ENV{CONTENT_LENGTH};
76 unless ( read( STDIN, $in, $len ) == $len ){
77 return;
78 }
79 }else{
80 $in = $ENV{QUERY_STRING};
81 }
82
83 return $in;
84 }
85  
86 =pod
87  
88 =head1 NAME
89  
90 Frontier::Responder - Create XML-RPC listeners for normal CGI processes
91  
92 =head1 SYNOPSIS
93  
94 use Frontier::Responder;
95 my $res = Frontier::Responder->new( methods => {
96 add => sub{ $_[0] + $_[1] },
97 cat => sub{ $_[0] . $_[1] },
98 },
99 );
100 print $res->answer;
101  
102 =head1 DESCRIPTION
103  
104 Use I<Frontier::Responder> whenever you need to create an XML-RPC listener
105 using a standard CGI interface. To be effective, a script using this class
106 will often have to be put a directory from which a web server is authorized
107 to execute CGI programs. An XML-RPC listener using this library will be
108 implementing the API of a particular XML-RPC application. Each remote
109 procedure listed in the API of the user defined application will correspond
110 to a hash key that is defined in the C<new> method of a I<Frontier::Responder>
111 object. This is exactly the way I<Frontier::Daemon> works as well.
112 In order to process the request and get the response, the C<answer> method
113 is needed. Its return value is XML ready for printing.
114  
115 For those new to XML-RPC, here is a brief description of this protocol.
116 XML-RPC is a way to execute functions on a different
117 machine. Both the client's request and listeners response are wrapped
118 up in XML and sent over HTTP. Because the XML-RPC conversation is in
119 XML, the implementation languages of the server (here called a I<listener>),
120 and the client can be different. This can be a powerful and simple way
121 to have very different platforms work together without acrimony. Implicit
122 in the use of XML-RPC is a contract or API that an XML-RPC listener
123 implements and an XML-RPC client calls. The API needs to list not only
124 the various procedures that can be called, but also the XML-RPC datatypes
125 expected for input and output. Remember that although Perl is permissive
126 about datatyping, other languages are not. Unforuntately, the XML-RPC spec
127 doesn't say how to document the API. It is recomended that the author
128 of a Perl XML-RPC listener should at least use POD to explain the API.
129 This allows for the programmatic generation of a clean web page.
130  
131 =head1 METHODS
132  
133 =over 4
134  
135 =item new( I<OPTIONS> )
136  
137 This is the class constructor. As is traditional, it returns
138 a blessed reference to a I<Frontier::Responder> object. It expects
139 arguments to be given like a hash (Perl's named parameter mechanism).
140 To be effective, populate the C<methods> parameter with a hashref
141 that has API procedure names as keys and subroutine references as
142 values. See the SYNOPSIS for a sample usage.
143  
144  
145 =item answer()
146  
147 In order to parse the request and execute the procedure, this method
148 must be called. It returns a XML string that contains the procedure's
149 response. In a typical CGI program, this string will simply be printed
150 to STDOUT.
151  
152  
153 =back
154  
155 =head1 SEE ALSO
156  
157 perl(1), Frontier::RPC2(3)
158  
159 <http://www.scripting.com/frontier5/xml/code/rpc.html>
160  
161 =head1 AUTHOR
162  
163 Ken MacLeod <ken@bitsko.slc.ut.us> wrote the underlying
164 RPC library.
165  
166 Joe Johnston <jjohn@cs.umb.edu> wrote an adaptation
167 of the Frontier::Daemon class to create this CGI XML-RPC
168 listener class.
169  
170 =cut