Skip to content

Commit

Permalink
fixed a few parse_header bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jun 1, 2013
1 parent 356a6a7 commit 3612d80
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 21 deletions.
5 changes: 3 additions & 2 deletions lib/Mojo/Cookie/Request.pm
Expand Up @@ -7,8 +7,9 @@ sub parse {
my ($self, $str) = @_;

my @cookies;
for my $token (map {@$_} @{parse_header($str // '')}) {
my ($name, $value) = @$token;
my @pairs = map {@$_} @{parse_header($str // '')};
while (@pairs) {
my ($name, $value) = (shift @pairs, shift @pairs);
next if $name =~ /^\$/;
push @cookies, $self->new(name => $name, value => $value // '');
}
Expand Down
10 changes: 5 additions & 5 deletions lib/Mojo/Cookie/Response.pm
Expand Up @@ -23,16 +23,16 @@ sub parse {

my @cookies;
my $tree = parse_header($str // '');
while (my $token = shift @$tree) {
while (my $pairs = shift @$tree) {
my $i = 0;
while (my $pair = shift @$token) {
my ($name, $value) = @$pair;
while (@$pairs) {
my ($name, $value) = (shift @$pairs, shift @$pairs);

# "expires" is a special case, thank you Netscape...
if ($name =~ /^expires$/i) {
my $next = shift @$tree;
if (my $rest = shift @$next) { $value .= ", $rest->[0]" }
push @$token, @$next;
$value .= join ' ', ',', grep {defined} splice @$next, 0, 10;
push @$pairs, @$next;
}

# This will only run once
Expand Down
13 changes: 6 additions & 7 deletions lib/Mojo/Util.pm
Expand Up @@ -132,16 +132,15 @@ sub monkey_patch {
sub parse_header {
my $str = shift;
# Nibbling parser
my (@tree, @token);
while ($str =~ s/^\s*([^=;,]*[^=;, ])\s*=?\s*//) {
push @token, [$1];
$token[-1][1] = unquote($1)
if $str =~ s/^("(?:\\\\|\\"|[^"])+"|[^;,]+)\s*//;
while ($str =~ s/^\s*([^=;, ]+)\s*//) {
push @token, $1, undef;
$token[-1] = unquote($1)
if $str =~ s/^=\s*("(?:\\\\|\\"|[^"])*"|[^;,]*)\s*//;
# Separator
$str =~ s/^\s*;\s*//;
next unless $str =~ s/^\s*,\s*//;
$str =~ s/^;\s*//;
next unless $str =~ s/^,\s*//;
push @tree, [@token];
@token = ();
}
Expand Down
20 changes: 13 additions & 7 deletions t/mojo/util.t
Expand Up @@ -60,21 +60,27 @@ is get_line(\$buffer), undef, 'no line';

# parse_header
is_deeply parse_header(''), [], 'right result';
is_deeply parse_header('foo,bar,baz'), [[['foo']], [['bar']], [['baz']]],
is_deeply parse_header('foo=;bar=""'), [['foo', '', 'bar', '']],
'right result';
is_deeply parse_header('f "o" o , ba r'), [[['f "o" o']], [['ba r']]],
is_deeply parse_header('foo,bar,baz'),
[['foo', undef], ['bar', undef], ['baz', undef]], 'right result';
is_deeply parse_header('f "o" o , ba r'),
[['f', undef, '"o"', undef, 'o', undef], ['ba', undef, 'r', undef]],
'right result';
is_deeply parse_header('foo="b a\" r\"\\\\"'), [[['foo', 'b a" r"\\']]],
is_deeply parse_header('foo="b a\" r\"\\\\"'), [['foo', 'b a" r"\\']],
'right result';
is_deeply parse_header('foo = "b a\" r\"\\\\"'), [[['foo', 'b a" r"\\']]],
is_deeply parse_header('foo = "b a\" r\"\\\\"'), [['foo', 'b a" r"\\']],
'right result';
my $header = q{</foo/bar>; rel="x"; t*=UTF-8'de'a%20b};
my $parsed = [[['</foo/bar>'], ['rel', 'x'], ['t*', 'UTF-8\'de\'a%20b']]];
my $parsed = [['</foo/bar>', undef, 'rel', 'x', 't*', 'UTF-8\'de\'a%20b']];
is_deeply parse_header($header), $parsed, 'right result';
$header = 'a=b c; A=b.c; D=/E; a-b=3; F=Thu, 07 Aug 2008 07:07:59 GMT; Ab;';
$parsed = [
[['a', 'b c'], ['A', 'b.c'], ['D', '/E'], ['a-b', '3'], ['F', 'Thu']],
[['07 Aug 2008 07:07:59 GMT'], ['Ab']]
['a', 'b c', 'A', 'b.c', 'D', '/E', 'a-b', '3', 'F', 'Thu'],
[
'07', undef, 'Aug', undef, '2008', undef,
'07:07:59', undef, 'GMT', undef, 'Ab', undef
]
];
is_deeply parse_header($header), $parsed, 'right result';

Expand Down

0 comments on commit 3612d80

Please sign in to comment.