2016-03-19 15:14:35 +00:00
|
|
|
#include "first.h"
|
|
|
|
|
2017-06-20 03:00:45 +00:00
|
|
|
#include "fdevent.h"
|
2021-09-12 23:13:44 +00:00
|
|
|
#include "fdlog.h"
|
2005-02-20 14:27:00 +00:00
|
|
|
#include "log.h"
|
2021-11-09 20:00:36 +00:00
|
|
|
#include "array.h"
|
2005-02-20 14:27:00 +00:00
|
|
|
#include "buffer.h"
|
2021-11-09 20:00:36 +00:00
|
|
|
#include "chunk.h"
|
2021-03-05 00:11:40 +00:00
|
|
|
#include "http_cgi.h"
|
2021-03-24 12:36:32 +00:00
|
|
|
#include "http_chunk.h"
|
2020-12-25 08:56:39 +00:00
|
|
|
#include "http_etag.h"
|
2018-09-09 05:50:33 +00:00
|
|
|
#include "http_header.h"
|
2021-08-02 09:11:36 +00:00
|
|
|
#include "request.h"
|
2019-04-30 00:20:47 +00:00
|
|
|
#include "stat_cache.h"
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
#include "plugin.h"
|
|
|
|
|
|
|
|
#include "response.h"
|
|
|
|
|
|
|
|
#include "sys-socket.h"
|
2020-11-22 07:41:11 +00:00
|
|
|
#include "sys-time.h"
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2009-10-11 14:31:42 +00:00
|
|
|
#include <sys/types.h>
|
2018-03-25 07:45:05 +00:00
|
|
|
#include <sys/stat.h>
|
2020-11-19 08:21:22 +00:00
|
|
|
#ifdef HAVE_SYS_WAIT_H
|
2017-06-20 03:00:45 +00:00
|
|
|
#include <sys/wait.h>
|
2020-11-19 08:21:22 +00:00
|
|
|
#endif
|
2009-10-11 14:31:42 +00:00
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2016-04-18 03:37:40 +00:00
|
|
|
#include <fcntl.h>
|
2009-10-11 14:31:42 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
#ifdef HAVE_PWD_H
|
2009-10-11 14:31:42 +00:00
|
|
|
# include <pwd.h>
|
2005-02-20 14:27:00 +00:00
|
|
|
#endif
|
|
|
|
|
2005-10-18 10:38:11 +00:00
|
|
|
#ifdef HAVE_SYS_FILIO_H
|
2009-10-11 14:31:42 +00:00
|
|
|
# include <sys/filio.h>
|
2005-10-18 10:38:11 +00:00
|
|
|
#endif
|
|
|
|
|
2021-11-09 20:00:36 +00:00
|
|
|
typedef struct {
|
|
|
|
const array *ssi_extension;
|
|
|
|
const buffer *content_type;
|
|
|
|
unsigned short conditional_requests;
|
|
|
|
unsigned short ssi_exec;
|
|
|
|
unsigned short ssi_recursion_max;
|
|
|
|
} plugin_config;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
PLUGIN_DATA;
|
|
|
|
plugin_config defaults;
|
|
|
|
plugin_config conf;
|
|
|
|
array *ssi_vars;
|
|
|
|
array *ssi_cgi_env;
|
|
|
|
buffer stat_fn;
|
|
|
|
buffer timefmt;
|
|
|
|
} plugin_data;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
array *ssi_vars;
|
|
|
|
array *ssi_cgi_env;
|
|
|
|
buffer *stat_fn;
|
|
|
|
buffer *timefmt;
|
|
|
|
int sizefmt;
|
|
|
|
|
|
|
|
int if_level, if_is_false_level, if_is_false, if_is_false_endif;
|
|
|
|
unsigned short ssi_recursion_depth;
|
|
|
|
|
|
|
|
chunkqueue wq;
|
|
|
|
log_error_st *errh;
|
|
|
|
plugin_config conf;
|
|
|
|
} handler_ctx;
|
|
|
|
|
2019-11-25 06:54:08 +00:00
|
|
|
static handler_ctx * handler_ctx_init(plugin_data *p, log_error_st *errh) {
|
2016-12-06 05:25:56 +00:00
|
|
|
handler_ctx *hctx = calloc(1, sizeof(*hctx));
|
|
|
|
force_assert(hctx);
|
2019-11-25 06:54:08 +00:00
|
|
|
hctx->errh = errh;
|
2021-03-19 06:07:33 +00:00
|
|
|
hctx->timefmt = &p->timefmt;
|
|
|
|
hctx->stat_fn = &p->stat_fn;
|
2016-12-06 05:25:56 +00:00
|
|
|
hctx->ssi_vars = p->ssi_vars;
|
|
|
|
hctx->ssi_cgi_env = p->ssi_cgi_env;
|
|
|
|
memcpy(&hctx->conf, &p->conf, sizeof(plugin_config));
|
|
|
|
return hctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handler_ctx_free(handler_ctx *hctx) {
|
2021-03-24 12:36:32 +00:00
|
|
|
chunkqueue_reset(&hctx->wq);
|
2016-12-06 05:25:56 +00:00
|
|
|
free(hctx);
|
|
|
|
}
|
|
|
|
|
2008-01-18 09:21:07 +00:00
|
|
|
/* The newest modified time of included files for include statement */
|
2021-07-12 18:46:49 +00:00
|
|
|
static volatile unix_time64_t include_file_last_mtime = 0;
|
2008-01-18 09:21:07 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
INIT_FUNC(mod_ssi_init) {
|
|
|
|
plugin_data *p;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
p = calloc(1, sizeof(*p));
|
2019-10-27 02:34:08 +00:00
|
|
|
force_assert(p);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2019-11-22 05:57:48 +00:00
|
|
|
p->ssi_vars = array_init(8);
|
|
|
|
p->ssi_cgi_env = array_init(32);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
FREE_FUNC(mod_ssi_free) {
|
|
|
|
plugin_data *p = p_d;
|
|
|
|
array_free(p->ssi_vars);
|
|
|
|
array_free(p->ssi_cgi_env);
|
2021-03-19 06:07:33 +00:00
|
|
|
free(p->timefmt.ptr);
|
|
|
|
free(p->stat_fn.ptr);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2019-10-27 02:34:08 +00:00
|
|
|
static void mod_ssi_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: /* ssi.extension */
|
|
|
|
pconf->ssi_extension = cpv->v.a;
|
|
|
|
break;
|
|
|
|
case 1: /* ssi.content-type */
|
|
|
|
pconf->content_type = cpv->v.b;
|
|
|
|
break;
|
|
|
|
case 2: /* ssi.conditional-requests */
|
|
|
|
pconf->conditional_requests = cpv->v.u;
|
|
|
|
break;
|
|
|
|
case 3: /* ssi.exec */
|
|
|
|
pconf->ssi_exec = cpv->v.u;
|
|
|
|
break;
|
|
|
|
case 4: /* ssi.recursion-max */
|
|
|
|
pconf->ssi_recursion_max = cpv->v.shrt;
|
|
|
|
break;
|
|
|
|
default:/* should not happen */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2019-10-27 02:34:08 +00:00
|
|
|
static void mod_ssi_merge_config(plugin_config * const pconf, const config_plugin_value_t *cpv) {
|
|
|
|
do {
|
|
|
|
mod_ssi_merge_config_cpv(pconf, cpv);
|
|
|
|
} while ((++cpv)->k_id != -1);
|
|
|
|
}
|
2017-03-05 20:39:45 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
static void mod_ssi_patch_config(request_st * const r, plugin_data * const p) {
|
2019-10-27 02:34:08 +00:00
|
|
|
memcpy(&p->conf, &p->defaults, sizeof(plugin_config));
|
|
|
|
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-10-27 02:34:08 +00:00
|
|
|
mod_ssi_merge_config(&p->conf, p->cvlist + p->cvlist[i].v.u2[0]);
|
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2019-10-27 02:34:08 +00:00
|
|
|
SETDEFAULTS_FUNC(mod_ssi_set_defaults) {
|
|
|
|
static const config_plugin_keys_t cpk[] = {
|
|
|
|
{ CONST_STR_LEN("ssi.extension"),
|
2019-12-08 00:15:55 +00:00
|
|
|
T_CONFIG_ARRAY_VLIST,
|
2019-10-27 02:34:08 +00:00
|
|
|
T_CONFIG_SCOPE_CONNECTION }
|
|
|
|
,{ CONST_STR_LEN("ssi.content-type"),
|
|
|
|
T_CONFIG_STRING,
|
|
|
|
T_CONFIG_SCOPE_CONNECTION }
|
|
|
|
,{ CONST_STR_LEN("ssi.conditional-requests"),
|
|
|
|
T_CONFIG_BOOL,
|
|
|
|
T_CONFIG_SCOPE_CONNECTION }
|
|
|
|
,{ CONST_STR_LEN("ssi.exec"),
|
|
|
|
T_CONFIG_BOOL,
|
|
|
|
T_CONFIG_SCOPE_CONNECTION }
|
|
|
|
,{ CONST_STR_LEN("ssi.recursion-max"),
|
|
|
|
T_CONFIG_SHORT,
|
|
|
|
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_ssi"))
|
|
|
|
return HANDLER_ERROR;
|
|
|
|
|
[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
|
|
|
/* 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: /* ssi.extension */
|
|
|
|
break;
|
|
|
|
case 1: /* ssi.content-type */
|
|
|
|
if (buffer_is_blank(cpv->v.b))
|
|
|
|
cpv->v.b = NULL;
|
|
|
|
break;
|
|
|
|
case 2: /* ssi.conditional-requests */
|
|
|
|
case 3: /* ssi.exec */
|
|
|
|
case 4: /* ssi.recursion-max */
|
|
|
|
break;
|
|
|
|
default:/* should not happen */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-27 02:34:08 +00:00
|
|
|
p->defaults.ssi_exec = 1;
|
|
|
|
|
|
|
|
/* 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_ssi_merge_config(&p->defaults, cpv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return HANDLER_GO_ON;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2016-04-18 03:37:40 +00:00
|
|
|
|
2021-11-09 20:00:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
#define TK_AND 1
|
|
|
|
#define TK_OR 2
|
|
|
|
#define TK_EQ 3
|
|
|
|
#define TK_NE 4
|
|
|
|
#define TK_GT 5
|
|
|
|
#define TK_GE 6
|
|
|
|
#define TK_LT 7
|
|
|
|
#define TK_LE 8
|
|
|
|
#define TK_NOT 9
|
|
|
|
#define TK_LPARAN 10
|
|
|
|
#define TK_RPARAN 11
|
|
|
|
#define TK_VALUE 12
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
const char *input;
|
|
|
|
size_t offset;
|
|
|
|
size_t size;
|
|
|
|
int in_brace;
|
|
|
|
int depth;
|
|
|
|
handler_ctx *p;
|
|
|
|
} ssi_tokenizer_t;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
buffer str;
|
|
|
|
enum { SSI_TYPE_UNSET, SSI_TYPE_BOOL, SSI_TYPE_STRING } type;
|
|
|
|
int bo;
|
|
|
|
} ssi_val_t;
|
|
|
|
|
|
|
|
__attribute_pure__
|
|
|
|
static int ssi_val_tobool(const ssi_val_t *B) {
|
|
|
|
return B->type == SSI_TYPE_BOOL ? B->bo : !buffer_is_blank(&B->str);
|
|
|
|
}
|
|
|
|
|
|
|
|
__attribute_pure__
|
|
|
|
static int ssi_eval_expr_cmp(const ssi_val_t * const v1, const ssi_val_t * const v2, const int cond) {
|
|
|
|
int cmp = (v1->type != SSI_TYPE_BOOL && v2->type != SSI_TYPE_BOOL)
|
|
|
|
? strcmp(v1->str.ptr ? v1->str.ptr : "",
|
|
|
|
v2->str.ptr ? v2->str.ptr : "")
|
|
|
|
: ssi_val_tobool(v1) - ssi_val_tobool(v2);
|
|
|
|
switch (cond) {
|
|
|
|
case TK_EQ: return (cmp == 0);
|
|
|
|
case TK_NE: return (cmp != 0);
|
|
|
|
case TK_GE: return (cmp >= 0);
|
|
|
|
case TK_GT: return (cmp > 0);
|
|
|
|
case TK_LE: return (cmp <= 0);
|
|
|
|
case TK_LT: return (cmp < 0);
|
|
|
|
default: return 0;/*(should not happen)*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
__attribute_pure__
|
|
|
|
static int ssi_eval_expr_cmp_bool(const ssi_val_t * const v1, const ssi_val_t * const v2, const int cond) {
|
|
|
|
return (cond == TK_OR)
|
|
|
|
? ssi_val_tobool(v1) || ssi_val_tobool(v2) /* TK_OR */
|
|
|
|
: ssi_val_tobool(v1) && ssi_val_tobool(v2); /* TK_AND */
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ssi_eval_expr_append_val(buffer * const b, const char *s, const size_t slen) {
|
|
|
|
if (buffer_is_blank(b))
|
|
|
|
buffer_append_string_len(b, s, slen);
|
|
|
|
else if (slen)
|
|
|
|
buffer_append_str2(b, CONST_STR_LEN(" "), s, slen);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ssi_expr_tokenizer(ssi_tokenizer_t * const t, buffer * const token) {
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
while (t->offset < t->size
|
|
|
|
&& (t->input[t->offset] == ' ' || t->input[t->offset] == '\t')) {
|
|
|
|
++t->offset;
|
|
|
|
}
|
|
|
|
if (t->offset >= t->size)
|
|
|
|
return 0;
|
|
|
|
if (t->input[t->offset] == '\0') {
|
|
|
|
log_error(t->p->errh, __FILE__, __LINE__,
|
|
|
|
"pos: %zu foobar", t->offset+1);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (t->input[t->offset]) {
|
|
|
|
case '=':
|
|
|
|
#if 0 /*(maybe accept "==", too)*/
|
|
|
|
if (t->input[t->offset + 1] == '=')
|
|
|
|
++t->offset;
|
|
|
|
#endif
|
|
|
|
t->offset++;
|
|
|
|
return TK_EQ;
|
|
|
|
case '>':
|
|
|
|
if (t->input[t->offset + 1] == '=') {
|
|
|
|
t->offset += 2;
|
|
|
|
return TK_GE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
t->offset += 1;
|
|
|
|
return TK_GT;
|
|
|
|
}
|
|
|
|
case '<':
|
|
|
|
if (t->input[t->offset + 1] == '=') {
|
|
|
|
t->offset += 2;
|
|
|
|
return TK_LE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
t->offset += 1;
|
|
|
|
return TK_LT;
|
|
|
|
}
|
|
|
|
case '!':
|
|
|
|
if (t->input[t->offset + 1] == '=') {
|
|
|
|
t->offset += 2;
|
|
|
|
return TK_NE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
t->offset += 1;
|
|
|
|
return TK_NOT;
|
|
|
|
}
|
|
|
|
case '&':
|
|
|
|
if (t->input[t->offset + 1] == '&') {
|
|
|
|
t->offset += 2;
|
|
|
|
return TK_AND;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
log_error(t->p->errh, __FILE__, __LINE__,
|
|
|
|
"pos: %zu missing second &", t->offset+1);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
case '|':
|
|
|
|
if (t->input[t->offset + 1] == '|') {
|
|
|
|
t->offset += 2;
|
|
|
|
return TK_OR;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
log_error(t->p->errh, __FILE__, __LINE__,
|
|
|
|
"pos: %zu missing second |", t->offset+1);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
case '(':
|
|
|
|
t->offset++;
|
|
|
|
t->in_brace++;
|
|
|
|
return TK_LPARAN;
|
|
|
|
case ')':
|
|
|
|
t->offset++;
|
|
|
|
t->in_brace--;
|
|
|
|
return TK_RPARAN;
|
|
|
|
case '\'':
|
|
|
|
/* search for the terminating "'" */
|
|
|
|
i = 1;
|
|
|
|
while (t->input[t->offset + i] && t->input[t->offset + i] != '\'')
|
|
|
|
++i;
|
|
|
|
if (t->input[t->offset + i]) {
|
|
|
|
ssi_eval_expr_append_val(token, t->input + t->offset + 1, i-1);
|
|
|
|
t->offset += i + 1;
|
|
|
|
return TK_VALUE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
log_error(t->p->errh, __FILE__, __LINE__,
|
|
|
|
"pos: %zu missing closing quote", t->offset+1);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
case '$': {
|
|
|
|
const char *var;
|
|
|
|
size_t varlen;
|
|
|
|
if (t->input[t->offset + 1] == '{') {
|
|
|
|
i = 2;
|
|
|
|
while (t->input[t->offset + i] && t->input[t->offset + i] != '}')
|
|
|
|
++i;
|
|
|
|
if (t->input[t->offset + i] != '}') {
|
|
|
|
log_error(t->p->errh, __FILE__, __LINE__,
|
|
|
|
"pos: %zu missing closing curly-brace", t->offset+1);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
++i; /* step past '}' */
|
|
|
|
var = t->input + t->offset + 2;
|
|
|
|
varlen = i-3;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for (i = 1; light_isalpha(t->input[t->offset + i]) ||
|
|
|
|
t->input[t->offset + i] == '_' ||
|
|
|
|
((i > 1) && light_isdigit(t->input[t->offset + i])); ++i) ;
|
|
|
|
var = t->input + t->offset + 1;
|
|
|
|
varlen = i-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const data_string *ds;
|
|
|
|
if ((ds = (const data_string *)
|
|
|
|
array_get_element_klen(t->p->ssi_cgi_env, var, varlen))
|
|
|
|
|| (ds = (const data_string *)
|
|
|
|
array_get_element_klen(t->p->ssi_vars, var, varlen)))
|
|
|
|
ssi_eval_expr_append_val(token, BUF_PTR_LEN(&ds->value));
|
|
|
|
t->offset += i;
|
|
|
|
return TK_VALUE;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
for (i = 0; isgraph(((unsigned char *)t->input)[t->offset + i]); ++i) {
|
|
|
|
char d = t->input[t->offset + i];
|
|
|
|
switch(d) {
|
|
|
|
default: continue;
|
|
|
|
case ' ':
|
|
|
|
case '\t':
|
|
|
|
case ')':
|
|
|
|
case '(':
|
|
|
|
case '\'':
|
|
|
|
case '=':
|
|
|
|
case '!':
|
|
|
|
case '<':
|
|
|
|
case '>':
|
|
|
|
case '&':
|
|
|
|
case '|':
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ssi_eval_expr_append_val(token, t->input + t->offset, i);
|
|
|
|
t->offset += i;
|
|
|
|
return TK_VALUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ssi_eval_expr_loop(ssi_tokenizer_t * const t, ssi_val_t * const v);
|
|
|
|
|
|
|
|
static int ssi_eval_expr_step(ssi_tokenizer_t * const t, ssi_val_t * const v) {
|
|
|
|
buffer_clear(&v->str);
|
|
|
|
v->type = SSI_TYPE_UNSET; /*(not SSI_TYPE_BOOL)*/
|
|
|
|
int next;
|
|
|
|
const int level = t->in_brace;
|
|
|
|
switch ((next = ssi_expr_tokenizer(t, &v->str))) {
|
|
|
|
case TK_VALUE:
|
|
|
|
do { next = ssi_expr_tokenizer(t, &v->str); } while (next == TK_VALUE);
|
|
|
|
return next;
|
|
|
|
case TK_LPARAN:
|
|
|
|
if (t->in_brace > 16) return -1; /*(arbitrary limit)*/
|
|
|
|
next = ssi_eval_expr_loop(t, v);
|
|
|
|
if (next == TK_RPARAN && level == t->in_brace) {
|
|
|
|
int result = ssi_val_tobool(v);
|
|
|
|
next = ssi_eval_expr_step(t, v); /*(resets v)*/
|
|
|
|
v->bo = result;
|
|
|
|
v->type = SSI_TYPE_BOOL;
|
|
|
|
return (next==TK_AND || next==TK_OR || next==TK_RPARAN || 0==next)
|
|
|
|
? next
|
|
|
|
: -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
case TK_RPARAN:
|
|
|
|
return t->in_brace >= 0 ? TK_RPARAN : -1;
|
|
|
|
case TK_NOT:
|
|
|
|
if (++t->depth > 16) return -1; /*(arbitrary limit)*/
|
|
|
|
next = ssi_eval_expr_step(t, v);
|
|
|
|
--t->depth;
|
|
|
|
if (-1 == next) return next;
|
|
|
|
v->bo = !ssi_val_tobool(v);
|
|
|
|
v->type = SSI_TYPE_BOOL;
|
|
|
|
return next;
|
|
|
|
default:
|
|
|
|
return next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ssi_eval_expr_loop_cmp(ssi_tokenizer_t * const t, ssi_val_t * const v1, int cond) {
|
|
|
|
ssi_val_t v2 = { { NULL, 0, 0 }, SSI_TYPE_UNSET, 0 };
|
|
|
|
int next = ssi_eval_expr_step(t, &v2);
|
|
|
|
if (-1 != next) {
|
|
|
|
v1->bo = ssi_eval_expr_cmp(v1, &v2, cond);
|
|
|
|
v1->type = SSI_TYPE_BOOL;
|
|
|
|
}
|
|
|
|
buffer_free_ptr(&v2.str);
|
|
|
|
return next;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ssi_eval_expr_loop(ssi_tokenizer_t * const t, ssi_val_t * const v1) {
|
|
|
|
int next = ssi_eval_expr_step(t, v1);
|
|
|
|
switch (next) {
|
|
|
|
case TK_AND: case TK_OR:
|
|
|
|
break;
|
|
|
|
case TK_EQ: case TK_NE:
|
|
|
|
case TK_GT: case TK_GE:
|
|
|
|
case TK_LT: case TK_LE:
|
|
|
|
next = ssi_eval_expr_loop_cmp(t, v1, next);
|
|
|
|
if (next == TK_RPARAN || 0 == next) return next;
|
|
|
|
if (next != TK_AND && next != TK_OR) {
|
|
|
|
log_error(t->p->errh, __FILE__, __LINE__,
|
|
|
|
"pos: %zu parser failed somehow near here", t->offset+1);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*(Note: '&&' and '||' evaluations are not short-circuited)*/
|
|
|
|
ssi_val_t v2 = { { NULL, 0, 0 }, SSI_TYPE_UNSET, 0 };
|
|
|
|
do {
|
|
|
|
int cond = next;
|
|
|
|
next = ssi_eval_expr_step(t, &v2);
|
|
|
|
switch (next) {
|
|
|
|
case TK_AND: case TK_OR: case 0:
|
|
|
|
break;
|
|
|
|
case TK_EQ: case TK_NE:
|
|
|
|
case TK_GT: case TK_GE:
|
|
|
|
case TK_LT: case TK_LE:
|
|
|
|
next = ssi_eval_expr_loop_cmp(t, &v2, next);
|
|
|
|
if (-1 == next) continue;
|
|
|
|
break;
|
|
|
|
case TK_RPARAN:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
v1->bo = ssi_eval_expr_cmp_bool(v1, &v2, cond);
|
|
|
|
v1->type = SSI_TYPE_BOOL;
|
|
|
|
} while (next == TK_AND || next == TK_OR);
|
|
|
|
buffer_free_ptr(&v2.str);
|
|
|
|
return next;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ssi_eval_expr(handler_ctx *p, const char *expr) {
|
|
|
|
ssi_tokenizer_t t;
|
|
|
|
t.input = expr;
|
|
|
|
t.offset = 0;
|
|
|
|
t.size = strlen(expr);
|
|
|
|
t.in_brace = 0;
|
|
|
|
t.depth = 0;
|
|
|
|
t.p = p;
|
|
|
|
|
|
|
|
ssi_val_t v = { { NULL, 0, 0 }, SSI_TYPE_UNSET, 0 };
|
|
|
|
int rc = ssi_eval_expr_loop(&t, &v);
|
|
|
|
rc = (0 == rc && 0 == t.in_brace && 0 == t.depth)
|
|
|
|
? ssi_val_tobool(&v)
|
|
|
|
: -1;
|
|
|
|
buffer_free_ptr(&v.str);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
consistent, shared code to create CGI env
consolidated from CGI, FastCGI, SCGI, SSI
Note: due to prior inconsistencies between the code in mod_cgi,
mod_fastcgi, mod_scgi, and mod_ssi, there are some minor behavior
changes.
CONTENT_LENGTH is now always set, even if 0
(though CONTENT_LENGTH is never set for FASTCGI_AUTHORIZER)
PATH_INFO is created only if present, not if empty.
(mod_fastcgi and mod_ssi previously set PATH_INFO="" (blank value))
PATH_TRANSLATED is now set if PATH_INFO is present
(previously missing from mod_cgi and mod_ssi)
mod_ssi now sets DOCUMENT_ROOT to con->physical.basedir, like others
(previously, mod_ssi set DOCUMENT_ROOT to con->physical.doc_root,
which matched con->physical.basedir unless mod_alias changed basedir)
mod_ssi now sets REQUEST_URI to con->request.orig_uri, like others
(previously, mod_ssi set REQUEST_URI to con->request.uri, which
matched con->request.orig_uri except after redirects, error docs)
2016-10-10 17:37:36 +00:00
|
|
|
static int ssi_env_add(void *venv, const char *key, size_t klen, const char *val, size_t vlen) {
|
2019-10-02 05:54:15 +00:00
|
|
|
array_set_key_value((array *)venv, key, klen, val, vlen);
|
2005-02-20 14:27:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
static int build_ssi_cgi_vars(request_st * const r, handler_ctx * const p) {
|
consistent, shared code to create CGI env
consolidated from CGI, FastCGI, SCGI, SSI
Note: due to prior inconsistencies between the code in mod_cgi,
mod_fastcgi, mod_scgi, and mod_ssi, there are some minor behavior
changes.
CONTENT_LENGTH is now always set, even if 0
(though CONTENT_LENGTH is never set for FASTCGI_AUTHORIZER)
PATH_INFO is created only if present, not if empty.
(mod_fastcgi and mod_ssi previously set PATH_INFO="" (blank value))
PATH_TRANSLATED is now set if PATH_INFO is present
(previously missing from mod_cgi and mod_ssi)
mod_ssi now sets DOCUMENT_ROOT to con->physical.basedir, like others
(previously, mod_ssi set DOCUMENT_ROOT to con->physical.doc_root,
which matched con->physical.basedir unless mod_alias changed basedir)
mod_ssi now sets REQUEST_URI to con->request.orig_uri, like others
(previously, mod_ssi set REQUEST_URI to con->request.uri, which
matched con->request.orig_uri except after redirects, error docs)
2016-10-10 17:37:36 +00:00
|
|
|
http_cgi_opts opts = { 0, 0, NULL, NULL };
|
|
|
|
/* temporarily remove Authorization from request headers
|
|
|
|
* so that Authorization does not end up in SSI environment */
|
2020-01-13 02:51:12 +00:00
|
|
|
buffer *vb_auth = http_header_request_get(r, HTTP_HEADER_AUTHORIZATION, CONST_STR_LEN("Authorization"));
|
2018-09-09 05:50:33 +00:00
|
|
|
buffer b_auth;
|
|
|
|
if (vb_auth) {
|
|
|
|
memcpy(&b_auth, vb_auth, sizeof(buffer));
|
|
|
|
memset(vb_auth, 0, sizeof(buffer));
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2018-10-23 00:28:53 +00:00
|
|
|
array_reset_data_strings(p->ssi_cgi_env);
|
2016-03-26 11:14:21 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
if (0 != http_cgi_headers(r, &opts, ssi_env_add, p->ssi_cgi_env)) {
|
|
|
|
r->http_status = 400;
|
consistent, shared code to create CGI env
consolidated from CGI, FastCGI, SCGI, SSI
Note: due to prior inconsistencies between the code in mod_cgi,
mod_fastcgi, mod_scgi, and mod_ssi, there are some minor behavior
changes.
CONTENT_LENGTH is now always set, even if 0
(though CONTENT_LENGTH is never set for FASTCGI_AUTHORIZER)
PATH_INFO is created only if present, not if empty.
(mod_fastcgi and mod_ssi previously set PATH_INFO="" (blank value))
PATH_TRANSLATED is now set if PATH_INFO is present
(previously missing from mod_cgi and mod_ssi)
mod_ssi now sets DOCUMENT_ROOT to con->physical.basedir, like others
(previously, mod_ssi set DOCUMENT_ROOT to con->physical.doc_root,
which matched con->physical.basedir unless mod_alias changed basedir)
mod_ssi now sets REQUEST_URI to con->request.orig_uri, like others
(previously, mod_ssi set REQUEST_URI to con->request.uri, which
matched con->request.orig_uri except after redirects, error docs)
2016-10-10 17:37:36 +00:00
|
|
|
return -1;
|
2016-03-26 11:14:21 +00:00
|
|
|
}
|
|
|
|
|
2018-09-09 05:50:33 +00:00
|
|
|
if (vb_auth) {
|
|
|
|
memcpy(vb_auth, &b_auth, sizeof(buffer));
|
2016-02-28 17:05:22 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-07-12 18:46:49 +00:00
|
|
|
static void mod_ssi_timefmt (buffer * const b, buffer *timefmtb, unix_time64_t t, int localtm) {
|
2021-03-24 10:59:53 +00:00
|
|
|
struct tm tm;
|
[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
|
|
|
const char * const timefmt = buffer_is_blank(timefmtb)
|
2021-03-23 15:52:21 +00:00
|
|
|
? "%a, %d %b %Y %T %Z"
|
2021-03-24 10:59:53 +00:00
|
|
|
: timefmtb->ptr;
|
2021-07-12 18:46:49 +00:00
|
|
|
buffer_append_strftime(b, timefmt, localtm
|
|
|
|
? localtime64_r(&t, &tm)
|
|
|
|
: gmtime64_r(&t, &tm));
|
[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(b))
|
2021-03-24 10:59:53 +00:00
|
|
|
buffer_copy_string_len(b, CONST_STR_LEN("(none)"));
|
2021-03-19 06:07:33 +00:00
|
|
|
}
|
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
static int mod_ssi_process_file(request_st *r, handler_ctx *p, struct stat *st);
|
2016-12-11 06:49:11 +00:00
|
|
|
|
2020-01-13 02:51:12 +00:00
|
|
|
static int process_ssi_stmt(request_st * const r, handler_ctx * const p, const char ** const l, size_t n, struct stat * const st) {
|
2016-04-18 03:37:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* <!--#element attribute=value attribute=value ... -->
|
|
|
|
*
|
|
|
|
* config DONE
|
|
|
|
* errmsg -- missing
|
|
|
|
* sizefmt DONE
|
|
|
|
* timefmt DONE
|
|
|
|
* echo DONE
|
|
|
|
* var DONE
|
|
|
|
* encoding -- missing
|
|
|
|
* exec DONE
|
|
|
|
* cgi -- never
|
|
|
|
* cmd DONE
|
|
|
|
* fsize DONE
|
|
|
|
* file DONE
|
|
|
|
* virtual DONE
|
|
|
|
* flastmod DONE
|
|
|
|
* file DONE
|
|
|
|
* virtual DONE
|
|
|
|
* include DONE
|
|
|
|
* file DONE
|
|
|
|
* virtual DONE
|
|
|
|
* printenv DONE
|
|
|
|
* set DONE
|
|
|
|
* var DONE
|
|
|
|
* value DONE
|
|
|
|
*
|
|
|
|
* if DONE
|
|
|
|
* elif DONE
|
|
|
|
* else DONE
|
|
|
|
* endif DONE
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* expressions
|
|
|
|
* AND, OR DONE
|
|
|
|
* comp DONE
|
|
|
|
* ${...} -- missing
|
|
|
|
* $... DONE
|
|
|
|
* '...' DONE
|
|
|
|
* ( ... ) DONE
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* ** all DONE **
|
|
|
|
* DATE_GMT
|
|
|
|
* The current date in Greenwich Mean Time.
|
|
|
|
* DATE_LOCAL
|
|
|
|
* The current date in the local time zone.
|
|
|
|
* DOCUMENT_NAME
|
|
|
|
* The filename (excluding directories) of the document requested by the user.
|
|
|
|
* DOCUMENT_URI
|
|
|
|
* The (%-decoded) URL path of the document requested by the user. Note that in the case of nested include files, this is not then URL for the current document.
|
|
|
|
* LAST_MODIFIED
|
|
|
|
* The last modification date of the document requested by the user.
|
|
|
|
* USER_NAME
|
|
|
|
* Contains the owner of the file which included it.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
size_t i, ssicmd = 0;
|
2019-11-25 06:54:08 +00:00
|
|
|
buffer *tb = NULL;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2016-12-11 06:49:11 +00:00
|
|
|
static const struct {
|
2005-02-20 14:27:00 +00:00
|
|
|
const char *var;
|
2006-10-04 13:26:23 +00:00
|
|
|
enum { SSI_UNSET, SSI_ECHO, SSI_FSIZE, SSI_INCLUDE, SSI_FLASTMOD,
|
2005-02-20 14:27:00 +00:00
|
|
|
SSI_CONFIG, SSI_PRINTENV, SSI_SET, SSI_IF, SSI_ELIF,
|
2016-12-11 07:39:59 +00:00
|
|
|
SSI_ELSE, SSI_ENDIF, SSI_EXEC, SSI_COMMENT } type;
|
2005-02-20 14:27:00 +00:00
|
|
|
} ssicmds[] = {
|
|
|
|
{ "echo", SSI_ECHO },
|
|
|
|
{ "include", SSI_INCLUDE },
|
|
|
|
{ "flastmod", SSI_FLASTMOD },
|
|
|
|
{ "fsize", SSI_FSIZE },
|
|
|
|
{ "config", SSI_CONFIG },
|
|
|
|
{ "printenv", SSI_PRINTENV },
|
|
|
|
{ "set", SSI_SET },
|
|
|
|
{ "if", SSI_IF },
|
|
|
|
{ "elif", SSI_ELIF },
|
|
|
|
{ "endif", SSI_ENDIF },
|
|
|
|
{ "else", SSI_ELSE },
|
|
|
|
{ "exec", SSI_EXEC },
|
2016-12-11 07:39:59 +00:00
|
|
|
{ "comment", SSI_COMMENT },
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
{ NULL, SSI_UNSET }
|
|
|
|
};
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
for (i = 0; ssicmds[i].var; i++) {
|
|
|
|
if (0 == strcmp(l[1], ssicmds[i].var)) {
|
|
|
|
ssicmd = ssicmds[i].type;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2021-03-24 12:36:32 +00:00
|
|
|
chunkqueue * const cq = &p->wq;
|
2020-01-13 02:51:12 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
switch(ssicmd) {
|
|
|
|
case SSI_ECHO: {
|
|
|
|
/* echo */
|
2009-07-21 20:35:27 +00:00
|
|
|
int var = 0;
|
|
|
|
/* int enc = 0; */
|
2005-02-20 14:27:00 +00:00
|
|
|
const char *var_val = NULL;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2016-12-11 06:49:11 +00:00
|
|
|
static const struct {
|
2005-02-20 14:27:00 +00:00
|
|
|
const char *var;
|
2016-03-26 11:14:21 +00:00
|
|
|
enum {
|
|
|
|
SSI_ECHO_UNSET,
|
|
|
|
SSI_ECHO_DATE_GMT,
|
|
|
|
SSI_ECHO_DATE_LOCAL,
|
|
|
|
SSI_ECHO_DOCUMENT_NAME,
|
|
|
|
SSI_ECHO_DOCUMENT_URI,
|
|
|
|
SSI_ECHO_LAST_MODIFIED,
|
|
|
|
SSI_ECHO_USER_NAME,
|
|
|
|
SSI_ECHO_SCRIPT_URI,
|
|
|
|
SSI_ECHO_SCRIPT_URL,
|
|
|
|
} type;
|
2005-02-20 14:27:00 +00:00
|
|
|
} echovars[] = {
|
|
|
|
{ "DATE_GMT", SSI_ECHO_DATE_GMT },
|
|
|
|
{ "DATE_LOCAL", SSI_ECHO_DATE_LOCAL },
|
|
|
|
{ "DOCUMENT_NAME", SSI_ECHO_DOCUMENT_NAME },
|
|
|
|
{ "DOCUMENT_URI", SSI_ECHO_DOCUMENT_URI },
|
|
|
|
{ "LAST_MODIFIED", SSI_ECHO_LAST_MODIFIED },
|
|
|
|
{ "USER_NAME", SSI_ECHO_USER_NAME },
|
2016-03-26 11:14:21 +00:00
|
|
|
{ "SCRIPT_URI", SSI_ECHO_SCRIPT_URI },
|
|
|
|
{ "SCRIPT_URL", SSI_ECHO_SCRIPT_URL },
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
{ NULL, SSI_ECHO_UNSET }
|
|
|
|
};
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2009-07-21 20:35:27 +00:00
|
|
|
/*
|
2016-12-11 06:49:11 +00:00
|
|
|
static const struct {
|
2005-02-20 14:27:00 +00:00
|
|
|
const char *var;
|
|
|
|
enum { SSI_ENC_UNSET, SSI_ENC_URL, SSI_ENC_NONE, SSI_ENC_ENTITY } type;
|
|
|
|
} encvars[] = {
|
|
|
|
{ "url", SSI_ENC_URL },
|
|
|
|
{ "none", SSI_ENC_NONE },
|
|
|
|
{ "entity", SSI_ENC_ENTITY },
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
{ NULL, SSI_ENC_UNSET }
|
|
|
|
};
|
2009-07-21 20:35:27 +00:00
|
|
|
*/
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
for (i = 2; i < n; i += 2) {
|
|
|
|
if (0 == strcmp(l[i], "var")) {
|
|
|
|
int j;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
var_val = l[i+1];
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
for (j = 0; echovars[j].var; j++) {
|
|
|
|
if (0 == strcmp(l[i+1], echovars[j].var)) {
|
|
|
|
var = echovars[j].type;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (0 == strcmp(l[i], "encoding")) {
|
2009-07-21 20:35:27 +00:00
|
|
|
/*
|
2005-02-20 14:27:00 +00:00
|
|
|
int j;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
for (j = 0; encvars[j].var; j++) {
|
|
|
|
if (0 == strcmp(l[i+1], encvars[j].var)) {
|
|
|
|
enc = encvars[j].type;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-07-21 20:35:27 +00:00
|
|
|
*/
|
2005-02-20 14:27:00 +00:00
|
|
|
} else {
|
2020-01-13 02:51:12 +00:00
|
|
|
log_error(r->conf.errh, __FILE__, __LINE__,
|
2019-11-25 06:54:08 +00:00
|
|
|
"ssi: unknown attribute for %s %s", l[1], l[i]);
|
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 (p->if_is_false) break;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (!var_val) {
|
2020-01-13 02:51:12 +00:00
|
|
|
log_error(r->conf.errh, __FILE__, __LINE__,
|
2019-11-25 06:54:08 +00:00
|
|
|
"ssi: %s var is missing", l[1]);
|
2005-02-20 14:27:00 +00:00
|
|
|
break;
|
|
|
|
}
|
2005-08-08 08:22:06 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
switch(var) {
|
|
|
|
case SSI_ECHO_USER_NAME: {
|
2020-01-13 02:51:12 +00:00
|
|
|
tb = r->tmp_buf;
|
|
|
|
buffer_clear(tb);
|
2005-02-20 14:27:00 +00:00
|
|
|
#ifdef HAVE_PWD_H
|
2019-11-25 06:54:08 +00:00
|
|
|
struct passwd *pw;
|
2016-04-18 03:37:40 +00:00
|
|
|
if (NULL == (pw = getpwuid(st->st_uid))) {
|
2020-01-13 02:51:12 +00:00
|
|
|
buffer_append_int(tb, st->st_uid);
|
2005-02-20 14:27:00 +00:00
|
|
|
} else {
|
2019-11-25 06:54:08 +00:00
|
|
|
buffer_copy_string(tb, pw->pw_name);
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
#else
|
2020-01-13 02:51:12 +00:00
|
|
|
buffer_append_int(tb, st->st_uid);
|
2005-02-20 14:27:00 +00:00
|
|
|
#endif
|
[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
|
|
|
chunkqueue_append_mem(cq, BUF_PTR_LEN(tb));
|
2005-02-20 14:27:00 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-11-22 07:41:11 +00:00
|
|