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