From b8f8e62fba06ad9b373684be8305ddcd0746528d Mon Sep 17 00:00:00 2001 From: Douman Date: Mon, 25 Mar 2019 20:40:24 +0300 Subject: [PATCH] Applying comments --- actix-files/Cargo.toml | 1 + actix-files/src/lib.rs | 7 ++++--- actix-files/src/named.rs | 23 +++++++++++------------ 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/actix-files/Cargo.toml b/actix-files/Cargo.toml index aa93ac225..65faa5e8c 100644 --- a/actix-files/Cargo.toml +++ b/actix-files/Cargo.toml @@ -22,6 +22,7 @@ actix-web = { path=".." } actix-http = { git = "https://github.com/actix/actix-http.git" } actix-service = "0.3.3" +bitflags = "1" bytes = "0.4" futures = "0.1" derive_more = "0.14" diff --git a/actix-files/src/lib.rs b/actix-files/src/lib.rs index 221e94da0..a81f21532 100644 --- a/actix-files/src/lib.rs +++ b/actix-files/src/lib.rs @@ -211,6 +211,7 @@ fn directory_listing( type MimeOverride = Fn(&mime::Name) -> DispositionType; +#[derive(Clone)] /// Static files handling /// /// `Files` service must be registered with `App::service()` method. @@ -283,7 +284,7 @@ impl Files { } /// Specifies mime override callback - pub fn mime_override(mut self, f: F) -> Self where for<'r, 's> F: Fn(&mime::Name) -> DispositionType + 'static { + pub fn mime_override(mut self, f: F) -> Self where F: Fn(&mime::Name) -> DispositionType + 'static { self.mime_override = Some(Rc::new(f)); self } @@ -302,7 +303,7 @@ impl Files { /// ///Default is true. pub fn use_etag(mut self, value: bool) -> Self { - self.file_flags.use_etag = value; + self.file_flags.set(named::Flags::ETAG, value); self } @@ -311,7 +312,7 @@ impl Files { /// ///Default is true. pub fn use_last_modified(mut self, value: bool) -> Self { - self.file_flags.use_last_modified = value; + self.file_flags.set(named::Flags::LAST_MD, value); self } diff --git a/actix-files/src/named.rs b/actix-files/src/named.rs index cac847d74..d2bf25691 100644 --- a/actix-files/src/named.rs +++ b/actix-files/src/named.rs @@ -7,6 +7,7 @@ use std::time::{SystemTime, UNIX_EPOCH}; #[cfg(unix)] use std::os::unix::fs::MetadataExt; +use bitflags::bitflags; use mime; use mime_guess::guess_mime_type; @@ -17,18 +18,16 @@ use actix_web::{Error, HttpMessage, HttpRequest, HttpResponse, Responder}; use crate::range::HttpRange; use crate::ChunkedReadFile; -#[derive(Debug, Clone, Copy)] -pub(crate) struct Flags { - pub use_etag: bool, - pub use_last_modified: bool, +bitflags! { + pub(crate) struct Flags: u32 { + const ETAG = 0b00000001; + const LAST_MD = 0b00000010; + } } impl Default for Flags { fn default() -> Self { - Self { - use_etag: true, - use_last_modified: true, - } + Flags::all() } } @@ -188,7 +187,7 @@ impl NamedFile { /// ///Default is true. pub fn use_etag(mut self, value: bool) -> Self { - self.flags.use_etag = value; + self.flags.set(Flags::ETAG, value); self } @@ -197,7 +196,7 @@ impl NamedFile { /// ///Default is true. pub fn use_last_modified(mut self, value: bool) -> Self { - self.flags.use_last_modified = value; + self.flags.set(Flags::LAST_MD, value); self } @@ -318,8 +317,8 @@ impl Responder for NamedFile { } } - let etag = if self.flags.use_etag { self.etag() } else { None }; - let last_modified = if self.flags.use_last_modified { + let etag = if self.flags.contains(Flags::ETAG) { self.etag() } else { None }; + let last_modified = if self.flags.contains(Flags::LAST_MD) { self.last_modified() } else { None