Multistream
Multistream creates a common socket stream client/server interface across
plaintext TCP, TLS encrypted TCP and UNIX sockets.
Below are the main files from the examples
Client Example
use anyhow::Result;
use multistream::Client;
const HTTP_REQUEST: &[u8] = b"GET / HTTP/1.1\r\nHost: suchprogramming.com\r\n\r\n";
#[tokio::main]
pub async fn main() -> Result<()> {
let mut client = Client::connect("suchprogramming.com:443").await?;
client.send(HTTP_REQUEST).await?;
loop {
let buffer = client.recv().await?;
let html = std::str::from_utf8(&buffer).unwrap();
print!("{}", html);
if html.contains("</html>") {
return Ok(());
}
}
}
Server Example
use anyhow::Result;
use multistream::{Server, CertAndKeyFilePaths};
const RESPONSE: &[u8] = b"HTTP/1.1 200 OK\r\nServer: a very great server\r\n\r\n";
#[tokio::main]
pub async fn main() -> Result<()> {
let cert_and_key = CertAndKeyFilePaths::new("cert.pem", "privkey.pem");
let mut server = Server::listen("0.0.0.0:8443", Some(cert_and_key)).await?;
let mut client = server.accept().await?;
let buffer = client.recv().await?;
println!("{:#?}", std::str::from_utf8(&buffer)?);
client.send(RESPONSE).await?;
Ok(())
}