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