From c74d7a1acf8f44ee1fdbe4de6ce05f07d04b09b0 Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Wed, 15 Jul 2020 08:38:44 -0700 Subject: [PATCH] Allow empty POST and PUT requests without content length RFC 7230 section 3.3.3 allows for this. Fixes #30 --- lib/webrick/httprequest.rb | 2 +- test/webrick/test_httprequest.rb | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/webrick/httprequest.rb b/lib/webrick/httprequest.rb index c7817970..294bd91a 100644 --- a/lib/webrick/httprequest.rb +++ b/lib/webrick/httprequest.rb @@ -522,7 +522,7 @@ def read_body(socket, block) if @remaining_size > 0 && @socket.eof? raise HTTPStatus::BadRequest, "invalid body size." end - elsif BODY_CONTAINABLE_METHODS.member?(@request_method) + elsif BODY_CONTAINABLE_METHODS.member?(@request_method) && !@socket.eof raise HTTPStatus::LengthRequired end return @body diff --git a/test/webrick/test_httprequest.rb b/test/webrick/test_httprequest.rb index a594f14f..759ccbda 100644 --- a/test/webrick/test_httprequest.rb +++ b/test/webrick/test_httprequest.rb @@ -425,6 +425,18 @@ def test_continue_not_sent assert_equal l, msg.size end + def test_empty_post + msg = <<-_end_of_message_ + POST /path?foo=x;foo=y;foo=z;bar=1 HTTP/1.1 + Host: test.ruby-lang.org:8080 + Content-Type: application/x-www-form-urlencoded + + _end_of_message_ + req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP) + req.parse(StringIO.new(msg.gsub(/^ {6}/, ""))) + req.body + end + def test_bad_messages param = "foo=1;foo=2;foo=3;bar=x" msg = <<-_end_of_message_