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

Code before:
  }
  Ok(file_path)
}

pub fn download_webview2_offline_installer(base_path: &Path, arch: &str) -> crate::Result<PathBuf> {
  let url = if arch == "x64" {
    WEBVIEW2_OFFLINE_INSTALLER_X64_URL
  } else {
    WEBVIEW2_OFFLINE_INSTALLER_X86_URL
  };
  let (guid, filename) = webview2_guid_path(url)?;
  let dir_path = base_path.join(guid);
  let file_path = dir_path.join(filename);
  if !file_path.exists() {
    create_dir_all(dir_path)?;
    std::fs::write(&file_path, download(url)?)?;
  }
  Ok(file_path)
}

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

  let (agent, url) = match std::env::var("GITHUB_CDN") {
    Ok(cdn) => {
      let mut parsed_cdn = url::Url::parse(&cdn).expect("Invalid cdn");
      parsed_cdn.set_path(url);

      (ureq::AgentBuilder::new().build(), parsed_cdn.to_string())
    }
    Err(_) => (
      ureq::AgentBuilder::new().try_proxy_from_env(true).build(),
      url.to_owned(),
    ),
  };

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

#[derive(Clone, Copy)]
pub enum HashAlgorithm {
  #[cfg(target_os = "windows")]
  Sha256,
  Sha1,
}

/// Function used to download a file and checks SHA256 to verify the download.
pub fn download_and_verify(

Review comment (suggestion):
We should propagate the error using `?` instead of panicing with `.expect` or better use it with `.context()` from `anyhow` crate.

```suggestion
      let mut parsed_cdn = url::Url::parse(&cdn).context("invalid tools download host URL")?;
```

You will need to import `anyhow::Context` trait

Code after:
  if !file_path.exists() {
    std::fs::write(&file_path, download(WEBVIEW2_BOOTSTRAPPER_URL)?)?;
  }
  Ok(file_path)
}

pub fn download_webview2_offline_installer(base_path: &Path, arch: &str) -> crate::Result<PathBuf> {
  let url = if arch == "x64" {
    WEBVIEW2_OFFLINE_INSTALLER_X64_URL
  } else {
    WEBVIEW2_OFFLINE_INSTALLER_X86_URL
  };
  let (guid, filename) = webview2_guid_path(url)?;
  let dir_path = base_path.join(guid);
  let file_path = dir_path.join(filename);
  if !file_path.exists() {
    create_dir_all(dir_path)?;
    std::fs::write(&file_path, download(url)?)?;
  }
  Ok(file_path)
}

fn create_agent_and_url(url: &str) -> crate::Result<(ureq::Agent, String)> {
  match std::env::var("TAURI_BUNDLER_TOOLS_GITHUB_MIRROR") {
    Ok(cdn) if url.starts_with("https://github.com/") => {
      let mut parsed_cdn = Url::parse(&cdn)?;
      parsed_cdn.set_path(url);
      Ok((AgentBuilder::new().build(), parsed_cdn.into()))
    }
    _ => Ok((
      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)?;

  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)
}

#[derive(Clone, Copy)]
pub enum HashAlgorithm {
  #[cfg(target_os = "windows")]
  Sha256,