Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: better CRL fetching and parsing
Signed-off-by: Richard Zak <richard@profian.com>
- Loading branch information
Showing
5 changed files
with
107 additions
and
42 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,18 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use anyhow::Context; | ||
use std::fs::OpenOptions; | ||
use std::path::Path; | ||
|
||
use sha2::{Digest, Sha256}; | ||
|
||
/// Fetch a URL and save the contents as the hash of the URL | ||
pub fn save_file(url: &str, dest: &Path) -> anyhow::Result<()> { | ||
let mut response = ureq::get(url) | ||
/// Fetch a URL and return the bytes | ||
pub fn fetch_file(url: &str) -> anyhow::Result<Vec<u8>> { | ||
let mut reader = ureq::get(url) | ||
.call() | ||
.context(format!("retrieving CRL {url} from server"))? | ||
.into_reader(); | ||
|
||
let mut dest = dest.to_path_buf(); | ||
dest.push(hex::encode(Sha256::digest(url))); | ||
|
||
let mut file = OpenOptions::new() | ||
.create(true) | ||
.write(true) | ||
.open(&dest) | ||
.context(format!( | ||
"opening destination file {:?} for saving CRL {url}", | ||
dest.display() | ||
))?; | ||
|
||
std::io::copy(&mut response, &mut file).context(format!( | ||
"saving CRL {url} to destination file {:?}", | ||
dest.display() | ||
))?; | ||
let mut bytes = vec![]; | ||
reader | ||
.read_to_end(&mut bytes) | ||
.context("reading bytes buffer")?; | ||
|
||
Ok(()) | ||
Ok(bytes) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters