-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'tokio-rs:main' into main
- Loading branch information
Showing
37 changed files
with
234 additions
and
99 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
use axum::response::IntoResponse; | ||
use http::{header, HeaderMap, HeaderValue}; | ||
use tracing::error; | ||
|
||
/// A file attachment response. | ||
/// | ||
/// This type will set the `Content-Disposition` header to `attachment`. In response a webbrowser | ||
/// will offer to download the file instead of displaying it directly. | ||
/// | ||
/// Use the `filename` and `content_type` methods to set the filename or content-type of the | ||
/// attachment. If these values are not set they will not be sent. | ||
/// | ||
/// | ||
/// # Example | ||
/// | ||
/// ```rust | ||
/// use axum::{http::StatusCode, routing::get, Router}; | ||
/// use axum_extra::response::Attachment; | ||
/// | ||
/// async fn cargo_toml() -> Result<Attachment<String>, (StatusCode, String)> { | ||
/// let file_contents = tokio::fs::read_to_string("Cargo.toml") | ||
/// .await | ||
/// .map_err(|err| (StatusCode::NOT_FOUND, format!("File not found: {err}")))?; | ||
/// Ok(Attachment::new(file_contents) | ||
/// .filename("Cargo.toml") | ||
/// .content_type("text/x-toml")) | ||
/// } | ||
/// | ||
/// let app = Router::new().route("/Cargo.toml", get(cargo_toml)); | ||
/// let _: Router = app; | ||
/// ``` | ||
/// | ||
/// # Note | ||
/// | ||
/// If you use axum with hyper, hyper will set the `Content-Length` if it is known. | ||
/// | ||
#[derive(Debug)] | ||
pub struct Attachment<T> { | ||
inner: T, | ||
filename: Option<HeaderValue>, | ||
content_type: Option<HeaderValue>, | ||
} | ||
|
||
impl<T: IntoResponse> Attachment<T> { | ||
/// Creates a new [`Attachment`]. | ||
pub fn new(inner: T) -> Self { | ||
Self { | ||
inner, | ||
filename: None, | ||
content_type: None, | ||
} | ||
} | ||
|
||
/// Sets the filename of the [`Attachment`]. | ||
/// | ||
/// This updates the `Content-Disposition` header to add a filename. | ||
pub fn filename<H: TryInto<HeaderValue>>(mut self, value: H) -> Self { | ||
self.filename = if let Ok(filename) = value.try_into() { | ||
Some(filename) | ||
} else { | ||
error!("Attachment filename contains invalid characters"); | ||
None | ||
}; | ||
self | ||
} | ||
|
||
/// Sets the content-type of the [`Attachment`] | ||
pub fn content_type<H: TryInto<HeaderValue>>(mut self, value: H) -> Self { | ||
if let Ok(content_type) = value.try_into() { | ||
self.content_type = Some(content_type); | ||
} else { | ||
error!("Attachment content-type contains invalid characters"); | ||
} | ||
self | ||
} | ||
} | ||
|
||
impl<T> IntoResponse for Attachment<T> | ||
where | ||
T: IntoResponse, | ||
{ | ||
fn into_response(self) -> axum::response::Response { | ||
let mut headers = HeaderMap::new(); | ||
|
||
if let Some(content_type) = self.content_type { | ||
headers.append(header::CONTENT_TYPE, content_type); | ||
} | ||
|
||
let content_disposition = if let Some(filename) = self.filename { | ||
let mut bytes = b"attachment; filename=\"".to_vec(); | ||
bytes.extend_from_slice(filename.as_bytes()); | ||
bytes.push(b'\"'); | ||
|
||
HeaderValue::from_bytes(&bytes).expect("This was a HeaderValue so this can not fail") | ||
} else { | ||
HeaderValue::from_static("attachment") | ||
}; | ||
|
||
headers.append(header::CONTENT_DISPOSITION, content_disposition); | ||
|
||
(headers, self.inner).into_response() | ||
} | ||
} |
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
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,9 @@ | ||
use axum::extract::Extension; | ||
use axum_macros::debug_handler; | ||
|
||
struct NonCloneType; | ||
|
||
#[debug_handler] | ||
async fn test_extension_non_clone(_: Extension<NonCloneType>) {} | ||
|
||
fn main() {} |
28 changes: 28 additions & 0 deletions
28
axum-macros/tests/debug_handler/fail/extension_not_clone.stderr
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,28 @@ | ||
error[E0277]: the trait bound `NonCloneType: Clone` is not satisfied | ||
--> tests/debug_handler/fail/extension_not_clone.rs:7:38 | ||
| | ||
7 | async fn test_extension_non_clone(_: Extension<NonCloneType>) {} | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ the trait `Clone` is not implemented for `NonCloneType`, which is required by `Extension<NonCloneType>: FromRequest<(), _>` | ||
| | ||
= help: the following other types implement trait `FromRequest<S, M>`: | ||
axum::body::Bytes | ||
Body | ||
Form<T> | ||
Json<T> | ||
axum::http::Request<Body> | ||
RawForm | ||
String | ||
Option<T> | ||
and $N others | ||
= note: required for `Extension<NonCloneType>` to implement `FromRequestParts<()>` | ||
= note: required for `Extension<NonCloneType>` to implement `FromRequest<(), axum_core::extract::private::ViaParts>` | ||
note: required by a bound in `__axum_macros_check_test_extension_non_clone_0_from_request_check` | ||
--> tests/debug_handler/fail/extension_not_clone.rs:7:38 | ||
| | ||
7 | async fn test_extension_non_clone(_: Extension<NonCloneType>) {} | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `__axum_macros_check_test_extension_non_clone_0_from_request_check` | ||
help: consider annotating `NonCloneType` with `#[derive(Clone)]` | ||
| | ||
4 + #[derive(Clone)] | ||
5 | struct NonCloneType; | ||
| |
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
Oops, something went wrong.