Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
removed a few rarely used methods from Mojo::Message
  • Loading branch information
kraih committed May 10, 2013
1 parent 97d5567 commit 27c8d96
Show file tree
Hide file tree
Showing 17 changed files with 123 additions and 208 deletions.
3 changes: 3 additions & 0 deletions Changes
Expand Up @@ -9,9 +9,12 @@
- Added close_gracefully method to Mojo::IOLoop::Stream.
- Changed Mojolicious default secret to the application moniker to make it
slightly more secure.
- Removed callback support from body method in Mojo::Message.
- Removed Mojolicious::Plugin::PoweredBy and
Mojolicious::Plugin::RequestTimer.
- Removed data attribute from Mojo::URL.
- Removed is_chunked, is_dynamic, is_multipart, has_leftovers, leftovers,
write and write_chunk methods from Mojo::Message.
- Removed deprecated end method from Mojo::IOLoop::Delay.
- Removed deprecated build_form_tx, build_json_tx, post_form and post_json
methods from Mojo::UserAgent.
Expand Down
93 changes: 8 additions & 85 deletions lib/Mojo/Message.pm
Expand Up @@ -10,7 +10,6 @@ use Mojo::JSON::Pointer;
use Mojo::Parameters;
use Mojo::Upload;
use Mojo::Util 'decode';
use Scalar::Util 'weaken';

