Skip to content

Commit

Permalink
fixed body event in Mojo::Content to work more reliably in CGI enviro…
Browse files Browse the repository at this point in the history
…nments
  • Loading branch information
kraih committed Nov 29, 2011
1 parent 7c20730 commit 4fafe3a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
2 changes: 2 additions & 0 deletions Changes
Expand Up @@ -4,6 +4,8 @@ This file documents the revision history for Perl extension Mojolicious.
- Added EXPERIMENTAL etag method to Mojo::Headers.
- Improved documentation.
- Fixed one-byte payload bug in Mojo::Transaction::WebSocket. (tinx)
- Fixed body event in Mojo::Content to work more reliably in CGI
environments.

2.34 2011-11-28 00:00:00
- Added "websocket.pl" to example scripts.
Expand Down
10 changes: 7 additions & 3 deletions lib/Mojo/Content.pm
Expand Up @@ -156,9 +156,8 @@ sub parse {

# Parse headers
$self->parse_until_body(@_);

# Still parsing headers
return $self if $self->{state} eq 'headers';
$self->_body;

# Relaxed parsing for wonky web servers
if ($self->auto_relax) {
Expand Down Expand Up @@ -297,6 +296,11 @@ sub write_chunk {
$self->{eof} = 1 if defined $chunk && $chunk eq '';
}

sub _body {
my $self = shift;
$self->emit('body') unless $self->{body}++;
}

sub _build_chunk {
my ($self, $chunk) = @_;

Expand Down Expand Up @@ -397,7 +401,7 @@ sub _parse_headers {
$self->{header_size} = $self->{raw_size} - length $leftovers;
$self->{pre_buffer} = $leftovers;
$self->{state} = 'body';
$self->emit('body');
$self->_body;
}
}

Expand Down
12 changes: 9 additions & 3 deletions t/mojo/request_cgi.t
@@ -1,14 +1,16 @@
#!/usr/bin/env perl
use Mojo::Base -strict;

use Test::More tests => 178;
use Test::More tests => 181;

# "Aren't we forgetting the true meaning of Christmas?
# You know, the birth of Santa."
use_ok 'Mojo::Message::Request';

# Parse Lighttpd like CGI environment variables and a body
my $req = Mojo::Message::Request->new;
my $req = Mojo::Message::Request->new;
my $body = 0;
$req->content->on(body => sub { $body++ });
$req->parse(
HTTP_CONTENT_LENGTH => 11,
HTTP_EXPECT => '100-continue',
Expand All @@ -19,7 +21,11 @@ $req->parse(
HTTP_HOST => 'localhost:8080',
SERVER_PROTOCOL => 'HTTP/1.0'
);
$req->parse('Hello World');
is $body, 1, 'body event has been emitted once';
$req->parse('Hello ');
is $body, 1, 'body event has been emitted once';
$req->parse('World');
is $body, 1, 'body event has been emitted once';
ok $req->is_finished, 'request is finished';
is $req->method, 'POST', 'right method';
is $req->headers->expect, '100-continue', 'right "Expect" value';
Expand Down

0 comments on commit 4fafe3a

Please sign in to comment.