| 1 | package Twiggy::Server; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Scalar::Util qw(blessed weaken); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Socket qw(IPPROTO_TCP TCP_NODELAY); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Errno qw(EAGAIN EINTR); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use IO::Handle; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use AnyEvent; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use AnyEvent::Handle; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use AnyEvent::Socket; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use AnyEvent::Util qw(WSAEWOULDBLOCK); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 1 | ▶ | use HTTP::Status; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 1 | ▶ | use Plack::HTTPParser qw(parse_http_request); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 1 | ▶ | use Plack::Util; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 1 | ▶ | use constant DEBUG => $ENV{TWIGGY_DEBUG}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 1 | ▶ | use constant HAS_AIO => !$ENV{PLACK_NO_SENDFILE} && try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=8.5
Subroutine
__ANON__: called CC=1 SCAR=2.6
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 1 | require AnyEvent::AIO; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 0 | require IO::AIO; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 0 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 1 | ▶ | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
!$ENV{'PLACK_NO_SENDFILE'} && &try(sub {
require AnyEvent::AIO;
require IO::AIO;
1;
}
)
MC/DC0%0/2
!$ENV{'PLACK_NO_SENDFILE'} && &try(sub {
require AnyEvent::AIO;
require IO::AIO;
1;
}
)
!$ENV{'PLACK_NO_SENDFILE'}
&try(sub {
require AnyEvent::AIO;
require IO::AIO;
1;
}
)
Truth table33%1/3
not(A) && B
A= !$ENV{'PLACK_NO_SENDFILE'}B= &try(sub {
require AnyEvent::AIO;
require IO::AIO;
1;
}
)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 1 | open my $null_io, '<', \''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 1 | my($class, @args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 1 | return bless { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | no_delay => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | timeout => 300, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | read_chunk_size => 4096, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | @args, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | }, $class; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 1 | ▶ | sub start_listen { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
start_listen: called CC=5 SCAR=16.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 1 | my ($self, $app) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 1 | ▶ | my @listen = @{$self->{listen} || [ ($self->{host} || '') . ":" . ($self->{port} || 0) ]}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%3/6
$self->{'listen'} || [($self->{'host'} || '') . ':' . ($self->{'port'} || 0)]
$self->{'host'} || ''
$self->{'port'} || 0
MC/DC0%0/1
$self->{'listen'} || [($self->{'host'} || '') . ':' . ($self->{'port'} || 0)]
$self->{'listen'}
MC/DC0%0/1
$self->{'host'} || ''
$self->{'host'}
MC/DC0%0/1
$self->{'port'} || 0
$self->{'port'}
Truth table50%1/2
A
A= $self->{'listen'}
Truth table50%1/2
A
A= $self->{'host'}
Truth table50%1/2
A
A= $self->{'port'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 1 | for my $listen (@listen) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 1 | push @{$self->{listen_guards}}, $self->_create_tcp_server($listen, $app); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 1 | ▶ | sub register_service { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
register_service: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 1 | my($self, $app) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 1 | $self->start_listen($app); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 1 | ▶ | $self->{exit_guard} = AE::cv { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | # Make sure that we are not listening on a socket anymore, while | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | # other events are being flushed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 1 | delete $self->{listen_guards}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 1 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 1 | $self->{exit_guard}->begin; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 1 | ▶ | sub _create_tcp_server { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_create_tcp_server: called CC=4 SCAR=14.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 1 | my ( $self, $listen, $app ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 1 | my($host, $port, $is_tcp); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 1 | ▶ | if ($listen =~ /:\d+$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 1 | ($host, $port) = split /:/, $listen; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 1 | ▶ | $host = undef if $host eq ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 1 | ▶ | $port = undef if $port == 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 1 | $is_tcp = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | $host = "unix/"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | $port = $listen; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 1 | my($listen_host, $listen_port); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 1 | return tcp_server $host, $port, $self->_accept_handler($app, $is_tcp, \$listen_host, \$listen_port), | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | $self->_accept_prepare_handler(\$listen_host, \$listen_port); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 1 | ▶ | sub _accept_prepare_handler { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_accept_prepare_handler: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 1 | my($self, $listen_host_r, $listen_port_r) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 1 | ▶ | return sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 1 | my ( $fh, $host, $port ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 1 | DEBUG && warn "Listening on $host:$port\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 1 | $$listen_host_r = $host; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 1 | $$listen_port_r = $port; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | $self->{server_ready}->({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | host => $host, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | port => $port, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | server_software => 'Twiggy', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 1 | ▶ | }) if $self->{server_ready}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 1 | ▶ | return $self->{backlog} || 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{'backlog'} || 0
MC/DC0%0/1
$self->{'backlog'} || 0
$self->{'backlog'}
Truth table50%1/2
A
A= $self->{'backlog'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 1 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 1 | ▶ | sub _accept_handler { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_accept_handler: called CC=1 SCAR=0.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 1 | my ( $self, $app, $is_tcp, $listen_host_r, $listen_port_r ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 2 | ▶ | return sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=20.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 2 | my ( $sock, $peer_host, $peer_port ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 2 | DEBUG && warn "$sock Accepted connection from $peer_host:$peer_port\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 2 | ▶ | return unless $sock; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 2 | $self->{exit_guard}->begin; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 2 | ▶ | if ( $is_tcp && $self->{no_delay} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$is_tcp and $self->{"no_delay"}
MC/DC0%0/2
$is_tcp and $self->{"no_delay"}
$is_tcp
$self->{"no_delay"}
Truth table33%1/3
A and B
A= $is_tcpB= $self->{"no_delay"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 2 | ▶ | setsockopt($sock, IPPROTO_TCP, TCP_NODELAY, 1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | or die "setsockopt(TCP_NODELAY) failed:$!"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 2 | my $headers = ""; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 3 | ▶ | my $try_parse = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 3 | ▶ | if ( $self->_try_read_headers($sock, $headers) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 1 | my $env = { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | SERVER_NAME => $$listen_host_r, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | SERVER_PORT => $$listen_port_r, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | SCRIPT_NAME => '', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | REMOTE_ADDR => $peer_host, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 'psgi.version' => [ 1, 0 ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 'psgi.errors' => *STDERR, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 'psgi.url_scheme' => 'http', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 'psgi.nonblocking' => Plack::Util::TRUE, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 'psgi.streaming' => Plack::Util::TRUE, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 'psgi.run_once' => Plack::Util::FALSE, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 'psgi.multithread' => Plack::Util::FALSE, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 'psgi.multiprocess' => Plack::Util::FALSE, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 'psgi.input' => undef, # will be set by _run_app() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 'psgix.io' => $sock, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 'psgix.input.buffered' => Plack::Util::TRUE, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 1 | my $reqlen = parse_http_request($headers, $env); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 1 | DEBUG && warn "$sock Parsed HTTP headers: request length=$reqlen\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 1 | ▶ | if ( $reqlen < 0 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | die "bad request"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 1 | return $env; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 2 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 2 | local $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 2 | ▶ | unless ( eval { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 2 | ▶ | if ( my $env = $try_parse->() ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | # the request data is already available, no need to parse more | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 0 | $self->_run_app($app, $env, $sock); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | # there's not yet enough data to parse the request, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | # set up a watcher | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 1 | $self->_create_req_parsing_watcher( $sock, $try_parse, $app ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | }) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 1 | my $disconnected = ($@ =~ /^client disconnected/); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 1 | $self->_bad_request($sock, $disconnected); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 1 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | # returns a closure that tries to parse | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | # this is not a method because it needs a buffer per socket | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 3 | ▶ | sub _try_read_headers { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_try_read_headers: called CC=10 SCAR=24.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 3 | my ( $self, $sock, undef ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | # FIXME add a timer to manage read timeouts | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 3 | local $/ = "\012"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 3 | read_more: for my $headers ( $_[2] ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 6 | ▶ | if ( defined(my $line = <$sock>) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 4 | $headers .= $line; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 4 | ▶ | if ( $line eq "\015\012" or $line eq "\012" ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$line eq "\r\n" or $line eq "\n"
MC/DC50%1/2
$line eq "\r\n" or $line eq "\n"
$line eq "\r\n"
$line eq "\n"
Truth table66%2/3
A or B
A= $line eq "\r\n"B= $line eq "\n"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | # got an empty line, we're done reading the headers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 1 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | # try to read more lines using buffered IO | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 3 | redo read_more; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | ▶ | } elsif ($! and $! != EAGAIN && $! != EINTR && $! != WSAEWOULDBLOCK ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition44%4/9
$! and ($! != 11 and $! != 4 and $! != "-1e+99")
$! != 11 and $! != 4 and $! != "-1e+99"
$! != 11 and $! != 4
MC/DC0%0/4
$! and ($! != 11 and $! != 4 and $! != "-1e+99")
$!
$! != 11
$! != 4
$! != "-1e+99"
Truth table40%2/5
A and B and C and D
A= $!B= $! != 11C= $! != 4D= $! != "-1e+99"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 0 | die $!; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | } elsif (!$!) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 1 | die "client disconnected"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 1 | DEBUG && warn "$sock did not read to end of req, wait for more data to arrive\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 1 | ▶ | sub _create_req_parsing_watcher { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_create_req_parsing_watcher: called CC=2 SCAR=8.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 1 | my ( $self, $sock, $try_parse, $app ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 1 | my $headers_io_watcher; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 0 | ▶ | my $timeout_timer = AE::timer $self->{timeout}, 0, sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 0 | DEBUG && warn "$sock Timeout\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 0 | undef $headers_io_watcher; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | 0 | undef $try_parse; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 0 | undef $sock; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 1 | ▶ | } if $self->{timeout}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 1 | ▶ | $headers_io_watcher = AE::io $sock, 0, sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.7
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 1 | ▶ | try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 1 | ▶ | if ( my $env = $try_parse->() ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 1 | undef $headers_io_watcher; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 1 | undef $timeout_timer; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 1 | $self->_run_app($app, $env, $sock); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 0 | ▶ | } catch { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 0 | undef $headers_io_watcher; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 0 | undef $timeout_timer; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 0 | my $disconnected = /^client disconnected/; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 0 | $self->_bad_request($sock, $disconnected); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 1 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 1 | ▶ | sub _bad_request { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_bad_request: called CC=4 SCAR=15.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 1 | my ( $self, $sock, $disconnected ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 1 | ▶ | return unless defined $sock and defined fileno $sock; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $sock and defined fileno $sock
MC/DC0%0/2
defined $sock and defined fileno $sock
defined $sock
defined fileno $sock
Truth table33%1/3
A and B
A= defined $sockB= defined fileno $sock
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 1 | my $response = [ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 400, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | [ 'Content-Type' => 'text/plain' ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | [ ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | ]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | # if client is already gone, don't try to write to it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 1 | ▶ | $response = [] if $disconnected; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 1 | $self->_write_psgi_response($sock, $response); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 0 | ▶ | sub _read_chunk { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_read_chunk: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 0 | my ($self, $sock, $remaining, $cb) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | 0 | my $data = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | 0 | my $read_chunk_size = $self->{read_chunk_size}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | 0 | ▶ | my $try_read = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=11 SCAR=48.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | READ_MORE: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 0 | ▶ | my $read_size = $remaining > $read_chunk_size ? $read_chunk_size : $remaining; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | 0 | my $rlen = read($sock, $data, $read_size, length($data)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 0 | ▶ | if (defined $rlen and $rlen > 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
Condition0%0/3
defined $rlen and $rlen > 0
MC/DC0%0/2
defined $rlen and $rlen > 0
defined $rlen
$rlen > 0
Truth table0%0/3
A and B
A= defined $rlenB= $rlen > 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 0 | $remaining -= $rlen; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 0 | ▶ | if ($remaining <= 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | 0 | $cb->($data); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | 0 | redo READ_MORE; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | } elsif (defined $rlen) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | 0 | $cb->($data); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | ▶ | } elsif ($! and $! != EAGAIN && $! != EINTR && $! != WSAEWOULDBLOCK) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/9
$! and ($! != 11 and $! != 4 and $! != "-1e+99")
$! != 11 and $! != 4 and $! != "-1e+99"
$! != 11 and $! != 4
MC/DC0%0/4
$! and ($! != 11 and $! != 4 and $! != "-1e+99")
$!
$! != 11
$! != 4
$! != "-1e+99"
Truth table0%0/5
A and B and C and D
A= $!B= $! != 11C= $! != 4D= $! != "-1e+99"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | 0 | die $!; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | } elsif (!$!) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | 0 | die "client disconnected"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 0 | ▶ | unless ($try_read->()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | 0 | ▶ | my $rw; $rw = AE::io($sock, 0, sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | 0 | ▶ | try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | 0 | ▶ | if ($try_read->()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | 0 | undef $rw; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | 0 | ▶ | } catch { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | 0 | undef $rw; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | 0 | $self->_bad_request($sock); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | 0 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | 1 | ▶ | sub _run_app { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_run_app: called CC=8 SCAR=29.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | 1 | my($self, $app, $env, $sock) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | 1 | ▶ | unless ($env->{'psgi.input'}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | 1 | ▶ | if ($env->{CONTENT_LENGTH}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | 0 | ▶ | $self->_read_chunk($sock, $env->{CONTENT_LENGTH}, sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | 0 | my ($data) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | 0 | open my $input, '<', \$data; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | 0 | $env->{'psgi.input'} = $input; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | 0 | $self->_run_app($app, $env, $sock); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | 0 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | 1 | $env->{'psgi.input'} = $null_io; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | 1 | my $res = Plack::Util::run_app $app, $env; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | 1 | ▶ | if ( ref $res eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | 0 | $self->_write_psgi_response($sock, $res); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | ▶ | } elsif ( blessed($res) and $res->isa("AnyEvent::CondVar") ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
builtin::blessed($res) and $res->isa("AnyEvent::CondVar")
MC/DC0%0/2
builtin::blessed($res) and $res->isa("AnyEvent::CondVar")
builtin::blessed($res)
$res->isa("AnyEvent::CondVar")
Truth table33%1/3
A and B
A= builtin::blessed($res)B= $res->isa("AnyEvent::CondVar")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | 0 | Carp::carp("Returning AnyEvent condvar is deprecated and will be removed in the next release of Twiggy. Use the streaming callback interface intstead."); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | 0 | ▶ | $res->cb(sub { $self->_write_psgi_response($sock, shift->recv) }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | } elsif ( ref $res eq 'CODE' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | 1 | my $created_writer; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | $res->( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | 1 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=20.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | 1 | my $res = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | 1 | ▶ | if ( @$res < 2 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | 0 | croak "Insufficient arguments"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | } elsif ( @$res == 2 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | 0 | my ( $status, $headers ) = @$res; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | 0 | $self->_flush($sock); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | 0 | my $writer = Twiggy::Writer->new($sock, $self->{exit_guard}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | 0 | $created_writer = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | 0 | my $buf = $self->_format_headers($status, $headers); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | 0 | $writer->write($$buf); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | 0 | return $writer; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | 1 | my ( $status, $headers, $body, $post ) = @$res; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | 1 | my $cv = $self->_write_psgi_response($sock, [ $status, $headers, $body ]); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | 0 | ▶ | $cv->cb(sub { $post->() }) if $post; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | 1 | $sock, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | 1 | ▶ | if($created_writer) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | 0 | $self->{exit_guard}->end; # normally _write_psgi_response calls this, but it doesn't get called when we use a writer! | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | 0 | croak("Unknown response type: $res"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | 2 | ▶ | sub _write_psgi_response { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_write_psgi_response: called CC=3 SCAR=13.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | 2 | my ( $self, $sock, $res ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | 2 | ▶ | if ( ref $res eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | 2 | ▶ | if ( scalar @$res == 0 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | # no response | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | 1 | $self->{exit_guard}->end; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | 1 | my ( $status, $headers, $body ) = @$res; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | 1 | my $cv = AE::cv; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | 1 | ▶ | $self->_write_headers( $sock, $status, $headers )->cb(sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=16.7
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | 1 | local $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | 0 | ▶ | if ( eval { $_[0]->recv; 1 } ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | 0 | ▶ | $self->_write_body($sock, $body)->cb(sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | 0 | shutdown $sock, 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | 0 | close $sock; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | 0 | $self->{exit_guard}->end; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | 0 | local $@; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | 0 | ▶ | eval { $cv->send($_[0]->recv); 1 } or $cv->croak($@); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | 0 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | 1 | $self->{exit_guard}->end; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | 0 | ▶ | eval { $cv->send($_[0]->recv); 1 } or $cv->croak($@); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | 1 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | 1 | return $cv; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | 1 | ▶ | no warnings 'uninitialized'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | 0 | warn "Unknown response type: $res"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | 0 | return $self->_write_psgi_response($sock, [ 204, [], [] ]); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | 1 | ▶ | sub _write_headers { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_write_headers: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | 1 | my ( $self, $sock, $status, $headers ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | 1 | $self->_write_buf( $sock, $self->_format_headers($status, $headers) ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | 1 | ▶ | sub _format_headers { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_format_headers: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | 1 | my ( $self, $status, $headers ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | 1 | my $hdr = sprintf "HTTP/1.0 %d %s\015\012", $status, HTTP::Status::status_message($status); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | 1 | my $i = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | 1 | my @delim = ("\015\012", ": "); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | 1 | foreach my $str ( @$headers ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | 4 | $hdr .= $str . $delim[++$i % 2]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | 1 | $hdr .= "\015\012"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | 1 | return \$hdr; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | # this flushes just the output buffer, not the input buffer (unlike | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | # $handle->flush) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | 0 | ▶ | sub _flush { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_flush: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | 0 | my ( $self, $sock ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | 0 | local $| = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | 0 | print $sock ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | # helper routine, similar to push write, but respects buffering, and refcounts | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | # itself | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | 1 | ▶ | sub _write_buf { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_write_buf: called CC=9 SCAR=32.7
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | 1 | my($self, $socket, $data) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | 1 | ▶ | no warnings 'uninitialized'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | # try writing immediately | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | 1 | ▶ | if ( (my $written = syswrite($socket, $$data)) < length($$data) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | 1 | ▶ | my $done = defined(wantarray) && AE::cv; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
defined wantarray && &AE::cv()
MC/DC0%0/2
defined wantarray && &AE::cv()
defined wantarray
&AE::cv()
Truth table33%1/3
A && B
A= defined wantarrayB= &AE::cv()
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | # either the write failed or was incomplete | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | 1 | ▶ | if ( !defined($written) and $! != EAGAIN && $! != EINTR && $! != WSAEWOULDBLOCK) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition11%1/9
not defined $written and ($! != 11 and $! != 4 and $! != "-1e+99")
$! != 11 and $! != 4 and $! != "-1e+99"
$! != 11 and $! != 4
MC/DC0%0/4
not defined $written and ($! != 11 and $! != 4 and $! != "-1e+99")
not defined $written
$! != 11
$! != 4
$! != "-1e+99"
Truth table20%1/5
not(A) and B and C and D
A= not defined $writtenB= $! != 11C= $! != 4D= $! != "-1e+99"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | # a real write error occured, like EPIPE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | 0 | ▶ | $done->croak($!) if $done; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | 0 | return $done; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | # the write was either incomplete or a non fatal error occured, so we | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | # need to set up an IO watcher to wait until we can properly write | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | 1 | my $length = length($$data); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | 1 | my $write_watcher; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | 1 | ▶ | $write_watcher = AE::io $socket, 1, sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=8 SCAR=31.3
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | write_more: { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | 1 | my $out = syswrite($socket, $$data, $length - $written, $written); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | 1 | ▶ | if ( defined($out) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | 0 | $written += $out; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | 0 | ▶ | if ( $written == $length ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | 0 | undef $write_watcher; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 460 | 0 | ▶ | $done->send(1) if $done; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | 0 | redo write_more; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 464 | ▶ | } elsif ($! != EAGAIN && $! != EINTR && $! != WSAEWOULDBLOCK) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%2/6
$! != 11 and $! != 4 and $! != "-1e+99"
$! != 11 and $! != 4
MC/DC0%0/3
$! != 11 and $! != 4 and $! != "-1e+99"
$! != 11
$! != 4
$! != "-1e+99"
Truth table25%1/4
A and B and C
A= $! != 11B= $! != 4C= $! != "-1e+99"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 465 | 1 | ▶ | $done->croak($!) if $done; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 466 | 1 | undef $write_watcher; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 467 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 468 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 469 | 1 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 471 | 1 | return $done; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 472 | } elsif ( defined wantarray ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 473 | 0 | my $done = AE::cv; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 474 | 0 | $done->send(1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 475 | 0 | return $done; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 476 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 477 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 479 | 0 | ▶ | sub _write_body { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_write_body: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 480 | 0 | my ( $self, $sock, $body ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 482 | 0 | ▶ | if ( ref $body eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 483 | 0 | my $buf = join "", @$body; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 484 | 0 | return $self->_write_buf($sock, \$buf); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 485 | } elsif ( Plack::Util::is_real_fh($body) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 486 | # real handles use nonblocking IO | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 487 | # either AIO or using watchers, with sendfile or with copying IO | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 488 | 0 | return $self->_write_real_fh($sock, $body); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 489 | ▶ | } elsif ( blessed($body) and $body->can("string_ref") ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
builtin::blessed($body) and $body->can("string_ref")
MC/DC0%0/2
builtin::blessed($body) and $body->can("string_ref")
builtin::blessed($body)
$body->can("string_ref")
Truth table0%0/3
A and B
A= builtin::blessed($body)B= $body->can("string_ref")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 490 | # optimize IO::String to not use its incredibly slow getline | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 491 | 0 | ▶ | if ( my $pos = $body->tell ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 492 | 0 | my $str = substr ${ $body->string_ref }, $pos; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 493 | 0 | return $self->_write_buf($sock, \$str); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 494 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 495 | 0 | return $self->_write_buf($sock, $body->string_ref); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 496 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 497 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 498 | 0 | return $self->_write_fh($sock, $body); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 499 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 500 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 502 | # like Plack::Util::foreach, but nonblocking on the output | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 503 | # handle | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 504 | 0 | ▶ | sub _write_fh { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_write_fh: not called CC=1 SCAR=3.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 505 | 0 | my ( $self, $sock, $body ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 507 | 0 | my $handle = AnyEvent::Handle->new( fh => $sock ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 508 | 0 | my $ret = AE::cv; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 510 | 0 | ▶ | $handle->on_error(sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 511 | 0 | my $err = $_[2]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 512 | 0 | $handle->destroy; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 513 | 0 | $ret->send($err); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 514 | 0 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 516 | 1 | ▶ | no warnings 'recursion'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 517 | 0 | $handle->on_drain( $self->_drain($body, $ret) ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 519 | 0 | return $ret; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 520 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 522 | 0 | ▶ | sub _drain { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_drain: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 523 | 0 | my ($self, $body, $ret) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 524 | 0 | ▶ | return sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 525 | 0 | my $handle = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 526 | 0 | local $/ = \ $self->{read_chunk_size}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 527 | 0 | ▶ | if ( defined( my $buf = $body->getline ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 528 | 0 | ▶ | if (length($buf)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 529 | 0 | $handle->push_write($buf); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 530 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 531 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 532 | # if on_drain is called and we don't do any | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 533 | # push_write, anyevent::handle thinks we are done. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 534 | # this fails for the deflater mw, since one 4096 chunk | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 535 | # of getline might not generate a deflated packet yet, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 536 | # which gets us an empty string here. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 537 | 0 | return $self->_drain($body, $ret)->($handle); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 538 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 539 | } elsif ( $! ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 540 | 0 | $ret->croak($!); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 541 | 0 | $handle->destroy; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 542 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 543 | 0 | $body->close; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 544 | 0 | ▶ | $handle->on_drain(sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 545 | 0 | shutdown $handle->fh, 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 546 | 0 | $handle->destroy; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 547 | 0 | $ret->send(1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 548 | 0 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 549 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 550 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 551 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 552 | 0 | } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 553 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 554 | # when the body handle is a real filehandle we use this routine, which is more | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 555 | # careful not to block when reading the response too | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 556 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 557 | # FIXME support only reading $length bytes from $body, instead of until EOF | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 558 | # FIXME use len = 0 param to sendfile | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 559 | # FIXME use Sys::Sendfile in nonblocking mode if AIO is not available | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 560 | # FIXME test sendfile on non file backed handles | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 561 | 0 | ▶ | sub _write_real_fh { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_write_real_fh: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 562 | 0 | my ( $self, $sock, $body ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 563 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 564 | 0 | ▶ | if ( HAS_AIO and -s $body ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
&HAS_AIO() and -s $body
MC/DC0%0/2
&HAS_AIO() and -s $body
&HAS_AIO()
-s $body
Truth table0%0/3
A and B
A= &HAS_AIO()B= -s $body
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 565 | 0 | my $cv = AE::cv; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 566 | 0 | my $offset = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 567 | 0 | my $length = -s $body; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 568 | 0 | $sock->blocking(1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 569 | 0 | ▶ | my $sendfile; $sendfile = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 570 | 0 | ▶ | IO::AIO::aio_sendfile( $sock, $body, $offset, $length - $offset, sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 571 | 0 | my $ret = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 572 | 0 | ▶ | $offset += $ret if $ret > 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 573 | 0 | ▶ | if ($offset >= $length || ($ret == -1 && ! ($! == EAGAIN || $! == EINTR))) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/9
$offset >= $length or $ret == -1 and not $! == 11 || $! == 4
$ret == -1 and not $! == 11 || $! == 4
$! == 11 || $! == 4
MC/DC0%0/4
$offset >= $length or $ret == -1 and not $! == 11 || $! == 4
$offset >= $length
$ret == -1
$! == 11
$! == 4
Truth table0%0/5
A or B and not(C || D)
A= $offset >= $lengthB= $ret == -1C= $! == 11D= $! == 4
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 574 | 0 | ▶ | if ( $ret == -1 ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 575 | 0 | $cv->croak($!); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 576 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 577 | 0 | $cv->send(1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 578 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 579 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 580 | 0 | undef $sendfile; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 581 | 0 | undef $sock; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 582 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 583 | 0 | $sendfile->(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 584 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 585 | 0 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 586 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 587 | 0 | $sendfile->(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 588 | 0 | return $cv; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 589 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 590 | 0 | return $self->_write_fh($sock, $body); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 591 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 592 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 593 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 594 | 1 | ▶ | sub run { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 595 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 596 | 1 | $self->register_service(@_); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 598 | 1 | ▶ | my $w; $w = AE::signal QUIT => sub { $self->{exit_guard}->end; undef $w }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 599 | 1 | $self->{exit_guard}->recv; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 600 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 601 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 602 | package Twiggy::Writer; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 603 | 1 | ▶ | use AnyEvent::Handle; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 604 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 605 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 606 | 0 | my ( $class, $socket, $exit ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 607 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 608 | 0 | ▶ | $exit->begin if $exit; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 609 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 610 | 0 | bless { handle => AnyEvent::Handle->new( fh => $socket ), exit_guard => $exit }, $class; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 611 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 612 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 613 | 0 | ▶ | sub write { $_[0]{handle}->push_write($_[1]) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
write: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 615 | 0 | ▶ | sub close { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
close: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 616 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 617 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 618 | 0 | my $exit_guard = delete $self->{exit_guard}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 619 | 0 | ▶ | $exit_guard->end if $exit_guard; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 620 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 621 | 0 | my $handle = delete $self->{handle}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 622 | 0 | ▶ | if ($handle) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 623 | 0 | $handle->on_drain; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 624 | 0 | $handle->on_error; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 625 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 626 | 0 | ▶ | $handle->on_drain(sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 627 | 0 | shutdown $_[0]->fh, 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 628 | 0 | $_[0]->destroy; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 629 | 0 | undef $handle; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 630 | 0 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 631 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 632 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 633 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 634 | 0 | ▶ | sub DESTROY { $_[0]->close } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 635 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 636 | package Twiggy::Server; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 637 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 638 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 639 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||