Add a flag to change the default language in the return status code

This commit is contained in:
Solene Rapenne 2020-12-02 18:07:10 +01:00
parent 7575537b6e
commit dd201d73d9
3 changed files with 46 additions and 25 deletions

View file

@ -39,6 +39,15 @@ Some files under `/var/gemini/` are required to test the code path
without a `-d` parameter.
# Command line parameters
**Vger** has a few parameters you can use in inetd configuration.
- `-d PATH`: use `PATH` to look for files. Default is `/var/gemini`
- `-l LANG`: change the language in the status return code. Default is `en`
- `-v`: enable virtualhost support, the hostname in the query will be considered as a directory name.
# How to configure Vger using relayd and inetd
Create directory `/var/gemini/` (I'd allow this to be configured

34
main.c
View file

@ -37,28 +37,24 @@
#define BUFF_LEN_2 1025
#define BUFF_LEN_3 1024
#define GEMINI_PART 9
#define DEFAULT_CHROOT "/var/gemini/"
#define DEFAULT_LANG "en"
#define DEFAULT_CHROOT "/var/gemini/"
void ok_status(void);
void err_status(void);
void display_file(char *);
void status(int, char *);
void display_file(char *, char *);
int main (int, char **);
void
ok_status(void)
status(int code, char *lang)
{
printf("20 text/gemini; lang=en\r\n");
printf("%i text/gemini; lang=%s\r\n",
code, lang);
}
void
err_status(void)
{
printf("40 text/gemini; lang=en\r\n");
}
void
display_file(char *path)
display_file(char *path, char *lang)
{
size_t buflen = BUFF_LEN_1;
char *buffer[BUFF_LEN_1];
@ -73,14 +69,14 @@ display_file(char *path)
if (fd != NULL && S_ISDIR(sb.st_mode) != 1) {
/* check if directory */
ok_status();
status(20, lang);
/* read the file and write it to stdout */
while ((nread = fread(buffer, sizeof(char), buflen, fd)) != 0)
fwrite(buffer, sizeof(char), nread, stdout);
fclose(fd);
} else {
err_status();
status(40, lang);
/*
* fprintf(stderr, "can't open %s %ld: %s\n", path,strlen(path),
* strerror(errno));
@ -96,13 +92,14 @@ main(int argc, char **argv)
char request [BUFF_LEN_2];
char hostname [BUFF_LEN_2];
char file [BUFF_LEN_2];
char path [BUFF_LEN_2] = "";
char path [BUFF_LEN_2] = "";
char lang [3] = DEFAULT_LANG;
int virtualhost = 0;
int option;
int start_with_gemini;
char *pos;
while ((option = getopt(argc, argv, ":d:v")) != -1) {
while ((option = getopt(argc, argv, ":d:l:v")) != -1) {
switch (option) {
case 'd':
strlcpy(path, optarg, sizeof(path));
@ -110,6 +107,9 @@ main(int argc, char **argv)
case 'v':
virtualhost = 1;
break;
case 'l':
strlcpy(lang, optarg, sizeof(lang));
break;
}
}
if (strlen(path) == 0)
@ -208,7 +208,7 @@ main(int argc, char **argv)
strlcat(path, file, sizeof(path));
/* open file and send it to stdout */
display_file(path);
display_file(path, lang);
return (0);
}

View file

@ -2,42 +2,54 @@
set -x
# md5 is BSD md5 binary
# Linux uses md5sum
MD5=md5
type md5 2>/dev/null
if [ $? -ne 0 ]; then
MD5=md5sum
fi
# serving a file
OUT=$(printf "gemini://host.name/main.gmi\r\n" | ../vger -d var/gemini/ | tee /dev/stderr | md5)
OUT=$(printf "gemini://host.name/main.gmi\r\n" | ../vger -d var/gemini/ | tee /dev/stderr | $MD5)
if ! [ $OUT = "d11e0c0ff074f5627f2d2af72fd07104" ] ; then echo "error" ; exit 1 ; fi
# default index.gmi file
OUT=$(printf "gemini://host.name\r\n" | ../vger -d var/gemini/ | tee /dev/stderr | md5)
OUT=$(printf "gemini://host.name\r\n" | ../vger -d var/gemini/ | tee /dev/stderr | $MD5)
if ! [ $OUT = "3edd48286850d386592403956aec770f" ] ; then echo "error" ; exit 1 ; fi
# default index.gmi file when using a trailing slash
OUT=$(printf "gemini://host.name/\r\n" | ../vger -d var/gemini/ | tee /dev/stderr | md5)
OUT=$(printf "gemini://host.name/\r\n" | ../vger -d var/gemini/ | tee /dev/stderr | $MD5)
if ! [ $OUT = "3edd48286850d386592403956aec770f" ] ; then echo "error" ; exit 1 ; fi
# file from local directory using virtualhosts
OUT=$(printf "gemini://perso.pw/index.gmi\r\n" | ../vger -v -d var/gemini/ | tee /dev/stderr | md5)
OUT=$(printf "gemini://perso.pw/index.gmi\r\n" | ../vger -v -d var/gemini/ | tee /dev/stderr | $MD5)
if ! [ $OUT = "0d36a423a4e8be813fda4022f08b3844" ] ; then echo "error" ; exit 1 ; fi
# file from local directory using virtualhosts without specifying a file
OUT=$(printf "gemini://perso.pw\r\n" | ../vger -v -d var/gemini/ | tee /dev/stderr | md5)
OUT=$(printf "gemini://perso.pw\r\n" | ../vger -v -d var/gemini/ | tee /dev/stderr | $MD5)
if ! [ $OUT = "0d36a423a4e8be813fda4022f08b3844" ] ; then echo "error" ; exit 1 ; fi
# file from local directory using virtualhosts without specifying a file using lang = fr
OUT=$(printf "gemini://perso.pw\r\n" | ../vger -v -d var/gemini/ -l fr | tee /dev/stderr | $MD5)
if ! [ $OUT = "7db981ce93fee268f29324912800f00d" ] ; then echo "error" ; exit 1 ; fi
#### no -d parameter from here
if [ -d /var/gemini/ ]
then
# file from /var/gemini/index.md
OUT=$(printf "gemini://host.name/index.md\r\n" | ../vger | tee /dev/stderr | md5)
OUT=$(printf "gemini://host.name/index.md\r\n" | ../vger | tee /dev/stderr | $MD5)
if ! [ $OUT = "bdbb22f0d1f4dd9e31bfc91686e7441d" ] ; then echo "error" ; exit 1 ; fi
# file from /var/gemini/blog/
OUT=$(printf "gemini://host.name/blog/\r\n" | ../vger | tee /dev/stderr | md5)
OUT=$(printf "gemini://host.name/blog/\r\n" | ../vger | tee /dev/stderr | $MD5)
if ! [ $OUT = "83bd01c9af0e44d5439b9ac95dc28132" ] ; then echo "error" ; exit 1 ; fi
# file from /var/gemini/blog
OUT=$(printf "gemini://host.name/blog\r\n" | ../vger | tee /dev/stderr | md5)
OUT=$(printf "gemini://host.name/blog\r\n" | ../vger | tee /dev/stderr | $MD5)
if ! [ $OUT = "f78c481e1614f1713e077b89aba5ab94" ] ; then echo "error" ; exit 1 ; fi
fi