mirror of
https://github.com/tokio-rs/axum.git
synced 2026-09-07 00:00:12 +02:00
Gracefully handle missing headers (#1810)
Co-authored-by: David Pedersen <[email protected]>
This commit is contained in:
co-authored by
David Pedersen
parent
5606ea3f9e
commit
fd96bce34d
@@ -9,11 +9,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
|
|
||||||
- **fixed:** Add `#[must_use]` attributes to types that do nothing unless used ([#1809])
|
- **fixed:** Add `#[must_use]` attributes to types that do nothing unless used ([#1809])
|
||||||
- **fixed:** Add `#[must_use]` to `WebSocketUpgrade::on_upgrade` ([#1801])
|
- **fixed:** Add `#[must_use]` to `WebSocketUpgrade::on_upgrade` ([#1801])
|
||||||
|
- **fixed:** Gracefully handle missing headers in the `TypedHeader` extractor ([#1810])
|
||||||
- **fixed:** Fix routing issues when loading a `Router` via a dynamic library ([#1806])
|
- **fixed:** Fix routing issues when loading a `Router` via a dynamic library ([#1806])
|
||||||
|
|
||||||
[#1801]: https://github.com/tokio-rs/axum/pull/1801
|
[#1801]: https://github.com/tokio-rs/axum/pull/1801
|
||||||
[#1806]: https://github.com/tokio-rs/axum/pull/1806
|
[#1806]: https://github.com/tokio-rs/axum/pull/1806
|
||||||
[#1809]: https://github.com/tokio-rs/axum/pull/1809
|
[#1809]: https://github.com/tokio-rs/axum/pull/1809
|
||||||
|
[#1810]: https://github.com/tokio-rs/axum/pull/1810
|
||||||
|
|
||||||
# 0.6.9 (24. February, 2023)
|
# 0.6.9 (24. February, 2023)
|
||||||
|
|
||||||
|
|||||||
+31
-13
@@ -62,17 +62,19 @@ where
|
|||||||
type Rejection = TypedHeaderRejection;
|
type Rejection = TypedHeaderRejection;
|
||||||
|
|
||||||
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
|
async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
|
||||||
match parts.headers.typed_try_get::<T>() {
|
let mut values = parts.headers.get_all(T::name()).iter();
|
||||||
Ok(Some(value)) => Ok(Self(value)),
|
let is_missing = values.size_hint() == (0, Some(0));
|
||||||
Ok(None) => Err(TypedHeaderRejection {
|
T::decode(&mut values)
|
||||||
|
.map(Self)
|
||||||
|
.map_err(|err| TypedHeaderRejection {
|
||||||
name: T::name(),
|
name: T::name(),
|
||||||
reason: TypedHeaderRejectionReason::Missing,
|
reason: if is_missing {
|
||||||
}),
|
// Report a more precise rejection for the missing header case.
|
||||||
Err(err) => Err(TypedHeaderRejection {
|
TypedHeaderRejectionReason::Missing
|
||||||
name: T::name(),
|
} else {
|
||||||
reason: TypedHeaderRejectionReason::Error(err),
|
TypedHeaderRejectionReason::Error(err)
|
||||||
}),
|
},
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -175,19 +177,35 @@ mod tests {
|
|||||||
async fn typed_header() {
|
async fn typed_header() {
|
||||||
async fn handle(
|
async fn handle(
|
||||||
TypedHeader(user_agent): TypedHeader<headers::UserAgent>,
|
TypedHeader(user_agent): TypedHeader<headers::UserAgent>,
|
||||||
|
TypedHeader(cookies): TypedHeader<headers::Cookie>,
|
||||||
) -> impl IntoResponse {
|
) -> impl IntoResponse {
|
||||||
user_agent.to_string()
|
let user_agent = user_agent.as_str();
|
||||||
|
let cookies = cookies.iter().collect::<Vec<_>>();
|
||||||
|
format!("User-Agent={user_agent:?}, Cookie={cookies:?}")
|
||||||
}
|
}
|
||||||
|
|
||||||
let app = Router::new().route("/", get(handle));
|
let app = Router::new().route("/", get(handle));
|
||||||
|
|
||||||
let client = TestClient::new(app);
|
let client = TestClient::new(app);
|
||||||
|
|
||||||
|
let res = client
|
||||||
|
.get("/")
|
||||||
|
.header("user-agent", "foobar")
|
||||||
|
.header("cookie", "a=1; b=2")
|
||||||
|
.header("cookie", "c=3")
|
||||||
|
.send()
|
||||||
|
.await;
|
||||||
|
let body = res.text().await;
|
||||||
|
assert_eq!(
|
||||||
|
body,
|
||||||
|
r#"User-Agent="foobar", Cookie=[("a", "1"), ("b", "2"), ("c", "3")]"#
|
||||||
|
);
|
||||||
|
|
||||||
let res = client.get("/").header("user-agent", "foobar").send().await;
|
let res = client.get("/").header("user-agent", "foobar").send().await;
|
||||||
let body = res.text().await;
|
let body = res.text().await;
|
||||||
assert_eq!(body, "foobar");
|
assert_eq!(body, r#"User-Agent="foobar", Cookie=[]"#);
|
||||||
|
|
||||||
let res = client.get("/").send().await;
|
let res = client.get("/").header("cookie", "a=1").send().await;
|
||||||
let body = res.text().await;
|
let body = res.text().await;
|
||||||
assert_eq!(body, "Header of type `user-agent` was missing");
|
assert_eq!(body, "Header of type `user-agent` was missing");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user