Skip to content

Commit

Permalink
feat: icx-cert: add --accept-encoding parameter, display content-type…
Browse files Browse the repository at this point in the history
… response header value if present (#276)
  • Loading branch information
ericswanson-dfinity authored Oct 21, 2021
1 parent 20162d2 commit c712fdf
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
13 changes: 11 additions & 2 deletions icx-cert/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,20 @@ mod pprint;
enum Command {
/// Fetches the specified URL and pretty-prints the certificate.
#[clap(name = "print")]
PPrint { url: String },
PPrint {
url: String,

/// Specifies one or more encodings to accept.
#[clap(long, multiple(true), number_of_values(1))]
accept_encoding: Option<Vec<String>>,
},
}

fn main() -> Result<()> {
match Command::parse() {
Command::PPrint { url } => pprint::pprint(url),
Command::PPrint {
url,
accept_encoding,
} => pprint::pprint(url, accept_encoding),
}
}
31 changes: 29 additions & 2 deletions icx-cert/src/pprint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use ic_agent::ic_types::{
hash_tree::{Label, LookupResult},
HashTree,
};
use reqwest::header;
use serde::{de::DeserializeOwned, Deserialize};
use sha2::Digest;

Expand Down Expand Up @@ -64,14 +65,37 @@ fn parse_base64_cbor<T: DeserializeOwned>(s: &str) -> Result<T> {
}

/// Downloads the asset with the specified URL and pretty-print certificate contents.
pub fn pprint(url: String) -> Result<()> {
let response = reqwest::blocking::get(url).with_context(|| "failed to fetch the document")?;
pub fn pprint(url: String, accept_encodings: Option<Vec<String>>) -> Result<()> {
let response = {
let client = reqwest::blocking::Client::builder();
let client = if let Some(accept_encodings) = accept_encodings {
let mut headers = header::HeaderMap::new();
let accept_encodings: String = accept_encodings.join(", ");
headers.insert(
"Accept-Encoding",
header::HeaderValue::from_str(&accept_encodings).unwrap(),
);
client.default_headers(headers)
} else {
client
};
client
.build()?
.get(url)
.send()
.with_context(|| "failed to fetch the document")?
};

let status = response.status().as_u16();
let certificate_header = response
.headers()
.get("IC-Certificate")
.ok_or_else(|| anyhow!("IC-Certificate header not found: {:?}", response.headers()))?
.to_owned();
let content_encoding = response
.headers()
.get("Content-Encoding")
.map(|x| x.to_owned());
let data = response
.bytes()
.with_context(|| "failed to get response body")?;
Expand All @@ -87,6 +111,9 @@ pub fn pprint(url: String) -> Result<()> {

println!("STATUS: {}", status);
println!("ROOT HASH: {}", hex::encode(&cert.tree.digest()));
if let Some(content_encoding) = content_encoding {
println!("CONTENT-ENCODING: {}", content_encoding.to_str().unwrap());
}
println!(
"DATA HASH: {}",
hex::encode(&sha2::Sha256::digest(data.as_ref()))
Expand Down

0 comments on commit c712fdf

Please sign in to comment.