From 628d863d2ed0af076ebfe313fcd1a03c9910aafc Mon Sep 17 00:00:00 2001 From: sigoden Date: Sun, 11 Dec 2022 15:18:44 +0800 Subject: [PATCH] chore: improve code quanity --- assets/index.css | 1 - src/auth.rs | 24 ++++++++++++------------ src/log_http.rs | 24 ++++++++++++------------ tests/auth.rs | 14 +++++++------- tests/fixtures.rs | 2 +- tests/hidden.rs | 6 +++--- tests/http.rs | 2 +- tests/webdav.rs | 16 ++++++++-------- 8 files changed, 44 insertions(+), 45 deletions(-) diff --git a/assets/index.css b/assets/index.css index c42ae00..61ba3a6 100644 --- a/assets/index.css +++ b/assets/index.css @@ -87,7 +87,6 @@ body { font-size: 16px; line-height: 16px; padding: 1px; - font-family: helvetica neue,luxi sans,Tahoma,hiragino sans gb,STHeiti,sans-serif; background-color: transparent; border: none; outline: none; diff --git a/src/auth.rs b/src/auth.rs index 533de32..e73b42b 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -213,8 +213,8 @@ impl AuthMethod { } AuthMethod::Digest => { let digest_value = strip_prefix(authorization.as_bytes(), b"Digest ")?; - let digest_vals = to_headermap(digest_value).ok()?; - digest_vals + let digest_map = to_headermap(digest_value).ok()?; + digest_map .get(b"username".as_ref()) .and_then(|b| std::str::from_utf8(b).ok()) .map(|v| v.to_string()) @@ -251,13 +251,13 @@ impl AuthMethod { } AuthMethod::Digest => { let digest_value = strip_prefix(authorization.as_bytes(), b"Digest ")?; - let digest_vals = to_headermap(digest_value).ok()?; + let digest_map = to_headermap(digest_value).ok()?; if let (Some(username), Some(nonce), Some(user_response)) = ( - digest_vals + digest_map .get(b"username".as_ref()) .and_then(|b| std::str::from_utf8(b).ok()), - digest_vals.get(b"nonce".as_ref()), - digest_vals.get(b"response".as_ref()), + digest_map.get(b"nonce".as_ref()), + digest_map.get(b"response".as_ref()), ) { match validate_nonce(nonce) { Ok(true) => {} @@ -269,12 +269,12 @@ impl AuthMethod { let mut ha = Context::new(); ha.consume(method); ha.consume(b":"); - if let Some(uri) = digest_vals.get(b"uri".as_ref()) { + if let Some(uri) = digest_map.get(b"uri".as_ref()) { ha.consume(uri); } let ha = format!("{:x}", ha.compute()); let mut correct_response = None; - if let Some(qop) = digest_vals.get(b"qop".as_ref()) { + if let Some(qop) = digest_map.get(b"qop".as_ref()) { if qop == &b"auth".as_ref() || qop == &b"auth-int".as_ref() { correct_response = Some({ let mut c = Context::new(); @@ -282,11 +282,11 @@ impl AuthMethod { c.consume(b":"); c.consume(nonce); c.consume(b":"); - if let Some(nc) = digest_vals.get(b"nc".as_ref()) { + if let Some(nc) = digest_map.get(b"nc".as_ref()) { c.consume(nc); } c.consume(b":"); - if let Some(cnonce) = digest_vals.get(b"cnonce".as_ref()) { + if let Some(cnonce) = digest_map.get(b"cnonce".as_ref()) { c.consume(cnonce); } c.consume(b":"); @@ -375,7 +375,7 @@ fn to_headermap(header: &[u8]) -> Result, ()> { } i += 1; } - sep.push(i); // same len for both Vecs + sep.push(i); i = 0; let mut ret = HashMap::new(); @@ -384,7 +384,7 @@ fn to_headermap(header: &[u8]) -> Result, ()> { i += 1; } if a <= i || k <= 1 + a { - //keys and vals must contain one char + //keys and values must contain one char return Err(()); } let key = &header[i..a]; diff --git a/src/log_http.rs b/src/log_http.rs index 15875fe..9f86fe4 100644 --- a/src/log_http.rs +++ b/src/log_http.rs @@ -6,7 +6,7 @@ pub const DEFAULT_LOG_FORMAT: &str = r#"$remote_addr "$request" $status"#; #[derive(Debug)] pub struct LogHttp { - elems: Vec, + elements: Vec, } #[derive(Debug)] @@ -19,8 +19,8 @@ enum LogElement { impl LogHttp { pub fn data(&self, req: &Request, args: &Arc) -> HashMap { let mut data = HashMap::default(); - for elem in self.elems.iter() { - match elem { + for element in self.elements.iter() { + match element { LogElement::Variable(name) => match name.as_str() { "request" => { data.insert(name.to_string(), format!("{} {}", req.method(), req.uri())); @@ -47,12 +47,12 @@ impl LogHttp { data } pub fn log(&self, data: &HashMap, err: Option) { - if self.elems.is_empty() { + if self.elements.is_empty() { return; } let mut output = String::new(); - for elem in self.elems.iter() { - match elem { + for element in self.elements.iter() { + match element { LogElement::Literal(value) => output.push_str(value.as_str()), LogElement::Header(name) | LogElement::Variable(name) => { output.push_str(data.get(name).map(|v| v.as_str()).unwrap_or("-")) @@ -69,21 +69,21 @@ impl LogHttp { impl FromStr for LogHttp { type Err = Box; fn from_str(s: &str) -> Result { - let mut elems = vec![]; + let mut elements = vec![]; let mut is_var = false; let mut cache = String::new(); for c in format!("{} ", s).chars() { if c == '$' { if !cache.is_empty() { - elems.push(LogElement::Literal(cache.to_string())); + elements.push(LogElement::Literal(cache.to_string())); } cache.clear(); is_var = true; } else if is_var && !(c.is_alphanumeric() || c == '_') { if let Some(value) = cache.strip_prefix("$http_") { - elems.push(LogElement::Header(value.replace('_', "-").to_string())); + elements.push(LogElement::Header(value.replace('_', "-").to_string())); } else if let Some(value) = cache.strip_prefix('$') { - elems.push(LogElement::Variable(value.to_string())); + elements.push(LogElement::Variable(value.to_string())); } cache.clear(); is_var = false; @@ -92,8 +92,8 @@ impl FromStr for LogHttp { } let cache = cache.trim(); if !cache.is_empty() { - elems.push(LogElement::Literal(cache.to_string())); + elements.push(LogElement::Literal(cache.to_string())); } - Ok(Self { elems }) + Ok(Self { elements }) } } diff --git a/tests/auth.rs b/tests/auth.rs index 83afb22..5aa2eef 100644 --- a/tests/auth.rs +++ b/tests/auth.rs @@ -54,10 +54,10 @@ fn auth_readonly( #[rstest] fn auth_nest( - #[with(&["--auth", "/@user:pass@user2:pass2", "--auth", "/dira@user3:pass3", "-A"])] + #[with(&["--auth", "/@user:pass@user2:pass2", "--auth", "/dir1@user3:pass3", "-A"])] server: TestServer, ) -> Result<(), Error> { - let url = format!("{}dira/file1", server.url()); + let url = format!("{}dir1/file1", server.url()); let resp = fetch!(b"PUT", &url).body(b"abc".to_vec()).send()?; assert_eq!(resp.status(), 401); let resp = fetch!(b"PUT", &url) @@ -73,7 +73,7 @@ fn auth_nest( #[rstest] fn auth_nest_share( - #[with(&["--auth", "/@user:pass@*", "--auth", "/dira@user3:pass3", "-A"])] server: TestServer, + #[with(&["--auth", "/@user:pass@*", "--auth", "/dir1@user3:pass3", "-A"])] server: TestServer, ) -> Result<(), Error> { let url = format!("{}index.html", server.url()); let resp = fetch!(b"GET", &url).send()?; @@ -98,9 +98,9 @@ fn auth_basic( #[rstest] fn auth_webdav_move( - #[with(&["--auth", "/@user:pass@*", "--auth", "/dira@user3:pass3", "-A"])] server: TestServer, + #[with(&["--auth", "/@user:pass@*", "--auth", "/dir1@user3:pass3", "-A"])] server: TestServer, ) -> Result<(), Error> { - let origin_url = format!("{}dira/test.html", server.url()); + let origin_url = format!("{}dir1/test.html", server.url()); let new_url = format!("{}test2.html", server.url()); let resp = fetch!(b"MOVE", &origin_url) .header("Destination", &new_url) @@ -111,9 +111,9 @@ fn auth_webdav_move( #[rstest] fn auth_webdav_copy( - #[with(&["--auth", "/@user:pass@*", "--auth", "/dira@user3:pass3", "-A"])] server: TestServer, + #[with(&["--auth", "/@user:pass@*", "--auth", "/dir1@user3:pass3", "-A"])] server: TestServer, ) -> Result<(), Error> { - let origin_url = format!("{}dira/test.html", server.url()); + let origin_url = format!("{}dir1/test.html", server.url()); let new_url = format!("{}test2.html", server.url()); let resp = fetch!(b"COPY", &origin_url) .header("Destination", &new_url) diff --git a/tests/fixtures.rs b/tests/fixtures.rs index c20c338..390ead9 100644 --- a/tests/fixtures.rs +++ b/tests/fixtures.rs @@ -33,7 +33,7 @@ pub static DIR_ASSETS: &str = "dir-assets/"; /// Directory names for testing purpose #[allow(dead_code)] -pub static DIRECTORIES: &[&str] = &["dira/", "dirb/", "dirc/", DIR_NO_INDEX, DIR_GIT, DIR_ASSETS]; +pub static DIRECTORIES: &[&str] = &["dir1/", "dir2/", "dir3/", DIR_NO_INDEX, DIR_GIT, DIR_ASSETS]; /// Test fixture which creates a temporary directory with a few files and directories inside. /// The directories also contain files. diff --git a/tests/hidden.rs b/tests/hidden.rs index 5b558c1..0dc6588 100644 --- a/tests/hidden.rs +++ b/tests/hidden.rs @@ -11,7 +11,7 @@ fn hidden_get_dir(#[case] server: TestServer, #[case] exist: bool) -> Result<(), let resp = reqwest::blocking::get(server.url())?; assert_eq!(resp.status(), 200); let paths = utils::retrieve_index_paths(&resp.text()?); - assert!(paths.contains("dira/")); + assert!(paths.contains("dir1/")); assert_eq!(paths.contains(".git/"), exist); assert_eq!(paths.contains("index.html"), exist); Ok(()) @@ -24,7 +24,7 @@ fn hidden_get_dir2(#[case] server: TestServer, #[case] exist: bool) -> Result<() let resp = reqwest::blocking::get(server.url())?; assert_eq!(resp.status(), 200); let paths = utils::retrieve_index_paths(&resp.text()?); - assert!(paths.contains("dira/")); + assert!(paths.contains("dir1/")); assert_eq!(paths.contains("index.html"), exist); assert_eq!(paths.contains("test.html"), exist); Ok(()) @@ -37,7 +37,7 @@ fn hidden_propfind_dir(#[case] server: TestServer, #[case] exist: bool) -> Resul let resp = fetch!(b"PROPFIND", server.url()).send()?; assert_eq!(resp.status(), 207); let body = resp.text()?; - assert!(body.contains("/dira/")); + assert!(body.contains("/dir1/")); assert_eq!(body.contains("/.git/"), exist); assert_eq!(body.contains("/index.html"), exist); Ok(()) diff --git a/tests/http.rs b/tests/http.rs index 8c22356..6ade44a 100644 --- a/tests/http.rs +++ b/tests/http.rs @@ -182,7 +182,7 @@ fn put_file_create_dir(#[with(&["-A"])] server: TestServer) -> Result<(), Error> #[rstest] fn put_file_conflict_dir(#[with(&["-A"])] server: TestServer) -> Result<(), Error> { - let url = format!("{}dira", server.url()); + let url = format!("{}dir1", server.url()); let resp = fetch!(b"PUT", &url).body(b"abc".to_vec()).send()?; assert_eq!(resp.status(), 403); Ok(()) diff --git a/tests/webdav.rs b/tests/webdav.rs index 36261fe..ed30591 100644 --- a/tests/webdav.rs +++ b/tests/webdav.rs @@ -7,13 +7,13 @@ use xml::escape::escape_str_pcdata; #[rstest] fn propfind_dir(server: TestServer) -> Result<(), Error> { - let resp = fetch!(b"PROPFIND", format!("{}dira", server.url())).send()?; + let resp = fetch!(b"PROPFIND", format!("{}dir1", server.url())).send()?; assert_eq!(resp.status(), 207); let body = resp.text()?; - assert!(body.contains("/dira/")); - assert!(body.contains("dira")); + assert!(body.contains("/dir1/")); + assert!(body.contains("dir1")); for f in FILES { - assert!(body.contains(&format!("/dira/{}", utils::encode_uri(f)))); + assert!(body.contains(&format!("/dir1/{}", utils::encode_uri(f)))); assert!(body.contains(&format!( "{}", escape_str_pcdata(f) @@ -24,13 +24,13 @@ fn propfind_dir(server: TestServer) -> Result<(), Error> { #[rstest] fn propfind_dir_depth0(server: TestServer) -> Result<(), Error> { - let resp = fetch!(b"PROPFIND", format!("{}dira", server.url())) + let resp = fetch!(b"PROPFIND", format!("{}dir1", server.url())) .header("depth", "0") .send()?; assert_eq!(resp.status(), 207); let body = resp.text()?; - assert!(body.contains("/dira/")); - assert!(body.contains("dira")); + assert!(body.contains("/dir1/")); + assert!(body.contains("dir1")); assert_eq!( body.lines() .filter(|v| *v == "HTTP/1.1 200 OK") @@ -102,7 +102,7 @@ fn mkcol_not_allow_upload(server: TestServer) -> Result<(), Error> { #[rstest] fn mkcol_already_exists(#[with(&["-A"])] server: TestServer) -> Result<(), Error> { - let resp = fetch!(b"MKCOL", format!("{}dira", server.url())).send()?; + let resp = fetch!(b"MKCOL", format!("{}dir1", server.url())).send()?; assert_eq!(resp.status(), 405); Ok(()) }