fix issue if missing ending '/' and add appropriate test

This commit is contained in:
prx 2021-01-06 21:37:38 +01:00 committed by Solene Rapenne
parent ec3d847e1c
commit 4a1b0c8ce2
3 changed files with 25 additions and 32 deletions

50
main.c
View file

@ -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, ':');

View file

@ -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

View file

@ -0,0 +1 @@
hello