~zethra/stargazer

f639b8c3f92c8c3c9a51b6c1bdf621325d938a05 — Ben Aaron Goldberg 24 days ago 508d7e6
Fixed #24 directory slash redirect

When a directory without a trailing slash is requested, stargazer should
redirect to that directory without a slash. The exception being root.

Signed-off-by: Ben Aaron Goldberg <ben@benaaron.dev>
M scripts/gemini-diagnostics => scripts/gemini-diagnostics +13 -0
@@ 879,6 879,19 @@ class DirectoryListing(BaseCheck):
            print("\nDiff:")
            print(show_diff(body, correct_body))

class RedirectDirectory(BaseCheck):
    """Directories without a trailing slash should be redirected excluding root"""

    def check(self):
        url = "gemini://localhost/dir\r\n"
        response = self.make_request(url)

        log("Status should return code 31")
        self.log_test(f"{response.status!r}", response.status == "31")

        log("Meta should be ..")
        self.log_test(f"{response.meta!r}", response.meta == "/dir/")

# noinspection PyTypeChecker
# fmt: off
parser = argparse.ArgumentParser(

M src/get_file.rs => src/get_file.rs +6 -6
@@ 48,12 48,18 @@ pub async fn get_file<'a>(
    let path = static_route.root.join(Path::new(&os_str));
    log::debug!("Requested file path: {}", path.display());

    let mut path_string = path_str.to_owned();
    let (path, path_type, mut mime_type) = unblock(move || {
        if !path.exists() {
            return Err(GemError::NotFound);
        }

        if path.is_dir() {
            // Redirect directories to have a trailing slash
            if !path_string.ends_with('/') {
                path_string.push('/');
                return Err(GemError::Redirect(path_string));
            }
            let index_path = path.join(&static_route.index);
            if index_path.exists() {
                let mime_type = get_mime_type(&index_path);


@@ 70,12 76,6 @@ pub async fn get_file<'a>(
    if path_type == PathType::Dir && !static_route.auto_index {
        return Err(GemError::NotFound);
    }
    // TODO show I be doing this?
    if path_type == PathType::Dir && !path_str.ends_with('/') {
        let mut new_path = path_str.to_owned();
        new_path.push('/');
        return Err(GemError::Redirect(new_path));
    }

    // Add lang or charset to mime_type
    match (lang, charset) {

A test_data/test_site/dir/index.gmi => test_data/test_site/dir/index.gmi +2 -0
@@ 0,0 1,2 @@
# Hello
=> test

A test_data/test_site/dir/test/index.gmi => test_data/test_site/dir/test/index.gmi +1 -0
@@ 0,0 1,1 @@
# It worked

M test_data/test_site/index.gmi => test_data/test_site/index.gmi +2 -1
@@ 1,3 1,4 @@
# Hello

=> ./list/ Directory Listing Test
=> ./list Directory Listing Test
=> dir A Directory