| 1 | #!/usr/bin/perl | ||||||||||||
| 2 | |||||||||||||
| 3 | package HTTP::Engine::Role::RequestBuilder::HTTPBody; | ||||||||||||
| 4 | 1 | ▶ | use Any::Moose '::Role'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | |||||||||||||
| 6 | with qw( | ||||||||||||
| 7 | HTTP::Engine::Role::RequestBuilder::ReadBody | ||||||||||||
| 8 | ); | ||||||||||||
| 9 | 1 | ▶ | use HTTP::Body; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 1 | ▶ | use HTTP::Engine::Request::Upload; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | |||||||||||||
| 12 | # tempolary file path for upload file. | ||||||||||||
| 13 | has upload_tmp => ( | ||||||||||||
| 14 | is => 'rw', | ||||||||||||
| 15 | ); | ||||||||||||
| 16 | |||||||||||||
| 17 | has chunk_size => ( | ||||||||||||
| 18 | is => 'ro', | ||||||||||||
| 19 | isa => 'Int', | ||||||||||||
| 20 | default => 4096, | ||||||||||||
| 21 | ); | ||||||||||||
| 22 | |||||||||||||
| 23 | sub _build_http_body { | ||||||||||||
| 24 | 0 | ▶ | my ( $self, $req ) = @_; | ||||||||||
|
Subroutine
_build_http_body: not called CC=1 SCAR=6.9
| |||||||||||||
| 25 | |||||||||||||
| 26 | 0 | $self->_read_to_end($req->_read_state); | |||||||||||
| 27 | |||||||||||||
| 28 | 0 | return delete $req->_read_state->{data}{http_body}; | |||||||||||
| 29 | } | ||||||||||||
| 30 | |||||||||||||
| 31 | sub _build_raw_body { | ||||||||||||
| 32 | 0 | ▶ | my ( $self, $req ) = @_; | ||||||||||
|
Subroutine
_build_raw_body: not called CC=1 SCAR=6.9
| |||||||||||||
| 33 | |||||||||||||
| 34 | 0 | $self->_read_to_end($req->_read_state); | |||||||||||
| 35 | |||||||||||||
| 36 | 0 | return delete $req->_read_state->{data}{raw_body}; | |||||||||||
| 37 | } | ||||||||||||
| 38 | |||||||||||||
| 39 | |||||||||||||
| 40 | sub _build_read_state { | ||||||||||||
| 41 | 0 | ▶ | my($self, $req) = @_; | ||||||||||
|
Subroutine
_build_read_state: not called CC=3 SCAR=24.8
| |||||||||||||
| 42 | |||||||||||||
| 43 | 0 | ▶ | my $length = $req->content_length || 0; | ||||||||||
|
Condition0%0/2
$req->content_length || 0
MC/DC0%0/1
$req->content_length || 0
$req->content_length
Truth table0%0/2
A
A= $req->content_length
| |||||||||||||
| 44 | 0 | my $type = $req->header('Content-Type'); | |||||||||||
| 45 | |||||||||||||
| 46 | 0 | my $body = HTTP::Body->new($type, $length); | |||||||||||
| 47 | 0 | ▶ | $body->tmpdir( $self->upload_tmp) if $self->upload_tmp; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 48 | |||||||||||||
| 49 | return $self->_read_init({ | ||||||||||||
| 50 | input_handle => $req->_connection->{input_handle}, | ||||||||||||
| 51 | 0 | content_length => $length, | |||||||||||
| 52 | read_position => 0, | ||||||||||||
| 53 | data => { | ||||||||||||
| 54 | raw_body => "", | ||||||||||||
| 55 | http_body => $body, | ||||||||||||
| 56 | }, | ||||||||||||
| 57 | }); | ||||||||||||
| 58 | } | ||||||||||||
| 59 | |||||||||||||
| 60 | sub _handle_read_chunk { | ||||||||||||
| 61 | 0 | ▶ | my ( $self, $state, $chunk ) = @_; | ||||||||||
|
Subroutine
_handle_read_chunk: not called CC=1 SCAR=6.9
| |||||||||||||
| 62 | |||||||||||||
| 63 | 0 | my $d = $state->{data}; | |||||||||||
| 64 | |||||||||||||
| 65 | 0 | $d->{raw_body} .= $chunk; | |||||||||||
| 66 | 0 | $d->{http_body}->add($chunk); | |||||||||||
| 67 | } | ||||||||||||
| 68 | |||||||||||||
| 69 | sub _prepare_uploads { | ||||||||||||
| 70 | 0 | ▶ | my($self, $req) = @_; | ||||||||||
|
Subroutine
_prepare_uploads: not called CC=6 SCAR=37.4
| |||||||||||||
| 71 | |||||||||||||
| 72 | 0 | my $uploads = $req->http_body->upload; | |||||||||||
| 73 | 0 | my %uploads; | |||||||||||
| 74 | 0 | for my $name (keys %{ $uploads }) { | |||||||||||
| 75 | 0 | my $files = $uploads->{$name}; | |||||||||||
| 76 | 0 | ▶ | $files = ref $files eq 'ARRAY' ? $files : [$files]; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 77 | |||||||||||||
| 78 | 0 | my @uploads; | |||||||||||
| 79 | 0 | for my $upload (@{ $files }) { | |||||||||||
| 80 | 0 | my $headers = HTTP::Headers::Fast->new( %{ $upload->{headers} } ); | |||||||||||
| 81 | push( | ||||||||||||
| 82 | @uploads, | ||||||||||||
| 83 | HTTP::Engine::Request::Upload->new( | ||||||||||||
| 84 | headers => $headers, | ||||||||||||
| 85 | tempname => $upload->{tempname}, | ||||||||||||
| 86 | size => $upload->{size}, | ||||||||||||
| 87 | filename => $upload->{filename}, | ||||||||||||
| 88 | ) | ||||||||||||
| 89 | 0 | ); | |||||||||||
| 90 | } | ||||||||||||
| 91 | 0 | ▶ | $uploads{$name} = @uploads > 1 ? \@uploads : $uploads[0]; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 92 | |||||||||||||
| 93 | # support access to the filename as a normal param | ||||||||||||
| 94 | 0 | my @filenames = map { $_->{filename} } @uploads; | |||||||||||
| 95 | 0 | ▶ | $req->parameters->{$name} = @filenames > 1 ? \@filenames : $filenames[0]; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 96 | } | ||||||||||||
| 97 | 0 | return \%uploads; | |||||||||||
| 98 | } | ||||||||||||
| 99 | |||||||||||||
| 100 | 1; | ||||||||||||
| 101 | |||||||||||||