| 1 | package Perlbal::ChunkedUploadState; | ||||||||||||||||||||
| 2 | 22 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | 3 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.6
Pod
undocumented
| |||||||||||||||||||||
| 5 | 3 | my ($pkg, %args) = @_; | |||||||||||||||||||
| 6 | 3 | my $self = bless { | |||||||||||||||||||
| 7 | 'buf' => '', | ||||||||||||||||||||
| 8 | 'bytes_remain' => 0, # remaining in chunk (ignoring final 2 byte CRLF) | ||||||||||||||||||||
| 9 | }, $pkg; | ||||||||||||||||||||
| 10 | 3 | foreach my $k (qw(on_new_chunk on_disconnect on_zero_chunk)) { | |||||||||||||||||||
| 11 | 9 | ▶ | $self->{$k} = (delete $args{$k}) || sub {}; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
Condition33%1/3
delete $args{$k} || sub {
}
MC/DC0%0/2
delete $args{$k} || sub {
}
delete $args{$k}
sub {
}
Truth table33%1/3
A || B
A= delete $args{$k}B= sub {
}
| |||||||||||||||||||||
| 12 | } | ||||||||||||||||||||
| 13 | 3 | ▶ | die "bogus args" if %args; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 14 | 3 | return $self; | |||||||||||||||||||
| 15 | } | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 17 | ▶ | sub on_readable { | ||||||||||||||||||
|
Subroutine
on_readable: called CC=3 SCAR=11.6
Pod
undocumented
| |||||||||||||||||||||
| 18 | 17 | my ($self, $ds) = @_; | |||||||||||||||||||
| 19 | 17 | my $rbuf = $ds->read(131072); | |||||||||||||||||||
| 20 | 17 | ▶ | unless (defined $rbuf) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 21 | 0 | $self->{on_disconnect}->(); | |||||||||||||||||||
| 22 | 0 | return; | |||||||||||||||||||
| 23 | } | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 17 | $self->{buf} .= $$rbuf; | |||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | 17 | ▶ | while ($self->drive_machine) {} | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 28 | } | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | # returns 1 if progress was made parsing buffer | ||||||||||||||||||||
| 31 | 57 | ▶ | sub drive_machine { | ||||||||||||||||||
|
Subroutine
drive_machine: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||
| 32 | 57 | my $self = shift; | |||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 57 | my $buflen = length($self->{buf}); | |||||||||||||||||||
| 35 | 57 | ▶ | return 0 unless $buflen; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | 49 | ▶ | if (my $br = $self->{bytes_remain}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 38 | 24 | ▶ | my $extract = $buflen > $br ? $br : $buflen; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 39 | 24 | my $ch = substr($self->{buf}, 0, $extract, ''); | |||||||||||||||||||
| 40 | 24 | $self->{bytes_remain} -= $extract; | |||||||||||||||||||
| 41 | 24 | ▶ | die "assert" if $self->{bytes_remain} < 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 42 | 24 | $self->{on_new_chunk}->(\$ch); | |||||||||||||||||||
| 43 | 24 | return 1; | |||||||||||||||||||
| 44 | } | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 25 | ▶ | return 0 unless $self->{buf} =~ s/^(?:\r\n)?([0-9a-fA-F]+)(?:;.*)?\r\n//; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 47 | 19 | $self->{bytes_remain} = hex($1); | |||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | 19 | ▶ | if ($self->{bytes_remain} == 0) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 50 | # FIXME: new state machine state for trailer parsing/discarding. | ||||||||||||||||||||
| 51 | # (before we do on_zero_chunk). for now, though, just assume | ||||||||||||||||||||
| 52 | # no trailers and throw away the extra post-trailer \r\n that | ||||||||||||||||||||
| 53 | # is probably in this packet. hacky. | ||||||||||||||||||||
| 54 | 3 | $self->{buf} =~ s/^\r\n//; | |||||||||||||||||||
| 55 | 3 | $self->{hit_zero} = 1; | |||||||||||||||||||
| 56 | 3 | $self->{on_zero_chunk}->(); | |||||||||||||||||||
| 57 | 3 | return 0; | |||||||||||||||||||
| 58 | } | ||||||||||||||||||||
| 59 | 16 | return 1; | |||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 12 | ▶ | sub hit_zero_chunk { $_[0]{hit_zero} } | ||||||||||||||||||
|
Subroutine
hit_zero_chunk: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 22 | 1; | |||||||||||||||||||