Skip to content

Commit

Permalink
Add redirection support to Bundix::Fetcher
Browse files Browse the repository at this point in the history
When a source is redirected, Bundix::Fetcher will follow the redirection
and use the new URL as the source URL. This is useful for sources that
are hosted on GitHub, as GitHub will redirect to a CDN.

Fixes nix-community#106
  • Loading branch information
nathantalewis committed Jun 20, 2023
1 parent e9f533a commit 91236d0
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/bundix/source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def sh(*args, &block)
Bundix.sh(*args, &block)
end

def download(file, url)
def download(file, url, limit = 10)
warn "Downloading #{file} from #{url}"
uri = URI(url)

Expand All @@ -28,6 +28,13 @@ def download(file, url)
File.open(file, 'wb+') do |local|
resp.read_body { |chunk| local.write(chunk) }
end
when Net::HTTPRedirection
location = resp['location']
raise "http error: Redirection loop detected" if location == url
raise "http error: Too many redirects" if limit < 1

warn "Redirected to #{location}"
download(file, location, limit - 1)
when Net::HTTPUnauthorized, Net::HTTPForbidden
debrief_access_denied(uri.host)
raise "http error #{resp.code}: #{uri.host}"
Expand Down

0 comments on commit 91236d0

Please sign in to comment.