axum_extra/body/async_read_body.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
use axum::{
body::{Body, Bytes, HttpBody},
response::{IntoResponse, Response},
Error,
};
use pin_project_lite::pin_project;
use std::{
pin::Pin,
task::{Context, Poll},
};
use tokio::io::AsyncRead;
use tokio_util::io::ReaderStream;
pin_project! {
/// An [`HttpBody`] created from an [`AsyncRead`].
///
/// # Example
///
/// `AsyncReadBody` can be used to stream the contents of a file:
///
/// ```rust
/// use axum::{
/// Router,
/// routing::get,
/// http::{StatusCode, header::CONTENT_TYPE},
/// response::{Response, IntoResponse},
/// };
/// use axum_extra::body::AsyncReadBody;
/// use tokio::fs::File;
///
/// async fn cargo_toml() -> Result<Response, (StatusCode, String)> {
/// let file = File::open("Cargo.toml")
/// .await
/// .map_err(|err| {
/// (StatusCode::NOT_FOUND, format!("File not found: {err}"))
/// })?;
///
/// let headers = [(CONTENT_TYPE, "text/x-toml")];
/// let body = AsyncReadBody::new(file);
/// Ok((headers, body).into_response())
/// }
///
/// let app = Router::new().route("/Cargo.toml", get(cargo_toml));
/// # let _: Router = app;
/// ```
#[cfg(feature = "async-read-body")]
#[derive(Debug)]
#[must_use]
pub struct AsyncReadBody {
#[pin]
body: Body,
}
}
impl AsyncReadBody {
/// Create a new `AsyncReadBody`.
pub fn new<R>(read: R) -> Self
where
R: AsyncRead + Send + 'static,
{
Self {
body: Body::from_stream(ReaderStream::new(read)),
}
}
}
impl HttpBody for AsyncReadBody {
type Data = Bytes;
type Error = Error;
#[inline]
fn poll_frame(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Result<http_body::Frame<Self::Data>, Self::Error>>> {
self.project().body.poll_frame(cx)
}
#[inline]
fn is_end_stream(&self) -> bool {
self.body.is_end_stream()
}
#[inline]
fn size_hint(&self) -> http_body::SizeHint {
self.body.size_hint()
}
}
impl IntoResponse for AsyncReadBody {
fn into_response(self) -> Response {
self.body.into_response()
}
}