From e592bbc0fe71429104c1d7de8c35156185a9e13f Mon Sep 17 00:00:00 2001 From: Quentin Rameau Date: Tue, 11 Jul 2017 13:36:40 +0200 Subject: [PATCH] Integrate compiled regex into vhost array --- config.def.h | 9 +++++---- quark.c | 17 +++++------------ 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/config.def.h b/config.def.h index 563fcb9..963821e 100644 --- a/config.def.h +++ b/config.def.h @@ -13,10 +13,11 @@ static const int maxnprocs = 512; #define HEADER_MAX 4096 #define FIELD_MAX 200 -static const struct { - char *name; - char *regex; - char *dir; +static struct { + const char *name; + const char *regex; + const char *dir; + regex_t re; } vhost[] = { { "example.org", "^(www.)example.org$", "/example.org" }, }; diff --git a/quark.c b/quark.c index 2b59f9d..6f49ecc 100644 --- a/quark.c +++ b/quark.c @@ -102,9 +102,6 @@ static char *status_str[] = { [S_VERSION_NOT_SUPPORTED] = "HTTP Version not supported", }; -/* vhost regex compilate */ -static regex_t vhost_regex[LEN(vhost)]; - long long strtonum(const char *, long long, long long, const char **); static char * @@ -566,14 +563,10 @@ sendresponse(int fd, struct request *r) /* match vhost */ if (vhosts) { for (i = 0; i < LEN(vhost); i++) { - if (!regexec(&vhost_regex[i], r->field[REQ_HOST], 0, - NULL, 0)) { - break; - } - } - if (i < LEN(vhost)) { - /* switch to vhost directory */ - if (chdir(vhost[i].dir) < 0) { + if (!regexec(&vhost[i].re, r->field[REQ_HOST], 0, + NULL, 0) && + /* switch to vhost directory */ + chdir(vhost[i].dir) < 0) { return sendstatus(fd, (errno == EACCES) ? S_FORBIDDEN : S_NOT_FOUND); } @@ -971,7 +964,7 @@ main(int argc, char *argv[]) /* compile and check the supplied vhost regexes */ if (vhosts) { for (i = 0; i < LEN(vhost); i++) { - if (regcomp(&vhost_regex[i], vhost[i].regex, + if (regcomp(&vhost[i].re, vhost[i].regex, REG_ICASE | REG_NOSUB)) { die("%s: regcomp '%s': invalid regex\n", argv0, vhost[i].regex);