summaryrefslogtreecommitdiff
path: root/src/hlua.c
diff options
context:
space:
mode:
authorWilliam Lallemand <wlallemand@haproxy.org>2021-10-06 10:57:44 +0200
committerWilliam Lallemand <wlallemand@haproxy.org>2021-10-06 15:21:02 +0200
commit746e6f3f8efe5646201f8d49d833393bacbc8c02 (patch)
tree57ccb3db9d420911b1a2f276d2be0b1b63d3a336 /src/hlua.c
parentef574b2101458d3af6b4c1453a7b5edadf001b66 (diff)
downloadhaproxy-httpclient-lua-2021-10-06.tar.gz
MINOR: httpclient/lua: supports headers via named argumentshaproxy-httpclient-lua-2021-10-06
Migrate the httpclient:get() method to named arguments so we can specify optional arguments. This allows to pass headers as an optional argument as an array. The () in the method call must be replaced by {}: local res = httpclient:get{url="http://127.0.0.1:9000/?s=99", headers= {["X-foo"] = { "salt" }, ["X-bar"] = {"pepper" }}}
Diffstat (limited to 'src/hlua.c')
-rw-r--r--src/hlua.c112
1 files changed, 104 insertions, 8 deletions
diff --git a/src/hlua.c b/src/hlua.c
index 9589473a7..7e759548c 100644
--- a/src/hlua.c
+++ b/src/hlua.c
@@ -7079,6 +7079,77 @@ __LJMP static int hlua_httpclient_get_yield(lua_State *L, int status, lua_KConte
return 0;
}
+
+/*
+ * Allocate and return an array of http_hdr ist extracted from the <headers> lua table
+ *
+ * Caller must free the result
+ */
+struct http_hdr *hlua_httpclient_table_to_hdrs(lua_State *L)
+{
+ struct http_hdr hdrs[global.tune.max_http_hdr];
+ struct http_hdr *result = NULL;
+ uint32_t hdr_num = 0;
+
+ lua_pushnil(L);
+ while (lua_next(L, -2) != 0) {
+ struct ist name, value;
+ const char *n, *v;
+ size_t nlen, vlen;
+
+ if (!lua_isstring(L, -2) || !lua_istable(L, -1)) {
+ /* Skip element if the key is not a string or if the value is not a table */
+ goto next_hdr;
+ }
+
+ n = lua_tolstring(L, -2, &nlen);
+ name = ist2(n, nlen);
+
+ /* Loop on header's values */
+ lua_pushnil(L);
+ while (lua_next(L, -2)) {
+ if (!lua_isstring(L, -1)) {
+ /* Skip the value if it is not a string */
+ goto next_value;
+ }
+
+ v = lua_tolstring(L, -1, &vlen);
+ value = ist2(v, vlen);
+ name = ist2(n, nlen);
+
+ hdrs[hdr_num].n = istdup(name);
+ hdrs[hdr_num].v = istdup(value);
+
+ hdr_num++;
+
+ next_value:
+ lua_pop(L, 1);
+ }
+
+ next_hdr:
+ lua_pop(L, 1);
+
+ }
+
+ if (hdr_num) {
+ /* alloc and copy the headers in the httpclient struct */
+ result = calloc((hdr_num + 1), sizeof(*hdrs));
+ if (!result)
+ goto skip_headers;
+ memcpy(result, hdrs, sizeof(struct http_hdr) * (hdr_num + 1));
+
+ result[hdr_num].n = IST_NULL;
+ result[hdr_num].v = IST_NULL;
+ }
+
+skip_headers:
+ lua_pop(L, 1);
+
+ return result;
+}
+
+
+
/*
* Sends and receive an HTTP request
*
@@ -7088,8 +7159,11 @@ __LJMP static int hlua_httpclient_get_yield(lua_State *L, int status, lua_KConte
__LJMP static int hlua_httpclient_get(lua_State *L)
{
struct hlua_httpclient *hlua_hc;
+ struct http_hdr *hdrs = NULL;
+ struct http_hdr *hdrs_i = NULL;
struct hlua *hlua;
- const char *url_str;
+ const char *url_str = NULL;
+ int ret;
hlua = hlua_gethlua(L);
@@ -7097,14 +7171,25 @@ __LJMP static int hlua_httpclient_get(lua_State *L)
WILL_LJMP(luaL_error(L, "The 'get' function is only allowed in "
"'frontend', 'backend' or 'task'"));
- if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
- WILL_LJMP(luaL_error(L, "'get' needs between 1 or 2 arguments"));
+ if (lua_gettop(L) != 2 || lua_type(L, -1) != LUA_TTABLE)
+ WILL_LJMP(luaL_error(L, "'get' needs a table as argument"));
- if (lua_type(L, -1) != LUA_TSTRING)
- WILL_LJMP(luaL_error(L, "'get' takes an URL as a string arugment"));
+ ret = lua_getfield(L, -1, "url");
+ if (ret == LUA_TSTRING) {
+ url_str = lua_tostring(L, -1);
+ }
+ lua_pop(L, 1);
+
+ ret = lua_getfield(L, -1, "headers");
+ if (ret == LUA_TTABLE) {
+ hdrs = hlua_httpclient_table_to_hdrs(L);
+ }
+ lua_pop(L, 1);
- /* arg 1: URL */
- url_str = lua_tostring(L, -1);
+ if (!url_str) {
+ WILL_LJMP(luaL_error(L, "'get' need a 'url' argument"));
+ return 0;
+ }
hlua_hc = hlua_checkhttpclient(L, 1);
@@ -7118,9 +7203,20 @@ __LJMP static int hlua_httpclient_get(lua_State *L)
hlua_hc->hc->ops.res_end = hlua_httpclient_res_cb;
- httpclient_req_gen(hlua_hc->hc, hlua_hc->hc->req.url, HTTP_METH_GET, NULL);
+ httpclient_req_gen(hlua_hc->hc, hlua_hc->hc->req.url, HTTP_METH_GET, hdrs);
httpclient_start(hlua_hc->hc);
+ /* free the temporary headers array */
+ hdrs_i = hdrs;
+ while (hdrs_i && isttest(hdrs_i->n)) {
+ istfree(&hdrs_i->n);
+ istfree(&hdrs_i->v);
+ hdrs_i++;
+ }
+ ha_free(&hdrs);
+
+
+
/* we return a "res" object */
lua_newtable(L);