2
0
Fork 0

[core] Use liCond* helpers in li_pattern_parse/eval

This commit is contained in:
Thomas Porzelt 2010-07-24 14:51:44 +02:00
parent fa3c81bd11
commit e2a551298e
4 changed files with 39 additions and 43 deletions

View File

@ -15,7 +15,7 @@ typedef struct {
union {
GString *str; /* PATTERN_STRING */
guint8 ndx; /* PATTERN_NTH and PATTERN_NTH_PREV */
liCondLValue var; /* PATTERN_VAR and PATTERN_VAR_ENCODED */
liConditionLValue *lvalue; /* PATTERN_VAR and PATTERN_VAR_ENCODED */
} data;
} liPatternPart;
@ -26,7 +26,7 @@ typedef GArray liPattern;
typedef void (*liPatternCB) (GString *pattern_result, guint8 nth_ndx, gpointer data);
/* constructs a new liPattern* by parsing the given string, returns NULL on error */
LI_API liPattern *li_pattern_new(const gchar* str);
LI_API liPattern *li_pattern_new(liServer *srv, const gchar* str);
LI_API void li_pattern_free(liPattern *pattern);
LI_API void li_pattern_eval(liVRequest *vr, GString *dest, liPattern *pattern, liPatternCB nth_callback, gpointer nth_data, liPatternCB nth_prev_callback, gpointer nth_prev_data);

View File

