chore: improve code quanity

This commit is contained in:
sigoden 2022-12-11 15:18:44 +08:00
parent 8d9705caa4
commit 628d863d2e
8 changed files with 44 additions and 45 deletions

View file

@ -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;

View file

@ -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<HashMap<&[u8], &[u8]>, ()> {
}
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<HashMap<&[u8], &[u8]>, ()> {
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];

View file

@ -6,7 +6,7 @@ pub const DEFAULT_LOG_FORMAT: &str = r#"$remote_addr "$request" $status"#;
#[derive(Debug)]
pub struct LogHttp {
elems: Vec<LogElement>,
elements: Vec<LogElement>,
}
#[derive(Debug)]
@ -19,8 +19,8 @@ enum LogElement {
impl LogHttp {
pub fn data(&self, req: &Request, args: &Arc<Args>) -> HashMap<String, String> {
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<String, String>, err: Option<String>) {
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<dyn std::error::Error>;
fn from_str(s: &str) -> Result<Self, Self::Err> {
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 })
}
}

View file

@ -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)

View file

@ -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.

View file

@ -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("<D:href>/dira/</D:href>"));
assert!(body.contains("<D:href>/dir1/</D:href>"));
assert_eq!(body.contains("<D:href>/.git/</D:href>"), exist);
assert_eq!(body.contains("<D:href>/index.html</D:href>"), exist);
Ok(())

View file

@ -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(())

View file

@ -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("<D:href>/dira/</D:href>"));
assert!(body.contains("<D:displayname>dira</D:displayname>"));
assert!(body.contains("<D:href>/dir1/</D:href>"));
assert!(body.contains("<D:displayname>dir1</D:displayname>"));
for f in FILES {
assert!(body.contains(&format!("<D:href>/dira/{}</D:href>", utils::encode_uri(f))));
assert!(body.contains(&format!("<D:href>/dir1/{}</D:href>", utils::encode_uri(f))));
assert!(body.contains(&format!(
"<D:displayname>{}</D:displayname>",
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("<D:href>/dira/</D:href>"));
assert!(body.contains("<D:displayname>dira</D:displayname>"));
assert!(body.contains("<D:href>/dir1/</D:href>"));
assert!(body.contains("<D:displayname>dir1</D:displayname>"));
assert_eq!(
body.lines()
.filter(|v| *v == "<D:status>HTTP/1.1 200 OK</D:status>")
@ -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(())
}