File Coverage

blib/lib/HTTP/Engine/Role/RequestBuilder/HTTPBody.pm
Criterion Covered Total %
statement 9 43 20.9
branch 0 8 0.0
condition 0 2 0.0
subroutine 3 8 37.5
pod n/a
total 12 61 19.6


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2              
3             package HTTP::Engine::Role::RequestBuilder::HTTPBody;
4 1     1   377 use Any::Moose '::Role';
  1         2  
  1         4  
5              
6             with qw(
7             HTTP::Engine::Role::RequestBuilder::ReadBody
8             );
9 1     1   1726 use HTTP::Body;
  1         2752  
  1         26  
10 1     1   318 use HTTP::Engine::Request::Upload;
  1         2  
  1         383  
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     0     my ( $self, $req ) = @_;
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     0     my ( $self, $req ) = @_;
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     0     my($self, $req) = @_;
42              
43 0   0       my $length = $req->content_length || 0;
44 0           my $type = $req->header('Content-Type');
45              
46 0           my $body = HTTP::Body->new($type, $length);
47 0 0         $body->tmpdir( $self->upload_tmp) if $self->upload_tmp;
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     0     my ( $self, $state, $chunk ) = @_;
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     0     my($self, $req) = @_;
71              
72 0           my $uploads = $req->http_body->upload;
73 0           my %uploads;
74 0           for my $name (keys %{ $uploads }) {
  0            
75 0           my $files = $uploads->{$name};
76 0 0         $files = ref $files eq 'ARRAY' ? $files : [$files];
77              
78 0           my @uploads;
79 0           for my $upload (@{ $files }) {
  0            
80 0           my $headers = HTTP::Headers::Fast->new( %{ $upload->{headers} } );
  0            
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 0         $uploads{$name} = @uploads > 1 ? \@uploads : $uploads[0];
92              
93             # support access to the filename as a normal param
94 0           my @filenames = map { $_->{filename} } @uploads;
  0            
95 0 0         $req->parameters->{$name} = @filenames > 1 ? \@filenames : $filenames[0];
96             }
97 0           return \%uploads;
98             }
99              
100             1;
101