Lim/RPC/Transport/Client/HTTP.pm

filter: Statementstmt 31.1% 47 / 151 Branchbran 0.0% 0 / 62 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 60.0% 15 / 25 Podpod 100% 3 / 3 totaltotal 24.4% 65 / 266 CC 38 SCAR 36.9
CC
38
Cov
21%
__ANON__
110.0
request
72.0
__ANON__
42.0
CRAP 39.9
« Lim/RPC/Protocol/JSONRPC1.pm ↑ Summary Lim/RPC/URIMaps.pm »
1 package Lim::RPC::Transport::Client::HTTP;
2  
3 1 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Log::Log4perl ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Scalar::Util qw(blessed weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use AnyEvent ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use AnyEvent::Socket ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use AnyEvent::Handle ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use HTTP::Request ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use HTTP::Response ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use HTTP::Status qw(:constants);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 ▶use Lim ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Lim::Error ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Lim::RPC::TLS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use Lim::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 ▶use base qw(Lim::RPC::Transport::Client);
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 =encoding utf8
25  
26 =head1 NAME
27  
28 ...
29  
30 =head1 VERSION
31  
32 See L<Lim> for version.
33  
34 =over 4
35  
36 =item MAX_RESPONSE_LEN
37  
38 =back
39  
40 =cut
41  
42 1 our $VERSION = $Lim::VERSION;
43  
44 sub MAX_RESPONSE_LEN (){ 8 * 1024 * 1024 }
45  
46 =head1 SYNOPSIS
47  
48 ...
49  
50 =head1 SUBROUTINES/METHODS
51  
52 =head2 Init
53  
54 =cut
55  
56 0 ▶sub Init {
Subroutine Init: not called CC=3 SCAR=24.8
Pod documented
57 0 my ($self) = @_;
58  
59 0 ▶ if ($self->isa('Lim::RPC::Transport::Client::HTTPS') and !defined Lim::RPC::TLS->instance->tls_ctx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->isa("Lim::RPC::Transport::Client::HTTPS") and not defined "Lim::RPC::TLS"->instance->tls_ctx) 0 0 0
Condition0%0/3
$self->isa("Lim::RPC::Transport::Client::HTTPS") and not defined "Lim::RPC::TLS"->instance->tls_ctx
!ll&&!rl&&r
000
MC/DC0%0/2
$self->isa("Lim::RPC::Transport::Client::HTTPS") and not defined "Lim::RPC::TLS"->instance->tls_ctx
$self->isa("Lim::RPC::Transport::Client::HTTPS") not defined "Lim::RPC::TLS"->instance->tls_ctx
Truth table0%0/3
A and not(B)
A= $self->isa("Lim::RPC::Transport::Client::HTTPS")B= not defined "Lim::RPC::TLS"->instance->tls_ctx
ABresult
0X0
100
111
60 0 confess 'using HTTPS but can not create TLS context';
61 }
62 }
63  
64 =head2 name
65  
66 =cut
67  
68 0 ▶sub name {
Subroutine name: not called CC=1 SCAR=6.9
Pod documented
69 0 'http';
70 }
71  
72 =head2 request
73  
74 =cut
75  
76 0 ▶sub request {
Subroutine request: not called CC=8 SCAR=42.8
Pod documented
77 0 my $self = shift;
78 0 my %args = ( @_ );
79 0 my $real_self = $self;
80 0 weaken($self);
81  
82 0 $self->{rbuf} = '';
83 0 $self->{host} = 'localhost';
84 0 ▶ $self->{port} = $self->isa('Lim::RPC::Transport::Client::HTTPS') ? 443 : 80;
Branch: 1 branch
BranchTrueFalseTotal
$self->isa('Lim::RPC::Transport::Client::HTTPS') ? : 0 0 0
85  
86 0 ▶ unless (blessed $args{request} and $args{request}->isa('HTTP::Request')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($args{"request"}) and $args{"request"}->isa("HTTP::Request")) 0 0 0
Condition0%0/3
builtin::blessed($args{"request"}) and $args{"request"}->isa("HTTP::Request")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($args{"request"}) and $args{"request"}->isa("HTTP::Request")
builtin::blessed($args{"request"}) $args{"request"}->isa("HTTP::Request")
Truth table0%0/3
A and B
A= builtin::blessed($args{"request"})B= $args{"request"}->isa("HTTP::Request")
ABresult
0X0
100
111
87 0 confess __PACKAGE__, ': No request or not HTTP::Request';
88 }
89  
90 0 foreach (qw(host port)) {
91 0 ▶ if (defined $args{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{$_}) 0 0 0
92 0 $self->{$_} = $args{$_};
93 }
94 }
95 0 ▶ if (defined $args{cb} and ref($args{cb}) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"cb"} and ref $args{"cb"} eq "CODE") 0 0 0
Condition0%0/3
defined $args{"cb"} and ref $args{"cb"} eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $args{"cb"} and ref $args{"cb"} eq "CODE"
defined $args{"cb"} ref $args{"cb"} eq "CODE"
Truth table0%0/3
A and B
A= defined $args{"cb"}B= ref $args{"cb"} eq "CODE"
ABresult
0X0
100
111
96 0 $self->{cb} = $args{cb};
97 }
98 0 $self->{request} = $args{request};
99 0 $self->{request}->protocol('HTTP/1.1');
100  
101 0 ▶ Lim::Util::resolve_host $self->{host}, $self->{port}, sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
102 0 my ($host, $port) = @_;
103  
104 0 ▶ unless (defined $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self) 0 0 0
105 0 return;
106 }
107  
108 0 ▶ unless (defined $host and defined $port) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $host and defined $port) 0 0 0
Condition0%0/3
defined $host and defined $port
!ll&&!rl&&r
000
MC/DC0%0/2
defined $host and defined $port
defined $host defined $port
Truth table0%0/3
A and B
A= defined $hostB= defined $port
ABresult
0X0
100
111
109 0 ▶ Lim::WARN and $self->{logger}->warn('Unable to resolve host ', $self->{host});
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn("Unable to resolve host ", $self->{"host"}) 0 0 0
110 0 ▶ if (exists $self->{cb}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"cb"}) 0 0 0
111 $self->{cb}->($self, Lim::Error->new(
112 message => 'Unable to resolve host '.$self->{host},
113 0 module => $self
114 ));
115 0 delete $self->{cb};
116 }
117 0 return;
118 }
119  
120 0 $self->{host} = $host;
121 0 $self->{port} = $port;
122 0 $self->_connect;
123 0 };
124 }
125  
126 =head2 _connect
127  
128 =cut
129  
130 0 ▶sub _connect {
Subroutine _connect: not called CC=1 SCAR=6.9
131 0 my ($self) = @_;
132 0 my $real_self = $self;
133 0 weaken($self);
134  
135 0 ▶ $self->{socket} = AnyEvent::Socket::tcp_connect $self->{host}, $self->{port}, sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
136 0 my ($fh, $host, $port) = @_;
137  
138 0 ▶ unless (defined $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self) 0 0 0
139 0 return;
140 }
141  
142 0 ▶ unless (defined $fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fh) 0 0 0
143 0 ▶ Lim::WARN and $self->{logger}->warn('No handle: ', $!);
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn("No handle: ", $!) 0 0 0
144  
145 0 ▶ if (exists $self->{cb}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"cb"}) 0 0 0
146 0 $self->{cb}->($self, Lim::Error->new(
147 message => $!,
148 module => $self
149 ));
150 0 delete $self->{cb};
151 }
152 0 return;
153 }
154  
155 0 my $handle;
156 $handle = AnyEvent::Handle->new(
157 fh => $fh,
158 ($self->isa('Lim::RPC::Transport::Client::HTTPS') ? (tls => 'connect', tls_ctx => Lim::RPC::TLS->instance->tls_ctx) : ()),
159 timeout => Lim::Config->{rpc}->{timeout},
160 0 ▶ on_error => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
161 0 my ($handle, $fatal, $message) = @_;
162  
163 0 ▶ unless (defined $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self) 0 0 0
164 0 return;
165 }
166  
167 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
168 0 ▶ if (exists $self->{cb}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"cb"}) 0 0 0
169 0 $self->{cb}->($self, Lim::Error->new(
170 message => $message,
171 module => $self
172 ));
173 0 delete $self->{cb};
174 }
175 0 $handle->destroy;
176 },
177 0 ▶ on_timeout => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
178 0 my ($handle) = @_;
179  
180 0 ▶ unless (defined $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self) 0 0 0
181 0 return;
182 }
183  
184 0 ▶ Lim::WARN and $self->{logger}->warn($handle, ' TIMEOUT');
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn($handle, " TIMEOUT") 0 0 0
185  
186 0 ▶ if (exists $self->{cb}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"cb"}) 0 0 0
187 0 $self->{cb}->($self, Lim::Error->new(
188 code => HTTP_REQUEST_TIMEOUT,
189 message => 'Request timed out',
190 module => $self
191 ));
192 0 delete $self->{cb};
193 }
194 0 $handle->destroy;
195 },
196 0 ▶ on_eof => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
197 0 my ($handle) = @_;
198  
199 0 ▶ unless (defined $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self) 0 0 0
200 0 return;
201 }
202  
203 0 ▶ Lim::WARN and $self->{logger}->warn($handle, ' EOF');
Branch: 1 branch
BranchTrueFalseTotal
Lim::WARN() and $self->{"logger"}->warn($handle, " EOF") 0 0 0
204  
205 0 ▶ if (exists $self->{cb}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"cb"}) 0 0 0
206 0 $self->{cb}->($self, Lim::Error->new(
207 code => HTTP_GONE,
208 message => 'Connection closed',
209 module => $self
210 ));
211 0 delete $self->{cb};
212 }
213 0 $handle->destroy;
214 },
215 0 ▶ on_read => sub {
Subroutine __ANON__: not called CC=10 SCAR=47.0
216 0 my ($handle) = @_;
217  
218 0 ▶ unless (defined $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self) 0 0 0
219 0 return;
220 }
221  
222 0 ▶ if ((length($self->{headers}) + (exists $self->{content} ? length($self->{content}) : 0) + length($handle->{rbuf})) > MAX_RESPONSE_LEN) {
Branch: 2 branches
BranchTrueFalseTotal
if (length($self->{"headers"}) + (exists $self->{"content"} ? length $self->{"content"} : 0) + length($handle->{"rbuf"}) > 8388608) 0 0 0
exists $self->{'content'} ? : 0 0 0
223 0 ▶ if (exists $self->{cb}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"cb"}) 0 0 0
224 0 $self->{cb}->($self, Lim::Error->new(
225 code => HTTP_REQUEST_ENTITY_TOO_LARGE,
226 message => 'Request too large',
227 module => $self
228 ));
229 0 delete $self->{cb};
230 }
231 0 $handle->push_shutdown;
232 0 $handle->destroy;
233 0 return;
234 }
235  
236 0 ▶ unless (exists $self->{content}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'content'}) { } 0 0 0
237 0 $self->{headers} .= $handle->{rbuf};
238  
239 0 ▶ if ($self->{headers} =~ /\015?\012\015?\012/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"headers"} =~ /\015?\012\015?\012/ou) 0 0 0
240 0 my ($headers, $content) = split(/\015?\012\015?\012/o, $self->{headers}, 2);
241 0 $self->{headers} = $headers;
242 0 $self->{content} = $content;
243 0 $self->{response} = HTTP::Response->parse($self->{headers});
244 }
245 }
246 else {
247 0 $self->{content} .= $handle->{rbuf};
248 }
249 0 $handle->{rbuf} = '';
250  
251 0 ▶ if (defined $self->{response} and length($self->{content}) == $self->{response}->header('Content-Length')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"response"} and length $self->{"content"} == $self->{"response"}->header("Content-Length")) 0 0 0
Condition0%0/3
defined $self->{"response"} and length $self->{"content"} == $self->{"response"}->header("Content-Length")
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"response"} and length $self->{"content"} == $self->{"response"}->header("Content-Length")
defined $self->{"response"} length $self->{"content"} == $self->{"response"}->header("Content-Length")
Truth table0%0/3
A and B
A= defined $self->{"response"}B= length $self->{"content"} == $self->{"response"}->header("Content-Length")
ABresult
0X0
100
111
252 0 my $response = $self->{response};
253 0 $response->content($self->{content});
254 0 delete $self->{response};
255 0 delete $self->{content};
256 0 $self->{headers} = '';
257  
258 0 ▶ if (exists $self->{cb}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"cb"}) 0 0 0
259 0 $self->{cb}->($self, $response);
260 0 delete $self->{cb};
261 }
262 0 $handle->push_shutdown;
263 0 $handle->destroy;
264 }
265 0 ▶ });
Branch: 1 branch
BranchTrueFalseTotal
$self->isa('Lim::RPC::Transport::Client::HTTPS') ? : 0 0 0
266  
267 0 $self->{handle} = $handle;
268 0 $handle->push_write($self->{request}->as_string("\015\012"));
269 0 };
270 }
271  
272 =head1 AUTHOR
273  
274 Jerry Lundström, C<< <lundstrom.jerry at gmail.com> >>
275  
276 =head1 BUGS
277  
278 Please report any bugs or feature requests to L<https://github.com/jelu/lim/issues>.
279  
280 =head1 SUPPORT
281  
282 You can find documentation for this module with the perldoc command.
283  
284 perldoc Lim
285  
286 You can also look for information at:
287  
288 =over 4
289  
290 =item * Lim issue tracker (report bugs here)
291  
292 L<https://github.com/jelu/lim/issues>
293  
294 =back
295  
296 =head1 ACKNOWLEDGEMENTS
297  
298 =head1 LICENSE AND COPYRIGHT
299  
300 Copyright 2012-2013 Jerry Lundström.
301  
302 This program is free software; you can redistribute it and/or modify it
303 under the terms of either: the GNU General Public License as published
304 by the Free Software Foundation; or the Artistic License.
305  
306 See http://dev.perl.org/licenses/ for more information.
307  
308  
309 =cut
310  
311 1 1; # End of Lim::RPC::Transport::Client::HTTP
« Lim/RPC/Protocol/JSONRPC1.pm ↑ Summary Lim/RPC/URIMaps.pm »