]> OzVa Git service - ozva-cloud/commitdiff
feat: add no-auth-read options
authorsigoden <sigoden@gmail.com>
Mon, 30 May 2022 04:40:57 +0000 (12:40 +0800)
committersigoden <sigoden@gmail.com>
Mon, 30 May 2022 04:40:57 +0000 (12:40 +0800)
README.md
src/args.rs
src/server.rs

index 47a16fe5869e6da9913492436872521cb1d30c8d..27aa55eabc84bab3754e55ccc6cc142ade17c4bb 100644 (file)
--- a/README.md
+++ b/README.md
@@ -45,7 +45,7 @@ duf folder_name
 
 Only serve static files, disable editing operations such as update or delete
 ```
-duf --no-change
+duf --readonly
 ```
 
 Finally, run this command to see a list of all available option
index f50f83b6056fcb3720150d39be770be9408395a3..97ab01e93b394dddf6dded01e4f1125c7762c854 100644 (file)
@@ -35,18 +35,23 @@ fn app() -> clap::Command<'static> {
                 .help("Path to a directory for serving files"),
         )
         .arg(
-            Arg::new("no-change")
-                .short('C')
-                .long("no-change")
+            Arg::new("readonly")
+                .short('r')
+                .long("readonly")
                 .help("Disable change operations such as update or delete"),
         )
         .arg(
             Arg::new("auth")
                 .short('a')
                 .long("auth")
-                .help("Authenticate with user and pass")
+                .help("Use HTTP authentication for all operations")
                 .value_name("user:pass"),
         )
+        .arg(
+            Arg::new("no-auth-read")
+                .long("no-auth-read")
+                .help("Do not authenticate read operations like static serving"),
+        )
         .arg(
             Arg::new("cors")
                 .long("cors")
@@ -65,6 +70,7 @@ pub struct Args {
     pub path: PathBuf,
     pub readonly: bool,
     pub auth: Option<String>,
+    pub no_auth_read: bool,
     pub cors: bool,
 }
 
@@ -78,9 +84,10 @@ impl Args {
         let port = matches.value_of_t::<u16>("port")?;
         let path = matches.value_of_os("path").unwrap_or_default();
         let path = Args::parse_path(path)?;
-        let readonly = matches.is_present("no-change");
+        let readonly = matches.is_present("readonly");
         let cors = matches.is_present("cors");
         let auth = matches.value_of("auth").map(|v| v.to_owned());
+        let no_auth_read = matches.is_present("no-auth-read");
 
         Ok(Args {
             address,
@@ -88,6 +95,7 @@ impl Args {
             path,
             readonly,
             auth,
+            no_auth_read,
             cors,
         })
     }
index fea10ab17716ca2b45f0136376c49edf2def180d..5f28ea138a1f4cbac4dc48cbdea920a0716b3b46 100644 (file)
@@ -319,6 +319,9 @@ impl InnerService {
                 let value = std::str::from_utf8(&value)?;
                 return Ok(value == auth);
             } else {
+                if self.args.no_auth_read && req.method() == Method::GET {
+                    return Ok(true);
+                }
                 return Ok(false);
             }
         }