1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-30 18:44:35 +01:00

for NamedFile process etag and last modified only if status code is 200

This commit is contained in:
Nikolay Kim 2018-04-10 10:57:53 -07:00
parent 5e6a0aa3df
commit be288fa00a
3 changed files with 53 additions and 8 deletions

View File

@ -216,8 +216,8 @@ impl<S> App<S> where S: 'static {
/// let app = App::new() /// let app = App::new()
/// .prefix("/app") /// .prefix("/app")
/// .resource("/test", |r| { /// .resource("/test", |r| {
/// r.method(http::Method::GET).f(|_| HttpResponse::Ok()); /// r.get().f(|_| HttpResponse::Ok());
/// r.method(http::Method::HEAD).f(|_| HttpResponse::MethodNotAllowed()); /// r.head().f(|_| HttpResponse::MethodNotAllowed());
/// }) /// })
/// .finish(); /// .finish();
/// } /// }
@ -309,8 +309,8 @@ impl<S> App<S> where S: 'static {
/// fn main() { /// fn main() {
/// let app = App::new() /// let app = App::new()
/// .resource("/test", |r| { /// .resource("/test", |r| {
/// r.method(http::Method::GET).f(|_| HttpResponse::Ok()); /// r.get().f(|_| HttpResponse::Ok());
/// r.method(http::Method::HEAD).f(|_| HttpResponse::MethodNotAllowed()); /// r.head().f(|_| HttpResponse::MethodNotAllowed());
/// }); /// });
/// } /// }
/// ``` /// ```
@ -377,7 +377,7 @@ impl<S> App<S> where S: 'static {
/// ///
/// fn main() { /// fn main() {
/// let app = App::new() /// let app = App::new()
/// .resource("/index.html", |r| r.f(index)) /// .resource("/index.html", |r| r.get().f(index))
/// .external_resource("youtube", "https://youtube.com/watch/{video_id}") /// .external_resource("youtube", "https://youtube.com/watch/{video_id}")
/// .finish(); /// .finish();
/// } /// }
@ -449,14 +449,14 @@ impl<S> App<S> where S: 'static {
/// ///
/// ```rust /// ```rust
/// # extern crate actix_web; /// # extern crate actix_web;
/// use actix_web::{App, HttpResponse, http, fs, middleware}; /// use actix_web::{App, HttpResponse, fs, middleware};
/// ///
/// // this function could be located in different module /// // this function could be located in different module
/// fn config(app: App) -> App { /// fn config(app: App) -> App {
/// app /// app
/// .resource("/test", |r| { /// .resource("/test", |r| {
/// r.method(http::Method::GET).f(|_| HttpResponse::Ok()); /// r.get().f(|_| HttpResponse::Ok());
/// r.method(http::Method::HEAD).f(|_| HttpResponse::MethodNotAllowed()); /// r.head().f(|_| HttpResponse::MethodNotAllowed());
/// }) /// })
/// } /// }
/// ///

View File

@ -182,6 +182,21 @@ impl Responder for NamedFile {
type Error = io::Error; type Error = io::Error;
fn respond_to(self, req: HttpRequest) -> Result<HttpResponse, io::Error> { fn respond_to(self, req: HttpRequest) -> Result<HttpResponse, io::Error> {
if self.status_code != StatusCode::OK {
let mut resp = HttpResponse::build(self.status_code);
resp.if_some(self.path().extension(), |ext, resp| {
resp.set(header::ContentType(get_mime_type(&ext.to_string_lossy())));
});
let reader = ChunkedReadFile {
size: self.md.len(),
offset: 0,
cpu_pool: self.cpu_pool.unwrap_or_else(|| req.cpu_pool().clone()),
file: Some(self.file),
fut: None,
};
return Ok(resp.streaming(reader))
}
if self.only_get && *req.method() != Method::GET && *req.method() != Method::HEAD { if self.only_get && *req.method() != Method::GET && *req.method() != Method::HEAD {
return Ok(HttpResponse::MethodNotAllowed() return Ok(HttpResponse::MethodNotAllowed()
.header(header::CONTENT_TYPE, "text/plain") .header(header::CONTENT_TYPE, "text/plain")

View File

@ -92,6 +92,36 @@ impl<S: 'static> ResourceHandler<S> {
self.routes.last_mut().unwrap() self.routes.last_mut().unwrap()
} }
/// Register a new `GET` route.
pub fn get(&mut self) -> &mut Route<S> {
self.routes.push(Route::default());
self.routes.last_mut().unwrap().filter(pred::Get())
}
/// Register a new `POST` route.
pub fn post(&mut self) -> &mut Route<S> {
self.routes.push(Route::default());
self.routes.last_mut().unwrap().filter(pred::Post())
}
/// Register a new `PUT` route.
pub fn put(&mut self) -> &mut Route<S> {
self.routes.push(Route::default());
self.routes.last_mut().unwrap().filter(pred::Put())
}
/// Register a new `DELETE` route.
pub fn delete(&mut self) -> &mut Route<S> {
self.routes.push(Route::default());
self.routes.last_mut().unwrap().filter(pred::Delete())
}
/// Register a new `HEAD` route.
pub fn head(&mut self) -> &mut Route<S> {
self.routes.push(Route::default());
self.routes.last_mut().unwrap().filter(pred::Head())
}
/// Register a new route and add method check to route. /// Register a new route and add method check to route.
/// ///
/// This is shortcut for: /// This is shortcut for: