kore/src/validator.c

140 lines
3.1 KiB
C
Raw Permalink Normal View History

/*
2022-01-31 22:02:06 +01:00
* Copyright (c) 2013-2022 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
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include "kore.h"
TAILQ_HEAD(, kore_validator) validators;
void
kore_validator_init(void)
{
TAILQ_INIT(&validators);
}
int
2014-08-04 12:40:21 +02:00
kore_validator_add(const char *name, u_int8_t type, const char *arg)
{
int ret;
struct kore_validator *val;
val = kore_malloc(sizeof(*val));
val->type = type;
switch (val->type) {
case KORE_VALIDATOR_TYPE_REGEX:
ret = regcomp(&(val->rctx), arg, REG_EXTENDED | REG_NOSUB);
if (ret) {
kore_free(val);
kore_log(LOG_NOTICE,
"validator %s has bad regex %s (%d)",
name, arg, ret);
return (KORE_RESULT_ERROR);
}
break;
case KORE_VALIDATOR_TYPE_FUNCTION:
val->rcall = kore_runtime_getcall(arg);
if (val->rcall == NULL) {
kore_free(val);
kore_log(LOG_NOTICE,
"validator %s has undefined callback %s",
name, arg);
return (KORE_RESULT_ERROR);
}
break;
default:
kore_free(val);
return (KORE_RESULT_ERROR);
}
val->arg = kore_strdup(arg);
val->name = kore_strdup(name);
TAILQ_INSERT_TAIL(&validators, val, list);
return (KORE_RESULT_OK);
}
int
2014-08-04 12:40:21 +02:00
kore_validator_run(struct http_request *req, const char *name, char *data)
{
struct kore_validator *val;
TAILQ_FOREACH(val, &validators, list) {
if (strcmp(val->name, name))
continue;
return (kore_validator_check(req, val, data));
}
return (KORE_RESULT_ERROR);
}
int
kore_validator_check(struct http_request *req, struct kore_validator *val,
Rework HTTP and worker processes. The HTTP layer used to make a copy of each incoming header and its value for a request. Stop doing that and make HTTP headers zero-copy all across the board. This change comes with some api function changes, notably the http_request_header() function which now takes a const char ** rather than a char ** out pointer. This commit also constifies several members of http_request, beware. Additional rework how the worker processes deal with the accept lock. Before: if a worker held the accept lock and it accepted a new connection it would release the lock for others and back off for 500ms before attempting to grab the lock again. This approach worked but under high load this starts becoming obvious. Now: - workers not holding the accept lock and not having any connections will wait less long before returning from kore_platform_event_wait(). - workers not holding the accept lock will no longer blindly wait an arbitrary amount in kore_platform_event_wait() but will look at how long until the next lock grab is and base their timeout on that. - if a worker its next_lock timeout is up and failed to grab the lock it will try again in half the time again. - the worker process holding the lock will when releasing the lock double check if it still has space for newer connections, if it does it will keep the lock until it is full. This prevents the lock from bouncing between several non busy worker processes all the time. Additional fixes: - Reduce the number of times we check the timeout list, only do it twice per second rather then every event tick. - Fix solo worker count for TLS (we actually hold two processes, not one). - Make sure we don't accidentally miscalculate the idle time causing new connections under heavy load to instantly drop. - Swap from gettimeofday() to clock_gettime() now that MacOS caught up.
2018-02-14 13:48:49 +01:00
const void *data)
{
int r;
switch (val->type) {
case KORE_VALIDATOR_TYPE_REGEX:
if (!regexec(&(val->rctx), data, 0, NULL, 0))
r = KORE_RESULT_OK;
else
r = KORE_RESULT_ERROR;
break;
case KORE_VALIDATOR_TYPE_FUNCTION:
r = kore_runtime_validator(val->rcall, req, data);
break;
default:
r = KORE_RESULT_ERROR;
kore_log(LOG_NOTICE, "invalid type %d for validator %s",
val->type, val->name);
break;
}
return (r);
}
void
kore_validator_reload(void)
{
struct kore_validator *val;
TAILQ_FOREACH(val, &validators, list) {
if (val->type != KORE_VALIDATOR_TYPE_FUNCTION)
continue;
kore_free(val->rcall);
val->rcall = kore_runtime_getcall(val->arg);
if (val->rcall == NULL)
fatal("no function for validator %s found", val->arg);
}
}
struct kore_validator *
2014-08-04 12:40:21 +02:00
kore_validator_lookup(const char *name)
{
struct kore_validator *val;
TAILQ_FOREACH(val, &validators, list) {
if (!strcmp(val->name, name))
return (val);
}
return (NULL);
}