diff --git a/documentation/sitefiles.md b/documentation/sitefiles.md index be158cd..4bfccab 100644 --- a/documentation/sitefiles.md +++ b/documentation/sitefiles.md @@ -22,7 +22,7 @@ sitefiles also allow comments with # * ```respondto``` - The type of http request to respond to. One of: * GET (defualt) * POST -* ```host``` - The hostname to respond to. Case insensitive, default: localhost +* ```host``` - The hostname to respond to. Case insensitive regex, default: .* # Part 4: Global variables diff --git a/example/logs b/example/logs new file mode 100644 index 0000000..35b1ed2 --- /dev/null +++ b/example/logs @@ -0,0 +1,152 @@ +[2022-01-30T17:15:55Z] swebs started +[2022-01-30T17:15:58Z] Accepting a stream failed +[2022-01-30T17:15:58Z] Accepting a stream failed +[2022-01-30T17:15:58Z] Accepting a stream failed +[2022-01-30T17:15:58Z] Accepting a stream failed +[2022-01-30T17:15:58Z] Accepting a stream failed +[2022-01-30T17:15:58Z] Accepting a stream failed +[2022-01-30T17:15:58Z] Accepting a stream failed +[2022-01-30T17:15:58Z] Accepting a stream failed +[2022-01-30T17:15:58Z] Accepting a stream failed +[2022-01-30T17:15:58Z] Accepting a stream failed +[2022-01-30T17:15:59Z] Accepting a stream failed +[2022-01-30T17:15:59Z] Accepting a stream failed +[2022-01-30T17:15:59Z] Accepting a stream failed +[2022-01-30T17:15:59Z] Accepting a stream failed +[2022-01-30T17:15:59Z] Accepting a stream failed +[2022-01-30T17:15:59Z] Accepting a stream failed +[2022-01-30T17:15:59Z] Accepting a stream failed +[2022-01-30T17:15:59Z] Accepting a stream failed +[2022-01-30T17:15:59Z] Accepting a stream failed +[2022-01-30T17:15:59Z] Accepting a stream failed +[2022-01-30T17:15:59Z] Accepting a stream failed +[2022-01-30T17:15:59Z] Accepting a stream failed +[2022-01-30T17:15:59Z] Accepting a stream failed +[2022-01-30T17:15:59Z] Accepting a stream failed +[2022-01-30T17:15:59Z] Accepting a stream failed +[2022-01-30T17:15:59Z] Accepting a stream failed +[2022-01-30T17:15:59Z] Accepting a stream failed +[2022-01-30T17:15:59Z] Accepting a stream failed +[2022-01-30T17:15:59Z] Accepting a stream failed +[2022-01-30T17:15:59Z] Accepting a stream failed +[2022-01-30T17:15:59Z] Accepting a stream failed +[2022-01-30T17:15:59Z] Accepting a stream failed +[2022-01-30T17:15:59Z] Accepting a stream failed +[2022-01-30T17:15:59Z] Accepting a stream failed +[2022-01-30T17:15:59Z] Accepting a stream failed +[2022-01-30T17:15:59Z] Accepting a stream failed +[2022-01-30T17:15:59Z] Accepting a stream failed +[2022-01-30T17:15:59Z] Accepting a stream failed +[2022-01-30T17:15:59Z] Accepting a stream failed +[2022-01-30T17:15:59Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:00Z] Accepting a stream failed +[2022-01-30T17:16:01Z] Accepting a stream failed +[2022-01-30T17:16:01Z] Accepting a stream failed +[2022-01-30T17:16:01Z] Accepting a stream failed +[2022-01-30T17:16:01Z] Accepting a stream failed +[2022-01-30T17:16:01Z] Accepting a stream failed +[2022-01-30T17:16:01Z] Accepting a stream failed +[2022-01-30T17:16:01Z] Accepting a stream failed +[2022-01-30T17:16:01Z] Accepting a stream failed +[2022-01-30T17:16:01Z] Accepting a stream failed +[2022-01-30T17:16:01Z] Accepting a stream failed +[2022-01-30T17:16:01Z] Accepting a stream failed +[2022-01-30T17:16:01Z] Accepting a stream failed +[2022-01-30T17:16:01Z] Accepting a stream failed +[2022-01-30T17:16:01Z] Accepting a stream failed +[2022-01-30T17:16:01Z] Accepting a stream failed +[2022-01-30T17:16:01Z] Accepting a stream failed +[2022-01-30T17:16:01Z] Accepting a stream failed +[2022-01-30T17:16:01Z] Accepting a stream failed +[2022-01-30T17:16:01Z] Accepting a stream failed +[2022-01-30T17:16:01Z] Accepting a stream failed +[2022-01-30T17:16:04Z] Accepting a stream failed +[2022-01-30T17:16:16Z] Accepting a stream failed +[2022-01-30T17:22:52Z] swebs started +[2022-01-30T17:22:56Z] Accepting a stream failed +[2022-01-30T17:22:56Z] Accepting a stream failed +[2022-01-30T17:22:56Z] Accepting a stream failed +[2022-01-30T17:22:56Z] Accepting a stream failed +[2022-01-30T17:22:56Z] Accepting a stream failed +[2022-01-30T17:22:56Z] Accepting a stream failed +[2022-01-30T17:22:56Z] Accepting a stream failed +[2022-01-30T17:22:56Z] Accepting a stream failed +[2022-01-30T17:22:56Z] Accepting a stream failed +[2022-01-30T17:22:56Z] Accepting a stream failed +[2022-01-30T17:23:02Z] Accepting a stream failed +[2022-01-30T17:38:41Z] swebs started +[2022-01-30T17:38:46Z] Accepting a stream failed +[2022-01-30T17:38:46Z] Accepting a stream failed +[2022-01-30T17:38:46Z] Accepting a stream failed +[2022-01-30T17:38:46Z] Accepting a stream failed +[2022-01-30T17:38:46Z] Accepting a stream failed +[2022-01-30T17:38:46Z] Accepting a stream failed +[2022-01-30T17:38:46Z] Accepting a stream failed +[2022-01-30T17:38:46Z] Accepting a stream failed +[2022-01-30T17:38:46Z] Accepting a stream failed +[2022-01-30T17:38:46Z] Accepting a stream failed +[2022-01-30T17:38:48Z] Accepting a stream failed +[2022-01-30T17:39:19Z] swebs started +[2022-01-30T17:39:19Z] Accepting a stream failed +[2022-01-30T17:39:45Z] swebs started +[2022-01-30T17:42:25Z] swebs started +[2022-01-30T17:43:31Z] swebs started diff --git a/example/site/alldomains.html b/example/site/alldomains.html new file mode 100644 index 0000000..904e39a --- /dev/null +++ b/example/site/alldomains.html @@ -0,0 +1 @@ +

