aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_dispatch/http/headers.rb
blob: 040b51e04091cc2db7202b4bfcf321dfc51e1455 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
module ActionDispatch
  module Http
    class Headers < ::Hash
      @@env_cache = Hash.new { |h,k| h[k] = "HTTP_#{k.upcase.gsub(/-/, '_')}" }

      def initialize(*args)

        if args.size == 1 && args[0].is_a?(Hash)
          super()
          update(args[0])
        else
          super
        end
      end

      def [](header_name)
        if include?(header_name)
          super
        else
          super(env_name(header_name))
        end
      end

      private
        # Converts a HTTP header name to an environment variable name.
        def env_name(header_name)
          @@env_cache[header_name]
        end
    end
  end
end