Change usage of `FromRequest` to `FromRequestParts` where needed
In axum 0.6.* `FromRequest` always consumes the request's body. `FromRequestParts` was
also introduced for when you don't need to consume the body.
You can read about it more in axum's changelog.
@@ -9,9 +9,9 @@ use axum::{
body::{boxed, BoxBody, HttpBody},
extract::{
ws::{CloseFrame, Message},
- FromRequest, RequestParts, WebSocketUpgrade,
+ FromRequestParts, WebSocketUpgrade,
},
- http::{self, Request, Response, StatusCode},
+ http::{self, request::Parts, Request, Response, StatusCode},
response::IntoResponse,
Error,
};
@@ -30,11 +30,15 @@ use tower_service::Service;
pub struct GraphQLProtocol(WebSocketProtocols);
#[async_trait::async_trait]
-impl<B: Send> FromRequest<B> for GraphQLProtocol {
+impl<S> FromRequestParts<S> for GraphQLProtocol
+where
+ S: Send + Sync,
+{
type Rejection = StatusCode;
- async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
- req.headers()
+ async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
+ parts
+ .headers
.get(http::header::SEC_WEBSOCKET_PROTOCOL)
.and_then(|value| value.to_str().ok())
.and_then(|protocols| {
@@ -97,12 +101,13 @@ where
let schema = self.schema.clone();
Box::pin(async move {
- let mut parts = RequestParts::new(req);
- let protocol = match GraphQLProtocol::from_request(&mut parts).await {
+ let (mut parts, _body) = req.into_parts();
+
+ let protocol = match GraphQLProtocol::from_request_parts(&mut parts, &()).await {
Ok(protocol) => protocol,
Err(err) => return Ok(err.into_response().map(boxed)),
};
- let upgrade = match WebSocketUpgrade::from_request(&mut parts).await {
+ let upgrade = match WebSocketUpgrade::from_request_parts(&mut parts, &()).await {
Ok(protocol) => protocol,
Err(err) => return Ok(err.into_response().map(boxed)),
};
