5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/mechanize/progressbar/mechanize_2_0.rb', line 5
def response_read response, request
body_io = StringIO.new
body_io.set_encoding Encoding::BINARY if body_io.respond_to? :set_encoding
total = 0
mpbar = MechanizeProgressBar.new(self.context, request, response)
begin
response.read_body { |part|
total += part.length
body_io.write(part)
log.debug("Read #{part.length} bytes (#{total} total)") if log && !mpbar.suppress_logger? mpbar.inc(part.length) }
rescue Net::HTTP::Persistent::Error => e
body_io.rewind
raise Mechanize::ResponseReadError.new(e, response, body_io)
ensure mpbar.finish end
body_io.rewind
log.debug("Read #{total} bytes total") if log && !mpbar.suppress_logger?
raise Mechanize::ResponseCodeError, response if
Net::HTTPUnknownResponse === response
content_length = response.content_length
unless Net::HTTP::Head === request or Net::HTTPRedirection === response then
raise EOFError, "Content-Length (#{content_length}) does not match " \
"response body length (#{body_io.length})" if
content_length and content_length != body_io.length
end
body_io
end
|