Skip to content

Commit

Permalink
fixed quoting bug in Mojo::Cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Aug 25, 2011
1 parent 0e02228 commit 64e9f3d
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 15 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -2,6 +2,7 @@ This file documents the revision history for Perl extension Mojolicious.

1.92 2011-08-25 00:00:00
- Improved documentation.
- Fixed quoting bug in Mojo::Cookie.

1.91 2011-08-25 00:00:00
- Added EXPERIMENTAL support for cloning Mojo::Message::Request
Expand Down
14 changes: 10 additions & 4 deletions lib/Mojo/Cookie.pm
Expand Up @@ -6,7 +6,6 @@ use overload
fallback => 1;

use Carp 'croak';
use Mojo::Util 'unquote';

has [qw/name path value version/];

Expand All @@ -21,7 +20,15 @@ my $NAME_RE = qr/
\s*
/x;
my $SEPARATOR_RE = qr/^\s*\;\s*/;
my $VALUE_RE = qr/^([^\;\,]+)\s*/;
my $VALUE_RE = qr/
^
(
"(?:\\\\|\\"|[^"])+" # Quoted
|
[^\;\,]+ # Unquoted
)
\s*
/x;

# "My Homer is not a communist.
# He may be a liar, a pig, an idiot, a communist,
Expand Down Expand Up @@ -83,8 +90,7 @@ Mojo::Cookie - HTTP 1.1 Cookie Base Class
=head1 DESCRIPTION
L<Mojo::Cookie> is an abstract base class for HTTP 1.1 cookies as described
in RFC 2965.
L<Mojo::Cookie> is an abstract base class for HTTP 1.1 cookies.
=head1 ATTRIBUTES
Expand Down
3 changes: 1 addition & 2 deletions lib/Mojo/Cookie/Request.pm
Expand Up @@ -87,8 +87,7 @@ Mojo::Cookie::Request - HTTP 1.1 Request Cookie Container
=head1 DESCRIPTION
L<Mojo::Cookie::Request> is a container for HTTP 1.1 request cookies as
described in RFC 2965.
L<Mojo::Cookie::Request> is a container for HTTP 1.1 request cookies.
=head1 ATTRIBUTES
Expand Down
3 changes: 1 addition & 2 deletions lib/Mojo/Cookie/Response.pm
Expand Up @@ -139,8 +139,7 @@ Mojo::Cookie::Response - HTTP 1.1 Response Cookie Container
=head1 DESCRIPTION
L<Mojo::Cookie::Response> is a container for HTTP 1.1 response cookies as
described in RFC 2965.
L<Mojo::Cookie::Response> is a container for HTTP 1.1 response cookies.
=head1 ATTRIBUTES
Expand Down
74 changes: 67 additions & 7 deletions t/mojo/cookie.t
@@ -1,7 +1,7 @@
#!/usr/bin/env perl
use Mojo::Base -strict;

use Test::More tests => 91;
use Test::More tests => 130;

# "What good is money if it can't inspire terror in your fellow man?"
use_ok 'Mojo::Cookie::Request';
Expand Down Expand Up @@ -45,6 +45,39 @@ is $cookies->[0]->name, 'foo', 'right name';
is $cookies->[0]->value, 'bar', 'right value';
is $cookies->[0]->path, '/test', 'right path';
is $cookies->[0]->version, '1', 'right version';
is $cookies->[1], undef, 'no more cookies';

# Parse request cookie (Netscape)
$cookie = Mojo::Cookie::Request->new;
$cookies = $cookie->parse('CUSTOMER=WILE_E_COYOTE');
is $cookies->[0]->name, 'CUSTOMER', 'right name';
is $cookies->[0]->value, 'WILE_E_COYOTE', 'right value';
is $cookies->[0]->version, '1', 'right version';
is $cookies->[1], undef, 'no more cookies';

# Parse multiple request cookies (Netscape)
$cookie = Mojo::Cookie::Request->new;
$cookies =
$cookie->parse('CUSTOMER=WILE_E_COYOTE; PART_NUMBER=ROCKET_LAUNCHER_0001');
is $cookies->[0]->name, 'CUSTOMER', 'right name';
is $cookies->[0]->value, 'WILE_E_COYOTE', 'right value';
is $cookies->[0]->version, '1', 'right version';
is $cookies->[1]->name, 'PART_NUMBER', 'right name';
is $cookies->[1]->value, 'ROCKET_LAUNCHER_0001', 'right value';
is $cookies->[1]->version, '1', 'right version';
is $cookies->[2], undef, 'no more cookies';

# Parse multiple request cookies from multiple header values (Netscape)
$cookie = Mojo::Cookie::Request->new;
$cookies =
$cookie->parse('CUSTOMER=WILE_E_COYOTE, PART_NUMBER=ROCKET_LAUNCHER_0001');
is $cookies->[0]->name, 'CUSTOMER', 'right name';
is $cookies->[0]->value, 'WILE_E_COYOTE', 'right value';
is $cookies->[0]->version, '1', 'right version';
is $cookies->[1]->name, 'PART_NUMBER', 'right name';
is $cookies->[1]->value, 'ROCKET_LAUNCHER_0001', 'right value';
is $cookies->[1]->version, '1', 'right version';
is $cookies->[2], undef, 'no more cookies';

# Parse request cookie without value
$cookie = Mojo::Cookie::Request->new;
Expand All @@ -55,6 +88,7 @@ is $cookies->[0]->path, '/test', 'right path';
is $cookies->[0]->version, '1', 'right version';
is $cookies->[0]->to_string_with_prefix, '$Version=1; foo=; $Path=/test',
'right result';
is $cookies->[1], undef, 'no more cookies';
$cookie = Mojo::Cookie::Request->new;
$cookies = $cookie->parse('$Version=1; foo=""; $Path="/test"');
is $cookies->[0]->name, 'foo', 'right name';
Expand All @@ -63,14 +97,16 @@ is $cookies->[0]->path, '/test', 'right path';
is $cookies->[0]->version, '1', 'right version';
is $cookies->[0]->to_string_with_prefix, '$Version=1; foo=; $Path=/test',
'right result';
is $cookies->[1], undef, 'no more cookies';

# Parse quoted request cookie
$cookie = Mojo::Cookie::Request->new;
$cookies = $cookie->parse('$Version=1; foo="b a\" r\"\\"; $Path="/test"');
is $cookies->[0]->name, 'foo', 'right name';
is $cookies->[0]->value, 'b a" r"\\', 'right value';
is $cookies->[0]->path, '/test', 'right path';
is $cookies->[0]->version, '1', 'right version';
$cookies = $cookie->parse('$Version=1; foo="b ,a\" r\"\\\\"; $Path="/test"');
is $cookies->[0]->name, 'foo', 'right name';
is $cookies->[0]->value, 'b ,a" r"\\', 'right value';
is $cookies->[0]->path, '/test', 'right path';
is $cookies->[0]->version, '1', 'right version';
is $cookies->[1], undef, 'no more cookies';

# Parse multiple cookie request
$cookies = Mojo::Cookie::Request->parse(
Expand All @@ -83,6 +119,7 @@ is $cookies->[1]->name, 'baz', 'right name';
is $cookies->[1]->value, 'la la', 'right value';
is $cookies->[1]->path, '/tset', 'right path';
is $cookies->[1]->version, '1', 'right version';
is $cookies->[2], undef, 'no more cookies';

# Response cookie as string
$cookie = Mojo::Cookie::Response->new;
Expand Down Expand Up @@ -139,10 +176,11 @@ is $cookies->[0]->port, '80 8080', 'right port';
is $cookies->[0]->secure, '1', 'right secure flag';
is $cookies->[0]->comment, 'lalalala', 'right comment';
is $cookies->[0]->version, '1', 'right version';
is $cookies->[1], undef, 'no more cookies';

# Parse quoted response cookie
$cookies = Mojo::Cookie::Response->parse(
'foo="b a\" r\"\\"; Version=1; Domain=kraih.com; Path=/test; Max-Age=60;'
'foo="b a\" r\"\\\\"; Version=1; Domain=kraih.com; Path=/test; Max-Age=60;'
. ' expires=Thu, 07 Aug 2008 07:07:59 GMT; Port="80 8080"; Secure;'
. ' Comment=lalalala');
is $cookies->[0]->name, 'foo', 'right name';
Expand All @@ -156,6 +194,25 @@ is $cookies->[0]->port, '80 8080', 'right port';
is $cookies->[0]->secure, '1', 'right secure flag';
is $cookies->[0]->comment, 'lalalala', 'right comment';
is $cookies->[0]->version, '1', 'right version';
is $cookies->[1], undef, 'no more cookies';

# Parse quoted response cookie (alternative)
$cookies = Mojo::Cookie::Response->parse(
'foo="b a\" ;r\"\\\\"; Version=1; Domain=kraih.com; Path=/test; Max-Age=60;'
. ' expires=Thu, 07 Aug 2008 07:07:59 GMT; Port="80 8080"; Secure;'
. ' Comment=lalalala');
is $cookies->[0]->name, 'foo', 'right name';
is $cookies->[0]->value, 'b a" ;r"\\', 'right value';
is $cookies->[0]->domain, 'kraih.com', 'right domain';
is $cookies->[0]->path, '/test', 'right path';
is $cookies->[0]->max_age, 60, 'right max age value';
is $cookies->[0]->expires, 'Thu, 07 Aug 2008 07:07:59 GMT',
'right expires value';
is $cookies->[0]->port, '80 8080', 'right port';
is $cookies->[0]->secure, '1', 'right secure flag';
is $cookies->[0]->comment, 'lalalala', 'right comment';
is $cookies->[0]->version, '1', 'right version';
is $cookies->[1], undef, 'no more cookies';

# Parse response cookie without value
$cookies = Mojo::Cookie::Response->parse(
Expand All @@ -177,6 +234,7 @@ is $cookies->[0]->to_string,
'foo=; Version=1; Domain=kraih.com; Path=/test; Max-Age=60;'
. ' expires=Thu, 07 Aug 2008 07:07:59 GMT; Port="80 8080"; Secure;'
. ' Comment=lalalala', 'right result';
is $cookies->[1], undef, 'no more cookies';
$cookies = Mojo::Cookie::Response->parse(
'foo=; Version=1; Domain=kraih.com; Path=/test; Max-Age=60;'
. ' expires=Thu, 07 Aug 2008 07:07:59 GMT; Port="80 8080"; Secure;'
Expand All @@ -196,6 +254,7 @@ is $cookies->[0]->to_string,
'foo=; Version=1; Domain=kraih.com; Path=/test; Max-Age=60;'
. ' expires=Thu, 07 Aug 2008 07:07:59 GMT; Port="80 8080"; Secure;'
. ' Comment=lalalala', 'right result';
is $cookies->[1], undef, 'no more cookies';

# Cookie with Max-Age 0 and expires 0
$cookie = Mojo::Cookie::Response->new;
Expand Down Expand Up @@ -223,3 +282,4 @@ is $cookies->[0]->expires->epoch, 0, 'right expires epoch value';
is $cookies->[0]->secure, '1', 'right secure flag';
is $cookies->[0]->comment, 'lalalala', 'right comment';
is $cookies->[0]->version, '1', 'right version';
is $cookies->[1], undef, 'no more cookies';

0 comments on commit 64e9f3d

Please sign in to comment.