tokio-gemini/examples/main.rs

59 lines
1.7 KiB
Rust

use tokio_gemini::certs::{file_sscv::FileBasedCertVerifier, insecure::AllowAllCertVerifier};
//
// cargo add tokio_gemini -F file-sscv
// cargo add tokio -F macros,rt-multi-thread,io-util,fs
//
#[tokio::main]
async fn main() -> Result<(), tokio_gemini::LibError> {
let mut args = std::env::args();
let mut insecure = false;
let mut url = "gemini://geminiprotocol.net/".to_owned();
_ = args.next(); // skip exe path
if let Some(arg) = args.next() {
if arg == "-k" {
insecure = true;
if let Some(arg) = args.next() {
url = arg;
}
} else {
url = arg;
}
}
let client = if insecure {
tokio_gemini::Client::builder()
.with_custom_verifier(AllowAllCertVerifier::yes_i_know_what_i_am_doing())
.build()
} else {
tokio_gemini::Client::builder()
.with_selfsigned_cert_verifier(FileBasedCertVerifier::init("known_hosts").await?)
.build()
};
let mut resp = client.request(&url).await?;
{
let status_code = resp.status().status_code();
let status_num: u8 = status_code.into();
eprintln!("{} {:?}", status_num, status_code);
}
if resp.status().reply_type() == tokio_gemini::ReplyType::Success {
let mime = resp.mime()?;
eprintln!("Mime: {}", mime);
if mime.type_() == mime::TEXT {
println!("{}", resp.text().await?);
} else {
eprintln!("Downloading into content.bin");
let mut f = tokio::fs::File::create("content.bin").await?;
tokio::io::copy(&mut resp.stream(), &mut f).await?;
}
} else {
eprintln!("Message: {}", resp.message());
}
Ok(())
}