class HTTPX::Response::Body

  1. lib/httpx/response/body.rb
Superclass: Object

Implementation of the HTTP Response body as a buffer which implements the IO writer protocol (for buffering the response payload), the IO reader protocol (for consuming the response payload), and can be iterated over (via each, which yields the payload in chunks).

Public Instance Aliases

to_str -> to_s

Attributes

encoding [R]

the payload encoding (i.e. “utf-8”, “ASCII-8BIT”)

encodings [R]

Array of encodings contained in the response “content-encoding” header.

Public Class methods

new(response, options)

initialized with the corresponding HTTPX::Response response and HTTPX::Options options.

[show source]
   # File lib/httpx/response/body.rb
15 def initialize(response, options)
16   @response = response
17   @headers = response.headers
18   @options = options
19   @window_size = options.window_size
20   @encoding = response.content_type.charset || Encoding::BINARY
21   @encodings = []
22   @length = 0
23   @buffer = nil
24   @reader = nil
25   @state = :idle
26   initialize_inflaters
27 end

Public Instance methods

==(other)
[show source]
    # File lib/httpx/response/body.rb
139 def ==(other)
140   object_id == other.object_id || begin
141     if other.respond_to?(:read)
142       _with_same_buffer_pos { FileUtils.compare_stream(@buffer, other) }
143     else
144       to_s == other.to_s
145     end
146   end
147 end
bytesize()

size of the decoded response payload. May differ from “content-length” header if response was encoded over-the-wire.

[show source]
   # File lib/httpx/response/body.rb
71 def bytesize
72   @length
73 end
close()

closes/cleans the buffer, resets everything

[show source]
    # File lib/httpx/response/body.rb
130 def close
131   if @buffer
132     @buffer.close
133     @buffer = nil
134   end
135   @length = 0
136   transition(:closed)
137 end
closed?()
[show source]
   # File lib/httpx/response/body.rb
35 def closed?
36   @state == :closed
37 end
copy_to(dest)

copies the payload to dest.

body.copy_to("path/to/file")
body.copy_to(Pathname.new("path/to/file"))
body.copy_to(File.new("path/to/file"))
[show source]
    # File lib/httpx/response/body.rb
117 def copy_to(dest)
118   return unless @buffer
119 
120   rewind
121 
122   if dest.respond_to?(:path) && @buffer.respond_to?(:path)
123     FileUtils.mv(@buffer.path, dest.path)
124   else
125     ::IO.copy_stream(@buffer, dest)
126   end
127 end
each()

yields the payload in chunks.

[show source]
   # File lib/httpx/response/body.rb
76 def each
77   return enum_for(__method__) unless block_given?
78 
79   begin
80     if @buffer
81       rewind
82       while (chunk = @buffer.read(@window_size))
83         yield(chunk.force_encoding(@encoding))
84       end
85     end
86   ensure
87     close
88   end
89 end
empty?()

whether the payload is empty.

[show source]
    # File lib/httpx/response/body.rb
108 def empty?
109   @length.zero?
110 end
filename()

returns the declared filename in the “contennt-disposition” header, when present.

[show source]
   # File lib/httpx/response/body.rb
92 def filename
93   return unless @headers.key?("content-disposition")
94 
95   Utils.get_filename(@headers["content-disposition"])
96 end
initialize_dup(other)
[show source]
   # File lib/httpx/response/body.rb
29 def initialize_dup(other)
30   super
31 
32   @buffer = other.instance_variable_get(:@buffer).dup
33 end
inspect()

:nocov:

[show source]
    # File lib/httpx/response/body.rb
150 def inspect
151   "#<HTTPX::Response::Body:#{object_id} " \
152     "@state=#{@state} " \
153     "@length=#{@length}>"
154 end
read(*args)

reads a chunk from the payload (implementation of the IO reader protocol).

[show source]
   # File lib/httpx/response/body.rb
58 def read(*args)
59   return unless @buffer
60 
61   unless @reader
62     rewind
63     @reader = @buffer
64   end
65 
66   @reader.read(*args)
67 end
rewind()

rewinds the response payload buffer.

[show source]
    # File lib/httpx/response/body.rb
158 def rewind
159   return unless @buffer
160 
161   # in case there's some reading going on
162   @reader = nil
163 
164   @buffer.rewind
165 end
to_s()

returns the full response payload as a string.

[show source]
    # File lib/httpx/response/body.rb
 99 def to_s
100   return "".b unless @buffer
101 
102   @buffer.to_s
103 end
write(chunk)

write the response payload chunk into the buffer. Inflates the chunk when required and supported.

[show source]
   # File lib/httpx/response/body.rb
41 def write(chunk)
42   return if @state == :closed
43 
44   return 0 if chunk.empty?
45 
46   chunk = decode_chunk(chunk)
47 
48   size = chunk.bytesize
49   @length += size
50   transition(:open)
51   @buffer.write(chunk)
52 
53   @response.emit(:chunk_received, chunk)
54   size
55 end