Lim/RPC/Protocol/REST.pm

filter: Statementstmt 31.2% 35 / 112 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 64.7% 11 / 17 Podpod 100% 5 / 5 totaltotal 25.5% 51 / 200 SCAR 60.4
CC
28
Cov
21%
handle
342.0
__ANON__
132.0
__ANON__
2.0
CRAP 420.6
« Lim/Component.pm ↑ Summary Lim/RPC/Protocol/HTTP.pm »
1 package Lim::RPC::Protocol::REST;
2  
3 5 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 5 ▶use Scalar::Util qw(blessed weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use HTTP::Status qw(:constants);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use HTTP::Request ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use HTTP::Response ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use JSON::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 5 ▶use Lim ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use Lim::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use Lim::RPC::Callback ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 5 ▶use base qw(Lim::RPC::Protocol);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 =encoding utf8
19  
20 =head1 NAME
21  
22 ...
23  
24 =head1 VERSION
25  
26 See L<Lim> for version.
27  
28 =cut
29  
30 5 our $VERSION = $Lim::VERSION;
31 5 our $JSON = JSON::XS->new->ascii->convert_blessed;
32 5 our %REST_CRUD = (
33 GET => 'READ',
34 POST => 'UPDATE',
35 PUT => 'CREATE',
36 DELETE => 'DELETE'
37 );
38  
39 =head1 SYNOPSIS
40  
41 ...
42  
43 =head1 SUBROUTINES/METHODS
44  
45 =head2 Init
46  
47 =cut
48  
49 ▶sub Init {
Subroutine Init: not called CC=1 SCAR=0.0
Pod documented
50 }
51  
52 =head2 Destroy
53  
54 =cut
55  
56 ▶sub Destroy {
Subroutine Destroy: not called CC=1 SCAR=0.0
Pod documented
57 }
58  
59 =head2 name
60  
61 =cut
62  
63 sub name {
64 4 ▶ 'rest';
Subroutine name: called CC=1 SCAR=0.0
Pod documented
65 }
66  
67 =head2 serve
68  
69 =cut
70  
71 ▶sub serve {
Subroutine serve: not called CC=1 SCAR=0.0
Pod documented
72 }
73  
74 =head2 handle
75  
76 =cut
77  
78 sub handle {
79 0 ▶ my ($self, $cb, $request) = @_;
Subroutine handle: not called CC=18 SCAR=58.3
Pod documented
80
81 0 ▶ unless (blessed($request) and $request->isa('HTTP::Request')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($request) and $request->isa("HTTP::Request")) 0 0 0
Condition0%0/3
builtin::blessed($request) and $request->isa("HTTP::Request")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($request) and $request->isa("HTTP::Request")
builtin::blessed($request) $request->isa("HTTP::Request")
Truth table0%0/3
A and B
A= builtin::blessed($request)B= $request->isa("HTTP::Request")
ABresult
0X0
100
111
82 0 return;
83 }
84  
85 0 ▶ if ($request->uri =~ /^\/([a-zA-Z]+)\/([a-zA-Z_]+)(?:\/([^\?]*)){0,1}/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($request->uri =~ m[^/([a-zA-Z]+)/([a-zA-Z_]+)(?:/([^\?]*)){0,1}]ou) 0 0 0
86 0 my ($module, $function, $parameters) = ($1, $2, $3);
87 0 my $response = HTTP::Response->new;
88 0 $response->request($request);
89 0 $response->protocol($request->protocol);
90
91 0 $module = lc($module);
92 0 my $server = $self->server;
93 0 ▶ if (defined $server and $server->have_module($module)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $server and $server->have_module($module)) { } 0 0 0
Condition0%0/3
defined $server and $server->have_module($module)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $server and $server->have_module($module)
defined $server $server->have_module($module)
Truth table0%0/3
A and B
A= defined $serverB= $server->have_module($module)
ABresult
0X0
100
111
94 0 my ($method, $call);
95
96 0 ▶ if (exists $REST_CRUD{$request->method}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Lim::RPC::Protocol::REST::REST_CRUD{$request->method}) { } 0 0 0
97 0 $method = lc($REST_CRUD{$request->method});
98 }
99 else {
100 0 $method = lc($request->method);
101 }
102 0 $function = lc($function);
103 0 $call = ucfirst($method).Lim::Util::Camelize($function);
104  
105 0 my $obj;
106 0 ▶ if ($server->have_module_call($module, $call)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($server->have_module_call($module, $call)) 0 0 0
107 0 $obj = $server->module_obj_by_protocol($module, $self->name);
108 }
109
110 0 my ($query, $jsonp);
111 0 ▶ if (defined $obj) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $obj) 0 0 0
112 0 ▶ Lim::DEBUG and $self->{logger}->debug('API call ', $module, '->', $call, '()');
Branch: 1 branch
BranchTrueFalseTotal
Lim::DEBUG() and $self->{"logger"}->debug("API call ", $module, "->", $call, "()") 0 0 0
113
114 0 ▶ if ($request->header('Content-Type') =~ /(?:^|\s)application\/x-www-form-urlencoded(?:$|\s|;)/o) {
Branch: 2 branches
BranchTrueFalseTotal
if ($request->header('Content-Type') =~ m[(?:^|\s)application/x-www-form-urlencoded(?:$|\s|;)]ou) { } 0 0 0
elsif ($request->header('Content-Type') =~ m[(?:^|\s)application/json(?:$|\s|;)]ou) { } 0 0 0
115 0 my $query_str = $request->content;
116 0 $query_str =~ s/[\015\012]+$//o;
117  
118 0 $query = Lim::Util::QueryDecode($query_str);
119 }
120 elsif ($request->header('Content-Type') =~ /(?:^|\s)application\/json(?:$|\s|;)/o) {
121 0 eval {
122 0 $query = $JSON->decode($request->content);
123 };
124 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
125 0 $response->code(HTTP_INTERNAL_SERVER_ERROR);
126 0 undef($query);
127 0 undef($obj);
128 }
129 }
130 else {
131 0 $query = Lim::Util::QueryDecode($request->uri->query);
132 }
133
134 0 $jsonp = delete $query->{jsonpCallback};
135
136 0 ▶ if (defined $parameters) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $parameters) 0 0 0
137 0 my $redirect_call = $server->process_module_call_uri_map($module, $call, $parameters, $query);
138
139 0 ▶ if (defined $redirect_call and $redirect_call) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $redirect_call and $redirect_call) 0 0 0
Condition0%0/3
defined $redirect_call and $redirect_call
!ll&&!rl&&r
000
MC/DC0%0/2
defined $redirect_call and $redirect_call
defined $redirect_call $redirect_call
Truth table0%0/3
A and B
A= defined $redirect_callB= $redirect_call
ABresult
0X0
100
111
140 0 ▶ Lim::DEBUG and $self->{logger}->debug('API call redirected ', $call, ' => ', $redirect_call);
Branch: 1 branch
BranchTrueFalseTotal
Lim::DEBUG() and $self->{"logger"}->debug("API call redirected ", $call, " => ", $redirect_call) 0 0 0
141 0 $call = $redirect_call;
142 }
143 }
144 }
145
146 0 ▶ if (defined $obj) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $obj) 0 0 0
147 0 my $real_self = $self;
148 0 weaken($self);
149 $obj->$call(Lim::RPC::Callback->new(
150 cb => sub {
151 0 ▶ my ($result) = @_;
Subroutine __ANON__: not called CC=11 SCAR=48.8
152
153 0 ▶ unless (defined $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self) 0 0 0
154 0 return;
155 }
156
157 0 ▶ if (blessed $result and $result->isa('Lim::Error')) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($result) and $result->isa('Lim::Error')) { } 0 0 0
elsif (ref $result eq 'HASH') { } 0 0 0
Condition0%0/3
builtin::blessed($result) and $result->isa("Lim::Error")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($result) and $result->isa("Lim::Error")
builtin::blessed($result) $result->isa("Lim::Error")
Truth table0%0/3
A and B
A= builtin::blessed($result)B= $result->isa("Lim::Error")
ABresult
0X0
100
111
158 0 $response->code($result->code);
159 0 eval {
160 0 $response->content($JSON->encode($result));
161 };
162 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
163 0 $response->code(HTTP_INTERNAL_SERVER_ERROR);
164 0 ▶ Lim::WARN and $self->{logger}->warn('JSON encode error: ', $@);
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn("JSON encode error: ", $@) 0 0 0
165 }
166 else {
167 0 $response->header(
168 'Content-Type' => 'application/json; charset=utf-8',
169 'Cache-Control' => 'no-cache',
170 'Pragma' => 'no-cache'
171 );
172  
173 0 ▶ if (defined $jsonp) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $jsonp) 0 0 0
174 0 $response->content($jsonp.'('.$response->content().');');
175 0 $response->header('Content-Type' => 'application/javascript; charset=utf-8');
176 }
177 }
178 }
179 elsif (ref($result) eq 'HASH') {
180 0 eval {
181 0 $response->content($JSON->encode($result));
182 };
183 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
184 0 $response->code(HTTP_INTERNAL_SERVER_ERROR);
185 0 ▶ Lim::WARN and $self->{logger}->warn('JSON encode error: ', $@);
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn("JSON encode error: ", $@) 0 0 0
186 }
187 else {
188 0 $response->header(
189 'Content-Type' => 'application/json; charset=utf-8',
190 'Cache-Control' => 'no-cache',
191 'Pragma' => 'no-cache'
192 );
193 0 $response->code(HTTP_OK);
194  
195 0 ▶ if (defined $jsonp) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $jsonp) 0 0 0
196 0 $response->content($jsonp.'('.$response->content().');');
197 0 $response->header('Content-Type' => 'application/javascript; charset=utf-8');
198 }
199 }
200 }
201 else {
202 0 $response->code(HTTP_INTERNAL_SERVER_ERROR);
203 }
204
205 0 $cb->cb->($response);
206 0 return;
207 },
208 reset_timeout => sub {
209 0 ▶ $cb->reset_timeout;
Subroutine __ANON__: not called CC=1 SCAR=6.9
210 0 }), $query);
211 0 return 1;
212 }
213 }
214 else {
215 0 return;
216 }
217  
218 0 $cb->cb->($response);
219 0 return 1;
220 }
221 0 return;
222 }
223  
224 =head1 AUTHOR
225  
226 Jerry Lundström, C<< <lundstrom.jerry at gmail.com> >>
227  
228 =head1 BUGS
229  
230 Please report any bugs or feature requests to L<https://github.com/jelu/lim/issues>.
231  
232 =head1 SUPPORT
233  
234 You can find documentation for this module with the perldoc command.
235  
236 perldoc Lim
237  
238 You can also look for information at:
239  
240 =over 4
241  
242 =item * Lim issue tracker (report bugs here)
243  
244 L<https://github.com/jelu/lim/issues>
245  
246 =back
247  
248 =head1 ACKNOWLEDGEMENTS
249  
250 =head1 LICENSE AND COPYRIGHT
251  
252 Copyright 2012-2013 Jerry Lundström.
253  
254 This program is free software; you can redistribute it and/or modify it
255 under the terms of either: the GNU General Public License as published
256 by the Free Software Foundation; or the Artistic License.
257  
258 See http://dev.perl.org/licenses/ for more information.
259  
260  
261 =cut
262  
263 5 1; # End of Lim::RPC::Protocol::REST
« Lim/Component.pm ↑ Summary Lim/RPC/Protocol/HTTP.pm »