Class: HTTPX::Request::Body

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/httpx/request/body.rb

Overview

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(headers, options) ⇒ Body

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



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/httpx/request/body.rb', line 15

def initialize(headers, options)
  @headers = headers

  # forego compression in the Range request case
  if @headers.key?("range")
    @headers.delete("accept-encoding")
  else
    @headers["accept-encoding"] ||= options.supported_compression_formats
  end

  initialize_body(options)

  return if @body.nil?

  @headers["content-type"] ||= @body.content_type
  @headers["content-length"] = @body.bytesize unless unbounded_body?
  super(@body)
end

Class Method Details

.initialize_deflater_body(body, encoding) ⇒ Object

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



131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/httpx/request/body.rb', line 131

def initialize_deflater_body(body, encoding)
  case encoding
  when "gzip"
    Transcoder::GZIP.encode(body)
  when "deflate"
    Transcoder::Deflate.encode(body)
  when "identity"
    body
  else
    body
  end
end

.new(_, options) ⇒ Object



7
8
9
10
11
# File 'lib/httpx/request/body.rb', line 7

def new(_, options)
  return options.body if options.body.is_a?(self)

  super
end

Instance Method Details

#bytesizeObject

returns the @body payload size in bytes.



65
66
67
68
69
# File 'lib/httpx/request/body.rb', line 65

def bytesize
  return 0 if @body.nil?

  @body.bytesize
end

#chunk!Object

sets the chunked transfer encoding header.



91
92
93
# File 'lib/httpx/request/body.rb', line 91

def chunk!
  @headers.add("transfer-encoding", "chunked")
end

#chunked?Boolean

returns whether the chunked transfer encoding header is set.

Returns:

  • (Boolean)


86
87
88
# File 'lib/httpx/request/body.rb', line 86

def chunked?
  @headers["transfer-encoding"] == "chunked"
end

#each(&block) ⇒ Object

consumes and yields the request payload in chunks.



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/httpx/request/body.rb', line 35

def each(&block)
  return enum_for(__method__) unless block
  return if @body.nil?

  body = stream(@body)
  if body.respond_to?(:read)
    ::IO.copy_stream(body, ProcIO.new(block))
  elsif body.respond_to?(:each)
    body.each(&block)
  else
    block[body.to_s]
  end
end

#empty?Boolean

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

Returns:

  • (Boolean)


57
58
59
60
61
62
# File 'lib/httpx/request/body.rb', line 57

def empty?
  return true if @body.nil?
  return false if chunked?

  @body.bytesize.zero?
end

#inspectObject

:nocov:



96
97
98
99
# File 'lib/httpx/request/body.rb', line 96

def inspect
  "#<HTTPX::Request::Body:#{object_id} " \
    "#{unbounded_body? ? "stream" : "@bytesize=#{bytesize}"}>"
end

#rewindObject

if the @body is rewindable, it rewinnds it.



50
51
52
53
54
# File 'lib/httpx/request/body.rb', line 50

def rewind
  return if empty?

  @body.rewind if @body.respond_to?(:rewind)
end

#stream(body) ⇒ Object

sets the body to yield using chunked trannsfer encoding format.



72
73
74
75
76
# File 'lib/httpx/request/body.rb', line 72

def stream(body)
  return body unless chunked?

  Transcoder::Chunker.encode(body.enum_for(:each))
end

#unbounded_body?Boolean

returns whether the body yields infinitely.

Returns:

  • (Boolean)


79
80
81
82
83
# File 'lib/httpx/request/body.rb', line 79

def unbounded_body?
  return @unbounded_body if defined?(@unbounded_body)

  @unbounded_body = !@body.nil? && (chunked? || @body.bytesize == Float::INFINITY)
end