switch dynamic handlers to regex based patterns, much easier.

This commit is contained in:
Joris Vink 2013-05-29 14:29:46 +02:00
parent 2290d09d3b
commit 088d877715
9 changed files with 81 additions and 25 deletions

View File

@ -90,11 +90,11 @@ struct connection {
#define HANDLER_TYPE_DYNAMIC 2 #define HANDLER_TYPE_DYNAMIC 2
struct kore_module_handle { struct kore_module_handle {
char *uri; char *path;
char *domain;
char *func; char *func;
void *addr; void *addr;
int type; int type;
regex_t rctx;
TAILQ_ENTRY(kore_module_handle) list; TAILQ_ENTRY(kore_module_handle) list;
}; };
@ -126,6 +126,7 @@ long long kore_strtonum(const char *, long long, long long, int *);
void kore_module_load(char *); void kore_module_load(char *);
void kore_module_reload(void); void kore_module_reload(void);
int kore_module_loaded(void); int kore_module_loaded(void);
int kore_module_domain_new(char *);
void *kore_module_handler_find(char *, char *); void *kore_module_handler_find(char *, char *);
int kore_module_handler_new(char *, char *, char *, int); int kore_module_handler_new(char *, char *, char *, int);

View File

@ -31,6 +31,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <regex.h>
#include <zlib.h> #include <zlib.h>
#include "spdy.h" #include "spdy.h"

View File

@ -32,6 +32,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <regex.h>
#include <zlib.h> #include <zlib.h>
#include "spdy.h" #include "spdy.h"
@ -139,6 +140,10 @@ configure_domain(char **argv)
if (current_domain != NULL) if (current_domain != NULL)
free(current_domain); free(current_domain);
current_domain = kore_strdup(argv[1]); current_domain = kore_strdup(argv[1]);
if (!kore_module_domain_new(current_domain)) {
kore_log("could not create new domain %s", current_domain);
return (KORE_RESULT_ERROR);
}
return (KORE_RESULT_OK); return (KORE_RESULT_OK);
} }

View File

@ -31,6 +31,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <regex.h>
#include <time.h> #include <time.h>
#include <zlib.h> #include <zlib.h>

View File

@ -34,6 +34,7 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <time.h> #include <time.h>
#include <regex.h>
#include <zlib.h> #include <zlib.h>
#include "spdy.h" #include "spdy.h"

View File

