From a709246d1739c44e04b00412e7a4ec09c1500fc3 Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Wed, 13 Mar 2013 16:25:28 +0100 Subject: `Http::Headers` respects dotted env vars, symbols, headers with numbers. --- actionpack/lib/action_dispatch/http/headers.rb | 17 ++++++---------- actionpack/test/dispatch/header_test.rb | 27 ++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/actionpack/lib/action_dispatch/http/headers.rb b/actionpack/lib/action_dispatch/http/headers.rb index 3f3f71b280..1574518a16 100644 --- a/actionpack/lib/action_dispatch/http/headers.rb +++ b/actionpack/lib/action_dispatch/http/headers.rb @@ -1,7 +1,7 @@ module ActionDispatch module Http class Headers - NON_PREFIX_VARIABLES = %w( + CGI_VARIABLES = %w( CONTENT_TYPE CONTENT_LENGTH HTTPS AUTH_TYPE GATEWAY_INTERFACE PATH_INFO PATH_TRANSLATED QUERY_STRING @@ -9,7 +9,7 @@ module ActionDispatch REQUEST_METHOD SCRIPT_NAME SERVER_NAME SERVER_PORT SERVER_PROTOCOL SERVER_SOFTWARE ) - HEADER_REGEXP = /\A[A-Za-z-]+\z/ + HTTP_HEADER = /\A[A-Za-z0-9-]+\z/ include Enumerable attr_reader :env @@ -52,16 +52,11 @@ module ActionDispatch private def env_name(key) - if key =~ HEADER_REGEXP - cgi_name(key) - else - key + key = key.to_s + if key =~ HTTP_HEADER + key = key.upcase.tr('-', '_') + key = "HTTP_" + key unless CGI_VARIABLES.include?(key) end - end - - def cgi_name(key) - key = key.upcase.gsub(/-/, '_') - key = "HTTP_#{key}" unless NON_PREFIX_VARIABLES.include?(key) key end end diff --git a/actionpack/test/dispatch/header_test.rb b/actionpack/test/dispatch/header_test.rb index 0bfc18b4ed..3bb3b3db23 100644 --- a/actionpack/test/dispatch/header_test.rb +++ b/actionpack/test/dispatch/header_test.rb @@ -38,6 +38,13 @@ class HeaderTest < ActiveSupport::TestCase assert_equal "127.0.0.1", @headers["HTTP_HOST"] end + test "headers can contain numbers" do + @headers["Content-MD5"] = "Q2hlY2sgSW50ZWdyaXR5IQ==" + + assert_equal "Q2hlY2sgSW50ZWdyaXR5IQ==", @headers["Content-MD5"] + assert_equal "Q2hlY2sgSW50ZWdyaXR5IQ==", @headers["HTTP_CONTENT_MD5"] + end + test "set new env variables" do @headers["HTTP_HOST"] = "127.0.0.1" @@ -97,4 +104,24 @@ class HeaderTest < ActiveSupport::TestCase assert_equal({"CONTENT_TYPE" => "text/plain", "HTTP_REFERER" => "/some/page"}, @headers.env) end + + test "env variables with . are not modified" do + headers = ActionDispatch::Http::Headers.new + headers.merge! "rack.input" => "", + "rack.request.cookie_hash" => "", + "action_dispatch.logger" => "" + + assert_equal(["action_dispatch.logger", + "rack.input", + "rack.request.cookie_hash"], headers.env.keys.sort) + end + + test "symbols are treated as strings" do + headers = ActionDispatch::Http::Headers.new(:SERVER_NAME => "example.com", + "HTTP_REFERER" => "/", + :Host => "test.com") + assert_equal "example.com", headers["SERVER_NAME"] + assert_equal "/", headers[:HTTP_REFERER] + assert_equal "test.com", headers["HTTP_HOST"] + end end -- cgit v1.2.3