update examples

This commit is contained in:
quininer 2018-05-03 18:07:31 +08:00
parent 8fc4084e06
commit 41425e0c2a
3 changed files with 56 additions and 54 deletions

View File

@ -22,7 +22,7 @@ rustls = "0.12"
webpki = "0.18.0-alpha" webpki = "0.18.0-alpha"
[dev-dependencies] [dev-dependencies]
futures = "0.2.0" # futures = "0.2.0"
tokio = "0.1.6" tokio = "0.1.6"
[features] [features]

View File

@ -9,10 +9,12 @@ webpki = "0.18.0-alpha"
tokio-rustls = { path = "../..", default-features = false, features = [ "tokio" ] } tokio-rustls = { path = "../..", default-features = false, features = [ "tokio" ] }
tokio = "0.1" tokio = "0.1"
tokio-core = "0.1"
clap = "2.26" clap = "2.26"
webpki-roots = "0.14" webpki-roots = "0.14"
[target.'cfg(unix)'.dependencies] [target.'cfg(unix)'.dependencies]
tokio-file-unix = "0.4" tokio-file-unix = "0.5"
[target.'cfg(not(unix))'.dependencies]
tokio-fs = "0.1"

View File

@ -1,19 +1,19 @@
extern crate clap; extern crate clap;
extern crate rustls; extern crate rustls;
extern crate tokio; extern crate tokio;
extern crate tokio_core;
extern crate webpki; extern crate webpki;
extern crate webpki_roots; extern crate webpki_roots;
extern crate tokio_rustls; extern crate tokio_rustls;
#[cfg(unix)] #[cfg(unix)] extern crate tokio_file_unix;
extern crate tokio_file_unix; #[cfg(not(unix))] extern crate tokio_fs;
use std::sync::Arc; use std::sync::Arc;
use std::net::ToSocketAddrs; use std::net::ToSocketAddrs;
use std::io::{ BufReader, stdout, stdin }; use std::io::BufReader;
use std::fs; use std::fs;
use tokio::io; use tokio::io;
use tokio::net::TcpStream;
use tokio::prelude::*; use tokio::prelude::*;
use clap::{ App, Arg }; use clap::{ App, Arg };
use rustls::ClientConfig; use rustls::ClientConfig;
@ -36,7 +36,7 @@ fn main() {
let port = matches.value_of("port") let port = matches.value_of("port")
.map(|port| port.parse().unwrap()) .map(|port| port.parse().unwrap())
.unwrap_or(443); .unwrap_or(443);
let domain = matches.value_of("domain").unwrap_or(host); let domain = matches.value_of("domain").unwrap_or(host).to_owned();
let cafile = matches.value_of("cafile"); let cafile = matches.value_of("cafile");
let text = format!("GET / HTTP/1.0\r\nHost: {}\r\n\r\n", domain); let text = format!("GET / HTTP/1.0\r\nHost: {}\r\n\r\n", domain);
@ -52,61 +52,61 @@ fn main() {
config.root_store.add_server_trust_anchors(&webpki_roots::TLS_SERVER_ROOTS); config.root_store.add_server_trust_anchors(&webpki_roots::TLS_SERVER_ROOTS);
} }
let arc_config = Arc::new(config); let arc_config = Arc::new(config);
let domain = webpki::DNSNameRef::try_from_ascii_str(domain).unwrap();
// Use async non-blocking I/O for stdin/stdout on Unixy platforms. let socket = TcpStream::connect(&addr);
#[cfg(unix)] #[cfg(unix)]
{ let resp = {
use tokio::io::AsyncRead; use tokio::reactor::Handle;
use tokio_core::reactor::Core; use tokio_file_unix::{ raw_stdin, raw_stdout, File };
use tokio_core::net::TcpStream;
use tokio_file_unix::{ StdFile, File };
let mut core = Core::new().unwrap(); let stdin = raw_stdin()
let handle = core.handle(); .and_then(File::new_nb)
let socket = TcpStream::connect(&addr, &handle); .and_then(|fd| fd.into_reader(&Handle::current()))
.unwrap();
let stdout = raw_stdout()
.and_then(File::new_nb)
.and_then(|fd| fd.into_io(&Handle::current()))
.unwrap();
let stdin = stdin(); socket
let stdin = File::new_nb(StdFile(stdin.lock())).unwrap() .and_then(move |stream| {
.into_io(&handle).unwrap(); let domain = webpki::DNSNameRef::try_from_ascii_str(&domain).unwrap();
arc_config.connect_async(domain, stream)
let stdout = stdout(); })
let stdout = File::new_nb(StdFile(stdout.lock())).unwrap() .and_then(move |stream| io::write_all(stream, text))
.into_io(&handle).unwrap(); .and_then(move |(stream, _)| {
let resp = socket
.and_then(|stream| arc_config.connect_async(domain, stream))
.and_then(|stream| io::write_all(stream, text.as_bytes()))
.and_then(|(stream, _)| {
let (r, w) = stream.split(); let (r, w) = stream.split();
io::copy(r, stdout) io::copy(r, stdout)
.map(|_| ()) .map(drop)
.select(io::copy(stdin, w).map(|_| ())) .select2(io::copy(stdin, w).map(drop))
.map_err(|(e, _)| e) .map_err(|res| res.split().0)
}); })
.map(drop)
.map_err(|err| eprintln!("{:?}", err))
};
core.run(resp).unwrap();
}
// XXX: For now, just use blocking I/O for stdin/stdout on other platforms.
// The network I/O will still be asynchronous and non-blocking.
#[cfg(not(unix))] #[cfg(not(unix))]
{ let resp = {
use std::io::{ Read, Write }; use tokio_fs::{ stdin as tokio_stdin, stdout as tokio_stdout };
use tokio::net::TcpStream;
let socket = TcpStream::connect(&addr); let (stdin, stdout) = (tokio_stdin(), tokio_stdout());
let mut input = Vec::new(); socket
stdin().read_to_end(&mut input).unwrap(); .and_then(move |stream| {
let domain = webpki::DNSNameRef::try_from_ascii_str(&domain).unwrap();
arc_config.connect_async(domain, stream)
})
.and_then(move |stream| io::write_all(stream, text))
.and_then(move |(stream, _)| {
let (r, w) = stream.split();
io::copy(r, stdout)
.map(drop)
.join(io::copy(stdin, w).map(drop))
})
.map(drop)
.map_err(|err| eprintln!("{:?}", err))
};
let resp = socket tokio::run(resp);
.and_then(|stream| arc_config.connect_async(domain, stream))
.and_then(|stream| io::write_all(stream, text.as_bytes()))
.and_then(|(stream, _)| io::write_all(stream, &input))
.and_then(|(stream, _)| io::read_to_end(stream, Vec::new()))
.and_then(|(_, output)| stdout().write_all(&output));
resp.wait().unwrap();
}
} }