2016-03-19 15:14:35 +00:00
|
|
|
#include "first.h"
|
|
|
|
|
2018-03-25 07:45:05 +00:00
|
|
|
#include "base.h"
|
2005-02-20 14:27:00 +00:00
|
|
|
#include "plugin.h"
|
|
|
|
#include "log.h"
|
2005-08-08 08:22:06 +00:00
|
|
|
#include "stat_cache.h"
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2018-03-25 07:45:05 +00:00
|
|
|
#include <stdlib.h>
|
2009-10-11 14:31:42 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2019-11-03 00:44:16 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* #
|
|
|
|
* # define a pattern for the host url finding
|
|
|
|
* # %% => % sign
|
|
|
|
* # %0 => domain name + tld
|
|
|
|
* # %1 => tld
|
|
|
|
* # %2 => domain name without tld
|
|
|
|
* # %3 => subdomain 1 name
|
|
|
|
* # %4 => subdomain 2 name
|
|
|
|
* # %_ => fqdn (without port info)
|
|
|
|
* #
|
|
|
|
* evhost.path-pattern = "/home/ckruse/dev/www/%3/htdocs/"
|
|
|
|
*
|
|
|
|
*/
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2019-11-03 00:44:16 +00:00
|
|
|
typedef struct {
|
|
|
|
/* pieces for path creation */
|
|
|
|
const buffer *path_pieces;
|
2005-02-20 14:27:00 +00:00
|
|
|
} plugin_config;
|
|
|
|
|
|
|
|
typedef struct {
|
2019-11-03 00:44:16 +00:00
|
|
|
PLUGIN_DATA;
|
|
|
|
plugin_config defaults;
|
|
|
|
plugin_config conf;
|
|
|
|
|
|
|
|
array split_vals;
|
2005-02-20 14:27:00 +00:00
|
|
|
} plugin_data;
|
|
|
|
|
|
|
|
INIT_FUNC(mod_evhost_init) {
|
2019-11-03 00:44:16 +00:00
|
|
|
return calloc(1, sizeof(plugin_data));
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2019-11-03 00:44:16 +00:00
|
|
|
static void mod_evhost_free_path_pieces(const buffer *path_pieces) {
|
|
|
|
buffer *b;
|
|
|
|
*(const buffer **)&b = path_pieces;
|
|
|
|
for (; path_pieces->ptr; ++path_pieces) free(path_pieces->ptr);
|
|
|
|
free(b);
|
|
|
|
}
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2019-11-19 08:39:40 +00:00
|
|
|
FREE_FUNC(mod_evhost_free) {
|
|
|
|
plugin_data * const p = p_d;
|
|
|
|
array_free_data(&p->split_vals);
|
2019-11-03 00:44:16 +00:00
|
|
|
if (NULL == p->cvlist) return;
|
|
|
|
/* (init i to 0 if global context; to 1 to skip empty global context) */
|
|
|
|
for (int i = !p->cvlist[0].v.u2[1], used = p->nconfig; i < used; ++i) {
|
|
|
|
config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0];
|
|
|
|
for (; -1 != cpv->k_id; ++cpv) {
|
|
|
|
if (cpv->vtype != T_CONFIG_LOCAL || NULL == cpv->v.v) continue;
|
|
|
|
switch (cpv->k_id) {
|
|
|
|
case 0: /* evhost.path-pattern */
|
|
|
|
mod_evhost_free_path_pieces(cpv->v.v);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2019-11-03 00:44:16 +00:00
|
|
|
__attribute_cold__
|
|
|
|
static buffer * mod_evhost_parse_pattern_err(buffer *bptr) {
|
|
|
|
for (; bptr->ptr; ++bptr) free(bptr->ptr);
|
|
|
|
return NULL;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2019-11-03 00:44:16 +00:00
|
|
|
static buffer * mod_evhost_parse_pattern(const char *ptr) {
|
|
|
|
uint32_t used = 0;
|
2019-12-08 05:42:23 +00:00
|
|
|
const uint32_t sz = 127;/* (sz+1 must match bptr[] num elts below) */
|
2019-11-03 00:44:16 +00:00
|
|
|
const char *pos;
|
2019-12-08 05:42:23 +00:00
|
|
|
buffer bptr[128]; /* (128 elements takes 2k on stack in 64-bit) */
|
2019-11-03 00:44:16 +00:00
|
|
|
memset(bptr, 0, sizeof(bptr));
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
for(pos=ptr;*ptr;ptr++) {
|
|
|
|
if(*ptr == '%') {
|
2016-10-20 17:50:39 +00:00
|
|
|
size_t len;
|
2019-11-03 00:44:16 +00:00
|
|
|
if (used >= sz-1) /* (should not happen) */
|
|
|
|
return mod_evhost_parse_pattern_err(bptr);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2016-10-20 17:50:39 +00:00
|
|
|
/* "%%" "%_" "%x" "%{x.y}" where x and y are *single digit* 0 - 9 */
|
|
|
|
if (ptr[1] == '%' || ptr[1] == '_' || light_isdigit(ptr[1])) {
|
|
|
|
len = 2;
|
|
|
|
} else if (ptr[1] == '{') {
|
2019-11-03 00:44:16 +00:00
|
|
|
if (!light_isdigit(ptr[2]))
|
|
|
|
return mod_evhost_parse_pattern_err(bptr);
|
2016-10-20 17:50:39 +00:00
|
|
|
if (ptr[3] == '.') {
|
2019-11-03 00:44:16 +00:00
|
|
|
if (!light_isdigit(ptr[4]))
|
|
|
|
return mod_evhost_parse_pattern_err(bptr);
|
|
|
|
if (ptr[5] != '}')
|
|
|
|
return mod_evhost_parse_pattern_err(bptr);
|
2016-10-20 17:50:39 +00:00
|
|
|
len = 6;
|
|
|
|
} else if (ptr[3] == '}') {
|
|
|
|
len = 4;
|
|
|
|
} else {
|
2019-11-03 00:44:16 +00:00
|
|
|
return mod_evhost_parse_pattern_err(bptr);
|
2016-10-20 17:50:39 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-11-03 00:44:16 +00:00
|
|
|
return mod_evhost_parse_pattern_err(bptr);
|
2016-10-20 17:50:39 +00:00
|
|
|
}
|
|
|
|
|
2019-11-03 00:44:16 +00:00
|
|
|
buffer_copy_string_len(bptr+used, pos, ptr-pos);
|
2016-10-20 17:50:39 +00:00
|
|
|
pos = ptr + len;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2019-11-03 00:44:16 +00:00
|
|
|
buffer_copy_string_len(bptr+used+1, ptr, len);
|
2016-10-20 17:50:39 +00:00
|
|
|
ptr += len - 1; /*(ptr++ in for() loop)*/
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2019-11-03 00:44:16 +00:00
|
|
|
used += 2;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if(*pos != '\0') {
|
2019-11-03 00:44:16 +00:00
|
|
|
if (used >= sz) /* (should not happen) */
|
|
|
|
return mod_evhost_parse_pattern_err(bptr);
|
|
|
|
buffer_copy_string_len(bptr+used, pos, ptr-pos);
|
|
|
|
++used;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2016-10-20 17:50:39 +00:00
|
|
|
|
2019-11-03 00:44:16 +00:00
|
|
|
buffer * const path_pieces =
|
|
|
|
malloc(sizeof(bptr) + ((used+1) * sizeof(buffer)));
|
|
|
|
force_assert(path_pieces);
|
|
|
|
return memcpy(path_pieces, bptr, (used+1) * sizeof(buffer));
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2019-11-03 00:44:16 +00:00
|
|
|
static void mod_evhost_merge_config_cpv(plugin_config * const pconf, const config_plugin_value_t * const cpv) {
|
|
|
|
switch (cpv->k_id) { /* index into static config_plugin_keys_t cpk[] */
|
|
|
|
case 0: /* evhost.path-pattern */
|
|
|
|
if (cpv->vtype == T_CONFIG_LOCAL)
|
|
|
|
pconf->path_pieces = cpv->v.v;
|
|
|
|
break;
|
|
|
|
default:/* should not happen */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2019-11-03 00:44:16 +00:00
|
|
|
static void mod_evhost_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv) {
|
|
|
|
do {
|
|
|
|
mod_evhost_merge_config_cpv(pconf, cpv);
|
|
|
|
} while ((++cpv)->k_id != -1);
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
static void mod_evhost_patch_config(request_st * const r, plugin_data * const p) {
|
2020-01-11 16:07:43 +00:00
|
|
|
p->conf = p->defaults; /* copy small struct instead of memcpy() */
|
|
|
|
/*memcpy(&p->conf, &p->defaults, sizeof(plugin_config));*/
|
2019-11-03 00:44:16 +00:00
|
|
|
for (int i = 1, used = p->nconfig; i < used; ++i) {
|
2020-01-13 02:51:12 +00:00
|
|
|
if (config_check_cond(r, (uint32_t)p->cvlist[i].k_id))
|
2019-11-03 00:44:16 +00:00
|
|
|
mod_evhost_merge_config(&p->conf, p->cvlist + p->cvlist[i].v.u2[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SETDEFAULTS_FUNC(mod_evhost_set_defaults) {
|
|
|
|
static const config_plugin_keys_t cpk[] = {
|
|
|
|
{ CONST_STR_LEN("evhost.path-pattern"),
|
|
|
|
T_CONFIG_STRING,
|
|
|
|
T_CONFIG_SCOPE_CONNECTION }
|
|
|
|
,{ NULL, 0,
|
|
|
|
T_CONFIG_UNSET,
|
|
|
|
T_CONFIG_SCOPE_UNSET }
|
|
|
|
};
|
|
|
|
|
|
|
|
plugin_data * const p = p_d;
|
|
|
|
if (!config_plugin_values_init(srv, p, cpk, "mod_evhost"))
|
|
|
|
return HANDLER_ERROR;
|
|
|
|
|
|
|
|
/* process and validate config directives
|
|
|
|
* (init i to 0 if global context; to 1 to skip empty global context) */
|
|
|
|
for (int i = !p->cvlist[0].v.u2[1]; i < p->nconfig; ++i) {
|
|
|
|
config_plugin_value_t *cpv = p->cvlist + p->cvlist[i].v.u2[0];
|
|
|
|
for (; -1 != cpv->k_id; ++cpv) {
|
|
|
|
switch (cpv->k_id) {
|
|
|
|
case 0: /* evhost.path-pattern */
|
[multiple] reduce redundant NULL buffer checks
This commit is a large set of code changes and results in removal of
hundreds, perhaps thousands, of CPU instructions, a portion of which
are on hot code paths.
Most (buffer *) used by lighttpd are not NULL, especially since buffers
were inlined into numerous larger structs such as request_st and chunk.
In the small number of instances where that is not the case, a NULL
check is often performed earlier in a function where that buffer is
later used with a buffer_* func. In the handful of cases that remained,
a NULL check was added, e.g. with r->http_host and r->conf.server_tag.
- check for empty strings at config time and set value to NULL if blank
string will be ignored at runtime; at runtime, simple pointer check
for NULL can be used to check for a value that has been set and is not
blank ("")
- use buffer_is_blank() instead of buffer_string_is_empty(),
and use buffer_is_unset() instead of buffer_is_empty(),
where buffer is known not to be NULL so that NULL check can be skipped
- use buffer_clen() instead of buffer_string_length() when buffer is
known not to be NULL (to avoid NULL check at runtime)
- use buffer_truncate() instead of buffer_string_set_length() to
truncate string, and use buffer_extend() to extend
Examples where buffer known not to be NULL:
- cpv->v.b from config_plugin_values_init is not NULL if T_CONFIG_BOOL
(though we might set it to NULL if buffer_is_blank(cpv->v.b))
- address of buffer is arg (&foo)
(compiler optimizer detects this in most, but not all, cases)
- buffer is checked for NULL earlier in func
- buffer is accessed in same scope without a NULL check (e.g. b->ptr)
internal behavior change:
callers must not pass a NULL buffer to some funcs.
- buffer_init_buffer() requires non-null args
- buffer_copy_buffer() requires non-null args
- buffer_append_string_buffer() requires non-null args
- buffer_string_space() requires non-null arg
2021-06-09 02:57:36 +00:00
|
|
|
if (!buffer_is_blank(cpv->v.b)) {
|
2019-11-03 00:44:16 +00:00
|
|
|
const char * const ptr = cpv->v.b->ptr;
|
|
|
|
cpv->v.v = mod_evhost_parse_pattern(ptr);
|
|
|
|
if (NULL == cpv->v.v) {
|
|
|
|
log_error(srv->errh, __FILE__, __LINE__,
|
|
|
|
"invalid evhost.path-pattern: %s", ptr);
|
|
|
|
return HANDLER_ERROR;
|
|
|
|
}
|
|
|
|
cpv->vtype = T_CONFIG_LOCAL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:/* should not happen */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* initialize p->defaults from global config context */
|
|
|
|
if (p->nconfig > 0 && p->cvlist->v.u2[1]) {
|
|
|
|
const config_plugin_value_t *cpv = p->cvlist + p->cvlist->v.u2[0];
|
|
|
|
if (-1 != cpv->k_id)
|
|
|
|
mod_evhost_merge_config(&p->defaults, cpv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return HANDLER_GO_ON;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-04-26 20:19:31 +00:00
|
|
|
* assign the different parts of the domain to array-indezes (sub2.sub1.domain.tld)
|
|
|
|
* - %0 - domain.tld
|
2005-02-20 14:27:00 +00:00
|
|
|
* - %1 - tld
|
2009-04-26 20:19:31 +00:00
|
|
|
* - %2 - domain
|
|
|
|
* - %3 - sub1
|
|
|
|
* - ...
|
2005-02-20 14:27:00 +00:00
|
|
|
*/
|
|
|
|
|
2018-12-03 05:35:00 +00:00
|
|
|
static void mod_evhost_parse_host(buffer *key, array *host, buffer *authority) {
|
[multiple] reduce redundant NULL buffer checks
This commit is a large set of code changes and results in removal of
hundreds, perhaps thousands, of CPU instructions, a portion of which
are on hot code paths.
Most (buffer *) used by lighttpd are not NULL, especially since buffers
were inlined into numerous larger structs such as request_st and chunk.
In the small number of instances where that is not the case, a NULL
check is often performed earlier in a function where that buffer is
later used with a buffer_* func. In the handful of cases that remained,
a NULL check was added, e.g. with r->http_host and r->conf.server_tag.
- check for empty strings at config time and set value to NULL if blank
string will be ignored at runtime; at runtime, simple pointer check
for NULL can be used to check for a value that has been set and is not
blank ("")
- use buffer_is_blank() instead of buffer_string_is_empty(),
and use buffer_is_unset() instead of buffer_is_empty(),
where buffer is known not to be NULL so that NULL check can be skipped
- use buffer_clen() instead of buffer_string_length() when buffer is
known not to be NULL (to avoid NULL check at runtime)
- use buffer_truncate() instead of buffer_string_set_length() to
truncate string, and use buffer_extend() to extend
Examples where buffer known not to be NULL:
- cpv->v.b from config_plugin_values_init is not NULL if T_CONFIG_BOOL
(though we might set it to NULL if buffer_is_blank(cpv->v.b))
- address of buffer is arg (&foo)
(compiler optimizer detects this in most, but not all, cases)
- buffer is checked for NULL earlier in func
- buffer is accessed in same scope without a NULL check (e.g. b->ptr)
internal behavior change:
callers must not pass a NULL buffer to some funcs.
- buffer_init_buffer() requires non-null args
- buffer_copy_buffer() requires non-null args
- buffer_append_string_buffer() requires non-null args
- buffer_string_space() requires non-null arg
2021-06-09 02:57:36 +00:00
|
|
|
char *ptr = authority->ptr + buffer_clen(authority);
|
2005-02-20 14:27:00 +00:00
|
|
|
char *colon = ptr; /* needed to filter out the colon (if exists) */
|
|
|
|
int first = 1;
|
|
|
|
int i;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2019-02-02 23:40:47 +00:00
|
|
|
/*if (ptr == authority->ptr) return;*//*(no authority checked earlier)*/
|
|
|
|
|
|
|
|
if (*authority->ptr == '[') { /* authority is IPv6 literal address */
|
|
|
|
colon = ptr;
|
|
|
|
if (ptr[-1] != ']') {
|
|
|
|
do { --ptr; } while (ptr > authority->ptr && ptr[-1] != ']');
|
|
|
|
if (*ptr != ':') return; /*(should not happen for valid authority)*/
|
|
|
|
colon = ptr;
|
|
|
|
}
|
|
|
|
ptr = authority->ptr;
|
2019-10-02 05:54:15 +00:00
|
|
|
array_set_key_value(host,CONST_STR_LEN("%0"),ptr,colon-ptr);
|
2019-02-02 23:40:47 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* first, find the domain + tld */
|
2018-12-03 05:35:00 +00:00
|
|
|
for(; ptr > authority->ptr; --ptr) {
|
2005-02-20 14:27:00 +00:00
|
|
|
if(*ptr == '.') {
|
|
|
|
if(first) first = 0;
|
|
|
|
else break;
|
|
|
|
} else if(*ptr == ':') {
|
|
|
|
colon = ptr;
|
|
|
|
first = 1;
|
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* if we stopped at a dot, skip the dot */
|
|
|
|
if (*ptr == '.') ptr++;
|
2019-10-02 05:54:15 +00:00
|
|
|
array_set_key_value(host, CONST_STR_LEN("%0"), ptr, colon-ptr);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* if the : is not the start of the authority, go on parsing the hostname */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2018-12-03 05:35:00 +00:00
|
|
|
if (colon != authority->ptr) {
|
|
|
|
for(ptr = colon - 1, i = 1; ptr > authority->ptr; --ptr) {
|
2005-02-20 14:27:00 +00:00
|
|
|
if(*ptr == '.') {
|
|
|
|
if (ptr != colon - 1) {
|
|
|
|
/* is something between the dots */
|
2018-09-21 20:56:08 +00:00
|
|
|
buffer_copy_string_len(key, CONST_STR_LEN("%"));
|
|
|
|
buffer_append_int(key, i++);
|
[multiple] reduce redundant NULL buffer checks
This commit is a large set of code changes and results in removal of
hundreds, perhaps thousands, of CPU instructions, a portion of which
are on hot code paths.
Most (buffer *) used by lighttpd are not NULL, especially since buffers
were inlined into numerous larger structs such as request_st and chunk.
In the small number of instances where that is not the case, a NULL
check is often performed earlier in a function where that buffer is
later used with a buffer_* func. In the handful of cases that remained,
a NULL check was added, e.g. with r->http_host and r->conf.server_tag.
- check for empty strings at config time and set value to NULL if blank
string will be ignored at runtime; at runtime, simple pointer check
for NULL can be used to check for a value that has been set and is not
blank ("")
- use buffer_is_blank() instead of buffer_string_is_empty(),
and use buffer_is_unset() instead of buffer_is_empty(),
where buffer is known not to be NULL so that NULL check can be skipped
- use buffer_clen() instead of buffer_string_length() when buffer is
known not to be NULL (to avoid NULL check at runtime)
- use buffer_truncate() instead of buffer_string_set_length() to
truncate string, and use buffer_extend() to extend
Examples where buffer known not to be NULL:
- cpv->v.b from config_plugin_values_init is not NULL if T_CONFIG_BOOL
(though we might set it to NULL if buffer_is_blank(cpv->v.b))
- address of buffer is arg (&foo)
(compiler optimizer detects this in most, but not all, cases)
- buffer is checked for NULL earlier in func
- buffer is accessed in same scope without a NULL check (e.g. b->ptr)
internal behavior change:
callers must not pass a NULL buffer to some funcs.
- buffer_init_buffer() requires non-null args
- buffer_copy_buffer() requires non-null args
- buffer_append_string_buffer() requires non-null args
- buffer_string_space() requires non-null arg
2021-06-09 02:57:36 +00:00
|
|
|
array_set_key_value(host, BUF_PTR_LEN(key), ptr+1, colon-ptr-1);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
colon = ptr;
|
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2020-02-22 22:24:12 +00:00
|
|
|
/* if the . is not the first character of the hostname */
|
2005-02-20 14:27:00 +00:00
|
|
|
if (colon != ptr) {
|
2018-09-21 20:56:08 +00:00
|
|
|
buffer_copy_string_len(key, CONST_STR_LEN("%"));
|
|
|
|
buffer_append_int(key, i /* ++ */);
|
[multiple] reduce redundant NULL buffer checks
This commit is a large set of code changes and results in removal of
hundreds, perhaps thousands, of CPU instructions, a portion of which
are on hot code paths.
Most (buffer *) used by lighttpd are not NULL, especially since buffers
were inlined into numerous larger structs such as request_st and chunk.
In the small number of instances where that is not the case, a NULL
check is often performed earlier in a function where that buffer is
later used with a buffer_* func. In the handful of cases that remained,
a NULL check was added, e.g. with r->http_host and r->conf.server_tag.
- check for empty strings at config time and set value to NULL if blank
string will be ignored at runtime; at runtime, simple pointer check
for NULL can be used to check for a value that has been set and is not
blank ("")
- use buffer_is_blank() instead of buffer_string_is_empty(),
and use buffer_is_unset() instead of buffer_is_empty(),
where buffer is known not to be NULL so that NULL check can be skipped
- use buffer_clen() instead of buffer_string_length() when buffer is
known not to be NULL (to avoid NULL check at runtime)
- use buffer_truncate() instead of buffer_string_set_length() to
truncate string, and use buffer_extend() to extend
Examples where buffer known not to be NULL:
- cpv->v.b from config_plugin_values_init is not NULL if T_CONFIG_BOOL
(though we might set it to NULL if buffer_is_blank(cpv->v.b))
- address of buffer is arg (&foo)
(compiler optimizer detects this in most, but not all, cases)
- buffer is checked for NULL earlier in func
- buffer is accessed in same scope without a NULL check (e.g. b->ptr)
internal behavior change:
callers must not pass a NULL buffer to some funcs.
- buffer_init_buffer() requires non-null args
- buffer_copy_buffer() requires non-null args
- buffer_append_string_buffer() requires non-null args
- buffer_string_space() requires non-null arg
2021-06-09 02:57:36 +00:00
|
|
|
array_set_key_value(host, BUF_PTR_LEN(key), ptr, colon-ptr);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-03 00:44:16 +00:00
|
|
|
static void mod_evhost_build_doc_root_path(buffer *b, array *parsed_host, buffer *authority, const buffer *path_pieces) {
|
2018-12-03 05:35:00 +00:00
|
|
|
array_reset_data_strings(parsed_host);
|
|
|
|
mod_evhost_parse_host(b, parsed_host, authority);
|
|
|
|
buffer_clear(b);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2019-11-03 00:44:16 +00:00
|
|
|
for (const char *ptr; (ptr = path_pieces->ptr); ++path_pieces) {
|
2005-02-20 14:27:00 +00:00
|
|
|
if (*ptr == '%') {
|
2019-10-10 03:24:25 +00:00
|
|
|
const data_string *ds;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (*(ptr+1) == '%') {
|
|
|
|
/* %% */
|
2018-12-03 05:35:00 +00:00
|
|
|
buffer_append_string_len(b, CONST_STR_LEN("%"));
|
2009-04-26 20:19:31 +00:00
|
|
|
} else if (*(ptr+1) == '_' ) {
|
|
|
|
/* %_ == full hostname */
|
2018-12-03 05:35:00 +00:00
|
|
|
char *colon = strchr(authority->ptr, ':');
|
2009-04-26 20:19:31 +00:00
|
|
|
|
|
|
|
if(colon == NULL) {
|
2018-12-03 05:35:00 +00:00
|
|
|
buffer_append_string_buffer(b, authority); /* adds fqdn */
|
2009-04-26 20:19:31 +00:00
|
|
|
} else {
|
|
|
|
/* strip the port out of the authority-part of the URI scheme */
|
2018-12-03 05:35:00 +00:00
|
|
|
buffer_append_string_len(b, authority->ptr, colon - authority->ptr); /* adds fqdn */
|
2009-04-26 20:19:31 +00:00
|
|
|
}
|
2016-10-20 17:50:39 +00:00
|
|
|
} else if (ptr[1] == '{' ) {
|
|
|
|
char s[3] = "% ";
|
|
|
|
s[1] = ptr[2]; /*(assumes single digit before '.', and, optionally, '.' and single digit after '.')*/
|
2017-05-12 03:15:29 +00:00
|
|
|
if (NULL != (ds = (data_string *)array_get_element_klen(parsed_host, s, 2))) {
|
2016-10-20 17:50:39 +00:00
|
|
|
if (ptr[3] != '.' || ptr[4] == '0') {
|
2019-10-13 08:59:57 +00:00
|
|
|
buffer_append_string_buffer(b, &ds->value);
|
2016-10-20 17:50:39 +00:00
|
|
|
} else {
|
[multiple] reduce redundant NULL buffer checks
This commit is a large set of code changes and results in removal of
hundreds, perhaps thousands, of CPU instructions, a portion of which
are on hot code paths.
Most (buffer *) used by lighttpd are not NULL, especially since buffers
were inlined into numerous larger structs such as request_st and chunk.
In the small number of instances where that is not the case, a NULL
check is often performed earlier in a function where that buffer is
later used with a buffer_* func. In the handful of cases that remained,
a NULL check was added, e.g. with r->http_host and r->conf.server_tag.
- check for empty strings at config time and set value to NULL if blank
string will be ignored at runtime; at runtime, simple pointer check
for NULL can be used to check for a value that has been set and is not
blank ("")
- use buffer_is_blank() instead of buffer_string_is_empty(),
and use buffer_is_unset() instead of buffer_is_empty(),
where buffer is known not to be NULL so that NULL check can be skipped
- use buffer_clen() instead of buffer_string_length() when buffer is
known not to be NULL (to avoid NULL check at runtime)
- use buffer_truncate() instead of buffer_string_set_length() to
truncate string, and use buffer_extend() to extend
Examples where buffer known not to be NULL:
- cpv->v.b from config_plugin_values_init is not NULL if T_CONFIG_BOOL
(though we might set it to NULL if buffer_is_blank(cpv->v.b))
- address of buffer is arg (&foo)
(compiler optimizer detects this in most, but not all, cases)
- buffer is checked for NULL earlier in func
- buffer is accessed in same scope without a NULL check (e.g. b->ptr)
internal behavior change:
callers must not pass a NULL buffer to some funcs.
- buffer_init_buffer() requires non-null args
- buffer_copy_buffer() requires non-null args
- buffer_append_string_buffer() requires non-null args
- buffer_string_space() requires non-null arg
2021-06-09 02:57:36 +00:00
|
|
|
if ((size_t)(ptr[4]-'0') <= buffer_clen(&ds->value)) {
|
2019-10-13 08:59:57 +00:00
|
|
|
buffer_append_string_len(b, ds->value.ptr+(ptr[4]-'0')-1, 1);
|
2016-10-20 17:50:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* unhandled %-sequence */
|
|
|
|
}
|
[multiple] reduce redundant NULL buffer checks
This commit is a large set of code changes and results in removal of
hundreds, perhaps thousands, of CPU instructions, a portion of which
are on hot code paths.
Most (buffer *) used by lighttpd are not NULL, especially since buffers
were inlined into numerous larger structs such as request_st and chunk.
In the small number of instances where that is not the case, a NULL
check is often performed earlier in a function where that buffer is
later used with a buffer_* func. In the handful of cases that remained,
a NULL check was added, e.g. with r->http_host and r->conf.server_tag.
- check for empty strings at config time and set value to NULL if blank
string will be ignored at runtime; at runtime, simple pointer check
for NULL can be used to check for a value that has been set and is not
blank ("")
- use buffer_is_blank() instead of buffer_string_is_empty(),
and use buffer_is_unset() instead of buffer_is_empty(),
where buffer is known not to be NULL so that NULL check can be skipped
- use buffer_clen() instead of buffer_string_length() when buffer is
known not to be NULL (to avoid NULL check at runtime)
- use buffer_truncate() instead of buffer_string_set_length() to
truncate string, and use buffer_extend() to extend
Examples where buffer known not to be NULL:
- cpv->v.b from config_plugin_values_init is not NULL if T_CONFIG_BOOL
(though we might set it to NULL if buffer_is_blank(cpv->v.b))
- address of buffer is arg (&foo)
(compiler optimizer detects this in most, but not all, cases)
- buffer is checked for NULL earlier in func
- buffer is accessed in same scope without a NULL check (e.g. b->ptr)
internal behavior change:
callers must not pass a NULL buffer to some funcs.
- buffer_init_buffer() requires non-null args
- buffer_copy_buffer() requires non-null args
- buffer_append_string_buffer() requires non-null args
- buffer_string_space() requires non-null arg
2021-06-09 02:57:36 +00:00
|
|
|
} else if (NULL != (ds = (data_string *)array_get_element_klen(parsed_host, BUF_PTR_LEN(path_pieces)))) {
|
2019-10-13 08:59:57 +00:00
|
|
|
buffer_append_string_buffer(b, &ds->value);
|
2005-02-20 14:27:00 +00:00
|
|
|
} else {
|
|
|
|
/* unhandled %-sequence */
|
|
|
|
}
|
|
|
|
} else {
|
2019-11-03 00:44:16 +00:00
|
|
|
buffer_append_string_buffer(b, path_pieces);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2018-12-03 05:35:00 +00:00
|
|
|
buffer_append_slash(b);
|
2018-12-03 05:03:12 +00:00
|
|
|
}
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
static handler_t mod_evhost_uri_handler(request_st * const r, void *p_d) {
|
2018-12-03 05:03:12 +00:00
|
|
|
plugin_data *p = p_d;
|
|
|
|
|
|
|
|
/* not authority set */
|
[multiple] reduce redundant NULL buffer checks
This commit is a large set of code changes and results in removal of
hundreds, perhaps thousands, of CPU instructions, a portion of which
are on hot code paths.
Most (buffer *) used by lighttpd are not NULL, especially since buffers
were inlined into numerous larger structs such as request_st and chunk.
In the small number of instances where that is not the case, a NULL
check is often performed earlier in a function where that buffer is
later used with a buffer_* func. In the handful of cases that remained,
a NULL check was added, e.g. with r->http_host and r->conf.server_tag.
- check for empty strings at config time and set value to NULL if blank
string will be ignored at runtime; at runtime, simple pointer check
for NULL can be used to check for a value that has been set and is not
blank ("")
- use buffer_is_blank() instead of buffer_string_is_empty(),
and use buffer_is_unset() instead of buffer_is_empty(),
where buffer is known not to be NULL so that NULL check can be skipped
- use buffer_clen() instead of buffer_string_length() when buffer is
known not to be NULL (to avoid NULL check at runtime)
- use buffer_truncate() instead of buffer_string_set_length() to
truncate string, and use buffer_extend() to extend
Examples where buffer known not to be NULL:
- cpv->v.b from config_plugin_values_init is not NULL if T_CONFIG_BOOL
(though we might set it to NULL if buffer_is_blank(cpv->v.b))
- address of buffer is arg (&foo)
(compiler optimizer detects this in most, but not all, cases)
- buffer is checked for NULL earlier in func
- buffer is accessed in same scope without a NULL check (e.g. b->ptr)
internal behavior change:
callers must not pass a NULL buffer to some funcs.
- buffer_init_buffer() requires non-null args
- buffer_copy_buffer() requires non-null args
- buffer_append_string_buffer() requires non-null args
- buffer_string_space() requires non-null arg
2021-06-09 02:57:36 +00:00
|
|
|
if (buffer_is_blank(&r->uri.authority)) return HANDLER_GO_ON;
|
2018-12-03 05:03:12 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
mod_evhost_patch_config(r, p);
|
2018-12-03 05:03:12 +00:00
|
|
|
|
|
|
|
/* missing even default(global) conf */
|
2019-11-03 00:44:16 +00:00
|
|
|
if (NULL == p->conf.path_pieces) return HANDLER_GO_ON;
|
2018-12-03 05:03:12 +00:00
|
|
|
|
2021-09-13 03:05:01 +00:00
|
|
|
buffer * const b = r->tmp_buf;/*(tmp_buf cleared before use in call below)*/
|
2020-01-13 02:51:12 +00:00
|
|
|
mod_evhost_build_doc_root_path(b, &p->split_vals, &r->uri.authority, p->conf.path_pieces);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2020-10-09 17:33:54 +00:00
|
|
|
if (!stat_cache_path_isdir(b)) {
|
2020-01-13 02:51:12 +00:00
|
|
|
log_perror(r->conf.errh, __FILE__, __LINE__, "%s", b->ptr);
|
2018-12-03 05:35:00 +00:00
|
|
|
} else {
|
2020-01-13 02:51:12 +00:00
|
|
|
buffer_copy_buffer(&r->physical.doc_root, b);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return HANDLER_GO_ON;
|
|
|
|
}
|
|
|
|
|
2009-03-07 21:05:37 +00:00
|
|
|
int mod_evhost_plugin_init(plugin *p);
|
2005-02-20 14:27:00 +00:00
|
|
|
int mod_evhost_plugin_init(plugin *p) {
|
|
|
|
p->version = LIGHTTPD_VERSION_ID;
|
2019-10-19 04:30:54 +00:00
|
|
|
p->name = "evhost";
|
2005-02-20 14:27:00 +00:00
|
|
|
p->init = mod_evhost_init;
|
|
|
|
p->set_defaults = mod_evhost_set_defaults;
|
|
|
|
p->handle_docroot = mod_evhost_uri_handler;
|
|
|
|
p->cleanup = mod_evhost_free;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|