Skip to content

Commit

Permalink
small optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed May 31, 2013
1 parent 5065000 commit 87e1711
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 49 deletions.
16 changes: 8 additions & 8 deletions lib/Mojo/Message.pm
Expand Up @@ -41,9 +41,9 @@ sub body_params {
my $params = $self->{body_params} = Mojo::Parameters->new;
$params->charset($self->content->charset || $self->default_charset);

# "x-application-urlencoded" and "application/x-www-form-urlencoded"
# "application/x-www-form-urlencoded"
my $type = $self->headers->content_type // '';
if ($type =~ m!(?:x-application|application/x-www-form)-urlencoded!i) {
if ($type =~ m!application/x-www-form-urlencoded!i) {
$params->parse($self->content->asset->slurp);
}

Expand Down Expand Up @@ -426,12 +426,12 @@ Slurp or replace C<content>.
my $params = $msg->body_params;
POST parameters extracted from C<x-application-urlencoded>,
C<application/x-www-form-urlencoded> or C<multipart/form-data> message body,
usually a L<Mojo::Parameters> object. Note that this method caches all data,
so it should not be called before the entire message body has been received.
Also note that message content needs to be loaded into memory to parse POST
parameters, so you have to make sure it is not excessively large.
POST parameters extracted from C<application/x-www-form-urlencoded> or
C<multipart/form-data> message body, usually a L<Mojo::Parameters> object.
Note that this method caches all data, so it should not be called before the
entire message body has been received. Also note that message content needs to
be loaded into memory to parse POST parameters, so you have to make sure it is
not excessively large.
# Get POST parameter value
say $msg->body_params->param('foo');
Expand Down
41 changes: 0 additions & 41 deletions t/mojo/request.t
Expand Up @@ -540,47 +540,6 @@ is $req->headers->content_length, 13, 'right "Content-Length" value';
is $req->headers->content_type, 'text/plain', 'right "Content-Type" value';
is $buffer, 'abcdabcdefghi', 'right content';

# Parse HTTP 1.1 "x-application-urlencoded"
$req = Mojo::Message::Request->new;
$req->parse("POST /foo/bar/baz.html?foo=13#23 HTTP/1.1\x0d\x0a");
$req->parse("Content-Length: 25\x0d\x0a");
$req->parse("Content-Type: x-application-urlencoded\x0d\x0a\x0d\x0a");
$req->parse('foo=bar& tset=23+&foo=bar');
ok $req->is_finished, 'request is finished';
is $req->method, 'POST', 'right method';
is $req->version, '1.1', 'right version';
is $req->url, '/foo/bar/baz.html?foo=13#23', 'right URL';
is $req->headers->content_type, 'x-application-urlencoded',
'right "Content-Type" value';
ok !$req->content->asset->is_file, 'stored in memory';
is $req->content->asset->size, 25, 'right size';
is $req->content->asset->slurp, 'foo=bar& tset=23+&foo=bar', 'right content';
is_deeply $req->body_params->to_hash->{foo}, [qw(bar bar)], 'right values';
is $req->body_params->to_hash->{' tset'}, '23 ', 'right value';
is $req->body_params, 'foo=bar&+tset=23+&foo=bar', 'right parameters';
is_deeply $req->params->to_hash->{foo}, [qw(bar bar 13)], 'right values';

# Parse HTTP 1.1 "x-application-urlencoded" (too big for memory)
$req = Mojo::Message::Request->new;
$req->content->asset->max_memory_size(10);
$req->parse("POST /foo/bar/baz.html?foo=13#23 HTTP/1.1\x0d\x0a");
$req->parse("Content-Length: 25\x0d\x0a");
$req->parse("Content-Type: x-application-urlencoded\x0d\x0a\x0d\x0a");
$req->parse('foo=bar& tset=23+&foo=bar');
ok $req->is_finished, 'request is finished';
is $req->method, 'POST', 'right method';
is $req->version, '1.1', 'right version';
is $req->url, '/foo/bar/baz.html?foo=13#23', 'right URL';
is $req->headers->content_type, 'x-application-urlencoded',
'right "Content-Type" value';
ok $req->content->asset->is_file, 'stored in file';
is $req->content->asset->size, 25, 'right size';
is $req->content->asset->slurp, 'foo=bar& tset=23+&foo=bar', 'right content';
is_deeply $req->body_params->to_hash->{foo}, [qw(bar bar)], 'right values';
is $req->body_params->to_hash->{' tset'}, '23 ', 'right value';
is $req->body_params, 'foo=bar&+tset=23+&foo=bar', 'right parameters';
is_deeply $req->params->to_hash->{foo}, [qw(bar bar 13)], 'right values';

# Parse HTTP 1.1 "application/x-www-form-urlencoded"
$req = Mojo::Message::Request->new;
$req->parse("POST /foo/bar/baz.html?foo=13#23 HTTP/1.1\x0d\x0a");
Expand Down

0 comments on commit 87e1711

Please sign in to comment.