patplex: Add reparse action.
[ashd.git] / src / patplex.c
index 23fcf4e..e893bc0 100644 (file)
@@ -24,6 +24,7 @@
 #include <errno.h>
 #include <ctype.h>
 #include <regex.h>
+#include <limits.h>
 #include <sys/wait.h>
 
 #ifdef HAVE_CONFIG_H
 #define PAT_METHOD 2
 #define PAT_HEADER 3
 #define PAT_ALL 4
-#define PAT_DEFAULT 5
 
 #define PATFL_MSS 1
 #define PATFL_UNQ 2
 
+#define HND_CHILD 1
+#define HND_REPARSE 2
+
 struct config {
     struct child *children;
     struct pattern *patterns;
@@ -69,6 +72,7 @@ struct pattern {
     char *childnm;
     struct rule **rules;
     char *restpat;
+    int handler, prio, disable;
 };
 
 static struct config *gconfig, *lconfig;
@@ -189,6 +193,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;
@@ -216,6 +221,7 @@ static struct pattern *parsepattern(struct cfstate *s)
                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;
@@ -235,7 +241,14 @@ static struct pattern *parsepattern(struct cfstate *s)
        } else if(!strcmp(s->argv[0], "all")) {
            newrule(pat)->type = PAT_ALL;
        } else if(!strcmp(s->argv[0], "default")) {
-           newrule(pat)->type = PAT_DEFAULT;
+           newrule(pat)->type = PAT_ALL;
+           pat->prio = -10;
+       } else if(!strcmp(s->argv[0], "order") || !strcmp(s->argv[0], "priority")) {
+           if(s->argc < 2) {
+               flog(LOG_WARNING, "%s:%i: missing specification for `%s' directive", s->file, s->lno, s->argv[0]);
+               continue;
+           }
+           pat->prio = atoi(s->argv[1]);
        } else if(!strcmp(s->argv[0], "handler")) {
            if(s->argc < 2) {
                flog(LOG_WARNING, "%s:%i: missing child name for `handler' directive", s->file, s->lno);
@@ -244,6 +257,9 @@ static struct pattern *parsepattern(struct cfstate *s)
            if(pat->childnm != NULL)
                free(pat->childnm);
            pat->childnm = sstrdup(s->argv[1]);
+           pat->handler = HND_CHILD;
+       } else if(!strcmp(s->argv[0], "reparse")) {
+           pat->handler = HND_REPARSE;
        } else if(!strcmp(s->argv[0], "restpat")) {
            if(s->argc < 2) {
                flog(LOG_WARNING, "%s:%i: missing pattern for `restpat' directive", s->file, s->lno);
@@ -277,7 +293,7 @@ static struct pattern *parsepattern(struct cfstate *s)
        freepattern(pat);
        return(NULL);
     }
-    if(pat->childnm == NULL) {
+    if(pat->handler == 0) {
        flog(LOG_WARNING, "%s:%i: missing handler in match declaration", s->file, sl);
        freepattern(pat);
        return(NULL);
@@ -381,16 +397,30 @@ static void qoffsets(char *buf, int *obuf, char *pstr, int unquote)
            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)
+struct match {
+    struct pattern *pat;
+    char **mstr;
+    int rmo;
+};
+
+static void freematch(struct match *match)
+{
+    freeca(match->mstr);
+    free(match);
+}
+
+static struct match *findmatch(struct config *cf, struct hthead *req, struct match *match)
 {
     int i, o;
     struct pattern *pat;
@@ -403,6 +433,8 @@ static struct pattern *findmatch(struct config *cf, struct hthead *req, int tryd
     
     mstr = NULL;
     for(pat = cf->patterns; pat != NULL; pat = pat->next) {
+       if(pat->disable || (match && (pat->prio <= match->pat->prio)))
+           continue;
        rmo = -1;
        for(i = 0; (rule = pat->rules[i]) != NULL; i++) {
            rx = NULL;
@@ -447,69 +479,80 @@ static struct pattern *findmatch(struct config *cf, struct hthead *req, int tryd
                    }
                }
            } else if(rule->type == PAT_ALL) {
-           } else if(rule->type == PAT_DEFAULT) {
-               if(!trydefault)
-                   break;
            }
        }
        if(!rule) {
-           if(pat->restpat) {
-               exprestpat(req, pat, mstr);
-           } else if(rmo != -1) {
-               replrest(req, req->rest + rmo);
-           }
-           if(mstr)
-               freeca(mstr);
-           return(pat);
-       }
-       if(mstr) {
-           freeca(mstr);
-           mstr = NULL;
+           if(match)
+               freematch(match);
+           omalloc(match);
+           match->pat = pat;
+           match->mstr = mstr;
+           match->rmo = rmo;
        }
     }
-    return(NULL);
+    return(match);
 }
 
-static void serve(struct hthead *req, int fd)
+static void execmatch(struct hthead *req, struct match *match)
 {
-    struct pattern *pat;
     struct headmod *head;
-    struct child *ch;
     
-    pat = NULL;
-    if(pat == NULL)
-       pat = findmatch(lconfig, req, 0);
-    if(pat == NULL)
-       pat = findmatch(lconfig, req, 1);
-    if(gconfig != NULL) {
-       if(pat == NULL)
-           pat = findmatch(gconfig, req, 0);
-       if(pat == NULL)
-           pat = findmatch(gconfig, req, 1);
+    if(match->pat->restpat)
+       exprestpat(req, match->pat, match->mstr);
+    else if(match->rmo != -1)
+       replrest(req, req->rest + match->rmo);
+    for(head = match->pat->headers; head != NULL; head = head->next) {
+       headrmheader(req, head->name);
+       headappheader(req, head->name, head->value);
     }
-    if(pat == 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 match *match;
+    struct child *ch;
+    
+    match = NULL;
+    match = findmatch(lconfig, req, match);
+    if(gconfig != NULL)
+       match = findmatch(gconfig, req, match);
+    if(match == NULL) {
        simpleerror(fd, 404, "Not Found", "The requested resource could not be found on this server.");
        return;
     }
-    ch = NULL;
-    if(ch == NULL)
-       ch = getchild(lconfig, pat->childnm);
-    if(gconfig != NULL) {
+    execmatch(req, match);
+    switch(match->pat->handler) {
+    case HND_CHILD:
+       ch = NULL;
        if(ch == NULL)
-           ch = getchild(gconfig, pat->childnm);
-    }
-    if(ch == NULL) {
-       flog(LOG_ERR, "child %s requested, but was not declared", pat->childnm);
-       simpleerror(fd, 500, "Configuration Error", "The server is erroneously configured. Handler %s was requested, but not declared.", pat->childnm);
-       return;
-    }
-    
-    for(head = pat->headers; head != NULL; head = head->next) {
-       headrmheader(req, head->name);
-       headappheader(req, head->name, head->value);
+           ch = getchild(lconfig, match->pat->childnm);
+       if((ch == NULL) && (gconfig != NULL))
+           ch = getchild(gconfig, match->pat->childnm);
+       if(ch == NULL) {
+           flog(LOG_ERR, "child %s requested, but was not declared", match->pat->childnm);
+           simpleerror(fd, 500, "Configuration Error", "The server is erroneously configured. Handler %s was requested, but not declared.", match->pat->childnm);
+           break;
+       }
+       if(childhandle(ch, req, fd, NULL, NULL))
+           childerror(req, fd);
+       break;
+    case HND_REPARSE:
+       match->pat->disable = 1;
+       serve(req, fd);
+       match->pat->disable = 0;
+       break;
+    default:
+       abort();
     }
-    if(childhandle(ch, req, fd, NULL, NULL))
-       simpleerror(fd, 500, "Server Error", "The request handler crashed.");
+    freematch(match);
 }
 
 static void reloadconf(char *nm)