| 1 | package Lim::RPC::Client; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 10 | ▶ | use common::sense; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 10 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 10 | ▶ | use Log::Log4perl (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 10 | ▶ | use Scalar::Util qw(blessed weaken); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 10 | ▶ | use AnyEvent (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 10 | ▶ | use AnyEvent::Socket (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 10 | ▶ | use AnyEvent::Handle (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 10 | ▶ | use HTTP::Request (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 10 | ▶ | use HTTP::Response (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 10 | ▶ | use HTTP::Status qw(:constants); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 10 | ▶ | use JSON::XS (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 10 | ▶ | use Lim (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 10 | ▶ | use Lim::Error (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 10 | ▶ | use Lim::RPC::TLS (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =encoding utf8 | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =head1 NAME | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | ... | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | =head1 VERSION | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | See L<Lim> for version. | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | =over 4 | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | =item OK | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | =item ERROR | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | =item MAX_RESPONSE_LEN | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =back | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | =cut | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 10 | our $VERSION = $Lim::VERSION; | |||||||||||||||||||||||||||
| 46 | 10 | our $JSON = JSON::XS->new->ascii; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | sub OK (){ 1 } | ||||||||||||||||||||||||||||
| 49 | sub ERROR (){ -1 } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | sub MAX_RESPONSE_LEN (){ 8 * 1024 * 1024 } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | ... | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | =head1 SUBROUTINES/METHODS | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | =head2 new | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | =cut | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | sub new { | ||||||||||||||||||||||||||||
| 64 | 0 | ▶ | my $this = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=15 SCAR=54.8
Pod
documented
| |||||||||||||||||||||||||||||
| 65 | 0 | ▶ | my $class = ref($this) || $this; | ||||||||||||||||||||||||||
|
Condition0%0/3
ref $this || $this
MC/DC0%0/2
ref $this || $this
ref $this
$this
Truth table0%0/3
A || B
A= ref $thisB= $this
| |||||||||||||||||||||||||||||
| 66 | 0 | my %args = ( @_ ); | |||||||||||||||||||||||||||
| 67 | 0 | my $self = { | |||||||||||||||||||||||||||
| 68 | logger => Log::Log4perl->get_logger, | ||||||||||||||||||||||||||||
| 69 | rbuf => '', | ||||||||||||||||||||||||||||
| 70 | status => 0, | ||||||||||||||||||||||||||||
| 71 | error => '' | ||||||||||||||||||||||||||||
| 72 | }; | ||||||||||||||||||||||||||||
| 73 | 0 | bless $self, $class; | |||||||||||||||||||||||||||
| 74 | 0 | my $real_self = $self; | |||||||||||||||||||||||||||
| 75 | 0 | weaken($self); | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | unless (defined $args{host}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 0 | confess __PACKAGE__, ': No host specified'; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | 0 | ▶ | unless (defined $args{port}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 0 | confess __PACKAGE__, ': No port specified'; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | 0 | ▶ | unless (defined $args{method}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 0 | confess __PACKAGE__, ': No method specified'; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | 0 | ▶ | unless (defined $args{uri}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 0 | confess __PACKAGE__, ': No uri specified'; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | 0 | ▶ | if (defined $args{data} and ref($args{data}) ne 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $args{"data"} and ref $args{"data"} ne "HASH"
MC/DC0%0/2
defined $args{"data"} and ref $args{"data"} ne "HASH"
defined $args{"data"}
ref $args{"data"} ne "HASH"
Truth table0%0/3
A and B
A= defined $args{"data"}B= ref $args{"data"} ne "HASH"
| |||||||||||||||||||||||||||||
| 90 | 0 | confess __PACKAGE__, ': Data is not a hash'; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 0 | ▶ | if (!defined Lim::RPC::TLS->instance->tls_ctx) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 0 | confess 'using HTTPS but can not create TLS context'; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | $self->{host} = $args{host}; | |||||||||||||||||||||||||||
| 98 | 0 | $self->{port} = $args{port}; | |||||||||||||||||||||||||||
| 99 | 0 | $self->{uri} = $args{uri}; | |||||||||||||||||||||||||||
| 100 | 0 | ▶ | if (defined $args{cb} and ref($args{cb}) eq 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $args{"cb"} and ref $args{"cb"} eq "CODE"
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"
| |||||||||||||||||||||||||||||
| 101 | 0 | $self->{cb} = $args{cb}; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | 0 | $self->{request} = HTTP::Request->new($args{method}, $self->{uri}); | |||||||||||||||||||||||||||
| 104 | 0 | $self->{request}->protocol('HTTP/1.1'); | |||||||||||||||||||||||||||
| 105 | 0 | ▶ | if (defined $args{data}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 0 | my $json; | |||||||||||||||||||||||||||
| 107 | 0 | eval { | |||||||||||||||||||||||||||
| 108 | 0 | $json = $JSON->encode($args{data}); | |||||||||||||||||||||||||||
| 109 | }; | ||||||||||||||||||||||||||||
| 110 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 0 | $self->{status} = ERROR; | |||||||||||||||||||||||||||
| 112 | 0 | $self->{error} = $@; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | ▶ | if (exists $self->{cb}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 0 | $self->{cb}->($self); | |||||||||||||||||||||||||||
| 116 | 0 | delete $self->{cb}; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | 0 | return; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | 0 | $self->{request}->content($json); | |||||||||||||||||||||||||||
| 121 | 0 | $self->{request}->header('Content-Length' => length($json)); | |||||||||||||||||||||||||||
| 122 | 0 | $self->{request}->header('Content-Type' => 'application/json'); | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | else { | ||||||||||||||||||||||||||||
| 125 | 0 | $self->{request}->header('Content-Length' => 0); | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | $self->{socket} = AnyEvent::Socket::tcp_connect $self->{host}, $self->{port}, sub { | ||||||||||||||||||||||||||||
| 129 | 0 | ▶ | my ($fh, $host, $port) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 0 | ▶ | unless (defined $self) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 0 | return; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 0 | ▶ | unless (defined $fh) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 0 | ▶ | Lim::WARN and $self->{logger}->warn('Error: ', $!); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 0 | $self->{status} = ERROR; | |||||||||||||||||||||||||||
| 138 | 0 | $self->{error} = $!; | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 0 | ▶ | if (exists $self->{cb}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 0 | $self->{cb}->($self); | |||||||||||||||||||||||||||
| 142 | 0 | delete $self->{cb}; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | 0 | return; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 0 | my $handle; | |||||||||||||||||||||||||||
| 148 | $handle = AnyEvent::Handle->new( | ||||||||||||||||||||||||||||
| 149 | fh => $fh, | ||||||||||||||||||||||||||||
| 150 | tls => 'connect', | ||||||||||||||||||||||||||||
| 151 | tls_ctx => Lim::RPC::TLS->instance->tls_ctx, | ||||||||||||||||||||||||||||
| 152 | timeout => Lim::Config->{rpc}->{timeout}, | ||||||||||||||||||||||||||||
| 153 | on_error => sub { | ||||||||||||||||||||||||||||
| 154 | 0 | ▶ | my ($handle, $fatal, $message) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 0 | ▶ | unless (defined $self) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | 0 | return; | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 0 | ▶ | Lim::WARN and $self->{logger}->warn($handle, ' Error: ', $message); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | 0 | $self->{status} = ERROR; | |||||||||||||||||||||||||||
| 162 | 0 | $self->{error} = $message; | |||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 0 | ▶ | if (exists $self->{cb}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | $self->{cb}->($self, Lim::Error->new( | ||||||||||||||||||||||||||||
| 166 | message => $self->{error}, | ||||||||||||||||||||||||||||
| 167 | 0 | module => $self | |||||||||||||||||||||||||||
| 168 | )); | ||||||||||||||||||||||||||||
| 169 | 0 | delete $self->{cb}; | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | 0 | $handle->destroy; | |||||||||||||||||||||||||||
| 172 | }, | ||||||||||||||||||||||||||||
| 173 | on_timeout => sub { | ||||||||||||||||||||||||||||
| 174 | 0 | ▶ | my ($handle) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 0 | ▶ | unless (defined $self) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | 0 | return; | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 0 | ▶ | Lim::WARN and $self->{logger}->warn($handle, ' TIMEOUT'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | 0 | $self->{status} = ERROR; | |||||||||||||||||||||||||||
| 182 | 0 | $self->{error} = 'Connection/Request/Response Timeout'; | |||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 0 | ▶ | if (exists $self->{cb}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 185 | $self->{cb}->($self, Lim::Error->new( | ||||||||||||||||||||||||||||
| 186 | code => HTTP_REQUEST_TIMEOUT, | ||||||||||||||||||||||||||||
| 187 | message => $self->{error}, | ||||||||||||||||||||||||||||
| 188 | 0 | module => $self | |||||||||||||||||||||||||||
| 189 | )); | ||||||||||||||||||||||||||||
| 190 | 0 | delete $self->{cb}; | |||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | 0 | $handle->destroy; | |||||||||||||||||||||||||||
| 193 | }, | ||||||||||||||||||||||||||||
| 194 | on_eof => sub { | ||||||||||||||||||||||||||||
| 195 | 0 | ▶ | my ($handle) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 0 | ▶ | unless (defined $self) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | 0 | return; | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 0 | ▶ | Lim::WARN and $self->{logger}->warn($handle, ' EOF'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 0 | ▶ | if (exists $self->{cb}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 204 | 0 | $self->{cb}->($self); | |||||||||||||||||||||||||||
| 205 | 0 | delete $self->{cb}; | |||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||
| 207 | 0 | $handle->destroy; | |||||||||||||||||||||||||||
| 208 | }, | ||||||||||||||||||||||||||||
| 209 | on_read => sub { | ||||||||||||||||||||||||||||
| 210 | 0 | ▶ | my ($handle) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=19 SCAR=59.4
| |||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 0 | ▶ | unless (defined $self) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 213 | 0 | return; | |||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 0 | ▶ | if ((length($self->{rbuf}) + length($handle->{rbuf})) > MAX_RESPONSE_LEN) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 217 | 0 | ▶ | if (exists $self->{on_error}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 218 | 0 | $self->{on_error}->($self, 1, 'Response too long'); | |||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||
| 220 | 0 | $handle->push_shutdown; | |||||||||||||||||||||||||||
| 221 | 0 | $handle->destroy; | |||||||||||||||||||||||||||
| 222 | 0 | return; | |||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 0 | ▶ | unless (exists $self->{content}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 226 | 0 | $self->{headers} .= $handle->{rbuf}; | |||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | 0 | ▶ | if ($self->{headers} =~ /\015?\012\015?\012/o) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 229 | 0 | my ($headers, $content) = split(/\015?\012\015?\012/o, $self->{headers}, 2); | |||||||||||||||||||||||||||
| 230 | 0 | $self->{headers} = $headers; | |||||||||||||||||||||||||||
| 231 | 0 | $self->{content} = $content; | |||||||||||||||||||||||||||
| 232 | 0 | $self->{response} = HTTP::Response->parse($self->{headers}); | |||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||
| 235 | else { | ||||||||||||||||||||||||||||
| 236 | 0 | $self->{content} .= $handle->{rbuf}; | |||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||
| 238 | 0 | $handle->{rbuf} = ''; | |||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | 0 | ▶ | if (defined $self->{response} and length($self->{content}) == $self->{response}->header('Content-Length')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $self->{"response"} and length $self->{"content"} == $self->{"response"}->header("Content-Length")
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")
| |||||||||||||||||||||||||||||
| 241 | 0 | my $response = $self->{response}; | |||||||||||||||||||||||||||
| 242 | 0 | $response->content($self->{content}); | |||||||||||||||||||||||||||
| 243 | 0 | delete $self->{response}; | |||||||||||||||||||||||||||
| 244 | 0 | delete $self->{content}; | |||||||||||||||||||||||||||
| 245 | 0 | $self->{headers} = ''; | |||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | 0 | my $data; | |||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | 0 | ▶ | if ($response->code == 200) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 250 | 0 | $self->{status} = OK; | |||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||
| 252 | else { | ||||||||||||||||||||||||||||
| 253 | 0 | $self->{status} = ERROR; | |||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | 0 | ▶ | if ($response->header('Content-Length')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 257 | 0 | ▶ | if ($response->header('Content-Type') =~ /application\/json/io) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 258 | 0 | eval { | |||||||||||||||||||||||||||
| 259 | 0 | $data = $JSON->decode($response->decoded_content); | |||||||||||||||||||||||||||
| 260 | }; | ||||||||||||||||||||||||||||
| 261 | 0 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 262 | 0 | $self->{status} = ERROR; | |||||||||||||||||||||||||||
| 263 | 0 | $self->{error} = $@; | |||||||||||||||||||||||||||
| 264 | 0 | undef($data); | |||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||
| 266 | else { | ||||||||||||||||||||||||||||
| 267 | 0 | ▶ | if (ref($data) ne 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 268 | 0 | $data = Lim::Error->new( | |||||||||||||||||||||||||||
| 269 | code => 500, | ||||||||||||||||||||||||||||
| 270 | message => 'Invalid data returned, not a hash', | ||||||||||||||||||||||||||||
| 271 | module => $self); | ||||||||||||||||||||||||||||
| 272 | 0 | $self->{status} = ERROR; | |||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||
| 274 | elsif ($self->{status} == ERROR) { | ||||||||||||||||||||||||||||
| 275 | 0 | $data = Lim::Error->new->set($data); | |||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||
| 279 | else { | ||||||||||||||||||||||||||||
| 280 | 0 | $self->{status} = ERROR; | |||||||||||||||||||||||||||
| 281 | 0 | $self->{error} = 'Unknown content type ['.$response->header('Content-Type').'] returned'; | |||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | 0 | ▶ | if ($self->{status} == ERROR) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 286 | 0 | ▶ | unless (defined $data) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 287 | $data = Lim::Error->new( | ||||||||||||||||||||||||||||
| 288 | code => $response->code, | ||||||||||||||||||||||||||||
| 289 | message => $self->{error}, | ||||||||||||||||||||||||||||
| 290 | 0 | module => $self); | |||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||
| 292 | 0 | ▶ | unless (blessed $data and $data->isa('Lim::Error')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
builtin::blessed($data) and $data->isa("Lim::Error")
MC/DC0%0/2
builtin::blessed($data) and $data->isa("Lim::Error")
builtin::blessed($data)
$data->isa("Lim::Error")
Truth table0%0/3
A and B
A= builtin::blessed($data)B= $data->isa("Lim::Error")
| |||||||||||||||||||||||||||||
| 293 | 0 | confess __PACKAGE__, ': status is ERROR but data is not a Lim::Error object'; | |||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | 0 | ▶ | if (exists $self->{cb}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 298 | 0 | $self->{cb}->($self, $data); | |||||||||||||||||||||||||||
| 299 | 0 | delete $self->{cb}; | |||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||
| 301 | 0 | $handle->push_shutdown; | |||||||||||||||||||||||||||
| 302 | 0 | $handle->destroy; | |||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||
| 304 | 0 | }); | |||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | 0 | $self->{handle} = $handle; | |||||||||||||||||||||||||||
| 307 | 0 | $handle->push_write($self->{request}->as_string("\015\012")); | |||||||||||||||||||||||||||
| 308 | 0 | delete $self->{request}; | |||||||||||||||||||||||||||
| 309 | 0 | }; | |||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | 0 | ▶ | Lim::OBJ_DEBUG and $self->{logger}->debug('new ', __PACKAGE__, ' ', $self); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 312 | 0 | $real_self; | |||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | sub DESTROY { | ||||||||||||||||||||||||||||
| 316 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 317 | 0 | ▶ | Lim::OBJ_DEBUG and $self->{logger}->debug('destroy ', __PACKAGE__, ' ', $self); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | 0 | delete $self->{client}; | |||||||||||||||||||||||||||
| 320 | 0 | delete $self->{socket}; | |||||||||||||||||||||||||||
| 321 | 0 | delete $self->{handle}; | |||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | =head2 status | ||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | =cut | ||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | sub status { | ||||||||||||||||||||||||||||
| 329 | 0 | ▶ | $_[0]->{status}; | ||||||||||||||||||||||||||
|
Subroutine
status: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | =head2 error | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | =cut | ||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | sub error { | ||||||||||||||||||||||||||||
| 337 | 0 | ▶ | $_[0]->{error}; | ||||||||||||||||||||||||||
|
Subroutine
error: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 338 | } | ||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | Jerry Lundström, C<< <lundstrom.jerry at gmail.com> >> | ||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | =head1 BUGS | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | Please report any bugs or feature requests to L<https://github.com/jelu/lim/issues>. | ||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | perldoc Lim | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | You can also look for information at: | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | =over 4 | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | =item * Lim issue tracker (report bugs here) | ||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | L<https://github.com/jelu/lim/issues> | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | =back | ||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | Copyright 2012-2013 Jerry Lundström. | ||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 371 | under the terms of either: the GNU General Public License as published | ||||||||||||||||||||||||||||
| 372 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | See http://dev.perl.org/licenses/ for more information. | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | =cut | ||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | 10 | 1; # End of Lim::RPC::Client | |||||||||||||||||||||||||||