From 14134dc8b9ffd6ab048ad1f99b4e6e94400b14ab Mon Sep 17 00:00:00 2001 From: Felix Date: Sun, 17 Jul 2011 17:35:23 -0500 Subject: [PATCH] Fix HTTP/0.9 requests in WEBrick Fixes HTTP/0.9 requests in WEBrick's httprequest. Calling meta_vars woul d raise an exception where the @header variable is not set (as would be the case for an HTTP/0.9 simple request), but meta_vars should be availa ble for any request. This is my patch from the thread at http://redmine. ruby-lang.org/issues/5022 . --- lib/webrick/httprequest.rb | 10 ++++++---- test/webrick/test_httprequest.rb | 12 ++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/webrick/httprequest.rb b/lib/webrick/httprequest.rb index 1e8789d2d6f46f..66cf267ed7feea 100644 --- a/lib/webrick/httprequest.rb +++ b/lib/webrick/httprequest.rb @@ -179,10 +179,12 @@ def [](header_name) # Iterates over the request headers def each - @header.each{|k, v| - value = @header[k] - yield(k, value.empty? ? nil : value.join(", ")) - } + unless @header.nil? then + @header.each{|k, v| + value = @header[k] + yield(k, value.empty? ? nil : value.join(", ")) + } + end end ## diff --git a/test/webrick/test_httprequest.rb b/test/webrick/test_httprequest.rb index d8467d4b4eacf5..7012f955afe013 100644 --- a/test/webrick/test_httprequest.rb +++ b/test/webrick/test_httprequest.rb @@ -3,6 +3,18 @@ require "test/unit" class TestWEBrickHTTPRequest < Test::Unit::TestCase + def test_simple_request + msg = <<-_end_of_message_ + GET / + _end_of_message_ + + req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP) + req.parse(StringIO.new(msg.gsub(/^ {6}/, ''))) + + # assertion fails if @header was not initialized and iteration is attempted on the nil reference + assert_nothing_raised('req.meta_vars should be available with HTTP/0.9 simple request') { req.meta_vars } + end + def test_parse_09 msg = <<-_end_of_message_ GET /