Skip to content

Commit

Permalink
Add Experimental HTTP/3 Support (#1599)
Browse files Browse the repository at this point in the history
This adds experimental HTTP/3 support, using `h3` and `h3-quinn` (though the internals are not to be depended on).
  • Loading branch information
kckeiks committed Mar 16, 2023
1 parent 119366e commit 57a8a01
Show file tree
Hide file tree
Showing 10 changed files with 742 additions and 42 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
target
Cargo.lock
*.swp
.idea
10 changes: 10 additions & 0 deletions Cargo.toml
Expand Up @@ -62,6 +62,9 @@ stream = ["tokio/fs", "tokio-util", "wasm-streams"]

socks = ["tokio-socks"]

# Experimental HTTP/3 client.
http3 = ["rustls-tls", "h3", "h3-quinn", "quinn", "futures-channel"]

# Internal (PRIVATE!) features used to aid testing.
# Don't rely on these whatsoever. They may disappear at anytime.

Expand Down Expand Up @@ -135,6 +138,13 @@ tokio-socks = { version = "0.5.1", optional = true }
## trust-dns
trust-dns-resolver = { version = "0.22", optional = true }

# HTTP/3 experimental support
h3 = { version="0.0.1", optional = true }
h3-quinn = { version="0.0.1", optional = true }
quinn = { version = "0.8", default-features = false, features = ["tls-rustls", "ring"], optional = true }
futures-channel = { version="0.3", optional = true}


[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
env_logger = "0.8"
hyper = { version = "0.14", default-features = false, features = ["tcp", "stream", "http1", "http2", "client", "server", "runtime"] }
Expand Down
51 changes: 51 additions & 0 deletions examples/h3_simple.rs
@@ -0,0 +1,51 @@
#![deny(warnings)]

// This is using the `tokio` runtime. You'll need the following dependency:
//
// `tokio = { version = "1", features = ["full"] }`
#[cfg(feature = "http3")]
#[cfg(not(target_arch = "wasm32"))]
#[tokio::main]
async fn main() -> Result<(), reqwest::Error> {
use http::Version;
use reqwest::{Client, IntoUrl, Response};

async fn get<T: IntoUrl + Clone>(url: T) -> reqwest::Result<Response> {
Client::builder()
.http3_prior_knowledge()
.build()?
.get(url)
.version(Version::HTTP_3)
.send()
.await
}

// Some simple CLI args requirements...
let url = match std::env::args().nth(1) {
Some(url) => url,
None => {
println!("No CLI URL provided, using default.");
"https://hyper.rs".into()
}
};

eprintln!("Fetching {:?}...", url);

let res = get(url).await?;

eprintln!("Response: {:?} {}", res.version(), res.status());
eprintln!("Headers: {:#?}\n", res.headers());

let body = res.text().await?;

println!("{}", body);

Ok(())
}

// The [cfg(not(target_arch = "wasm32"))] above prevent building the tokio::main function
// for wasm32 target, because tokio isn't compatible with wasm32.
// If you aren't building for wasm32, you don't need that line.
// The two lines below avoid the "'main' function not found" error when building for wasm32 target.
#[cfg(any(target_arch = "wasm32", not(feature = "http3")))]
fn main() {}

0 comments on commit 57a8a01

Please sign in to comment.