@ -34,6 +34,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <regex.h>
#include <zlib.h> #include <zlib.h>
#include "spdy.h" #include "spdy.h"
@ -43,7 +44,14 @@ static void *mod_handle = NULL;
static char *mod_name = NULL; static char *mod_name = NULL;
static time_t mod_last_mtime = 0; static time_t mod_last_mtime = 0;
static TAILQ_HEAD(, kore_module_handle) handlers; struct module_domain {
char *domain;
TAILQ_HEAD(, kore_module_handle) handlers;
TAILQ_ENTRY(module_domain) list;
};
static TAILQ_HEAD(, module_domain) domains;
static struct module_domain *kore_module_domain_lookup(char *);
void void
kore_module_load(char *module_name) kore_module_load(char *module_name)
@ -63,13 +71,14 @@ kore_module_load(char *module_name)
if (mod_handle == NULL) if (mod_handle == NULL)
fatal("%s", dlerror()); fatal("%s", dlerror());
TAILQ_INIT(&handlers); TAILQ_INIT(&domains);
mod_name = kore_strdup(module_name); mod_name = kore_strdup(module_name);
} }
void void
kore_module_reload(void) kore_module_reload(void)
{ {
struct module_domain *dom;
struct kore_module_handle *hdlr; struct kore_module_handle *hdlr;
if (dlclose(mod_handle)) if (dlclose(mod_handle))
@ -79,11 +88,13 @@ kore_module_reload(void)
if (mod_handle == NULL) if (mod_handle == NULL)
fatal("kore_module_reload(): %s", dlerror()); fatal("kore_module_reload(): %s", dlerror());
TAILQ_FOREACH(hdlr, &handlers, list) { TAILQ_FOREACH(dom, &domains, list) {
TAILQ_FOREACH(hdlr, &(dom->handlers), list) {
hdlr->addr = dlsym(mod_handle, hdlr->func); hdlr->addr = dlsym(mod_handle, hdlr->func);
if (hdlr->func == NULL) if (hdlr->func == NULL)
fatal("no function '%s' found", hdlr->func); fatal("no function '%s' found", hdlr->func);
} }
}
kore_log("reloaded '%s' module", mod_name); kore_log("reloaded '%s' module", mod_name);
} }
@ -94,10 +105,27 @@ kore_module_loaded(void)
return (mod_handle != NULL ? KORE_RESULT_OK : KORE_RESULT_ERROR); return (mod_handle != NULL ? KORE_RESULT_OK : KORE_RESULT_ERROR);
} }
int
kore_module_domain_new(char *domain)
{
struct module_domain *dom;
if (kore_module_domain_lookup(domain) != NULL)
return (KORE_RESULT_ERROR);
dom = (struct module_domain *)kore_malloc(sizeof(*dom));
dom->domain = kore_strdup(domain);
TAILQ_INIT(&(dom->handlers));
TAILQ_INSERT_TAIL(&domains, dom, list);
return (KORE_RESULT_OK);
}
int int
kore_module_handler_new(char *path, char *domain, char *func, int type) kore_module_handler_new(char *path, char *domain, char *func, int type)
{ {
void *addr; void *addr;
struct module_domain *dom;
struct kore_module_handle *hdlr; struct kore_module_handle *hdlr;
char uri[512]; char uri[512];
@ -110,15 +138,26 @@ kore_module_handler_new(char *path, char *domain, char *func, int type)
return (KORE_RESULT_ERROR); return (KORE_RESULT_ERROR);
} }
snprintf(uri, sizeof(uri), "%s%s", domain, path); if ((dom = kore_module_domain_lookup(domain)) == NULL)
return (KORE_RESULT_ERROR);
hdlr = (struct kore_module_handle *)kore_malloc(sizeof(*hdlr)); hdlr = (struct kore_module_handle *)kore_malloc(sizeof(*hdlr));
hdlr->addr = addr; hdlr->addr = addr;
hdlr->type = type; hdlr->type = type;
hdlr->uri = kore_strdup(uri); hdlr->path = kore_strdup(path);
hdlr->func = kore_strdup(func); hdlr->func = kore_strdup(func);
TAILQ_INSERT_TAIL(&(handlers), hdlr, list);
if (hdlr->type == HANDLER_TYPE_DYNAMIC) {
if (regcomp(&(hdlr->rctx), hdlr->path, REG_NOSUB)) {
free(hdlr->func);
free(hdlr->path);
free(hdlr);
kore_log("regcomp() on %s failed", path);
return (KORE_RESULT_ERROR);
}
}
TAILQ_INSERT_TAIL(&(dom->handlers), hdlr, list);
return (KORE_RESULT_OK); return (KORE_RESULT_OK);
} }
@ -126,29 +165,34 @@ void *
kore_module_handler_find(char *domain, char *path) kore_module_handler_find(char *domain, char *path)
{ {
size_t len; size_t len;
struct module_domain *dom;
struct kore_module_handle *hdlr; struct kore_module_handle *hdlr;
char uri[512], *p;
snprintf(uri, sizeof(uri), "%s%s", domain, path); if ((dom = kore_module_domain_lookup(domain)) == NULL)
p = strchr(uri, '.'); return (NULL);
TAILQ_FOREACH(hdlr, &handlers, list) { TAILQ_FOREACH(hdlr, &(dom->handlers), list) {
if (hdlr->type == HANDLER_TYPE_STATIC) { if (hdlr->type == HANDLER_TYPE_STATIC) {
if (hdlr->uri[0] != '.' && !strcmp(hdlr->uri, uri)) if (!strcmp(hdlr->path, path))
return (hdlr->addr);
if (p != NULL && hdlr->uri[0] == '.' &&
!strcmp(hdlr->uri, p))
return (hdlr->addr); return (hdlr->addr);
} else { } else {
len = strlen(hdlr->uri); if (!regexec(&(hdlr->rctx), path, 0, NULL, 0))
if (hdlr->uri[0] != '.' &&
!strncmp(hdlr->uri, uri, len))
return (hdlr->addr);
if (p != NULL && hdlr->uri[0] == '.' &&
!strncmp(hdlr->uri, p, len))
return (hdlr->addr); return (hdlr->addr);
} }
} }
return (NULL); return (NULL);
} }
static struct module_domain *
kore_module_domain_lookup(char *domain)
{
struct module_domain *dom;
TAILQ_FOREACH(dom, &domains, list) {
if (!strcmp(dom->domain, domain))
return (dom);
}
return (NULL);
}

View File

@ -32,6 +32,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <regex.h>
#include <zlib.h> #include <zlib.h>
#include "spdy.h" #include "spdy.h"

View File

@ -31,6 +31,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <regex.h>
#include <zlib.h> #include <zlib.h>
#include "spdy.h" #include "spdy.h"

View File

@ -31,6 +31,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <regex.h>
#include <zlib.h> #include <zlib.h>
#include "spdy.h" #include "spdy.h"