Skip to content

Commit

Permalink
chore: update hyper crates, use hyper io traits
Browse files Browse the repository at this point in the history
  • Loading branch information
oddgrd committed Jul 29, 2023
1 parent 7a09dbc commit 1e022d5
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 13 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/check_guides.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ EOF
fi
if [ $value = stable ]; then
cat >> "$value/Cargo.toml" <<-EOF
hyper = { version = "1.0.0-rc.3", features = ["full"] }
hyper = { version = "1.0.0-rc.4", features = ["full"] }
tokio = { version = "1", features = ["full"] }
http-body-util = "0.1.0-rc.2"
http-body-util = "0.1.0-rc.3"
hyper-util = { git = "https://github.com/hyperium/hyper-util.git" }
EOF
cargo build --manifest-path "$value/Cargo.toml"
fi
Expand Down
4 changes: 2 additions & 2 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ relative_links:
plugins:
- jekyll-redirect-from

docs_url: https://docs.rs/hyper/1.0.0-rc.3
docs_url: https://docs.rs/hyper/1.0.0-rc.4
examples_url: https://github.com/hyperium/hyper/tree/master/examples
http_body_util_url: https://docs.rs/http-body-util/0.1.0-rc.2
http_body_util_url: https://docs.rs/http-body-util/0.1.0-rc.3
hyper_tls_url: https://docs.rs/hyper-tls/*

futures_url: https://docs.rs/futures/0.3.*
Expand Down
25 changes: 20 additions & 5 deletions _stable/client/basic.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ Let's tell Cargo about our dependencies by having this in the Cargo.toml.

```toml
[dependencies]
hyper = { version = "1.0.0-rc.3", features = ["full"] }
hyper = { version = "1.0.0-rc.4", features = ["full"] }
tokio = { version = "1", features = ["full"] }
http-body-util = "0.1.0-rc.2"
http-body-util = "0.1.0-rc.3"
hyper-util = { git = "https://github.com/hyperium/hyper-util.git" }
```

Now, we need to import pieces to use from our dependencies:
Expand All @@ -22,9 +23,11 @@ Now, we need to import pieces to use from our dependencies:
# extern crate http_body_util;
# extern crate hyper;
# extern crate tokio;
# extern crate hyper_util;
use http_body_util::Empty;
use hyper::Request;
use hyper::body::Bytes;
use hyper_util::rt::TokioIo;
use tokio::net::TcpStream;
# fn main() {}
```
Expand Down Expand Up @@ -74,10 +77,12 @@ setup we'll spawn a `tokio::task` and `await` it.
```rust
# extern crate http_body_util;
# extern crate hyper;
# extern crate hyper_util;
# extern crate tokio;
# use http_body_util::Empty;
# use hyper::body::Bytes;
# use hyper::Request;
# use hyper_util::rt::TokioIo;
# use tokio::net::TcpStream;
# async fn run() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
// Parse our URL...
Expand All @@ -92,8 +97,12 @@ let address = format!("{}:{}", host, port);
// Open a TCP connection to the remote host
let stream = TcpStream::connect(address).await?;

// Use an adapter to access something implementing `tokio::io` traits as if they implement
// `hyper::rt` IO traits.
let io = TokioIo::new(stream);

// Perform a TCP handshake
let (mut sender, conn) = hyper::client::conn::http1::handshake(stream).await?;
let (mut sender, conn) = hyper::client::conn::http1::handshake(io).await?;

