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.
Returns the header field corresponding to the case-insensitive key. For example, a key of "Content-Type" might return "text/html"
# File net/http.rb, line 929 def []( key ) @header[key.downcase] end
Sets the header field corresponding to the case-insensitive key.
# File net/http.rb, line 934 def []=( key, val ) @header[key.downcase] = val end
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
# File net/http.rb, line 941 def fetch( key, *args, &block ) # :yield: +key+ @header.fetch(key.downcase, *args, &block) end
Iterates for each header names and values.
# File net/http.rb, line 946 def each_header( &block ) # :yield: +key+, +value+ @header.each(&block) end
Iterates for each header names.
# File net/http.rb, line 953 def each_key( &block ) # :yield: +key+ @header.each_key(&block) end
Iterates for each header values.
# File net/http.rb, line 958 def each_value( &block ) # :yield: +value+ @header.each_value(&block) end
Removes a header field.
# File net/http.rb, line 963 def delete( key ) @header.delete(key.downcase) end
true if key header exists.
# File net/http.rb, line 968 def key?( key ) @header.key?(key.downcase) end
Returns a Hash consist of header names and values.
# File net/http.rb, line 973 def to_hash @header.dup end
As for each_header, except the keys are provided in canonical form, which is to say, capitalized.
# File net/http.rb, line 979 def canonical_each @header.each do |k,v| yield canonical(k), v end end
Returns a Range object which represents Range: header field, or nil if there is no such header.
# 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
Set Range: header from Range (arg r) or beginning index and length from it (arg i&len).
# 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
Returns an Integer object which represents the Content-Length: header field or nil if that field is not provided.
# 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
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.
# File net/http.rb, line 1054 def chunked? s = @header['transfer-encoding'] (s and /(?:\A|[^\-\w])chunked(?:[^\-\w]|\z)/i === s) ? true : false end
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.
# 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
The length of the range represented in Range: header.
# File net/http.rb, line 1070 def range_length r = self.content_range r and (r.end - r.begin) end
Set the Authorization: header for "Basic" authorization.
# File net/http.rb, line 1076 def basic_auth( account, password ) @header['authorization'] = basic_encode(account, password) end