From 4a1b0c8ce24a818bef8f432511faddb5bcbe56cd Mon Sep 17 00:00:00 2001 From: prx Date: Wed, 6 Jan 2021 21:37:38 +0100 Subject: [PATCH] fix issue if missing ending '/' and add appropriate test --- main.c | 50 ++++++++++++------------------- tests/test.sh | 6 +++- tests/var/gemini/subdir/index.gmi | 1 + 3 files changed, 25 insertions(+), 32 deletions(-) create mode 100644 tests/var/gemini/subdir/index.gmi diff --git a/main.c b/main.c index 08b9fd7..ad392ba 100644 --- a/main.c +++ b/main.c @@ -158,24 +158,31 @@ display_file(const char *path, const char *lang) /* check if directory */ - if (S_ISDIR(sb.st_mode) == 1) - goto err; + if (S_ISDIR(sb.st_mode) != 0) { + /* look for index.gmi inside dir */ + char index_path[GEMINI_REQUEST_MAX] = {'\0'}; + estrlcpy(index_path, path, sizeof(index_path)); + estrlcat(index_path, "/index.gmi", sizeof(index_path)); + display_file(index_path, lang); + + } else { /* open the file requested */ - if ((fd = fopen(path, "r")) == NULL) - goto err; + if ((fd = fopen(path, "r")) == NULL) { goto err; } - file_mime = get_file_mime(path); + file_mime = get_file_mime(path); - status(20, file_mime, lang); + status(20, file_mime, lang); - /* read the file and write it to stdout */ - while ((nread = fread(buffer, sizeof(char), sizeof(buffer), fd)) != 0) - fwrite(buffer, sizeof(char), nread, stdout); - goto closefd; - syslog(LOG_DAEMON, "path served %s", path); + /* read the file and write it to stdout */ + while ((nread = fread(buffer, sizeof(char), sizeof(buffer), fd)) != 0) + fwrite(buffer, sizeof(char), nread, stdout); + goto closefd; + syslog(LOG_DAEMON, "path served %s", path); + } return; + err: /* return an error code and no content */ status(51, "text/gemini", lang); @@ -185,10 +192,6 @@ err: redirect: /* read symbolic link target to redirect */ if (readlink(path, target, FILENAME_MAX) == -1) { - int errnum = errno; - fprintf(stderr, "Value of errno: %d\n", errno); - perror("Error printed by perror"); - fprintf(stderr, "Error opening file: %s\n", strerror( errnum )); goto err; } @@ -196,7 +199,7 @@ redirect: syslog(LOG_DAEMON, "redirection from %s to %s", path, target); closefd: - if (S_ISREG(sb.st_mode) == 1) { + if (S_ISREG(sb.st_mode) != 0) { fclose(fd); } } @@ -284,21 +287,6 @@ main(int argc, char **argv) estrlcpy(file, pos, strlen(pos)+1); /* just keep hostname in request */ pos[0] = '\0'; - - /* - * use a default file if no file are requested this - * can happen in two cases gemini://hostname/ - * gemini://hostname/directory/ - */ - if (strlen(file) == 0) - estrlcpy(file, "/index.gmi", 11); - if (file[strlen(file) - 1] == '/') - estrlcat(file, "index.gmi", sizeof(file)); - } else { - /* - * there are no slash / in the request - */ - estrlcpy(file, "/index.gmi", 11); } /* check if client added :port at end of request */ pos = strchr(request, ':'); diff --git a/tests/test.sh b/tests/test.sh index 301ff8b..5e3f0dd 100644 --- a/tests/test.sh +++ b/tests/test.sh @@ -26,6 +26,10 @@ if ! [ $OUT = "3edd48286850d386592403956aec770f" ] ; then echo "error" ; exit 1 OUT=$(printf "gemini://host.name:1965\r\n" | ../vger -d var/gemini/ | tee /dev/stderr | $MD5) if ! [ $OUT = "3edd48286850d386592403956aec770f" ] ; then echo "error" ; exit 1 ; fi +# serving index.gmi automatically in a sub directory ending without "/" +OUT=$(printf "gemini://host.name/subdir\r\n" | ../vger -d var/gemini/ | tee /dev/stderr | $MD5) +if ! [ $OUT = "d11e0c0ff074f5627f2d2af72fd07104" ] ; then echo "error" ; exit 1 ; fi + # file from local directory with lang=fr and markdown MIME type OUT=$(printf "gemini://perso.pw/file.md\r\n" | ../vger -d var/gemini/ -l fr | tee /dev/stderr | $MD5) if ! [ $OUT = "e663f17730d5ddc24010c14a238e1e78" ] ; then echo "error" ; exit 1 ; fi @@ -89,7 +93,7 @@ then # file from /var/gemini/blog OUT=$(printf "gemini://host.name/blog\r\n" | ../vger | tee /dev/stderr | $MD5) - if ! [ $OUT = "ae3ce9fb5191a08a2c1f3e36b2985a01" ] ; then echo "error" ; exit 1 ; fi + if ! [ $OUT = "83bd01c9af0e44d5439b9ac95dc28132" ] ; then echo "error" ; exit 1 ; fi fi diff --git a/tests/var/gemini/subdir/index.gmi b/tests/var/gemini/subdir/index.gmi new file mode 100644 index 0000000..ce01362 --- /dev/null +++ b/tests/var/gemini/subdir/index.gmi @@ -0,0 +1 @@ +hello