has content => sub { Mojo::Content::Single->new };
has default_charset => 'UTF-8';
Expand All @@ -28,14 +27,8 @@ sub body {
# Get
return $content->asset->slurp unless defined(my $new = shift);

# Callback
if (ref $new eq 'CODE') {
weaken $self;
return $content->unsubscribe('read')->on(read => sub { $self->$new(pop) });
}

# Set raw content
else { $content->asset(Mojo::Asset::Memory->new->add_chunk($new)) }
$content->asset(Mojo::Asset::Memory->new->add_chunk($new));

return $self;
}
Expand Down Expand Up @@ -85,7 +78,7 @@ sub cookies { croak 'Method "cookies" not implemented by subclass' }
sub dom {
my $self = shift;

return undef if $self->is_multipart;
return undef if $self->content->is_multipart;
my $dom = $self->{dom}
||= Mojo::DOM->new->charset($self->content->charset // undef)
->parse($self->body);
Expand Down Expand Up @@ -121,9 +114,10 @@ sub fix_headers {
my $self = shift;

# Content-Length or Connection (unless chunked transfer encoding is used)
return $self if $self->{fix}++ || $self->is_chunked;
my $content = $self->content;
return $self if $self->{fix}++ || $content->is_chunked;
my $headers = $self->headers;
$self->is_dynamic
$content->is_dynamic
? $headers->connection('close')
: $headers->content_length($self->body_size)
unless $headers->content_length;
Expand Down Expand Up @@ -152,13 +146,9 @@ sub get_start_line_chunk {
croak 'Method "get_start_line_chunk" not implemented by subclass';
}

sub has_leftovers { shift->content->has_leftovers }

sub header_size { shift->fix_headers->content->header_size }

sub headers { shift->content->headers }
sub is_chunked { shift->content->is_chunked }
sub is_dynamic { shift->content->is_dynamic }
sub headers { shift->content->headers }

sub is_finished { (shift->{state} // '') eq 'finished' }

Expand All @@ -167,17 +157,13 @@ sub is_limit_exceeded {
return !!($code eq 413 || $code eq 431);
}

sub is_multipart { shift->content->is_multipart }

sub json {
my ($self, $pointer) = @_;
return undef if $self->is_multipart;
return undef if $self->content->is_multipart;
my $data = $self->{json} ||= Mojo::JSON->new->decode($self->body);
return $pointer ? Mojo::JSON::Pointer->new->get($data, $pointer) : $data;
}

sub leftovers { shift->content->leftovers }

sub param { shift->body_params->param(@_) }

sub parse {
Expand Down Expand Up @@ -256,9 +242,6 @@ sub uploads {
return \@uploads;
}

sub write { shift->_write(write => @_) }
sub write_chunk { shift->_write(write_chunk => @_) }

sub _build {
my ($self, $method) = @_;

Expand Down Expand Up @@ -343,13 +326,6 @@ sub _parse_formdata {
return \@formdata;
}

sub _write {
my ($self, $method, $chunk, $cb) = @_;
weaken $self;
$self->content->$method($chunk => sub { shift and $self->$cb(@_) if $cb });
return $self;
}

1;

=head1 NAME
Expand Down Expand Up @@ -466,14 +442,8 @@ implements the following new ones.
my $bytes = $msg->body;
$msg = $msg->body('Hello!');
my $cb = $msg->body(sub {...});
Access C<content> data or replace all subscribers of the C<read> event.
$msg->body(sub {
my ($msg, $bytes) = @_;
say "Streaming: $bytes";
});
Slurp or replace C<content>.
=head2 body_params
Expand Down Expand Up @@ -592,12 +562,6 @@ Get a chunk of header data, starting from a specific position.
Get a chunk of start line data starting from a specific position. Meant to be
overloaded in a subclass.
=head2 has_leftovers
my $success = $msg->has_leftovers;
Check if there are leftovers.
=head2 header_size
my $size = $msg->header_size;
Expand All @@ -610,19 +574,6 @@ Size of headers in bytes.
Message headers, usually a L<Mojo::Headers> object.
=head2 is_chunked
my $success = $msg->is_chunked;
Check if content is chunked.
=head2 is_dynamic
my $success = $msg->is_dynamic;
Check if content will be dynamically generated, which prevents C<clone> from
working.
=head2 is_finished
my $success = $msg->is_finished;
Expand All @@ -635,12 +586,6 @@ Check if message parser/generator is finished.
Check if message has exceeded C<max_line_size> or C<max_message_size>.
=head2 is_multipart
my $success = $msg->is_multipart;
Check if content is a L<Mojo::Content::MultiPart> object.
=head2 json
my $hash = $msg->json;
Expand All @@ -656,12 +601,6 @@ it should not be called before the entire message body has been received.
say $msg->json->{foo}{bar}[23];
say $msg->json('/foo/bar/23');
=head2 leftovers
my $bytes = $msg->leftovers;
Get leftover data from content parser.
=head2 param
my @names = $msg->param;
Expand Down Expand Up @@ -707,22 +646,6 @@ entire message body has been received.
All C<multipart/form-data> file uploads, usually L<Mojo::Upload> objects.
=head2 write
$msg = $msg->write($bytes);
$msg = $msg->write($bytes => sub {...});
Write dynamic content non-blocking, the optional drain callback will be
invoked once all data has been written.
=head2 write_chunk
$msg = $msg->write_chunk($bytes);
$msg = $msg->write_chunk($bytes => sub {...});
Write dynamic content non-blocking with C<chunked> transfer encoding, the
optional drain callback will be invoked once all data has been written.
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
Expand Down
5 changes: 3 additions & 2 deletions lib/Mojo/Server/Daemon.pm
Expand Up @@ -151,9 +151,10 @@ sub _finish {
return $self->_remove($id) if $req->error || !$tx->keep_alive;

# Build new transaction for leftovers
return unless $req->has_leftovers;
my $content = $req->content;
return unless $content->has_leftovers;
$tx = $c->{tx} = $self->_build_tx($id, $c);
$tx->server_read($req->leftovers);
$tx->server_read($content->leftovers);
}

sub _listen {
Expand Down
7 changes: 4 additions & 3 deletions lib/Mojo/Transaction/HTTP.pm
Expand Up @@ -15,7 +15,8 @@ sub client_read {
return $self->{state} = 'finished'
if !$res->is_status_class(100) || $res->headers->upgrade;
$self->res($res->new)->emit(unexpected => $res);
$self->client_read($res->leftovers) if $res->has_leftovers;
my $content = $res->content;
$self->client_read($content->leftovers) if $content->has_leftovers;
}

sub client_write { shift->_write(0) }
Expand Down Expand Up @@ -62,7 +63,7 @@ sub _body {
# Prepare body chunk
my $buffer = $msg->get_body_chunk($self->{offset});
my $written = defined $buffer ? length $buffer : 0;
$self->{write} = $msg->is_dynamic ? 1 : ($self->{write} - $written);
$self->{write} = $msg->content->is_dynamic ? 1 : ($self->{write} - $written);
$self->{offset} = $self->{offset} + $written;
if (defined $buffer) { delete $self->{delay} }

Expand Down Expand Up @@ -98,7 +99,7 @@ sub _headers {
# Body
else {
$self->{state} = 'write_body';
$self->{write} = $msg->is_dynamic ? 1 : $msg->body_size;
$self->{write} = $msg->content->is_dynamic ? 1 : $msg->body_size;
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/UserAgent.pm
Expand Up @@ -317,7 +317,7 @@ sub _handle {
if (my $jar = $self->cookie_jar) { $jar->extract($old) }
$old->client_close;
$self->_finish($new, $c->{cb});
$new->client_read($old->res->leftovers);
$new->client_read($old->res->content->leftovers);
}

# Finish normal connection
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/UserAgent/Transactor.pm
Expand Up @@ -355,7 +355,7 @@ requests, with support for content generators.
# Streaming response
my $tx = $t->tx(GET => 'http://example.com');
$tx->res->body(sub { say $_[1] });
$tx->res->content->unsubscribe('read')->on(read => sub { say $_[1] });
# Custom socket
my $tx = $t->tx(GET => 'http://example.com');
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojolicious/Command/get.pm
Expand Up @@ -93,8 +93,8 @@ sub run {
$tx->res->on(progress => $cb);

# Stream content
$tx->res->body(
sub {
$tx->res->content->unsubscribe('read')->on(
read => sub {
$cb->(my $res = shift);

# Ignore intermediate content
Expand Down
8 changes: 5 additions & 3 deletions lib/Mojolicious/Controller.pm
Expand Up @@ -70,7 +70,7 @@ sub finish {
$tx->finish(@_) and return $self if $tx->is_websocket;

# Chunked stream
if ($tx->res->is_chunked) {
if ($tx->res->content->is_chunked) {
$self->write_chunk(@_) if @_;
return $self->write_chunk('');
}
Expand Down Expand Up @@ -430,14 +430,16 @@ sub url_for {
sub write {
my ($self, $chunk, $cb) = @_;
($cb, $chunk) = ($chunk, undef) if ref $chunk eq 'CODE';
$self->res->write($chunk => sub { shift and $self->$cb(@_) if $cb });
my $content = $self->res->content;
$content->write($chunk => sub { shift and $self->$cb(@_) if $cb });
return $self->rendered;
}

sub write_chunk {
my ($self, $chunk, $cb) = @_;
($cb, $chunk) = ($chunk, undef) if ref $chunk eq 'CODE';
$self->res->write_chunk($chunk => sub { shift and $self->$cb(@_) if $cb });
my $content = $self->res->content;
$content->write_chunk($chunk => sub { shift and $self->$cb(@_) if $cb });
return $self->rendered;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Test/Mojo.pm
Expand Up @@ -431,7 +431,7 @@ Current transaction, usually a L<Mojo::Transaction::HTTP> object.
# More specific tests
is $t->tx->res->json->{foo}, 'bar', 'right value';
ok $t->tx->res->is_multipart, 'multipart content';
ok $t->tx->res->content->is_multipart, 'multipart content';
# Test custom transactions
$t->tx($t->tx->previous)->status_is(302)->header_like(Location => qr/foo/);
Expand Down

0 comments on commit 27c8d96

Please sign in to comment.