Debian Codegraph @

crt/s2n/bindings/rust-examples/hyper-server-client/src/bin/client.rs

raw symbols SCIP
1 // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 use bytes::Bytes;
5 use clap::Parser;
6 use http_body_util::{BodyExt, Full};
7 use hyper_util::{client::legacy::Client, rt::TokioExecutor};
8 use std::error::Error;
9 use std::str::FromStr;
10
11 /// NOTE: this CA is to be used for demonstration purposes only!
12 const CA: &[u8] = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/../certs/ca-cert.pem"));
13
14 #[derive(Parser)]
15 struct Args {
16 #[clap(short, long, default_value = "localhost:8888")]
17 addr: String,
18 #[clap(short, long, default_value = "")]
19 body: String,
20 }
21
22 async fn run_client(addr: &str, request_body: Vec<u8>) -> Result<(), Box<dyn Error>> {
23 // Configure the s2n-tls client.
24 let config = {
25 let mut builder = s2n_tls::config::Builder::new();
26 builder.trust_pem(CA)?;
27 builder.build()?
28 };
29
30 // Create a hyper-util client with this configuration, using the s2n-tls-hyper HttpsConnector.
31 let connector = s2n_tls_hyper::connector::HttpsConnector::new(config);
32 let client: Client<_, Full<Bytes>> = Client::builder(TokioExecutor::new()).build(connector);
33
34 // Create an HTTP request to send to the server.
35 let uri = http::Uri::from_str(format!("https://{addr}/").as_str())?;
36 let request: http::Request<Full<Bytes>> = http::Request::builder()
37 .method(http::Method::GET)
38 .uri(uri)
39 .body(Full::from(request_body.clone()))?;
40
41 // Send the request to the server.
42 let response = client.request(request).await?;
43 assert_eq!(response.status(), http::StatusCode::OK);
44
45 // Get the response body.
46 let response_body = response.into_body().collect().await?.to_bytes();
47 println!(
48 "Response body: \n{}",
49 String::from_utf8_lossy(&response_body)
50 );
51
52 Ok(())
53 }
54
55 #[tokio::main]
56 async fn main() -> Result<(), Box<dyn Error>> {
57 let args = Args::parse();
58 run_client(&args.addr, args.body.into_bytes()).await?;
59 Ok(())
60 }