Add virtualhost features with a flag + according tests

This commit is contained in:
Solene Rapenne 2020-12-02 15:31:21 +01:00
parent 24aa4ba284
commit 615079e26c
3 changed files with 33 additions and 6 deletions

View file

@ -1,5 +1,7 @@
# A simplistic and secure Gemini server
**Vger** is a gemini server supporting virtualhosts.
**Vger** design is relying on inetd and a daemon to take care of
TLS. The idea is to delegate TLS and network to daemons which
proved doing it correctly, so vger takes its request from stdin and

20
main.c
View file

@ -97,14 +97,19 @@ main(int argc, char **argv)
char hostname [BUFF_LEN_2];
char file [BUFF_LEN_2];
char path [BUFF_LEN_2] = "";
int virtualhost = 0;
int option;
int start_with_gemini;
char *pos;
while ((option = getopt(argc, argv, ":d:")) != -1) {
while ((option = getopt(argc, argv, ":d:v")) != -1) {
switch (option) {
case 'd':
strlcpy(path, optarg, sizeof(path));
break;
case 'v':
virtualhost = 1;
break;
}
}
if (strlen(path) == 0)
@ -133,7 +138,7 @@ main(int argc, char **argv)
* check if the beginning of the request starts with
* gemini://
*/
int start_with_gemini = strncmp(request, "gemini://", 9);
start_with_gemini = strncmp(request, "gemini://", 9);
/* the request must start with gemini:// */
if (start_with_gemini != 0) {
@ -164,7 +169,7 @@ main(int argc, char **argv)
/* separate hostname and uri */
if (position != -1) {
strlcpy(hostname, request, position);
strlcpy(hostname, request, position + 1);
strlcpy(file, request + position + 1, sizeof(request));
/*
@ -190,6 +195,15 @@ main(int argc, char **argv)
strlcpy(file, "/index.gmi", 11);
}
/*
* if virtualhost feature is actived looking under the default path +
* hostname directory gemini://foobar/hello will look for
* path/foobar/hello
*/
if (virtualhost) {
strlcat(path, hostname, sizeof(path));
strlcat(path, "/", sizeof(path));
}
/* add the base dir to the file requested */
strlcat(path, file, sizeof(path));

View file

@ -14,15 +14,24 @@ if ! [ $OUT = "3edd48286850d386592403956aec770f" ] ; then echo "error" ; exit 1
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 /var/gemini/index.md
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 local directory using virtualhosts
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)
if ! [ $OUT = "0d36a423a4e8be813fda4022f08b3844" ] ; 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)
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)
if ! [ $OUT = "83bd01c9af0e44d5439b9ac95dc28132" ] ; then echo "error" ; exit 1 ; fi
@ -32,3 +41,5 @@ then
if ! [ $OUT = "f78c481e1614f1713e077b89aba5ab94" ] ; then echo "error" ; exit 1 ; fi
fi
echo "SUCCESS"