From e2a551298e51e47af265d8e0b29c2bc6687211f1 Mon Sep 17 00:00:00 2001 From: Thomas Porzelt Date: Sat, 24 Jul 2010 14:51:44 +0200 Subject: [PATCH] [core] Use liCond* helpers in li_pattern_parse/eval --- include/lighttpd/pattern.h | 4 +-- src/main/pattern.c | 59 +++++++++++++++++-------------------- src/main/plugin_core.c | 13 ++++---- src/modules/mod_memcached.c | 4 +-- 4 files changed, 38 insertions(+), 42 deletions(-) diff --git a/include/lighttpd/pattern.h b/include/lighttpd/pattern.h index bbde64b..1ee4ffb 100644 --- a/include/lighttpd/pattern.h +++ b/include/lighttpd/pattern.h @@ -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); diff --git a/src/main/pattern.c b/src/main/pattern.c index dfc181f..ff1729e 100644 --- a/src/main/pattern.c +++ b/src/main/pattern.c @@ -1,6 +1,6 @@ #include -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,32 +160,15 @@ 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")); + res = li_condition_get_value(vr, part->data.lvalue, &cond_val, LI_COND_VALUE_HINT_STRING); + + 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 - 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; + g_string_append(dest, li_condition_value_to_string(vr, &cond_val)); } - if (encoded) - li_string_encode_append(str->str, dest, LI_ENCODING_URI); - else - g_string_append_len(dest, GSTR_LEN(str)); - break; } } diff --git a/src/main/plugin_core.c b/src/main/plugin_core.c index c981ac5..b69a5ca 100644 --- a/src/main/plugin_core.c +++ b/src/main/plugin_core.c @@ -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; diff --git a/src/modules/mod_memcached.c b/src/modules/mod_memcached.c index b8c508c..6926c1a 100644 --- a/src/modules/mod_memcached.c +++ b/src/modules/mod_memcached.c @@ -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;