Url encoding and decoding
This commit is contained in:
18
src/main.rs
18
src/main.rs
@@ -24,6 +24,7 @@ mod conn;
|
|||||||
mod logger;
|
mod logger;
|
||||||
mod revproxy;
|
mod revproxy;
|
||||||
mod tls;
|
mod tls;
|
||||||
|
mod util;
|
||||||
|
|
||||||
fn get_mime(path: &PathBuf) -> String {
|
fn get_mime(path: &PathBuf) -> String {
|
||||||
let mut mime = "text/gemini".to_string();
|
let mut mime = "text/gemini".to_string();
|
||||||
@@ -86,10 +87,18 @@ async fn get_content(path: PathBuf, u: url::Url) -> Result<String, io::Error> {
|
|||||||
}
|
}
|
||||||
let file = file.path();
|
let file = file.path();
|
||||||
let p = file.strip_prefix(&path).unwrap();
|
let p = file.strip_prefix(&path).unwrap();
|
||||||
|
let ps = match p.to_str() {
|
||||||
|
Some(s) => s,
|
||||||
|
None => continue,
|
||||||
|
};
|
||||||
|
let ep = match u.join(ps) {
|
||||||
|
Ok(p) => p,
|
||||||
|
_ => continue,
|
||||||
|
};
|
||||||
if m.is_dir() {
|
if m.is_dir() {
|
||||||
list.push_str(&format!("=> {}/ {}/\r\n", p.display(), p.display()));
|
list.push_str(&format!("=> {}/ {}/\r\n", ep, p.display()));
|
||||||
} else {
|
} else {
|
||||||
list.push_str(&format!("=> {} {}\r\n", p.display(), p.display()));
|
list.push_str(&format!("=> {} {}\r\n", ep, p.display()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -215,14 +224,15 @@ async fn handle_connection(
|
|||||||
let usr: Vec<&str> = usr.splitn(2, "/").collect();
|
let usr: Vec<&str> = usr.splitn(2, "/").collect();
|
||||||
path.push("/home/");
|
path.push("/home/");
|
||||||
if usr.len() == 2 {
|
if usr.len() == 2 {
|
||||||
path.push(format!("{}/{}/{}", usr[0], "public_gemini", usr[1]));
|
path.push(format!("{}/{}/{}", usr[0], "public_gemini", util::url_decode(usr[1].as_bytes())));
|
||||||
} else {
|
} else {
|
||||||
path.push(format!("{}/{}/", usr[0], "public_gemini"));
|
path.push(format!("{}/{}/", usr[0], "public_gemini"));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
path.push(&srv.server.dir);
|
path.push(&srv.server.dir);
|
||||||
if url.path() != "" || url.path() != "/" {
|
if url.path() != "" || url.path() != "/" {
|
||||||
path.push(url.path().trim_start_matches("/"));
|
let decoded = util::url_decode(url.path().trim_start_matches("/").as_bytes());
|
||||||
|
path.push(decoded);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
8
src/util.rs
Normal file
8
src/util.rs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
use url::form_urlencoded;
|
||||||
|
|
||||||
|
pub fn url_decode(url: &[u8]) -> String {
|
||||||
|
let decoded: String = form_urlencoded::parse(url)
|
||||||
|
.map(|(key, val)| [key, val].concat())
|
||||||
|
.collect();
|
||||||
|
return decoded
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user