Represents HTTP message header.
Size of body. nil when size is unknown (e.g. chunked response).
Request/Response is chunked or not.
Response only. HTTP status reason phrase.
Request only. Requested via proxy or not.
Request only. Requested method.
Request only. Requested query.
Request only. Requested URI.
Response only. HTTP status
Creates a Message::Headers. Use #init_request, #init_response, or #init_connect_request for acutual initialize.
# File lib/httpclient/http.rb, line 159 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 318 def [](key) get(key).collect { |item| item[1] } end
Adds a header. See set.
# File lib/httpclient/http.rb, line 313 def []=(key, value) set(key, value) end
Adds a header. Addition order is preserved.
# File lib/httpclient/http.rb, line 273 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 302 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 249 def body_size=(body_size) @body_size = body_size end
Returns ‘Content-Type’ header value.
# File lib/httpclient/http.rb, line 220 def content_type self['Content-Type'][0] end
Sets ‘Content-Type’ header value. Overrides if already exists.
# File lib/httpclient/http.rb, line 225 def content_type=(content_type) delete('Content-Type') self['Content-Type'] = content_type end
# File lib/httpclient/http.rb, line 341 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 329 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 307 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 254 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 292 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 183 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 194 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 203 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 284 def set(key, value) delete(key) add(key, value) end
# File lib/httpclient/http.rb, line 234 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 268 def set_date_header set('Date', Time.now.httpdate) end
# File lib/httpclient/http.rb, line 322 def set_headers(headers) headers.each do |key, value| add(key, value) end set_body_encoding end
Sets status code and reason phrase.
# File lib/httpclient/http.rb, line 214 def status_code=(status_code) @status_code = status_code @reason_phrase = STATUS_CODE_MAP[@status_code] end