use async-std and tokio i guess
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Cadey Ratio 2020-07-27 20:01:09 -04:00
parent 564eb3990c
commit 2bc1aa315f
3 changed files with 18 additions and 7 deletions

View File

@ -2,7 +2,7 @@
## 0.4.0 ## 0.4.0
Tokio has been purged. This crate now uses async-std. Tokio has been somewhat purged.
## 0.3.0 ## 0.3.0

View File

@ -15,8 +15,10 @@ num = "0.2"
num-derive = "0.3" num-derive = "0.3"
num-traits = "0.2" num-traits = "0.2"
rustls = { version = "0.18", optional = true, features = ["dangerous_configuration"] } rustls = { version = "0.18", optional = true, features = ["dangerous_configuration"] }
tokio-rustls = { version = "0.14", features = ["dangerous_configuration"], optional = true }
webpki = { version = "0.21.0", optional = true } webpki = { version = "0.21.0", optional = true }
webpki-roots = { version = "0.20", optional = true } webpki-roots = { version = "0.20", optional = true }
tokio = { version = "0.2", features = ["full"], optional = true }
async-tls = { version = "0.9.0", default-features = false, optional = true } async-tls = { version = "0.9.0", default-features = false, optional = true }
async-std = { version = "1.6", optional = true } async-std = { version = "1.6", optional = true }
log = "0.4" log = "0.4"
@ -32,9 +34,10 @@ pretty_env_logger = "0.4"
default = ["client", "server"] default = ["client", "server"]
client = [ client = [
"rustls", "tokio-rustls",
"webpki", "webpki",
"webpki-roots", "webpki-roots",
"tokio",
"async-std", "async-std",
"async-tls/client" "async-tls/client"
] ]

View File

@ -1,8 +1,13 @@
use crate::Response; use crate::Response;
use async_std::{io::prelude::*, net::TcpStream};
use async_tls::TlsConnector;
use rustls::TLSError;
use std::{io::Cursor, sync::Arc}; use std::{io::Cursor, sync::Arc};
use tokio::{
io::{AsyncReadExt, AsyncWriteExt},
net::TcpStream,
};
use tokio_rustls::{
rustls::{TLSError},
TlsConnector,
};
use url::Url; use url::Url;
#[derive(thiserror::Error, Debug)] #[derive(thiserror::Error, Debug)]
@ -26,7 +31,7 @@ pub enum Error {
InvalidScheme(String), InvalidScheme(String),
} }
pub async fn get<T>(u: T, cfg: rustls::ClientConfig) -> Result<crate::Response, Error> pub async fn get<T>(u: T, cfg: tokio_rustls::rustls::ClientConfig) -> Result<crate::Response, Error>
where where
T: Into<String>, T: Into<String>,
{ {
@ -42,10 +47,11 @@ where
let cfg = Arc::new(cfg); let cfg = Arc::new(cfg);
let host = ur.host_str().unwrap(); let host = ur.host_str().unwrap();
let name_ref = webpki::DNSNameRef::try_from_ascii_str(host)?;
let config = TlsConnector::from(cfg); let config = TlsConnector::from(cfg);
let sock = TcpStream::connect(&format!("{}:{}", host, ur.port().unwrap())).await?; let sock = TcpStream::connect(&format!("{}:{}", host, ur.port().unwrap())).await?;
let mut tls = config.connect(host, sock).await?; let mut tls = config.connect(name_ref, sock).await?;
let req = format!("{}\r\n", u); let req = format!("{}\r\n", u);
log::trace!("writing request {:?}", req); log::trace!("writing request {:?}", req);
@ -57,6 +63,8 @@ where
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use tokio_rustls::rustls;
fn config() -> rustls::ClientConfig { fn config() -> rustls::ClientConfig {
let mut config = rustls::ClientConfig::new(); let mut config = rustls::ClientConfig::new();
config config