| 1 | package FCGI::Client::Connection; | ||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use FCGI::Client::Constant; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 1 | ▶ | use Time::HiRes qw(time); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 1 | ▶ | use List::Util qw(max); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 1 | ▶ | use POSIX qw(EAGAIN); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 1 | ▶ | use FCGI::Client::Record; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 1 | ▶ | use FCGI::Client::RecordFactory; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 1 | ▶ | use Types::Standard qw/Int/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 1 | ▶ | use Moo; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 1 | has sock => ( | |||||||||||||||||||
| 16 | is => 'ro', | ||||||||||||||||||||
| 17 | required => 1, | ||||||||||||||||||||
| 18 | ); | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 1 | has timeout => ( | |||||||||||||||||||
| 21 | is => 'rw', | ||||||||||||||||||||
| 22 | isa => Int, | ||||||||||||||||||||
| 23 | default => 10, | ||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 1 | ▶ | no Moo; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 0 | ▶ | sub request { | ||||||||||||||||||
|
Subroutine
request: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 29 | 0 | my ($self, $env, $content) = @_; | |||||||||||||||||||
| 30 | 0 | local $SIG{PIPE} = "IGNORE"; | |||||||||||||||||||
| 31 | { | ||||||||||||||||||||
| 32 | 0 | ▶ | local $SIG{ALRM} = sub { Carp::confess('REQUEST_TIME_OUT') }; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 33 | 0 | ▶ | my $orig_alarm = alarm($self->timeout) || 0; | ||||||||||||||||||
|
Condition0%0/2
alarm $self->timeout || 0
MC/DC0%0/1
alarm $self->timeout || 0
alarm $self->timeout
Truth table0%0/2
A
A= alarm $self->timeout
| |||||||||||||||||||||
| 34 | 0 | $self->_send_request($env, $content); | |||||||||||||||||||
| 35 | 0 | my @res = $self->_receive_response($self->sock); | |||||||||||||||||||
| 36 | 0 | alarm($orig_alarm); | |||||||||||||||||||
| 37 | 0 | return @res; | |||||||||||||||||||
| 38 | } | ||||||||||||||||||||
| 39 | } | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | 0 | ▶ | sub _receive_response { | ||||||||||||||||||
|
Subroutine
_receive_response: not called CC=5 SCAR=34.0
| |||||||||||||||||||||
| 42 | 0 | my ($self, $sock) = @_; | |||||||||||||||||||
| 43 | 0 | my ($stdout, $stderr); | |||||||||||||||||||
| 44 | 0 | ▶ | while (my $res = $self->_read_record($self)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 45 | 0 | my $type = $res->type; | |||||||||||||||||||
| 46 | 0 | ▶ | if ($type == FCGI_STDOUT) { | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 47 | 0 | $stdout .= $res->content; | |||||||||||||||||||
| 48 | } elsif ($type == FCGI_STDERR) { | ||||||||||||||||||||
| 49 | 0 | $stderr .= $res->content; | |||||||||||||||||||
| 50 | } elsif ($type == FCGI_END_REQUEST) { | ||||||||||||||||||||
| 51 | 0 | my $appstatus = unpack('N', $res->content); | |||||||||||||||||||
| 52 | 0 | $sock->close(); | |||||||||||||||||||
| 53 | 0 | return ($stdout, $stderr, $appstatus); | |||||||||||||||||||
| 54 | } else { | ||||||||||||||||||||
| 55 | 0 | die "unknown response type: " . $res->type; | |||||||||||||||||||
| 56 | } | ||||||||||||||||||||
| 57 | } | ||||||||||||||||||||
| 58 | 0 | die 'connection breaked from server process?'; | |||||||||||||||||||
| 59 | } | ||||||||||||||||||||
| 60 | 0 | ▶ | sub _send_request { | ||||||||||||||||||
|
Subroutine
_send_request: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 61 | 0 | my ($self, $env, $content) = @_; | |||||||||||||||||||
| 62 | 0 | my $reqid = 1; | |||||||||||||||||||
| 63 | 0 | $self->sock->print(FCGI::Client::RecordFactory->create_request($reqid, $env, $content)); | |||||||||||||||||||
| 64 | } | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | 0 | ▶ | sub _read_record { | ||||||||||||||||||
|
Subroutine
_read_record: not called CC=9 SCAR=45.0
| |||||||||||||||||||||
| 67 | 0 | my ($self) = @_; | |||||||||||||||||||
| 68 | 0 | my $header_raw = ''; | |||||||||||||||||||
| 69 | 0 | ▶ | while (length($header_raw) != FCGI_HEADER_LEN) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 70 | 0 | ▶ | $self->_read_timeout(\$header_raw, FCGI_HEADER_LEN-length($header_raw), length($header_raw)) or return; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 71 | } | ||||||||||||||||||||
| 72 | 0 | my $header = FCGI::Client::RecordHeader->new(raw => $header_raw); | |||||||||||||||||||
| 73 | 0 | my $content_length = $header->content_length; | |||||||||||||||||||
| 74 | 0 | my $content = ''; | |||||||||||||||||||
| 75 | 0 | ▶ | if ($content_length != 0) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 76 | 0 | ▶ | while (length($content) != $content_length) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 77 | 0 | ▶ | $self->_read_timeout(\$content, $content_length-length($content), length($content)) or return; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 78 | } | ||||||||||||||||||||
| 79 | } | ||||||||||||||||||||
| 80 | 0 | my $padding_length = $header->padding_length; | |||||||||||||||||||
| 81 | 0 | my $padding = ''; | |||||||||||||||||||
| 82 | 0 | ▶ | if ($padding_length != 0) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 83 | 0 | ▶ | while (length($padding) != $padding_length) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 84 | 0 | ▶ | $self->_read_timeout(\$padding, $padding_length, 0) or return; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 85 | } | ||||||||||||||||||||
| 86 | } | ||||||||||||||||||||
| 87 | 0 | return FCGI::Client::Record->new( | |||||||||||||||||||
| 88 | header => $header, | ||||||||||||||||||||
| 89 | content => $content, | ||||||||||||||||||||
| 90 | ); | ||||||||||||||||||||
| 91 | } | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | # returns 1 if socket is ready, undef on timeout | ||||||||||||||||||||
| 94 | 0 | ▶ | sub _wait_socket { | ||||||||||||||||||
|
Subroutine
_wait_socket: not called CC=5 SCAR=34.0
| |||||||||||||||||||||
| 95 | 0 | my ( $self, $sock, $is_write, $wait_until ) = @_; | |||||||||||||||||||
| 96 | 0 | ▶ | do { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 97 | 0 | my $vec = ''; | |||||||||||||||||||
| 98 | 0 | vec( $vec, $sock->fileno, 1 ) = 1; | |||||||||||||||||||
| 99 | 0 | ▶ | if ( | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 100 | select( | ||||||||||||||||||||
| 101 | $is_write ? undef : $vec, | ||||||||||||||||||||
| 102 | $is_write ? $vec : undef, | ||||||||||||||||||||
| 103 | undef, | ||||||||||||||||||||
| 104 | max( $wait_until - time, 0 ) | ||||||||||||||||||||
| 105 | ) > 0 | ||||||||||||||||||||
| 106 | ) | ||||||||||||||||||||
| 107 | { | ||||||||||||||||||||
| 108 | 0 | return 1; | |||||||||||||||||||
| 109 | } | ||||||||||||||||||||
| 110 | } while ( time < $wait_until ); | ||||||||||||||||||||
| 111 | 0 | return; | |||||||||||||||||||
| 112 | } | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | # returns (positive) number of bytes read, or undef if the socket is to be closed | ||||||||||||||||||||
| 115 | 0 | ▶ | sub _read_timeout { | ||||||||||||||||||
|
Subroutine
_read_timeout: not called CC=5 SCAR=34.0
| |||||||||||||||||||||
| 116 | 0 | my ( $self, $buf, $len, $off, ) = @_; | |||||||||||||||||||
| 117 | 0 | my $sock = $self->sock; | |||||||||||||||||||
| 118 | 0 | my $timeout = $self->timeout; | |||||||||||||||||||
| 119 | 0 | my $wait_until = time + $timeout; | |||||||||||||||||||
| 120 | 0 | ▶ | while ( $self->_wait_socket( $sock, undef, $wait_until ) ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 121 | 0 | ▶ | if ( my $ret = $sock->sysread( $$buf, $len, $off ) ) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 122 | 0 | return $ret; | |||||||||||||||||||
| 123 | } | ||||||||||||||||||||
| 124 | ▶ | elsif ( !( !defined($ret) && $! == EAGAIN ) ) { | |||||||||||||||||||
|
Condition0%0/3
!defined($ret) && $! == 11
MC/DC0%0/2
!defined($ret) && $! == 11
!defined($ret)
$! == 11
Truth table0%0/3
not(A) && B
A= !defined($ret)B= $! == 11
| |||||||||||||||||||||
| 125 | 0 | last; | |||||||||||||||||||
| 126 | } | ||||||||||||||||||||
| 127 | } | ||||||||||||||||||||
| 128 | 0 | return; | |||||||||||||||||||
| 129 | } | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | 1 | 1; | |||||||||||||||||||
| 132 | __END__ | ||||||||||||||||||||