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 f56938283d60d65a727f91e444bbbb76209f5c43
parent 38df26f59cf768b8e25b4160b621b1702a270615
Author: Joris Vink <joris@coders.se>
Date:   Wed, 25 Jan 2017 22:23:34 +0100

Merge branch 'master' of github.com:jorisvink/kore

Diffstat:
LICENSE | 2+-
examples/generic/src/example.c | 3+++
examples/messaging/conf/messaging.conf | 3++-
examples/messaging/src/messaging.c | 15+++++++++++++++
includes/http.h | 1+
includes/kore.h | 1+
src/http.c | 38++++++++++++++++++++++++++++++++++----
src/msg.c | 10++++++++++
8 files changed, 67 insertions(+), 6 deletions(-)

diff --git a/LICENSE b/LICENSE @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013-2016 Joris Vink <joris@coders.se> + * Copyright (c) 2013-2017 Joris Vink <joris@coders.se> * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above diff --git a/examples/generic/src/example.c b/examples/generic/src/example.c @@ -58,6 +58,9 @@ example_load(int state) switch (state) { case KORE_MODULE_LOAD: kore_log(LOG_NOTICE, "module loading"); + + /* Set server version */ + http_server_version("Server/0.1"); break; case KORE_MODULE_UNLOAD: kore_log(LOG_NOTICE, "module unloading"); diff --git a/examples/messaging/conf/messaging.conf b/examples/messaging/conf/messaging.conf @@ -8,5 +8,6 @@ workers 4 domain 127.0.0.1 { certfile cert/server.crt certkey cert/server.key - static / page + static / page + static /shutdown page_shutdown } diff --git a/examples/messaging/src/messaging.c b/examples/messaging/src/messaging.c @@ -28,6 +28,7 @@ int init(int); int page(struct http_request *); +int page_shutdown(struct http_request *req); void received_message(struct kore_msg *, const void *); /* Initialization callback. */ @@ -73,3 +74,17 @@ page(struct http_request *req) http_response(req, 200, NULL, 0); return (KORE_RESULT_OK); } + +/* + * Page request which will send a message to the parent + * requesting process shutdown. + */ +int +page_shutdown(struct http_request *req) +{ + /* Send shutdown request to parent. */ + kore_msg_send(KORE_MSG_PARENT, KORE_MSG_SHUTDOWN, "1", 1); + + http_response(req, 200, NULL, 0); + return (KORE_RESULT_OK); +} diff --git a/includes/http.h b/includes/http.h @@ -221,6 +221,7 @@ void kore_accesslog(struct http_request *); void http_init(void); void http_cleanup(void); +void http_server_version(const char *); void http_process(void); const char *http_status_text(int); const char *http_method_text(int); diff --git a/includes/kore.h b/includes/kore.h @@ -417,6 +417,7 @@ struct kore_timer { #define KORE_MSG_WEBSOCKET 2 #define KORE_MSG_KEYMGR_REQ 3 #define KORE_MSG_KEYMGR_RESP 4 +#define KORE_MSG_SHUTDOWN 5 /* Predefined message targets. */ #define KORE_MSG_PARENT 1000 diff --git a/src/http.c b/src/http.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013-2016 Joris Vink <joris@coders.se> + * Copyright (c) 2013-2017 Joris Vink <joris@coders.se> * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -23,6 +23,8 @@ #include <fcntl.h> #include <unistd.h> +#include <sys/socket.h> +#include <netinet/in.h> #include "kore.h" #include "http.h" @@ -118,6 +120,19 @@ http_cleanup(void) kore_pool_cleanup(&http_body_path); } +void +http_server_version(const char *version) +{ + int l; + + l = snprintf(http_version, sizeof(http_version), + "server: %s\r\n", version); + if (l == -1 || (size_t)l >= sizeof(http_version)) + fatal("http_server_version(): http_version buffer too small"); + + http_version_len = l; +} + int http_request_new(struct connection *c, const char *host, const char *method, const char *path, const char *version, @@ -132,11 +147,26 @@ http_request_new(struct connection *c, const char *host, kore_debug("http_request_new(%p, %s, %s, %s, %s)", c, host, method, path, version); - if ((p = strrchr(host, ':')) != NULL) - *p = '\0'; + switch (c->addrtype) { + case AF_INET6: + if (*host == '[') { + if ((p = strrchr(host, ']')) == NULL) { + http_error_response(c, 400); + return (KORE_RESULT_ERROR); + } + p++; + if (*p == ':') + *p = '\0'; + } + break; + default: + if ((p = strrchr(host, ':')) != NULL) + *p = '\0'; + break; + } if ((hostlen = strlen(host)) >= KORE_DOMAINNAME_LEN - 1) { - http_error_response(c, 500); + http_error_response(c, 400); return (KORE_RESULT_ERROR); } diff --git a/src/msg.c b/src/msg.c @@ -34,6 +34,7 @@ static int msg_recv_packet(struct netbuf *); static int msg_recv_data(struct netbuf *); static void msg_disconnected_parent(struct connection *); static void msg_disconnected_worker(struct connection *); +static void msg_type_shutdown(struct kore_msg *msg, const void *data); #if !defined(KORE_NO_HTTP) static void msg_type_accesslog(struct kore_msg *, const void *); @@ -57,6 +58,8 @@ kore_msg_parent_init(void) kore_msg_parent_add(kw); } + kore_msg_register(KORE_MSG_SHUTDOWN, msg_type_shutdown); + #if !defined(KORE_NO_HTTP) kore_msg_register(KORE_MSG_ACCESSLOG, msg_type_accesslog); #endif @@ -207,6 +210,13 @@ msg_disconnected_worker(struct connection *c) c->hdlr_extra = NULL; } +static void +msg_type_shutdown(struct kore_msg *msg, const void *data) +{ + kore_log(LOG_NOTICE, "worker requested shutdown"); + kore_signal(SIGQUIT); +} + #if !defined(KORE_NO_HTTP) static void msg_type_accesslog(struct kore_msg *msg, const void *data)