kore

An easy to use, scalable and secure web application framework for writing web APIs in C.
Commits | Files | Refs | README | LICENSE | git clone https://git.kore.io/kore.git

commit 807764614be04aa74e0ef8d7df7657713a643a1a
parent 53cbc1a21eeb558477591e7154952b64195e5354
Author: Joris Vink <joris@coders.se>
Date:   Thu,  2 May 2013 13:30:13 +0200

from now on configuration files must specify a domain for the handlers that follow. This allows for easy subdomain configuration.

example:

domain joris.local
static / serve_index

domain .joris.local
static / serve_another_index

Diffstat:
example.conf | 1+
includes/kore.h | 5+++--
src/config.c | 25+++++++++++++++++++++++--
src/http.c | 2+-
src/module.c | 20+++++++++++++++-----
5 files changed, 43 insertions(+), 10 deletions(-)

diff --git a/example.conf b/example.conf @@ -8,5 +8,6 @@ load example/example.module # Declare page handlers below. # handler path module_callback +domain 10.211.55.3 static /css/style.css betrippin_serve_style_css static / betrippin_serve_index diff --git a/includes/kore.h b/includes/kore.h @@ -89,6 +89,7 @@ struct connection { struct kore_module_handle { char *uri; + char *domain; void *func; int type; @@ -121,8 +122,8 @@ long long kore_strtonum(const char *, long long, long long, int *); void kore_module_load(char *); int kore_module_loaded(void); -void *kore_module_handler_find(char *); -int kore_module_handler_new(char *, char *, int); +void *kore_module_handler_find(char *, char *); +int kore_module_handler_new(char *, char *, char *, int); void fatal(const char *, ...); void kore_log_internal(char *, int, const char *, ...); diff --git a/src/config.c b/src/config.c @@ -40,6 +40,7 @@ static int configure_bind(char **); static int configure_load(char **); static int configure_handler(char **); +static int configure_domain(char **); static struct { const char *name; @@ -48,10 +49,12 @@ static struct { { "bind", configure_bind }, { "load", configure_load }, { "static", configure_handler }, - { "dynamic", configure_handler }, + { "domain", configure_domain }, { NULL, NULL }, }; +static char *current_domain = NULL; + void kore_parse_config(const char *config_path) { @@ -127,10 +130,28 @@ configure_load(char **argv) } static int +configure_domain(char **argv) +{ + if (argv[1] == NULL) + return (KORE_RESULT_ERROR); + + if (current_domain != NULL) + free(current_domain); + current_domain = kore_strdup(argv[1]); + + return (KORE_RESULT_OK); +} + +static int configure_handler(char **argv) { int type; + if (current_domain == NULL) { + kore_log("missing domain for page handler"); + return (KORE_RESULT_ERROR); + } + if (argv[1] == NULL || argv[2] == NULL) return (KORE_RESULT_ERROR); @@ -141,7 +162,7 @@ configure_handler(char **argv) else return (KORE_RESULT_ERROR); - if (!kore_module_handler_new(argv[1], argv[2], type)) { + if (!kore_module_handler_new(argv[1], current_domain, argv[2], type)) { kore_log("cannot create handler for %s", argv[1]); return (KORE_RESULT_ERROR); } diff --git a/src/http.c b/src/http.c @@ -231,7 +231,7 @@ http_process(void) for (req = TAILQ_FIRST(&http_requests); req != NULL; req = next) { next = TAILQ_NEXT(req, list); - handler = kore_module_handler_find(req->path); + handler = kore_module_handler_find(req->host, req->path); if (handler == NULL) r = http_generic_404(req); else diff --git a/src/module.c b/src/module.c @@ -72,12 +72,14 @@ kore_module_loaded(void) } int -kore_module_handler_new(char *uri, char *func, int type) +kore_module_handler_new(char *path, char *domain, char *func, int type) { void *addr; struct kore_module_handle *hdlr; + char uri[512]; - kore_log("kore_module_handler_new(%s, %s, %d)", uri, func, type); + kore_log("kore_module_handler_new(%s, %s, %s, %d)", path, + domain, func, type); addr = dlsym(mod_handle, func); if (addr == NULL) { @@ -85,22 +87,30 @@ kore_module_handler_new(char *uri, char *func, int type) return (KORE_RESULT_ERROR); } + snprintf(uri, sizeof(uri), "%s%s", domain, path); + hdlr = (struct kore_module_handle *)kore_malloc(sizeof(*hdlr)); - hdlr->uri = kore_strdup(uri); hdlr->func = addr; hdlr->type = type; + hdlr->uri = kore_strdup(uri); TAILQ_INSERT_TAIL(&(handlers), hdlr, list); return (KORE_RESULT_OK); } void * -kore_module_handler_find(char *uri) +kore_module_handler_find(char *domain, char *path) { struct kore_module_handle *hdlr; + char uri[512], *p; + + snprintf(uri, sizeof(uri), "%s%s", domain, path); + p = strchr(uri, '.'); TAILQ_FOREACH(hdlr, &handlers, list) { - if (!strcmp(hdlr->uri, uri)) + if (hdlr->uri[0] != '.' && !strcmp(hdlr->uri, uri)) + return (hdlr->func); + if (hdlr->uri[0] == '.' && !strcmp(hdlr->uri, p)) return (hdlr->func); }