lib/Rex/Interface/Exec/IOReader.pm

filter: Statementstmt 72.1% 44 / 61 Branchbran 60.0% 24 / 40 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 4 / 4 Podpod 0.0% 0 / 1 totaltotal 66.9% 73 / 109 CC 24 SCAR 38.4
CC
24
Cov
67%
io_read
52.1
BEGIN
1.0
BEGIN
1.0
CRAP 46.5
« lib/Rex/Commands/Iptables.pm ↑ Summary lib/Rex/Interface/Exec/HTTP.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4  
5 package Rex::Interface::Exec::IOReader;
6  
7 60 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 60 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 60 our $VERSION = '1.16.1'; # VERSION
11  
12 60 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶sub io_read {
Subroutine io_read: called CC=24 SCAR=39.5
Pod undocumented
15 2 my ( $self, $out_fh, $err_fh, $pid, $option ) = @_;
16 2 my ( $out, $err, $out_line, $err_line );
17  
18 2 my $selector = IO::Select->new();
19 2 $selector->add($out_fh);
20 2 $selector->add($err_fh);
21  
22 2 ▶ my $rex_int_conf = Rex::Commands::get("rex_internals") || {};
Condition50%1/2
Rex::Commands::get('rex_internals') || {}
l!l
02
MC/DC0%0/1
Rex::Commands::get('rex_internals') || {}
Rex::Commands::get('rex_internals')
Truth table50%1/2
A
A= Rex::Commands::get('rex_internals')
Aresult
11
00
23 2 my $buffer_size = 1024;
24 2 ▶ if ( exists $rex_int_conf->{read_buffer_size} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $rex_int_conf->{"read_buffer_size"}) 0 2 2
25 0 $buffer_size = $rex_int_conf->{read_buffer_size};
26 }
27  
28 2 my ( $last_line_stdout, $last_line_stderr ) = ( "", "" );
29  
30 2 ▶ while ( my @ready = $selector->can_read ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@ready) = $selector->can_read) 27905 2 27907
31 27905 foreach my $fh (@ready) {
32 28144 my $buf = "";
33  
34 28144 my $len = sysread $fh, $buf, $buffer_size;
35 28144 ▶ $selector->remove($fh) unless $len;
Branch: 1 branch
BranchTrueFalseTotal
unless $len 4 28140 28144
36  
37 28144 $buf =~ s/\r?\n/\n/g; # normalize EOL characters
38  
39 # append buffer to the proper overall output
40 28144 ▶ $out .= $buf if $fh == $out_fh;
Branch: 1 branch
BranchTrueFalseTotal
if $fh == $out_fh 247 27897 28144
41 28144 ▶ $err .= $buf if $fh == $err_fh;
Branch: 1 branch
BranchTrueFalseTotal
if $fh == $err_fh 27897 247 28144
42  
43 28144 ▶ if ( $buf =~ /\n/ ) { # buffer has one or more newlines
Branch: 1 branch
BranchTrueFalseTotal
if ($buf =~ /\n/u) { } 1 28143 28144
44 1 my @line_chunks = split /\n/, $buf;
45  
46 1 my $partial_last_chunk = '';
47 1 ▶ if ( $buf !~ /\n$/ ) { # last chunk is partial
Branch: 1 branch
BranchTrueFalseTotal
unless ($buf =~ /\n$/u) 0 1 1
48 0 $partial_last_chunk = pop @line_chunks;
49 }
50  
51 1 foreach my $chunk (@line_chunks) {
52 1 ▶ if ( $fh == $out_fh ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($fh == $out_fh) { } 0 1 1
elsif ($fh == $err_fh) { } 1 0 1
53 0 $out_line .= $chunk;
54 $self->execute_line_based_operation( $out_line, $option )
55 0 ▶ && do { kill( 'KILL', $pid ); goto END_OPEN };
Branch: 1 branch
BranchTrueFalseTotal
$self->execute_line_based_operation($out_line, $option) and do { kill "KILL", $pid; goto END_OPEN } 0 0 0
56 0 $out_line = '';
57 }
58 elsif ( $fh == $err_fh ) {
59 1 $err_line .= $chunk;
60 $self->execute_line_based_operation( $err_line, $option )
61 0 ▶ && do { kill( 'KILL', $pid ); goto END_OPEN };
Branch: 1 branch
BranchTrueFalseTotal
$self->execute_line_based_operation($err_line, $option) and do { kill "KILL", $pid; goto END_OPEN } 0 1 1
62 1 $err_line = '';
63 }
64 }
65  
66 1 ▶ if ($partial_last_chunk) { # append partial chunk to line if present
Branch: 1 branch
BranchTrueFalseTotal
if ($partial_last_chunk) 0 1 1
67 0 ▶ $out_line .= $partial_last_chunk if $fh == $out_fh;
Branch: 1 branch
BranchTrueFalseTotal
if $fh == $out_fh 0 0 0
68 0 ▶ $err_line .= $partial_last_chunk if $fh == $err_fh;
Branch: 1 branch
BranchTrueFalseTotal
if $fh == $err_fh 0 0 0
69 }
70  
71 }
72 else { # buffer doesn't have any newlines
73 28143 ▶ $out_line .= $buf if $fh == $out_fh;
Branch: 1 branch
BranchTrueFalseTotal
if $fh == $out_fh 247 27896 28143
74 28143 ▶ $err_line .= $buf if $fh == $err_fh;
Branch: 1 branch
BranchTrueFalseTotal
if $fh == $err_fh 27896 247 28143
75 }
76 }
77 }
78  
79 2 ▶ unless ($out) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($out) 1 1 2
80 1 $out = $last_line_stdout;
81 $self->execute_line_based_operation( $out, $option )
82 0 ▶ && do { kill( 'KILL', $pid ); goto END_OPEN };
Branch: 1 branch
BranchTrueFalseTotal
$self->execute_line_based_operation($out, $option) and do { kill "KILL", $pid; goto END_OPEN } 0 1 1
83 }
84  
85 2 ▶ unless ($err) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($err) 0 2 2
86 0 $err = $last_line_stderr;
87 $self->execute_line_based_operation( $err, $option )
88 0 ▶ && do { kill( 'KILL', $pid ); goto END_OPEN };
Branch: 1 branch
BranchTrueFalseTotal
$self->execute_line_based_operation($err, $option) and do { kill "KILL", $pid; goto END_OPEN } 0 0 0
89 }
90  
91 END_OPEN:
92  
93 2 return ( $out, $err );
94 }
95  
96 60 1;
« lib/Rex/Commands/Iptables.pm ↑ Summary lib/Rex/Interface/Exec/HTTP.pm »