summaryrefslogtreecommitdiffstats
path: root/third_party/rust/h2/examples
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
commit2aa4a82499d4becd2284cdb482213d541b8804dd (patch)
treeb80bf8bf13c3766139fbacc530efd0dd9d54394c /third_party/rust/h2/examples
parentInitial commit. (diff)
downloadfirefox-upstream.tar.xz
firefox-upstream.zip
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/h2/examples')
-rw-r--r--third_party/rust/h2/examples/akamai.rs75
-rw-r--r--third_party/rust/h2/examples/client.rs52
-rw-r--r--third_party/rust/h2/examples/server.rs44
3 files changed, 171 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..29d8a93474
--- /dev/null
+++ b/third_party/rust/h2/examples/akamai.rs
@@ -0,0 +1,75 @@
+use h2::client;
+use http::{Method, Request};
+use tokio::net::TcpStream;
+use tokio_rustls::TlsConnector;
+
+use rustls::Session;
+use webpki::DNSNameRef;
+
+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 c = rustls::ClientConfig::new();
+ c.root_store
+ .add_server_trust_anchors(&webpki_roots::TLS_SERVER_ROOTS);
+ 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 = DNSNameRef::try_from_ascii_str("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.get_alpn_protocol();
+ assert_eq!(
+ Some(ALPN_H2.as_bytes()),
+ negotiated_protocol.as_ref().map(|x| &**x)
+ );
+ }
+
+ 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(())
+}
diff --git a/third_party/rust/h2/examples/client.rs b/third_party/rust/h2/examples/client.rs
new file mode 100644
index 0000000000..61e237aa3c
--- /dev/null
+++ b/third_party/rust/h2/examples/client.rs
@@ -0,0 +1,52 @@
+use h2::client;
+use http::{HeaderMap, Request};
+
+use std::error::Error;
+
+use tokio::net::TcpStream;
+
+#[tokio::main]
+pub async fn main() -> Result<(), Box<dyn Error>> {
+ let _ = env_logger::try_init();
+
+ let tcp = TcpStream::connect("127.0.0.1:5928").await?;
+ let (mut client, h2) = client::handshake(tcp).await?;
+
+ println!("sending request");
+
+ let request = Request::builder()
+ .uri("https://http2.akamai.com/")
+ .body(())
+ .unwrap();
+
+ let mut trailers = HeaderMap::new();
+ trailers.insert("zomg", "hello".parse().unwrap());
+
+ let (response, mut stream) = client.send_request(request, false).unwrap();
+
+ // send trailers
+ stream.send_trailers(trailers).unwrap();
+
+ // Spawn a task to run the conn...
+ tokio::spawn(async move {
+ if let Err(e) = h2.await {
+ println!("GOT ERR={:?}", e);
+ }
+ });
+
+ let response = response.await?;
+ println!("GOT RESPONSE: {:?}", response);
+
+ // Get the body
+ let mut body = response.into_body();
+
+ while let Some(chunk) = body.data().await {
+ println!("GOT CHUNK = {:?}", chunk?);
+ }
+
+ if let Some(trailers) = body.trailers().await? {
+ println!("GOT TRAILERS: {:?}", trailers);
+ }
+
+ Ok(())
+}
diff --git a/third_party/rust/h2/examples/server.rs b/third_party/rust/h2/examples/server.rs
new file mode 100644
index 0000000000..1753b7a2e8
--- /dev/null
+++ b/third_party/rust/h2/examples/server.rs
@@ -0,0 +1,44 @@
+use std::error::Error;
+
+use bytes::Bytes;
+use h2::server;
+use tokio::net::{TcpListener, TcpStream};
+
+#[tokio::main]
+async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
+ let _ = env_logger::try_init();
+
+ let mut listener = TcpListener::bind("127.0.0.1:5928").await?;
+
+ println!("listening on {:?}", listener.local_addr());
+
+ loop {
+ if let Ok((socket, _peer_addr)) = listener.accept().await {
+ tokio::spawn(async move {
+ if let Err(e) = handle(socket).await {
+ println!(" -> err={:?}", e);
+ }
+ });
+ }
+ }
+}
+
+async fn handle(socket: TcpStream) -> Result<(), Box<dyn Error + Send + Sync>> {
+ let mut connection = server::handshake(socket).await?;
+ println!("H2 connection bound");
+
+ while let Some(result) = connection.accept().await {
+ let (request, mut respond) = result?;
+ println!("GOT request: {:?}", request);
+ let response = http::Response::new(());
+
+ let mut send = respond.send_response(response, false)?;
+
+ println!(">>>> sending data");
+ send.send_data(Bytes::from_static(b"hello world"), true)?;
+ }
+
+ println!("~~~~~~~~~~~~~~~~~~~~~~~~~~~ H2 connection CLOSE !!!!!! ~~~~~~~~~~~");
+
+ Ok(())
+}