Implementation of the HTTP Request
body as a delegator which iterates (responds to each
) payload chunks.
Methods
Public Class
Public Instance
Attributes
options | [RW] |
Public Class methods
# File lib/httpx/request/body.rb 117 def initialize_body(params) 118 if (body = params.delete(:body)) 119 # @type var body: bodyIO 120 Transcoder::Body.encode(body) 121 elsif (form = params.delete(:form)) 122 # @type var form: Transcoder::urlencoded_input 123 Transcoder::Form.encode(form) 124 elsif (json = params.delete(:json)) 125 # @type var body: _ToJson 126 Transcoder::JSON.encode(json) 127 end 128 end
returns the body
wrapped with the correct deflater accordinng to the given encodisng
.
# File lib/httpx/request/body.rb 131 def initialize_deflater_body(body, encoding) 132 case encoding 133 when "gzip" 134 Transcoder::GZIP.encode(body) 135 when "deflate" 136 Transcoder::Deflate.encode(body) 137 when "identity" 138 body 139 else 140 body 141 end 142 end
# 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
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
# File lib/httpx/request/body.rb 28 def initialize(h, options, **params) 29 @headers = h 30 @body = self.class.initialize_body(params) 31 @options = options.merge(params) 32 33 if @body 34 if @options.compress_request_body && @headers.key?("content-encoding") 35 36 @headers.get("content-encoding").each do |encoding| 37 @body = self.class.initialize_deflater_body(@body, encoding) 38 end 39 end 40 41 @headers["content-type"] ||= @body.content_type 42 @headers["content-length"] = @body.bytesize unless unbounded_body? 43 end 44 45 super(@body) 46 end
Public Instance methods
returns the +@body+ payload size in bytes.
# File lib/httpx/request/body.rb 79 def bytesize 80 return 0 if @body.nil? 81 82 @body.bytesize 83 end
sets the chunked transfer encoding header.
# File lib/httpx/request/body.rb 105 def chunk! 106 @headers.add("transfer-encoding", "chunked") 107 end
returns whether the chunked transfer encoding header is set.
# File lib/httpx/request/body.rb 100 def chunked? 101 @headers["transfer-encoding"] == "chunked" 102 end
consumes and yields the request payload in chunks.
# File lib/httpx/request/body.rb 49 def each(&block) 50 return enum_for(__method__) unless block 51 return if @body.nil? 52 53 body = stream(@body) 54 if body.respond_to?(:read) 55 ::IO.copy_stream(body, ProcIO.new(block)) 56 elsif body.respond_to?(:each) 57 body.each(&block) 58 else 59 block[body.to_s] 60 end 61 end
return true
if the body
has been fully drained (or does nnot exist).
# File lib/httpx/request/body.rb 71 def empty? 72 return true if @body.nil? 73 return false if chunked? 74 75 @body.bytesize.zero? 76 end
:nocov:
# File lib/httpx/request/body.rb 110 def inspect 111 "#<HTTPX::Request::Body:#{object_id} " \ 112 "#{unbounded_body? ? "stream" : "@bytesize=#{bytesize}"}>" 113 end
if the +@body+ is rewindable, it rewinnds it.
# File lib/httpx/request/body.rb 64 def rewind 65 return if empty? 66 67 @body.rewind if @body.respond_to?(:rewind) 68 end
sets the body to yield using chunked trannsfer encoding format.
# File lib/httpx/request/body.rb 86 def stream(body) 87 return body unless chunked? 88 89 Transcoder::Chunker.encode(body.enum_for(:each)) 90 end
returns whether the body yields infinitely.
# File lib/httpx/request/body.rb 93 def unbounded_body? 94 return @unbounded_body if defined?(@unbounded_body) 95 96 @unbounded_body = !@body.nil? && (chunked? || @body.bytesize == Float::INFINITY) 97 end