Improved regexes
This commit is contained in:
@@ -27,7 +27,7 @@ typedef enum {
|
||||
|
||||
typedef struct {
|
||||
RequestType respondto;
|
||||
char *host;
|
||||
regex_t host;
|
||||
Command command;
|
||||
regex_t path;
|
||||
char *arg;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user