Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
removed pair_separator attribute from Mojo::Parameters
  • Loading branch information
kraih committed May 14, 2013
1 parent 485fa33 commit 08ac740
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 58 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -15,6 +15,7 @@
- Removed callback support from body method in Mojo::Message.
- Removed Mojolicious::Plugin::PoweredBy and
Mojolicious::Plugin::RequestTimer.
- Removed pair_separator attribute from Mojolicious::Parameters.
- Removed data attribute from Mojo::URL.
- Removed captures attribute from Mojolicious::Routes::Match.
- Removed charset attribute from Mojo::DOM::HTML.
Expand Down
41 changes: 13 additions & 28 deletions lib/Mojo/Parameters.pm
Expand Up @@ -8,8 +8,7 @@ use overload

use Mojo::Util qw(decode encode url_escape url_unescape);

has charset => 'UTF-8';
has pair_separator => '&';
has charset => 'UTF-8';

sub new { shift->SUPER::new->parse(@_) }

Expand All @@ -34,8 +33,7 @@ sub append {
sub clone {
my $self = shift;

my $clone = $self->new->charset($self->charset)
->pair_separator($self->pair_separator);
my $clone = $self->new->charset($self->charset);
if (defined $self->{string}) { $clone->{string} = $self->{string} }
else { $clone->params([@{$self->params}]) }

Expand Down Expand Up @@ -81,16 +79,13 @@ sub params {
# Parse string
if (defined(my $str = delete $self->{string})) {
my $params = $self->{params} = [];

# Detect pair separator for reconstruction
return $params unless length($str // '');
$self->pair_separator(';') if $str =~ /;/ && $str !~ /\&/;
return $params unless length $str;

# W3C suggests to also accept ";" as a separator
my $charset = $self->charset;
for my $pair (split /[\&\;]+/, $str) {
$pair =~ /^([^=]*)(?:=(.*))?$/;
my $name = $1 // '';
for my $pair (split /&|;/, $str) {
next unless $pair =~ /^([^=]+)(?:=(.*))?$/;
my $name = $1;
my $value = $2 // '';

# Replace "+" with whitespace, unescape and decode
Expand Down Expand Up @@ -171,19 +166,16 @@ sub to_string {
my ($name, $value) = @{$params}[$i, $i + 1];

# Escape and replace whitespace with "+"
$name = encode $charset, $name if $charset;
$name = url_escape $name, '^A-Za-z0-9\-._~!$\'()*,:@/?';
$name =~ s/\%20/\+/g;
if ($value) {
$value = encode $charset, $value if $charset;
$value = url_escape $value, '^A-Za-z0-9\-._~!$\'()*,:@/?';
$value =~ s/\%20/\+/g;
}
$name = encode $charset, $name if $charset;
$name = url_escape $name, '^A-Za-z0-9\-._~!$\'()*,:@/?';
$value = encode $charset, $value if $charset;
$value = url_escape $value, '^A-Za-z0-9\-._~!$\'()*,:@/?';
s/\%20/\+/g for $name, $value;

push @pairs, defined $value ? "$name=$value" : $name;
push @pairs, "$name=$value";
}

return join $self->pair_separator, @pairs;
return join '&', @pairs;
}

1;
Expand Down Expand Up @@ -225,13 +217,6 @@ Charset used for encoding and decoding parameters, defaults to C<UTF-8>.
# Disable encoding and decoding
$params->charset(undef);
=head2 pair_separator
my $separator = $params->pair_separator;
$params = $params->pair_separator(';');
Separator for parameter pairs, defaults to C<&>.
=head1 METHODS
L<Mojo::Parameters> inherits all methods from L<Mojo::Base> and implements the
Expand Down
29 changes: 11 additions & 18 deletions t/mojo/parameters.t
Expand Up @@ -6,26 +6,22 @@ use Mojo::Parameters;
# Basic functionality
my $params = Mojo::Parameters->new('foo=b%3Bar&baz=23');
my $params2 = Mojo::Parameters->new('x', 1, 'y', 2);
is $params->pair_separator, '&', 'right pair separator';
is $params->to_string, 'foo=b%3Bar&baz=23', 'right format';
is $params2->to_string, 'x=1&y=2', 'right format';
is $params->to_string, 'foo=b%3Bar&baz=23', 'right format';
is $params->to_string, 'foo=b%3Bar&baz=23', 'right format';
is $params2->to_string, 'x=1&y=2', 'right format';
is $params->to_string, 'foo=b%3Bar&baz=23', 'right format';
is_deeply $params->params, ['foo', 'b;ar', 'baz', 23], 'right structure';
is $params->[0], 'foo', 'right value';
is $params->[1], 'b;ar', 'right value';
is $params->[2], 'baz', 'right value';
is $params->[3], 23, 'right value';
is $params->[4], undef, 'no value';
$params->pair_separator(';');
is $params->to_string, 'foo=b%3Bar;baz=23', 'right format';
is "$params", 'foo=b%3Bar;baz=23', 'right format';

# Append
is_deeply $params->params, ['foo', 'b;ar', 'baz', 23], 'right structure';
$params->append(a => 4, a => 5, b => 6, b => 7);
is $params->to_string, "foo=b%3Bar;baz=23;a=4;a=5;b=6;b=7", 'right format';
is $params->to_string, "foo=b%3Bar&baz=23&a=4&a=5&b=6&b=7", 'right format';
push @$params, c => 'f;oo';
is $params->to_string, "foo=b%3Bar;baz=23;a=4;a=5;b=6;b=7;c=f%3Boo",
is $params->to_string, "foo=b%3Bar&baz=23&a=4&a=5&b=6&b=7&c=f%3Boo",
'right format';

# Clone
Expand All @@ -36,7 +32,7 @@ isnt "$params", "$clone", 'unequal parameters';

# Merge
$params->merge($params2);
is $params->to_string, 'foo=b%3Bar;baz=23;a=4;a=5;b=6;b=7;c=f%3Boo;x=1;y=2',
is $params->to_string, 'foo=b%3Bar&baz=23&a=4&a=5&b=6&b=7&c=f%3Boo&x=1&y=2',
'right format';
is $params2->to_string, 'x=1&y=2', 'right format';

Expand All @@ -53,9 +49,9 @@ $params->parse('q=1;w=2;e=3;e=4;r=6;t=7');
is $params->to_string, 'q=1;w=2;e=3;e=4;r=6;t=7', 'right format';

# Remove
is $params->remove('r')->to_string, 'q=1;w=2;e=3;e=4;t=7', 'right format';
is $params->remove('r')->to_string, 'q=1&w=2&e=3&e=4&t=7', 'right format';
$params->remove('e');
is $params->to_string, 'q=1;w=2;t=7', 'right format';
is $params->to_string, 'q=1&w=2&t=7', 'right format';

# Hash
is_deeply $params->to_hash, {q => 1, w => 2, t => 7}, 'right structure';
Expand All @@ -72,7 +68,7 @@ is $params->to_string, 'foo=&bar=bar', 'right format';
$params = Mojo::Parameters->new(bar => 'bar', foo => undef);
is $params->to_string, 'bar=bar&foo=', 'right format';

# 0 value
# "0"
$params = Mojo::Parameters->new(foo => 0);
is_deeply $params->param('foo'), 0, 'right structure';
is $params->to_string, 'foo=0', 'right format';
Expand All @@ -84,14 +80,11 @@ is $params->to_string, 'foo=0', 'right format';

# Semicolon
$params = Mojo::Parameters->new('foo=bar;baz');
is $params->pair_separator, '&', 'right pair separator';
is $params->to_string, 'foo=bar;baz', 'right format';
is $params->to_string, 'foo=bar;baz', 'right format';
is_deeply $params->params, [foo => 'bar', baz => ''], 'right structure';
is_deeply $params->to_hash, {foo => 'bar', baz => ''}, 'right structure';
is $params->pair_separator, ';', 'right pair separator';
is $params->to_string, 'foo=bar;baz=', 'right format';
is $params->to_string, 'foo=bar&baz=', 'right format';
$params = Mojo::Parameters->new('foo=bar%3Bbaz');
is $params->pair_separator, '&', 'right pair separator';
is_deeply $params->params, [foo => 'bar;baz'], 'right structure';
is_deeply $params->to_hash, {foo => 'bar;baz'}, 'right structure';
is $params->to_string, 'foo=bar%3Bbaz', 'right format';
Expand Down
24 changes: 12 additions & 12 deletions t/mojo/request.t
Expand Up @@ -543,18 +543,18 @@ 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: 26\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');
$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, 26, 'right size';
is $req->content->asset->slurp, 'foo=bar& tset=23+;&foo=bar', 'right content';
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';
Expand All @@ -564,18 +564,18 @@ is_deeply $req->params->to_hash->{foo}, [qw(bar bar 13)], 'right values';
$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: 26\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');
$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, 26, 'right size';
is $req->content->asset->slurp, 'foo=bar& tset=23+;&foo=bar', 'right content';
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';
Expand All @@ -584,17 +584,17 @@ 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");
$req->parse("Content-Length: 26\x0d\x0a");
$req->parse("Content-Length: 25\x0d\x0a");
$req->parse("Content-Type: application/x-www-form-urlencoded\x0d\x0a");
$req->parse("\x0d\x0afoo=bar&+tset=23+;&foo=bar");
$req->parse("\x0d\x0afoo=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, 'application/x-www-form-urlencoded',
'right "Content-Type" value';
is $req->content->asset->size, 26, 'right size';
is $req->content->asset->slurp, 'foo=bar&+tset=23+;&foo=bar', 'right content';
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';
Expand Down

0 comments on commit 08ac740

Please sign in to comment.