Lim/RPC/Protocol/HTTP.pm

filter: Statementstmt 33.9% 40 / 118 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 76.4% 13 / 17 Podpod 100% 5 / 5 totaltotal 29.2% 58 / 198 CC 25 SCAR 59.8
CC
25
Cov
24%
handle
650.0
BEGIN
1.0
BEGIN
1.0
CRAP 396.7
« Lim/Plugins.pm ↑ Summary Lim/Util/DBI.pm »
1 package Lim::RPC::Protocol::HTTP;
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 LWP::MediaTypes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Fcntl ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use JSON::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 5 ▶use Lim ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use Lim::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use Lim::RPC::Callback ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 5 ▶use base qw(Lim::RPC::Protocol);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 =encoding utf8
21  
22 =head1 NAME
23  
24 ...
25  
26 =head1 VERSION
27  
28 See L<Lim> for version.
29  
30 =cut
31  
32 5 our $VERSION = $Lim::VERSION;
33 5 our $JSON = JSON::XS->new->utf8->convert_blessed;
34  
35 =head1 SYNOPSIS
36  
37 ...
38  
39 =head1 SUBROUTINES/METHODS
40  
41 =head2 Init
42  
43 =cut
44  
45 0 ▶sub Init {
Subroutine Init: not called CC=1 SCAR=0.0
Pod documented
46 }
47  
48 =head2 Destroy
49  
50 =cut
51  
52 0 ▶sub Destroy {
Subroutine Destroy: not called CC=1 SCAR=0.0
Pod documented
53 }
54  
55 =head2 name
56  
57 =cut
58  
59 4 ▶sub name {
Subroutine name: called CC=1 SCAR=0.0
Pod documented
60 4 'http';
61 }
62  
63 =head2 serve
64  
65 =cut
66  
67 0 ▶sub serve {
Subroutine serve: not called CC=1 SCAR=0.0
Pod documented
68 }
69  
70 =head2 handle
71  
72 =cut
73  
74 0 ▶sub handle {
Subroutine handle: not called CC=25 SCAR=64.8
Pod documented
75 0 my ($self, $cb, $request) = @_;
76
77 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
78 0 return;
79 }
80
81 0 ▶ unless (defined Lim::Config->{protocol}->{http}->{webroot}
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &Lim::Config()->{"protocol"}{"http"}{"webroot"} and -d &Lim::Config()->{"protocol"}{"http"}{"webroot"}) 0 0 0
Condition0%0/3
defined &Lim::Config()->{"protocol"}{"http"}{"webroot"} and -d &Lim::Config()->{"protocol"}{"http"}{"webroot"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined &Lim::Config()->{"protocol"}{"http"}{"webroot"} and -d &Lim::Config()->{"protocol"}{"http"}{"webroot"}
defined &Lim::Config()->{"protocol"}{"http"}{"webroot"} -d &Lim::Config()->{"protocol"}{"http"}{"webroot"}
Truth table0%0/3
A and B
A= defined &Lim::Config()->{"protocol"}{"http"}{"webroot"}B= -d &Lim::Config()->{"protocol"}{"http"}{"webroot"}
ABresult
0X0
100
111
82 and -d Lim::Config->{protocol}->{http}->{webroot})
83 {
84 0 return;
85 }
86  
87 0 my (@path, $path, $file);
88 0 foreach $path (split(/\//o, $request->uri->path)) {
89 0 ▶ if ($path eq '..') {
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq "..") 0 0 0
90 0 ▶ unless (scalar @path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @path) 0 0 0
91 0 return;
92 }
93 0 pop(@path);
94 0 next;
95 }
96 0 ▶ if ($path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path) 0 0 0
97 0 push(@path, $path);
98 }
99 }
100  
101 0 $file = pop(@path);
102 0 $path = join('/', Lim::Config->{protocol}->{http}->{webroot}, @path);
103  
104 0 ▶ if (-d $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $path) 0 0 0
105 0 my $response = HTTP::Response->new;
106 0 $response->request($request);
107 0 $response->protocol($request->protocol);
108
109 0 ▶ unless (defined $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file) 0 0 0
110 0 $file = 'index.html';
111 }
112 0 $path .= '/'.$file;
113
114 0 ▶ if (-d $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $path) 0 0 0
115 0 $path .= '/index.html';
116 }
117
118 0 ▶ unless (-r $path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-r $path) 0 0 0
119 0 return;
120 }
121  
122 0 my $query;
123 0 ▶ if ($request->header('Content-Type') =~ /(?:^|\s)application\/x-www-form-urlencoded(?:$|\s|;)/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($request->header('Content-Type') =~ m[(?:^|\s)application/x-www-form-urlencoded(?:$|\s|;)]ou) { } 0 0 0
124 0 my $query_str = $request->content;
125 0 $query_str =~ s/[\015\012]+$//o;
126  
127 0 $query = Lim::Util::QueryDecode($query_str);
128 }
129 else {
130 0 $query = Lim::Util::QueryDecode($request->uri->query);
131 }
132
133 0 ▶ Lim::DEBUG and $self->{logger}->debug('Serving file ', $path);
Branch: 1 branch
BranchTrueFalseTotal
Lim::DEBUG() and $self->{"logger"}->debug("Serving file ", $path) 0 0 0
134  
135 0 ▶ unless (sysopen(FILE, $path, Fcntl::O_RDONLY)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (sysopen FILE, $path, 0) 0 0 0
136 0 $response->code(HTTP_FORBIDDEN);
137 0 $cb->cb->($response);
138 0 return 1;
139 }
140  
141 0 binmode(FILE);
142
143 0 my ($size, $mtime) = (stat(FILE))[7,9];
144 0 ▶ unless (defined $size and defined $mtime) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $size and defined $mtime) 0 0 0
Condition0%0/3
defined $size and defined $mtime
!ll&&!rl&&r
000
MC/DC0%0/2
defined $size and defined $mtime
defined $size defined $mtime
Truth table0%0/3
A and B
A= defined $sizeB= defined $mtime
ABresult
0X0
100
111
145 0 $response->code(HTTP_INTERNAL_SERVER_ERROR);
146 0 $cb->cb->($response);
147 0 return 1;
148 }
149
150 0 ▶ if (defined $query->{jsonpCallback}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $query->{"jsonpCallback"}) 0 0 0
151 0 my ($content, $buf);
152 0 ▶ while (sysread(FILE, $buf, 64*1024)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sysread FILE, $buf, 65536) 0 0 0
153 0 $content .= $buf;
154 }
155 0 close(FILE);
156
157 0 eval {
158 0 $content = $JSON->encode({ content => $content });
159 };
160 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
161 0 $response->code(HTTP_INTERNAL_SERVER_ERROR);
162 0 $cb->cb->($response);
163 0 return 1;
164 }
165  
166 0 $response->header('Content-Type' => 'application/javascript; charset=utf-8');
167 0 $response->content($query->{jsonpCallback}.'('.$content.');');
168 0 $response->code(HTTP_OK);
169  
170 0 $cb->cb->($response);
171 0 return 1;
172 }
173
174 0 ▶ unless (LWP::MediaTypes::guess_media_type($path, $response)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (LWP::MediaTypes::guess_media_type($path, $response)) 0 0 0
175 0 $response->code(HTTP_INTERNAL_SERVER_ERROR);
176 0 $cb->cb->($response);
177 0 return 1;
178 }
179  
180 0 ▶ if ($request->header('If-Modified-Since')
Branch: 1 branch
BranchTrueFalseTotal
if ($request->header("If-Modified-Since") and $request->header("If-Modified-Since") >= $mtime) 0 0 0
Condition0%0/3
$request->header("If-Modified-Since") and $request->header("If-Modified-Since") >= $mtime
!ll&&!rl&&r
000
MC/DC0%0/2
$request->header("If-Modified-Since") and $request->header("If-Modified-Since") >= $mtime
$request->header("If-Modified-Since") $request->header("If-Modified-Since") >= $mtime
Truth table0%0/3
A and B
A= $request->header("If-Modified-Since")B= $request->header("If-Modified-Since") >= $mtime
ABresult
0X0
100
111
181 and $request->header('If-Modified-Since') >= $mtime)
182 {
183 0 close(FILE);
184 0 $response->code(HTTP_NOT_MODIFIED);
185 0 $cb->cb->($response);
186 0 return 1;
187 }
188  
189 $response->header(
190 0 'Content-Length' => $size,
191 'Last-Modified' => $mtime
192 );
193  
194 0 my $buf;
195 0 ▶ while (sysread(FILE, $buf, 64*1024)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sysread FILE, $buf, 65536) 0 0 0
196 0 $response->add_content($buf);
197 }
198 0 close(FILE);
199
200 0 $response->code(HTTP_OK);
201  
202 0 $cb->cb->($response);
203 0 return 1;
204 }
205 0 return;
206 }
207  
208 =head1 AUTHOR
209  
210 Jerry Lundström, C<< <lundstrom.jerry at gmail.com> >>
211  
212 =head1 BUGS
213  
214 Please report any bugs or feature requests to L<https://github.com/jelu/lim/issues>.
215  
216 =head1 SUPPORT
217  
218 You can find documentation for this module with the perldoc command.
219  
220 perldoc Lim
221  
222 You can also look for information at:
223  
224 =over 4
225  
226 =item * Lim issue tracker (report bugs here)
227  
228 L<https://github.com/jelu/lim/issues>
229  
230 =back
231  
232 =head1 ACKNOWLEDGEMENTS
233  
234 =head1 LICENSE AND COPYRIGHT
235  
236 Copyright 2012-2013 Jerry Lundström.
237  
238 This program is free software; you can redistribute it and/or modify it
239 under the terms of either: the GNU General Public License as published
240 by the Free Software Foundation; or the Artistic License.
241  
242 See http://dev.perl.org/licenses/ for more information.
243  
244  
245 =cut
246  
247 5 1; # End of Lim::RPC::Protocol::HTTP
« Lim/Plugins.pm ↑ Summary Lim/Util/DBI.pm »