Last active
May 29, 2024 08:41
-
-
Save bplaat/81396b22e1b5cd2743d17ce1c778cb63 to your computer and use it in GitHub Desktop.
Rust raw https request example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [package] | |
| name = "bassietest" | |
| version = "0.1.0" | |
| edition = "2021" | |
| [dependencies] | |
| anyhow = "1.0" | |
| rustls = { version = "0.23", default-features = false, features = ["std", "tls12", "ring"] } | |
| webpki-roots = "0.26" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use std::io::{Read, Write}; | |
| use std::net::TcpStream; | |
| use std::sync::Arc; | |
| fn main() -> anyhow::Result<()> { | |
| let host = "example.com"; | |
| let path = "/"; | |
| let mut socket = TcpStream::connect(format!("{}:443", host))?; | |
| let root_store = | |
| rustls::RootCertStore::from_iter(webpki_roots::TLS_SERVER_ROOTS.iter().cloned()); | |
| let config = rustls::ClientConfig::builder() | |
| .with_root_certificates(root_store) | |
| .with_no_client_auth(); | |
| let mut client = rustls::ClientConnection::new(Arc::new(config), host.try_into()?)?; | |
| let mut stream = rustls::Stream::new(&mut client, &mut socket); | |
| _ = stream.write( | |
| format!( | |
| "GET {} HTTP/1.1\r\nHost: {}\r\nConnection: close\r\n\r\n", | |
| path, host | |
| ) | |
| .as_bytes(), | |
| )?; | |
| let mut response = Vec::new(); | |
| stream.read_to_end(&mut response)?; | |
| std::io::stdout().write_all(&response)?; | |
| Ok(()) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment