Class HTTP::Message::Body
In: lib/httpclient/http.rb
Parent: Object

Represents HTTP message body.

Methods

Classes and Modules

Class HTTP::Message::Body::Parts

Constants

DEFAULT_CHUNK_SIZE = 1024 * 16   Default value for chunk_size

Attributes

chunk_size  [RW]  maxbytes of IO#read for streaming request. See DEFAULT_CHUNK_SIZE.
size  [R]  Size of body. nil when size is unknown (e.g. chunked response).

Public Class methods

Creates a Message::Body. Use init_request or init_response for acutual initialize.

[Source]

# File lib/httpclient/http.rb, line 399
      def initialize
        @body = nil
        @size = nil
        @positions = nil
        @chunk_size = nil
      end

Public Instance methods

Returns a message body itself.

[Source]

# File lib/httpclient/http.rb, line 481
      def content
        @body
      end

Dumps message body to given dev. dev needs to respond to <<.

Message header must be given as the first argument for performance reason. (header is dumped to dev, too) If no dev (the second argument) given, this method returns a dumped String.

[Source]

# File lib/httpclient/http.rb, line 431
      def dump(header = '', dev = '')
        if @body.is_a?(Parts)
          dev << header
          buf = ''
          @body.parts.each do |part|
            if Message.file?(part)
              reset_pos(part)
              while !part.read(@chunk_size, buf).nil?
                dev << buf
              end
            else
              dev << part
            end
          end
        elsif @body
          dev << header + @body
        else
          dev << header
        end
        dev
      end

Dumps message body with chunked encoding to given dev. dev needs to respond to <<.

Message header must be given as the first argument for performance reason. (header is dumped to dev, too) If no dev (the second argument) given, this method returns a dumped String.

[Source]

# File lib/httpclient/http.rb, line 460
      def dump_chunked(header = '', dev = '')
        dev << header
        if @body.is_a?(Parts)
          @body.parts.each do |part|
            if Message.file?(part)
              reset_pos(part)
              dump_chunks(part, dev)
            else
              dev << dump_chunk(part)
            end
          end
          dev << (dump_last_chunk + CRLF)
        elsif @body
          reset_pos(@body)
          dump_chunks(@body, dev)
          dev << (dump_last_chunk + CRLF)
        end
        dev
      end

Initialize this instance as a request.

[Source]

# File lib/httpclient/http.rb, line 407
      def init_request(body = nil, boundary = nil)
        @boundary = boundary
        @positions = {}
        set_content(body, boundary)
        @chunk_size = DEFAULT_CHUNK_SIZE
      end

Initialize this instance as a response.

[Source]

# File lib/httpclient/http.rb, line 415
      def init_response(body = nil)
        @body = body
        if @body.respond_to?(:size)
          @size = @body.size
        else
          @size = nil
        end
      end

[Validate]