Merge branch 'master' into timeheap
[ashd.git] / src / patplex.c
index 13fa062..9939543 100644 (file)
@@ -44,6 +44,7 @@
 #define PAT_DEFAULT 5
 
 #define PATFL_MSS 1
+#define PATFL_UNQ 2
 
 struct config {
     struct child *children;
@@ -188,6 +189,7 @@ static struct pattern *parsepattern(struct cfstate *s)
                flog(LOG_WARNING, "%s:%i: missing pattern for `%s' match", s->file, s->lno, s->argv[0]);
                continue;
            }
+           rxfl = 0;
            if(s->argc >= 3) {
                if(strchr(s->argv[2], 'i'))
                    rxfl |= REG_ICASE;
@@ -207,12 +209,15 @@ static struct pattern *parsepattern(struct cfstate *s)
            if(s->argc >= 3) {
                if(strchr(s->argv[2], 's'))
                    rule->fl |= PATFL_MSS;
+               if(strchr(s->argv[2], 'q'))
+                   rule->fl |= PATFL_UNQ;
            }
        } else if(!strcmp(s->argv[0], "header")) {
            if(s->argc < 3) {
                flog(LOG_WARNING, "%s:%i: missing header name or pattern for `header' match", s->file, s->lno);
                continue;
            }
+           rxfl = 0;
            if(s->argc >= 4) {
                if(strchr(s->argv[3], 'i'))
                    rxfl |= REG_ICASE;
@@ -360,12 +365,42 @@ static void exprestpat(struct hthead *req, struct pattern *pat, char **mstr)
     buffree(buf);
 }
 
+static void qoffsets(char *buf, int *obuf, char *pstr, int unquote)
+{
+    int i, o, d1, d2;
+    
+    if(unquote) {
+       i = o = 0;
+       while(pstr[i]) {
+           obuf[o] = i;
+           if((pstr[i] == '%') && ((d1 = hexdigit(pstr[i + 1])) >= 0) && ((d2 = hexdigit(pstr[i + 2])) >= 0)) {
+               buf[o] = (d1 << 4) | d2;
+               i += 3;
+           } else {
+               buf[o] = pstr[i];
+               i++;
+           }
+           o++;
+       }
+       buf[o] = 0;
+       obuf[o] = i;
+    } else {
+       for(i = 0; pstr[i]; i++) {
+           buf[i] = pstr[i];
+           obuf[i] = i;
+       }
+       buf[i] = 0;
+       obuf[i] = i;
+    }
+}
+
 static struct pattern *findmatch(struct config *cf, struct hthead *req, int trydefault)
 {
     int i, o;
     struct pattern *pat;
     struct rule *rule;
-    int rmo, matched;
+    int rmo;
+    regex_t *rx;
     char *pstr;
     char **mstr;
     regmatch_t gr[10];
@@ -374,46 +409,52 @@ static struct pattern *findmatch(struct config *cf, struct hthead *req, int tryd
     for(pat = cf->patterns; pat != NULL; pat = pat->next) {
        rmo = -1;
        for(i = 0; (rule = pat->rules[i]) != NULL; i++) {
-           matched = 0;
+           rx = NULL;
            if(rule->type == PAT_REST) {
-               if((matched = !regexec(rule->pattern, pstr = req->rest, 10, gr, 0)))
-                   rmo = gr[0].rm_eo;
-               else
-                   break;
+               rx = rule->pattern;
+               pstr = req->rest;
            } else if(rule->type == PAT_URL) {
-               if(!(matched = !regexec(rule->pattern, pstr = req->url, 10, gr, 0)))
-                   break;
+               rx = rule->pattern;
+               pstr = req->url;
            } else if(rule->type == PAT_METHOD) {
-               if(!(matched = !regexec(rule->pattern, pstr = req->method, 10, gr, 0)))
-                   break;
+               rx = rule->pattern;
+               pstr = req->method;
            } else if(rule->type == PAT_HEADER) {
+               rx = rule->pattern;
                if(!(pstr = getheader(req, rule->header)))
                    break;
-               if(!(matched = !regexec(rule->pattern, pstr, 10, gr, 0)))
+           }
+           if(rx != NULL) {
+               char pbuf[strlen(pstr) + 1];
+               int  obuf[strlen(pstr) + 1];
+               qoffsets(pbuf, obuf, pstr, !!(rule->fl & PATFL_UNQ));
+               if(regexec(rx, pbuf, 10, gr, 0))
                    break;
+               else if(rule->type == PAT_REST)
+                   rmo = obuf[gr[0].rm_eo];
+               if(rule->fl & PATFL_MSS) {
+                   if(mstr) {
+                       flog(LOG_WARNING, "two pattern rules marked with `s' flag found (for handler %s)", pat->childnm);
+                       freeca(mstr);
+                   }
+                   for(o = 0; o < 10; o++) {
+                       if(gr[o].rm_so < 0)
+                           break;
+                   }
+                   mstr = szmalloc((o + 1) * sizeof(*mstr));
+                   for(o = 0; o < 10; o++) {
+                       if(gr[o].rm_so < 0)
+                           break;
+                       mstr[o] = smalloc(obuf[gr[o].rm_eo] - obuf[gr[o].rm_so] + 1);
+                       memcpy(mstr[o], pstr + obuf[gr[o].rm_so], obuf[gr[o].rm_eo] - obuf[gr[o].rm_so]);
+                       mstr[o][obuf[gr[o].rm_eo] - obuf[gr[o].rm_so]] = 0;
+                   }
+               }
            } else if(rule->type == PAT_ALL) {
            } else if(rule->type == PAT_DEFAULT) {
                if(!trydefault)
                    break;
            }
-           if(matched && (rule->fl & PATFL_MSS)) {
-               if(mstr) {
-                   flog(LOG_WARNING, "two pattern rules marked with `s' flag found (for handler %s)", pat->childnm);
-                   freeca(mstr);
-               }
-               for(o = 0; o < 10; o++) {
-                   if(gr[o].rm_so < 0)
-                       break;
-               }
-               mstr = szmalloc((o + 1) * sizeof(*mstr));
-               for(o = 0; o < 10; o++) {
-                   if(gr[o].rm_so < 0)
-                       break;
-                   mstr[o] = smalloc(gr[o].rm_eo - gr[o].rm_so + 1);
-                   memcpy(mstr[o], pstr + gr[o].rm_so, gr[o].rm_eo - gr[o].rm_so);
-                   mstr[o][gr[o].rm_eo - gr[o].rm_so] = 0;
-               }
-           }
        }
        if(!rule) {
            if(pat->restpat) {
@@ -433,6 +474,14 @@ static struct pattern *findmatch(struct config *cf, struct hthead *req, int tryd
     return(NULL);
 }
 
+static void childerror(struct hthead *req, int fd)
+{
+    if(errno == EAGAIN)
+       simpleerror(fd, 500, "Server Error", "The request handler is overloaded.");
+    else
+       simpleerror(fd, 500, "Server Error", "The request handler crashed.");
+}
+
 static void serve(struct hthead *req, int fd)
 {
     struct pattern *pat;
@@ -472,7 +521,7 @@ static void serve(struct hthead *req, int fd)
        headappheader(req, head->name, head->value);
     }
     if(childhandle(ch, req, fd, NULL, NULL))
-       simpleerror(fd, 500, "Server Error", "The request handler crashed.");
+       childerror(req, fd);
 }
 
 static void reloadconf(char *nm)
@@ -514,7 +563,7 @@ int main(int argc, char **argv)
 {
     int c;
     int nodef;
-    char *gcf;
+    char *gcf, *lcf;
     struct hthead *req;
     int fd;
     
@@ -542,8 +591,14 @@ int main(int argc, char **argv)
            free(gcf);
        }
     }
-    if((lconfig = readconfig(argv[optind])) == NULL) {
-       flog(LOG_ERR, "could not read `%s'", argv[optind]);
+    if((strchr(lcf = argv[optind], '/')) == NULL) {
+       if((lcf = findstdconf(sprintf3("ashd/%s", lcf))) == NULL) {
+           flog(LOG_ERR, "could not find requested configuration file `%s'", argv[optind]);
+           exit(1);
+       }
+    }
+    if((lconfig = readconfig(lcf)) == NULL) {
+       flog(LOG_ERR, "could not read `%s'", lcf);
        exit(1);
     }
     signal(SIGCHLD, chldhandler);
@@ -551,7 +606,7 @@ int main(int argc, char **argv)
     signal(SIGPIPE, sighandler);
     while(1) {
        if(reload) {
-           reloadconf(argv[optind]);
+           reloadconf(lcf);
            reload = 0;
        }
        if((fd = recvreq(0, &req)) < 0) {