Sha256: 14a7ece345c7b3087a7517472b45048328c39b83d93554c089a35239bd2d0a8c

Contents?: true

Size: 1.35 KB

Versions: 19

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true

module Jeanine
  class Headers
    CGI_VARIABLES = Set.new(%W[
        AUTH_TYPE
        CONTENT_LENGTH
        CONTENT_TYPE
        GATEWAY_INTERFACE
        HTTPS
        PATH_INFO
        PATH_TRANSLATED
        QUERY_STRING
        REMOTE_ADDR
        REMOTE_HOST
        REMOTE_IDENT
        REMOTE_USER
        REQUEST_METHOD
        SCRIPT_NAME
        SERVER_NAME
        SERVER_PORT
        SERVER_PROTOCOL
        SERVER_SOFTWARE
      ]).freeze

    HTTP_HEADER = /\A[A-Za-z0-9-]+\z/

    include Enumerable

    def initialize(request)
      @req = request
    end

    def [](key)
      @req.get_header env_name(key)
    end

    def key?(key)
      @req.has_header? env_name(key)
    end
    alias :include? :key?

    DEFAULT = Object.new

    def fetch(key, default = DEFAULT)
      @req.fetch_header(env_name(key)) do
        return default unless default == DEFAULT
        return yield if block_given?
        raise KeyError, key
      end
    end

    def each(&block)
      @req.each_header(&block)
    end

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

    private

    def env_name(key)
      key = key.to_s
      if HTTP_HEADER.match?(key)
        key = key.upcase.tr("-", "_")
        key = "HTTP_" + key unless CGI_VARIABLES.include?(key)
      end
      key
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
jeanine-0.8.1 lib/jeanine/headers.rb
jeanine-0.8 lib/jeanine/headers.rb
jeanine-0.7.8 lib/jeanine/headers.rb
jeanine-0.7.7 lib/jeanine/headers.rb
jeanine-0.7.6 lib/jeanine/headers.rb
jeanine-0.7.5 lib/jeanine/headers.rb
jeanine-0.7.4 lib/jeanine/headers.rb
jeanine-0.7.3.2 lib/jeanine/headers.rb
jeanine-0.7.3.1 lib/jeanine/headers.rb
jeanine-0.7.3 lib/jeanine/headers.rb
jeanine-0.7.2 lib/jeanine/headers.rb
jeanine-0.7.1 lib/jeanine/headers.rb
jeanine-0.7.0 lib/jeanine/headers.rb
jeanine-0.6.0 lib/jeanine/headers.rb
jeanine-0.5.0 lib/jeanine/headers.rb
jeanine-0.4.0 lib/jeanine/headers.rb
jeanine-0.3.0 lib/jeanine/headers.rb
jeanine-0.2.0 lib/jeanine/headers.rb
jeanine-0.1.0 lib/jeanine/headers.rb