Navigation Menu

Skip to content

Commit

Permalink
added finish_cb callback to Mojo::Message
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Mar 2, 2010
1 parent e84033c commit da2c2a6
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 23 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -8,6 +8,7 @@ This file documents the revision history for Perl extension Mojolicious.
That means "plackup myapp.pl" and "plackup script/myapp" should
just work.
- Added referrer method to Mojo::Headers. (esskar)
- Added finish_cb callback to Mojo::Message.
- Removed bundled RFCs.
- Fixed IRI handling. (sharifulin)
- Fixed mixed IRI/IDNA handling.
Expand Down
12 changes: 1 addition & 11 deletions lib/Mojo/Content.pm
Expand Up @@ -14,7 +14,7 @@ use Mojo::Headers;

use constant CHUNK_SIZE => $ENV{MOJO_CHUNK_SIZE} || 8192;

__PACKAGE__->attr([qw/body_cb filter progress_cb/]);
__PACKAGE__->attr([qw/body_cb filter/]);
__PACKAGE__->attr([qw/buffer filter_buffer/] => sub { Mojo::ByteStream->new }
);
__PACKAGE__->attr(headers => sub { Mojo::Headers->new });
Expand Down Expand Up @@ -356,16 +356,6 @@ Input buffer for filtering.
The headers.
=head2 C<progress_cb>
my $cb = $content->progress_cb;
$content = $content->progress_cb(sub {
my $self = shift;
print '+';
});
Progress reporting callback.
=head2 C<relaxed>
my $relaxed = $content->relaxed;
Expand Down
3 changes: 0 additions & 3 deletions lib/Mojo/Content/Single.pm
Expand Up @@ -28,9 +28,6 @@ sub body_size { shift->asset->size }
sub get_body_chunk {
my ($self, $offset) = @_;

# Progress
$self->progress_cb->($self, 'body', $offset) if $self->progress_cb;

# Body generator
return $self->generate_body_chunk($offset) if $self->body_cb;

Expand Down
52 changes: 44 additions & 8 deletions lib/Mojo/Message.pm
Expand Up @@ -19,7 +19,8 @@ use constant CHUNK_SIZE => $ENV{MOJO_CHUNK_SIZE} || 8192;

__PACKAGE__->attr(buffer => sub { Mojo::ByteStream->new });
__PACKAGE__->attr(content => sub { Mojo::Content::Single->new });
__PACKAGE__->attr(default_charset => 'UTF-8');
__PACKAGE__->attr(default_charset => 'UTF-8');
__PACKAGE__->attr([qw/finish_cb progress_cb/]);
__PACKAGE__->attr([qw/major_version minor_version/] => 1);

# I'll keep it short and sweet. Family. Religion. Friendship.
Expand Down Expand Up @@ -155,7 +156,17 @@ sub build {
# It cost 80 million dollars to make.
# How do you sleep at night?
# On top of a pile of money, with many beautiful women.
sub build_body { shift->content->build_body(@_) }
sub build_body {
my $self = shift;

# Body
my $body = $self->content->build_body(@_);

# Finished
if (my $cb = $self->finish_cb) { $self->$cb }

return $body;
}

sub build_headers {
my $self = shift;
Expand Down Expand Up @@ -242,13 +253,28 @@ sub fix_headers {
return $self;
}

sub get_body_chunk { shift->content->get_body_chunk(@_) }
sub get_body_chunk {
my $self = shift;

# Progress
if (my $cb = $self->progress_cb) { $self->$cb('body', @_) }

# Chunk
if (defined(my $chunk = $self->content->get_body_chunk(@_))) {
return $chunk;
}

# Finished
if (my $cb = $self->finish_cb) { $self->$cb }

return;
}

sub get_header_chunk {
my $self = shift;

# Progress
$self->progress_cb->($self, 'headers', @_) if $self->progress_cb;
if (my $cb = $self->progress_cb) { $self->$cb('headers', @_) }

# HTTP 0.9 has no headers
return '' if $self->version eq '0.9';
Expand All @@ -263,7 +289,7 @@ sub get_start_line_chunk {
my ($self, $offset) = @_;

# Progress
$self->progress_cb->($self, 'start_line', $offset) if $self->progress_cb;
if (my $cb = $self->progress_cb) { $self->$cb('start_line', @_) }

my $copy = $self->_build_start_line;
return substr($copy, $offset, CHUNK_SIZE);
Expand Down Expand Up @@ -312,8 +338,6 @@ sub parse_until_body {
return $self->_parse(1);
}

sub progress_cb { shift->content->progress_cb(@_) }

sub start_line_size { length shift->build_start_line }

sub to_string { shift->build(@_) }
Expand Down Expand Up @@ -410,7 +434,7 @@ sub _parse {
my $until_body = @_ ? shift : 0;

# Progress
$self->progress_cb->($self) if $self->progress_cb;
if (my $cb = $self->progress_cb) { $self->$cb }

# Start line and headers
my $buffer = $self->buffer;
Expand Down Expand Up @@ -452,6 +476,9 @@ sub _parse {
$self->state('done_with_leftovers')
if $self->content->is_state('done_with_leftovers');

# Finished
if ((my $cb = $self->finish_cb) && $self->is_finished) { $self->$cb }

return $self;
}

Expand Down Expand Up @@ -545,6 +572,15 @@ Content container, defaults to a L<Mojo::Content::Single> object.
Default charset used for form data parsing.
=head2 C<finish_cb>
my $cb = $message->finish_cb;
$message = $message->finish_cb(sub {
my $self = shift;
});
Callback called after message building or parsing is finished.
=head2 C<headers>
my $headers = $message->headers;
Expand Down
4 changes: 3 additions & 1 deletion t/mojo/message.t
Expand Up @@ -7,7 +7,7 @@ use warnings;

use utf8;

use Test::More tests => 492;
use Test::More tests => 494;

use File::Spec;
use File::Temp;
Expand Down Expand Up @@ -137,6 +137,7 @@ is($req->headers->content_length, 27);

# Parse full HTTP 1.0 request with zero chunk
$req = Mojo::Message::Request->new;
$req->finish_cb(sub { is($_[0]->state, 'done') });
$req->parse('GET /foo/bar/baz.html?fo');
$req->parse("o=13#23 HTTP/1.0\x0d\x0aContent");
$req->parse('-Type: text/');
Expand Down Expand Up @@ -404,6 +405,7 @@ is($req->build,

# Build full HTTP 1.1 request
$req = Mojo::Message::Request->new;
$req->finish_cb(sub { is($_[0]->state, 'start') });
$req->method('GET');
$req->url->parse('http://127.0.0.1/foo/bar');
$req->headers->expect('100-continue');
Expand Down

0 comments on commit da2c2a6

Please sign in to comment.