// Spawn a task to poll the connection, driving the HTTP state
tokio::task::spawn(async move {
Expand Down Expand Up @@ -126,18 +135,21 @@ status of the response to see that it returned the expected `200 OK` status.
```rust
# extern crate http_body_util;
# extern crate hyper;
# extern crate hyper_util;
# extern crate tokio;
# use http_body_util::Empty;
# use hyper::body::Bytes;
# use hyper::Request;
# use hyper_util::rt::TokioIo;
# use tokio::net::TcpStream;
# async fn run() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
# let url = "http://httpbin.org/ip".parse::<hyper::Uri>()?;
# let host = url.host().expect("uri has no host");
# let port = url.port_u16().unwrap_or(80);
# let addr = format!("{}:{}", host, port);
# let stream = TcpStream::connect(addr).await?;
# let (mut sender, conn) = hyper::client::conn::http1::handshake(stream).await?;
# let io = TokioIo::new(stream);
# let (mut sender, conn) = hyper::client::conn::http1::handshake(io).await?;
# tokio::task::spawn(async move {
# if let Err(err) = conn.await {
# println!("Connection failed: {:?}", err);
Expand Down Expand Up @@ -182,10 +194,12 @@ use tokio::io::{stdout, AsyncWriteExt as _};
```rust
# extern crate http_body_util;
# extern crate hyper;
# extern crate hyper_util;
# extern crate tokio;
# use http_body_util::{BodyExt, Empty};
# use hyper::body::Bytes;
# use hyper::Request;
# use hyper_util::rt::TokioIo;
# use tokio::net::TcpStream;
# use tokio::io::{self, AsyncWriteExt as _};
# async fn run() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
Expand All @@ -194,7 +208,8 @@ use tokio::io::{stdout, AsyncWriteExt as _};
# let port = url.port_u16().unwrap_or(80);
# let addr = format!("{}:{}", host, port);
# let stream = TcpStream::connect(addr).await?;
# let (mut sender, conn) = hyper::client::conn::http1::handshake(stream).await?;
# let io = TokioIo::new(stream);
# let (mut sender, conn) = hyper::client::conn::http1::handshake(io).await?;
# tokio::task::spawn(async move {
# if let Err(err) = conn.await {
# println!("Connection failed: {:?}", err);
Expand Down
2 changes: 1 addition & 1 deletion _stable/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ You can start using it by first adding it to your `Cargo.toml`:

```toml
[dependencies]
hyper = { version = "1.0.0-rc.3", features = ["full"] }
hyper = { version = "1.0.0-rc.4", features = ["full"] }
```

- If building a web server, continue with the [Server guide][].
Expand Down
15 changes: 12 additions & 3 deletions _stable/server/hello-world.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ First we need to declare our dependencies, let's add the following to our `Cargo

```toml
[dependencies]
hyper = { version = "1.0.0-rc.3", features = ["full"] }
hyper = { version = "1.0.0-rc.4", features = ["full"] }
tokio = { version = "1", features = ["full"] }
http-body-util = "0.1.0-rc.2"
http-body-util = "0.1.0-rc.3"
hyper-util = { git = "https://github.com/hyperium/hyper-util.git" }
```

Next, we need to add some imports in our `main.rs` file:
Expand All @@ -20,6 +21,7 @@ Next, we need to add some imports in our `main.rs` file:
# extern crate tokio;
# extern crate hyper;
# extern crate http_body_util;
# extern crate hyper_util;
use std::convert::Infallible;
use std::net::SocketAddr;

Expand All @@ -28,6 +30,7 @@ use hyper::body::Bytes;
use hyper::server::conn::http1;
use hyper::service::service_fn;
use hyper::{Request, Response};
use hyper_util::rt::TokioIo;
use tokio::net::TcpListener;
# fn main() {}
```
Expand Down Expand Up @@ -72,6 +75,7 @@ We'll dive in to the specifics of some of these things in another guide.
# extern crate tokio;
# extern crate hyper;
# extern crate http_body_util;
# extern crate hyper_util;
# mod no_run {
# use std::convert::Infallible;
# use std::net::SocketAddr;
Expand All @@ -81,6 +85,7 @@ We'll dive in to the specifics of some of these things in another guide.
# use hyper::server::conn::http1;
# use hyper::service::service_fn;
# use hyper::{Request, Response};
# use hyper_util::rt::TokioIo;
# use tokio::net::TcpListener;
# async fn hello(
# _: Request<hyper::body::Incoming>,
Expand All @@ -98,12 +103,16 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
loop {
let (stream, _) = listener.accept().await?;

// Use an adapter to access something implementing `tokio::io` traits as if they implement
// `hyper::rt` IO traits.
let io = TokioIo::new(stream);

// Spawn a tokio task to serve multiple connections concurrently
tokio::task::spawn(async move {
// Finally, we bind the incoming connection to our `hello` service
if let Err(err) = http1::Builder::new()
// `service_fn` converts our function in a `Service`
.serve_connection(stream, service_fn(hello))
.serve_connection(io, service_fn(hello))
.await
{
println!("Error serving connection: {:?}", err);
Expand Down

0 comments on commit 1e022d5

Please sign in to comment.