HTTP Message

A class that describes 1 HTTP request / response message.

Methods
Classes and Modules
Class HTTP::Message::Body
Class HTTP::Message::Headers
Constants
CRLF = "\r\n"
Public Class methods
create_query_multipart_str(query, boundary)
# File lib/facets/more/http.rb, line 489
      def create_query_multipart_str(query, boundary)
        if multiparam_query?(query)
          query.collect { |attr, value|
            value ||= ''
            if value.is_a? File
              params = {
                'filename' => value.path,
                # Creation time is not available from File::Stat
                # 'creation-date' => value.ctime.rfc822,
                'modification-date' => value.mtime.rfc822,
                'read-date' => value.atime.rfc822,
              }
              param_str = params.to_a.collect { |k, v|
                "#{k}=\"#{v}\""
              }.join("; ")
              "--#{boundary}\n" +
                %{Content-Disposition: form-data; name="#{attr.to_s}"; #{param_str}\n} +
                "Content-Type: #{mime_type(value.path)}\n\n#{value.read}\n"
            else
              "--#{boundary}\n" +
                %{Content-Disposition: form-data; name="#{attr.to_s}"\n} +
                "\n#{value.to_s}\n"
            end
          }.join('') + "--#{boundary}--\n"
        else
          query.to_s
        end
      end
create_query_part_str(query)
# File lib/facets/more/http.rb, line 481
      def create_query_part_str(query)
        if multiparam_query?(query)
          escape_query(query)
        else
          query.to_s
        end
      end
escape(str)

from CGI.escape

# File lib/facets/more/http.rb, line 529
      def escape(str)
        str.gsub(/([^ a-zA-Z0-9_.-]+)/n) {
          '%' + $1.unpack('H2' * $1.size).join('%').upcase
        }.tr(' ', '+')
      end
escape_query(query)
# File lib/facets/more/http.rb, line 522
      def escape_query(query)
        query.collect { |attr, value|
          escape(attr.to_s) << '=' << escape(value.to_s)
        }.join('&')
      end
mime_type(path)
# File lib/facets/more/http.rb, line 535
      def mime_type(path)
        case path
        when /.(htm|html)$/
          'text/html'
        when /.doc$/
          'application/msword'
        else
          'text/plain'
        end
      end
multiparam_query?(query)
# File lib/facets/more/http.rb, line 518
      def multiparam_query?(query)
        query.is_a?(Array) or query.is_a?(Hash)
      end
new()
# File lib/facets/more/http.rb, line 385
    def initialize
      @body = @header = nil
    end
new_request(method, uri, query = nil, body = nil, proxy = nil, boundary = nil)
# File lib/facets/more/http.rb, line 394
    def self.new_request(method, uri, query = nil, body = nil, proxy = nil,
        boundary = nil)
      m = self.__new
      m.header = Headers.new
      m.header.init_request(method, uri, query, proxy)
      m.body = Body.new(body, nil, nil, nil, boundary)
      m
    end
new_response(body = '')
# File lib/facets/more/http.rb, line 403
    def self.new_response(body = '')
      m = self.__new
      m.header = Headers.new
      m.header.init_response(Status::OK)
      m.body = Body.new(body)
      m
    end
Public Instance methods
body()
# File lib/facets/more/http.rb, line 439
    def body
      @body
    end
body=(body)
# File lib/facets/more/http.rb, line 443
    def body=(body)
      @body = body
      sync_header
    end
content()
# File lib/facets/more/http.rb, line 435
    def content
      @body.content
    end
contenttype()
# File lib/facets/more/http.rb, line 472
    def contenttype
      @header.contenttype
    end
contenttype=(contenttype)
# File lib/facets/more/http.rb, line 476
    def contenttype=(contenttype)
      @header.contenttype = contenttype
    end
dump(dev = '')
# File lib/facets/more/http.rb, line 411
    def dump(dev = '')
      sync_header
      dev = header.dump(dev)
      dev << CRLF
      dev = body.dump(dev) if body
      dev
    end
header()
# File lib/facets/more/http.rb, line 426
    def header
      @header
    end
header=(header)
# File lib/facets/more/http.rb, line 430
    def header=(header)
      @header = header
      sync_body
    end
load(str)
# File lib/facets/more/http.rb, line 419
    def load(str)
      buf = str.dup
      unless self.header.load(buf)
        self.body.load(buf)
      end
    end
reason()
# File lib/facets/more/http.rb, line 464
    def reason
      @header.reason_phrase
    end
reason=(reason)
# File lib/facets/more/http.rb, line 468
    def reason=(reason)
      @header.reason_phrase = reason
    end
status()
# File lib/facets/more/http.rb, line 448
    def status
      @header.response_status_code
    end
status=(status)
# File lib/facets/more/http.rb, line 452
    def status=(status)
      @header.response_status_code = status
    end
version()
# File lib/facets/more/http.rb, line 456
    def version
      @header.http_version
    end
version=(version)
# File lib/facets/more/http.rb, line 460
    def version=(version)
      @header.http_version = version
    end
Private Instance methods
sync_body()
# File lib/facets/more/http.rb, line 558
    def sync_body
      if @header and @body
        @body.type = @header.body_type
        @body.charset = @header.body_charset
        @body.size = @header.body_size
        @body.date = @header.body_date
      end
    end
sync_header()
# File lib/facets/more/http.rb, line 549
    def sync_header
      if @header and @body
        @header.body_type = @body.type
        @header.body_charset = @body.charset
        @header.body_size = @body.size
        @header.body_date = @body.date
      end
    end