Lim/RPC/Transport/HTTP.pm

filter: Statementstmt 72.3% 128 / 177 Branchbran 40.7% 35 / 86 Conditioncond 33.3% 7 / 21 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 82.7% 24 / 29 Podpod 100% 6 / 6 totaltotal 60.0% 200 / 333 SCAR 54.2
CC
52
Cov
60%
__ANON__
35.2
__ANON__
27.6
__ANON__
14.7
CRAP 226.9
« Lim/RPC/Value/Collection.pm ↑ Summary Lim/RPC.pm »
1 package Lim::RPC::Transport::HTTP;
2  
3 5 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 ▶use Scalar::Util qw(blessed weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 ▶use AnyEvent ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use AnyEvent::Socket ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 ▶use HTTP::Status qw(:constants);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use HTTP::Request ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use HTTP::Response ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use URI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 5 ▶use Lim ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use Lim::RPC::TLS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 ▶use Lim::RPC::Callback ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 5 ▶use base qw(Lim::RPC::Transport);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 =encoding utf8
23  
24 =head1 NAME
25  
26 ...
27  
28 =head1 VERSION
29  
30 See L<Lim> for version.
31  
32 =over 4
33  
34 =item MAX_REQUEST_LEN
35  
36 =back
37  
38 =cut
39  
40 5 our $VERSION = $Lim::VERSION;
41  
42 sub MAX_REQUEST_LEN (){ 8 * 1024 * 1024 }
43  
44 =head1 SYNOPSIS
45  
46 ...
47  
48 =head1 SUBROUTINES/METHODS
49  
50 =head2 Init
51  
52 =cut
53  
54 sub Init {
55 4 ▶ my $self = shift;
Subroutine Init: called CC=6 SCAR=22.9
Pod documented
56 4 my %args = ( @_ );
57 4 my $real_self = $self;
58 4 weaken($self);
59  
60 4 $self->{client} = {};
61 4 $self->{host} = Lim::Config->{rpc}->{transport}->{http}->{host};
62 4 $self->{port} = Lim::Config->{rpc}->{transport}->{http}->{port};
63  
64 4 ▶ if (exists $args{uri}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"uri"}) 4 0 4
65 4 ▶ unless (blessed($args{uri}) and $args{uri}->isa('URI')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($args{"uri"}) and $args{"uri"}->isa("URI")) 0 4 4
Condition33%1/3
builtin::blessed($args{"uri"}) and $args{"uri"}->isa("URI")
!ll&&!rl&&r
004
MC/DC0%0/2
builtin::blessed($args{"uri"}) and $args{"uri"}->isa("URI")
builtin::blessed($args{"uri"}) $args{"uri"}->isa("URI")
Truth table33%1/3
A and B
A= builtin::blessed($args{"uri"})B= $args{"uri"}->isa("URI")
ABresult
0X0
100
111
66 0 confess 'uri argument is not a URI class';
67 }
68
69 4 $self->{host} = $args{uri}->host;
70 4 $self->{port} = $args{uri}->port;
71 }
72
73 4 ▶ if ($self->isa('Lim::RPC::Transport::HTTPS') and !defined Lim::RPC::TLS->instance->tls_ctx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->isa("Lim::RPC::Transport::HTTPS") and not defined "Lim::RPC::TLS"->instance->tls_ctx) 0 4 4
Condition33%1/3
$self->isa("Lim::RPC::Transport::HTTPS") and not defined "Lim::RPC::TLS"->instance->tls_ctx
!ll&&!rl&&r
400
MC/DC0%0/2
$self->isa("Lim::RPC::Transport::HTTPS") and not defined "Lim::RPC::TLS"->instance->tls_ctx
$self->isa("Lim::RPC::Transport::HTTPS") not defined "Lim::RPC::TLS"->instance->tls_ctx
Truth table33%1/3
A and not(B)
A= $self->isa("Lim::RPC::Transport::HTTPS")B= not defined "Lim::RPC::TLS"->instance->tls_ctx
ABresult
0X0
100
111
74 0 confess 'using HTTPS but can not create TLS context';
75 }
76
77 $self->{socket} = AnyEvent::Socket::tcp_server $self->{host}, $self->{port}, sub {
78 4 ▶ my ($fh, $host, $port) = @_;
Subroutine __ANON__: called CC=3 SCAR=14.3
79  
80 4 ▶ Lim::RPC_DEBUG and $self->{logger}->debug('Connection from ', $host, ':', $port);
Branch: 1 branch
BranchTrueFalseTotal
Lim::RPC_DEBUG() and $self->{"logger"}->debug("Connection from ", $host, ":", $port) 4 0 4
81  
82 4 my $handle;
83 $handle = AnyEvent::Handle->new(
84 fh => $fh,
85 ($self->isa('Lim::RPC::Transport::HTTPS') ? (tls => 'accept', tls_ctx => Lim::RPC::TLS->instance->tls_ctx) : ()),
86 # timeout => Lim::Config->{rpc}->{timeout},
87 on_error => sub {
88 0 ▶ my ($handle, $fatal, $message) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
89
90 0 ▶ unless (defined $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self) 0 0 0
91 0 return;
92 }
93  
94 0 ▶ Lim::WARN and $self->{logger}->warn($handle, ' Error: ', $message);
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn($handle, " Error: ", $message) 0 0 0
95
96 0 delete $self->{client}->{$handle};
97 0 $handle->destroy;
98 },
99 on_timeout => sub {
100 0 ▶ my ($handle) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
101
102 0 ▶ unless (defined $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self) 0 0 0
103 0 return;
104 }
105  
106 0 ▶ Lim::WARN and $self->{logger}->warn($handle, ' TIMEOUT');
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn($handle, " TIMEOUT") 0 0 0
107
108 # my $client = $self->{client}->{$handle};
109 #
110 # if (defined $client) {
111 # if (exists $client->{processing} and exists $client->{protocol}) {
112 # $client->{protocol}->timeout($client->{request});
113 # }
114 # }
115
116 0 delete $self->{client}->{$handle};
117 0 $handle->destroy;
118 },
119 on_eof => sub {
120 4 ▶ my ($handle) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.8
121
122 4 ▶ unless (defined $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self) 0 4 4
123 0 return;
124 }
125  
126 4 ▶ Lim::RPC_DEBUG and $self->{logger}->debug($handle, ' EOF');
Branch: 1 branch
BranchTrueFalseTotal
Lim::RPC_DEBUG() and $self->{"logger"}->debug($handle, " EOF") 4 0 4
127
128 4 delete $self->{client}->{$handle};
129 4 $handle->destroy;
130 },
131 on_drain => sub {
132 8 ▶ my ($handle) = @_;
Subroutine __ANON__: called CC=3 SCAR=13.3
133  
134 8 ▶ unless (defined $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self) 0 8 8
135 0 return;
136 }
137  
138 8 ▶ if ($self->{client}->{$handle}->{close}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"client"}{$handle}{"close"}) 0 8 8
139 0 shutdown $handle->{fh}, 2;
140 }
141 },
142 on_read => sub {
143 4 ▶ my ($handle) = @_;
Subroutine __ANON__: called CC=14 SCAR=35.6
144  
145 4 ▶ unless (defined $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self) 0 4 4
146 0 return;
147 }
148
149 4 my $client = $self->{client}->{$handle};
150
151 4 ▶ unless (defined $client) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $client) 0 4 4
152 0 ▶ Lim::WARN and $self->{logger}->warn($handle, ' unknown client');
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn($handle, " unknown client") 0 0 0
153 0 $handle->push_shutdown;
154 0 $handle->destroy;
155 0 return;
156 }
157
158 4 ▶ if (exists $client->{process_watcher}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $client->{"process_watcher"}) 0 4 4
159 0 ▶ Lim::WARN and $self->{logger}->warn($handle, ' Request received while processing other request');
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn($handle, " Request received while processing other request") 0 0 0
160 0 $handle->push_shutdown;
161 0 $handle->destroy;
162 0 return;
163 }
164
165 4 ▶ if ((length($client->{headers}) + (exists $client->{content} ? length($client->{content}) : 0) + length($client->{rbuf})) > MAX_REQUEST_LEN) {
Branch: 2 branches
BranchTrueFalseTotal
if (length($client->{"headers"}) + (exists $client->{"content"} ? length $client->{"content"} : 0) + length($client->{"rbuf"}) > 8388608) 0 4 4
exists $client->{'content'} ? : 0 4 4
166 0 ▶ Lim::WARN and $self->{logger}->warn($handle, ' Request too long');
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn($handle, " Request too long") 0 0 0
167 0 $handle->push_shutdown;
168 0 $handle->destroy;
169 0 return;
170 }
171
172 4 ▶ unless (exists $client->{content}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $client->{'content'}) { } 0 4 4
173 4 $client->{headers} .= $handle->{rbuf};
174
175 4 ▶ if ($client->{headers} =~ /\015?\012\015?\012/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($client->{"headers"} =~ /\015?\012\015?\012/ou) 4 0 4
176 4 my ($headers, $content) = split(/\015?\012\015?\012/o, $client->{headers}, 2);
177 4 $client->{headers} = $headers;
178 4 $client->{content} = $content;
179 4 $client->{request} = HTTP::Request->parse($client->{headers});
180 }
181 }
182 else {
183 0 $client->{content} .= $handle->{rbuf};
184 }
185 4 $handle->{rbuf} = '';
186
187 4 ▶ if (defined $client->{request} and length($client->{content}) == $client->{request}->header('Content-Length')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $client->{"request"} and length $client->{"content"} == $client->{"request"}->header("Content-Length")) 4 0 4
Condition33%1/3
defined $client->{"request"} and length $client->{"content"} == $client->{"request"}->header("Content-Length")
!ll&&!rl&&r
004
MC/DC0%0/2
defined $client->{"request"} and length $client->{"content"} == $client->{"request"}->header("Content-Length")
defined $client->{"request"} length $client->{"content"} == $client->{"request"}->header("Content-Length")
Truth table33%1/3
A and B
A= defined $client->{"request"}B= length $client->{"content"} == $client->{"request"}->header("Content-Length")
ABresult
0X0
100
111
188 4 $client->{request}->content($client->{content});
189 4 delete $client->{content};
190 4 $client->{headers} = '';
191
192 4 ▶ Lim::RPC_DEBUG and $self->{logger}->debug('HTTP Request: ', $client->{request}->as_string);
Branch: 1 branch
BranchTrueFalseTotal
Lim::RPC_DEBUG() and $self->{"logger"}->debug("HTTP Request: ", $client->{"request"}->as_string) 4 0 4
193
194 4 $client->{processing} = 1;
195 # $handle->timeout(Lim::Config->{rpc}->{call_timeout});
196 4 my $real_client = $client;
197 4 weaken($client);
198 $client->{process_watcher} = AnyEvent->timer(
199 after => 0,
200 cb => sub {
201 4 ▶ unless (defined $self and defined $client) {
Subroutine __ANON__: called CC=8 SCAR=26.9
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self and defined $client) 0 4 4
Condition33%1/3
defined $self and defined $client
!ll&&!rl&&r
004
MC/DC0%0/2
defined $self and defined $client
defined $self defined $client
Truth table33%1/3
A and B
A= defined $selfB= defined $client
ABresult
0X0
100
111
202 0 return;
203 }
204  
205 my $cb = Lim::RPC::Callback->new(
206 cb => sub {
207 6 ▶ my ($response) = @_;
Subroutine __ANON__: called CC=14 SCAR=33.2
208
209 6 ▶ unless (defined $self and defined $client) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self and defined $client) 0 6 6
Condition33%1/3
defined $self and defined $client
!ll&&!rl&&r
006
MC/DC0%0/2
defined $self and defined $client
defined $self defined $client
Truth table33%1/3
A and B
A= defined $selfB= defined $client
ABresult
0X0
100
111
210 0 return;
211 }
212
213 6 ▶ unless (exists $client->{processing}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $client->{"processing"}) 2 4 6
214 2 return;
215 }
216  
217 4 ▶ unless (blessed($response) and $response->isa('HTTP::Response')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($response) and $response->isa("HTTP::Response")) 0 4 4
Condition33%1/3
builtin::blessed($response) and $response->isa("HTTP::Response")
!ll&&!rl&&r
004
MC/DC0%0/2
builtin::blessed($response) and $response->isa("HTTP::Response")
builtin::blessed($response) $response->isa("HTTP::Response")
Truth table33%1/3
A and B
A= builtin::blessed($response)B= $response->isa("HTTP::Response")
ABresult
0X0
100
111
218 0 return;
219 }
220  
221 4 ▶ unless ($response->code) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->code) 0 4 4
222 0 $response->code(HTTP_NOT_FOUND);
223 }
224
225 4 ▶ if ($response->code != HTTP_OK and !length($response->content)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->code != 200 and not length $response->content) 0 4 4
Condition33%1/3
$response->code != 200 and not length $response->content
!ll&&!rl&&r
400
MC/DC0%0/2
$response->code != 200 and not length $response->content
$response->code != 200 not length $response->content
Truth table33%1/3
A and not(B)
A= $response->code != 200B= not length $response->content
ABresult
0X0
100
111
226 0 $response->header('Content-Type' => 'text/plain; charset=utf-8');
227 0 $response->content($response->code.' '.HTTP::Status::status_message($response->code)."\015\012");
228 }
229  
230 4 $response->header('Content-Length' => length($response->content));
231 4 ▶ unless (defined $response->header('Content-Type')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $response->header("Content-Type")) 0 4 4
232 0 $response->header('Content-Type' => 'text/html; charset=utf-8');
233 }
234
235 4 ▶ unless ($response->protocol) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->protocol) 2 2 4
236 2 $response->protocol('HTTP/1.1');
237 }
238
239 4 ▶ Lim::RPC_DEBUG and $self->{logger}->debug('HTTP Response: ', $response->as_string);
Branch: 1 branch
BranchTrueFalseTotal
Lim::RPC_DEBUG() and $self->{"logger"}->debug("HTTP Response: ", $response->as_string) 4 0 4
240  
241 4 ▶ if ($client->{request}->header('Connection') eq 'close') {
Branch: 1 branch
BranchTrueFalseTotal
if ($client->{'request'}->header('Connection') eq 'close') { } 0 4 4
242 0 ▶ Lim::RPC_DEBUG and $self->{logger}->debug('Connection requested to be closed');
Branch: 1 branch
BranchTrueFalseTotal
Lim::RPC_DEBUG() and $self->{"logger"}->debug("Connection requested to be closed") 0 0 0
243 # $client->{handle}->timeout(0);
244 0 $client->{close} = 1;
245 }
246 else {
247 # $client->{handle}->timeout(Lim::Config->{rpc}->{timeout});
248 }
249 4 $client->{handle}->push_write($response->as_string("\015\012"));
250
251 4 delete $client->{processing};
252 4 delete $client->{request};
253 4 delete $client->{response};
254 4 delete $client->{process_watcher};
255 4 delete $client->{protocol};
256 },
257 reset_timeout => sub {
258 0 ▶ unless (defined $client) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $client) 0 0 0
259 0 return;
260 }
261
262 # $client->{handle}->timeout_reset;
263 4 });
264
265 4 foreach my $protocol ($self->protocols) {
266 4 ▶ Lim::RPC_DEBUG and $self->{logger}->debug('Trying protocol ', $protocol->name);
Branch: 1 branch
BranchTrueFalseTotal
Lim::RPC_DEBUG() and $self->{"logger"}->debug("Trying protocol ", $protocol->name) 4 0 4
267 4 ▶ if ($protocol->handle($cb, $client->{request}, $self)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($protocol->handle($cb, $client->{"request"}, $self)) 4 0 4
268 4 $client->{protocol} = $protocol;
269 4 ▶ Lim::RPC_DEBUG and $self->{logger}->debug('Request handled by protocol ', $protocol->name);
Branch: 1 branch
BranchTrueFalseTotal
Lim::RPC_DEBUG() and $self->{"logger"}->debug("Request handled by protocol ", $protocol->name) 4 0 4
270 4 return;
271 }
272 }
273 0 ▶ Lim::RPC_DEBUG and $self->{logger}->debug('Did not find any protocol handler for request');
Branch: 1 branch
BranchTrueFalseTotal
Lim::RPC_DEBUG() and $self->{"logger"}->debug("Did not find any protocol handler for request") 0 0 0
274 0 my $response = HTTP::Response->new;
275 0 $response->request($client->{request});
276 0 $response->protocol($client->{request}->protocol);
277 0 $cb->cb->($response);
278 4 });
279 }
280 4 ▶ });
Branch: 1 branch
BranchTrueFalseTotal
$self->isa('Lim::RPC::Transport::HTTPS') ? : 0 4 4
281  
282 4 $self->{client}->{$handle} = {
283 handle => $handle,
284 headers => '',
285 close => 0
286 };
287 }, sub {
288 4 ▶ my (undef, $host, $port) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.2
289
290 4 ▶ Lim::RPC_DEBUG and $self->{logger}->debug(__PACKAGE__, ' ', $self, ' ready at ', $host, ':', $port);
Branch: 1 branch
BranchTrueFalseTotal
Lim::RPC_DEBUG() and $self->{"logger"}->debug(__PACKAGE__, " ", $self, " ready at ", $host, ":", $port) 4 0 4
291
292 4 $self->{real_host} = $host;
293 4 $self->{real_port} = $port;
294
295 4 ▶ $self->{uri} = URI->new(
Branch: 1 branch
BranchTrueFalseTotal
$self->isa('Lim::RPC::Transport::HTTPS') ? : 0 4 4
296 ($self->isa('Lim::RPC::Transport::HTTPS') ? 'https://' : 'http://').
297 $host.':'.$port);
298 4 $Lim::CONFIG->{rpc}->{srv_listen};
299 4 };
300 }
301  
302 =head2 Destroy
303  
304 =cut
305  
306 sub Destroy {
307 4 ▶ my ($self) = @_;
Subroutine Destroy: called CC=1 SCAR=0.0
Pod documented
308
309 4 delete $self->{client};
310 4 delete $self->{socket};
311 }
312  
313 =head2 name
314  
315 =cut
316  
317 sub name {
318 4 ▶ 'http';
Subroutine name: called CC=1 SCAR=0.0
Pod documented
319 }
320  
321 =head2 uri
322  
323 =cut
324  
325 sub uri {
326 0 ▶ $_[0]->{uri};
Subroutine uri: not called CC=1 SCAR=6.9
Pod documented
327 }
328  
329 =head2 host
330  
331 =cut
332  
333 sub host {
334 0 ▶ $_[0]->{real_host};
Subroutine host: not called CC=1 SCAR=6.9
Pod documented
335 }
336  
337 =head2 port
338  
339 =cut
340  
341 sub port {
342 4 ▶ $_[0]->{real_port};
Subroutine port: called CC=1 SCAR=0.0
Pod documented
343 }
344  
345 =head1 AUTHOR
346  
347 Jerry Lundström, C<< <lundstrom.jerry at gmail.com> >>
348  
349 =head1 BUGS
350  
351 Please report any bugs or feature requests to L<https://github.com/jelu/lim/issues>.
352  
353 =head1 SUPPORT
354  
355 You can find documentation for this module with the perldoc command.
356  
357 perldoc Lim
358  
359 You can also look for information at:
360  
361 =over 4
362  
363 =item * Lim issue tracker (report bugs here)
364  
365 L<https://github.com/jelu/lim/issues>
366  
367 =back
368  
369 =head1 ACKNOWLEDGEMENTS
370  
371 =head1 LICENSE AND COPYRIGHT
372  
373 Copyright 2012-2013 Jerry Lundström.
374  
375 This program is free software; you can redistribute it and/or modify it
376 under the terms of either: the GNU General Public License as published
377 by the Free Software Foundation; or the Artistic License.
378  
379 See http://dev.perl.org/licenses/ for more information.
380  
381  
382 =cut
383  
384 5 1; # End of Lim::RPC::Transport::HTTP
« Lim/RPC/Value/Collection.pm ↑ Summary Lim/RPC.pm »