add simple.rs example

This commit is contained in:
DarkCat09 2024-08-06 12:32:42 +04:00
parent dbbcf322c3
commit 4f52475f2c
Signed by: DarkCat09
GPG key ID: 0A26CD5B3345D6E3
2 changed files with 58 additions and 0 deletions

View file

@ -20,6 +20,10 @@ tokio-rustls = { version = "0.26.0", default-features = false, features = ["ring
url = "2.5.2"
webpki-roots = "0.26.3"
[[example]]
name = "simple"
path = "examples/simple.rs"
[[example]]
name = "main"
path = "examples/main.rs"

54
examples/simple.rs Normal file
View file

@ -0,0 +1,54 @@
use tokio_gemini::{
certs::{
fingerprint::{generate_fingerprint, Algorithm},
verifier::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;
impl SelfsignedCertVerifier for CertVerifier {
fn verify(
&self,
cert: &tokio_gemini::certs::verifier::CertificateDer,
host: &str,
_now: tokio_gemini::certs::verifier::UnixTime,
) -> Result<bool, tokio_rustls::rustls::Error> {
// For real verification example with known_hosts file
// see examples/main.rs
eprintln!(
"Host = {}\nFingerprint = {}",
host,
generate_fingerprint(cert, Algorithm::Sha512),
);
Ok(true)
}
}