Skip to content
This repository has been archived by the owner on Apr 14, 2021. It is now read-only.

[Settings] Allow configuring a mirror fallback timeout without a trailing slash #5608

Merged
merged 1 commit into from
Apr 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions lib/bundler/mirror.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,16 @@ def parse(key, value)
mirror = if config.all?
@all
else
(@mirrors[config.uri] = @mirrors[config.uri] || Mirror.new)
@mirrors[config.uri] ||= Mirror.new
end
config.update_mirror(mirror)
end

private

def fetch_valid_mirror_for(uri)
mirror = (@mirrors[URI(uri.to_s.downcase)] || @mirrors[URI(uri.to_s).host] || Mirror.new(uri)).validate!(@prober)
mirror = @mirrors[uri.to_s.downcase] || @mirrors[URI(uri.to_s).host] || Mirror.new(uri)
mirror.validate!(@prober)
mirror = Mirror.new(uri) unless mirror.valid?
mirror
end
Expand All @@ -71,7 +72,7 @@ def uri=(uri)
@uri = if uri.nil?
nil
else
URI(uri.to_s)
URI(uri.to_s.downcase)
end
@valid = nil
end
Expand Down Expand Up @@ -117,7 +118,7 @@ class MirrorConfig

def initialize(config_line, value)
uri, fallback =
config_line.match(%r{^mirror\.(all|.+?)(\.fallback_timeout)?\/?$}).captures
config_line.match(%r{\Amirror\.(all|.+?)(\.fallback_timeout)?\/?\z}).captures
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

out of curiosity what's the reason for this change?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

preference -- I don't like using ^$ since they're relative to a line, rather than the whole string

@fallback = !fallback.nil?
@all = false
if uri == "all"
Expand Down
24 changes: 21 additions & 3 deletions lib/bundler/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def credentials_for(uri)

def gem_mirrors
all.inject(Mirrors.new) do |mirrors, k|
mirrors.parse(k, self[k]) if k =~ /^mirror\./
mirrors.parse(k, self[k]) if k.start_with?("mirror.")
mirrors
end
end
Expand Down Expand Up @@ -321,16 +321,34 @@ def load_config(config_file)
end
end

PER_URI_OPTIONS = %w(
fallback_timeout
).freeze

NORMALIZE_URI_OPTIONS_PATTERN =
/
\A
(\w+\.)? # optional prefix key
(https?.*?) # URI
(\.#{Regexp.union(PER_URI_OPTIONS)})? # optional suffix key
\z
/ix

# TODO: duplicates Rubygems#normalize_uri
# TODO: is this the correct place to validate mirror URIs?
def self.normalize_uri(uri)
uri = uri.to_s
uri = "#{uri}/" unless uri =~ %r{/\Z}
if uri =~ NORMALIZE_URI_OPTIONS_PATTERN
prefix = $1
uri = $2
suffix = $3
end
uri = "#{uri}/" unless uri.end_with?("/")
uri = URI(uri)
unless uri.absolute?
raise ArgumentError, format("Gem sources must be absolute. You provided '%s'.", uri)
end
uri
"#{prefix}#{uri}#{suffix}".downcase
end
end
end
10 changes: 10 additions & 0 deletions spec/bundler/settings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,16 @@
URI("http://rubygems-mirror.org/")
)
end

it "normalizes URIs with a fallback_timeout option" do
settings["mirror.https://rubygems.org/.fallback_timeout"] = "true"
expect(settings.all).to include("mirror.https://rubygems.org/.fallback_timeout")
end

it "normalizes URIs with a fallback_timeout option without a trailing slash" do
settings["mirror.https://rubygems.org.fallback_timeout"] = "true"
expect(settings.all).to include("mirror.https://rubygems.org/.fallback_timeout")
end
end

describe "BUNDLE_ keys format" do
Expand Down