summaryrefslogtreecommitdiffstats
path: root/third_party/rust/h2/examples
diff options
context:
space:
mode:
Diffstat (limited to 'third_party/rust/h2/examples')
-rw-r--r--third_party/rust/h2/examples/akamai.rs85
-rw-r--r--third_party/rust/h2/examples/client.rs52
-rw-r--r--third_party/rust/h2/examples/server.rs65
3 files changed, 202 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..e522b37ff2
--- /dev/null
+++ b/third_party/rust/h2/examples/akamai.rs
@@ -0,0 +1,85 @@
+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_server_trust_anchors(webpki_roots::TLS_SERVER_ROOTS.0.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.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..6d6490db08
--- /dev/null
+++ b/third_party/rust/h2/examples/server.rs
@@ -0,0 +1,65 @@
+use std::error::Error;
+
+use bytes::Bytes;
+use h2::server::{self, SendResponse};
+use h2::RecvStream;
+use http::Request;
+use tokio::net::{TcpListener, TcpStream};
+
+#[tokio::main]
+async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
+ let _ = env_logger::try_init();
+
+ let 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) = serve(socket).await {
+ println!(" -> err={:?}", e);
+ }
+ });
+ }
+ }
+}
+
+async fn serve(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, respond) = result?;
+ tokio::spawn(async move {
+ if let Err(e) = handle_request(request, respond).await {
+ println!("error while handling request: {}", e);
+ }
+ });
+ }
+
+ println!("~~~~~~~~~~~ H2 connection CLOSE !!!!!! ~~~~~~~~~~~");
+ Ok(())
+}
+
+async fn handle_request(
+ mut request: Request<RecvStream>,
+ mut respond: SendResponse<Bytes>,
+) -> Result<(), Box<dyn Error + Send + Sync>> {
+ println!("GOT request: {:?}", request);
+
+ let body = request.body_mut();
+ while let Some(data) = body.data().await {
+ let data = data?;
+ println!("<<<< recv {:?}", data);
+ let _ = body.flow_control().release_capacity(data.len());
+ }
+
+ let response = http::Response::new(());
+ let mut send = respond.send_response(response, false)?;
+ println!(">>>> send");
+ send.send_data(Bytes::from_static(b"hello "), false)?;
+ send.send_data(Bytes::from_static(b"world\n"), true)?;
+
+ Ok(())
+}