Skip to content
This repository
Browse code

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
Michael Koziarski authored January 11, 2008
2  actionpack/CHANGELOG
... ...
@@ -1,5 +1,7 @@
1 1
 *SVN*
2 2
 
  3
+* Provide nicer access to HTTP Headers.  Instead of request.env["HTTP_REFERRER"] you can now use request.headers["Referrer"]. [Koz]
  4
+
3 5
 * UrlWriter respects relative_url_root.  #10748 [Cheah Chu Yeow]
4 6
 
5 7
 * 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]
1  actionpack/lib/action_controller.rb
@@ -55,6 +55,7 @@
55 55
 require 'action_controller/components'
56 56
 require 'action_controller/record_identifier'
57 57
 require 'action_controller/request_forgery_protection'
  58
+require 'action_controller/headers'
58 59
 
59 60
 require 'action_view'
60 61
 ActionController::Base.template_class = ActionView::Base
31  actionpack/lib/action_controller/headers.rb
... ...
@@ -0,0 +1,31 @@
  1
+module ActionController
  2
+  module Http
  3
+    class Headers < ::Hash
  4
+      
  5
+      def initialize(constructor = {})
  6
+         if constructor.is_a?(Hash)
  7
+           super()
  8
+           update(constructor)
  9
+         else
  10
+           super(constructor)
  11
+         end
  12
+       end
  13
+      
  14
+      def [](header_name)
  15
+        if include?(header_name)
  16
+          super 
  17
+        else
  18
+          super(normalize_header(header_name))
  19
+        end
  20
+      end
  21
+      
  22
+      
  23
+      private
  24
+        # Takes an HTTP header name and returns it in the 
  25
+        # format 
  26
+        def normalize_header(header_name)
  27
+          "HTTP_#{header_name.upcase.gsub(/-/, '_')}"
  28
+        end
  29
+    end
  30
+  end
  31
+end
4  actionpack/lib/action_controller/request.rb
@@ -61,8 +61,10 @@ def head?
61 61
       request_method == :head
62 62
     end
63 63
 
  64
+    # Provides acccess to the request's HTTP headers, for example:
  65
+    #  request.headers["Content-Type"] # => "text/plain"
64 66
     def headers
65  
-      @env
  67
+      @headers ||= ActionController::Http::Headers.new(@env)
66 68
     end
67 69
 
68 70
     def content_length
14  actionpack/test/controller/header_test.rb
... ...
@@ -0,0 +1,14 @@
  1
+require 'abstract_unit'
  2
+
  3
+class HeaderTest < Test::Unit::TestCase
  4
+  def setup
  5
+    @headers = ActionController::Http::Headers.new("HTTP_CONTENT_TYPE"=>"text/plain")
  6
+  end
  7
+  
  8
+  def test_content_type_works
  9
+    assert_equal "text/plain", @headers["Content-Type"]
  10
+    assert_equal "text/plain", @headers["content-type"]
  11
+    assert_equal "text/plain", @headers["CONTENT_TYPE"]
  12
+    assert_equal "text/plain", @headers["HTTP_CONTENT_TYPE"]    
  13
+  end
  14
+end

0 notes on commit 104f31a

Please sign in to comment.
Something went wrong with that request. Please try again.