In Files

  • net/http/header.rb

Class/Module Index [+]

Quicksearch

Net::HTTPHeader

The HTTPHeader module defines methods for reading and writing HTTP headers.

It is used as a mixin by other classes, to provide hash-like access to HTTP header values. Unlike raw hash access, HTTPHeader provides access via case-insensitive keys. It also provides methods for accessing commonly-used HTTP header values in more convenient formats.

Public Instance Methods

[](key) click to toggle source

Returns the header field corresponding to the case-insensitive key. For example, a key of “Content-Type” might return “text/html”

 
               # File net/http/header.rb, line 33
def [](key)
  a = @header[key.downcase] or return nil
  a.join(', ')
end
            
[]=(key, val) click to toggle source

Sets the header field corresponding to the case-insensitive key.

 
               # File net/http/header.rb, line 39
def []=(key, val)
  unless val
    @header.delete key.downcase
    return val
  end
  set_field(key, val)
end
            
add_field(key, val) click to toggle source
Ruby 1.8.3

Adds a value to a named header field, instead of replacing its value. Second argument val must be a String. See also []=, [] and get_fields.

request.add_field 'X-My-Header', 'a'
p request['X-My-Header']              #=> "a"
p request.get_fields('X-My-Header')   #=> ["a"]
request.add_field 'X-My-Header', 'b'
p request['X-My-Header']              #=> "a, b"
p request.get_fields('X-My-Header')   #=> ["a", "b"]
request.add_field 'X-My-Header', 'c'
p request['X-My-Header']              #=> "a, b, c"
p request.get_fields('X-My-Header')   #=> ["a", "b", "c"]
 
               # File net/http/header.rb, line 62
def add_field(key, val)
  if @header.key?(key.downcase)
    append_field_value(@header[key.downcase], val)
  else
    set_field(key, val)
  end
end
            
initialize_http_header(initheader) click to toggle source
 
               # File net/http/header.rb, line 12
def initialize_http_header(initheader)
  @header = {}
  return unless initheader
  initheader.each do |key, value|
    warn "net/http: warning: duplicated HTTP header: #{key}" if key?(key) and $VERBOSE
    if value.nil?
      warn "net/http: warning: nil HTTP header: #{key}" if $VERBOSE
    else
      @header[key.downcase] = [value.strip]
    end
  end
end