Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fixed small boundary bug in Mojo::Content::MultiPart
  • Loading branch information
kraih committed Oct 17, 2011
1 parent 558f7fe commit 2aab61d
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 26 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -30,6 +30,7 @@ This file documents the revision history for Perl extension Mojolicious.
- Fixed small memory leak in Mojolicious.
- Fixed small memory leak in Mojo::DOM.
- Fixed small memory leak in Mojo::Message.
- Fixed small boundary bug in Mojo::Content::MultiPart.
- Fixed small formatting bug in Mojo::Headers.
- Fixed small proxy message generation bug.
- Fixed small detection bug in Mojolicious::Types.
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Content.pm
Expand Up @@ -487,7 +487,7 @@ implements the following new ones.
=head2 C<body_contains>
my $found = $content->body_contains('foo bar baz');
my $success = $content->body_contains('foo bar baz');
Check if content contains a specific string.
Expand Down
13 changes: 4 additions & 9 deletions lib/Mojo/Content/MultiPart.pm
Expand Up @@ -7,16 +7,11 @@ has parts => sub { [] };

sub body_contains {
my ($self, $chunk) = @_;

# Check parts
my $found = 0;
for my $part (@{$self->parts}) {
my $headers = $part->build_headers;
$found += 1 if $headers =~ /$chunk/g;
$found += $part->body_contains($chunk);
return 1 if index($part->build_headers, $chunk) >= 0;
return 1 if $part->body_contains($chunk);
}

return $found ? 1 : 0;
return;
}

sub body_size {
Expand Down Expand Up @@ -277,7 +272,7 @@ implements the following new ones.
=head2 C<body_contains>
my $found = $content->body_contains('foobarbaz');
my $success = $content->body_contains('foobarbaz');
Check if content parts contain a specific string.
Expand Down
7 changes: 3 additions & 4 deletions lib/Mojo/Content/Single.pm
Expand Up @@ -9,9 +9,8 @@ has asset => sub { Mojo::Asset::Memory->new };
has auto_upgrade => 1;

sub body_contains {
my ($self, $chunk) = @_;
return 1 if $self->asset->contains($chunk) >= 0;
return 0;
return 1 if shift->asset->contains(shift) >= 0;
return;
}

sub body_size {
Expand Down Expand Up @@ -129,7 +128,7 @@ implements the following new ones.
=head2 C<body_contains>
my $found = $content->body_contains('1234567');
my $success = $content->body_contains('1234567');
Check if content contains a specific string.
Expand Down
8 changes: 4 additions & 4 deletions lib/Mojo/Transaction/HTTP.pm
Expand Up @@ -127,14 +127,14 @@ sub keep_alive {
my $res = $self->res;
my $req_conn = lc($req->headers->connection || '');
my $res_conn = lc($res->headers->connection || '');
return 0 if $req_conn eq 'close' || $res_conn eq 'close';
return if $req_conn eq 'close' || $res_conn eq 'close';

# Keep alive
return 1 if $req_conn eq 'keep-alive' || $res_conn eq 'keep-alive';

# No keep alive for 0.9 and 1.0
return 0 if $req->version ~~ [qw/0.9 1.0/];
return 0 if $res->version ~~ [qw/0.9 1.0/];
return if $req->version ~~ [qw/0.9 1.0/];
return if $res->version ~~ [qw/0.9 1.0/];

return 1;
}
Expand Down Expand Up @@ -400,7 +400,7 @@ Write client data.
=head2 C<keep_alive>
my $keep_alive = $tx->keep_alive;
my $success = $tx->keep_alive;
Check if connection can be kept alive.
Expand Down
30 changes: 26 additions & 4 deletions t/mojo/content.t
@@ -1,12 +1,34 @@
#!/usr/bin/env perl
use Mojo::Base -strict;

use Test::More tests => 3;
use Test::More tests => 17;

# "No matter how good you are at something,
# there's always about a million people better than you."
use_ok 'Mojo::Content::MultiPart';
use_ok 'Mojo::Content::Single';

# "No matter how good you are at something,
# there's always about a million people better than you."
# Single
my $content = Mojo::Content::Single->new;
is $content->body_contains('a'), 0, 'content contains "a"';
$content->asset->add_chunk('foo');
is $content->body_contains('a'), undef, 'content does not contain "a"';
is $content->body_contains('f'), 1, 'content contains "f"';
is $content->body_contains('o'), 1, 'content contains "o"';
is $content->body_contains('foo'), 1, 'content contains "foo"';

# Multipart
$content = Mojo::Content::MultiPart->new(parts => [$content]);
is $content->body_contains('a'), undef, 'content does not contain "a"';
is $content->body_contains('f'), 1, 'content contains "f"';
is $content->body_contains('o'), 1, 'content contains "o"';
is $content->body_contains('foo'), 1, 'content contains "foo"';
push @{$content->parts}, Mojo::Content::Single->new;
$content->parts->[1]->asset->add_chunk('.*?foo+');
$content->parts->[1]->headers->header('X-Bender' => 'bar+');
is $content->body_contains('z'), undef, 'content does not contain "z"';
is $content->body_contains('f'), 1, 'content contains "f"';
is $content->body_contains('o'), 1, 'content contains "o"';
is $content->body_contains('foo'), 1, 'content contains "foo"';
is $content->body_contains('bar+'), 1, 'content contains "bar+"';
is $content->body_contains('.'), 1, 'content contains "."';
is $content->body_contains('.*?foo+'), 1, 'content contains ".*?foo+"';
8 changes: 4 additions & 4 deletions t/mojolicious/longpolling_lite_app.t
Expand Up @@ -243,7 +243,7 @@ $t->get_ok('/shortpoll')->status_is(200)
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->content_type_is('text/plain')->content_is('this was short.');
is $t->tx->kept_alive, undef, 'connection was not kept alive';
is $t->tx->keep_alive, 0, 'connection will not be kept alive';
is $t->tx->keep_alive, undef, 'connection will not be kept alive';
is $shortpoll, 1, 'finished';

# GET /shortpoll/plain
Expand All @@ -261,8 +261,8 @@ $t->get_ok('/shortpoll/nolength')->status_is(200)
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->header_is('Content-Length' => undef)->content_type_is('text/plain')
->content_is('this was short and had no length.');
is $t->tx->kept_alive, 1, 'connection was not kept alive';
is $t->tx->keep_alive, 0, 'connection will be kept alive';
is $t->tx->kept_alive, 1, 'connection was not kept alive';
is $t->tx->keep_alive, undef, 'connection will be kept alive';
is $shortpoll_nolength, 'finished!', 'finished';

# GET /longpoll
Expand Down Expand Up @@ -314,7 +314,7 @@ $t->get_ok('/longpoll/nolength')->status_is(200)
->header_is('X-Powered-By' => 'Mojolicious (Perl)')
->header_is('Content-Length' => undef)->content_type_is('text/plain')
->content_is('hi there, what length?');
is $t->tx->keep_alive, 0, 'connection will not be kept alive';
is $t->tx->keep_alive, undef, 'connection will not be kept alive';
is $longpoll_nolength, 'finished!', 'finished';

# GET /longpoll/nested
Expand Down

0 comments on commit 2aab61d

Please sign in to comment.