From 58e8f44f117e6b6baa4c821e2988284d8937aa9e Mon Sep 17 00:00:00 2001 From: Tatsuhiko Miyagawa Date: Fri, 27 Nov 2020 15:12:28 -0800 Subject: [PATCH 1/2] add a failng test for #655 --- t/Plack-Request/body-unbuffered.t | 37 +++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 t/Plack-Request/body-unbuffered.t diff --git a/t/Plack-Request/body-unbuffered.t b/t/Plack-Request/body-unbuffered.t new file mode 100644 index 000000000..cda8d7405 --- /dev/null +++ b/t/Plack-Request/body-unbuffered.t @@ -0,0 +1,37 @@ +use strict; +use warnings; +use Test::More; +use Plack::Test; +use Plack::Request; +use Plack::Util; +use HTTP::Request::Common; + +my $app = sub { + my $env = shift; + + $env->{'psgix.input.buffered'} = 0; + + my $input = $env->{'psgi.input'}; + $env->{'psgi.input'} = Plack::Util::inline_object + read => sub { $input->read(@_) }; + + my $req = Plack::Request->new($env); + is $req->content, '{}'; + + $req->new_response(200)->finalize; +}; + +test_psgi $app, sub { + my $cb = shift; + + # empty Content-Type + my $req = POST "/"; + $req->content_type(""); + $req->content("{}"); + $req->content_length(2); + + my $res = $cb->($req); + ok $res->is_success or diag $res->as_string; +}; + +done_testing; From 0e3322b4bf3b8f786a7e0abac25e0464594934c2 Mon Sep 17 00:00:00 2001 From: Tatsuhiko Miyagawa Date: Fri, 27 Nov 2020 15:23:03 -0800 Subject: [PATCH 2/2] work around HTTP::Entity::Parser retrurning early if Content-Type is not set --- lib/Plack/Request.pm | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/Plack/Request.pm b/lib/Plack/Request.pm index e75bd6015..3382ddffd 100644 --- a/lib/Plack/Request.pm +++ b/lib/Plack/Request.pm @@ -251,13 +251,15 @@ sub _buffer_length_for { sub _parse_request_body { my $self = shift; - if ( !$self->env->{CONTENT_TYPE} ) { - $self->env->{'plack.request.body_parameters'} = []; - $self->env->{'plack.request.upload'} = Hash::MultiValue->new(); - return; - } - my ($params,$uploads) = $self->request_body_parser->parse($self->env); + my ($params,$uploads) = do { + # work around HTTP::Entity::Parser issue not replacing psgi.input when Content-Type is not set + local $self->env->{CONTENT_TYPE} = 'application/octet-stream' + unless $self->env->{CONTENT_TYPE}; + + $self->request_body_parser->parse($self->env); + }; + $self->env->{'plack.request.body_parameters'} = $params; my $upload_hash = Hash::MultiValue->new();