# Net::HTTPHeader (Module)

In: | net/http.rb

Header module.

Provides access to @header in the mixed-into class as a hash-like object, except with case-insensitive keys. Also provides methods for accessing commonly-used header values in a more convenient format.

## Methods

[] []= basic_auth canonical_each chunked? content_length content_range delete each each_header each_key each_value fetch key? proxy_basic_auth range range= range_length set_range to_hash

## External Aliases

size -> length

## Public Instance methods

[]( key )

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

[Source]

    # File net/http.rb, line 929
        def []( key )
          @header[key.downcase]
        end

[]=( key, val )

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

[Source]

    # File net/http.rb, line 934
        def []=( key, val )
          @header[key.downcase] = val
        end

fetch( key, *args ) {|+key+| ...}

Returns the header field corresponding to the case-insensitive key. Returns the default value `args`, or the result of the block, or nil, if there’s no header field named key. See Hash#fetch

[Source]

    # File net/http.rb, line 941
        def fetch( key, *args, &block )  # :yield: +key+

          @header.fetch(key.downcase, *args, &block)
        end

each_header( ) {|+key+, +value+| ...}

Iterates for each header names and values.

[Source]

    # File net/http.rb, line 946
        def each_header( &block ) # :yield: +key+, +value+

          @header.each(&block)
        end

each( )

Alias for each_header

each_key( ) {|+key+| ...}

Iterates for each header names.

[Source]

    # File net/http.rb, line 953
        def each_key( &block ) # :yield: +key+

          @header.each_key(&block)
        end

each_value( ) {|+value+| ...}

Iterates for each header values.

[Source]

    # File net/http.rb, line 958
        def each_value( &block ) # :yield: +value+

          @header.each_value(&block)
        end

delete( key )

Removes a header field.

[Source]

    # File net/http.rb, line 963
        def delete( key )
          @header.delete(key.downcase)
        end

key?( key )

true if `key` header exists.

[Source]

    # File net/http.rb, line 968
        def key?( key )
          @header.key?(key.downcase)
        end

to_hash()

Returns a Hash consist of header names and values.

[Source]

    # File net/http.rb, line 973
        def to_hash
          @header.dup
        end

canonical_each() {|canonical(k), v| ...}

As for each_header, except the keys are provided in canonical form, which is to say, capitalized.

[Source]

    # File net/http.rb, line 979
        def canonical_each
          @header.each do |k,v|
            yield canonical(k), v
          end
        end

range()

Returns a Range object which represents Range: header field, or `nil` if there is no such header.

[Source]

    # File net/http.rb, line 992
        def range
          s = @header['range'] or return nil
          s.split(/,/).map {|spec|
            m = /bytes\s*=\s*(\d+)?\s*-\s*(\d+)?/i.match(spec) or
                    raise HTTPHeaderSyntaxError, "wrong Range: #{spec}"
            d1 = m[1].to_i
            d2 = m[2].to_i
            if    m[1] and m[2] then  d1..d2
            elsif m[1]          then  d1..-1
            elsif          m[2] then -d2..-1
            else
              raise HTTPHeaderSyntaxError, 'range is not specified'
            end
          }
        end

range=( r, fin = nil )

Set Range: header from Range (arg r) or beginning index and length from it (arg i&len).

[Source]

    # File net/http.rb, line 1010
        def range=( r, fin = nil )
          r = (r ... r + fin) if fin

          case r
          when Numeric
            s = r > 0 ? "0-#{r - 1}" : "-#{-r}"
          when Range
            first = r.first
            last = r.last
            if r.exclude_end?
              last -= 1
            end

            if last == -1
              s = first > 0 ? "#{first}-" : "-#{-first}"
            else
              first >= 0 or raise HTTPHeaderSyntaxError, 'range.first is negative'
              last > 0  or raise HTTPHeaderSyntaxError, 'range.last is negative'
              first < last or raise HTTPHeaderSyntaxError, 'must be .first < .last'
              s = "#{first}-#{last}"
            end
          else
            raise TypeError, 'Range/Integer is required'
          end

          @header['range'] = "bytes=#{s}"
          r
        end

set_range( r, fin = nil )

Alias for range=

content_length()

Returns an Integer object which represents the Content-Length: header field or `nil` if that field is not provided.

[Source]

    # File net/http.rb, line 1043
        def content_length
          s = @header['content-length'] or return nil
          m = /\d+/.match(s) or
                  raise HTTPHeaderSyntaxError, 'wrong Content-Length format'
          m[0].to_i
        end

chunked?()

Returns "true" if the "transfer-encoding" header is present and set to "chunked". This is an HTTP/1.1 feature, allowing the the content to be sent in "chunks" without at the outset stating the entire content length.

[Source]

    # File net/http.rb, line 1054
        def chunked?
          s = @header['transfer-encoding']
          (s and /(?:\A|[^\-\w])chunked(?:[^\-\w]|\z)/i === s) ? true : false
        end

content_range()

Returns a Range object which represents Content-Range: header field. This indicates, for a partial entity body, where this fragment fits inside the full entity body, as range of byte offsets.

[Source]

    # File net/http.rb, line 1062
        def content_range
          s = @header['content-range'] or return nil
          m = %<bytes\s+(\d+)-(\d+)/(?:\d+|\*)>i.match(s) or
                  raise HTTPHeaderSyntaxError, 'wrong Content-Range format'
          m[1].to_i .. m[2].to_i + 1
        end

range_length()

The length of the range represented in Range: header.

[Source]

    # File net/http.rb, line 1070
        def range_length
          r = self.content_range
          r and (r.end - r.begin)
        end

basic_auth( account, password )

Set the Authorization: header for "Basic" authorization.

[Source]

    # File net/http.rb, line 1076
        def basic_auth( account, password )
          @header['authorization'] = basic_encode(account, password)
        end

proxy_basic_auth( account, password )

Set Proxy-Authorization: header for "Basic" authorization.

[Source]

    # File net/http.rb, line 1081
        def proxy_basic_auth( account, password )
          @header['proxy-authorization'] = basic_encode(account, password)
        end

[Validate]