[hackers] [quark] Integrate compiled regex into vhost array || Quentin Rameau

From: <git_AT_suckless.org>
Date: Tue, 11 Jul 2017 13:46:38 +0200 (CEST)

commit e592bbc0fe71429104c1d7de8c35156185a9e13f
Author: Quentin Rameau <quinq_AT_fifth.space>
AuthorDate: Tue Jul 11 13:36:40 2017 +0200
Commit: Laslo Hunhold <dev_AT_frign.de>
CommitDate: Tue Jul 11 13:45:57 2017 +0200

    Integrate compiled regex into vhost array

diff --git a/config.def.h b/config.def.h
index 563fcb9..963821e 100644
--- a/config.def.h
+++ b/config.def.h
_AT_@ -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
_AT_@ -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 *
_AT_@ -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);
                         }
_AT_@ -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);
Received on Tue Jul 11 2017 - 13:46:38 CEST

This archive was generated by hypermail 2.3.0 : Tue Jul 11 2017 - 13:49:01 CEST