File Coverage

blib/lib/CGI/JSONRPC.pm
Criterion Covered Total %
statement 18 35 51.4
branch 0 6 0.0
condition 0 6 0.0
subroutine 6 9 66.6
pod 0 3 0.0
total 24 59 40.6


line stmt bran cond sub pod time code
1             #!perl
2              
3             package CGI::JSONRPC;
4              
5 1     1   38598 use strict;
  1         2  
  1         26  
6 1     1   4 use warnings;
  1         2  
  1         20  
7 1     1   595 use CGI::JSONRPC::Dispatcher;
  1         4  
  1         41  
8 1     1   701 use CGI::JSONRPC::Base;
  1         4  
  1         31  
9 1     1   5 use base qw(CGI::JSONRPC::Base);
  1         1  
  1         83  
10 1     1   1767 use CGI;
  1         16768  
  1         7  
11              
12             our $VERSION = "0.11";
13              
14             return 1;
15              
16             sub headers_js {
17 0     0 0   my $self = shift;
18 0           $self->{cgi}->header("text/javascript");
19             }
20              
21             sub headers_json {
22 0     0 0   my $self = shift;
23 0           $self->{cgi}->header("text/json");
24             }
25              
26             sub handler {
27 0     0 0   my($class, $cgi,@args) = @_;
28            
29 0   0       $cgi ||= CGI->new;
30 0           my $self = $class->new(
31             path => $cgi->url(-absolute => 1, -full => 0, -path_info => 0),
32             path_info => $cgi->path_info(),
33             cgi => $cgi,
34             @args
35             );
36              
37 0           $self->{path_info} =~ s{^/|/$}{}g;
38 0           $self->{path_info} =~ s{//}{/}g;
39            
40 0           my $method = $cgi->request_method;
41            
42 0 0 0       if($method eq 'GET' || $method eq 'HEAD') {
    0          
43 0           print $self->headers_js(), $self->return_javascript;
44 0           return 1;
45             } elsif($method eq 'POST') {
46 0 0         my $json = $cgi->param('POSTDATA') or die "No POST data was sent!";
47 0           print $self->headers_json(), $self->run_json_request($json);
48 0           return 1;
49             } else {
50 0           die "Unsupported method: ", $cgi->method;
51             }
52             }
53              
54             =pod
55              
56             =head1 NAME
57              
58             CGI::JSONRPC - CGI handler for JSONRPC
59              
60             =head1 SYNOPSIS
61              
62             use CGI;
63             use CGI::JSONRPC;
64             my $cgi = new CGI;
65             CGI::JSONRPC->handler($cgi);
66             exit;
67            
68              
69             =head1 DESCRIPTION
70              
71             CGI::JSONRPC is a pole for perl.
72              
73             CGI::JSONRPC implements the JSONRPC protocol as defined at
74             L. When a JSONRPC request is received by
75             this handler, it is translated into a method call. The method and
76             it's arguments are determined by the JSON payload coming from the
77             browser, and the package to call this method on is determined by
78             the C apache config directive.
79              
80             A sample "dispatcher" module is supplied,
81             L
82              
83             B I
84             The interface is somewhat stable and well-tested, but other changes may
85             come as I work in implementing this on my website.>
86              
87             =head1 USAGE
88              
89             When contacted with a GET request, CGI::JSONRPC will reply with the
90             contents of JSONRPC.js, which contains code that can be used to create
91             JavaScript classes that can communicate with their Perl counterparts.
92             See the /examples/hello.html file for some sample JavaScript that uses
93             this library, and /examples/httpd.conf for the corresponding Perl.
94              
95             When contacted with a POST request, CGI::JSONRPC will attempt to
96             process and dispatch a JSONRPC request. If a valid JSONRPC request was
97             sent in the POST data, the dispatcher class will be called, with the
98             following arguments:
99              
100             =over
101              
102             =item $class
103              
104             Just like any other class method, the first argument passed in will be
105             name of the class being invoked.
106              
107             =item $id
108              
109             The object ID string from the JSONRPC request. In accordance with the
110             json-rpc spec, your response will only be sent to the client if this
111             value is defined.
112              
113             =item @params
114              
115             All further arguments to the method will be the arugments passed to the
116             JSONRPC constructor. It is expected to be a hash of key value option
117             pairs.
118              
119             =back
120              
121             If the client specified an C, your method's return value will be serialized
122             into a JSON array and sent to the client as the "result" section of the
123             JSONRPC response.
124              
125             =head2 The default dispatcher
126              
127             The default dispatcher adds another layer of functionality; it expects the
128             first argument in @params to be the name of the class the method is being
129             invoked on. See L for more details on that.
130              
131             =head1 AUTHOR
132              
133             Tyler "Crackerjack" MacDonald and
134             David Labatte .
135              
136             A lot of the JavaScript code was borrowed from Ingy döt Net's
137             L package.
138              
139             =head1 LICENSE
140              
141             Copyright 2008 Tyler "Crackerjack" MacDonald and
142             David Labatte
143              
144             This is free software; You may distribute it under the same terms as perl
145             itself.
146              
147             =head1 SEE ALSO
148              
149             The "examples" directory (examples/httpd.conf and examples/hello.html),
150             L, L.
151              
152             =cut