61 lines
1.8 KiB
Rust
61 lines
1.8 KiB
Rust
// Copyright (c) 2019 Reyk Floeter <contact@reykfloeter.com>
|
|
//
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
// copyright notice and this permission notice appear in all copies.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
#[macro_use]
|
|
extern crate log;
|
|
|
|
mod cert;
|
|
|
|
use cert::ServerKeyPair;
|
|
use log::LevelFilter;
|
|
use std::io;
|
|
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
|
use tokio_libtls::prelude::*;
|
|
|
|
async fn async_https_connect(servername: String) -> io::Result<()> {
|
|
let request = format!(
|
|
"GET / HTTP/1.1\r\n\
|
|
Host: {}\r\n\
|
|
Connection: close\r\n\r\n",
|
|
servername
|
|
);
|
|
|
|
let config = TlsConfigBuilder::new().build()?;
|
|
let mut tls = AsyncTls::connect(&(servername + ":443"), &config, None).await?;
|
|
tls.write_all(request.as_bytes()).await?;
|
|
|
|
let mut buf = vec![0u8; 1024];
|
|
tls.read_exact(&mut buf).await?;
|
|
|
|
let ok = b"HTTP/1.1 200 OK\r\n";
|
|
assert_eq!(&buf[..ok.len()], ok);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[tokio::main]
|
|
async fn main() {
|
|
env_logger::builder()
|
|
.filter_level(LevelFilter::Debug)
|
|
.init();
|
|
|
|
async_https_connect("www.example.com".to_owned())
|
|
.await
|
|
.unwrap();
|
|
|
|
let server_key = ServerKeyPair::new();
|
|
|
|
info!("{:?}", server_key);
|
|
}
|