| 1 | package HTTP::Body::Builder::MultiPart; | ||||||||||||||
| 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 | 1 | ▶ | use utf8; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 1 | ▶ | use 5.008_005; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | 1 | ▶ | use File::Basename (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | 1 | my $CRLF = "\015\012"; | |||||||||||||
| 10 | |||||||||||||||
| 11 | sub new { | ||||||||||||||
| 12 | 3 | ▶ | my $class = shift; | ||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||
| 13 | 3 | ▶ | my %args = @_==1 ? %{$_[0]} : @_; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 14 | 3 | bless { | |||||||||||||
| 15 | boundary => 'xYzZY', | ||||||||||||||
| 16 | buffer_size => 2048, | ||||||||||||||
| 17 | %args | ||||||||||||||
| 18 | }, $class; | ||||||||||||||
| 19 | } | ||||||||||||||
| 20 | |||||||||||||||
| 21 | sub add_content { | ||||||||||||||
| 22 | 3 | ▶ | my ($self, $name, $value) = @_; | ||||||||||||
|
Subroutine
add_content: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 23 | 3 | push @{$self->{content}}, [$name, $value]; | |||||||||||||
| 24 | } | ||||||||||||||
| 25 | |||||||||||||||
| 26 | sub add_file { | ||||||||||||||
| 27 | 2 | ▶ | my ($self, $name, $filename) = @_; | ||||||||||||
|
Subroutine
add_file: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 28 | 2 | push @{$self->{file}}, [$name, $filename]; | |||||||||||||
| 29 | } | ||||||||||||||
| 30 | |||||||||||||||
| 31 | sub content_type { | ||||||||||||||
| 32 | 3 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
content_type: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 33 | 3 | return 'multipart/form-data'; | |||||||||||||
| 34 | } | ||||||||||||||
| 35 | |||||||||||||||
| 36 | sub _gen { | ||||||||||||||
| 37 | 3 | ▶ | my ($self, $code) = @_; | ||||||||||||
|
Subroutine
_gen: called CC=6 SCAR=18.5
| |||||||||||||||
| 38 | |||||||||||||||
| 39 | 3 | for my $row (@{$self->{content}}) { | |||||||||||||
| 40 | 3 | $code->(join('', "--$self->{boundary}$CRLF", | |||||||||||||
| 41 | qq{Content-Disposition: form-data; name="$row->[0]"$CRLF}, | ||||||||||||||
| 42 | "$CRLF", | ||||||||||||||
| 43 | $row->[1] . $CRLF | ||||||||||||||
| 44 | )); | ||||||||||||||
| 45 | } | ||||||||||||||
| 46 | 3 | for my $row (@{$self->{file}}) { | |||||||||||||
| 47 | 2 | my $filename = File::Basename::basename($row->[1]); | |||||||||||||
| 48 | 2 | $code->(join('', "--$self->{boundary}$CRLF", | |||||||||||||
| 49 | qq{Content-Disposition: form-data; name="$row->[0]"; filename="$filename"$CRLF}, | ||||||||||||||
| 50 | "Content-Type: text/plain$CRLF", | ||||||||||||||
| 51 | "$CRLF", | ||||||||||||||
| 52 | )); | ||||||||||||||
| 53 | open my $fh, '<:raw', $row->[1] | ||||||||||||||
| 54 | 2 | ▶ | or do { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 55 | 0 | $self->{errstr} = "Cannot open '$row->[1]' for reading: $!"; | |||||||||||||
| 56 | 0 | return; | |||||||||||||
| 57 | }; | ||||||||||||||
| 58 | 2 | my $buf; | |||||||||||||
| 59 | 2 | while (1) { | |||||||||||||
| 60 | 4 | my $r = read $fh, $buf, $self->{buffer_size}; | |||||||||||||
| 61 | 4 | ▶ | if (not defined $r) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 62 | 0 | $self->{errstr} = "Cannot open '$row->[1]' for reading: $!"; | |||||||||||||
| 63 | 0 | return; | |||||||||||||
| 64 | } elsif ($r == 0) { # eof | ||||||||||||||
| 65 | 2 | last; | |||||||||||||
| 66 | } else { | ||||||||||||||
| 67 | 2 | $code->($buf); | |||||||||||||
| 68 | } | ||||||||||||||
| 69 | } | ||||||||||||||
| 70 | 2 | $code->($CRLF); | |||||||||||||
| 71 | } | ||||||||||||||
| 72 | 3 | $code->("--$self->{boundary}--$CRLF"); | |||||||||||||
| 73 | 3 | return 1; | |||||||||||||
| 74 | } | ||||||||||||||
| 75 | |||||||||||||||
| 76 | sub as_string { | ||||||||||||||
| 77 | 2 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
as_string: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||
| 78 | 2 | my $buf = ''; | |||||||||||||
| 79 | 7 | ▶ | $self->_gen(sub { $buf .= $_[0] }) | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 80 | 2 | ▶ | or return; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 81 | 2 | $buf; | |||||||||||||
| 82 | } | ||||||||||||||
| 83 | |||||||||||||||
| 84 | 0 | ▶ | sub errstr { shift->{errstr} } | ||||||||||||
|
Subroutine
errstr: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 85 | |||||||||||||||
| 86 | sub write_file { | ||||||||||||||
| 87 | 1 | ▶ | my ($self, $filename) = @_; | ||||||||||||
|
Subroutine
write_file: called CC=3 SCAR=12.0
Pod
documented
| |||||||||||||||
| 88 | |||||||||||||||
| 89 | open my $fh, '>:raw', $filename | ||||||||||||||
| 90 | 1 | ▶ | or do { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 91 | 0 | $self->{errstr} = "Cannot open '$filename' for writing: $!"; | |||||||||||||
| 92 | 0 | return; | |||||||||||||
| 93 | }; | ||||||||||||||
| 94 | 5 | ▶ | $self->_gen(sub { print {$fh} $_[0] }) | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 95 | 1 | ▶ | or return; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 96 | 1 | close $fh; | |||||||||||||
| 97 | } | ||||||||||||||
| 98 | |||||||||||||||
| 99 | 1 | 1; | |||||||||||||
| 100 | __END__ | ||||||||||||||