From 615079e26c34d2c8083ed431268b77b75b0cfab0 Mon Sep 17 00:00:00 2001 From: Solene Rapenne Date: Wed, 2 Dec 2020 15:31:21 +0100 Subject: [PATCH] Add virtualhost features with a flag + according tests --- README.md | 2 ++ main.c | 20 +++++++++++++++++--- tests/test.sh | 17 ++++++++++++++--- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index a8ed5eb..c365961 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/main.c b/main.c index 2842f35..2b0fb13 100644 --- a/main.c +++ b/main.c @@ -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)); diff --git a/tests/test.sh b/tests/test.sh index 619f7cd..dff99d7 100644 --- a/tests/test.sh +++ b/tests/test.sh @@ -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"