|
1 |
| -use std::error::Error as StdError; |
| 1 | +//! Demonstrates construction and usage of a TLS-capable HTTP client. |
| 2 | +
|
| 3 | +extern crate tls_rustls_0_23 as rustls; |
| 4 | + |
| 5 | +use std::{error::Error as StdError, sync::Arc}; |
| 6 | + |
| 7 | +use actix_tls::connect::rustls_0_23::webpki_roots_cert_store; |
| 8 | +use rustls::ClientConfig; |
2 | 9 |
|
3 |
| -/// If we want to make requests to addresses starting with `https`, we need to enable the rustls feature of awc |
4 |
| -/// `awc = { version = "3.5.0", features = ["rustls"] }` |
5 | 10 | #[actix_rt::main]
|
6 | 11 | async fn main() -> Result<(), Box<dyn StdError>> {
|
7 | 12 | env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
|
8 | 13 |
|
9 |
| - // construct request builder |
10 |
| - let client = awc::Client::new(); |
| 14 | + let mut config = ClientConfig::builder() |
| 15 | + .with_root_certificates(webpki_roots_cert_store()) |
| 16 | + .with_no_client_auth(); |
| 17 | + |
| 18 | + let protos = vec![b"h2".to_vec(), b"http/1.1".to_vec()]; |
| 19 | + config.alpn_protocols = protos; |
| 20 | + |
| 21 | + // construct request builder with TLS support |
| 22 | + let client = awc::Client::builder() |
| 23 | + .connector(awc::Connector::new().rustls_0_23(Arc::new(config))) |
| 24 | + .finish(); |
11 | 25 |
|
12 | 26 | // configure request
|
13 | 27 | let request = client
|
14 | 28 | .get("https://www.rust-lang.org/")
|
15 |
| - .append_header(("User-Agent", "Actix-web")); |
| 29 | + .append_header(("User-Agent", "awc/3.0")); |
16 | 30 |
|
17 |
| - println!("Request: {:?}", request); |
| 31 | + println!("Request: {request:?}"); |
18 | 32 |
|
19 | 33 | let mut response = request.send().await?;
|
20 | 34 |
|
21 | 35 | // server response head
|
22 |
| - println!("Response: {:?}", response); |
| 36 | + println!("Response: {response:?}"); |
23 | 37 |
|
24 | 38 | // read response body
|
25 | 39 | let body = response.body().await?;
|
|
0 commit comments