Skip to content

Commit

Permalink
Encode spaces in filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrubeck committed Dec 31, 2020
1 parent 9683146 commit 70b28a6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ getopts = "0.2.21"
log = "0.4"
mime_guess = "2.0"
once_cell = "1.4"
percent-encoding = "2.1"
rustls = "0.19.0"
url = "2.1"

Expand Down
13 changes: 11 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use async_std::{
};
use async_tls::TlsAcceptor;
use once_cell::sync::Lazy;
use percent_encoding::{AsciiSet, CONTROLS, percent_decode_str, percent_encode};
use rustls::{
internal::pemfile::{certs, pkcs8_private_keys},
NoClientAuth, ServerConfig,
Expand Down Expand Up @@ -181,7 +182,9 @@ async fn parse_request<R: Read + Unpin>(
async fn send_response<W: Write + Unpin>(url: Url, stream: &mut W) -> Result {
let mut path = std::path::PathBuf::from(&ARGS.content_dir);
if let Some(segments) = url.path_segments() {
path.extend(segments);
for segment in segments {
path.push(&*percent_decode_str(segment).decode_utf8()?);
}
}
if async_std::fs::metadata(&path).await?.is_dir() {
if url.path().ends_with('/') || url.path().is_empty() {
Expand Down Expand Up @@ -226,6 +229,7 @@ async fn send_header<W: Write + Unpin>(stream: &mut W, status: &str, meta: &[&st
}

async fn list_directory<W: Write + Unpin>(stream: &mut W, path: &Path) -> Result {
const WHITESPACE: AsciiSet = CONTROLS.add(b' ');
log::info!("Listing directory {:?}", path);
send_text_gemini_header(stream).await?;
let mut entries = async_std::fs::read_dir(path).await?;
Expand All @@ -239,7 +243,12 @@ async fn list_directory<W: Write + Unpin>(stream: &mut W, path: &Path) -> Result
if entry.file_type().await?.is_dir() {
name += "/";
}
lines.push(format!("=> {}\n", name));
if name.contains(char::is_whitespace) {
let url = percent_encode(name.as_bytes(), &WHITESPACE);
lines.push(format!("=> {} {}\n", url, name));
} else {
lines.push(format!("=> {}\n", name));
}
}
lines.sort();
for line in lines {
Expand Down

0 comments on commit 70b28a6

Please sign in to comment.