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