RPC/Legacy/Server/Apache2.pm untested

filter: Statementstmt 0.0% 0 / 76 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 111 CC 7 SCAR 20.5
CC
7
Cov
0%
retrieve_json_from_get
20.0
retrieve_json_from_post
12.0
response
6.0
CRAP 7.8
1 ##############################################################################
2 package JSON::RPC::Legacy::Server::Apache2;
3  
4 use strict;
5  
6 use lib qw(/var/www/cgi-bin/json/);
7 use base qw(JSON::RPC::Legacy::Server);
8  
9 use Apache2::Const -compile => qw(OK HTTP_BAD_REQUEST SERVER_ERROR);
10  
11 use APR::Table ();
12 use Apache2::RequestRec ();
13 use Apache2::RequestIO ();
14 use Apache2::RequestUtil ();
15  
16  
17 $JSON::RPC::Legacy::Server::Apache::VERSION = '1.06';
18  
19  
20 sub handler {
21 my($r) = @_;
22  
23 my $s = __PACKAGE__->new;
24  
25 $s->request($r);
26  
27 $s->{path_info} = $r->path_info;
28  
29 my @modules = $r->dir_config('dispatch') || $r->dir_config('dispatch_to');
30  
31 $s->return_die_message( $r->dir_config('return_die_message') );
32  
33 $s->dispatch([@modules]);
34  
35 $s->handle(@_);
36  
37 Apache2::Const::OK;
38 }
39  
40  
41 sub new {
42 my $class = shift;
43 return $class->SUPER::new();
44 }
45  
46  
47 sub retrieve_json_from_post {
48 my $self = shift;
49 my $r = $self->request;
50 my $len = $r->headers_in()->get('Content-Length');
51  
52 return if($r->method ne 'POST');
53 return if($len > $self->max_length);
54  
55 my ($buf, $content);
56  
57 while( $r->read($buf,$len) ){
58 $content .= $buf;
59 }
60  
61 $content;
62 }
63  
64  
65 sub retrieve_json_from_get {
66 my $self = shift;
67 my $r = $self->request;
68 my $args = $r->args;
69  
70 $args = '' if (!defined $args);
71  
72 $self->{path_info} = $r->path_info;
73  
74 my $params = {};
75  
76 $self->version(1.1);
77  
78 for my $pair (split/&/, $args) {
79 my ($key, $value) = split/=/, $pair;
80 if ( defined ( my $val = $params->{ $key } ) ) {
81 if ( ref $val ) {
82 push @{ $params->{ $key } }, $value;
83 }
84 else { # change a scalar into an arrayref
85 $params->{ $key } = [];
86 push @{ $params->{ $key } }, $val, $value;
87 }
88 }
89 else {
90 $params->{ $key } = $value;
91 }
92 }
93  
94 my $method = $r->path_info;
95  
96 $method =~ s{^.*/}{};
97 $self->{path_info} =~ s{/?[^/]+$}{};
98  
99 $self->json->encode({
100 version => '1.1',
101 method => $method,
102 params => $params,
103 });
104 }
105  
106  
107 sub response {
108 my ($self, $response) = @_;
109 my $r = $self->request;
110  
111 $r->content_type($self->content_type);
112 $r->print($response->content);
113  
114 return ($response->code == 200)
115 ? Apache2::Const::OK : Apache2::Const::SERVER_ERROR;
116 }
117  
118  
119  
120 1;
121 __END__