Permalink
Browse files

[plugin_core] rename 'blank' action to 'respond' and allow optional i…

…nteger, string or list of integer+string parameters which support patterns
  • Loading branch information...
1 parent 2647a97 commit cdf183137f094710f6f923577f6ade4c15edb6c3 Thomas Porzelt committed Oct 17, 2011
Showing with 74 additions and 13 deletions.
  1. +70 −9 src/main/plugin_core.c
  2. +4 −4 tests/t-header-modify.py
@@ -914,26 +914,87 @@ static liAction* core_log_write(liServer *srv, liWorker *wrk, liPlugin* p, liVal
}
-static liHandlerResult core_handle_blank(liVRequest *vr, gpointer param, gpointer *context) {
- UNUSED(param);
+typedef struct respond_param respond_param;
+struct respond_param {
+ guint status_code;
+ liPattern *pattern;
+};
+
+static void core_respond_free(liServer *srv, gpointer param) {
+ respond_param *rp = param;
+
+ UNUSED(srv);
+
+ if (rp->pattern)
+ li_pattern_free(rp->pattern);
+
+ g_slice_free(respond_param, rp);
+}
+
+static liHandlerResult core_handle_respond(liVRequest *vr, gpointer param, gpointer *context) {
+ respond_param *rp = param;
+
UNUSED(context);
- if (!li_vrequest_handle_direct(vr)) return LI_HANDLER_GO_ON;
+ if (!li_vrequest_handle_direct(vr))
+ return LI_HANDLER_GO_ON;
+
+ vr->response.http_status = rp->status_code;
- vr->response.http_status = 200;
+ if (!li_http_header_lookup(vr->response.headers, CONST_STR_LEN("content-type")))
+ li_http_header_insert(vr->response.headers, CONST_STR_LEN("Content-Type"), CONST_STR_LEN("text/plain"));
+
+ if (rp->pattern) {
+ g_string_truncate(vr->wrk->tmp_str, 0);
+ li_pattern_eval(vr, vr->wrk->tmp_str, rp->pattern, NULL, NULL, NULL, NULL);
+ li_chunkqueue_append_mem(vr->out, GSTR_LEN(vr->wrk->tmp_str));
+ }
return LI_HANDLER_GO_ON;
}
-static liAction* core_blank(liServer *srv, liWorker *wrk, liPlugin* p, liValue *val, gpointer userdata) {
+static liAction* core_respond(liServer *srv, liWorker *wrk, liPlugin* p, liValue *val, gpointer userdata) {
+ respond_param *rp;
+
UNUSED(wrk); UNUSED(p); UNUSED(userdata);
- if (val) {
- ERROR(srv, "%s", "'blank' action doesn't have parameters");
+ rp = g_slice_new(respond_param);
+
+ if (!val) {
+ // respond;
+ rp->status_code = 200;
+ rp->pattern = NULL;
+ } else if (val->type == LI_VALUE_STRING) {
+ // respond "foo";
+ rp->status_code = 200;
+ rp->pattern = li_pattern_new(srv, val->data.string->str);
+
+ if (!rp->pattern) {
+ g_slice_free(respond_param, rp);
+ ERROR(srv, "%s", "'respond' action takes an optional string as parameter");
+ return NULL;
+ }
+ } else if (val->type == LI_VALUE_NUMBER) {
+ // respond 404;
+ rp->status_code = val->data.number;
+ rp->pattern = NULL;
+ } else if (val->type == LI_VALUE_LIST && val->data.list->len == 2 && g_array_index(val->data.list, liValue*, 0)->type == LI_VALUE_NUMBER && g_array_index(val->data.list, liValue*, 1)->type == LI_VALUE_STRING) {
+ // respond 200 => "foo";
+ rp->status_code = g_array_index(val->data.list, liValue*, 0)->data.number;
+ rp->pattern = li_pattern_new(srv, g_array_index(val->data.list, liValue*, 1)->data.string->str);
+
+ if (!rp->pattern) {
+ g_slice_free(respond_param, rp);
+ ERROR(srv, "%s", "'respond' action takes an optional string as parameter");
+ return NULL;
+ }
+ } else {
+ g_slice_free(respond_param, rp);
+ ERROR(srv, "%s", "'respond' action takes an optional string as parameter");
return NULL;
}
- return li_action_new_function(core_handle_blank, NULL, NULL, NULL);
+ return li_action_new_function(core_handle_respond, NULL, core_respond_free, rp);
}
@@ -1874,7 +1935,7 @@ static const liPluginAction actions[] = {
{ "log.write", core_log_write, NULL },
- { "blank", core_blank, NULL },
+ { "respond", core_respond, NULL },
{ "env.set", core_env_set, NULL },
{ "env.add", core_env_add, NULL },
@@ -8,7 +8,7 @@ class TestHeaderAdd(CurlRequest):
config = """
header.add "Test-Header" => "%{req.query}";
header.add "Test-Header" => "%{req.path}";
-blank;
+respond;
"""
URL = "/path?simple_query"
@@ -24,7 +24,7 @@ class TestHeaderAppend(CurlRequest):
config = """
header.append "Test-Header" => "%{req.query}";
header.append "Test-Header" => "%{req.path}";
-blank;
+respond;
"""
URL = "/path?simple_query"
@@ -40,7 +40,7 @@ class TestHeaderOverwrite(CurlRequest):
config = """
header.overwrite "Test-Header" => "%{req.query}";
header.overwrite "Test-Header" => "%{req.path}";
-blank;
+respond;
"""
URL = "/path?simple_query"
@@ -56,7 +56,7 @@ class TestHeaderRemove(CurlRequest):
config = """
header.add "Test-Header" => "%{req.query}";
header.remove "Test-Header";
-blank;
+respond;
"""
URL = "/path?simple_query"

0 comments on commit cdf1831

Please sign in to comment.