-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
78 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use super::{IntoResponse, IntoResponseParts, Response, ResponseParts, TryIntoHeaderError}; | ||
use http::header::{HeaderName, HeaderValue}; | ||
use std::{convert::TryInto, fmt}; | ||
|
||
/// Append headers to a response. | ||
/// | ||
/// Returning something like `[("content-type", "foo=bar")]` from a handler will override any | ||
/// existing `content-type` headers. If instead you want to append headers, use `AppendHeaders`: | ||
/// | ||
/// ```rust | ||
/// use axum::{ | ||
/// response::{AppendHeaders, IntoResponse}, | ||
/// http::header::SET_COOKIE, | ||
/// }; | ||
/// | ||
/// async fn handler() -> impl IntoResponse { | ||
/// // something that sets the `set-cookie` header | ||
/// let set_some_cookies = /* ... */ | ||
/// # axum::http::HeaderMap::new(); | ||
/// | ||
/// ( | ||
/// set_some_cookies, | ||
/// // append two `set-cookie` headers to the response | ||
/// // without overriding the ones added by `set_some_cookies` | ||
/// AppendHeaders([ | ||
/// (SET_COOKIE, "foo=bar"), | ||
/// (SET_COOKIE, "baz=qux"), | ||
/// ]) | ||
/// ) | ||
/// } | ||
/// ``` | ||
#[derive(Debug)] | ||
pub struct AppendHeaders<K, V, const N: usize>(pub [(K, V); N]); | ||
|
||
impl<K, V, const N: usize> IntoResponse for AppendHeaders<K, V, N> | ||
where | ||
K: TryInto<HeaderName>, | ||
K::Error: fmt::Display, | ||
V: TryInto<HeaderValue>, | ||
V::Error: fmt::Display, | ||
{ | ||
fn into_response(self) -> Response { | ||
(self, ()).into_response() | ||
} | ||
} | ||
|
||
impl<K, V, const N: usize> IntoResponseParts for AppendHeaders<K, V, N> | ||
where | ||
K: TryInto<HeaderName>, | ||
K::Error: fmt::Display, | ||
V: TryInto<HeaderValue>, | ||
V::Error: fmt::Display, | ||
{ | ||
type Error = TryIntoHeaderError<K::Error, V::Error>; | ||
|
||
fn into_response_parts(self, mut res: ResponseParts) -> Result<ResponseParts, Self::Error> { | ||
for (key, value) in self.0 { | ||
let key = key.try_into().map_err(TryIntoHeaderError::key)?; | ||
let value = value.try_into().map_err(TryIntoHeaderError::value)?; | ||
res.headers_mut().append(key, value); | ||
} | ||
|
||
Ok(res) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters