summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2013-09-02 10:20:59 -0400
committerJesse Luehrs <doy@tozt.net>2013-09-02 10:53:31 -0400
commit31a50c75661e9bb58026643cecc11e448544e80e (patch)
tree0546aff486e918655af01307f59a3eb41be85fbd
parent9b83105e5c42d3b9cb39e8f43c0e80975e2622dd (diff)
downloadweb-request-31a50c75661e9bb58026643cecc11e448544e80e.tar.gz
web-request-31a50c75661e9bb58026643cecc11e448544e80e.zip
make sure we buffer the entire input (backported from Plack a96a5b6)
otherwise, the app may read things before the Web::Request instance does, and if the app doesn't buffer the input, then that data would not be seen
-rw-r--r--lib/Web/Request.pm2
-rw-r--r--t/content.t20
2 files changed, 19 insertions, 3 deletions
diff --git a/lib/Web/Request.pm b/lib/Web/Request.pm
index a895ef2..597530e 100644
--- a/lib/Web/Request.pm
+++ b/lib/Web/Request.pm
@@ -478,6 +478,8 @@ sub content {
my $fh = $self->_input or return '';
my $cl = $self->content_length or return '';
+ $fh->seek(0, 0); # just in case middleware/apps read it without seeking back
+
$fh->read(my $content, $cl, 0);
$fh->seek(0, 0);
diff --git a/t/content.t b/t/content.t
index aa39a1e..eb636fd 100644
--- a/t/content.t
+++ b/t/content.t
@@ -6,9 +6,20 @@ use Plack::Test;
use Web::Request;
+my ($content_before, $content_after);
+
my $app = sub {
- my $req = Web::Request->new_from_env(shift);
- is $req->content, 'body';
+ my $env = shift;
+
+ my $req = Web::Request->new_from_env($env);
+ $content_before = $req->content;
+
+ # emulate other PSGI apps that reads from input, but not reset
+ $env->{'psgi.input'}->read(my($dummy), $env->{CONTENT_LENGTH}, 0);
+
+ $req = Web::Request->new_from_env($env);
+ $content_after = $req->content;
+
$req->new_response(status => 200)->finalize;
};
@@ -19,7 +30,10 @@ test_psgi $app, sub {
$req->content("body");
$req->content_type('text/plain');
$req->content_length(4);
- $cb->($req);
+ my $res = $cb->($req);
+ ok($res->is_success) || diag $res->content;
+ is $content_before, "body";
+ is $content_after, "body";
};
done_testing;