Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Provide a nicer way to access headers. request.headers["Content-Type"…

…] instead of request.headers["HTTP_CONTENT_TYPE"] [Koz]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8625 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 104f31af1dc412160b624da1b09c5456fa862f53 1 parent e6de958
@NZKoz NZKoz authored
View
2  actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Provide nicer access to HTTP Headers. Instead of request.env["HTTP_REFERRER"] you can now use request.headers["Referrer"]. [Koz]
+
* UrlWriter respects relative_url_root. #10748 [Cheah Chu Yeow]
* The asset_host block takes the controller request as an optional second argument. Example: use a single asset host for SSL requests. #10549 [Cheah Chu Yeow, Peter B, Tom Taylor]
View
1  actionpack/lib/action_controller.rb
@@ -55,6 +55,7 @@
require 'action_controller/components'
require 'action_controller/record_identifier'
require 'action_controller/request_forgery_protection'
+require 'action_controller/headers'
require 'action_view'
ActionController::Base.template_class = ActionView::Base
View
31 actionpack/lib/action_controller/headers.rb
@@ -0,0 +1,31 @@
+module ActionController
+ module Http
+ class Headers < ::Hash
+
+ def initialize(constructor = {})
+ if constructor.is_a?(Hash)
+ super()
+ update(constructor)
+ else
+ super(constructor)
+ end
+ end
+
+ def [](header_name)
+ if include?(header_name)
+ super
+ else
+ super(normalize_header(header_name))
+ end
+ end
+
+
+ private
+ # Takes an HTTP header name and returns it in the
+ # format
+ def normalize_header(header_name)
+ "HTTP_#{header_name.upcase.gsub(/-/, '_')}"
+ end
+ end
+ end
+end
View
4 actionpack/lib/action_controller/request.rb
@@ -61,8 +61,10 @@ def head?
request_method == :head
end
+ # Provides acccess to the request's HTTP headers, for example:
+ # request.headers["Content-Type"] # => "text/plain"
def headers
- @env
+ @headers ||= ActionController::Http::Headers.new(@env)
end
def content_length
View
14 actionpack/test/controller/header_test.rb
@@ -0,0 +1,14 @@
+require 'abstract_unit'
+
+class HeaderTest < Test::Unit::TestCase
+ def setup
+ @headers = ActionController::Http::Headers.new("HTTP_CONTENT_TYPE"=>"text/plain")
+ end
+
+ def test_content_type_works
+ assert_equal "text/plain", @headers["Content-Type"]
+ assert_equal "text/plain", @headers["content-type"]
+ assert_equal "text/plain", @headers["CONTENT_TYPE"]
+ assert_equal "text/plain", @headers["HTTP_CONTENT_TYPE"]
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.