Skip to content

Commit

Permalink
renamed parse_header to split_header
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jun 1, 2013
1 parent df3d5e0 commit 55c4922
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 48 deletions.
2 changes: 1 addition & 1 deletion Changes
@@ -1,6 +1,6 @@

4.11 2013-06-02
- Added parse_header function to Mojo::Util.
- Added split_header function to Mojo::Util.
- Fixed small quality detection bug in Mojolicious::Types.
- Fixed a few small boundary and charset detection bugs in Mojo::Content.

Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/Cookie/Request.pm
@@ -1,13 +1,13 @@
package Mojo::Cookie::Request;
use Mojo::Base 'Mojo::Cookie';

use Mojo::Util qw(parse_header quote);
use Mojo::Util qw(quote split_header);

sub parse {
my ($self, $str) = @_;

my @cookies;
my @pairs = map {@$_} @{parse_header($str // '')};
my @pairs = map {@$_} @{split_header($str // '')};
while (@pairs) {
my ($name, $value) = (shift @pairs, shift @pairs);
next if $name =~ /^\$/;
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/Cookie/Response.pm
Expand Up @@ -2,7 +2,7 @@ package Mojo::Cookie::Response;
use Mojo::Base 'Mojo::Cookie';

use Mojo::Date;
use Mojo::Util qw(parse_header quote);
use Mojo::Util qw(quote split_header);

has [qw(domain httponly max_age path secure)];

Expand All @@ -22,7 +22,7 @@ sub parse {
my ($self, $str) = @_;

my @cookies;
my $tree = parse_header($str // '');
my $tree = split_header($str // '');
while (my $pairs = shift @$tree) {
my $i = 0;
while (@$pairs) {
Expand Down
58 changes: 29 additions & 29 deletions lib/Mojo/Util.pm
Expand Up @@ -39,9 +39,9 @@ my %CACHE;
our @EXPORT_OK = (
qw(b64_decode b64_encode camelize class_to_file class_to_path decamelize),
qw(decode deprecated encode get_line hmac_sha1_sum html_unescape md5_bytes),
qw(md5_sum monkey_patch parse_header punycode_decode punycode_encode quote),
qw(secure_compare sha1_bytes sha1_sum slurp spurt squish steady_time trim),
qw(unquote url_escape url_unescape xml_escape xor_encode)
qw(md5_sum monkey_patch punycode_decode punycode_encode quote),
qw(secure_compare sha1_bytes sha1_sum slurp split_header spurt squish),
qw(steady_time trim unquote url_escape url_unescape xml_escape xor_encode)
);

sub b64_decode { decode_base64($_[0]) }
Expand Down Expand Up @@ -129,26 +129,6 @@ sub monkey_patch {
*{"${class}::$_"} = $patch{$_} for keys %patch;
}
sub parse_header {
my $str = shift;
my (@tree, @token);
while ($str =~ s/^[,;\s]*([^=;, ]+)\s*//) {
push @token, $1, undef;
$token[-1] = unquote($1)
if $str =~ s/^=\s*("(?:\\\\|\\"|[^"])*"|[^;, ]*)\s*//;
# Separator
$str =~ s/^;\s*//;
next unless $str =~ s/^,\s*//;
push @tree, [@token];
@token = ();
}
# Take care of final token
return [@token ? (@tree, \@token) : @tree];
}
# Direct translation of RFC 3492
sub punycode_decode {
my $input = shift;
Expand Down Expand Up @@ -264,6 +244,26 @@ sub slurp {
return $content;
}

sub split_header {
my $str = shift;

my (@tree, @token);
while ($str =~ s/^[,;\s]*([^=;, ]+)\s*//) {
push @token, $1, undef;
$token[-1] = unquote($1)
if $str =~ s/^=\s*("(?:\\\\|\\"|[^"])*"|[^;, ]*)\s*//;

# Separator
$str =~ s/^;\s*//;
next unless $str =~ s/^,\s*//;
push @tree, [@token];
@token = ();
}

# Take care of final token
return [@token ? (@tree, \@token) : @tree];
}

sub spurt {
my ($content, $path) = @_;
croak qq{Can't open file "$path": $!} unless open my $file, '>', $path;
Expand Down Expand Up @@ -517,12 +517,6 @@ Monkey patch functions into package.
two => sub { say 'Two!' },
three => sub { say 'Three!' };
=head2 parse_header
my $tree = parse_header 'foo="bar baz"; test=123, yada';
Parse HTTP header value.
=head2 punycode_decode
my $str = punycode_decode $punycode;
Expand Down Expand Up @@ -565,6 +559,12 @@ Generate SHA1 checksum for string.
Read all data at once from file.
=head2 split_header
my $tree = split_header 'foo="bar baz"; test=123, yada';
Split HTTP header value.
=head2 spurt
$content = spurt $content, '/etc/passwd';
Expand Down
28 changes: 14 additions & 14 deletions t/mojo/util.t
Expand Up @@ -11,8 +11,8 @@ use Mojo::DeprecationTest;
use Mojo::Util
qw(b64_decode b64_encode camelize class_to_file class_to_path decamelize),
qw(decode encode get_line hmac_sha1_sum html_unescape md5_bytes md5_sum),
qw(monkey_patch parse_header punycode_decode punycode_encode quote),
qw(secure_compare sha1_bytes sha1_sum slurp spurt squish steady_time trim),
qw(monkey_patch punycode_decode punycode_encode quote secure_compare),
qw(sha1_bytes sha1_sum slurp split_header spurt squish steady_time trim),
qw(unquote url_escape url_unescape xml_escape xor_encode);

# camelize
Expand Down Expand Up @@ -58,28 +58,28 @@ is get_line(\$buffer), 'yada', 'right line';
is $buffer, '', 'no buffer content';
is get_line(\$buffer), undef, 'no line';

# parse_header
is_deeply parse_header(''), [], 'right result';
is_deeply parse_header(',,foo,, ,bar'), [['foo', undef], ['bar', undef]],
# split_header
is_deeply split_header(''), [], 'right result';
is_deeply split_header(',,foo,, ,bar'), [['foo', undef], ['bar', undef]],
'right result';
is_deeply parse_header(';;foo; ; ;bar'), [['foo', undef, 'bar', undef]],
is_deeply split_header(';;foo; ; ;bar'), [['foo', undef, 'bar', undef]],
'right result';
is_deeply parse_header('foo=;bar=""'), [['foo', '', 'bar', '']],
is_deeply split_header('foo=;bar=""'), [['foo', '', 'bar', '']],
'right result';
is_deeply parse_header('foo=bar baz=yada'), [['foo', 'bar', 'baz', 'yada']],
is_deeply split_header('foo=bar baz=yada'), [['foo', 'bar', 'baz', 'yada']],
'right result';
is_deeply parse_header('foo,bar,baz'),
is_deeply split_header('foo,bar,baz'),
[['foo', undef], ['bar', undef], ['baz', undef]], 'right result';
is_deeply parse_header('f "o" o , ba r'),
is_deeply split_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 split_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 split_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>', undef, 'rel', 'x', 't*', 'UTF-8\'de\'a%20b']];
is_deeply parse_header($header), $parsed, 'right result';
is_deeply split_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', undef, 'A', 'b.c', 'D', '/E', 'a-b', '3', 'F', 'Thu'],
Expand All @@ -88,7 +88,7 @@ $parsed = [
'07:07:59', undef, 'GMT', undef, 'Ab', undef
]
];
is_deeply parse_header($header), $parsed, 'right result';
is_deeply split_header($header), $parsed, 'right result';

# b64_encode
is b64_encode('foobar$%^&3217'), "Zm9vYmFyJCVeJjMyMTc=\n",
Expand Down

0 comments on commit 55c4922

Please sign in to comment.