Sha256: 06a1deb425622d2e493ddc941fa3b2952a14d26b2995c7f1e4002efd6a24c6f1

Contents?: true

Size: 1.68 KB

Versions: 15

Compression:

Stored size: 1.68 KB

Contents

# Patch based on https://github.com/rack/rack/pull/1881
# This should be removed when/if this functionality makes its way into a rack
# release (possible rack 3.1.0)
module Rack
  class Request
    def headers
      @headers ||= Headers.new(@env)
    end

    class Headers
      def initialize(env)
        @env = env
      end

      def [](k)
        @env[header_to_env_key(k)]
      end

      def []=(k, v)
        @env[header_to_env_key(k)] = v
      end

      def add(k, v)
        k = header_to_env_key(k)
        case existing = @env[k]
        when nil
          @env[k] = v
        when String
          @env[k] = [existing, v]
        when Array
          existing << v
        end
      end

      def delete(k)
        @env.delete(header_to_env_key(k))
      end

      def each
        return to_enum(:each) unless block_given?

        @env.each do |k, v|
          next unless k = env_to_header_key(k)
          yield k, v
        end
      end

      def fetch(k, &block)
        @env.fetch(header_to_env_key(k), &block)
      end

      def has_key?(k)
        @env.has_key?(header_to_env_key(k))
      end

      def to_h
        h = {}
        each{|k, v| h[k] = v}
        h
      end

      private

      def env_to_header_key(k)
        case k
        when /\AHTTP_/
          k = k[5..-1]
          k.downcase!
          k.tr!('_', '-')
          k
        when "CONTENT_LENGTH", "CONTENT_TYPE"
          k = k.downcase
          k.tr!('_', '-')
          k
        end
      end

      def header_to_env_key(k)
        k = k.upcase
        k.tr!('-', '_')
        unless k == "CONTENT_LENGTH" || k == "CONTENT_TYPE"
          k = "HTTP_#{k}"
        end
        k
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
inferno_core-0.6.2 lib/inferno/ext/rack.rb
inferno_core-0.6.1 lib/inferno/ext/rack.rb
inferno_core-0.6.0 lib/inferno/ext/rack.rb
inferno_core-0.5.4 lib/inferno/ext/rack.rb
inferno_core-0.5.3 lib/inferno/ext/rack.rb
inferno_core-0.5.2 lib/inferno/ext/rack.rb
inferno_core-0.5.1 lib/inferno/ext/rack.rb
inferno_core-0.5.0 lib/inferno/ext/rack.rb
inferno_core-0.4.44 lib/inferno/ext/rack.rb
inferno_core-0.4.43 lib/inferno/ext/rack.rb
inferno_core-0.4.42 lib/inferno/ext/rack.rb
inferno_core-0.4.41 lib/inferno/ext/rack.rb
inferno_core-0.4.40 lib/inferno/ext/rack.rb
inferno_core-0.4.39 lib/inferno/ext/rack.rb
inferno_core-0.4.38 lib/inferno/ext/rack.rb