client.rs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. use h2::client;
  2. use http::{HeaderMap, Request};
  3. use std::error::Error;
  4. use tokio::net::TcpStream;
  5. #[tokio::main]
  6. pub async fn main() -> Result<(), Box<dyn Error>> {
  7. let _ = env_logger::try_init();
  8. let tcp = TcpStream::connect("127.0.0.1:5928").await?;
  9. let (mut client, h2) = client::handshake(tcp).await?;
  10. println!("sending request");
  11. let request = Request::builder()
  12. .uri("https://http2.akamai.com/")
  13. .body(())
  14. .unwrap();
  15. let mut trailers = HeaderMap::new();
  16. trailers.insert("zomg", "hello".parse().unwrap());
  17. let (response, mut stream) = client.send_request(request, false).unwrap();
  18. // send trailers
  19. stream.send_trailers(trailers).unwrap();
  20. // Spawn a task to run the conn...
  21. tokio::spawn(async move {
  22. if let Err(e) = h2.await {
  23. println!("GOT ERR={:?}", e);
  24. }
  25. });
  26. let response = response.await?;
  27. println!("GOT RESPONSE: {:?}", response);
  28. // Get the body
  29. let mut body = response.into_body();
  30. while let Some(chunk) = body.data().await {
  31. println!("GOT CHUNK = {:?}", chunk?);
  32. }
  33. if let Some(trailers) = body.trailers().await? {
  34. println!("GOT TRAILERS: {:?}", trailers);
  35. }
  36. Ok(())
  37. }