class HTTPX::Request::Body

  1. lib/httpx/request/body.rb
Superclass: SimpleDelegator

Implementation of the HTTP Request body as a delegator which iterates (responds to each) payload chunks.

Methods

Public Class

  1. new
  2. new

Public Instance

  1. bytesize
  2. chunk!
  3. chunked?
  4. each
  5. empty?
  6. inspect
  7. rewind
  8. stream
  9. unbounded_body?

Public Class methods

new(_, options)
[show source]
   # File lib/httpx/request/body.rb
 7 def new(_, options)
 8   return options.body if options.body.is_a?(self)
 9 
10   super
11 end
new(headers, options)

inits the instance with the request headers and options, which contain the payload definition.

[show source]
   # File lib/httpx/request/body.rb
15 def initialize(headers, options)
16   @headers = headers
17 
18   # forego compression in the Range request case
19   if @headers.key?("range")
20     @headers.delete("accept-encoding")
21   else
22     @headers["accept-encoding"] ||= options.supported_compression_formats
23   end
24 
25   initialize_body(options)
26 
27   return if @body.nil?
28 
29   @headers["content-type"] ||= @body.content_type
30   @headers["content-length"] = @body.bytesize unless unbounded_body?
31   super(@body)
32 end

Public Instance methods

bytesize()

returns the +@body+ payload size in bytes.

[show source]
   # File lib/httpx/request/body.rb
65 def bytesize
66   return 0 if @body.nil?
67 
68   @body.bytesize
69 end
chunk!()

sets the chunked transfer encoding header.

[show source]
   # File lib/httpx/request/body.rb
91 def chunk!
92   @headers.add("transfer-encoding", "chunked")
93 end
chunked?()

returns whether the chunked transfer encoding header is set.

[show source]
   # File lib/httpx/request/body.rb
86 def chunked?
87   @headers["transfer-encoding"] == "chunked"
88 end
each(&block)

consumes and yields the request payload in chunks.

[show source]
   # File lib/httpx/request/body.rb
35 def each(&block)
36   return enum_for(__method__) unless block
37   return if @body.nil?
38 
39   body = stream(@body)
40   if body.respond_to?(:read)
41     ::IO.copy_stream(body, ProcIO.new(block))
42   elsif body.respond_to?(:each)
43     body.each(&block)
44   else
45     block[body.to_s]
46   end
47 end
empty?()

return true if the body has been fully drained (or does nnot exist).

[show source]
   # File lib/httpx/request/body.rb
57 def empty?
58   return true if @body.nil?
59   return false if chunked?
60 
61   @body.bytesize.zero?
62 end
inspect()

:nocov:

[show source]
   # File lib/httpx/request/body.rb
96 def inspect
97   "#<HTTPX::Request::Body:#{object_id} " \
98     "#{unbounded_body? ? "stream" : "@bytesize=#{bytesize}"}>"
99 end
rewind()

if the +@body+ is rewindable, it rewinnds it.

[show source]
   # File lib/httpx/request/body.rb
50 def rewind
51   return if empty?
52 
53   @body.rewind if @body.respond_to?(:rewind)
54 end
stream(body)

sets the body to yield using chunked trannsfer encoding format.

[show source]
   # File lib/httpx/request/body.rb
72 def stream(body)
73   return body unless chunked?
74 
75   Transcoder::Chunker.encode(body.enum_for(:each))
76 end
unbounded_body?()

returns whether the body yields infinitely.

[show source]
   # File lib/httpx/request/body.rb
79 def unbounded_body?
80   return @unbounded_body if defined?(@unbounded_body)
81 
82   @unbounded_body = !@body.nil? && (chunked? || @body.bytesize == Float::INFINITY)
83 end