@ -1,6 +1,6 @@
#include <lighttpd/base.h>
liPattern *li_pattern_new(const gchar* str) {
liPattern *li_pattern_new(liServer *srv, const gchar* str) {
GArray *pattern;
liPatternPart part;
gchar *c;
@ -19,6 +19,7 @@ liPattern *li_pattern_new(const gchar* str) {
c++;
} else {
/* parse error */
ERROR(srv, "could not parse pattern: \"%s\"", str);
li_pattern_free((liPattern*)pattern);
return NULL;
}
@ -48,29 +49,33 @@ liPattern *li_pattern_new(const gchar* str) {
if (*c == '\0') {
/* parse error */
ERROR(srv, "could not parse pattern: \"%s\"", str);
li_pattern_free((liPattern*)pattern);
return NULL;
}
part.data.var = li_cond_lvalue_from_string(c-len, len);
part.data.lvalue = li_condition_lvalue_new(li_cond_lvalue_from_string(c-len, len), NULL);
part.type = encoded ? PATTERN_VAR_ENCODED : PATTERN_VAR;
g_array_append_val(pattern, part);
if (part.data.var == LI_COMP_UNKNOWN) {
if (part.data.lvalue->type == LI_COMP_UNKNOWN) {
/* parse error */
ERROR(srv, "could not parse pattern: \"%s\"", str);
li_pattern_free((liPattern*)pattern);
return NULL;
}
if (len && *c == '}') {
part.type = encoded ? PATTERN_VAR_ENCODED : PATTERN_VAR;
g_array_append_val(pattern, part);
c++;
} else {
/* parse error */
ERROR(srv, "could not parse pattern: \"%s\"", str);
li_pattern_free((liPattern*)pattern);
return NULL;
}
} else {
/* parse error */
ERROR(srv, "could not parse pattern: \"%s\"", str);
li_pattern_free((liPattern*)pattern);
return NULL;
}
@ -88,6 +93,7 @@ liPattern *li_pattern_new(const gchar* str) {
c++;
} else {
/* parse error */
ERROR(srv, "could not parse pattern: \"%s\"", str);
li_pattern_free((liPattern*)pattern);
return NULL;
}
@ -109,13 +115,19 @@ liPattern *li_pattern_new(const gchar* str) {
void li_pattern_free(liPattern *pattern) {
guint i;
GArray *arr;
liPatternPart *part;
if (!pattern) return;
arr = (GArray*) pattern;
for (i = 0; i < arr->len; i++) {
if (g_array_index(arr, liPatternPart, i).type == PATTERN_STRING)
g_string_free(g_array_index(arr, liPatternPart, i).data.str, TRUE);
part = &g_array_index(arr, liPatternPart, i);
switch (part->type) {
case PATTERN_STRING: g_string_free(part->data.str, TRUE); break;
case PATTERN_VAR_ENCODED: /* fall through */
case PATTERN_VAR: li_condition_lvalue_release(part->data.lvalue); break;
default: break;
}
}
g_array_free(arr, TRUE);
@ -124,8 +136,8 @@ void li_pattern_free(liPattern *pattern) {
void li_pattern_eval(liVRequest *vr, GString *dest, liPattern *pattern, liPatternCB nth_callback, gpointer nth_data, liPatternCB nth_prev_callback, gpointer nth_prev_data) {
guint i;
gboolean encoded;
GString *str;
GString str_stack;
liHandlerResult res;
liConditionValue cond_val;
GArray *arr = (GArray*) pattern;
for (i = 0; i < arr->len; i++) {
@ -148,31 +160,14 @@ void li_pattern_eval(liVRequest *vr, GString *dest, liPattern *pattern, liPatter
encoded = TRUE;
/* fall through */
case PATTERN_VAR:
switch (part->data.var) {
case LI_COMP_REQUEST_LOCALIP: str = vr->con->local_addr_str; break;
case LI_COMP_REQUEST_REMOTEIP: str = vr->con->remote_addr_str; break;
case LI_COMP_REQUEST_SCHEME:
if (vr->con->is_ssl)
str_stack = li_const_gstring(CONST_STR_LEN("https"));
else
str_stack = li_const_gstring(CONST_STR_LEN("http"));
str = &str_stack;
break;
case LI_COMP_REQUEST_PATH: str = vr->request.uri.path; break;
case LI_COMP_REQUEST_HOST: str = vr->request.uri.host; break;
case LI_COMP_REQUEST_QUERY_STRING: str = vr->request.uri.query; break;
case LI_COMP_REQUEST_METHOD: str = vr->request.http_method_str; break;
case LI_COMP_REQUEST_CONTENT_LENGTH:
g_string_printf(vr->con->wrk->tmp_str, "%"L_GOFFSET_FORMAT, vr->request.content_length);
str = vr->con->wrk->tmp_str;
break;
default: continue;
}
res = li_condition_get_value(vr, part->data.lvalue, &cond_val, LI_COND_VALUE_HINT_STRING);
if (encoded)
li_string_encode_append(str->str, dest, LI_ENCODING_URI);
else
g_string_append_len(dest, GSTR_LEN(str));
if (res == LI_HANDLER_GO_ON) {
if (encoded)
li_string_encode_append(li_condition_value_to_string(vr, &cond_val), dest, LI_ENCODING_URI);
else
g_string_append(dest, li_condition_value_to_string(vr, &cond_val));
}
break;
}

View File

@ -270,7 +270,7 @@ static liAction* core_docroot(liServer *srv, liWorker *wrk, liPlugin* p, liValue
arr = g_array_new(FALSE, TRUE, sizeof(liPattern*));
if (val->type == LI_VALUE_STRING) {
pattern = li_pattern_new(val->data.string->str);
pattern = li_pattern_new(srv, val->data.string->str);
g_array_append_val(arr, pattern);
} else {
for (i = 0; i < val->data.list->len; i++) {
@ -281,7 +281,7 @@ static liAction* core_docroot(liServer *srv, liWorker *wrk, liPlugin* p, liValue
return NULL;
}
pattern = li_pattern_new(v->data.string->str);
pattern = li_pattern_new(srv, v->data.string->str);
g_array_append_val(arr, pattern);
}
}
@ -822,14 +822,15 @@ static void core_log_write_free(liServer *srv, gpointer param) {
static liHandlerResult core_handle_log_write(liVRequest *vr, gpointer param, gpointer *context) {
liPattern *pattern = param;
GString *str = g_string_sized_new(127);
UNUSED(context);
/* eval pattern, ignore $n and %n */
g_string_truncate(vr->wrk->tmp_str, 0);
li_pattern_eval(vr, vr->wrk->tmp_str, pattern, NULL, NULL, NULL, NULL);
li_pattern_eval(vr, str, pattern, NULL, NULL, NULL, NULL);
VR_INFO(vr, "%s", vr->wrk->tmp_str->str);
VR_INFO(vr, "%s", str->str);
g_string_free(str, TRUE);
return LI_HANDLER_GO_ON;
}
@ -843,7 +844,7 @@ static liAction* core_log_write(liServer *srv, liWorker *wrk, liPlugin* p, liVal
return NULL;
}
pattern = li_pattern_new(val->data.string->str);
pattern = li_pattern_new(srv, val->data.string->str);
if (!pattern) {
ERROR(srv, "%s", "log.write failed to parse pattern");
return NULL;

View File

@ -147,7 +147,7 @@ static memcached_ctx* mc_ctx_parse(liServer *srv, liPlugin *p, liValue *config)
ctx->addr = li_sockaddr_from_string(&def_server, 11211);
ctx->pattern = li_pattern_new("%{req.path}");
ctx->pattern = li_pattern_new(srv, "%{req.path}");
ctx->flags = 0;
ctx->ttl = 30;
@ -181,7 +181,7 @@ static memcached_ctx* mc_ctx_parse(liServer *srv, liPlugin *p, liValue *config)
goto option_failed;
}
li_pattern_free(ctx->pattern);
ctx->pattern = li_pattern_new(value->data.string->str);
ctx->pattern = li_pattern_new(srv, value->data.string->str);
if (NULL == ctx->pattern) {
ERROR(srv, "memcache: couldn't parse pattern for key '%s'", value->data.string->str);
goto option_failed;