Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
handle quoted values separately
  • Loading branch information
kraih committed Feb 10, 2015
1 parent da8ff04 commit 79ee42a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
16 changes: 8 additions & 8 deletions lib/Mojo/Util.pm
Expand Up @@ -50,7 +50,6 @@ my %XML = (

# "Sun, 06 Nov 1994 08:49:37 GMT" and "Sunday, 06-Nov-94 08:49:37 GMT"
my $EXPIRES_RE = qr/(\w+\,\s+\d+\W+\w+\D+\d+\s+\d+:\d+:\d+\s+GMT)/;
my $VALUE_RE = qr/("(?:\\\\|\\"|[^"])*"|[^;, ]*)/;

# Encoding cache
my %CACHE;
Expand Down Expand Up @@ -394,12 +393,14 @@ sub _header {
# Special "expires" value
if ($expires && $str =~ s/^=\s*$EXPIRES_RE//o) { $token[-1] = $1 }

# Normal value
elsif ($str =~ s/^=\s*$VALUE_RE\s*//o) { $token[-1] = unquote $1 }
# Quoted value
elsif ($str =~ s/^=\s*("(?:\\\\|\\"|[^"])*")//) { $token[-1] = unquote $1 }

# Unquoted value
elsif ($str =~ s/^=\s*([^;, ]*)//) { $token[-1] = $1 }

# Separator
$str =~ s/^;\s*//;
next unless $str =~ s/^,\s*//;
next unless $str =~ s/^\s*,\s*//;
push @tree, [@token];
@token = ();
}
Expand Down Expand Up @@ -679,9 +680,8 @@ L<RFC 6265|http://tools.ietf.org/html/rfc6265>.
my $tree = split_header 'foo="bar baz"; test=123, yada';
Split HTTP header value into key/value pairs, keys without a value get
C<undef> assigned, and each comma separated part will get its own array
reference.
Split HTTP header value into key/value pairs, each comma separated part gets
its own array reference, and keys without a value get C<undef> assigned.
# "one"
split_header('one; two="three four", five=six')->[0][0];
Expand Down
2 changes: 2 additions & 0 deletions t/mojo/util.t
Expand Up @@ -55,6 +55,8 @@ is class_to_path("Foo'Bar'Baz"), 'Foo/Bar/Baz.pm', 'right path';
# split_header
is_deeply split_header(''), [], 'right result';
is_deeply split_header('foo=b=a=r'), [['foo', 'b=a=r']], 'right result';
is_deeply split_header('a=b ,, , c=d ;; ; e=f g h=i'),
[['a', 'b'], ['c', 'd', 'e', 'f', 'g', undef, 'h', 'i']], 'right result';
is_deeply split_header(',,foo,, ,bar'), [['foo', undef], ['bar', undef]],
'right result';
is_deeply split_header(';;foo; ; ;bar'), [['foo', undef, 'bar', undef]],
Expand Down

0 comments on commit 79ee42a

Please sign in to comment.