From ee6a6ec03e80041783d64cbd00b486fb36272f79 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sun, 29 Dec 2024 15:17:18 +0000 Subject: [PATCH] docs: add tls to awc example --- awc/Cargo.toml | 6 +++--- awc/examples/client.rs | 30 ++++++++++++++++++++++-------- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/awc/Cargo.toml b/awc/Cargo.toml index c09f32ac8..d6626f906 100644 --- a/awc/Cargo.toml +++ b/awc/Cargo.toml @@ -153,9 +153,9 @@ tokio = { version = "1.24.2", features = ["rt-multi-thread", "macros"] } zstd = "0.13" tls-rustls-0_23 = { package = "rustls", version = "0.23" } # add rustls 0.23 with default features to make aws_lc_rs work in tests -[lints] -workspace = true - [[example]] name = "client" required-features = ["rustls-0_23-webpki-roots"] + +[lints] +workspace = true diff --git a/awc/examples/client.rs b/awc/examples/client.rs index 41626315c..b6eb919c7 100644 --- a/awc/examples/client.rs +++ b/awc/examples/client.rs @@ -1,25 +1,39 @@ -use std::error::Error as StdError; +//! Demonstrates construction and usage of a TLS-capable HTTP client. + +extern crate tls_rustls_0_23 as rustls; + +use std::{error::Error as StdError, sync::Arc}; + +use actix_tls::connect::rustls_0_23::webpki_roots_cert_store; +use rustls::ClientConfig; -/// If we want to make requests to addresses starting with `https`, we need to enable the rustls feature of awc -/// `awc = { version = "3.5.0", features = ["rustls"] }` #[actix_rt::main] async fn main() -> Result<(), Box> { env_logger::init_from_env(env_logger::Env::new().default_filter_or("info")); - // construct request builder - let client = awc::Client::new(); + let mut config = ClientConfig::builder() + .with_root_certificates(webpki_roots_cert_store()) + .with_no_client_auth(); + + let protos = vec![b"h2".to_vec(), b"http/1.1".to_vec()]; + config.alpn_protocols = protos; + + // construct request builder with TLS support + let client = awc::Client::builder() + .connector(awc::Connector::new().rustls_0_23(Arc::new(config))) + .finish(); // configure request let request = client .get("https://www.rust-lang.org/") - .append_header(("User-Agent", "Actix-web")); + .append_header(("User-Agent", "awc/3.0")); - println!("Request: {:?}", request); + println!("Request: {request:?}"); let mut response = request.send().await?; // server response head - println!("Response: {:?}", response); + println!("Response: {response:?}"); // read response body let body = response.body().await?;