Improved regexes

This commit is contained in:
Nate Choe
2022-01-30 11:45:48 -06:00
parent bdac013f91
commit 5a6c38c680
7 changed files with 182 additions and 22 deletions

View File

@@ -22,7 +22,7 @@ sitefiles also allow comments with #
* ```respondto``` - The type of http request to respond to. One of: * ```respondto``` - The type of http request to respond to. One of:
* GET (defualt) * GET (defualt)
* POST * 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 # Part 4: Global variables

152
example/logs Normal file
View File

@@ -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

View File

@@ -0,0 +1 @@
<h1>This page shows up on all hostnames</h1>

View File

@@ -4,9 +4,11 @@ define cert domain.crt
define timeout 2000 define timeout 2000
set host localhost:8000 set host localhost:8000
read ^/$ site/index.html read / site/index.html
read ^/hello$ site/hello.html read /hello site/hello.html
read ^/blog/.*$ site/blog/ read /blog/.* site/blog/
#/blog/2021-1-25.html turns into site/blog//blog/2021-1-25.html #/blog/2021-1-25.html turns into site/blog//blog/2021-1-25.html
set host 127.0.0.1:8000 set host 127.0.0.1:8000
read ^/$ site/easteregg.html read / site/easteregg.html
set host .*
read /alldomains site/alldomains.html

View File

@@ -27,7 +27,7 @@ typedef enum {
typedef struct { typedef struct {
RequestType respondto; RequestType respondto;
char *host; regex_t host;
Command command; Command command;
regex_t path; regex_t path;
char *arg; char *arg;

View File

@@ -95,6 +95,13 @@ forbidden:
return; 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) { int sendResponse(Connection *conn, Sitefile *site) {
char *host = NULL; char *host = NULL;
for (int i = 0; i < conn->fieldCount; i++) { 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++) { for (int i = 0; i < site->size; i++) {
if (site->content[i].respondto != conn->type) if (site->content[i].respondto != conn->type)
continue; continue;
if (istrcmp(site->content[i].host, host)) if (fullmatch(&site->content[i].host, host))
continue; continue;
if (regexec(&site->content[i].path, conn->path, 0, NULL, 0) if (fullmatch(&site->content[i].path, conn->path) == 0) {
== 0) {
switch (site->content[i].command) { switch (site->content[i].command) {
case READ: case READ:
readResponse(conn, site->content[i].arg); readResponse(conn, site->content[i].arg);

View File

@@ -184,7 +184,9 @@ Sitefile *parseSitefile(char *path) {
return NULL; return NULL;
} }
RequestType respondto = GET; RequestType respondto = GET;
char *host = copyString("localhost"); const int cflags = REG_EXTENDED | REG_ICASE;
char *host = NULL;
int lasthostset = 0;
int argc; int argc;
char **argv; char **argv;
for (;;) { for (;;) {
@@ -207,20 +209,13 @@ Sitefile *parseSitefile(char *path) {
goto error; goto error;
} }
else if (strcmp(argv[1], "host") == 0) { else if (strcmp(argv[1], "host") == 0) {
if (istrcmp(host, argv[2]) == 0) if (ret->size == lasthostset)
goto setValue; free(host);
for (int i = 0; i < ret->size; i++) {
if (istrcmp(argv[2],
ret->content[i].host) == 0) {
host = ret->content[i].host;
goto setValue;
}
}
host = copyString(argv[2]); host = copyString(argv[2]);
lasthostset = ret->size;
} }
else else
goto error; goto error;
setValue:
continue; continue;
} }
else if (strcmp(argv[0], "define") == 0) { else if (strcmp(argv[0], "define") == 0) {
@@ -256,7 +251,7 @@ setValue:
goto error; goto error;
if (regcomp(&ret->content[ret->size].path, argv[1], if (regcomp(&ret->content[ret->size].path, argv[1],
REG_EXTENDED | REG_NOSUB)) cflags))
goto error; goto error;
if (strcmp(argv[0], "read") == 0) { if (strcmp(argv[0], "read") == 0) {
@@ -268,7 +263,10 @@ setValue:
goto error; goto error;
freeTokens(argc, argv); freeTokens(argc, argv);
ret->content[ret->size].respondto = respondto; 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++; ret->size++;
} }
error: error:
@@ -281,6 +279,7 @@ nterror:
void freeSitefile(Sitefile *site) { void freeSitefile(Sitefile *site) {
for (long i = 0; i < site->size; i++) { for (long i = 0; i < site->size; i++) {
regfree(&site->content[i].path); regfree(&site->content[i].path);
regfree(&site->content[i].host);
free(site->content[i].arg); free(site->content[i].arg);
} }
free(site->content); free(site->content);