diff --git a/Library/Homebrew/env_config.rb b/Library/Homebrew/env_config.rb
index 20a9a9cd6b..065d8497df 100644
--- a/Library/Homebrew/env_config.rb
+++ b/Library/Homebrew/env_config.rb
@@ -46,8 +46,12 @@ module Homebrew
},
HOMEBREW_BOTTLE_DOMAIN: {
description: "Use this URL as the download mirror for bottles. " \
+ "If bottles at that URL are temporarily unavailable, " \
+ "the default bottle domain will be used as a fallback mirror. " \
"For example, `HOMEBREW_BOTTLE_DOMAIN=http://localhost:8080` will cause all bottles to " \
- "download from the prefix `http://localhost:8080/`.",
+ "download from the prefix `http://localhost:8080/`. " \
+ "If bottles are not available at `HOMEBREW_BOTTLE_DOMAIN` " \
+ "they will be downloaded from the default bottle domain.",
default_text: "macOS: `https://homebrew.bintray.com/`, " \
"Linux: `https://linuxbrew.bintray.com/`.",
default: HOMEBREW_BOTTLE_DEFAULT_DOMAIN,
diff --git a/Library/Homebrew/software_spec.rb b/Library/Homebrew/software_spec.rb
index 13f62a0177..71ddf95a22 100644
--- a/Library/Homebrew/software_spec.rb
+++ b/Library/Homebrew/software_spec.rb
@@ -2,6 +2,7 @@
# frozen_string_literal: true
require "resource"
+require "download_strategy"
require "checksum"
require "version"
require "options"
@@ -292,7 +293,7 @@ class Bottle
attr_reader :name, :resource, :prefix, :cellar, :rebuild
- def_delegators :resource, :url, :fetch, :verify_download_integrity
+ def_delegators :resource, :url, :verify_download_integrity
def_delegators :resource, :cached_download, :clear_cache
def initialize(formula, spec)
@@ -314,6 +315,18 @@ class Bottle
@rebuild = spec.rebuild
end
+ def fetch(verify_download_integrity: true)
+ # add the default bottle domain as a fallback mirror
+ if @resource.download_strategy == CurlDownloadStrategy &&
+ @resource.url.start_with?(Homebrew::EnvConfig.bottle_domain)
+ fallback_url = @resource.url
+ .sub(/^#{Regexp.escape(Homebrew::EnvConfig.bottle_domain)}/,
+ HOMEBREW_BOTTLE_DEFAULT_DOMAIN)
+ @resource.mirror(fallback_url) if [@resource.url, *@resource.mirrors].exclude?(fallback_url)
+ end
+ @resource.fetch(verify_download_integrity: verify_download_integrity)
+ end
+
def compatible_locations?
@spec.compatible_locations?
end
diff --git a/docs/Manpage.md b/docs/Manpage.md
index 37953ca162..3cd394612c 100644
--- a/docs/Manpage.md
+++ b/docs/Manpage.md
@@ -1729,7 +1729,7 @@ example, run `export HOMEBREW_NO_INSECURE_REDIRECT=1` rather than just
If set, use Bootsnap to speed up repeated `brew` calls. A no-op when using Homebrew's vendored, relocatable Ruby on macOS (as it doesn't work).
- `HOMEBREW_BOTTLE_DOMAIN`
-
Use this URL as the download mirror for bottles. For example, `HOMEBREW_BOTTLE_DOMAIN=http://localhost:8080` will cause all bottles to download from the prefix `http://localhost:8080/`.
+
Use this URL as the download mirror for bottles. If bottles at that URL are temporarily unavailable, the default bottle domain will be used as a fallback mirror. For example, `HOMEBREW_BOTTLE_DOMAIN=http://localhost:8080` will cause all bottles to download from the prefix `http://localhost:8080/`. If bottles are not available at `HOMEBREW_BOTTLE_DOMAIN` they will be downloaded from the default bottle domain.
*Default:* macOS: `https://homebrew.bintray.com/`, Linux: `https://linuxbrew.bintray.com/`.
diff --git a/manpages/brew.1 b/manpages/brew.1
index 7125d4bbd5..2356370dbe 100644
--- a/manpages/brew.1
+++ b/manpages/brew.1
@@ -2415,7 +2415,7 @@ If set, use Bootsnap to speed up repeated \fBbrew\fR calls\. A no\-op when using
\fBHOMEBREW_BOTTLE_DOMAIN\fR
.
.br
-Use this URL as the download mirror for bottles\. For example, \fBHOMEBREW_BOTTLE_DOMAIN=http://localhost:8080\fR will cause all bottles to download from the prefix \fBhttp://localhost:8080/\fR\.
+Use this URL as the download mirror for bottles\. If bottles at that URL are temporarily unavailable, the default bottle domain will be used as a fallback mirror\. For example, \fBHOMEBREW_BOTTLE_DOMAIN=http://localhost:8080\fR will cause all bottles to download from the prefix \fBhttp://localhost:8080/\fR\. If bottles are not available at \fBHOMEBREW_BOTTLE_DOMAIN\fR they will be downloaded from the default bottle domain\.
.
.IP
\fIDefault:\fR macOS: \fBhttps://homebrew\.bintray\.com/\fR, Linux: \fBhttps://linuxbrew\.bintray\.com/\fR\.