lib/httpx/response.rb in httpx-0.24.7 vs lib/httpx/response.rb in httpx-1.0.0

- old
+ new

@@ -122,203 +122,10 @@ return false if content_length.nil? content_length == "0" end end - - class Body - attr_reader :encoding - - def initialize(response, options) - @response = response - @headers = response.headers - @options = options - @threshold_size = options.body_threshold_size - @window_size = options.window_size - @encoding = response.content_type.charset || Encoding::BINARY - @length = 0 - @buffer = nil - @state = :idle - end - - def initialize_dup(other) - super - - @buffer = other.instance_variable_get(:@buffer).dup - end - - def closed? - @state == :closed - end - - def write(chunk) - return if @state == :closed - - size = chunk.bytesize - @length += size - transition - @buffer.write(chunk) - - @response.emit(:chunk_received, chunk) - size - end - - def read(*args) - return unless @buffer - - unless @reader - rewind - @reader = @buffer - end - - @reader.read(*args) - end - - def bytesize - @length - end - - def each - return enum_for(__method__) unless block_given? - - begin - if @buffer - rewind - while (chunk = @buffer.read(@window_size)) - yield(chunk.force_encoding(@encoding)) - end - end - ensure - close - end - end - - def filename - return unless @headers.key?("content-disposition") - - Utils.get_filename(@headers["content-disposition"]) - end - - def to_s - case @buffer - when StringIO - begin - @buffer.string.force_encoding(@encoding) - rescue ArgumentError - @buffer.string - end - when Tempfile - rewind - content = _with_same_buffer_pos { @buffer.read } - begin - content.force_encoding(@encoding) - rescue ArgumentError # ex: unknown encoding name - utf - content - end - else - "".b - end - end - alias_method :to_str, :to_s - - def empty? - @length.zero? - end - - def copy_to(dest) - return unless @buffer - - rewind - - if dest.respond_to?(:path) && @buffer.respond_to?(:path) - FileUtils.mv(@buffer.path, dest.path) - else - ::IO.copy_stream(@buffer, dest) - end - end - - # closes/cleans the buffer, resets everything - def close - if @buffer - @buffer.close - @buffer.unlink if @buffer.respond_to?(:unlink) - @buffer = nil - end - @length = 0 - @state = :closed - end - - def ==(other) - object_id == other.object_id || begin - if other.respond_to?(:read) - _with_same_buffer_pos { FileUtils.compare_stream(@buffer, other) } - else - to_s == other.to_s - end - end - end - - # :nocov: - def inspect - "#<HTTPX::Response::Body:#{object_id} " \ - "@state=#{@state} " \ - "@length=#{@length}>" - end - # :nocov: - - def rewind - return unless @buffer - - # in case there's some reading going on - @reader = nil - - @buffer.rewind - end - - private - - def transition - case @state - when :idle - if @length > @threshold_size - @state = :buffer - @buffer = Tempfile.new("httpx", encoding: Encoding::BINARY, mode: File::RDWR) - else - @state = :memory - @buffer = StringIO.new("".b) - end - when :memory - # @type ivar @buffer: StringIO | Tempfile - if @length > @threshold_size - aux = @buffer - @buffer = Tempfile.new("httpx", encoding: Encoding::BINARY, mode: File::RDWR) - aux.rewind - ::IO.copy_stream(aux, @buffer) - # (this looks like a bug from Ruby < 2.3 - @buffer.pos = aux.pos ################## - ######################################## - aux.close - @state = :buffer - end - end - - nil unless %i[memory buffer].include?(@state) - end - - def _with_same_buffer_pos - return yield unless @buffer && @buffer.respond_to?(:pos) - - # @type ivar @buffer: StringIO | Tempfile - current_pos = @buffer.pos - @buffer.rewind - begin - yield - ensure - @buffer.pos = current_pos - end - end - end end class ContentType MIME_TYPE_RE = %r{^([^/]+/[^;]+)(?:$|;)}.freeze CHARSET_RE = /;\s*charset=([^;]+)/i.freeze @@ -356,26 +163,14 @@ @error = error @options = Options.new(options) log_exception(@error) end - def status - warn ":#{__method__} is deprecated, use :error.message instead" - @error.message + def to_s + @error.full_message(highlight: false) end - if Exception.method_defined?(:full_message) - def to_s - @error.full_message(highlight: false) - end - else - def to_s - "#{@error.message} (#{@error.class})\n" \ - "#{@error.backtrace.join("\n") if @error.backtrace}" - end - end - def close @response.close if @response.respond_to?(:close) end def finished? @@ -386,6 +181,8 @@ raise @error end end end -require "httpx/pmatch_extensions" if RUBY_VERSION >= "3.0.0" +require_relative "response/body" +require_relative "response/buffer" +require_relative "pmatch_extensions" if RUBY_VERSION >= "3.0.0"