Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to http-body 1.0 #348

Merged
merged 59 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from 56 commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
6c76166
punt on examples for now
davidpdrsn Mar 24, 2023
35c9aed
remove dependency on hyper
davidpdrsn Mar 24, 2023
375bfa1
port to http-body 1.0
davidpdrsn Mar 24, 2023
9fad529
note which types from http_body_util is in our public api
davidpdrsn Mar 24, 2023
8734fc4
wrap body types
davidpdrsn Mar 24, 2023
c129997
fix some warnings
davidpdrsn Nov 10, 2023
a34e5a2
comment out compression stuff for now
davidpdrsn Nov 10, 2023
bfe751f
validate-request docs
davidpdrsn Nov 10, 2023
f951e1a
trace
davidpdrsn Nov 10, 2023
46e3465
timeout
davidpdrsn Nov 10, 2023
3419c36
set_status
davidpdrsn Nov 10, 2023
92d3eb2
set_header
davidpdrsn Nov 10, 2023
fbee8cc
serve_dir
davidpdrsn Nov 10, 2023
54b1670
redirect
davidpdrsn Nov 10, 2023
fdaa24f
follow_redirect
davidpdrsn Nov 10, 2023
4b412bb
sensitive_headers
davidpdrsn Nov 10, 2023
f14f012
request_id
davidpdrsn Nov 10, 2023
6efd5df
propagate_header
davidpdrsn Nov 10, 2023
b06f942
normalize_path
davidpdrsn Nov 10, 2023
9c98dc0
in_flight_requests
davidpdrsn Nov 10, 2023
2e481e4
map_response_body
davidpdrsn Nov 10, 2023
615ef81
map_request_body
davidpdrsn Nov 10, 2023
8f14848
limit
davidpdrsn Nov 10, 2023
ec8fca4
lib
davidpdrsn Nov 10, 2023
c3fab2a
cors
davidpdrsn Nov 10, 2023
bec9c5d
classify
davidpdrsn Nov 10, 2023
197e5b5
add_extension
davidpdrsn Nov 10, 2023
27eda19
catch_panic
davidpdrsn Nov 10, 2023
0fe1db5
add_authorization
davidpdrsn Nov 10, 2023
a556ad5
require_authorization + async_require_authorization
davidpdrsn Nov 10, 2023
618df5a
builder
davidpdrsn Nov 10, 2023
19b7656
format
davidpdrsn Nov 10, 2023
345c515
Merge branch 'master' into http-body-1.0
davidpdrsn Nov 10, 2023
83d4681
fixes
davidpdrsn Nov 10, 2023
fcdabf8
fix docs
davidpdrsn Nov 10, 2023
b7b33bb
fix cargo hack
davidpdrsn Nov 10, 2023
5807013
is this breaking patches in Cargo.toml?
davidpdrsn Nov 10, 2023
602f06f
update to 1.0 of dependencies
davidpdrsn Nov 15, 2023
a93aab0
porting compression like this seems to work \o/
davidpdrsn Nov 19, 2023
bb91534
use `BodyExt::collect` in tests
davidpdrsn Nov 19, 2023
d69d18c
compression is back!
davidpdrsn Nov 19, 2023
29e3d45
and thats decompression!
davidpdrsn Nov 19, 2023
30a8cad
remove hyper specific test
davidpdrsn Nov 19, 2023
3b41aab
Update examples/axum-key-value-store/src/main.rs
davidpdrsn Nov 19, 2023
43ba8d8
bring back client examples
davidpdrsn Nov 19, 2023
76de6cc
fix doc tests
davidpdrsn Nov 19, 2023
2534cc7
Merge branch 'master' into http-body-1.0
davidpdrsn Nov 19, 2023
ccb3e99
forgot ServiceBuilderExt
davidpdrsn Nov 19, 2023
77ba9a8
changelog
davidpdrsn Nov 19, 2023
6adc41c
Fix potential ordering issues with `BodyIntoStream`
davidpdrsn Nov 20, 2023
aa9ad9c
don't delegate `is_end_stream`
davidpdrsn Nov 20, 2023
298ba9a
fix typos
davidpdrsn Nov 20, 2023
cc08044
use std::task::ready
davidpdrsn Nov 20, 2023
2813604
uncomment CompressionLayer in examples
davidpdrsn Nov 20, 2023
4a68a6d
Derive default
davidpdrsn Nov 20, 2023
2f936e3
Merge branch 'master' into http-body-1.0
davidpdrsn Nov 20, 2023
677012b
format
davidpdrsn Nov 20, 2023
ec64471
bring back hyper compat test
davidpdrsn Nov 21, 2023
bd40051
remove lint. Lets just add this back later. Wanna merge now
davidpdrsn Nov 21, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[workspace]
resolver = "2"
members = [
"tower-http",
"examples/*",
Expand Down
6 changes: 6 additions & 0 deletions examples/axum-key-value-store/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
fn main() {
eprintln!("this example has not yet been updated to hyper 1.0");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not? I did it with little effort for the tower-async version. Want me to contribute this as a PR to your PR?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same reason as #348 (comment), axum doesn't yet support http 1.0

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True. For now I was using your branch. I get you might not want to do that, but now you do not have an example at all... so dunno.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

axum doesn't yet support http 1.0

but now it does, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep it does.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A PR to restore this example using axum 0.7: #448

}

/*
use axum::{
body::Bytes,
extract::{Path, State},
Expand Down Expand Up @@ -108,3 +113,4 @@ async fn set_key(Path(path): Path<String>, state: State<AppState>, value: Bytes)

// See https://github.com/tokio-rs/axum/blob/main/examples/testing/src/main.rs for an example of
// how to test axum apps
*/
6 changes: 6 additions & 0 deletions examples/tonic-key-value-store/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
fn main() {
eprint!("this example has not yet been updated to hyper 1.0");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not? I did it with little effort for the tower-async version. Want me to contribute this as a PR to your PR?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How did you port it? Tonic doesn't support http 1.0. Did you do a bunch of mapping of the types?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I was wrong. Tonic and Warp examples I deleted during my original port as I do not use these libraries. I only kept in the hyper and axum ones and those are the only once I fixed this time as well.

}

/*
use bytes::Bytes;
use clap::Parser;
use futures::StreamExt;
Expand Down Expand Up @@ -370,3 +375,4 @@ mod tests {
addr
}
}
*/
6 changes: 6 additions & 0 deletions examples/warp-key-value-store/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
fn main() {
eprint!("this example has not yet been updated to hyper 1.0");
}

/*
use bytes::Bytes;
use clap::Parser;
use hyper::{
Expand Down Expand Up @@ -222,3 +227,4 @@ mod tests {
addr
}
}
*/
3 changes: 3 additions & 0 deletions tower-http/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Changed

- Bump Minimum Supported Rust Version to 1.66 ([#433])
- Update to http-body 1.0 ([#348])
- Update to http 1.0 ([#348])
- Preserve service error type in RequestDecompression ([#368])

## Removed
Expand All @@ -27,6 +29,7 @@ http-range-header to `0.4`

[#418]: https://github.com/tower-rs/tower-http/pull/418
[#433]: https://github.com/tower-rs/tower-http/pull/433
[#348]: https://github.com/tower-rs/tower-http/pull/348
[#368]: https://github.com/tower-rs/tower-http/pull/368

# 0.4.2 (July 19, 2023)
Expand Down
16 changes: 10 additions & 6 deletions tower-http/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "tower-http"
description = "Tower middleware and utilities for HTTP clients and servers"
version = "0.4.2"
version = "0.4.4"
authors = ["Tower Maintainers <[email protected]>"]
edition = "2018"
license = "MIT"
Expand All @@ -15,8 +15,9 @@ rust-version = "1.66"
[dependencies]
bitflags = "2.0.2"
bytes = "1"
http = "0.2.7"
http-body = "0.4.5"
http = "1.0"
http-body = "1.0.0"
http-body-util = "0.1.0"
pin-project-lite = "0.2.7"
tower-layer = "0.3"
tower-service = "0.3"
Expand All @@ -38,16 +39,19 @@ httpdate = { version = "1.0", optional = true }
uuid = { version = "1.0", features = ["v4"], optional = true }

[dev-dependencies]
async-trait = "0.1"
brotli = "3"
bytes = "1"
flate2 = "1.0"
brotli = "3"
hyper = { version = "0.14", features = ["full"] }
futures-util = "0.3.14"
hyper-util = { version = "0.1", features = ["client-legacy", "http1", "tokio"] }
once_cell = "1"
serde_json = "1.0"
sync_wrapper = "0.1.1"
tokio = { version = "1", features = ["full"] }
tower = { version = "0.4.10", features = ["buffer", "util", "retry", "make", "timeout"] }
tracing-subscriber = "0.3"
uuid = { version = "1.0", features = ["v4"] }
serde_json = "1.0"
zstd = "0.12"

[features]
Expand Down
11 changes: 6 additions & 5 deletions tower-http/src/add_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
//! use tower_http::add_extension::AddExtensionLayer;
//! use tower::{Service, ServiceExt, ServiceBuilder, service_fn};
//! use http::{Request, Response};
//! use hyper::Body;
//! use bytes::Bytes;
//! use http_body_util::Full;
//! use std::{sync::Arc, convert::Infallible};
//!
//! # struct DatabaseConnectionPool;
Expand All @@ -21,11 +22,11 @@
//! pool: DatabaseConnectionPool,
//! }
//!
//! async fn handle(req: Request<Body>) -> Result<Response<Body>, Infallible> {
//! async fn handle(req: Request<Full<Bytes>>) -> Result<Response<Full<Bytes>>, Infallible> {
//! // Grab the state from the request extensions.
//! let state = req.extensions().get::<Arc<State>>().unwrap();
//!
//! Ok(Response::new(Body::empty()))
//! Ok(Response::new(Full::default()))
//! }
//!
//! # #[tokio::main]
Expand All @@ -44,7 +45,7 @@
//! let response = service
//! .ready()
//! .await?
//! .call(Request::new(Body::empty()))
//! .call(Request::new(Full::default()))
//! .await?;
//! # Ok(())
//! # }
Expand Down Expand Up @@ -137,8 +138,8 @@ where
mod tests {
#[allow(unused_imports)]
use super::*;
use crate::test_helpers::Body;
use http::Response;
use hyper::Body;
use std::{convert::Infallible, sync::Arc};
use tower::{service_fn, ServiceBuilder, ServiceExt};

Expand Down
28 changes: 14 additions & 14 deletions tower-http/src/auth/add_authorization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@
//! ```
//! use tower_http::validate_request::{ValidateRequestHeader, ValidateRequestHeaderLayer};
//! use tower_http::auth::AddAuthorizationLayer;
//! use hyper::{Request, Response, Body, Error};
//! use http::{StatusCode, header::AUTHORIZATION};
//! use tower::{Service, ServiceExt, ServiceBuilder, service_fn};
//! # async fn handle(request: Request<Body>) -> Result<Response<Body>, Error> {
//! # Ok(Response::new(Body::empty()))
//! use http::{Request, Response, StatusCode, header::AUTHORIZATION};
//! use tower::{Service, ServiceExt, ServiceBuilder, service_fn, BoxError};
//! use http_body_util::Full;
//! use bytes::Bytes;
//! # async fn handle(request: Request<Full<Bytes>>) -> Result<Response<Full<Bytes>>, BoxError> {
//! # Ok(Response::new(Full::default()))
//! # }
//!
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! # async fn main() -> Result<(), BoxError> {
//! # let service_that_requires_auth = ValidateRequestHeader::basic(
//! # tower::service_fn(handle),
//! # "username",
Expand All @@ -30,7 +31,7 @@
//! let response = client
//! .ready()
//! .await?
//! .call(Request::new(Body::empty()))
//! .call(Request::new(Full::default()))
//! .await?;
//!
//! assert_eq!(StatusCode::OK, response.status());
Expand Down Expand Up @@ -84,7 +85,7 @@ impl AddAuthorizationLayer {
///
/// # Panics
///
/// Panics if the token is not a valid [`HeaderValue`](http::header::HeaderValue).
/// Panics if the token is not a valid [`HeaderValue`].
pub fn bearer(token: &str) -> Self {
let value =
HeaderValue::try_from(format!("Bearer {}", token)).expect("token is not valid header");
Expand Down Expand Up @@ -147,7 +148,7 @@ impl<S> AddAuthorization<S> {
///
/// # Panics
///
/// Panics if the token is not a valid [`HeaderValue`](http::header::HeaderValue).
/// Panics if the token is not a valid [`HeaderValue`].
pub fn bearer(inner: S, token: &str) -> Self {
AddAuthorizationLayer::bearer(token).layer(inner)
}
Expand Down Expand Up @@ -187,12 +188,11 @@ where

#[cfg(test)]
mod tests {
use crate::validate_request::ValidateRequestHeaderLayer;

#[allow(unused_imports)]
use super::*;
use crate::test_helpers::Body;
use crate::validate_request::ValidateRequestHeaderLayer;
use http::{Response, StatusCode};
use hyper::Body;
use std::convert::Infallible;
use tower::{BoxError, Service, ServiceBuilder, ServiceExt};

#[tokio::test]
Expand Down Expand Up @@ -245,7 +245,7 @@ mod tests {
let auth = request.headers().get(http::header::AUTHORIZATION).unwrap();
assert!(auth.is_sensitive());

Ok::<_, hyper::Error>(Response::new(Body::empty()))
Ok::<_, Infallible>(Response::new(Body::empty()))
});

let mut client = AddAuthorization::bearer(svc, "foo").as_sensitive(true);
Expand Down
38 changes: 20 additions & 18 deletions tower-http/src/auth/async_require_authorization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
//!
//! ```
//! use tower_http::auth::{AsyncRequireAuthorizationLayer, AsyncAuthorizeRequest};
//! use hyper::{Request, Response, Body, Error};
//! use http::{StatusCode, header::AUTHORIZATION};
//! use tower::{Service, ServiceExt, ServiceBuilder, service_fn};
//! use http::{Request, Response, StatusCode, header::AUTHORIZATION};
//! use tower::{Service, ServiceExt, ServiceBuilder, service_fn, BoxError};
//! use futures_util::future::BoxFuture;
//! use bytes::Bytes;
//! use http_body_util::Full;
//!
//! #[derive(Clone, Copy)]
//! struct MyAuth;
Expand All @@ -19,7 +20,7 @@
//! B: Send + Sync + 'static,
//! {
//! type RequestBody = B;
//! type ResponseBody = Body;
//! type ResponseBody = Full<Bytes>;
//! type Future = BoxFuture<'static, Result<Request<B>, Response<Self::ResponseBody>>>;
//!
//! fn authorize(&mut self, mut request: Request<B>) -> Self::Future {
Expand All @@ -33,7 +34,7 @@
//! } else {
//! let unauthorized_response = Response::builder()
//! .status(StatusCode::UNAUTHORIZED)
//! .body(Body::empty())
//! .body(Full::<Bytes>::default())
//! .unwrap();
//!
//! Err(unauthorized_response)
Expand All @@ -47,10 +48,10 @@
//! # None
//! }
//!
//! #[derive(Debug)]
//! #[derive(Debug, Clone)]
//! struct UserId(String);
//!
//! async fn handle(request: Request<Body>) -> Result<Response<Body>, Error> {
//! async fn handle(request: Request<Full<Bytes>>) -> Result<Response<Full<Bytes>>, BoxError> {
//! // Access the `UserId` that was set in `on_authorized`. If `handle` gets called the
//! // request was authorized and `UserId` will be present.
//! let user_id = request
Expand All @@ -60,11 +61,11 @@
//!
//! println!("request from {:?}", user_id);
//!
//! Ok(Response::new(Body::empty()))
//! Ok(Response::new(Full::default()))
//! }
//!
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! # async fn main() -> Result<(), BoxError> {
//! let service = ServiceBuilder::new()
//! // Authorize requests using `MyAuth`
//! .layer(AsyncRequireAuthorizationLayer::new(MyAuth))
Expand All @@ -77,10 +78,11 @@
//!
//! ```
//! use tower_http::auth::{AsyncRequireAuthorizationLayer, AsyncAuthorizeRequest};
//! use hyper::{Request, Response, Body, Error};
//! use http::StatusCode;
//! use tower::{Service, ServiceExt, ServiceBuilder};
//! use http::{Request, Response, StatusCode};
//! use tower::{Service, ServiceExt, ServiceBuilder, BoxError};
//! use futures_util::future::BoxFuture;
//! use http_body_util::Full;
//! use bytes::Bytes;
//!
//! async fn check_auth<B>(request: &Request<B>) -> Option<UserId> {
//! // ...
Expand All @@ -90,21 +92,21 @@
//! #[derive(Debug)]
//! struct UserId(String);
//!
//! async fn handle(request: Request<Body>) -> Result<Response<Body>, Error> {
//! async fn handle(request: Request<Full<Bytes>>) -> Result<Response<Full<Bytes>>, BoxError> {
//! # todo!();
//! // ...
//! }
//!
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! # async fn main() -> Result<(), BoxError> {
//! let service = ServiceBuilder::new()
//! .layer(AsyncRequireAuthorizationLayer::new(|request: Request<Body>| async move {
//! .layer(AsyncRequireAuthorizationLayer::new(|request: Request<Full<Bytes>>| async move {
//! if let Some(user_id) = check_auth(&request).await {
//! Ok(request)
//! } else {
//! let unauthorized_response = Response::builder()
//! .status(StatusCode::UNAUTHORIZED)
//! .body(Body::empty())
//! .body(Full::<Bytes>::default())
//! .unwrap();
//!
//! Err(unauthorized_response)
Expand Down Expand Up @@ -306,9 +308,9 @@ where
mod tests {
#[allow(unused_imports)]
use super::*;
use crate::test_helpers::Body;
use futures_util::future::BoxFuture;
use http::{header, StatusCode};
use hyper::Body;
use tower::{BoxError, ServiceBuilder, ServiceExt};

#[derive(Clone, Copy)]
Expand Down Expand Up @@ -346,7 +348,7 @@ mod tests {
}
}

#[derive(Debug)]
#[derive(Clone, Debug)]
struct UserId(String);

#[tokio::test]
Expand Down
Loading
Loading