Sha256: b2e69d4ba49fae64fc389217105495d2ebb98da75f96adcd136e3237ae398aad

Contents?: true

Size: 1.65 KB

Versions: 18

Compression:

Stored size: 1.65 KB

Contents

require 'net/http'
require 'net/https'
require 'open-uri'
require 'uri'

module Vagrant
  module Downloaders
    # Downloads a file from an HTTP URL to a temporary file. This
    # downloader reports its progress to stdout while downloading.
    class HTTP < Base
      def self.match?(uri)
        # URI.parse barfs on '<drive letter>:\\files \on\ windows'
        extracted = URI.extract(uri).first
        extracted && extracted.include?(uri)
      end

      def download!(source_url, destination_file)
        proxy_uri = URI.parse(ENV["http_proxy"] || "")
        uri = URI.parse(source_url)
        http = Net::HTTP.new(uri.host, uri.port, proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password)

        if uri.scheme == "https"
          http.use_ssl = true
          http.verify_mode = OpenSSL::SSL::VERIFY_NONE
        end

        http.start do |h|
          h.request_get(uri.request_uri) do |response|
            total = response.content_length
            progress = 0
            segment_count = 0

            response.read_body do |segment|
              # Report the progress out
              progress += segment.length
              segment_count += 1

              # Progress reporting is limited to every 25 segments just so
              # we're not constantly updating
              if segment_count % 25 == 0
                env.ui.report_progress(progress, total)
                segment_count = 0
              end

              # Store the segment
              destination_file.write(segment)
            end
          end
        end
      rescue SocketError
        raise Errors::DownloaderHTTPSocketError.new
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 2 rubygems

Version Path
vagrantup-0.6.8 lib/vagrant/downloaders/http.rb
vagrantup-0.6.7 lib/vagrant/downloaders/http.rb
vagrantup-0.6.6 lib/vagrant/downloaders/http.rb
vagrantup-0.6.5 lib/vagrant/downloaders/http.rb
vagrantup-0.6.4 lib/vagrant/downloaders/http.rb
vagrantup-0.6.3 lib/vagrant/downloaders/http.rb
vagrantup-0.6.2 lib/vagrant/downloaders/http.rb
vagrantup-0.6.1 lib/vagrant/downloaders/http.rb
vagrantup-0.6.0 lib/vagrant/downloaders/http.rb
vagrant-0.6.8 lib/vagrant/downloaders/http.rb
vagrant-0.6.7 lib/vagrant/downloaders/http.rb
vagrant-0.6.6 lib/vagrant/downloaders/http.rb
vagrant-0.6.5 lib/vagrant/downloaders/http.rb
vagrant-0.6.4 lib/vagrant/downloaders/http.rb
vagrant-0.6.3 lib/vagrant/downloaders/http.rb
vagrant-0.6.2 lib/vagrant/downloaders/http.rb
vagrant-0.6.1 lib/vagrant/downloaders/http.rb
vagrant-0.6.0 lib/vagrant/downloaders/http.rb