Sha256: a1b69c9916390df490b479f9cb2d2883e153638686b77a09acb645dd9a65c7f3

Contents?: true

Size: 1.45 KB

Versions: 7

Compression:

Stored size: 1.45 KB

Contents

require "stringio"
require "zlib"

class Bundler::CompactIndexClient
  class Updater
    class MisMatchedChecksumError < Error; end

    def initialize(fetcher)
      @fetcher = fetcher
    end

    def update(local_path, remote_path, retrying = nil)
      headers = {}

      if local_path.file?
        headers["If-None-Match"] = etag_for(local_path)
        headers["Range"] = "bytes=#{local_path.size}-"
      else
        # Fastly ignores Range when Accept-Encoding: gzip is set
        headers["Accept-Encoding"] = "gzip"
      end

      response = @fetcher.call(remote_path, headers)
      return if response.is_a?(Net::HTTPNotModified)

      content = response.body
      if response["Content-Encoding"] == "gzip"
        content = Zlib::GzipReader.new(StringIO.new(content)).read
      end

      mode = response.is_a?(Net::HTTPPartialContent) ? "a" : "w"
      local_path.open(mode) {|f| f << content }

      return if etag_for(local_path) == response["ETag"]

      if retrying.nil?
        local_path.delete
        update(local_path, remote_path, :retrying)
      else
        raise MisMatchedChecksumError, "Checksum of /#{remote_path} " \
          "does not match the checksum provided by server! Something is wrong."
      end
    end

    def etag_for(path)
      sum = checksum_for_file(path)
      sum ? '"' << sum << '"' : nil
    end

    def checksum_for_file(path)
      return nil unless path.file?
      Digest::MD5.file(path).hexdigest
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
bundler-1.12.0 lib/bundler/vendor/compact_index_client/lib/compact_index_client/updater.rb
bundler-1.12.0.rc.4 lib/bundler/vendor/compact_index_client/lib/compact_index_client/updater.rb
bundler-1.12.0.rc.3 lib/bundler/vendor/compact_index_client/lib/compact_index_client/updater.rb
bundler-1.12.0.rc.2 lib/bundler/vendor/compact_index_client/lib/compact_index_client/updater.rb
bundler-1.12.0.rc lib/bundler/vendor/compact_index_client/lib/compact_index_client/updater.rb
bundler-1.12.0.pre.2 lib/bundler/vendor/compact_index_client/lib/compact_index_client/updater.rb
bundler-1.12.0.pre.1 lib/bundler/vendor/compact_index_client/lib/compact_index_client/updater.rb