]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: httpclient/lua: add 'dst' optionnal field
authorWilliam Lallemand <wlallemand@haproxy.org>
Thu, 17 Feb 2022 19:00:23 +0000 (20:00 +0100)
committerWilliam Lallemand <wlallemand@haproxy.org>
Thu, 17 Feb 2022 19:07:00 +0000 (20:07 +0100)
The 'dst' optionnal field on a httpclient request can be used to set an
alternative server address in the haproxy address format. Which means it
could be use with unix@, ipv6@ etc.

Should fix issue #1471.

doc/lua-api/index.rst
reg-tests/lua/lua_httpclient.lua
reg-tests/lua/lua_httpclient.vtc
src/hlua.c

index bf96a47b4818a64f0093f0f945f7211e2773bc7e..9aed82c4a2656c73784713287498f635381ad0b1 100644 (file)
@@ -1869,13 +1869,14 @@ HTTPClient class
   :param string request.url: Is a mandatory parameter for the request that contains the URL.
   :param string request.body: Is an optional parameter for the request that contains the body to send.
   :param table request.headers: Is an optional parameter for the request that contains the headers to send.
+  :param table request.dst: Is an optional parameter for the destination in haproxy address format.
   :returns: Lua table containing the response
 
 
 .. code-block:: lua
 
   local httpclient = core.httpclient()
-  local response = httpclient:post{url="http://127.0.0.1", body=body}
+  local response = httpclient:post{url="http://127.0.0.1", body=body, dst="unix@/var/run/http.sock"}
 
 ..
 
@@ -1888,7 +1889,7 @@ HTTPClient class
         ["content-type"]  = { "text/html" },
         ["cache-control"] = { "no-cache", "no-store" },
     },
-    body = "<html><body><h1>invalid request<h1></body></html>"
+    body = "<html><body><h1>invalid request<h1></body></html>",
   }
 ..
 
index e9ec0f02c731dd45048914c45e8986cf27c870f9..b5a5180c12a9544e66d969b120cfc45024edbc20 100644 (file)
@@ -42,7 +42,7 @@ local function cron()
 
        core.Info("Third httpclient request")
        local httpclient3 = core.httpclient()
-       local response3 = httpclient3:get{url="http://127.0.0.1:" .. vtc_port3, headers={ [ "Host" ] = { "foobar.haproxy.local" } }}
+       local response3 = httpclient3:get{url="http://127.0.0.1", dst = vtc_port3, headers={ [ "Host" ] = { "foobar.haproxy.local" } }}
 
 end
 
index 7c55d54bf331eb76abdc5a3ad86023f3312c9f96..0850ddb5f3f21cf846540c82fe79955d0be0de02 100644 (file)
@@ -49,10 +49,15 @@ haproxy h1 -conf {
         mode http
         http-request use-service lua.fakeserv
 
+   listen li1
+       mode http
+       bind unix@${testdir}/srv3
+       server srv3 ${s3_addr}:${s3_port}
+
 } -start
 
 client c0 -connect ${h1_fe1_sock} {
-    txreq -url "/" -hdr "vtcport: ${s1_port}" -hdr "vtcport2: ${s2_port}" -hdr "vtcport3: ${s3_port}"
+    txreq -url "/" -hdr "vtcport: ${s1_port}" -hdr "vtcport2: ${s2_port}" -hdr "vtcport3: unix@${testdir}/srv3"
     rxresp
     expect resp.status == 200
 } -run
index 06d4ce0c39ac156c3903448cf3ac9883ea9d8d1b..0098d02b8fb54a687dc4ff9c4b1da0420ee94030 100644 (file)
@@ -7230,6 +7230,15 @@ __LJMP static int hlua_httpclient_send(lua_State *L, enum http_meth_t meth)
        if (lua_gettop(L) != 2 || lua_type(L, -1) != LUA_TTABLE)
                WILL_LJMP(luaL_error(L, "'get' needs a table as argument"));
 
+       hlua_hc = hlua_checkhttpclient(L, 1);
+
+       ret = lua_getfield(L, -1, "dst");
+       if (ret == LUA_TSTRING) {
+               if (httpclient_set_dst(hlua_hc->hc, lua_tostring(L, -1)) < 0)
+                       WILL_LJMP(luaL_error(L, "Can't use the 'dst' argument"));
+       }
+       lua_pop(L, 1);
+
        ret = lua_getfield(L, -1, "url");
        if (ret == LUA_TSTRING) {
                url_str = lua_tostring(L, -1);
@@ -7254,7 +7263,6 @@ __LJMP static int hlua_httpclient_send(lua_State *L, enum http_meth_t meth)
                return 0;
        }
 
-       hlua_hc = hlua_checkhttpclient(L, 1);
 
        hlua_hc->hc->req.url = istdup(ist(url_str));
        hlua_hc->hc->req.meth = meth;