Permalink
Browse files

Pass last_modified down to request, remove block from open_uri_or_path

git-svn-id: http://rubygems.rubyforge.org/svn/trunk@1804 3d4018f9-ac1a-0410-99e9-8a154d859a19
  • Loading branch information...
1 parent afa5e76 commit 4a87136b42c30490bbb27f87ffd0ac97fd960304 drbrain committed Jun 24, 2008
Showing with 33 additions and 25 deletions.
  1. +20 −17 lib/rubygems/remote_fetcher.rb
  2. +13 −8 test/test_gem_remote_fetcher.rb
@@ -1,5 +1,6 @@
require 'net/http'
require 'stringio'
+require 'time'
require 'uri'
require 'rubygems'
@@ -133,7 +134,7 @@ def download(spec, source_uri, install_dir = Gem.dir)
# Downloads +uri+ and returns it as a String.
def fetch_path(uri, mtime = nil)
- open_uri_or_path(uri) do |input|
+ open_uri_or_path(uri, mtime) do |input|
data = input.read
data = Gem.gunzip data if uri.to_s =~ /gz$/
data
@@ -247,24 +248,22 @@ def connection_for(uri)
# Read the data from the (source based) URI, but if it is a file:// URI,
# read from the filesystem instead.
- def open_uri_or_path(uri, depth = 0, &block)
- if file_uri?(uri)
- open(get_file_uri_path(uri), &block)
- else
- uri = URI.parse uri unless URI::Generic === uri
+ def open_uri_or_path(uri, last_modified = nil, depth = 0, &block)
+ return open(get_file_uri_path(uri), &block) if file_uri? uri
- response = request uri
+ uri = URI.parse uri unless URI::Generic === uri
- case response
- when Net::HTTPOK then
- block.call(StringIO.new(response.body)) if block
- when Net::HTTPRedirection then
- raise FetchError.new('too many redirects', uri) if depth > 10
+ response = request uri, Net::HTTP::Get, last_modified
- open_uri_or_path(response['Location'], depth + 1, &block)
- else
- raise FetchError.new("bad response #{response.message} #{response.code}", uri)
- end
+ case response
+ when Net::HTTPOK then
+ block.call(StringIO.new(response.body)) if block
+ when Net::HTTPRedirection then
+ raise FetchError.new('too many redirects', uri) if depth > 10
+
+ open_uri_or_path(response['Location'], last_modified, depth + 1, &block)
+ else
+ raise FetchError.new("bad response #{response.message} #{response.code}", uri)
end
end
@@ -273,7 +272,7 @@ def open_uri_or_path(uri, depth = 0, &block)
# a Net::HTTP response object. request maintains a table of persistent
# connections to reduce connect overhead.
- def request(uri, request_class = Net::HTTP::Get)
+ def request(uri, request_class, last_modified = nil)
request = request_class.new uri.request_uri
unless uri.nil? || uri.user.nil? || uri.user.empty? then
@@ -289,6 +288,10 @@ def request(uri, request_class = Net::HTTP::Get)
request.add_field 'Connection', 'keep-alive'
request.add_field 'Keep-Alive', '30'
+ if last_modified then
+ request.add_field 'If-Modified-Since', last_modified.rfc2822
+ end
+
connection = connection_for uri
retried = false
@@ -376,7 +376,7 @@ def test_explicit_proxy_with_user_auth_in_env
def test_fetch_path_io_error
fetcher = Gem::RemoteFetcher.new nil
- def fetcher.open_uri_or_path(uri) raise EOFError; end
+ def fetcher.open_uri_or_path(uri, mtime) raise EOFError; end
e = assert_raise Gem::RemoteFetcher::FetchError do
fetcher.fetch_path 'uri'
@@ -389,7 +389,7 @@ def fetcher.open_uri_or_path(uri) raise EOFError; end
def test_fetch_path_socket_error
fetcher = Gem::RemoteFetcher.new nil
- def fetcher.open_uri_or_path(uri) raise SocketError; end
+ def fetcher.open_uri_or_path(uri, mtime) raise SocketError; end
e = assert_raise Gem::RemoteFetcher::FetchError do
fetcher.fetch_path 'uri'
@@ -402,7 +402,7 @@ def fetcher.open_uri_or_path(uri) raise SocketError; end
def test_fetch_path_system_call_error
fetcher = Gem::RemoteFetcher.new nil
- def fetcher.open_uri_or_path(uri);
+ def fetcher.open_uri_or_path(uri, mtime = nil)
raise Errno::ECONNREFUSED, 'connect(2)'
end
@@ -417,6 +417,11 @@ def fetcher.open_uri_or_path(uri);
def test_fetch_path_unmodified
fetcher = Gem::RemoteFetcher.new nil
+
+ def fetcher.open_uri_or_path(uri, mtime)
+ yield StringIO.new('')
+ end
+
util_stub_connection_for o(:request => o(:body => '', :code => 304,
:date => Time.at(0).to_s))
@@ -493,9 +498,9 @@ def res.body() 'real_path' end
conn = { 'gems.example.com:80' => conn }
fetcher.instance_variable_set :@connections, conn
- fetcher.send :open_uri_or_path, 'http://gems.example.com/redirect' do |io|
- assert_equal 'real_path', io.read
- end
+ data = fetcher.open_uri_or_path 'http://gems.example.com/redirect'
+
+ assert_equal 'real_path', data
end
def test_open_uri_or_path_limited_redirects
@@ -513,7 +518,7 @@ def conn.request(req)
fetcher.instance_variable_set :@connections, conn
e = assert_raise Gem::RemoteFetcher::FetchError do
- fetcher.send :open_uri_or_path, 'http://gems.example.com/redirect'
+ fetcher.open_uri_or_path 'http://gems.example.com/redirect'
end
assert_equal 'too many redirects (http://gems.example.com/redirect)',
@@ -524,7 +529,7 @@ def test_request
uri = URI.parse "#{@gem_repo}/specs.#{Gem.marshal_version}"
util_stub_connection_for o(:request => o(:body => :junk, :code => 200))
- response = @fetcher.request uri
+ response = @fetcher.request uri, Net::HTTP::Get
assert_equal 200, response.code
assert_equal :junk, response.body

0 comments on commit 4a87136

Please sign in to comment.