Skip to content

Commit

Permalink
fixed small formatting bug in Mojo::Headers
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Sep 29, 2011
1 parent 1247311 commit dd8f277
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 41 deletions.
5 changes: 5 additions & 0 deletions Changes
@@ -1,5 +1,10 @@
This file documents the revision history for Perl extension Mojolicious.

2.0 2011-09-30 00:00:00
- Code name "Leaf Fluttering In Wind", this is a major release.
- Improved Mojo::Headers parser performance slightly.
- Fixed small formatting bug in Mojo::Headers.

1.99 2011-09-29 00:00:00
- Deprecated direct hash access to the flash in
Mojolicious::Controller.
Expand Down
51 changes: 12 additions & 39 deletions lib/Mojo/Headers.pm
Expand Up @@ -153,14 +153,10 @@ sub header {
my $name = shift;

# Replace
if (@_) {
$self->remove($name);
return $self->add($name, @_);
}

return unless my $headers = $self->{headers}->{lc $name};
return $self->remove($name)->add($name, @_) if @_;

# String
return unless my $headers = $self->{headers}->{lc $name};
return join ', ', map { join ', ', @$_ } @$headers unless wantarray;

# Array
Expand All @@ -181,14 +177,8 @@ sub leftovers { shift->{buffer} }
sub location { scalar shift->header(Location => @_) }

sub names {
my $self = shift;

# Normal case
my @headers;
for my $name (keys %{$self->{headers}}) {
push @headers, $NORMALCASE_HEADERS{$name} || $name;
}

push @headers, $NORMALCASE_HEADERS{$_} || $_ for keys %{shift->{headers}};
return \@headers;
}

Expand All @@ -199,45 +189,33 @@ sub parse {
$self->{state} = 'headers';
$self->{buffer} = '' unless defined $self->{buffer};
$self->{buffer} .= $chunk if defined $chunk;
my $headers = $self->{cache} || [];
my $headers = $self->{cache} ||= [];
my $max = $self->max_line_size;
while (defined(my $line = get_line $self->{buffer})) {

# Check line size
# Check line size limit
if (length $line > $max) {

# Abort
$self->{state} = 'done';
$self->{limit} = 1;
return $self;
}

# 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" }

# Empty line
else {

# Store headers
for (my $i = 0; $i < @$headers; $i += 2) {
$self->add($headers->[$i], $headers->[$i + 1]);
}

# Done
$self->add(splice @$headers, 0, 2) while @$headers;
$self->{state} = 'done';
$self->{cache} = [];
return $self;
}
}
$self->{cache} = $headers;

# Check line size
# Check line size limit
if (length $self->{buffer} > $max) {

# Abort
$self->{state} = 'done';
$self->{limit} = 1;
}
Expand Down Expand Up @@ -306,20 +284,15 @@ sub to_hash {
sub to_string {
my $self = shift;

# Prepare headers
# Format multiline values
my @headers;
for my $name (@{$self->names}) {

# Multiline value
for my $values ($self->header($name)) {
my $value = join "\x0d\x0a ", @$values;
push @headers, "$name: $value";
}
push @headers, "$name: " . join("\x0d\x0a ", @$_)
for $self->header($name);
}

# Format headers
my $headers = join "\x0d\x0a", @headers;
return length $headers ? $headers : undef;
return join "\x0d\x0a", @headers;
}

sub trailer { scalar shift->header(Trailer => @_) }
Expand Down
6 changes: 4 additions & 2 deletions lib/Mojolicious.pm
Expand Up @@ -34,8 +34,8 @@ has sessions => sub { Mojolicious::Sessions->new };
has static => sub { Mojolicious::Static->new };
has types => sub { Mojolicious::Types->new };

our $CODENAME = 'Smiling Face With Sunglasses';
our $VERSION = '1.99';
our $CODENAME = 'Leaf Fluttering In Wind';
our $VERSION = '2.0';

# "These old doomsday devices are dangerously unstable.
# I'll rest easier not knowing where they are."
Expand Down Expand Up @@ -688,6 +688,8 @@ L<http://www.apache.org/licenses/LICENSE-2.0>.
Every major release of L<Mojolicious> has a code name, these are the ones
that have been used in the past.
2.0, C<Leaf Fluttering In Wind> (u1F343)
1.4, C<Smiling Face With Sunglasses> (u1F60E)
1.3, C<Tropical Drink> (u1F379)
Expand Down

0 comments on commit dd8f277

Please sign in to comment.