|
5 | 5 |
|
6 | 6 | use std::{ |
7 | 7 | fmt::{self, Display, Formatter}, |
| 8 | + future::Future, |
8 | 9 | net::SocketAddr, |
9 | 10 | str::FromStr, |
10 | 11 | }; |
11 | 12 |
|
12 | | -use async_trait::async_trait; |
13 | 13 | use axum::{ |
| 14 | + body::Body, |
14 | 15 | extract::{ConnectInfo, FromRequest, FromRequestParts, Query}, |
15 | 16 | http::Request, |
16 | 17 | }; |
@@ -98,53 +99,63 @@ pub struct DnsRequestQuery(pub(crate) DNSRequest, pub(crate) DnsMimeType); |
98 | 99 | #[derive(Debug)] |
99 | 100 | pub struct DnsRequestBody(pub(crate) DNSRequest); |
100 | 101 |
|
101 | | -#[async_trait] |
102 | 102 | impl<S> FromRequestParts<S> for DnsRequestQuery |
103 | 103 | where |
104 | 104 | S: Send + Sync, |
105 | 105 | { |
106 | 106 | type Rejection = AppError; |
107 | 107 |
|
108 | | - async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> { |
109 | | - let ConnectInfo(src_addr) = ConnectInfo::from_request_parts(parts, state).await?; |
110 | | - |
111 | | - match parts.headers.get(header::ACCEPT) { |
112 | | - Some(content_type) if content_type == "application/dns-message" => { |
113 | | - handle_dns_message_query(parts, state, src_addr).await |
114 | | - } |
115 | | - Some(content_type) if content_type == "application/dns-json" => { |
116 | | - handle_dns_json_query(parts, state, src_addr).await |
| 108 | + #[allow(clippy::manual_async_fn)] |
| 109 | + fn from_request_parts( |
| 110 | + parts: &mut Parts, |
| 111 | + state: &S, |
| 112 | + ) -> impl Future<Output = Result<Self, Self::Rejection>> + Send { |
| 113 | + async move { |
| 114 | + let ConnectInfo(src_addr) = ConnectInfo::from_request_parts(parts, state).await?; |
| 115 | + |
| 116 | + match parts.headers.get(header::ACCEPT) { |
| 117 | + Some(content_type) if content_type == "application/dns-message" => { |
| 118 | + handle_dns_message_query(parts, state, src_addr).await |
| 119 | + } |
| 120 | + Some(content_type) if content_type == "application/dns-json" => { |
| 121 | + handle_dns_json_query(parts, state, src_addr).await |
| 122 | + } |
| 123 | + Some(content_type) if content_type == "application/x-javascript" => { |
| 124 | + handle_dns_json_query(parts, state, src_addr).await |
| 125 | + } |
| 126 | + None => handle_dns_message_query(parts, state, src_addr).await, |
| 127 | + _ => Err(AppError::with_status(StatusCode::NOT_ACCEPTABLE)), |
117 | 128 | } |
118 | | - Some(content_type) if content_type == "application/x-javascript" => { |
119 | | - handle_dns_json_query(parts, state, src_addr).await |
120 | | - } |
121 | | - None => handle_dns_message_query(parts, state, src_addr).await, |
122 | | - _ => Err(AppError::with_status(StatusCode::NOT_ACCEPTABLE)), |
123 | 129 | } |
124 | 130 | } |
125 | 131 | } |
126 | 132 |
|
127 | | -#[async_trait] |
128 | 133 | impl<S> FromRequest<S> for DnsRequestBody |
129 | 134 | where |
130 | 135 | S: Send + Sync, |
131 | 136 | { |
132 | 137 | type Rejection = AppError; |
133 | 138 |
|
134 | | - async fn from_request(req: axum::extract::Request, state: &S) -> Result<Self, Self::Rejection> { |
135 | | - let (mut parts, body) = req.into_parts(); |
| 139 | + #[allow(clippy::manual_async_fn)] |
| 140 | + fn from_request( |
| 141 | + req: Request<Body>, |
| 142 | + state: &S, |
| 143 | + ) -> impl Future<Output = Result<Self, Self::Rejection>> + Send { |
| 144 | + async move { |
| 145 | + let (mut parts, body) = req.into_parts(); |
136 | 146 |
|
137 | | - let ConnectInfo(src_addr) = ConnectInfo::from_request_parts(&mut parts, state).await?; |
| 147 | + let ConnectInfo(src_addr) = ConnectInfo::from_request_parts(&mut parts, state).await?; |
138 | 148 |
|
139 | | - let req = Request::from_parts(parts, body); |
| 149 | + let req = Request::from_parts(parts, body); |
140 | 150 |
|
141 | | - let body = Bytes::from_request(req, state) |
142 | | - .await |
143 | | - .map_err(|_| AppError::with_status(StatusCode::INTERNAL_SERVER_ERROR))?; |
| 151 | + let body = Bytes::from_request(req, state) |
| 152 | + .await |
| 153 | + .map_err(|_| AppError::with_status(StatusCode::INTERNAL_SERVER_ERROR))?; |
144 | 154 |
|
145 | | - let request = decode_request(&body, src_addr)?; |
| 155 | + let request = decode_request(&body, src_addr)?; |
146 | 156 |
|
147 | | - Ok(DnsRequestBody(request)) |
| 157 | + Ok(DnsRequestBody(request)) |
| 158 | + } |
148 | 159 | } |
149 | 160 | } |
150 | 161 |
|
|
0 commit comments