diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
commit | 26a029d407be480d791972afb5975cf62c9360a6 (patch) | |
tree | f435a8308119effd964b339f76abb83a57c29483 /third_party/rust/h2/examples/akamai.rs | |
parent | Initial commit. (diff) | |
download | firefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz firefox-26a029d407be480d791972afb5975cf62c9360a6.zip |
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/h2/examples/akamai.rs')
-rw-r--r-- | third_party/rust/h2/examples/akamai.rs | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/third_party/rust/h2/examples/akamai.rs b/third_party/rust/h2/examples/akamai.rs new file mode 100644 index 0000000000..788bf3005d --- /dev/null +++ b/third_party/rust/h2/examples/akamai.rs @@ -0,0 +1,82 @@ +use h2::client; +use http::{Method, Request}; +use tokio::net::TcpStream; +use tokio_rustls::TlsConnector; + +use tokio_rustls::rustls::{OwnedTrustAnchor, RootCertStore, ServerName}; + +use std::convert::TryFrom; +use std::error::Error; +use std::net::ToSocketAddrs; + +const ALPN_H2: &str = "h2"; + +#[tokio::main] +pub async fn main() -> Result<(), Box<dyn Error>> { + let _ = env_logger::try_init(); + + let tls_client_config = std::sync::Arc::new({ + let mut root_store = RootCertStore::empty(); + root_store.add_trust_anchors(webpki_roots::TLS_SERVER_ROOTS.iter().map(|ta| { + OwnedTrustAnchor::from_subject_spki_name_constraints( + ta.subject, + ta.spki, + ta.name_constraints, + ) + })); + + let mut c = tokio_rustls::rustls::ClientConfig::builder() + .with_safe_defaults() + .with_root_certificates(root_store) + .with_no_client_auth(); + c.alpn_protocols.push(ALPN_H2.as_bytes().to_owned()); + c + }); + + // Sync DNS resolution. + let addr = "http2.akamai.com:443" + .to_socket_addrs() + .unwrap() + .next() + .unwrap(); + + println!("ADDR: {:?}", addr); + + let tcp = TcpStream::connect(&addr).await?; + let dns_name = ServerName::try_from("http2.akamai.com").unwrap(); + let connector = TlsConnector::from(tls_client_config); + let res = connector.connect(dns_name, tcp).await; + let tls = res.unwrap(); + { + let (_, session) = tls.get_ref(); + let negotiated_protocol = session.alpn_protocol(); + assert_eq!(Some(ALPN_H2.as_bytes()), negotiated_protocol); + } + + println!("Starting client handshake"); + let (mut client, h2) = client::handshake(tls).await?; + + println!("building request"); + let request = Request::builder() + .method(Method::GET) + .uri("https://http2.akamai.com/") + .body(()) + .unwrap(); + + println!("sending request"); + let (response, other) = client.send_request(request, true).unwrap(); + + tokio::spawn(async move { + if let Err(e) = h2.await { + println!("GOT ERR={:?}", e); + } + }); + + println!("waiting on response : {:?}", other); + let (_, mut body) = response.await?.into_parts(); + println!("processing body"); + while let Some(chunk) = body.data().await { + println!("RX: {:?}", chunk?); + } + Ok(()) +} |