pgp/http/
hkp.rs

1//! # HKP key discovery
2//!
3//! Module dedicated to HTTP Keyserver Protocol. Since HKP is just
4//! HTTP, this module only contains a function that formats a given
5//! URI to match [HKP specs].
6//!
7//! [HKP specs]: https://datatracker.ietf.org/doc/html/draft-shaw-openpgp-hkp-00
8
9use http::ureq::http::Uri;
10
11use crate::{Error, Result};
12
13/// Formats the given URI to match the HKP specs.
14///
15/// It basically adds `/pks` plus few query params.
16pub(crate) fn format_key_server_uri(uri: Uri, email: &str) -> Result<Uri> {
17    let authority = uri.host().unwrap_or("localhost");
18    let scheme = match uri.scheme_str() {
19        Some("hkps") => "https",
20        _ => "http",
21    };
22
23    let pks_path = format!("pks/lookup?op=get&search={email}");
24    let path = if uri.path().is_empty() {
25        String::from("/") + &pks_path
26    } else {
27        uri.path().to_owned() + &pks_path
28    };
29
30    let uri = Uri::builder()
31        .scheme(scheme)
32        .authority(authority)
33        .path_and_query(path)
34        .build()
35        .map_err(|err| Error::BuildKeyServerUriError(err.into(), uri))?;
36
37    Ok(uri)
38}