Parent

Files

HTTP::Message

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.

How to use HTTP response message

  1. Gets response message body.

     res = clnt.get(url)
     p res.content #=> String
    
  2. Gets response status code.

     res = clnt.get(url)
     p res.status #=> 200, 501, etc. (Integer)
    
  3. 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

Constants

CRLF

Attributes

header[RW]
HTTP::Message::Headers

message header.

body[R]
HTTP::Message::Body

message body.

peer_cert[RW]
OpenSSL::X509::Certificate

response only. server certificate which is used for retrieving the response.

Public Class Methods

file?(obj) click to toggle source

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
745:       def file?(obj)
746:         obj.respond_to?(:read) and obj.respond_to?(:path) and
747:           obj.respond_to?(:pos) and obj.respond_to?(:pos=)
748:       end
internal_mime_type(path) click to toggle source

Default MIME type handler. See mime_type_handler=.

     # File lib/httpclient/http.rb, line 707
707:       def internal_mime_type(path)
708:         case path
709:         when /\.txt$/
710:           'text/plain'
711:         when /\.(htm|html)$/
712:           'text/html'
713:         when /\.doc$/
714:           'application/msword'
715:         when /\.png$/
716:           'image/png'
717:         when /\.gif$/
718:           'image/gif'
719:         when /\.(jpg|jpeg)$/
720:           'image/jpeg'
721:         else
722:           'application/octet-stream'
723:         end
724:       end
keep_alive_enabled?(version) click to toggle source

Returns true if the given HTTP version allows keep alive connection.

version

Float

     # File lib/httpclient/http.rb, line 728
728:       def keep_alive_enabled?(version)
729:         version >= 1.1
730:       end
mime_type_handler() click to toggle source

Returns MIME type handler.

     # File lib/httpclient/http.rb, line 684
684:       def mime_type_handler
685:         @@mime_type_handler
686:       end
mime_type_handler=(handler) click to toggle source

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
679:       def mime_type_handler=(handler)
680:         @@mime_type_handler = handler
681:       end
multiparam_query?(query) click to toggle source

Returns true if the given query (or body) has a multiple parameter.

     # File lib/httpclient/http.rb, line 733
733:       def multiparam_query?(query)
734:         query.is_a?(Array) or query.is_a?(Hash)
735:       end
new_connect_request(uri) click to toggle source

Creates a Message instance of ‘CONNECT’ request. ‘CONNECT’ request does not have Body.

uri

an URI that need to connect. Only uri.host and uri.port are used.

     # File lib/httpclient/http.rb, line 623
623:       def new_connect_request(uri)
624:         m = new
625:         m.header.init_connect_request(uri)
626:         m.header.body_size = nil
627:         m
628:       end
new_request(method, uri, query = nil, body = nil, boundary = nil) click to toggle source

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

[“a”, “b”], [“a”, “c”]

> ‘host/part?a=b&a=c

body

a Hash or an Array of body part. e.g. { “a” => “b” } => ‘a=b’. Give an array to pass multiple value like

[“a”, “b”], [“a”, “c”]

> ‘a=b&a=c’.

boundary

When the boundary given, it is sent as a multipart/form-data using this boundary String.

     # File lib/httpclient/http.rb, line 643
643:       def new_request(method, uri, query = nil, body = nil, boundary = nil)
644:         m = new
645:         m.header.init_request(method, uri, query)
646:         m.body = Body.new
647:         m.body.init_request(body || '', boundary)
648:         if body
649:           m.header.body_size = m.body.size
650:           m.header.chunked = true if m.body.size.nil?
651:         else
652:           m.header.body_size = nil
653:         end
654:         m
655:       end
new_response(body) click to toggle source

Creates a Message instance of response.

body

a String or an IO of response message body.

     # File lib/httpclient/http.rb, line 659
659:       def new_response(body)
660:         m = new
661:         m.header.init_response(Status::OK)
662:         m.body = Body.new
663:         m.body.init_response(body)
664:         m.header.body_size = m.body.size || 0
665:         m
666:       end

Public Instance Methods

body=(body) click to toggle source

Sets a new body. header.body_size is updated with new body.size.

     # File lib/httpclient/http.rb, line 811
811:     def body=(body)
812:       @body = body
813:       @header.body_size = @body.size if @header
814:     end
code() click to toggle source
Alias for: status
content() click to toggle source

Returns a content of message body. A String or an IO.

     # File lib/httpclient/http.rb, line 861
861:     def content
862:       @body.content
863:     end
contenttype() click to toggle source

Sets ‘Content-Type’ header value. Overrides if already exists.

     # File lib/httpclient/http.rb, line 851
851:     def contenttype
852:       @header.contenttype
853:     end
contenttype=(contenttype) click to toggle source

Returns ‘Content-Type’ header value.

     # File lib/httpclient/http.rb, line 856
856:     def contenttype=(contenttype)
857:       @header.contenttype = contenttype
858:     end
dump(dev = '') click to toggle source

Dumps message (header and body) to given dev. dev needs to respond to <<.

     # File lib/httpclient/http.rb, line 798
798:     def dump(dev = '')
799:       str = header.dump + CRLF
800:       if header.chunked
801:         dev = body.dump_chunked(str, dev)
802:       elsif body
803:         dev = body.dump(str, dev)
804:       else
805:         dev << str
806:       end
807:       dev
808:     end
reason() click to toggle source

Returns HTTP status reason phrase in response. String.

     # File lib/httpclient/http.rb, line 841
841:     def reason
842:       @header.reason_phrase
843:     end
reason=(reason) click to toggle source

Sets HTTP status reason phrase of response. String.

     # File lib/httpclient/http.rb, line 846
846:     def reason=(reason)
847:       @header.reason_phrase = reason
848:     end
status() click to toggle source

Returns HTTP status code in response. Integer.

     # File lib/httpclient/http.rb, line 827
827:     def status
828:       @header.status_code
829:     end
Also aliased as: code, status_code
status=(status) click to toggle source

Sets HTTP status code of response. Integer. Reason phrase is updated, too.

     # File lib/httpclient/http.rb, line 836
836:     def status=(status)
837:       @header.status_code = status
838:     end
status_code() click to toggle source
Alias for: status
version() click to toggle source

Returns HTTP version in a HTTP header. Float.

     # File lib/httpclient/http.rb, line 817
817:     def version
818:       @header.http_version
819:     end
version=(version) click to toggle source

Sets HTTP version in a HTTP header. Float.

     # File lib/httpclient/http.rb, line 822
822:     def version=(version)
823:       @header.http_version = version
824:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.