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

buffer [R]
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
18 def initialize(response, options)
19   @response = response
20   @headers = response.headers
21   @options = options
22   @window_size = options.window_size
23   @encodings = []
24   @length = 0
25   @buffer = nil
26   @reader = nil
27   @state = :idle
28 
29   # initialize response encoding
30   @encoding = if (enc = response.content_type.charset)
31     begin
32       Encoding.find(enc)
33     rescue ArgumentError
34       Encoding::BINARY
35     end
36   else
37     Encoding::BINARY
38   end
39 
40   initialize_inflaters
41 end

Public Instance methods

==(other)
[show source]
    # File lib/httpx/response/body.rb
153 def ==(other)
154   super || case other
155            when Response::Body
156              @buffer == other.buffer
157            else
158              @buffer = other
159            end
160 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
83 def bytesize
84   @length
85 end
close()

closes/cleans the buffer, resets everything

[show source]
    # File lib/httpx/response/body.rb
144 def close
145   if @buffer
146     @buffer.close
147     @buffer = nil
148   end
149   @length = 0
150   transition(:closed)
151 end
closed?()
[show source]
   # File lib/httpx/response/body.rb
49 def closed?
50   @state == :closed
51 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
129 def copy_to(dest)
130   return unless @buffer
131 
132   rewind
133 
134   if dest.respond_to?(:path) && @buffer.respond_to?(:path)
135     FileUtils.mv(@buffer.path, dest.path)
136   else
137     IO.copy_stream(@buffer, dest)
138   end
139 ensure
140   close
141 end
each()

yields the payload in chunks.

[show source]
    # File lib/httpx/response/body.rb
 88 def each
 89   return enum_for(__method__) unless block_given?
 90 
 91   begin
 92     if @buffer
 93       rewind
 94       while (chunk = @buffer.read(@window_size))
 95         yield(chunk.force_encoding(@encoding))
 96       end
 97     end
 98   ensure
 99     close
100   end
101 end
empty?()

whether the payload is empty.

[show source]
    # File lib/httpx/response/body.rb
120 def empty?
121   @length.zero?
122 end
filename()

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

[show source]
    # File lib/httpx/response/body.rb
104 def filename
105   return unless @headers.key?("content-disposition")
106 
107   Utils.get_filename(@headers["content-disposition"])
108 end
initialize_dup(other)
[show source]
   # File lib/httpx/response/body.rb
43 def initialize_dup(other)
44   super
45 
46   @buffer = other.instance_variable_get(:@buffer).dup
47 end
inspect()

:nocov:

[show source]
    # File lib/httpx/response/body.rb
163 def inspect
164   "#<#{self.class}:#{object_id} " \
165     "@state=#{@state} " \
166     "@length=#{@length}>"
167 end
read(*args)

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

[show source]
   # File lib/httpx/response/body.rb
70 def read(*args)
71   return unless @buffer
72 
73   unless @reader
74     rewind
75     @reader = @buffer
76   end
77 
78   @reader.read(*args)
79 end
rewind()

rewinds the response payload buffer.

[show source]
    # File lib/httpx/response/body.rb
171 def rewind
172   return unless @buffer
173 
174   # in case there's some reading going on
175   @reader = nil
176 
177   @buffer.rewind
178 end
to_s()

returns the full response payload as a string.

[show source]
    # File lib/httpx/response/body.rb
111 def to_s
112   return "".b unless @buffer
113 
114   @buffer.to_s
115 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
55 def write(chunk)
56   return if @state == :closed
57 
58   return 0 if chunk.empty?
59 
60   chunk = decode_chunk(chunk)
61 
62   transition(:open)
63   @buffer.write(chunk)
64 
65   @response.emit(:chunk_received, chunk)
66   chunk.bytesize
67 end