tokio-gemini/examples/simple.rs

53 lines
1.3 KiB
Rust
Raw Normal View History

2024-08-06 11:32:42 +03:00
use tokio_gemini::{
certs::{fingerprint::CertFingerprint, SelfsignedCertVerifier},
2024-08-06 11:32:42 +03:00
Client, LibError,
};
// Much simpler than examples/main.rs
// Hardcoded URL, no cert check, always write to stdout
//
// cargo add tokio-gemini
// cargo add tokio -F macros,rt-multi-thread
//
const URL: &str = "gemini://geminiprotocol.net/docs/protocol-specification.gmi";
#[tokio::main]
async fn main() -> Result<(), LibError> {
let client = Client::builder()
.with_selfsigned_cert_verifier(CertVerifier)
.build();
match client.request(URL).await?.ensure_ok() {
Ok(mut resp) => {
println!("{}", resp.text().await?);
}
Err(resp) => {
println!("{} {}", resp.status().num(), resp.message());
}
}
Ok(())
}
struct CertVerifier;
impl SelfsignedCertVerifier for CertVerifier {
async fn verify<'c>(
2024-08-06 11:32:42 +03:00
&self,
cert: &'c tokio_gemini::certs::CertificateDer<'c>,
2024-08-06 11:32:42 +03:00
host: &str,
port: u16,
) -> Result<bool, tokio_gemini::LibError> {
// For real verification example with known_hosts and DANE
2024-08-06 11:32:42 +03:00
// see examples/main.rs
eprintln!(
"Host = {}:{}\nFingerprint = {}",
2024-08-06 11:32:42 +03:00
host,
port,
CertFingerprint::new_sha256(cert).base64(),
2024-08-06 11:32:42 +03:00
);
Ok(true)
}
}