Skip to content

Commit

Permalink
removed on_body method from Mojo::Message again
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Oct 11, 2011
1 parent 33cb9e6 commit 52a0ce2
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 59 deletions.
3 changes: 1 addition & 2 deletions Changes
Expand Up @@ -6,8 +6,7 @@ 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 method to Mojo::Content and
Mojo::Message.
- Added EXPERIMENTAL on_body method to Mojo::Content.
- 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
2 changes: 1 addition & 1 deletion lib/Mojo/Content.pm
Expand Up @@ -599,7 +599,7 @@ Note that this method is EXPERIMENTAL and might change without warning!
Register C<read> event.
$content->on_read(sub {
my ($self, $chunk) = @_;
my ($message, $chunk) = @_;
say $chunk;
});
Expand Down
50 changes: 10 additions & 40 deletions lib/Mojo/Message.pm
Expand Up @@ -43,28 +43,20 @@ sub body {
my $self = shift;

# Downgrade multipart content
$self->content(Mojo::Content::Single->new)
if $self->content->isa('Mojo::Content::MultiPart');

# Get
$self->content(Mojo::Content::Single->new) if $self->content->is_multipart;
my $content = $self->content;
return $content->has_subscribers('read') ? undef : $content->asset->slurp
unless @_;

# New content
my $new = shift;
$content->unsubscribe_all('read');
$content->asset(Mojo::Asset::Memory->new);
return $self unless defined $new;
# Get
return $content->asset->slurp unless defined(my $new = shift);

# Callback
if (ref $new eq 'CODE') {
weaken $self;
$content->on_read(sub { shift and $self->$new(@_) });
return $content->on_read(sub { $self->$new(pop) });
}

# Set text content
elsif (length $new) { $content->asset->add_chunk($new) }
else { $content->asset(Mojo::Asset::Memory->new->add_chunk($new)) }

return $self;
}
Expand Down Expand Up @@ -315,7 +307,6 @@ 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 @@ -435,15 +426,9 @@ sub _parse {
my $state = $self->{state} || '';
if ($state eq 'body' || $state eq 'content' || $state eq 'done') {

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

# Until body
my $buffer = delete $self->{buffer};
my $content = $self->content;
my $buffer = delete $self->{buffer};
if ($until_body) { $self->content($content->parse_until_body($buffer)) }

# CGI
Expand Down Expand Up @@ -562,15 +547,6 @@ 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.
Note that this event is EXPERIMENTAL and might change without warning!
=head2 C<finish>
$message->on(finish => sub {
Expand Down Expand Up @@ -643,9 +619,10 @@ Check if message is at least a specific version.
my $string = $message->body;
$message = $message->body('Hello!');
$message = $message->body(sub {...});
my $cb = $message->body(sub {...});
Simple C<content> access.
Access and replace text content or register C<read> event with content, which
will be emitted when new content arrives.
=head2 C<body_params>
Expand Down Expand Up @@ -798,13 +775,6 @@ 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
22 changes: 9 additions & 13 deletions t/mojo/message.t
Expand Up @@ -3,7 +3,7 @@ use Mojo::Base -strict;

use utf8;

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

use File::Spec;
use File::Temp;
Expand Down Expand Up @@ -2281,10 +2281,9 @@ 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;
my $counter2 = $counter = 0;
$req = Mojo::Message::Request->new;
$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 @@ -2312,15 +2311,12 @@ $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 $counter2, 1, 'right count';
is $counter, 8, '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 Expand Up @@ -2595,19 +2591,19 @@ $req->body('');
is $req->body, '', 'right content';
$req->body('hi there!');
is $req->body, 'hi there!', 'right content';
$req->body(undef);
is $req->body, '', 'right content';
is $req->content->has_subscribers('read'), undef, 'no subscribers';
$req->body(sub { });
$cb = $req->body(sub { });
is $req->content->has_subscribers('read'), 1, 'has subscribers';
$req->body(undef);
$req->content->unsubscribe(read => $cb);
is $req->content->has_subscribers('read'), undef, 'no subscribers';
$req->body('');
is $req->body, '', 'right content';
$req->body(0);
is $req->body, 0, 'right content';
is $req->content->has_subscribers('read'), undef, 'no subscribers';
$req->body(sub { });
$cb = $req->body(sub { });
is $req->content->has_subscribers('read'), 1, 'has subscribers';
$req->content->unsubscribe(read => $cb);
$req->body('hello!');
is $req->content->has_subscribers('read'), undef, 'no subscribers';
is $req->body, 'hello!', 'right content';
Expand Down
7 changes: 4 additions & 3 deletions t/mojolicious/upload_stream_lite_app.t
Expand Up @@ -21,8 +21,9 @@ app->hook(
after_build_tx => sub {
my $tx = shift;
weaken $tx;
$tx->req->on_body(
sub { $tx->emit('request') if shift->url->path->contains('/upload') });
$tx->req->content->on_body(
sub { $tx->emit('request') if $tx->req->url->path->contains('/upload') }
);
}
);

Expand All @@ -33,7 +34,7 @@ post '/upload' => sub {

# First invocation, prepare streaming
my $id = $self->param('id');
$self->req->body(sub { $cache->{$id} .= pop });
$self->req->content->on_read(sub { $cache->{$id} .= pop });
return unless $self->req->is_done;

# Second invocation, render response
Expand Down

0 comments on commit 52a0ce2

Please sign in to comment.