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.

Attributes

options [RW]

Public Class methods

initialize_deflater_body(body, encoding)

returns the body wrapped with the correct deflater accordinng to the given encodisng.

[show source]
    # File lib/httpx/request/body.rb
127 def initialize_deflater_body(body, encoding)
128   case encoding
129   when "gzip"
130     Transcoder::GZIP.encode(body)
131   when "deflate"
132     Transcoder::Deflate.encode(body)
133   when "identity"
134     body
135   else
136     body
137   end
138 end
new(_, options, body: nil, **params)
[show source]
   # File lib/httpx/request/body.rb
 7 def new(_, options, body: nil, **params)
 8   if body.is_a?(self)
 9     # request derives its options from body
10     body.options = options.merge(params)
11     return body
12   end
13 
14   super
15 end
new(headers, options, body: nil, form: nil, json: nil, xml: nil, **params)

inits the instance with the request headers, options and params, which contain the payload definition. it wraps the given body with the appropriate encoder on initialization.

..., json: { foo: "bar" }) #=> json encoder
..., form: { foo: "bar" }) #=> form urlencoded encoder
..., form: { foo: Pathname.open("path/to/file") }) #=> multipart urlencoded encoder
..., form: { foo: File.open("path/to/file") }) #=> multipart urlencoded encoder
..., form: { body: "bla") }) #=> raw data encoder
[show source]
   # File lib/httpx/request/body.rb
28 def initialize(headers, options, body: nil, form: nil, json: nil, xml: nil, **params)
29   @headers = headers
30   @options = options.merge(params)
31 
32   @body = if body
33     Transcoder::Body.encode(body)
34   elsif form
35     Transcoder::Form.encode(form)
36   elsif json
37     Transcoder::JSON.encode(json)
38   elsif xml
39     Transcoder::Xml.encode(xml)
40   end
41 
42   if @body
43     if @options.compress_request_body && @headers.key?("content-encoding")
44 
45       @headers.get("content-encoding").each do |encoding|
46         @body = self.class.initialize_deflater_body(@body, encoding)
47       end
48     end
49 
50     @headers["content-type"] ||= @body.content_type
51     @headers["content-length"] = @body.bytesize unless unbounded_body?
52   end
53 
54   super(@body)
55 end

Public Instance methods

bytesize()

returns the +@body+ payload size in bytes.

[show source]
   # File lib/httpx/request/body.rb
88 def bytesize
89   return 0 if @body.nil?
90 
91   @body.bytesize
92 end
chunk!()

sets the chunked transfer encoding header.

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

returns whether the chunked transfer encoding header is set.

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

consumes and yields the request payload in chunks.

[show source]
   # File lib/httpx/request/body.rb
58 def each(&block)
59   return enum_for(__method__) unless block
60   return if @body.nil?
61 
62   body = stream(@body)
63   if body.respond_to?(:read)
64     ::IO.copy_stream(body, ProcIO.new(block))
65   elsif body.respond_to?(:each)
66     body.each(&block)
67   else
68     block[body.to_s]
69   end
70 end
empty?()

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

[show source]
   # File lib/httpx/request/body.rb
80 def empty?
81   return true if @body.nil?
82   return false if chunked?
83 
84   @body.bytesize.zero?
85 end
inspect()

:nocov:

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

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

[show source]
   # File lib/httpx/request/body.rb
73 def rewind
74   return if empty?
75 
76   @body.rewind if @body.respond_to?(:rewind)
77 end
stream(body)

sets the body to yield using chunked trannsfer encoding format.

[show source]
   # File lib/httpx/request/body.rb
95 def stream(body)
96   return body unless chunked?
97 
98   Transcoder::Chunker.encode(body.enum_for(:each))
99 end
unbounded_body?()

returns whether the body yields infinitely.

[show source]
    # File lib/httpx/request/body.rb
102 def unbounded_body?
103   return @unbounded_body if defined?(@unbounded_body)
104 
105   @unbounded_body = !@body.nil? && (chunked? || @body.bytesize == Float::INFINITY)
106 end