Skip to content

Commit

Permalink
docs: add tls to awc example
Browse files Browse the repository at this point in the history
  • Loading branch information
robjtede committed Dec 29, 2024
1 parent 34327bd commit ee6a6ec
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
6 changes: 3 additions & 3 deletions awc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
30 changes: 22 additions & 8 deletions awc/examples/client.rs
Original file line number Diff line number Diff line change
@@ -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<dyn StdError>> {
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?;
Expand Down

0 comments on commit ee6a6ec

Please sign in to comment.