Skip to content

Commit

Permalink
fixed small argument bug in Mojo::Headers->to_hash
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Apr 2, 2012
1 parent 6f16107 commit e54ff44
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 38 deletions.
3 changes: 2 additions & 1 deletion Changes
@@ -1,10 +1,11 @@
This file documents the revision history for Perl extension Mojolicious.

2.71 2012-04-02
2.71 2012-04-03
- Improved Hypnotoad error handling.
- Improved version command to detect proxy servers automatically.
- Improved documentation.
- Improved tests.
- Fixed small argument bug in Mojo::Headers->to_hash.

2.70 2012-03-30
- Improved speed of version command by switching to the MetaCPAN API.
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Base.pm
Expand Up @@ -207,7 +207,7 @@ hash or a hash reference with attribute values.
BaseSubClass->attr([qw/name1 name2 name3/] => 'foo');
BaseSubClass->attr([qw/name1 name2 name3/] => sub {...});
Create attributes. An arrayref can be used to create more than one attribute.
Create attributes. An array can be used to create more than one attribute.
Pass an optional second argument to set a default value, it should be a
constant or a sub reference. The sub reference will be excuted at accessor
read time if there's no set value.
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojo/ByteStream.pm
Expand Up @@ -37,7 +37,7 @@ sub import {
# Well, I think the veal died of loneliness."
sub new {
my $class = shift;
bless \(my $dummy = join '', @_), ref $class || $class;
return bless \(my $dummy = join '', @_), ref $class || $class;
}

sub clone {
Expand Down Expand Up @@ -80,7 +80,7 @@ sub size { length ${shift()} }

sub split {
my ($self, $pattern) = @_;
Mojo::Collection->new(map { $self->new($_) } split $pattern, $$self);
return Mojo::Collection->new(map { $self->new($_) } split $pattern, $$self);
}

sub to_string { ${shift()} }
Expand Down
14 changes: 7 additions & 7 deletions lib/Mojo/Collection.pm
Expand Up @@ -19,7 +19,7 @@ sub import {

sub new {
my $class = shift;
bless [@_], ref $class || $class;
return bless [@_], ref $class || $class;
}

sub each {
Expand All @@ -41,34 +41,34 @@ sub first {
# I can get by with one."
sub grep {
my ($self, $cb) = @_;
$self->new(grep { $_->$cb } @$self);
return $self->new(grep { $_->$cb } @$self);
}

sub join {
my ($self, $expression) = @_;
Mojo::ByteStream->new(join $expression, map({"$_"} @$self));
return Mojo::ByteStream->new(join $expression, map({"$_"} @$self));
}

sub map {
my ($self, $cb) = @_;
$self->new(map { $_->$cb } @$self);
return $self->new(map { $_->$cb } @$self);
}

sub reverse {
my $self = shift;
$self->new(reverse @$self);
return $self->new(reverse @$self);
}

sub shuffle {
my $self = shift;
$self->new(List::Util::shuffle @$self);
return $self->new(List::Util::shuffle @$self);
}

sub size { scalar @{$_[0]} }

sub slice {
my $self = shift;
$self->new(@$self[@_]);
return $self->new(@$self[@_]);
}

sub sort {
Expand Down
4 changes: 3 additions & 1 deletion lib/Mojo/Date.pm
Expand Up @@ -23,10 +23,12 @@ sub new { shift->SUPER::new->parse(@_) }
# at you?"
sub parse {
my ($self, $date) = @_;

# Invalid
return $self unless defined $date;

# epoch (784111777)
$self->epoch($date) and return $self if $date =~ /^\d+$/;
return $self->epoch($date) if $date =~ /^\d+$/;

# RFC 822/1123 (Sun, 06 Nov 1994 08:49:37 GMT)
my ($day, $month, $year, $h, $m, $s);
Expand Down
39 changes: 16 additions & 23 deletions lib/Mojo/Headers.pm
Expand Up @@ -33,12 +33,11 @@ sub add {

# Make sure we have a normal case entry for name
my $lcname = lc $name;
$NORMALCASE{$lcname} = $name unless exists $NORMALCASE{$lcname};
$name = $lcname;
$NORMALCASE{$lcname} //= $name;

# Add lines
push @{$self->{headers}->{$name}}, (ref $_ || '') eq 'ARRAY' ? $_ : [$_]
for @_;
push @{$self->{headers}->{$lcname}},
map { (ref $_ || '') eq 'ARRAY' ? $_ : [$_] } @_;

return $self;
}
Expand Down Expand Up @@ -90,9 +89,7 @@ sub is_limit_exceeded { shift->{limit} }
sub leftovers { delete shift->{buffer} }

sub names {
my @headers;
push @headers, $NORMALCASE{$_} || $_ for keys %{shift->{headers}};
return \@headers;
[map { $NORMALCASE{$_} || $_ } keys %{shift->{headers}}];
}

sub parse {
Expand All @@ -108,8 +105,7 @@ sub parse {

# Check line size limit
if (length $line > $max) {
$self->{state} = 'finished';
$self->{limit} = 1;
$self->{limit} = $self->{state} = 'finished';
return $self;
}

Expand All @@ -128,10 +124,8 @@ sub parse {
}

# Check line size limit
if (length $self->{buffer} > $max) {
$self->{state} = 'finished';
$self->{limit} = 1;
}
$self->{limit} = $self->{state} = 'finished'
if length $self->{buffer} > $max;

return $self;
}
Expand All @@ -145,22 +139,21 @@ sub remove {
}

sub to_hash {
my $self = shift;
my %params = @_;
my ($self, $multi) = @_;

# Build
my $hash = {};
for my $header (@{$self->names}) {
my @headers = $self->header($header);

# Nested arrayrefs
if ($params{arrayref}) { $hash->{$header} = [@headers] }
# Multi line
if ($multi) { $hash->{$header} = [@headers] }

# Flat arrayref
# Flat
else {

# Turn single value arrayrefs into strings
for my $h (@headers) { $h = $h->[0] if @$h == 1 }
# Turn single value arrays into strings
@$_ == 1 and $_ = $_->[0] for @headers;
$hash->{$header} = @headers > 1 ? [@headers] : $headers[0];
}
}
Expand Down Expand Up @@ -531,10 +524,10 @@ Shortcut for the C<Status> header.
=head2 C<to_hash>
my $hash = $headers->to_hash;
my $hash = $headers->to_hash(arrayref => 1);
my $hash = $headers->to_hash(1);
Format headers as a hash. Nested arrayrefs to represent multi line values are
optional.
Format headers as a hash, nested arrays to represent multi line values are
disabled by default.
=head2 C<to_string>
Expand Down
6 changes: 3 additions & 3 deletions t/mojo/headers.t
Expand Up @@ -25,7 +25,7 @@ my $hash = $headers->to_hash;
is $hash->{Connection}, 'close', 'right value';
is $hash->{Expect}, 'continue-100', 'right value';
is $hash->{'Content-Type'}, 'text/html', 'right value';
$hash = $headers->to_hash(arrayref => 1);
$hash = $headers->to_hash(1);
is_deeply $hash->{Connection}, [['close']], 'right structure';
is_deeply $hash->{Expect}, [['continue-100']], 'right structure';
is_deeply $hash->{'Content-Type'}, [['text/html']], 'right structure';
Expand Down Expand Up @@ -119,7 +119,7 @@ is $headers->to_string,
. "X-Test: 25\x0d\x0a 26", 'right format';
my @array = $headers->header('X-Test');
is_deeply \@array, [[23, 24], ['single line'], [25, 26]], 'right structure';
is_deeply $headers->to_hash(arrayref => 1),
is_deeply $headers->to_hash(1),
{'X-Test' => [[23, 24], ['single line'], [25, 26]]}, 'right structure';
is_deeply $headers->to_hash,
{'X-Test' => [[23, 24], 'single line', [25, 26]]}, 'right structure';
Expand Down Expand Up @@ -158,7 +158,7 @@ $hash = $headers->to_hash;
is_deeply $hash->{'X-Test'}, [[23, 24], 'single line', [25, 26]],
'right structure';
is_deeply $hash->{'X-Test2'}, 'foo', 'right structure';
$hash = $headers->to_hash(arrayref => 1);
$hash = $headers->to_hash(1);
is_deeply $hash->{'X-Test'}, [[23, 24], ['single line'], [25, 26]],
'right structure';
is_deeply $hash->{'X-Test2'}, [['foo']], 'right structure';
Expand Down

0 comments on commit e54ff44

Please sign in to comment.