-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Alllow customizing HTTP headers for NamedFile
, StaticFiles
#95
Comments
@eliovir: In the meantime i found a 'dirty' and maybe expensive workarround to fulfill our need.
|
The code from @majkcramer is now obsolete, I've found this way to do it with current Rocket: struct CachedFile(NamedFile);
impl<'r> Responder<'r> for CachedFile {
fn respond_to(self, req: &Request) -> response::Result<'r> {
Response::build_from(self.0.respond_to(req)?)
.raw_header("Cache-control", "max-age=86400") // 24h (24*60*60)
.ok()
}
}
#[get("/resources/<file..>")]
fn files(file: PathBuf) -> Option<CachedFile> {
NamedFile::open(Path::new("resources/").join(file)).ok().map(|nf| CachedFile(nf))
} |
NamedFile
, StaticFiles
any progress on this, it has been nearly 5 years |
For anybody wanting to take a stab at this, I encourage doing so, but please note that several PR attempts haven't made it through for a variety of reasons. For example, from #1802:
From #1802 (comment):
A successful PR would:
I imagine something like the following: let headers = ETag | LastModified | Cache::MaxAge(3.days()) | Cache::MustRevalidate;
NamedFile::open("foo").await.headers(headers.clone());
FileServer::from("/foo").headers(headers); I'm not sure how to get this particular API (the |
Here's an alternative workaround solution which still makes use of the validations in #[derive(Debug, Clone)]
struct CachedFileServer {
root: PathBuf,
server: FileServer,
}
impl CachedFileServer {
/// The default rank use by `FileServer` routes.
const DEFAULT_RANK: isize = 10;
#[track_caller]
pub fn from<P: AsRef<Path>>(path: P) -> Self {
CachedFileServer { root: path.as_ref().into(), server: FileServer::from(path) }
}
}
impl From<CachedFileServer> for Vec<Route> {
fn from(server: CachedFileServer) -> Self {
let source = figment::Source::File(server.root.clone());
let mut route = Route::ranked(CachedFileServer::DEFAULT_RANK, Method::Get, "/<path..>", server);
route.name = Some(format!("CachedFileServer: {}", source).into());
vec![route]
}
}
#[async_trait::async_trait]
impl Handler for CachedFileServer {
async fn handle<'r>(&self, req: &'r Request<'_>, data: Data<'r>) -> Outcome<'r> {
match self.server.handle(req, data).await {
Outcome::Success(mut resp) => {
resp.set_raw_header("Cache-control", "max-age=86400"); // 24h (24 * 60 * 60)
Outcome::Success(resp)
},
i => i,
}
}
} |
I think this issue can now be closed, as of #2789. These (and any headers) can be added via the rewrite API introduced in that PR. |
This has been converted into a tracking issue for "Alllow customizing HTTP headers for NamedFile, StaticFiles". Original text after the fold.
This tracking issue is a merger of the following:
In short,
NamedFile
, and by implicationStaticFiles
, should support options to compute and set more response headers. In summary, these are:Last-Modified
If-Modified-Since
ETag
If-Match
Content-Disposition
Cache-Control
Content-Range
,If-Range
,Range
Content-Type
Hi,
happy new year with successfull Rust projects!
I'm building a little personal project with Rust and Rocket. I have to serve static pages (js, images) and using the Firefox Inspector (to see how fast is this little app) I saw that static files served with NamedFile are not cached by the browser... as the only HTTP headers are
Content-Type
,Date
,Server
andTransfert-Encoding
.I thing that at least the header
Last-Modified
should be added (maybe alsoExpires
andCache-Control
).I'm still new to Rust and do not know how to derive/override the standard
NamedFile
to add the header usingResponse.set_header()
. Looking at NamedFile source, it seems that adding new header could be done inside the original code.Thanks for your project.
The text was updated successfully, but these errors were encountered: