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
85 def bytesize
86   @length
87 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
131 def copy_to(dest)
132   return unless @buffer
133 
134   rewind
135 
136   if dest.respond_to?(:path) && @buffer.respond_to?(:path)
137     FileUtils.mv(@buffer.path, dest.path)
138   else
139     ::IO.copy_stream(@buffer, dest)
140   end
141 end
each()

yields the payload in chunks.

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

whether the payload is empty.

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

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

[show source]
    # File lib/httpx/response/body.rb
106 def filename
107   return unless @headers.key?("content-disposition")
108 
109   Utils.get_filename(@headers["content-disposition"])
110 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
72 def read(*args)
73   return unless @buffer
74 
75   unless @reader
76     rewind
77     @reader = @buffer
78   end
79 
80   @reader.read(*args)
81 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
113 def to_s
114   return "".b unless @buffer
115 
116   @buffer.to_s
117 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   size = chunk.bytesize
63   @length += size
64   transition(:open)
65   @buffer.write(chunk)
66 
67   @response.emit(:chunk_received, chunk)
68   size
69 end