mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-29 00:00:18 +02:00
Remove B type param: Follow ups (#1789)
Co-authored-by: Jonas Platte <[email protected]> Co-authored-by: Michael Scofield <[email protected]>
This commit is contained in:
co-authored by
Jonas Platte
Michael Scofield
parent
72c1b7a80c
commit
6703f8634c
@@ -7,8 +7,8 @@
|
||||
use axum::{
|
||||
async_trait,
|
||||
body::{Body, Bytes},
|
||||
extract::FromRequest,
|
||||
http::{Request, StatusCode},
|
||||
extract::{FromRequest, Request},
|
||||
http::StatusCode,
|
||||
middleware::{self, Next},
|
||||
response::{IntoResponse, Response},
|
||||
routing::post,
|
||||
@@ -41,10 +41,7 @@ async fn main() {
|
||||
}
|
||||
|
||||
// middleware that shows how to consume the request body upfront
|
||||
async fn print_request_body(
|
||||
request: Request<Body>,
|
||||
next: Next<Body>,
|
||||
) -> Result<impl IntoResponse, Response> {
|
||||
async fn print_request_body(request: Request, next: Next) -> Result<impl IntoResponse, Response> {
|
||||
let request = buffer_request_body(request).await?;
|
||||
|
||||
Ok(next.run(request).await)
|
||||
@@ -52,7 +49,7 @@ async fn print_request_body(
|
||||
|
||||
// the trick is to take the request apart, buffer the body, do what you need to do, then put
|
||||
// the request back together
|
||||
async fn buffer_request_body(request: Request<Body>) -> Result<Request<Body>, Response> {
|
||||
async fn buffer_request_body(request: Request) -> Result<Request, Response> {
|
||||
let (parts, body) = request.into_parts();
|
||||
|
||||
// this wont work if the body is an long running stream
|
||||
@@ -84,7 +81,7 @@ where
|
||||
{
|
||||
type Rejection = Response;
|
||||
|
||||
async fn from_request(req: Request<Body>, state: &S) -> Result<Self, Self::Rejection> {
|
||||
async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection> {
|
||||
let body = Bytes::from_request(req, state)
|
||||
.await
|
||||
.map_err(|err| err.into_response())?;
|
||||
|
||||
@@ -6,9 +6,7 @@
|
||||
//! - Complexity: Manually implementing `FromRequest` results on more complex code
|
||||
use axum::{
|
||||
async_trait,
|
||||
body::Body,
|
||||
extract::{rejection::JsonRejection, FromRequest, MatchedPath},
|
||||
http::Request,
|
||||
extract::{rejection::JsonRejection, FromRequest, MatchedPath, Request},
|
||||
http::StatusCode,
|
||||
response::IntoResponse,
|
||||
RequestPartsExt,
|
||||
@@ -30,7 +28,7 @@ where
|
||||
{
|
||||
type Rejection = (StatusCode, axum::Json<Value>);
|
||||
|
||||
async fn from_request(req: Request<Body>, state: &S) -> Result<Self, Self::Rejection> {
|
||||
async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection> {
|
||||
let (mut parts, body) = req.into_parts();
|
||||
|
||||
// We can use other extractors to provide better rejection messages.
|
||||
|
||||
@@ -13,8 +13,9 @@
|
||||
//! Example is based on <https://github.com/hyperium/hyper/blob/master/examples/http_proxy.rs>
|
||||
|
||||
use axum::{
|
||||
body::{self, Body},
|
||||
http::{Method, Request, StatusCode},
|
||||
body::Body,
|
||||
extract::Request,
|
||||
http::{Method, StatusCode},
|
||||
response::{IntoResponse, Response},
|
||||
routing::get,
|
||||
Router,
|
||||
@@ -59,7 +60,7 @@ async fn main() {
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
async fn proxy(req: Request<Body>) -> Result<Response, hyper::Error> {
|
||||
async fn proxy(req: Request) -> Result<Response, hyper::Error> {
|
||||
tracing::trace!(?req);
|
||||
|
||||
if let Some(host_addr) = req.uri().authority().map(|auth| auth.to_string()) {
|
||||
@@ -74,7 +75,7 @@ async fn proxy(req: Request<Body>) -> Result<Response, hyper::Error> {
|
||||
}
|
||||
});
|
||||
|
||||
Ok(Response::new(body::boxed(body::Empty::new())))
|
||||
Ok(Response::new(Body::empty()))
|
||||
} else {
|
||||
tracing::warn!("CONNECT host is not socket addr: {:?}", req.uri());
|
||||
Ok((
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
//! cargo run -p example-low-level-rustls
|
||||
//! ```
|
||||
|
||||
use axum::{body::Body, extract::ConnectInfo, http::Request, routing::get, Router};
|
||||
use axum::{extract::ConnectInfo, extract::Request, routing::get, Router};
|
||||
use futures_util::future::poll_fn;
|
||||
use hyper::server::{
|
||||
accept::Accept,
|
||||
@@ -67,7 +67,7 @@ async fn main() {
|
||||
|
||||
let protocol = protocol.clone();
|
||||
|
||||
let svc = MakeService::<_, Request<Body>>::make_service(&mut app, &stream);
|
||||
let svc = MakeService::<_, Request<hyper::Body>>::make_service(&mut app, &stream);
|
||||
|
||||
tokio::spawn(async move {
|
||||
if let Ok(stream) = acceptor.accept(stream).await {
|
||||
|
||||
@@ -8,9 +8,8 @@
|
||||
|
||||
use axum::{
|
||||
async_trait,
|
||||
body::Body,
|
||||
extract::FromRequest,
|
||||
http::{header::CONTENT_TYPE, Request, StatusCode},
|
||||
extract::{FromRequest, Request},
|
||||
http::{header::CONTENT_TYPE, StatusCode},
|
||||
response::{IntoResponse, Response},
|
||||
routing::post,
|
||||
Form, Json, RequestExt, Router,
|
||||
@@ -61,7 +60,7 @@ where
|
||||
{
|
||||
type Rejection = Response;
|
||||
|
||||
async fn from_request(req: Request<Body>, _state: &S) -> Result<Self, Self::Rejection> {
|
||||
async fn from_request(req: Request, _state: &S) -> Result<Self, Self::Rejection> {
|
||||
let content_type_header = req.headers().get(CONTENT_TYPE);
|
||||
let content_type = content_type_header.and_then(|value| value.to_str().ok());
|
||||
|
||||
|
||||
@@ -6,7 +6,8 @@
|
||||
|
||||
use axum::{
|
||||
body::{Body, Bytes},
|
||||
http::{Request, StatusCode},
|
||||
extract::Request,
|
||||
http::StatusCode,
|
||||
middleware::{self, Next},
|
||||
response::{IntoResponse, Response},
|
||||
routing::post,
|
||||
@@ -38,8 +39,8 @@ async fn main() {
|
||||
}
|
||||
|
||||
async fn print_request_response(
|
||||
req: Request<Body>,
|
||||
next: Next<Body>,
|
||||
req: Request,
|
||||
next: Next,
|
||||
) -> Result<impl IntoResponse, (StatusCode, String)> {
|
||||
let (parts, body) = req.into_parts();
|
||||
let bytes = buffer_and_print("request", body).await?;
|
||||
|
||||
@@ -8,8 +8,7 @@
|
||||
//! ```
|
||||
|
||||
use axum::{
|
||||
extract::MatchedPath,
|
||||
http::Request,
|
||||
extract::{MatchedPath, Request},
|
||||
middleware::{self, Next},
|
||||
response::IntoResponse,
|
||||
routing::get,
|
||||
@@ -94,7 +93,7 @@ fn setup_metrics_recorder() -> PrometheusHandle {
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
async fn track_metrics<B>(req: Request<B>, next: Next<B>) -> impl IntoResponse {
|
||||
async fn track_metrics(req: Request, next: Next) -> impl IntoResponse {
|
||||
let start = Instant::now();
|
||||
let path = if let Some(matched_path) = req.extensions().get::<MatchedPath>() {
|
||||
matched_path.as_str().to_owned()
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
use axum::{body::BoxBody, http::header::CONTENT_TYPE, response::IntoResponse};
|
||||
use axum::{
|
||||
extract::Request,
|
||||
http::header::CONTENT_TYPE,
|
||||
response::{IntoResponse, Response},
|
||||
};
|
||||
use futures::{future::BoxFuture, ready};
|
||||
use hyper::{Body, Request, Response};
|
||||
use std::{
|
||||
convert::Infallible,
|
||||
task::{Context, Poll},
|
||||
@@ -41,16 +44,16 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<A, B> Service<Request<Body>> for MultiplexService<A, B>
|
||||
impl<A, B> Service<Request<hyper::Body>> for MultiplexService<A, B>
|
||||
where
|
||||
A: Service<Request<Body>, Error = Infallible>,
|
||||
A: Service<Request<hyper::Body>, Error = Infallible>,
|
||||
A::Response: IntoResponse,
|
||||
A::Future: Send + 'static,
|
||||
B: Service<Request<Body>>,
|
||||
B: Service<Request<hyper::Body>>,
|
||||
B::Response: IntoResponse,
|
||||
B::Future: Send + 'static,
|
||||
{
|
||||
type Response = Response<BoxBody>;
|
||||
type Response = Response;
|
||||
type Error = B::Error;
|
||||
type Future = BoxFuture<'static, Result<Self::Response, Self::Error>>;
|
||||
|
||||
@@ -73,7 +76,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
fn call(&mut self, req: Request<Body>) -> Self::Future {
|
||||
fn call(&mut self, req: Request<hyper::Body>) -> Self::Future {
|
||||
// require users to call `poll_ready` first, if they don't we're allowed to panic
|
||||
// as per the `tower::Service` contract
|
||||
assert!(
|
||||
|
||||
@@ -9,8 +9,8 @@
|
||||
|
||||
use axum::{
|
||||
body::Body,
|
||||
extract::State,
|
||||
http::{uri::Uri, Request},
|
||||
extract::{Request, State},
|
||||
http::uri::Uri,
|
||||
response::{IntoResponse, Response},
|
||||
routing::get,
|
||||
Router,
|
||||
@@ -36,7 +36,7 @@ async fn main() {
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
async fn handler(State(client): State<Client>, mut req: Request<Body>) -> Response {
|
||||
async fn handler(State(client): State<Client>, mut req: Request) -> Response {
|
||||
let path = req.uri().path();
|
||||
let path_query = req
|
||||
.uri()
|
||||
|
||||
@@ -5,11 +5,7 @@
|
||||
//! ```
|
||||
|
||||
use axum::{
|
||||
body::Body,
|
||||
handler::HandlerWithoutStateExt,
|
||||
http::{Request, StatusCode},
|
||||
routing::get,
|
||||
Router,
|
||||
extract::Request, handler::HandlerWithoutStateExt, http::StatusCode, routing::get, Router,
|
||||
};
|
||||
use std::net::SocketAddr;
|
||||
use tower::ServiceExt;
|
||||
@@ -97,7 +93,7 @@ fn calling_serve_dir_from_a_handler() -> Router {
|
||||
// call `ServeDir` yourself from a handler
|
||||
Router::new().nest_service(
|
||||
"/foo",
|
||||
get(|request: Request<Body>| async {
|
||||
get(|request: Request| async {
|
||||
let service = ServeDir::new("assets");
|
||||
let result = service.oneshot(request).await;
|
||||
result
|
||||
|
||||
@@ -5,9 +5,9 @@
|
||||
//! ```
|
||||
|
||||
use axum::{
|
||||
body::{Body, Bytes},
|
||||
extract::{Multipart, Path},
|
||||
http::{Request, StatusCode},
|
||||
body::Bytes,
|
||||
extract::{Multipart, Path, Request},
|
||||
http::StatusCode,
|
||||
response::{Html, Redirect},
|
||||
routing::{get, post},
|
||||
BoxError, Router,
|
||||
@@ -52,7 +52,7 @@ async fn main() {
|
||||
// POST'ing to `/file/foo.txt` will create a file called `foo.txt`.
|
||||
async fn save_request_body(
|
||||
Path(file_name): Path<String>,
|
||||
request: Request<Body>,
|
||||
request: Request,
|
||||
) -> Result<(), (StatusCode, String)> {
|
||||
stream_to_file(&file_name, request.into_body()).await
|
||||
}
|
||||
|
||||
@@ -162,7 +162,7 @@ mod tests {
|
||||
// in multiple request
|
||||
#[tokio::test]
|
||||
async fn multiple_request() {
|
||||
let mut app = app();
|
||||
let mut app = app().into_service();
|
||||
|
||||
let request = Request::builder().uri("/").body(Body::empty()).unwrap();
|
||||
let response = ServiceExt::<Request<Body>>::ready(&mut app)
|
||||
@@ -190,20 +190,15 @@ mod tests {
|
||||
// tests.
|
||||
#[tokio::test]
|
||||
async fn with_into_make_service_with_connect_info() {
|
||||
let mut app = app().layer(MockConnectInfo(SocketAddr::from(([0, 0, 0, 0], 3000))));
|
||||
let mut app = app()
|
||||
.layer(MockConnectInfo(SocketAddr::from(([0, 0, 0, 0], 3000))))
|
||||
.into_service();
|
||||
|
||||
let request = Request::builder()
|
||||
.uri("/requires-connect-into")
|
||||
.body(Body::empty())
|
||||
.unwrap();
|
||||
let response = app
|
||||
.as_service()
|
||||
.ready()
|
||||
.await
|
||||
.unwrap()
|
||||
.call(request)
|
||||
.await
|
||||
.unwrap();
|
||||
let response = app.ready().await.unwrap().call(request).await.unwrap();
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,9 +12,8 @@
|
||||
|
||||
use async_trait::async_trait;
|
||||
use axum::{
|
||||
body::Body,
|
||||
extract::{rejection::FormRejection, Form, FromRequest},
|
||||
http::{Request, StatusCode},
|
||||
extract::{rejection::FormRejection, Form, FromRequest, Request},
|
||||
http::StatusCode,
|
||||
response::{Html, IntoResponse, Response},
|
||||
routing::get,
|
||||
Router,
|
||||
@@ -70,7 +69,7 @@ where
|
||||
{
|
||||
type Rejection = ServerError;
|
||||
|
||||
async fn from_request(req: Request<Body>, state: &S) -> Result<Self, Self::Rejection> {
|
||||
async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection> {
|
||||
let Form(value) = Form::<T>::from_request(req, state).await?;
|
||||
value.validate()?;
|
||||
Ok(ValidatedForm(value))
|
||||
|
||||
Reference in New Issue
Block a user