quark

quark web server
git clone git://git.suckless.org/quark
Log | Files | Refs | LICENSE

commit e592bbc0fe71429104c1d7de8c35156185a9e13f
parent 233bf68b4b9457bc4747c65038b80765a9eeb39e
Author: Quentin Rameau <quinq@fifth.space>
Date:   Tue, 11 Jul 2017 13:36:40 +0200

Integrate compiled regex into vhost array

Diffstat:
Mconfig.def.h | 9+++++----
Mquark.c | 17+++++------------
2 files changed, 10 insertions(+), 16 deletions(-)

diff --git 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 @@ -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);