module 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
# File lib/net/http/header.rb, line 37 def [](key) a = @header[key.downcase] or return nil a.join(', ') end
Returns the header field corresponding to the case-insensitive key. For example, a key of “Content-Type” might return “text/html”
# File lib/net/http/header.rb, line 43 def []=(key, val) unless val @header.delete key.downcase return val end set_field(key, val) end
Sets the header field corresponding to the case-insensitive key.
# File lib/net/http/header.rb, line 66 def add_field(key, val) if @header.key?(key.downcase) append_field_value(@header[key.downcase], val) else set_field(key, val) end end
- 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 lib/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 value = value.strip # raise error for invalid byte sequences if value.count("\r\n") > 0 raise ArgumentError, 'header field value cannot include CR/LF' end @header[key.downcase] = [value] end end end
Ruby Core © 1993–2017 Yukihiro Matsumoto
Licensed under the Ruby License.
Ruby Standard Library © contributors
Licensed under their own licenses.