def response_read response, request, uri
content_length = response.content_length
if use_tempfile? content_length then
body_io = Tempfile.new 'mechanize-raw'
body_io.unlink
body_io.binmode if defined? body_io.binmode
else
body_io = StringIO.new
end
body_io.set_encoding Encoding::BINARY if body_io.respond_to? :set_encoding
total = 0
begin
response.read_body { |part|
total += part.length
if StringIO === body_io and use_tempfile? total then
new_io = Tempfile.new 'mechanize-raw'
new_io.unlink
new_io.binmode
new_io.write body_io.string
body_io = new_io
end
body_io.write(part)
log.debug("Read #{part.length} bytes (#{total} total)") if log
}
rescue Net::HTTP::Persistent::Error => e
body_io.rewind
raise Mechanize::ResponseReadError.new(e, response, body_io, uri,
@context)
end
body_io.flush
body_io.rewind
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