Skip to content

Commit

Permalink
added experimental on_body method to Mojo::Message
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Oct 11, 2011
1 parent 269f6c5 commit 6a473bf
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
3 changes: 2 additions & 1 deletion Changes
Expand Up @@ -6,7 +6,8 @@ This file documents the revision history for Perl extension Mojolicious.
- Renamed Mojo::IOLoop::EventEmitter to Mojo::EventEmitter.
- Replaced one_tick method in Mojo::IOWatcher with start and stop
methods.
- Added EXPERIMENTAL on_body attribute to Mojo::Content.
- Added EXPERIMENTAL on_body method to Mojo::Content and
Mojo::Message.
- Added EXPERIMENTAL contains method to Mojo::Path.
- Added EXPERIMENTAL auto_upgrade attribute to Mojo::Content::Single.
- Added EXPERIMENTAL boundary method to Mojo::Content.
Expand Down
22 changes: 22 additions & 0 deletions lib/Mojo/Message.pm
Expand Up @@ -315,6 +315,7 @@ sub leftovers { shift->content->leftovers }

sub max_line_size { shift->headers->max_line_size(@_) }

sub on_body { shift->on(body => shift) }
sub on_finish { shift->on(finish => shift) }
sub on_progress { shift->on(progress => shift) }

Expand Down Expand Up @@ -434,6 +435,12 @@ sub _parse {
my $state = $self->{state} || '';
if ($state eq 'body' || $state eq 'content' || $state eq 'done') {

# Body event
if ($self->has_subscribers('body') && !$self->{body}++) {
weaken $self;
$self->content->on_body(sub { $self->emit('body') });
}

# Until body
my $content = $self->content;
my $buffer = delete $self->{buffer};
Expand Down Expand Up @@ -555,6 +562,14 @@ in RFC 2616 and RFC 2388.
L<Mojo::Message> can emit the following events.
=head2 C<body>
$message->on(body => sub {
my $message = shift;
});
Emitted once all headers have been parsed and the content starts.
=head2 C<finish>
$message->on(finish => sub {
Expand Down Expand Up @@ -782,6 +797,13 @@ Remove leftover data from message parser.
Maximum line size in bytes.
Note that this method is EXPERIMENTAL and might change without warning!
=head2 C<on_body>
$message->on_body(sub {...});
Register C<body> event.
Note that this method is EXPERIMENTAL and might change without warning!
=head2 C<on_finish>
$message->on_finish(sub {...});
Expand Down
12 changes: 8 additions & 4 deletions t/mojo/message.t
Expand Up @@ -3,7 +3,7 @@ use Mojo::Base -strict;

use utf8;

use Test::More tests => 1237;
use Test::More tests => 1240;

use File::Spec;
use File::Temp;
Expand Down Expand Up @@ -2281,9 +2281,10 @@ is $req2->cookie('bar')->value, 'baz', 'right value';
is $req2->body, "Hello World!\n", 'right content';

# Parse full HTTP 1.0 request with cookies and progress callback
$req = Mojo::Message::Request->new;
$counter = 0;
$req = Mojo::Message::Request->new;
my $counter2 = $counter = 0;
$req->on_progress(sub { $counter++ });
$req->on_body(sub { $counter2++ });
is $counter, 0, 'right count';
is $req->content->is_parsing_body, undef, 'is not parsing body';
is $req->is_done, undef, 'request is not done';
Expand Down Expand Up @@ -2311,12 +2312,15 @@ $req->parse("est/23\x0d\x0a");
is $counter, 6, 'right count';
is $req->content->is_parsing_body, undef, 'is not parsing body';
is $req->is_done, undef, 'request is not done';
is $counter2, 0, 'right count';
$req->parse("Content-Length: 27\x0d\x0a\x0d\x0aHell");
is $counter, 7, 'right count';
is $req->content->is_parsing_body, 1, 'is parsing body';
is $req->is_done, undef, 'request is not done';
is $counter2, 1, 'right count';
$req->parse("o World!\n1234\nlalalala\n");
is $counter, 8, 'right count';
is $counter, 8, 'right count';
is $counter2, 1, 'right count';
is $req->content->is_parsing_body, undef, 'is not parsing body';
is $req->is_done, 1, 'request is done';
ok $req->is_done, 'request is done';
Expand Down
5 changes: 2 additions & 3 deletions t/mojolicious/upload_stream_lite_app.t
Expand Up @@ -21,9 +21,8 @@ app->hook(
after_build_tx => sub {
my $tx = shift;
weaken $tx;
$tx->req->content->on_body(
sub { $tx->emit('request') if $tx->req->url->path->contains('/upload') }
);
$tx->req->on_body(
sub { $tx->emit('request') if shift->url->path->contains('/upload') });
}
);

Expand Down

0 comments on commit 6a473bf

Please sign in to comment.