This page shows up on all hostnames

diff --git a/example/sitefile b/example/sitefile index 846e3a7..7f2233f 100644 --- a/example/sitefile +++ b/example/sitefile @@ -4,9 +4,11 @@ define cert domain.crt define timeout 2000 set host localhost:8000 -read ^/$ site/index.html -read ^/hello$ site/hello.html -read ^/blog/.*$ site/blog/ +read / site/index.html +read /hello site/hello.html +read /blog/.* site/blog/ #/blog/2021-1-25.html turns into site/blog//blog/2021-1-25.html set host 127.0.0.1:8000 -read ^/$ site/easteregg.html +read / site/easteregg.html +set host .* +read /alldomains site/alldomains.html diff --git a/src/include/sitefile.h b/src/include/sitefile.h index b93faf7..1eb1253 100644 --- a/src/include/sitefile.h +++ b/src/include/sitefile.h @@ -27,7 +27,7 @@ typedef enum { typedef struct { RequestType respondto; - char *host; + regex_t host; Command command; regex_t path; char *arg; diff --git a/src/responses.c b/src/responses.c index 16ccdb2..17d2c8d 100644 --- a/src/responses.c +++ b/src/responses.c @@ -95,6 +95,13 @@ forbidden: return; } +static int fullmatch(regex_t *regex, char *str) { + regmatch_t match; + if (regexec(regex, str, 1, &match, 0)) + return 1; + return match.rm_so != 0 || match.rm_eo != strlen(str); +} + int sendResponse(Connection *conn, Sitefile *site) { char *host = NULL; for (int i = 0; i < conn->fieldCount; i++) { @@ -110,10 +117,9 @@ int sendResponse(Connection *conn, Sitefile *site) { for (int i = 0; i < site->size; i++) { if (site->content[i].respondto != conn->type) continue; - if (istrcmp(site->content[i].host, host)) + if (fullmatch(&site->content[i].host, host)) continue; - if (regexec(&site->content[i].path, conn->path, 0, NULL, 0) - == 0) { + if (fullmatch(&site->content[i].path, conn->path) == 0) { switch (site->content[i].command) { case READ: readResponse(conn, site->content[i].arg); diff --git a/src/sitefile.c b/src/sitefile.c index 7710709..17f92a0 100644 --- a/src/sitefile.c +++ b/src/sitefile.c @@ -184,7 +184,9 @@ Sitefile *parseSitefile(char *path) { return NULL; } RequestType respondto = GET; - char *host = copyString("localhost"); + const int cflags = REG_EXTENDED | REG_ICASE; + char *host = NULL; + int lasthostset = 0; int argc; char **argv; for (;;) { @@ -207,20 +209,13 @@ Sitefile *parseSitefile(char *path) { goto error; } else if (strcmp(argv[1], "host") == 0) { - if (istrcmp(host, argv[2]) == 0) - goto setValue; - for (int i = 0; i < ret->size; i++) { - if (istrcmp(argv[2], - ret->content[i].host) == 0) { - host = ret->content[i].host; - goto setValue; - } - } + if (ret->size == lasthostset) + free(host); host = copyString(argv[2]); + lasthostset = ret->size; } else goto error; -setValue: continue; } else if (strcmp(argv[0], "define") == 0) { @@ -256,7 +251,7 @@ setValue: goto error; if (regcomp(&ret->content[ret->size].path, argv[1], - REG_EXTENDED | REG_NOSUB)) + cflags)) goto error; if (strcmp(argv[0], "read") == 0) { @@ -268,7 +263,10 @@ setValue: goto error; freeTokens(argc, argv); ret->content[ret->size].respondto = respondto; - ret->content[ret->size].host = host; + if (host == NULL) + regcomp(&ret->content[ret->size].host, ".*", cflags); + else + regcomp(&ret->content[ret->size].host, host, cflags); ret->size++; } error: @@ -281,6 +279,7 @@ nterror: void freeSitefile(Sitefile *site) { for (long i = 0; i < site->size; i++) { regfree(&site->content[i].path); + regfree(&site->content[i].host); free(site->content[i].arg); } free(site->content);