added example of using mbedtls as a TLS provider

make authentication mode a parameter, default to Required
This commit is contained in:
Michael Richardson
2021-12-24 15:14:23 -05:00
committed by Martin Algesten
parent ff0e91d33e
commit 034981f535
3 changed files with 194 additions and 0 deletions

View File

@@ -0,0 +1,72 @@
use std::io::{self, Read};
use std::sync::{Arc};
use std::time::Duration;
use std::{env, error, fmt, result};
pub mod mbedtls_connector;
use log::{error, info};
use ureq;
#[derive(Debug)]
struct Oops(String);
impl From<io::Error> for Oops {
fn from(e: io::Error) -> Oops {
Oops(e.to_string())
}
}
impl From<ureq::Error> for Oops {
fn from(e: ureq::Error) -> Oops {
Oops(e.to_string())
}
}
impl error::Error for Oops {}
impl fmt::Display for Oops {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
type Result<T> = result::Result<T, Oops>;
fn get(agent: &ureq::Agent, url: &str) -> Result<Vec<u8>> {
let response = agent.get(url).call()?;
let mut reader = response.into_reader();
let mut bytes = vec![];
reader.read_to_end(&mut bytes)?;
Ok(bytes)
}
fn get_and_write(agent: &ureq::Agent, url: &str) {
info!("🕷️ {}", url);
match get(agent, url) {
Ok(_) => info!("Good: ✔️ {}\n", url),
Err(e) => error!("Bad: ⚠️ {} {}\n", url, e),
}
}
fn main() -> Result<()> {
let _args = env::args();
env_logger::init();
let agent = ureq::builder()
.tls_connector(Arc::new(mbedtls_connector::MbedTlsConnector::new(mbedtls::ssl::config::AuthMode::None)))
.timeout_connect(Duration::from_secs(5))
.timeout(Duration::from_secs(20))
.build();
get_and_write(&agent, "https://example.com/");
Ok(())
}
/*
* Local Variables:
* compile-command: "cargo build --example mbedtls-req --features=\"mbedtls\""
* mode: rust
* End:
*/