Skip to content

Commit

Permalink
fixed bug in Mojo::Headers that prevented multiline headers from bein…
Browse files Browse the repository at this point in the history
…g parsed correctly
  • Loading branch information
kraih committed Mar 13, 2013
1 parent 4034235 commit f43d247
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
2 changes: 2 additions & 0 deletions Changes
Expand Up @@ -5,6 +5,8 @@
- Improved dumper helper to sort hash keys.
- Improved documentation.
- Improved tests.
- Fixed bug in Mojo::Headers that prevented multiline headers from being
parsed correctly.

3.89 2013-03-04
- Improved documentation.
Expand Down
16 changes: 10 additions & 6 deletions lib/Mojo/Headers.pm
Expand Up @@ -102,10 +102,10 @@ sub parse {
}

# New header
if ($line =~ /^(\S+)\s*:\s*(.*)$/) { push @$headers, $1, $2 }
if ($line =~ /^(\S+)\s*:\s*(.*)$/) { push @$headers, $1, [$2] }

# Multiline
elsif (@$headers && $line =~ s/^\s+//) { $headers->[-1] .= " $line" }
elsif (@$headers && $line =~ s/^\s+//) { push @{$headers->[-1]}, $line }

# Empty line
else {
Expand Down Expand Up @@ -245,7 +245,9 @@ Shortcut for the C<Accept-Ranges> header.
=head2 add
$headers = $headers->add('Content-Type', 'text/plain');
$headers = $headers->add(Foo => 'one value');
$headers = $headers->add(Foo => 'first value', 'second value');
$headers = $headers->add(Foo => ['first line', 'second line']);
Add one or more header lines.
Expand Down Expand Up @@ -363,9 +365,11 @@ Parse headers from a hash reference.
=head2 header
my $string = $headers->header('Content-Type');
my @lines = $headers->header('Content-Type');
$headers = $headers->header('Content-Type' => 'text/plain');
my $string = $headers->header('Foo');
my @lines = $headers->header('Foo');
$headers = $headers->header(Foo => 'one value');
$headers = $headers->header(Foo => 'first value', 'second value');
$headers = $headers->header(Foo => ['first line', 'second line']);
Get or replace the current header values.
Expand Down
30 changes: 26 additions & 4 deletions t/mojo/headers.t
Expand Up @@ -5,8 +5,8 @@ use Mojo::Headers;

# Basic functionality
my $headers = Mojo::Headers->new;
$headers->add('Connection', 'close');
$headers->add('Connection', 'keep-alive');
$headers->add(Connection => 'close');
$headers->add(Connection => 'keep-alive');
is $headers->header('Connection'), 'close, keep-alive', 'right value';
$headers->remove('Connection');
is $headers->header('Connection'), undef, 'no value';
Expand Down Expand Up @@ -96,8 +96,8 @@ is $headers->www_authenticate('foo')->www_authenticate, 'foo', 'right value';

# Clone
$headers = Mojo::Headers->new;
$headers->add('Connection', 'close');
$headers->add('Connection', 'keep-alive');
$headers->add(Connection => 'close');
$headers->add(Connection => 'keep-alive');
is $headers->header('Connection'), 'close, keep-alive', 'right value';
my $clone = $headers->clone;
$headers->connection('nothing');
Expand Down Expand Up @@ -142,6 +142,28 @@ is $headers->expect, '100-continue', 'right value';
is $headers->cache_control, 'public', 'right value';
is $headers->expires, 'Thu, 01 Dec 1994 16:00:00 GMT', 'right value';

# Parse multiline headers
$headers = Mojo::Headers->new;
$headers->parse(<<'EOF');
Foo: first
second
third
Content-Type: text/plain
Foo: first again
second again
EOF
ok $headers->is_finished, 'parser is finished';
my $multi = [['first', 'second', 'third'], ['first again', 'second again']];
$hash = {'Content-Type' => 'text/plain', Foo => $multi};
is_deeply $headers->to_hash, $hash, 'right structure';
is_deeply [$headers->header('Foo')], $multi, 'right structure';
is scalar $headers->header('Foo'),
'first, second, third, first again, second again', 'right value';
$headers = Mojo::Headers->new->parse($headers->to_string . "\x0d\x0a\x0d\x0a");
ok $headers->is_finished, 'parser is finished';
is_deeply $headers->to_hash, $hash, 'successful roundtrip';

# Set headers from hash
$headers = Mojo::Headers->new;
$headers->from_hash({Connection => 'close', 'Content-Type' => 'text/html'});
Expand Down

0 comments on commit f43d247

Please sign in to comment.