Sha256: 84076c2a4dce8b304fdb6e0b9758d4ab9663c9d3e79aecde07b9cacb07708b83

Contents?: true

Size: 1.4 KB

Versions: 18

Compression:

Stored size: 1.4 KB

Contents

require 'epitools/progressbar'

class Mechanize
  class Chain
    class ResponseReader
      include Mechanize::Handler

      def initialize(response)
        @response = response
      end

      def handle(ctx, params)
        params[:response] = @response
        body = StringIO.new
        total = 0

        if @response.respond_to? :content_type
          pbar = ProgressBar.new("  |_ #{@response.content_type}", @response.content_length)
        else
          pbar = nil
        end

        @response.read_body { |part|
          total += part.length
          body.write(part)

          pbar.set(total) if pbar
          Mechanize.log.debug("Read #{total} bytes") if Mechanize.log
        }

        pbar.finish if pbar

        body.rewind

        res_klass = Net::HTTPResponse::CODE_TO_OBJ[@response.code.to_s]
        raise ResponseCodeError.new(@response) unless res_klass

        # Net::HTTP ignores EOFError if Content-length is given, so we emulate it here.
        unless res_klass <= Net::HTTPRedirection
          raise EOFError if (!params[:request].is_a?(Net::HTTP::Head)) && @response.content_length() && @response.content_length() != total
        end

        @response.each_header { |k,v|
          Mechanize.log.debug("response-header: #{ k } => #{ v }")
        } if Mechanize.log

        params[:response_body] = body
        params[:res_klass] = res_klass
        super
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
epitools-0.4.10 lib/epitools/browser/mechanize_progressbar.rb
epitools-0.4.9 lib/epitools/browser/mechanize_progressbar.rb
epitools-0.4.8 lib/epitools/browser/mechanize_progressbar.rb
epitools-0.4.7 lib/epitools/browser/mechanize_progressbar.rb
epitools-0.4.6 lib/epitools/browser/mechanize_progressbar.rb
epitools-0.4.5 lib/epitools/browser/mechanize_progressbar.rb
epitools-0.4.4 lib/epitools/browser/mechanize_progressbar.rb
epitools-0.4.3 lib/epitools/browser/mechanize_progressbar.rb
epitools-0.4.2 lib/epitools/browser/mechanize_progressbar.rb
epitools-0.4.1 lib/epitools/browser/mechanize_progressbar.rb
epitools-0.4.0 lib/epitools/browser/mechanize_progressbar.rb
epitools-0.3.4 lib/epitools/browser/mechanize_progressbar.rb
epitools-0.3.3 lib/epitools/browser/mechanize_progressbar.rb
epitools-0.3.2 lib/epitools/browser/mechanize_progressbar.rb
epitools-0.3.1 lib/epitools/browser/mechanize_progressbar.rb
epitools-0.3.0 lib/epitools/browser/mechanize_progressbar.rb
epitools-0.2.1 lib/epitools/browser/mechanize_progressbar.rb
epitools-0.2.0 lib/epitools/browser/mechanize_progressbar.rb