Object
Represents a HTTP message. A message is for a request or a response.
Request message is generated from given parameters internally so users don't need to care about it. Response message is the instance that methods of HTTPClient returns so users need to know how to extract HTTP response data from Message.
Some attributes are only for a request or a response, not both.
Gets response message body.
res = clnt.get(url) p res.content #=> String
Gets response status code.
res = clnt.get(url) p res.status #=> 200, 501, etc. (Integer)
Gets response header.
res = clnt.get(url) res.header['set-cookie'].each do |value| p value end assert_equal(1, res.header['last-modified'].size) p res.header['last-modified'].first
Returns true if the given object is a File. In HTTPClient, a file is;
must respond to :read for retrieving String chunks.
must respond to :path and returns a path for Content-Disposition.
must respond to :pos and :pos= to rewind for reading. Rewinding is only needed for following HTTP redirect. Some IO impl defines :pos= but raises an Exception for pos= such as StringIO but there's no problem as far as using it for non-following methods (get/post/etc.)
# File lib/httpclient/http.rb, line 745 def file?(obj) obj.respond_to?(:read) and obj.respond_to?(:path) and obj.respond_to?(:pos) and obj.respond_to?(:pos=) end
Default MIME type handler. See mime_type_handler=.
# File lib/httpclient/http.rb, line 707 def internal_mime_type(path) case path when /\.txt$/ 'text/plain' when /\.(htm|html)$/ 'text/html' when /\.doc$/ 'application/msword' when /\.png$/ 'image/png' when /\.gif$/ 'image/gif' when /\.(jpg|jpeg)$/ 'image/jpeg' else 'application/octet-stream' end end
Returns true if the given HTTP version allows keep alive connection.
version |
Float |
# File lib/httpclient/http.rb, line 728 def keep_alive_enabled?(version) version >= 1.1 end
Returns MIME type handler.
# File lib/httpclient/http.rb, line 684 def mime_type_handler @@mime_type_handler end
Sets MIME type handler.
handler must respond to :call with a single argument :path and returns a MIME type String e.g. 'text/html'. When the handler returns nil or an empty String, 'application/octet-stream' is used.
When you set nil to the handler, internal_mime_type is used instead. The handler is nil by default.
# File lib/httpclient/http.rb, line 679 def mime_type_handler=(handler) @@mime_type_handler = handler end
Returns true if the given query (or body) has a multiple parameter.
# File lib/httpclient/http.rb, line 733 def multiparam_query?(query) query.is_a?(Array) or query.is_a?(Hash) end
Creates a Message instance of general request.
method |
HTTP method String. |
uri |
an URI object which represents an URL of web resource. |
query |
a Hash or an Array of query part of URL. e.g. { "a" => "b" } => 'host/part?a=b' Give an array to pass multiple value like
|
body |
a Hash or an Array of body part. e.g. { "a" => "b" } => 'a=b'. Give an array to pass multiple value like
|
boundary |
When the boundary given, it is sent as a multipart/form-data using this boundary String. |
# File lib/httpclient/http.rb, line 643 def new_request(method, uri, query = nil, body = nil, boundary = nil) m = new m.header.init_request(method, uri, query) m.body = Body.new m.body.init_request(body || '', boundary) if body m.header.body_size = m.body.size m.header.chunked = true if m.body.size.nil? else m.header.body_size = nil end m end
Creates a Message instance of response.
body |
a String or an IO of response message body. |
# File lib/httpclient/http.rb, line 659 def new_response(body) m = new m.header.init_response(Status::OK) m.body = Body.new m.body.init_response(body) m.header.body_size = m.body.size || 0 m end
For backward compatibility.
Sets a new body. header.body_size is updated with new body.size.
# File lib/httpclient/http.rb, line 811 def body=(body) @body = body @header.body_size = @body.size if @header end
Returns a content of message body. A String or an IO.
# File lib/httpclient/http.rb, line 861 def content @body.content end
Sets 'Content-Type' header value. Overrides if already exists.
# File lib/httpclient/http.rb, line 851 def contenttype @header.contenttype end
Returns 'Content-Type' header value.
# File lib/httpclient/http.rb, line 856 def contenttype=(contenttype) @header.contenttype = contenttype end
Dumps message (header and body) to given dev. dev needs to respond to <<.
# File lib/httpclient/http.rb, line 798 def dump(dev = '') str = header.dump + CRLF if header.chunked dev = body.dump_chunked(str, dev) elsif body dev = body.dump(str, dev) else dev << str end dev end
Returns HTTP status reason phrase in response. String.
# File lib/httpclient/http.rb, line 841 def reason @header.reason_phrase end
Sets HTTP status reason phrase of response. String.
# File lib/httpclient/http.rb, line 846 def reason=(reason) @header.reason_phrase = reason end
Returns HTTP status code in response. Integer.
# File lib/httpclient/http.rb, line 827 def status @header.status_code end
Sets HTTP status code of response. Integer. Reason phrase is updated, too.
# File lib/httpclient/http.rb, line 836 def status=(status) @header.status_code = status end
Generated with the Darkfish Rdoc Generator 2.