kore

Kore is a web application platform for writing scalable, concurrent web based processes in C or Python.
Commits | Files | Refs | README | LICENSE | git clone https://git.kore.io/kore.git

commit 088d877715f591c98e469aeaa0402c912dfa2df6
parent 2290d09d3bb2ca7f50f57512a45c3bd7a2163713
Author: Joris Vink <joris@coders.se>
Date:   Wed, 29 May 2013 14:29:46 +0200

switch dynamic handlers to regex based patterns, much easier.

Diffstat:
includes/kore.h | 5+++--
src/buf.c | 1+
src/config.c | 5+++++
src/http.c | 1+
src/kore.c | 1+
src/module.c | 90+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------
src/net.c | 1+
src/spdy.c | 1+
src/utils.c | 1+
9 files changed, 81 insertions(+), 25 deletions(-)

diff --git a/includes/kore.h b/includes/kore.h @@ -90,11 +90,11 @@ struct connection { #define HANDLER_TYPE_DYNAMIC 2 struct kore_module_handle { - char *uri; - char *domain; + char *path; char *func; void *addr; int type; + regex_t rctx; 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_reload(void); int kore_module_loaded(void); +int kore_module_domain_new(char *); void *kore_module_handler_find(char *, char *); int kore_module_handler_new(char *, char *, char *, int); diff --git a/src/buf.c b/src/buf.c @@ -31,6 +31,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <regex.h> #include <zlib.h> #include "spdy.h" diff --git a/src/config.c b/src/config.c @@ -32,6 +32,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <regex.h> #include <zlib.h> #include "spdy.h" @@ -139,6 +140,10 @@ configure_domain(char **argv) if (current_domain != NULL) free(current_domain); 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); } diff --git a/src/http.c b/src/http.c @@ -31,6 +31,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <regex.h> #include <time.h> #include <zlib.h> diff --git a/src/kore.c b/src/kore.c @@ -34,6 +34,7 @@ #include <string.h> #include <unistd.h> #include <time.h> +#include <regex.h> #include <zlib.h> #include "spdy.h" diff --git a/src/module.c b/src/module.c @@ -34,6 +34,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <regex.h> #include <zlib.h> #include "spdy.h" @@ -43,7 +44,14 @@ static void *mod_handle = NULL; static char *mod_name = NULL; 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 kore_module_load(char *module_name) @@ -63,13 +71,14 @@ kore_module_load(char *module_name) if (mod_handle == NULL) fatal("%s", dlerror()); - TAILQ_INIT(&handlers); + TAILQ_INIT(&domains); mod_name = kore_strdup(module_name); } void kore_module_reload(void) { + struct module_domain *dom; struct kore_module_handle *hdlr; if (dlclose(mod_handle)) @@ -79,10 +88,12 @@ kore_module_reload(void) if (mod_handle == NULL) fatal("kore_module_reload(): %s", dlerror()); - TAILQ_FOREACH(hdlr, &handlers, list) { - hdlr->addr = dlsym(mod_handle, hdlr->func); - if (hdlr->func == NULL) - fatal("no function '%s' found", hdlr->func); + TAILQ_FOREACH(dom, &domains, list) { + TAILQ_FOREACH(hdlr, &(dom->handlers), list) { + hdlr->addr = dlsym(mod_handle, hdlr->func); + if (hdlr->func == NULL) + fatal("no function '%s' found", hdlr->func); + } } kore_log("reloaded '%s' module", mod_name); @@ -95,9 +106,26 @@ kore_module_loaded(void) } 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 kore_module_handler_new(char *path, char *domain, char *func, int type) { void *addr; + struct module_domain *dom; struct kore_module_handle *hdlr; char uri[512]; @@ -110,15 +138,26 @@ kore_module_handler_new(char *path, char *domain, char *func, int type) 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->addr = addr; hdlr->type = type; - hdlr->uri = kore_strdup(uri); + hdlr->path = kore_strdup(path); 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); } @@ -126,29 +165,34 @@ void * kore_module_handler_find(char *domain, char *path) { size_t len; + struct module_domain *dom; struct kore_module_handle *hdlr; - char uri[512], *p; - snprintf(uri, sizeof(uri), "%s%s", domain, path); - p = strchr(uri, '.'); + if ((dom = kore_module_domain_lookup(domain)) == NULL) + return (NULL); - TAILQ_FOREACH(hdlr, &handlers, list) { + TAILQ_FOREACH(hdlr, &(dom->handlers), list) { if (hdlr->type == HANDLER_TYPE_STATIC) { - if (hdlr->uri[0] != '.' && !strcmp(hdlr->uri, uri)) - return (hdlr->addr); - if (p != NULL && hdlr->uri[0] == '.' && - !strcmp(hdlr->uri, p)) + if (!strcmp(hdlr->path, path)) return (hdlr->addr); } else { - len = strlen(hdlr->uri); - if (hdlr->uri[0] != '.' && - !strncmp(hdlr->uri, uri, len)) - return (hdlr->addr); - if (p != NULL && hdlr->uri[0] == '.' && - !strncmp(hdlr->uri, p, len)) + if (!regexec(&(hdlr->rctx), path, 0, NULL, 0)) return (hdlr->addr); } } 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); +} diff --git a/src/net.c b/src/net.c @@ -32,6 +32,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <regex.h> #include <zlib.h> #include "spdy.h" diff --git a/src/spdy.c b/src/spdy.c @@ -31,6 +31,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <regex.h> #include <zlib.h> #include "spdy.h" diff --git a/src/utils.c b/src/utils.c @@ -31,6 +31,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <regex.h> #include <zlib.h> #include "spdy.h"