Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serve content with support for HTTP conditionals and range requests #687

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/utils.rs → src/utils/middleware.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Miscellaneous utilities.
//! Miscellaneous middleware utilities.

use crate::utils::async_trait;
use crate::{Middleware, Next, Request, Response};
pub use async_trait::async_trait;
use std::future::Future;

/// Define a middleware that operates on incoming requests.
Expand Down
8 changes: 8 additions & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//! Miscellaneous utilities.

mod middleware;
mod serve_content;

pub use async_trait::async_trait;
pub use middleware::{After, Before};
pub use serve_content::{serve_content, serve_content_with, ModState};
98 changes: 98 additions & 0 deletions src/utils/serve_content.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
//! Content serving utilities with support for conditional requests
//! as defined in [RFC 7232](https://tools.ietf.org/html/rfc7232)

use async_std::io::{BufRead as AsyncBufRead, Read as AsyncRead, Seek as AsyncSeek};

use crate::{Request, Response, Result, StatusCode};

/// A HTTP ressource modification state.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// A HTTP ressource modification state.
/// A HTTP resource modification state.

And other uses of the word

///
/// The modification state can be verified
/// against a `Request` with conditional headers to eventually serve a
/// `304 Not modified` or `206 Partial Content` response.
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum ModState {
/// The ressource has been modified.
Modified,
/// The ressource last modification date.
/// Used with `If-Modified-since`, `If-Unmodified-Since`
/// and `If-Range` conditional headers.
///
/// It is considered as a strong validator for `If-Range` requests.
Date(http_types::utils::HttpDate),
/// A ressource's version identifier.
/// Used with `If-None-Match`, `If-Match` and `If-Range` conditional headers.
Etag(http_types::conditional::ETag),
}

/// Serve content according to its modification state and the request's conditional headers:
/// If-Match, If-None-Match, If-Modified-Since, If-Unmodified-Since, Range, If-Range.
///
/// The few first bytes of the content is read to guess the MIME Type.
///
/// # Examples
///
/// Serve a static file returning `304 Not Modified` if the file's modification date
/// is earlier than the request's `If-Modified-Since` header.
///
/// ```
/// # #[allow(dead_code)]
/// async fn route_handler(request: tide::Request<()>) -> tide::Result {
/// use async_std::io::BufReader;
/// use async_std::fs::File;
/// use tide::utils::{serve_content, ModState};
///
/// let file = File::open("/foo/bar").await?;
/// let metadata = file.metadata().await?;
/// let mod_time = metadata.modified()?;
/// let content = BufReader::new(file);
///
/// serve_content(request, content, ModState::from(mod_time)).await
/// }
/// ```
pub async fn serve_content<S, T>(req: Request<S>, content: T, mod_state: ModState) -> Result
where
T: AsyncRead + AsyncBufRead + AsyncSeek + Send + Sync + Unpin + 'static,
{
let res = Response::new(StatusCode::Ok);
serve_content_with(req, res, content, mod_state).await
}

/// Similar than `serve_content` but allows to use a predefined `Response`.
///
/// `serve_content_with` only modifies the response content, status and headers
/// related to conditional requests. The MIME type is not guessed from the content data.
///
/// # Examples
///
/// Serve a static file returning `304 Not Modified` if the file's modification date
/// is earlier than the request's `If-Modified-Since` header, with HTML MIME type.
///
/// ```
/// # use tide::{Response, Redirect, Request, StatusCode};
/// # use async_std::io::BufReader;
/// # #[allow(dead_code)]
/// async fn route_handler(request: Request<()>) -> tide::Result {
/// use tide::utils::{serve_content, ModState};
///
/// let file = async_std::fs::File::open("/foo/bar").await?;
/// let metadata = file.metadata().await?;
/// let mod_time = metadata.modified()?;
/// let content = BufReader::new(file);
///
/// let response = tide::Response::builder(200).content_type(tide::mime::HTML);
///
/// serve_content_with(request, response, content, ModState::from(mod_time)).await
/// }
/// ```
pub async fn serve_content_with<S, T>(
req: Request<S>,
res: Response,
content: T,
mod_state: ModState,
) -> Result
where
T: AsyncRead + AsyncBufRead + AsyncSeek + Send + Sync + Unpin + 'static,
{
todo!();
}