fix: escape path-prefix/url-prefix different

This commit is contained in:
sigoden 2022-06-06 08:00:26 +08:00
parent ce154d9ebc
commit b0cc901416
3 changed files with 11 additions and 11 deletions

View file

@ -143,7 +143,7 @@ impl Args {
let uri_prefix = if path_prefix.is_empty() { let uri_prefix = if path_prefix.is_empty() {
"/".to_owned() "/".to_owned()
} else { } else {
format!("/{}/", encode_uri(&path_prefix)) format!("/{}/", &path_prefix)
}; };
let cors = matches.is_present("cors"); let cors = matches.is_present("cors");
let auth = match matches.value_of("auth") { let auth = match matches.value_of("auth") {
@ -237,8 +237,3 @@ pub fn load_private_key(filename: &str) -> BoxResult<PrivateKey> {
} }
Ok(PrivateKey(keys[0].to_owned())) Ok(PrivateKey(keys[0].to_owned()))
} }
pub fn encode_uri(v: &str) -> String {
let parts: Vec<_> = v.split('/').map(urlencoding::encode).collect();
parts.join("/")
}

View file

@ -10,7 +10,7 @@ pub type BoxResult<T> = Result<T, Box<dyn std::error::Error>>;
use std::env; use std::env;
use std::io::Write; use std::io::Write;
use crate::args::{encode_uri, matches, Args}; use crate::args::{matches, Args};
use crate::server::serve; use crate::server::serve;
#[tokio::main] #[tokio::main]

View file

@ -1,5 +1,5 @@
use crate::auth::{generate_www_auth, valid_digest}; use crate::auth::{generate_www_auth, valid_digest};
use crate::{encode_uri, Args, BoxResult}; use crate::{Args, BoxResult};
use xml::escape::escape_str_pcdata; use xml::escape::escape_str_pcdata;
use async_walkdir::WalkDir; use async_walkdir::WalkDir;
@ -822,7 +822,7 @@ impl PathItem {
<D:status>HTTP/1.1 200 OK</D:status> <D:status>HTTP/1.1 200 OK</D:status>
</D:propstat> </D:propstat>
</D:response>"#, </D:response>"#,
prefix, escape_str_pcdata(prefix),
escape_str_pcdata(&self.name), escape_str_pcdata(&self.name),
escape_str_pcdata(&self.base_name), escape_str_pcdata(&self.base_name),
mtime mtime
@ -840,7 +840,7 @@ impl PathItem {
<D:status>HTTP/1.1 200 OK</D:status> <D:status>HTTP/1.1 200 OK</D:status>
</D:propstat> </D:propstat>
</D:response>"#, </D:response>"#,
prefix, escape_str_pcdata(prefix),
escape_str_pcdata(&self.name), escape_str_pcdata(&self.name),
escape_str_pcdata(&self.base_name), escape_str_pcdata(&self.base_name),
self.size.unwrap_or_default(), self.size.unwrap_or_default(),
@ -975,7 +975,7 @@ fn to_content_range(range: &Range, complete_length: u64) -> Option<ContentRange>
} }
fn print_listening(address: &str, port: u16, prefix: &str, tls: bool) { fn print_listening(address: &str, port: u16, prefix: &str, tls: bool) {
let prefix = prefix.trim_end_matches('/'); let prefix = encode_uri(prefix.trim_end_matches('/'));
let addrs = retrieve_listening_addrs(address); let addrs = retrieve_listening_addrs(address);
let protocol = if tls { "https" } else { "http" }; let protocol = if tls { "https" } else { "http" };
if addrs.len() == 1 { if addrs.len() == 1 {
@ -1006,3 +1006,8 @@ fn retrieve_listening_addrs(address: &str) -> Vec<String> {
} }
vec![address.to_owned()] vec![address.to_owned()]
} }
fn encode_uri(v: &str) -> String {
let parts: Vec<_> = v.split('/').map(urlencoding::encode).collect();
parts.join("/")
}