alphasort autoindex + add proper error code fir cgi

This commit is contained in:
prx 2021-01-14 14:30:11 +01:00
parent 495fa3213f
commit 2cc63136f8
2 changed files with 23 additions and 20 deletions

39
main.c
View File

@ -201,31 +201,31 @@ closefd:
void
autoindex(const char *path)
{
struct dirent *dp;
DIR *fd;
int n = 0;
struct dirent **namelist;
if (!(fd = opendir(path))) {
err(1,"opendir '%s':", path);
}
syslog(LOG_DAEMON, "autoindex: %s", path);
status(20, "text/gemini");
/* TODO : add ending / in name if directory */
while ((dp = readdir(fd))) {
/* skip self */
if (!strcmp(dp->d_name, ".")) {
continue;
}
if (dp->d_type == DT_DIR) {
printf("=> ./%s/ %s/\n", dp->d_name, dp->d_name);
} else {
printf("=> ./%s %s\n", dp->d_name, dp->d_name);
if ((n = scandir(path, &namelist, NULL, alphasort)) < 0) {
status(51, "text/gemini");
errlog("Can't scan %s", path);
} else {
for(int j = 0; j < n; j++) {
if (!strcmp(namelist[j]->d_name, ".")) {
continue;
}
if (namelist[j]->d_type == DT_DIR) {
printf("=> ./%s/ %s/\n", namelist[j]->d_name, namelist[j]->d_name);
} else {
printf("=> ./%s %s\n", namelist[j]->d_name, namelist[j]->d_name);
}
free(namelist[j]);
}
free(namelist);
}
closedir(fd);
}
void
@ -236,6 +236,7 @@ cgi(const char *cgicmd)
pid_t pid;
if (pipe(pipedes) != 0) {
status(42, "text/gemini");
err(1, "pipe failed");
}
@ -244,6 +245,7 @@ cgi(const char *cgicmd)
if (pid < 0) {
close(pipedes[0]);
close(pipedes[1]);
status(42, "text/gemini");
err(1, "fork failed");
}
@ -257,6 +259,7 @@ cgi(const char *cgicmd)
/* use fread/fwrite because are buffered */
output = fdopen(pipedes[0], "r");
if (output == NULL) {
status(42, "text/gemini");
err(1, "fdopen failed");
}
@ -276,7 +279,7 @@ cgi(const char *cgicmd)
close(pipedes[1]); /* no need this file descriptor : it is now stdout */
execlp(cgicmd, cgicmd, NULL);
/* if execlp is ok, this will never be reached */
status(42, "text/plain");
status(42, "text/gemini");
errlog("error when trying to execlp %s", cgicmd);
}
}

View File

@ -72,7 +72,7 @@ if ! [ $OUT = "874f5e1af67eff6b93bedf8ac8033066" ] ; then echo "error" ; exit 1
# auto index in directory
OUT=$(printf "gemini://host.name/autoidx/\r\n" | ../vger -d var/gemini/ -i | tee /dev/stderr | $MD5)
if ! [ $OUT = "9cb7ef77cbcd74dadafdbff47d864152" ] ; then echo "error" ; exit 1 ; fi
if ! [ $OUT = "770a987b8f5cf7169e6bc3c6563e1570" ] ; then echo "error" ; exit 1 ; fi
# cgi simple script
OUT=$(printf "gemini://host.name/cgi-bin/test.cgi\r\n" | ../vger -d var/gemini/ -c /cgi-bin | tee /dev/stderr | $MD5)
@ -84,7 +84,7 @@ if ! [ $OUT = "fa065a67d1f7c973501d4a9e3ca2ea57" ] ; then echo "error" ; exit 1
# cgi with error
OUT=$(printf "gemini://host.name/cgi-bin/nope\r\n" | ../vger -d var/gemini/ -c /cgi-bin | tee /dev/stderr | $MD5)
if ! [ $OUT = "babaa82d5b2bd4e8d21ab412e060c890" ] ; then echo "error" ; exit 1 ; fi
if ! [ $OUT = "2c88347cfac44450035283a8508a29cb" ] ; then echo "error" ; exit 1 ; fi
# must fail only on OpenBSD !
# try to escape from unveil