Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fixed a few more multipart bugs
  • Loading branch information
kraih committed Nov 21, 2014
1 parent 31a45d4 commit 34c7b4f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
19 changes: 10 additions & 9 deletions lib/Mojo/Content/MultiPart.pm
Expand Up @@ -18,12 +18,10 @@ sub body_size {
my $self = shift;

# Check for existing Content-Lenght header
my $content_len = $self->headers->content_length;
return $content_len if $content_len;
if (my $len = $self->headers->content_length) { return $len }

# Calculate length of whole body
my $boundary_len = length($self->build_boundary) + 6;
my $len = $boundary_len - 2;
my $len = my $boundary_len = length($self->build_boundary) + 6;
$len += $_->header_size + $_->body_size + $boundary_len for @{$self->parts};

return $len;
Expand All @@ -33,16 +31,15 @@ sub build_boundary {
my $self = shift;

# Check for existing boundary
if (defined(my $boundary = $self->boundary)) { return $boundary }
my $boundary;
return $boundary if defined($boundary = $self->boundary);

# Generate and check boundary
my $boundary;
my $size = 1;
while (1) {
do {
$boundary = b64_encode join('', map chr(rand 256), 1 .. $size++ * 3);
$boundary =~ s/\W/X/g;
last unless $self->body_contains($boundary);
}
} while $self->body_contains($boundary);

# Add boundary to Content-Type header
my $headers = $self->headers;
Expand Down Expand Up @@ -90,6 +87,10 @@ sub get_body_chunk {
$len += $content_len;

# Boundary
if ($#$parts == $i) {
$boundary .= '--';
$boundary_len += 2;
}
return substr "\x0d\x0a--$boundary\x0d\x0a", $offset - $len
if ($len + $boundary_len) > $offset;
$len += $boundary_len;
Expand Down
11 changes: 7 additions & 4 deletions t/mojo/request.t
Expand Up @@ -1433,7 +1433,7 @@ is $req->version, '1.1', 'right version';
is $req->url, '/foo/bar', 'right URL';
is $req->url->to_abs, 'http://127.0.0.1/foo/bar', 'right absolute URL';
is $req->headers->host, '127.0.0.1', 'right "Host" value';
is $req->headers->content_length, '104', 'right "Content-Length" value';
is $req->headers->content_length, '106', 'right "Content-Length" value';
is $req->headers->content_type, 'multipart/mixed; boundary=7am1X',
'right "Content-Type" value';
is $req->content->parts->[0]->asset->slurp, 'Hallo Welt lalalala!',
Expand Down Expand Up @@ -1463,7 +1463,7 @@ is $req->version, '1.1', 'right version';
is $req->url, '/foo/bar', 'right URL';
is $req->url->to_abs, 'http://127.0.0.1/foo/bar', 'right absolute URL';
is $req->headers->host, '127.0.0.1', 'right "Host" value';
is $req->headers->content_length, '104', 'right "Content-Length" value';
is $req->headers->content_length, '106', 'right "Content-Length" value';
is $req->headers->content_type, 'multipart/mixed; boundary=7am1X',
'right "Content-Type" value';
is $req->content->parts->[0]->asset->slurp, 'Hallo Welt lalalala!',
Expand All @@ -1479,7 +1479,7 @@ is $clone->version, '1.1', 'right version';
is $clone->url, '/foo/bar', 'right URL';
is $clone->url->to_abs, 'http://127.0.0.1/foo/bar', 'right absolute URL';
is $clone->headers->host, '127.0.0.1', 'right "Host" value';
is $clone->headers->content_length, '104', 'right "Content-Length" value';
is $clone->headers->content_length, '106', 'right "Content-Length" value';
is $clone->headers->content_type, 'multipart/mixed; boundary=7am1X',
'right "Content-Type" value';
is $clone->content->parts->[0]->asset->slurp, 'Hallo Welt lalalala!',
Expand Down Expand Up @@ -1675,12 +1675,15 @@ ok $req->is_finished, 'request is finished';
is $req->method, 'POST', 'right method';
is $req->version, '1.1', 'right version';
is $req->url, '/example/testform_handler', 'right URL';
isnt $req->headers->content_length, 318, 'different "Content-Length" value';
is $req->headers->content_length, 323, 'right "Content-Length" value';
is $req->param('Vorname'), 'T', 'right value';
is $req->param('Zuname'), '', 'right value';
is $req->param('Text'), '', 'right value';
is $req->content->parts->[0]->asset->slurp, 'T', 'right content';
is $req->content->leftovers, '', 'no leftovers';
is $req->content->get_body_chunk(322), "\x0a", 'right chunk';
is $req->content->get_body_chunk(321), "\x0d\x0a", 'right chunk';
is $req->content->get_body_chunk(320), "-\x0d\x0a", 'right chunk';

# Parse multipart/form-data request with charset
$req = Mojo::Message::Request->new;
Expand Down
2 changes: 1 addition & 1 deletion t/mojo/response.t
Expand Up @@ -771,7 +771,7 @@ is $res->code, 200, 'right status';
is $res->message, 'OK', 'right message';
is $res->version, '1.1', 'right version';
is $res->headers->date, 'Sun, 17 Aug 2008 16:27:35 GMT', 'right "Date" value';
is $res->headers->content_length, '108', 'right "Content-Length" value';
is $res->headers->content_length, '110', 'right "Content-Length" value';
is $res->headers->content_type, 'multipart/mixed; boundary=7am1X',
'right "Content-Type" value';
is $res->content->parts->[0]->asset->slurp, 'Hallo Welt lalalalalala!',
Expand Down

0 comments on commit 34c7b4f

Please sign in to comment.