Streaming/Request.pm

filter: Statementstmt 95.7% 113 / 118 Branchbran 85.2% 29 / 34 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 82.1% 23 / 28 Podpod 83.3% 5 / 6 totaltotal 89.5% 171 / 191 CC 21 SCAR 12.1
CC
21
Cov
92%
print_content
9.0
__ANON__
6.0
print_header
3.1
CRAP 3.4
↑ Summary Streaming.pm »
1 package Plack::App::CGIBin::Streaming::Request;
2  
3 36 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 36 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 36 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 36 ▶no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 36 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 36 my %trace=
10 (
11 # new=>sub {warn "NEW: @_"},
12 # header=>sub {warn "HEADER: @_"},
13 # flush=>sub {warn "FLUSH: @_"},
14 # status_out=>sub {warn "STATUS OUT: @_"},
15 # content=>sub {warn "CONTENT: @_"},
16 # finalize_start=>sub {warn "FINALIZE START: @_"},
17 # finalize_end=>sub {warn "FINALIZE END: @_"},
18 );
19 36 ▶use constant TRACE=>0; do {
Subroutine BEGIN: called CC=1 SCAR=0.0
20 36 ▶ no warnings 'void';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
22 0 my $what=shift;
23 0 local $SIG{__WARN__};
24 0 ▶ $trace{$what} and $trace{$what}->(@_);
Branch: 1 branch
BranchTrueFalseTotal
$trace{$what} and $trace{$what}->(@_) 0 0 0
25 36 };
26 };
27  
28 36 our @attr;
29  
30 36 our $DEFAULT_CONTENT_TYPE='text/plain';
31 36 our $DEFAULT_MAX_BUFFER=8000;
32  
33 36 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=6.9
34 36 @attr=(qw/env responder writer _buffer _buflen _headers max_buffer
35 content_type filter_before filter_after on_status_output
36 parse_headers _header_buffer status notes on_flush on_finalize
37 suppress_flush binmode_ok/);
38 36 for (@attr) {
39 684 my $attr=$_;
40 36 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
41 684 ▶ *{__PACKAGE__.'::'.$attr}=sub : lvalue {
Subroutine __ANON__: called CC=2 SCAR=6.9
42 83944 my $I=$_[0];
43 83944 ▶ $I->{$attr}=$_[1] if @_>1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 147 83797 83944
44 83944 $I->{$attr};
45 684 };
46 }
47 }
48  
49 33 ▶sub new {
Subroutine new: called CC=3 SCAR=11.1
Pod undocumented
50 33 my $class=shift;
51 33 ▶ $class=ref($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
0330
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
52 my $self=bless {
53 content_type=>$DEFAULT_CONTENT_TYPE,
54 max_buffer=>$DEFAULT_MAX_BUFFER,
55 83447 ▶ filter_before=>sub{},
Subroutine __ANON__: called CC=1 SCAR=0.0
56 83455 ▶ filter_after=>sub{},
Subroutine __ANON__: called CC=1 SCAR=0.0
57 29 ▶ on_status_output=>sub{},
Subroutine __ANON__: called CC=1 SCAR=0.0
58 993 ▶ on_flush=>sub{},
Subroutine __ANON__: called CC=1 SCAR=0.0
59 25 ▶ on_finalize=>sub{},
Subroutine __ANON__: called CC=1 SCAR=0.0
60 33 notes=>+{},
61 _headers=>[],
62 _buffer=>[],
63 _buflen=>0,
64 status=>200,
65 }, $class;
66  
67 33 ▶ for( my $i=0; $i<@_; $i+=2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @_) 145 33 178
68 145 my $method=$_[$i];
69 145 $self->$method($_[$i+1]);
70 }
71  
72 33 if (TRACE) {
73 (ref(TRACE) eq 'CODE'
74 ? TRACE->(new=>$self)
75 : warn "NEW $self");
76 }
77  
78 33 return $self;
79 }
80  
81 70 ▶sub print_header {
Subroutine print_header: called CC=3 SCAR=11.2
Pod documented
82 70 my $self = shift;
83  
84 70 ▶ croak "KEY => VALUE pairs expected" if @_%2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 70 70
85 70 ▶ croak "It's too late to set a HTTP header" if $self->{writer};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"writer"} 0 70 70
86  
87 70 if (TRACE) {
88 (ref(TRACE)
89 ? TRACE->(header=>$self, @_)
90 : warn "print_header $self: @_");
91 }
92  
93 70 push @{$self->{_headers}}, @_;
94 }
95  
96 83489 ▶sub print_content {
Subroutine print_content: called CC=9 SCAR=22.0
Pod documented
97 83489 my $self = shift;
98  
99 83489 ▶ if ($self->{parse_headers}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"parse_headers"}) 24 83465 83489
100 24 $self->{_header_buffer}.=join('', @_);
101 24 ▶ while( $self->{_header_buffer}=~s/\A(\S+)[ \t]*:[ \t]*(.+?)\r?\n// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_header_buffer"} =~ s/\A(\S+)[ \t]*:[ \t]*(.+?)\r?\n//u) 52 24 76
102 52 my ($hdr, $val)=($1, $2);
103 52 ▶ if ($hdr=~/\Astatus\z/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($hdr =~ /\Astatus\z/iu) { } 20 32 52
elsif ($hdr =~ /\Acontent-type\z/iu) { } 24 8 32
104 20 $self->{status}=$val;
105 } elsif ($hdr=~/\Acontent-type\z/i) {
106 24 $self->{content_type}=$val;
107 } else {
108 8 $self->print_header($hdr, $val);
109 }
110 }
111 24 ▶ if ($self->{_header_buffer}=~s/\A\r?\n//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_header_buffer"} =~ s/\A\r?\n//u) 24 0 24
112 24 delete $self->{parse_headers}; # done
113 $self->print_content(delete $self->{_header_buffer})
114 24 ▶ if length $self->{_header_buffer};
Branch: 1 branch
BranchTrueFalseTotal
if length $self->{"_header_buffer"} 11 13 24
115 }
116 24 return;
117 }
118  
119 83465 my @data=@_;
120 83465 $self->{filter_before}->($self, \@data);
121  
122 83465 my $len = 0;
123 83465 $len += length $_ for @data;
124  
125 83465 if (TRACE) {
126 (ref(TRACE)
127 ? TRACE->(content=>$self, @data)
128 : warn "print_content $self: $len bytes");
129 }
130  
131 83465 push @{$self->{_buffer}}, @data;
132 83465 $len += $self->{_buflen};
133 83465 $self->{_buflen}=$len;
134  
135 83465 ▶ if ($len > $self->{max_buffer}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > $self->{"max_buffer"}) 996 82469 83465
136 996 local $self->{suppress_flush};
137 996 $self->flush;
138 }
139  
140 83465 $self->filter_after->($self, \@data);
141 }
142  
143 33 ▶sub _status_out {
Subroutine _status_out: called CC=3 SCAR=11.0
144 33 my $self = shift;
145 33 my $is_done = shift;
146  
147 33 if (TRACE) {
148 (ref(TRACE)
149 ? TRACE->(status_out=>$self, $is_done)
150 : warn "status_out $self: $self->{status}");
151 }
152  
153 33 $self->print_header('Content-Type', $self->{content_type});
154 $self->print_header('Content-Length', $self->{_buflen})
155 33 ▶ if $is_done;
Branch: 1 branch
BranchTrueFalseTotal
if $is_done 21 12 33
156 33 $self->on_status_output->($self);
157  
158 $self->{writer}=$self->{responder}->([$self->{status},
159 $self->{_headers},
160 33 ▶ $is_done ? $self->{_buffer}: ()]);
Branch: 1 branch
BranchTrueFalseTotal
$is_done ? : 21 12 33
161 }
162  
163 7 ▶sub status_written {
Subroutine status_written: called CC=1 SCAR=0.0
Pod documented
164 7 my $self = shift;
165 7 return !!$self->{writer};
166 }
167  
168 1010 ▶sub flush {
Subroutine flush: called CC=3 SCAR=11.0
Pod documented
169 1010 my $self = shift;
170 1010 ▶ return 0 unless @{$self->{_buffer}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"_buffer"};} 2 1008 1010
171  
172 1008 if (TRACE) {
173 (ref(TRACE)
174 ? TRACE->(flush=>$self)
175 : warn "flush $self");
176 }
177  
178 1008 ▶ $self->_status_out unless $self->{writer};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"writer"} 12 996 1008
179  
180 1008 $self->{writer}->write(join '', @{$self->{_buffer}});
181 1008 @{$self->{_buffer}}=();
182 1008 $self->{_buflen}=0;
183  
184 1008 $self->{on_flush}->($self);
185  
186 1008 return 0;
187 }
188  
189 33 ▶sub finalize {
Subroutine finalize: called CC=2 SCAR=6.9
Pod documented
190 33 my $self = shift;
191  
192 33 if (TRACE) {
193 (ref(TRACE)
194 ? TRACE->(finalize_start=>$self)
195 : warn "finalize start $self");
196 }
197  
198 33 $self->{on_finalize}->($self);
199 33 ▶ if ($self->{writer}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'writer'}) { } 12 21 33
200 12 $self->{writer}->write(join '', @{$self->{_buffer}});
201 12 $self->{writer}->close;
202 } else {
203 21 $self->_status_out(1);
204 }
205  
206 33 if (TRACE) {
207 (ref(TRACE)
208 ? TRACE->(finalize_end=>$self)
209 : warn "finalize end $self");
210 }
211  
212 33 %$self=();
213 33 bless $self, 'Plack::App::CGIBin::Streaming::Request::Demolished';
214 }
215  
216 package # prevent CPAN indexing
217 Plack::App::CGIBin::Streaming::Request::Demolished;
218 36 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
219  
220 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=1 SCAR=6.9
221 0 our $AUTOLOAD;
222 0 die "Calling $AUTOLOAD on a demolished request.";
223 }
224  
225 0 ▶sub flush {}
Subroutine flush: not called CC=1 SCAR=0.0
226 0 ▶sub finalize {}
Subroutine finalize: not called CC=1 SCAR=0.0
227 0 ▶sub DESTROY {}
Subroutine DESTROY: not called CC=1 SCAR=0.0
228  
229 36 1;
230  
231 __END__
↑ Summary Streaming.pm »