Skip to content

Commit

Permalink
made http request parsing strict again
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Oct 5, 2010
1 parent b06aafc commit f99c60d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
25 changes: 17 additions & 8 deletions lib/Mojo/Content.pm
Expand Up @@ -11,10 +11,10 @@ use Mojo::Headers;

use constant CHUNK_SIZE => $ENV{MOJO_CHUNK_SIZE} || 262144;

__PACKAGE__->attr([qw/auto_relax relaxed/] => 0);
__PACKAGE__->attr([qw/buffer chunked_buffer/] => sub { b() });
__PACKAGE__->attr(headers => sub { Mojo::Headers->new });
__PACKAGE__->attr(headers => sub { Mojo::Headers->new });
__PACKAGE__->attr('on_read');
__PACKAGE__->attr(relaxed => 0);

# DEPRECATED in Comet!
*read_cb = \&on_read;
Expand Down Expand Up @@ -169,11 +169,13 @@ sub parse {
# Still parsing headers
return $self if $self->{_state} eq 'headers';

# Relaxed parsing for broken server
my $headers = $self->headers;
$self->relaxed(1)
if !defined $headers->content_length
&& ($headers->connection || '') =~ /close/i;
# Relaxed parsing for broken web servers
if ($self->auto_relax) {
my $headers = $self->headers;
$self->relaxed(1)
if !defined $headers->content_length
&& ($headers->connection || '') =~ /close/i;
}

# Chunked
if ($self->is_chunked && ($self->{_state} || '') ne 'headers') {
Expand Down Expand Up @@ -434,6 +436,13 @@ in RFC 2616.
L<Mojo::Content> implements the following attributes.
=head2 C<auto_relax>
my $relax = $content->auto_relax;
$content = $content->auto_relax(1);
Try to detect broken web servers and turn on relaxed parsing automatically.
=head2 C<buffer>
my $buffer = $content->buffer;
Expand Down Expand Up @@ -474,7 +483,7 @@ Note that this attribute is EXPERIMENTAL and might change without warning!
my $relaxed = $content->relaxed;
$content = $content->relaxed(1);
Activate relaxed parsing for HTTP 0.9.
Activate relaxed parsing for HTTP 0.9 and broken web servers.
=head1 METHODS
Expand Down
4 changes: 3 additions & 1 deletion lib/Mojo/Message/Response.pm
Expand Up @@ -191,7 +191,9 @@ sub _parse_start_line {
$self->code($3);
$self->message($4);
$self->{_state} = 'content';
$self->content->relaxed(1) unless $self->at_least_version('1.1');
$self->at_least_version('1.1')
? $self->content->auto_relax(1)
: $self->content->relaxed(1);
}
else { $self->error('Bad response start line.') }
}
Expand Down
29 changes: 27 additions & 2 deletions t/mojo/message.t
Expand Up @@ -5,7 +5,7 @@ use warnings;

use utf8;

use Test::More tests => 844;
use Test::More tests => 858;

use File::Spec;
use File::Temp;
Expand Down Expand Up @@ -96,6 +96,31 @@ is $req->url, '/foo/bar/baz.html', 'right URL';
is $req->headers->content_type, 'text/plain', 'right "Content-Type" value';
is $req->headers->content_length, 0, 'right "Content-Length" value';

# Parse HTTP 1.0 start line and headers, no body (missing Content-Length)
$req = Mojo::Message::Request->new;
$req->parse("GET /foo/bar/baz.html HTTP/1.0\x0d\x0a");
$req->parse("Content-Type: text/plain\x0d\x0a\x0d\x0a");
ok $req->is_done, 'request is done';
is $req->method, 'GET', 'right method';
is $req->major_version, 1, 'right major version';
is $req->minor_version, 0, 'right minor version';
is $req->url, '/foo/bar/baz.html', 'right URL';
is $req->headers->content_type, 'text/plain', 'right "Content-Type" value';
is $req->headers->content_length, undef, 'no "Content-Length" value';

# Parse HTTP 1.0 start line and headers, no body (missing Content-Length)
$req = Mojo::Message::Request->new;
$req->parse("GET /foo/bar/baz.html HTTP/1.0\x0d\x0a");
$req->parse("Content-Type: text/plain\x0d\x0a");
$req->parse("Connection: Close\x0d\x0a\x0d\x0a");
ok $req->is_done, 'request is done';
is $req->method, 'GET', 'right method';
is $req->major_version, 1, 'right major version';
is $req->minor_version, 0, 'right minor version';
is $req->url, '/foo/bar/baz.html', 'right URL';
is $req->headers->content_type, 'text/plain', 'right "Content-Type" value';
is $req->headers->content_length, undef, 'no "Content-Length" value';

# Parse HTTP 1.0 start line and headers, no body (with line size limit)
$req = Mojo::Message::Request->new;
my $backup = $ENV{MOJO_MAX_LINE_SIZE} || '';
Expand Down Expand Up @@ -818,7 +843,7 @@ $res->parse("HTTP/1.1 413 Request Entity Too Large\x0d\x0a"
. "Date: Tue, 09 Feb 2010 16:34:51 GMT\x0d\x0a"
. "Server: Mojolicious (Perl)\x0d\x0a"
. "X-Powered-By: Mojolicious (Perl)\x0d\x0a\x0d\x0a");
ok !$res->is_done, 'response is done';
ok !$res->is_done, 'response is not done';
is $res->code, 413, 'right status';
is $res->message, 'Request Entity Too Large', 'right message';
is $res->major_version, 1, 'right major version';
Expand Down

0 comments on commit f99c60d

Please sign in to comment.