This repository has been archived by the owner on Feb 20, 2023. It is now read-only.
/
https.rs
67 lines (54 loc) · 1.51 KB
/
https.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
cargo run -p async-http-client-lite-demo-async-net --bin https httpbin.org 443 /ip
*/
use std::env;
use std::io;
use futures::executor::block_on;
use async_http_client_lite::{
AsyncNetTcpConnector, ClientBackendKind, ClientTls, ClientTlsKind, Connector, Request,
};
fn main() -> io::Result<()> {
block_on(run())
}
async fn run() -> io::Result<()> {
let domain = env::args()
.nth(1)
.unwrap_or_else(|| env::var("DOMAIN").unwrap_or("httpbin.org".to_owned()));
let port: u16 = env::args()
.nth(2)
.unwrap_or_else(|| env::var("PORT").unwrap_or("443".to_owned()))
.parse()
.unwrap();
let uri = env::args()
.nth(3)
.unwrap_or_else(|| env::var("URI").unwrap_or("/ip".to_owned()));
println!("https {} {} {}", domain, port, uri);
//
let addr = format!("{}:{}", domain, port);
//
let mut client = AsyncNetTcpConnector::connect(
addr,
None,
Some(ClientTls::new(
ClientTlsKind::default_async_tls(),
domain.to_owned(),
)),
)
.await?;
//
let request = Request::builder()
.method("GET")
.uri(uri)
.header("Host", domain)
.header("User-Agent", "curl/7.71.1")
.header("Accept", "*/*")
.body(vec![])
.unwrap();
println!("{:?}", request);
let (response, _) = client
.response(request, ClientBackendKind::H1AsyncHttp1Lite)
.await?;
println!("{:?}", response);
println!("done");
Ok(())
}