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