tokio-gemini/examples/simple.rs

54 lines
1.3 KiB
Rust

use async_trait::async_trait;
use tokio_gemini::{
certs::{fingerprint::CertFingerprint, SelfsignedCertVerifier},
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;
#[async_trait]
impl SelfsignedCertVerifier for CertVerifier {
async fn verify(
&self,
cert: &tokio_gemini::certs::CertificateDer<'_>,
host: &str,
port: u16,
) -> Result<bool, tokio_gemini::LibError> {
// For real verification example with known_hosts and DANE
// see examples/main.rs
eprintln!(
"Host = {}:{}\nFingerprint = {}",
host,
port,
CertFingerprint::new_sha256(cert).base64(),
);
Ok(true)
}
}