Object
Represents HTTP message header.
$KCODE to charset mapping definition.
Placeholder URI object for nil uri.
HTTP response status code to reason phrase mapping definition.
Creates a Message::Headers. Use init_request, init_response, or init_connect_request for acutual initialize.
# File lib/httpclient/http.rb, line 158 def initialize @http_version = '1.1' @body_size = nil @chunked = false @request_method = nil @request_uri = nil @request_query = nil @request_absolute_uri = nil @status_code = nil @reason_phrase = nil @body_type = nil @body_charset = nil @body_date = nil @body_encoding = nil @is_request = nil @header_item = [] @dumped = false end
Returns an Array of header values for the given key.
# File lib/httpclient/http.rb, line 317 def [](key) get(key).collect { |item| item[1] } end
Adds a header. See set.
# File lib/httpclient/http.rb, line 312 def []=(key, value) set(key, value) end
Adds a header. Addition order is preserved.
# File lib/httpclient/http.rb, line 272 def add(key, value) if value.is_a?(Array) value.each do |v| @header_item.push([key, v]) end else @header_item.push([key, value]) end end
Returns an Array of all headers.
# File lib/httpclient/http.rb, line 301 def all @header_item end
Sets byte size of message body. body_size == nil means that the body is_a? IO
# File lib/httpclient/http.rb, line 248 def body_size=(body_size) @body_size = body_size end
Returns 'Content-Type' header value.
# File lib/httpclient/http.rb, line 219 def content_type self['Content-Type'][0] end
Sets 'Content-Type' header value. Overrides if already exists.
# File lib/httpclient/http.rb, line 224 def content_type=(content_type) delete('Content-Type') self['Content-Type'] = content_type end
# File lib/httpclient/http.rb, line 340 def create_query_part() query_str = nil if @request_uri.query query_str = @request_uri.query end if @request_query if query_str query_str += "&#{Message.create_query_part_str(@request_query)}" else query_str = Message.create_query_part_str(@request_query) end end query_str end
# File lib/httpclient/http.rb, line 328 def create_query_uri() if @request_method == 'CONNECT' return "#{@request_uri.host}:#{@request_uri.port}" end path = @request_uri.path path = '/' if path.nil? or path.empty? if query_str = create_query_part() path += "?#{query_str}" end path end
Deletes headers of the given key.
# File lib/httpclient/http.rb, line 306 def delete(key) key = key.upcase @header_item.delete_if { |k, v| k.upcase == key } end
Dumps message header part and returns a dumped String.
# File lib/httpclient/http.rb, line 253 def dump set_header str = nil if @is_request str = request_line else str = response_status_line end str + @header_item.collect { |key, value| "#{ key }: #{ value }#{ CRLF }" }.join end
Returns an Array of headers for the given key. Each element is a pair of key and value. It returns an single element Array even if the only one header exists. If nil key given, it returns all headers.
# File lib/httpclient/http.rb, line 291 def get(key = nil) if key.nil? all else key = key.upcase @header_item.find_all { |k, v| k.upcase == key } end end
Initialize this instance as a CONNECT request.
# File lib/httpclient/http.rb, line 182 def init_connect_request(uri) @is_request = true @request_method = 'CONNECT' @request_uri = uri @request_query = nil @http_version = '1.0' end
Initialize this instance as a general request.
# File lib/httpclient/http.rb, line 193 def init_request(method, uri, query = nil) @is_request = true @request_method = method @request_uri = uri || NIL_URI @request_query = query @request_absolute_uri = false end
Initialize this instance as a response.
# File lib/httpclient/http.rb, line 202 def init_response(status_code, req = nil) @is_request = false self.status_code = status_code if req @request_method = req.request_method @request_uri = req.request_uri @request_query = req.request_query end end
Sets a header.
# File lib/httpclient/http.rb, line 283 def set(key, value) delete(key) add(key, value) end
# File lib/httpclient/http.rb, line 233 def set_body_encoding if type = self.content_type OpenURI::Meta.init(o = '') o.meta_add_field('content-type', type) @body_encoding = o.encoding end end
Set Date header
# File lib/httpclient/http.rb, line 267 def set_date_header set('Date', Time.now.httpdate) end
Generated with the Darkfish Rdoc Generator 2.