2
0
Fork 0
lighttpd2/src/main/config_parser.rl

1379 lines
39 KiB
Plaintext
Raw Normal View History

#include <lighttpd/base.h>
#include <lighttpd/config_parser.h>
2008-07-08 16:51:03 +00:00
#ifdef HAVE_LUA_H
# include <lighttpd/config_lua.h>
#endif
#if 0
2008-07-23 20:22:33 +00:00
#define _printf(fmt, ...) g_print(fmt, __VA_ARGS__)
2008-07-15 20:12:19 +00:00
#else
#define _printf(fmt, ...) /* */
2008-07-15 20:12:19 +00:00
#endif
2008-07-08 16:51:03 +00:00
/** config parser state machine **/
%%{
2008-08-02 20:56:07 +00:00
## ragel stuff
2008-07-08 16:51:03 +00:00
machine config_parser;
2008-08-02 20:56:07 +00:00
variable p ctx->p;
variable pe ctx->pe;
variable eof ctx->eof;
2008-07-08 16:51:03 +00:00
2008-08-02 20:56:07 +00:00
access ctx->;
2008-07-08 16:51:03 +00:00
prepush {
2008-11-18 10:03:59 +00:00
/* _printf("current stacksize: %d, top: %d\n", ctx->stacksize, ctx->top); */
2008-07-08 16:51:03 +00:00
/* increase stacksize if necessary */
2008-08-02 20:56:07 +00:00
if (ctx->stacksize == ctx->top)
2008-07-08 16:51:03 +00:00
{
2008-08-02 20:56:07 +00:00
/* increase stacksize by 8 */
ctx->stack = g_realloc(ctx->stack, sizeof(int) * (ctx->stacksize + 8));
ctx->stacksize += 8;
2008-07-08 16:51:03 +00:00
}
}
2008-08-02 20:56:07 +00:00
## actions
action mark { ctx->mark = fpc; }
# basic types
2008-07-08 16:51:03 +00:00
action boolean {
liValue *o;
2008-07-23 20:22:33 +00:00
2009-07-09 20:17:24 +00:00
o = li_value_new_bool(*ctx->mark == 't' ? TRUE : FALSE);
2008-08-02 20:56:07 +00:00
g_queue_push_head(ctx->option_stack, o);
_printf("got boolean %s in line %zd\n", *ctx->mark == 't' ? "true" : "false", ctx->line);
2008-07-08 16:51:03 +00:00
}
2008-07-23 20:22:33 +00:00
2008-08-02 20:56:07 +00:00
action integer {
liValue *o;
gint64 i = 0;
2008-08-02 20:56:07 +00:00
for (gchar *c = ctx->mark; c < fpc; c++)
i = i * 10 + *c - 48;
2009-07-09 20:17:24 +00:00
o = li_value_new_number(i);
/* push value onto stack */
2008-08-02 20:56:07 +00:00
g_queue_push_head(ctx->option_stack, o);
_printf("got integer %" G_GINT64_FORMAT " in line %zd\n", i, ctx->line);
2008-07-08 16:51:03 +00:00
}
2008-07-25 20:44:10 +00:00
action integer_suffix {
liValue *o;
GString *str;
o = g_queue_peek_head(ctx->option_stack);
str = g_string_new_len(ctx->mark, fpc - ctx->mark);
if (g_str_equal(str->str, "kbyte")) o->data.number *= 1024;
else if (g_str_equal(str->str, "mbyte")) o->data.number *= 1024 * 1024;
else if (g_str_equal(str->str, "gbyte")) o->data.number *= 1024 * 1024 * 1024;
else if (g_str_equal(str->str, "tbyte")) o->data.number *= 1024 * 1024 * 1024 * G_GINT64_CONSTANT(1024);
else if (g_str_equal(str->str, "kbit")) o->data.number *= 1000;
else if (g_str_equal(str->str, "mbit")) o->data.number *= 1000 * 1000;
else if (g_str_equal(str->str, "gbit")) o->data.number *= 1000 * 1000 * 1000;
else if (g_str_equal(str->str, "tbit")) o->data.number *= 1000 * 1000 * 1000 * G_GINT64_CONSTANT(1000);
else if (g_str_equal(str->str, "min")) o->data.number *= 60;
else if (g_str_equal(str->str, "hours")) o->data.number *= 60 * 60;
else if (g_str_equal(str->str, "days")) o->data.number *= 60 * 60 * 24;
g_string_free(str, TRUE);
_printf("got int with suffix: %" G_GINT64_FORMAT "\n", o->data.number);
2008-07-25 20:44:10 +00:00
}
2008-08-02 20:56:07 +00:00
action string {
liValue *o;
2008-08-02 20:56:07 +00:00
GString *str;
gchar ch;
str = g_string_sized_new(fpc - ctx->mark - 2);
for (gchar *c = (ctx->mark+1); c != (fpc-1); c++) {
if (*c != '\\')
g_string_append_c(str, *c);
else {
guint avail = fpc - 1 - c;
if (avail == 0) {
ERROR(srv, "%s", "invalid \\ at end of string");
g_string_free(str, TRUE);
return FALSE;
}
switch (*(c+1)) {
case '\\': g_string_append_c(str, '\\'); c++; break;
2009-07-04 12:54:27 +00:00
case '"': g_string_append_c(str, '"'); c++; break;
case 'n': g_string_append_c(str, '\n'); c++; break;
case 'r': g_string_append_c(str, '\r'); c++; break;
case 't': g_string_append_c(str, '\t'); c++; break;
case 'x':
if (avail < 3 || !(
((*(c+2) >= '0' && *(c+2) <= '9') || (*(c+2) >= 'A' && *(c+2) <= 'F') || (*(c+2) >= 'a' && *(c+2) <= 'f')) &&
((*(c+3) >= '0' && *(c+3) <= '9') || (*(c+3) >= 'A' && *(c+3) <= 'F') || (*(c+3) >= 'a' && *(c+3) <= 'f')))) {
ERROR(srv, "%s", "invalid \\xHH in string");
g_string_free(str, TRUE);
return FALSE;
}
/* append char from hex */
/* first char */
if (*(c+2) <= '9')
ch = 16 * (*(c+2) - '0');
else if (*(c+2) <= 'F')
ch = 16 * (10 + *(c+2) - 'A');
else
ch = 16 * (10 + *(c+2) - 'a');
/* second char */
if (*(c+3) <= '9')
ch += *(c+3) - '0';
else if (*(c+3) <= 'F')
ch += 10 + *(c+3) - 'A';
else
ch += 10 + *(c+3) - 'a';
c += 3;
g_string_append_c(str, ch);
break;
default:
g_string_append_c(str, '\\');
}
}
}
2008-07-08 16:51:03 +00:00
2009-07-09 20:17:24 +00:00
o = li_value_new_string(str);
2008-08-02 20:56:07 +00:00
g_queue_push_head(ctx->option_stack, o);
_printf("got string %s", "");
for (gchar *c = ctx->mark + 1; c < fpc - 1; c++) _printf("%c", *c);
_printf(" in line %zd\n", ctx->line);
2008-07-08 16:51:03 +00:00
}
2008-08-02 20:56:07 +00:00
# advanced types
action list_start {
liValue *o;
/* create new list value and put it on stack, list entries are put in it by getting the previous value from the stack */
2009-07-09 20:17:24 +00:00
o = li_value_new_list();
2008-08-02 20:56:07 +00:00
g_queue_push_head(ctx->option_stack, o);
fcall list_scanner;
}
2008-07-23 20:22:33 +00:00
2008-08-02 20:56:07 +00:00
action list_push {
liValue *o, *l;
2008-07-23 20:22:33 +00:00
/* pop current value from stack and append it to the new top of the stack value (the list) */
2008-08-02 20:56:07 +00:00
o = g_queue_pop_head(ctx->option_stack);
2008-07-23 20:22:33 +00:00
2008-08-02 20:56:07 +00:00
l = g_queue_peek_head(ctx->option_stack);
assert(l->type == LI_VALUE_LIST);
2008-07-23 20:22:33 +00:00
g_array_append_val(l->data.list, o);
2008-07-23 20:22:33 +00:00
2009-07-09 20:17:24 +00:00
_printf("list_push %s\n", li_value_type_string(o->type));
2008-08-02 20:56:07 +00:00
}
2008-07-23 20:22:33 +00:00
2008-08-02 20:56:07 +00:00
action list_end {
fret;
2008-07-23 20:22:33 +00:00
}
2008-08-02 20:56:07 +00:00
action hash_start {
liValue *o;
2008-08-02 20:56:07 +00:00
/* create new hash value and put it on stack, if a key-value pair is encountered, get it by walking 2 steps back the stack */
2009-07-09 20:17:24 +00:00
o = li_value_new_hash();
2008-08-02 20:56:07 +00:00
g_queue_push_head(ctx->option_stack, o);
fcall hash_scanner;
2008-07-08 16:51:03 +00:00
}
2008-07-23 20:22:33 +00:00
2008-08-02 20:56:07 +00:00
action hash_push {
liValue *k, *v, *h; /* key value hashtable */
2008-08-02 20:56:07 +00:00
GString *str;
2008-07-08 16:51:03 +00:00
2008-08-02 20:56:07 +00:00
v = g_queue_pop_head(ctx->option_stack);
k = g_queue_pop_head(ctx->option_stack);
h = g_queue_peek_head(ctx->option_stack);
/* duplicate key so value can be free'd */
str = g_string_new_len(k->data.string->str, k->data.string->len);
2008-08-02 20:56:07 +00:00
g_hash_table_insert(h->data.hash, str, v);
2008-08-02 20:56:07 +00:00
2009-07-09 20:17:24 +00:00
_printf("hash_push: %s: %s => %s\n", li_value_type_string(k->type), li_value_type_string(v->type), li_value_type_string(h->type));
2008-09-24 22:00:23 +00:00
2009-07-09 20:17:24 +00:00
li_value_free(k);
2008-07-08 16:51:03 +00:00
}
action hash_end {
fret;
}
2008-08-02 20:56:07 +00:00
action block_start {
fcall block_scanner;
}
2008-07-08 16:51:03 +00:00
2008-08-02 20:56:07 +00:00
action block_end {
fret;
2008-07-08 16:51:03 +00:00
}
2008-08-02 20:56:07 +00:00
action keyvalue_start {
2008-11-18 10:03:59 +00:00
/* fpc--; */
2008-09-24 22:00:23 +00:00
_printf("keyvalue start in line %zd\n", ctx->line);
fcall key_value_scanner;
2008-07-08 16:51:03 +00:00
}
2008-08-02 20:56:07 +00:00
action keyvalue_end {
liValue *k, *v, *l;
2008-08-02 20:56:07 +00:00
/* we have a key and a value on the stack; convert them to a list with 2 elements */
2008-07-08 16:51:03 +00:00
2008-08-02 20:56:07 +00:00
v = g_queue_pop_head(ctx->option_stack);
k = g_queue_pop_head(ctx->option_stack);
2008-07-25 20:44:10 +00:00
2009-07-09 20:17:24 +00:00
l = li_value_new_list();
2008-07-25 20:44:10 +00:00
g_array_append_val(l->list, k);
g_array_append_val(l->list, v);
2008-07-25 20:44:10 +00:00
2009-07-09 20:17:24 +00:00
_printf("key-value pair: %s => %s in line %zd\n", li_value_type_string(k->type), li_value_type_string(v->type), ctx->line);
2008-09-24 22:00:23 +00:00
2008-08-02 20:56:07 +00:00
/* push list on the stack */
g_queue_push_head(ctx->option_stack, l);
2008-07-08 16:51:03 +00:00
2008-11-18 10:03:59 +00:00
/* fpc--; */
2008-08-02 20:56:07 +00:00
fret;
}
2008-07-08 16:51:03 +00:00
action liValue {
liValue *o;
2008-09-24 22:00:23 +00:00
o = g_queue_peek_head(ctx->option_stack);
/* check if we need to cast the value */
if (ctx->cast != LI_CFG_PARSER_CAST_NONE) {
if (ctx->cast == LI_CFG_PARSER_CAST_INT) {
2008-09-24 22:00:23 +00:00
/* cast string to integer */
gint x = 0;
guint i = 0;
gboolean negative = FALSE;
if (o->type != LI_VALUE_STRING) {
2009-07-09 20:17:24 +00:00
ERROR(srv, "can only cast strings to integers, %s given", li_value_type_string(o->type));
2008-09-24 22:00:23 +00:00
return FALSE;
}
if (o->data.string->str[0] == '-') {
2008-09-24 22:00:23 +00:00
negative = TRUE;
i++;
}
for (; i < o->data.string->len; i++) {
gchar c = o->data.string->str[i];
2008-09-24 22:00:23 +00:00
if (c < '0' || c > '9') {
ERROR(srv, "%s", "cast(int) parameter doesn't look like a numerical string");
2008-09-24 22:00:23 +00:00
return FALSE;
}
x = x * 10 + c - '0';
}
if (negative)
x *= -1;
g_string_free(o->data.string, TRUE);
o->data.number = x;
o->type = LI_VALUE_NUMBER;
2008-09-24 22:00:23 +00:00
}
else if (ctx->cast == LI_CFG_PARSER_CAST_STR) {
2008-09-24 22:00:23 +00:00
/* cast integer to string */
GString *str;
if (o->type != LI_VALUE_NUMBER) {
2009-07-09 20:17:24 +00:00
ERROR(srv, "can only cast integers to strings, %s given", li_value_type_string(o->type));
2008-09-24 22:00:23 +00:00
return FALSE;
}
str = g_string_sized_new(0);
g_string_printf(str, "%" G_GINT64_FORMAT, o->data.number);
o->data.string = str;
o->type = LI_VALUE_STRING;
2008-09-24 22:00:23 +00:00
}
ctx->cast = LI_CFG_PARSER_CAST_NONE;
2008-09-24 22:00:23 +00:00
}
2009-07-09 20:17:24 +00:00
_printf("value (%s) in line %zd\n", li_value_type_string(o->type), ctx->line);
2008-09-24 22:00:23 +00:00
}
action value_statement_start {
fcall value_statement_scanner;
}
action value_statement_end {
fret;
2008-08-02 20:56:07 +00:00
}
2008-07-08 16:51:03 +00:00
action value_statement_op {
g_queue_push_head(ctx->value_op_stack, ctx->mark);
}
action value_statement {
/* value (+|-|*|/) value */
/* compute new value out of the two */
liValue *l, *r, *o;
gboolean free_l, free_r;
gchar op;
free_l = free_r = TRUE;
r = g_queue_pop_head(ctx->option_stack);
l = g_queue_pop_head(ctx->option_stack);
o = NULL;
op = *(gchar*)g_queue_pop_head(ctx->value_op_stack);
if (op == '=') {
2008-09-24 22:00:23 +00:00
/* value => value */
free_l = FALSE;
free_r = FALSE;
2009-07-09 20:17:24 +00:00
o = li_value_new_list();
g_array_append_val(o->data.list, l);
g_array_append_val(o->data.list, r);
2008-09-24 22:00:23 +00:00
}
else if (l->type == LI_VALUE_NUMBER && r->type == LI_VALUE_NUMBER) {
switch (op) {
2009-07-09 20:17:24 +00:00
case '+': o = li_value_new_number(l->data.number + r->data.number); break;
case '-': o = li_value_new_number(l->data.number - r->data.number); break;
case '*': o = li_value_new_number(l->data.number * r->data.number); break;
case '/': o = li_value_new_number(l->data.number / r->data.number); break;
}
}
else if (l->type == LI_VALUE_STRING) {
o = l;
free_l = FALSE;
if (r->type == LI_VALUE_STRING && op == '+') {
/* str + str */
o->data.string = g_string_append_len(o->data.string, GSTR_LEN(r->data.string));
}
else if (r->type == LI_VALUE_NUMBER && op == '+') {
2008-09-24 22:00:23 +00:00
/* str + int */
g_string_append_printf(o->data.string, "%" G_GINT64_FORMAT, r->data.number);
2008-09-24 22:00:23 +00:00
}
else if (r->type == LI_VALUE_NUMBER && op == '*') {
/* str * int */
if (r->data.number < 0) {
ERROR(srv, "string multiplication with negative number (%" G_GINT64_FORMAT ")?", r->data.number);
return FALSE;
}
else if (r->data.number == 0) {
o->data.string = g_string_truncate(o->data.string, 0);
}
else {
GString *str;
str = g_string_new_len(l->data.string->str, l->data.string->len);
for (gint i = 1; i < r->data.number; i++)
o->data.string = g_string_append_len(o->data.string, str->str, str->len);
g_string_free(str, TRUE);
}
}
2008-09-24 22:00:23 +00:00
else
o = NULL;
}
else if (l->type == LI_VALUE_LIST) {
if (op == '+') {
/* append r to the end of l */
free_l = FALSE; /* use l as the new o */
free_r = FALSE; /* r gets appended to o */
o = l;
g_array_append_val(l->data.list, r);
}
else if (op == '*') {
/* merge l and r */
if (r->type == LI_VALUE_LIST) {
2008-09-24 22:00:23 +00:00
/* merge lists */
free_l = FALSE;
g_array_append_vals(l->data.list, r->data.list->data, r->data.list->len);
g_array_set_size(r->data.list, 0);
2008-09-24 22:00:23 +00:00
o = l;
}
}
}
else if (l->type == LI_VALUE_HASH && r->type == LI_VALUE_HASH && op == '+') {
/* merge hashtables */
GHashTableIter iter;
gpointer key, val;
free_l = FALSE; /* keep l, it's the new o */
o = l;
g_hash_table_iter_init(&iter, r->data.hash);
while (g_hash_table_iter_next(&iter, &key, &val)) {
g_hash_table_insert(o->data.hash, key, val);
g_hash_table_iter_steal(&iter); /* steal key->value so it doesn't get deleted when destroying r */
}
}
if (o == NULL) {
WARNING(srv, "erronous value statement: %s %c %s in line %zd\n",
li_value_type_string(l->type), op,
2009-07-09 20:17:24 +00:00
li_value_type_string(r->type), ctx->line);
return FALSE;
}
2008-09-24 22:00:23 +00:00
_printf("value statement: %s %c%s %s => %s in line %zd\n",
2009-07-09 20:17:24 +00:00
li_value_type_string(l->type),
op,
op == '=' ? ">" : "",
2009-07-09 20:17:24 +00:00
li_value_type_string(r->type),
li_value_type_string(o->type),
2008-09-24 22:00:23 +00:00
ctx->line);
if (free_l)
2009-07-09 20:17:24 +00:00
li_value_free(l);
if (free_r)
2009-07-09 20:17:24 +00:00
li_value_free(r);
g_queue_push_head(ctx->option_stack, o);
}
2008-08-02 20:56:07 +00:00
action varname {
/* varname, push it as string value onto the stack */
liValue *o;
2008-08-02 20:56:07 +00:00
GString *str;
2008-07-08 16:51:03 +00:00
2008-08-02 20:56:07 +00:00
str = g_string_new_len(ctx->mark, fpc - ctx->mark);
2009-07-09 20:17:24 +00:00
o = li_value_new_string(str);
2008-08-02 20:56:07 +00:00
g_queue_push_head(ctx->option_stack, o);
}
2008-07-08 16:51:03 +00:00
2008-08-03 20:26:37 +00:00
action actionref {
/* varname is on the stack */
liValue *o, *r, *t;
o = g_queue_pop_head(ctx->option_stack);
_printf("got actionref: %s in line %zd\n", o->data.string->str, ctx->line);
/* action refs starting with "var." are user defined variables */
if (g_str_has_prefix(o->data.string->str, "var.")) {
/* look up var in hashtable, copy and push value onto stack */
t = g_hash_table_lookup(ctx->uservars, o->data.string);
if (t == NULL) {
WARNING(srv, "unknown variable '%s'", o->data.string->str);
2009-07-09 20:17:24 +00:00
li_value_free(o);
return FALSE;
}
2009-07-09 20:17:24 +00:00
r = li_value_copy(t);
}
else if (g_str_has_prefix(o->data.string->str, "env.")) {
/* look up string in environment, push value onto stack */
gchar *env = getenv(o->data.string->str + 4);
2008-09-24 22:00:23 +00:00
if (env == NULL) {
ERROR(srv, "unknown environment variable: %s", o->data.string->str + 4);
2009-07-09 20:17:24 +00:00
li_value_free(o);
2008-09-24 22:00:23 +00:00
return FALSE;
}
2009-07-09 20:17:24 +00:00
r = li_value_new_string(g_string_new(env));
2008-09-24 22:00:23 +00:00
}
else {
/* real action, lookup hashtable and create new action value */
liAction *a;
a = g_hash_table_lookup(ctx->action_blocks, o->data.string);
if (a == NULL) {
WARNING(srv, "unknown action block referenced: %s", o->data.string->str);
return FALSE;
}
2009-07-09 20:17:24 +00:00
li_action_acquire(a);
r = li_value_new_action(srv, a);
}
g_queue_push_head(ctx->option_stack, r);
2009-07-09 20:17:24 +00:00
li_value_free(o);
2008-08-03 20:26:37 +00:00
}
action operator {
if ((fpc - ctx->mark) == 1) {
switch (*ctx->mark) {
case '<': ctx->op = LI_CONFIG_COND_LT; break;
case '>': ctx->op = LI_CONFIG_COND_GT; break;
}
}
else {
if (*ctx->mark == '>' && *(ctx->mark+1) == '=') ctx->op = LI_CONFIG_COND_GE;
else if (*ctx->mark == '<' && *(ctx->mark+1) == '=') ctx->op = LI_CONFIG_COND_LE;
else if (*ctx->mark == '=' && *(ctx->mark+1) == '=') ctx->op = LI_CONFIG_COND_EQ;
else if (*ctx->mark == '!' && *(ctx->mark+1) == '=') ctx->op = LI_CONFIG_COND_NE;
else if (*ctx->mark == '=' && *(ctx->mark+1) == '^') ctx->op = LI_CONFIG_COND_PREFIX;
else if (*ctx->mark == '!' && *(ctx->mark+1) == '^') ctx->op = LI_CONFIG_COND_NOPREFIX;
else if (*ctx->mark == '=' && *(ctx->mark+1) == '$') ctx->op = LI_CONFIG_COND_SUFFIX;
else if (*ctx->mark == '!' && *(ctx->mark+1) == '$') ctx->op = LI_CONFIG_COND_NOSUFFIX;
else if (*ctx->mark == '=' && *(ctx->mark+1) == '~') ctx->op = LI_CONFIG_COND_MATCH;
else if (*ctx->mark == '!' && *(ctx->mark+1) == '~') ctx->op = LI_CONFIG_COND_NOMATCH;
else if (*ctx->mark == '=' && *(ctx->mark+1) == '/') ctx->op = LI_CONFIG_COND_IP;
else if (*ctx->mark == '!' && *(ctx->mark+1) == '/') ctx->op = LI_CONFIG_COND_NOTIP;
}
}
2008-08-02 20:56:07 +00:00
# statements
action assignment {
liValue *val, *name;
liAction *a, *al;
2008-07-08 16:51:03 +00:00
/* top of the stack is the value, then the varname as string value */
2008-08-02 20:56:07 +00:00
val = g_queue_pop_head(ctx->option_stack);
name = g_queue_pop_head(ctx->option_stack);
2008-07-08 16:51:03 +00:00
assert(name->type == LI_VALUE_STRING);
2008-07-08 16:51:03 +00:00
2009-07-09 20:17:24 +00:00
_printf("got assignment: %s = %s; in line %zd\n", name->data.string->str, li_value_type_string(val->type), ctx->line);
if (g_str_has_prefix(name->data.string->str, "var.")) {
/* assignment vor user defined variable, insert into hashtable */
gpointer old_key;
gpointer old_val;
2010-01-24 13:00:29 +00:00
GString *str = li_value_extract_string(name);
/* free old key and value if we are overwriting it */
if (g_hash_table_lookup_extended(ctx->uservars, str, &old_key, &old_val)) {
g_hash_table_remove(ctx->uservars, str);
g_string_free(old_key, TRUE);
2009-07-09 20:17:24 +00:00
li_value_free(old_val);
}
g_hash_table_insert(ctx->uservars, str, val);
}
else if (ctx->in_setup_block) {
/* in setup { } block, override default values for options */
2009-07-09 20:17:24 +00:00
if (!li_plugin_set_default_option(srv, name->data.string->str, val)) {
ERROR(srv, "failed overriding default value for option \"%s\"", name->data.string->str);
2009-07-09 20:17:24 +00:00
li_value_free(name);
li_value_free(val);
return FALSE;
}
2009-07-09 20:17:24 +00:00
li_value_free(val);
}
else {
/* normal assignment */
a = li_option_action(srv, srv->main_worker, name->data.string->str, val);
2009-07-09 20:17:24 +00:00
li_value_free(val);
if (a == NULL) {
2009-07-09 20:17:24 +00:00
li_value_free(name);
return FALSE;
}
al = g_queue_peek_head(ctx->action_list_stack);
g_array_append_val(al->data.list, a);
}
2009-07-09 20:17:24 +00:00
li_value_free(name);
}
action function_noparam {
liValue *name;
liAction *a, *al;
2008-08-03 20:26:37 +00:00
name = g_queue_pop_head(ctx->option_stack);
2008-07-08 16:51:03 +00:00
assert(name->type == LI_VALUE_STRING);
_printf("got function: %s; in line %zd\n", name->data.string->str, ctx->line);
if (g_str_equal(name->data.string->str, "break")) {
}
else if (g_str_equal(name->data.string->str, "__halt")) {
}
else {
if (ctx->in_setup_block) {
/* we are in the setup { } block, call setups and don't append to action list */
2009-07-09 20:17:24 +00:00
if (!li_call_setup(srv, name->data.string->str, NULL)) {
li_value_free(name);
return FALSE;
}
}
else {
2009-09-14 19:06:55 +00:00
/* lookup hashtable of defined actions */
a = g_hash_table_lookup(ctx->action_blocks, name->data.string);
if (a == NULL) {
a = li_create_action(srv, srv->main_worker, name->data.string->str, NULL);
2009-09-14 19:06:55 +00:00
} else {
li_action_acquire(a);
}
if (a == NULL) {
li_value_free(name);
return FALSE;
}
if (a == NULL) {
2009-07-09 20:17:24 +00:00
li_value_free(name);
return FALSE;
}
2008-08-03 20:26:37 +00:00
2009-09-14 19:06:55 +00:00
al = g_queue_peek_head(ctx->action_list_stack);
g_array_append_val(al->data.list, a);
}
}
2009-07-09 20:17:24 +00:00
li_value_free(name);
2008-08-02 20:56:07 +00:00
}
2008-07-08 16:51:03 +00:00
action function_param {
2008-08-02 20:56:07 +00:00
/* similar to assignment */
liValue *val, *name;
liAction *a, *al;
2008-07-08 16:51:03 +00:00
/* top of the stack is the value, then the varname as string value */
2008-08-02 20:56:07 +00:00
val = g_queue_pop_head(ctx->option_stack);
name = g_queue_pop_head(ctx->option_stack);
2008-07-08 16:51:03 +00:00
assert(name->type == LI_VALUE_STRING);
2008-07-08 16:51:03 +00:00
2009-07-09 20:17:24 +00:00
_printf("got function: %s %s; in line %zd\n", name->data.string->str, li_value_type_string(val->type), ctx->line);
if (g_str_equal(name->data.string->str, "include")) {
GPatternSpec *pattern;
GDir *dir;
gchar *pos;
const gchar *filename;
GString *path;
guint len;
GError *err = NULL;
li_value_free(name);
if (val->type != LI_VALUE_STRING || val->data.string->len == 0) {
WARNING(srv, "include directive takes a non-empty string as parameter, %s given", li_value_type_string(val->type));
2009-07-09 20:17:24 +00:00
li_value_free(val);
return FALSE;
2008-08-02 20:56:07 +00:00
}
/* split path into dirname and filename/pattern. e.g. /etc/lighttpd/vhost_*.conf => /etc/lighttpd/ and vhost_*.conf */
if (val->data.string->str[0] == G_DIR_SEPARATOR) {
/* absolute path */
pos = strrchr(val->data.string->str, G_DIR_SEPARATOR);
path = g_string_new_len(val->data.string->str, pos - val->data.string->str + 1);
pattern = g_pattern_spec_new(pos+1);
} else {
/* relative path */
pos = strrchr(ctx->filename, G_DIR_SEPARATOR);
if (pos) {
path = g_string_new_len(ctx->filename, pos - ctx->filename + 1);
} else {
/* current working directory */
path = g_string_new_len(CONST_STR_LEN("." G_DIR_SEPARATOR_S));
}
pos = strrchr(val->data.string->str, G_DIR_SEPARATOR);
if (pos) {
g_string_append_len(path, val->data.string->str, pos - val->data.string->str + 1);
pattern = g_pattern_spec_new(pos+1);
} else {
pattern = g_pattern_spec_new(val->data.string->str);
}
}
/* we got a path, check for matching names */
dir = g_dir_open(path->str, 0, &err);
if (!dir) {
ERROR(srv, "include: could not open directory \"%s\": %s", path->str, err->message);
g_string_free(path, TRUE);
li_value_free(val);
g_error_free(err);
g_pattern_spec_free(pattern);
return FALSE;
}
len = path->len;
/* loop through all filenames in the directory and include matching ones */
while (NULL != (filename = g_dir_read_name(dir))) {
if (!g_pattern_match_string(pattern, filename))
continue;
g_string_append(path, filename);
if (!li_config_parser_file(srv, ctx_stack, path->str)) {
g_string_free(path, TRUE);
g_pattern_spec_free(pattern);
g_dir_close(dir);
li_value_free(val);
return FALSE;
}
g_string_truncate(path, len);
}
g_string_free(path, TRUE);
g_pattern_spec_free(pattern);
g_dir_close(dir);
2009-07-09 20:17:24 +00:00
li_value_free(val);
2008-08-02 20:56:07 +00:00
}
else if (g_str_equal(name->data.string->str, "include_shell")) {
if (val->type != LI_VALUE_STRING) {
WARNING(srv, "include_shell directive takes a string as parameter, %s given", li_value_type_string(val->type));
2009-07-09 20:17:24 +00:00
li_value_free(name);
li_value_free(val);
return FALSE;
2008-08-02 20:56:07 +00:00
}
if (!config_parser_shell(srv, ctx_stack, val->data.string->str)) {
2009-07-09 20:17:24 +00:00
li_value_free(name);
li_value_free(val);
return FALSE;
}
2009-07-09 20:17:24 +00:00
li_value_free(val);
2008-08-02 20:56:07 +00:00
}
#ifdef HAVE_LUA_H
else if (g_str_equal(name->data.string->str, "include_lua")) {
if (val->type != LI_VALUE_STRING) {
WARNING(srv, "include_lua directive takes a string as parameter, %s given", li_value_type_string(val->type));
li_value_free(name);
li_value_free(val);
return FALSE;
}
if (!li_config_lua_load(srv->L, srv, srv->main_worker, val->data.string->str, &a, TRUE, NULL)) {
ERROR(srv, "include_lua '%s' failed", val->data.string->str);
li_value_free(name);
li_value_free(val);
return FALSE;
}
li_value_free(name);
li_value_free(val);
/* include lua doesn't need to produce an action */
if (a != NULL) {
al = g_queue_peek_head(ctx->action_list_stack);
g_array_append_val(al->data.list, a);
}
}
#endif
/* internal functions */
else if (g_str_has_prefix(name->data.string->str, "__")) {
if (g_str_equal(name->data.string->str + 2, "print")) {
2009-07-09 20:17:24 +00:00
GString *tmpstr = li_value_to_string(val);
DEBUG(srv, "%s:%zd type: %s, value: %s", ctx->filename, ctx->line, li_value_type_string(val->type), tmpstr->str);
2008-09-24 22:00:23 +00:00
g_string_free(tmpstr, TRUE);
}
li_value_free(val);
li_value_free(name);
}
/* normal function action */
2008-08-02 20:56:07 +00:00
else {
if (ctx->in_setup_block) {
/* we are in the setup { } block, call setups and don't append to action list */
2009-07-09 20:17:24 +00:00
if (!li_call_setup(srv, name->data.string->str, val)) {
li_value_free(name);
li_value_free(val);
return FALSE;
}
2009-07-09 20:17:24 +00:00
li_value_free(val);
}
else {
al = g_queue_peek_head(ctx->action_list_stack);
a = li_create_action(srv, srv->main_worker, name->data.string->str, val);
2009-07-09 20:17:24 +00:00
li_value_free(val);
if (a == NULL) {
2009-07-09 20:17:24 +00:00
li_value_free(name);
return FALSE;
}
g_array_append_val(al->data.list, a);
}
2008-07-08 16:51:03 +00:00
li_value_free(name);
}
2008-08-02 20:56:07 +00:00
}
2008-07-08 16:51:03 +00:00
action condition_start {
/* stack: value, varname OR value, key, varname */
liValue *v, *n, *k;
gchar *str;
liCondition *cond;
liConditionLValue *lvalue;
liCondLValue lvalue_type;
2008-07-08 16:51:03 +00:00
/* if condition is nonbool, then it has a value and maybe a key too on the stack */
if (ctx->condition_nonbool) {
v = g_queue_pop_head(ctx->option_stack);
if (ctx->condition_with_key)
k = g_queue_pop_head(ctx->option_stack);
else
k = NULL;
} else {
v = NULL;
k = NULL;
}
2008-08-02 20:56:07 +00:00
n = g_queue_pop_head(ctx->option_stack);
assert(n->type == LI_VALUE_STRING);
2008-07-08 16:51:03 +00:00
2009-07-09 20:17:24 +00:00
/*_printf("got condition: %s:%s %s %s in line %zd\n", n->data.string->str, ctx->condition_with_key ? k->data.string->str : "", li_comp_op_to_string(ctx->op), li_value_type_string(v->type), ctx->line);*/
/* create condition lvalue */
str = n->data.string->str;
lvalue_type = li_cond_lvalue_from_string(GSTR_LEN(n->data.string));
if (lvalue_type == LI_COMP_UNKNOWN) {
ERROR(srv, "unknown lvalue for condition: %s", n->data.string->str);
return FALSE;
}
if ((lvalue_type == LI_COMP_REQUEST_HEADER || lvalue_type == LI_COMP_ENVIRONMENT || lvalue_type == LI_COMP_RESPONSE_HEADER) && k == NULL) {
ERROR(srv, "%s conditional needs a key", n->data.string->str);
return FALSE;
}
lvalue = li_condition_lvalue_new(lvalue_type, k ? li_value_extract_string(k) : NULL);
if (ctx->condition_nonbool) {
if (v->type == LI_VALUE_STRING) {
2010-01-24 13:00:29 +00:00
cond = li_condition_new_string(srv, ctx->op, lvalue, li_value_extract_string(v));
}
else if (v->type == LI_VALUE_NUMBER)
2010-01-24 13:00:29 +00:00
cond = li_condition_new_int(srv, ctx->op, lvalue, v->data.number);
else {
cond = NULL;
}
} else {
/* boolean condition */
2009-07-09 20:17:24 +00:00
cond = li_condition_new_bool(srv, lvalue, !ctx->condition_negated);
}
if (cond == NULL) {
WARNING(srv, "%s", "could not create condition");
return FALSE;
}
g_queue_push_head(ctx->condition_stack, cond);
2009-07-09 20:17:24 +00:00
g_queue_push_head(ctx->action_list_stack, li_action_new_list());
2009-07-09 20:17:24 +00:00
li_value_free(n);
if (ctx->condition_nonbool) {
2009-07-09 20:17:24 +00:00
li_value_free(k);
li_value_free(v);
}
ctx->condition_with_key = FALSE;
ctx->condition_nonbool = FALSE;
ctx->condition_negated = FALSE;
2008-08-02 20:56:07 +00:00
}
2008-07-08 16:51:03 +00:00
action condition_end {
liCondition *cond;
liAction *a, *al;
cond = g_queue_pop_head(ctx->condition_stack);
al = g_queue_pop_head(ctx->action_list_stack);
2009-07-09 20:17:24 +00:00
a = li_action_new_condition(cond, al, NULL);
al = g_queue_peek_head(ctx->action_list_stack);
g_array_append_val(al->data.list, a);
}
action condition_key {
ctx->condition_with_key = TRUE;
2008-08-02 20:56:07 +00:00
}
2008-07-08 16:51:03 +00:00
action else_nocond_start {
2008-08-26 22:57:51 +00:00
/* start a new action list */
2009-07-09 20:17:24 +00:00
g_queue_push_head(ctx->action_list_stack, li_action_new_list());
2008-08-26 22:57:51 +00:00
_printf("got else_nocond_start in line %zd\n", ctx->line);
}
action else_nocond_end {
2008-08-26 22:57:51 +00:00
/*
else block WITHOUT condition
- pop current action list from stack
- peek previous action list from stack
- get last action from action list
- put current action list as target_else of the last action
*/
liAction *al, *target, *cond;
2008-08-26 22:57:51 +00:00
target = g_queue_pop_head(ctx->action_list_stack);
al = g_queue_peek_head(ctx->action_list_stack);
cond = g_array_index(al->data.list, liAction*, al->data.list->len - 1); /* last action in the list is our condition */
2008-08-26 22:57:51 +00:00
while (cond->data.condition.target_else) {
2008-08-26 22:57:51 +00:00
/* condition has already an else statement, try the target */
cond = cond->data.condition.target_else;
2008-08-26 22:57:51 +00:00
}
cond->data.condition.target_else = target;
2008-08-26 22:57:51 +00:00
_printf("got else_nocond_end in line %zd\n", ctx->line);
}
action else_cond_end {
/*
else block WITH condition
- get current condition action from action list
- get previous condition action from action list
- put current condition action as target_else of the previous condition
- remove current condition action from action list
*/
liAction *prev, *cur, *al;