-
Notifications
You must be signed in to change notification settings - Fork 321
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
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,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}; |
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,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. | ||
/// | ||
/// 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!(); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And other uses of the word