Skip to content

Commit

Permalink
Let $req->content not barf when there's no Content-Type (i.e. GET/HEA…
Browse files Browse the repository at this point in the history
…D requests)
  • Loading branch information
miyagawa committed Jan 27, 2010
1 parent 83c6810 commit 1983fd4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/Plack/Request.pm
Expand Up @@ -88,7 +88,7 @@ sub content {
$self->_parse_request_body;
}

my $fh = $self->env->{'plack.request.tempfh'};
my $fh = $self->env->{'plack.request.tempfh'} or return ''; # no tempfh = no content body
$fh->read(my($content), $self->content_length);
$fh->seek(0, 0);

Expand Down Expand Up @@ -255,8 +255,16 @@ sub new_response {
sub _parse_request_body {
my $self = shift;

my $content_type = $self->env->{CONTENT_TYPE};
unless ($content_type) {
# This is GET/HEAD
$self->env->{'plack.request.body'} = {};
$self->env->{'plack.request.upload'} = {};
return;
}

# Do not use ->content_type to get multipart boundary correctly
my $body = HTTP::Body->new($self->env->{CONTENT_TYPE}, $self->env->{CONTENT_LENGTH});
my $body = HTTP::Body->new($content_type, $self->env->{CONTENT_LENGTH});
my $cl = $self->content_length;

my $input = $self->input;
Expand Down
23 changes: 23 additions & 0 deletions t/Plack-Request/content-on-get.t
@@ -0,0 +1,23 @@
use strict;
use warnings;
use Test::More;
use Plack::Test;
use Plack::Request;
use HTTP::Request::Common;

my $app = sub {
my $req = Plack::Request->new(shift);
is $req->content, '';
$req->new_response(200)->finalize;
};

test_psgi $app, sub {
my $cb = shift;
my $res = $cb->(GET "/");
ok $res->is_success;

$res = $cb->(HEAD "/");
ok $res->is_success;
};

done_testing;

0 comments on commit 1983fd4

Please sign in to comment.