use std::time::Duration;
use crate::error::{err, ErrorCode, Result};
use crate::keys::{keys_from_response, KeyDoc};
use crate::lexical;
const MAX_BODY: u64 = 256 * 1024;
#[deprecated(
since = "0.3.1",
note = "removed at 1.0: fetch the key document with your own HTTP client and pass the response to `keys_from_response`"
)]
pub fn fetch_keys(host: &str) -> Result<KeyDoc> {
if lexical::check_dns_name(host, "host").is_err() {
return err(ErrorCode::KeyUnavailable, "$", format!("{host:?} is not a DNS name: no keys are resolved for it"));
}
let agent: ureq::Agent = ureq::Agent::config_builder()
.max_redirects(0)
.http_status_as_error(false)
.https_only(true)
.timeout_global(Some(Duration::from_secs(10)))
.build()
.into();
let url = format!("https://{host}/.well-known/datumesh.json");
let mut resp = match agent.get(&url).call() {
Ok(r) => r,
Err(e) => return err(ErrorCode::KeyUnavailable, "$", format!("{url}: {e}")),
};
let status = resp.status().as_u16();
if status != 200 {
return keys_from_response(host, status, None);
}
match resp.body_mut().with_config().limit(MAX_BODY).read_to_vec() {
Ok(body) => keys_from_response(host, status, Some(&body)),
Err(e) => err(ErrorCode::KeyUnavailable, "$", format!("{url}: {e}")),
}
}