From a413606f199875f1d4640cf2482ee9608aa0a05d Mon Sep 17 00:00:00 2001 From: Gustav Munkby Date: Fri, 25 Oct 2013 11:04:58 +0200 Subject: Handle cross-host redirects with plain Net::HTTP --- lib/bundler/fetcher.rb | 7 ++++- spec/install/gems/dependency_api_spec.rb | 34 +++++++++++++++++++++++++ spec/support/artifice/endpoint_host_redirect.rb | 15 +++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 spec/support/artifice/endpoint_host_redirect.rb diff --git a/lib/bundler/fetcher.rb b/lib/bundler/fetcher.rb index f5ad0347e7..951cc95200 100644 --- a/lib/bundler/fetcher.rb +++ b/lib/bundler/fetcher.rb @@ -203,7 +203,12 @@ module Bundler else req = Net::HTTP::Get.new uri.request_uri req.basic_auth(uri.user, uri.password) if uri.user && uri.password - response = @connection.request(req) + if uri.host == @connection.address && uri.port == @connection.port + connection = @connection + else + connection = Net::HTTP.new(uri.host, uri.port) + end + response = connection.request(req) end rescue OpenSSL::SSL::SSLError raise CertificateFailureError.new(@public_uri) diff --git a/spec/install/gems/dependency_api_spec.rb b/spec/install/gems/dependency_api_spec.rb index c88fdcc2d2..093b314e5f 100644 --- a/spec/install/gems/dependency_api_spec.rb +++ b/spec/install/gems/dependency_api_spec.rb @@ -162,6 +162,40 @@ describe "gemcutter's dependency API" do should_be_installed "rack 1.0.0" end + it "handles host redirects" do + gemfile <<-G + source "#{source_uri}" + gem "rack" + G + + bundle :install, :artifice => "endpoint_host_redirect" + should_be_installed "rack 1.0.0" + end + + it "handles host redirects without Net::HTTP::Persistent" do + gemfile <<-G + source "#{source_uri}" + gem "rack" + G + + FileUtils.mkdir_p lib_path + File.open(lib_path("disable_net_http_persistent.rb"), "w") do |h| + h.write <<-H + module Kernel + alias require_without_disabled_net_http require + def require(*args) + raise LoadError, 'simulated' if args.first == 'openssl' && !caller.grep(/vendored_persistent/).empty? + require_without_disabled_net_http(*args) + end + end + H + end + + bundle :install, :artifice => "endpoint_host_redirect", :requires => [lib_path("disable_net_http_persistent.rb")] + expect(out).to_not match(/Too many redirects/) + should_be_installed "rack 1.0.0" + end + it "timeouts when Bundler::Fetcher redirects too much" do gemfile <<-G source "#{source_uri}" diff --git a/spec/support/artifice/endpoint_host_redirect.rb b/spec/support/artifice/endpoint_host_redirect.rb new file mode 100644 index 0000000000..e44d63e2a6 --- /dev/null +++ b/spec/support/artifice/endpoint_host_redirect.rb @@ -0,0 +1,15 @@ +require File.expand_path("../endpoint", __FILE__) + +Artifice.deactivate + +class EndpointHostRedirect < Endpoint + get "/fetch/actual/gem/:id", :host_name => 'localgemserver.test' do + redirect "http://bundler.localgemserver.test#{request.path_info}" + end + + get "/api/v1/dependencies" do + status 404 + end +end + +Artifice.activate_with(EndpointHostRedirect) -- cgit v1.2.1