Skip to content

Commit

Permalink
Use CARGO_HTTP_CAINFO CA file in build script if present (#935)
Browse files Browse the repository at this point in the history
  • Loading branch information
ranger-ross authored May 21, 2024
1 parent 1cac88d commit 335407b
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions utoipa-swagger-ui/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{
env,
error::Error,
fs::{self, File},
io,
io::{self, Read},
path::PathBuf,
};

Expand Down Expand Up @@ -170,12 +170,32 @@ struct SwaggerUiDist;
}

fn download_file(url: &str, path: PathBuf) -> Result<(), Box<dyn Error>> {
let mut response = reqwest::blocking::get(url)?;
let mut client_builder = reqwest::blocking::Client::builder();

if let Ok(cainfo) = env::var("CARGO_HTTP_CAINFO") {
match parse_ca_file(&cainfo) {
Ok(cert) => client_builder = client_builder.add_root_certificate(cert),
Err(e) => println!(
"failed to load certificate from CARGO_HTTP_CAINFO `{cainfo}`, attempting to download without it. Error: {e:?}",
),
}
}

let client = client_builder.build()?;

let mut response = client.get(url).send()?;
let mut file = File::create(path)?;
io::copy(&mut response, &mut file)?;
Ok(())
}

fn parse_ca_file(path: &str) -> Result<reqwest::Certificate, Box<dyn Error>> {
let mut buf = Vec::new();
File::open(path)?.read_to_end(&mut buf)?;
let cert = reqwest::Certificate::from_pem(&buf)?;
Ok(cert)
}

fn overwrite_target_file(target_dir: &str, swagger_ui_dist_zip: &str, path_in: PathBuf) {
let filename = path_in.file_name().unwrap().to_str().unwrap();
println!("overwrite file: {:?}", path_in.file_name().unwrap());
Expand Down

0 comments on commit 335407b

Please sign in to comment.