Repository: tauri-apps/tauri
File: crates/tauri-bundler/src/bundle/windows/util.rs (Rust)

Code before:
    .ok()
    .and_then(|template| {
      let re =
        Regex::new(r"https://github.com/([^/]+)/([^/]+)/releases/download/([^/]+)/(.*)").unwrap();
      re.captures(github_url).map(|caps| {
        template
          .replace("<owner>", &caps[1])
          .replace("<repo>", &caps[2])
          .replace("<version>", &caps[3])
          .replace("<asset>", &caps[4])
      })
    })
}

fn generate_mirror_url_from_base(github_url: &str) -> crate::Result<Option<String>> {
  std::env::var("TAURI_BUNDLER_TOOLS_GITHUB_MIRROR")
    .ok()
    .map(|cdn| {
      let mut parsed_cdn = Url::parse(&cdn)?;
      parsed_cdn.set_path(github_url);
      Ok(parsed_cdn.into())
    })
    .transpose()
}

fn generate_alternative_url(url: &str) -> crate::Result<Option<(ureq::Agent, String)>> {
  if !url.starts_with("https://github.com/") {
    return Ok(None);
  }

  generate_mirror_url_from_template(url)
    .or_else(|_| generate_mirror_url_from_base(url))
    .map(|alt_url| alt_url.map(|alt_url| (ureq::AgentBuilder::new().build(), alt_url)))
}

fn create_agent_and_url(url: &str) -> crate::Result<(ureq::Agent, String)> {
  generate_alternative_url(url).map(|alt_url| {
    alt_url.unwrap_or_else(|| {
      (
        ureq::AgentBuilder::new().try_proxy_from_env(true).build(),
        url.to_owned(),
      )
    })
  })
}

pub fn download(url: &str) -> crate::Result<Vec<u8>> {
  log::info!(action = "Downloading"; "{}", url);

  let (agent, final_url) = create_agent_and_url(url)?;


Review comment (suggestion):
With the change requested above, this can also be changed to just return an `Option<(ureq::Agent, Stirng)>` 

Code after:
    .ok()
    .and_then(|template| {
      let re =
        Regex::new(r"https://github.com/([^/]+)/([^/]+)/releases/download/([^/]+)/(.*)").unwrap();
      re.captures(github_url).map(|caps| {
        template
          .replace("<owner>", &caps[1])
          .replace("<repo>", &caps[2])
          .replace("<version>", &caps[3])
          .replace("<asset>", &caps[4])
      })
    })
}

fn generate_github_mirror_url_from_base(github_url: &str) -> Option<String> {
  std::env::var("TAURI_BUNDLER_TOOLS_GITHUB_MIRROR")
    .ok()
    .and_then(|cdn| Url::parse(&cdn).ok())
    .map(|mut cdn| {
      cdn.set_path(github_url);
      cdn.to_string()
    })
}

fn generate_github_alternative_url(url: &str) -> Option<(ureq::Agent, String)> {
  if !url.starts_with("https://github.com/") {
    return None;
  }

  generate_github_mirror_url_from_template(url)
    .or_else(|| generate_github_mirror_url_from_base(url))
    .map(|alt_url| (ureq::AgentBuilder::new().build(), alt_url))
}

fn create_agent_and_url(url: &str) -> (ureq::Agent, String) {
  generate_github_alternative_url(url).unwrap_or((
    ureq::AgentBuilder::new().try_proxy_from_env(true).build(),
    url.to_owned(),
  ))
}

pub fn download(url: &str) -> crate::Result<Vec<u8>> {
  let (agent, final_url) = create_agent_and_url(url);

  log::info!(action = "Downloading"; "{}", final_url);

  let response = agent.get(&final_url).call().map_err(Box::new)?;
  let mut bytes = Vec::new();
  response.into_reader().read_to_end(&mut bytes)?;
  Ok(bytes)
}