[Rubygems-developers] [patch] use open-uri in remote_installer.rb

George Marrows george.marrows at ntlworld.com
Mon Mar 22 23:01:50 EST 2004


Nice work with rubygems!

Here's a patch to use open-uri.rb in the remote installer. It simplifies 
the code quite a bit & allows rubygems to make use of open-uri's 
corrected no_proxy support.

Warning: I've only tested that setting a proxy *doesn't* work (I don't 
have access to a proxy), rather than tested that it does work. As it 
defers to open-uri, though, I'm hoping it should be OK. Apologies if not.

-- George

Index: lib/rubygems/remote_installer.rb
===================================================================
RCS file: /var/cvs/rubygems/rubygems/lib/rubygems/remote_installer.rb,v
retrieving revision 1.13
diff -u -r1.13 remote_installer.rb
--- lib/rubygems/remote_installer.rb    17 Mar 2004 02:20:40 -0000    1.13
+++ lib/rubygems/remote_installer.rb    22 Mar 2004 22:59:55 -0000
@@ -6,7 +6,9 @@
     ##
     # http_proxy:: [String] URL of http proxy.  Will override any 
environment
     #   variable setting
-    def initialize(http_proxy=nil)
+    def initialize(http_proxy=true)
+      require 'open-uri'
+
       @http_proxy=http_proxy
     end
 
@@ -15,6 +17,7 @@
     # package_name:: [String] Name of the Gem to install
     # version_requirement:: [default = "> 0.0.0"] Gem version 
requirement to install
     def install(package_name, version_requirement = "> 0.0.0", 
force=false, directory=Gem.dir)
+
       unless version_requirement.respond_to?(:version)
         version_requirement = Version::Requirement.new(version_requirement)
       end
@@ -60,12 +63,17 @@
     # Given a list of sources, return a hash of all the caches from 
those sources, where the key is the source and the value is the cache.
     def get_caches(sources)
       require 'yaml'
+
       caches = {}
       sources.each do |source|
-        response = fetch(source + "/yaml")
-        spec = YAML.load(response.body)
-        raise "Didn't get a valid YAML document" if not spec
-        caches[source] = spec
+    open(source + "/yaml",
+         :proxy => @http_proxy) do |yaml_source|
+
+      spec = YAML.load(yaml_source.read)
+
+      raise "Didn't get a valid YAML document" if not spec
+      caches[source] = spec
+    end
       end
       return caches
     end
@@ -110,39 +118,12 @@
     def download_gem(destination_file, source, spec)
       # TODO
       uri = source + "/gems/#{spec.full_name}.gem"
-      response = fetch(uri)
-      write_gem_to_file(response.body, destination_file)
-    end
-    ##
-    # Returns HTTP proxy info if specified
-    # (code adapted/borrowed from raa-install)
-    def get_proxy
-      name = 'http_proxy'
-      if proxy_uri = @http_proxy || ENV[name] || ENV[name.upcase]
-        proxy_uri = URI.parse(proxy_uri)
-        name = 'no_proxy'
-        if no_proxy = ENV[name] || ENV[name.upcase]
-          no_proxy.scan(/([^:,]*)(?::(\d+))?/) {|host, port|
-            if /(\A|\.)#{Regexp.quote host}\z/i =~ proxy_uri.host && 
(!port || 80 == port.to_i)
-              proxy_uri = nil
-              break
-            end               
-          }
-        end
-        proxy_uri
-      else
-        nil
-      end
-    end
 
-    ##
-    # Returns the class to use for downloading files via http.  This 
method exists sole so that it can be overridden in the derived class in 
a unit test to return a mock http class.  Note that we could override 
the fetch method in the derived class instead, but then we wouldn't be 
able to test it.
-    def http_class
-      require 'net/http'
-      if proxy_uri = get_proxy
-        Net::HTTP.Proxy(proxy_uri.host, proxy_uri.port)
-      else
-        Net::HTTP
+      open(uri,
+       :proxy => @http_proxy) do |gem_source|
+
+    write_gem_to_file(gem_source.read, destination_file)
+
       end
     end
 
@@ -152,23 +133,6 @@
       end
     end
 
-    def fetch( uri_str, limit = 10 )
-      require 'uri'
-      require 'net/http'
-
-      # You should choose better exception.
-      raise ArgumentError, 'http redirect too deep' if limit == 0
-
-      response = http_class().get_response(URI.parse(uri_str))
-      case response
-      when Net::HTTPSuccess     then response
-      when Net::HTTPRedirection then fetch(response['location'], limit - 1)
-      else
-        $stderr.puts "Error downloading #{uri_str}"
-        response.error!
-      end
-    end
-
     def new_installer(gem)
       return Installer.new(gem)
     end




More information about the Rubygems-developers mailing list