use std::net::{TcpListener, TcpStream};
use anyhow::Result;
use async_native_tls::{Identity, TlsAcceptor};
use smol::{future, prelude::*, Async};
const RESPONSE: &[u8] = br#"
HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: 47
<!DOCTYPE html><html><body>Hello!</body></html>
"#;
async fn serve(mut stream: Async<TcpStream>, tls: Option<TlsAcceptor>) -> Result<()> {
match tls {
None => {
println!("Serving http://{}", stream.get_ref().local_addr()?);
stream.write_all(RESPONSE).await?;
}
Some(tls) => {
println!("Serving https://{}", stream.get_ref().local_addr()?);
match tls.accept(stream).await {
Ok(mut stream) => {
stream.write_all(RESPONSE).await?;
stream.flush().await?;
stream.close().await?;
}
Err(err) => println!("Failed to establish secure TLS connection: {:#?}", err),
}
}
}
Ok(())
}
async fn listen(listener: Async<TcpListener>, tls: Option<TlsAcceptor>) -> Result<()> {
match &tls {
None => println!("Listening on http://{}", listener.get_ref().local_addr()?),
Some(_) => println!("Listening on https://{}", listener.get_ref().local_addr()?),
}
loop {
let (stream, _) = listener.accept().await?;
let tls = tls.clone();
smol::spawn(async move {
if let Err(err) = serve(stream, tls).await {
println!("Connection error: {:#?}", err);
}
})
.detach();
}
}
fn main() -> Result<()> {
let identity = Identity::from_pkcs12(include_bytes!("identity.pfx"), "password")?;
let tls = TlsAcceptor::from(native_tls::TlsAcceptor::new(identity)?);
smol::block_on(async {
let http = listen(Async::<TcpListener>::bind(([127, 0, 0, 1], 8000))?, None);
let https = listen(
Async::<TcpListener>::bind(([127, 0, 0, 1], 8001))?,
Some(tls),
);
future::try_zip(http, https).await?;
Ok(())
})
}