MINOR: httpclient/lua: add 'dst' optionnal field

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.
This commit is contained in:
William Lallemand 2022-02-17 20:00:23 +01:00
parent 7b2e0ee1c1
commit 4f4f2b7b5f
4 changed files with 19 additions and 5 deletions

View 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.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 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.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 :returns: Lua table containing the response
.. code-block:: lua .. code-block:: lua
local httpclient = core.httpclient() 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" }, ["content-type"] = { "text/html" },
["cache-control"] = { "no-cache", "no-store" }, ["cache-control"] = { "no-cache", "no-store" },
}, },
body = "<html><body><h1>invalid request<h1></body></html>" body = "<html><body><h1>invalid request<h1></body></html>",
} }
.. ..

View File

@ -42,7 +42,7 @@ local function cron()
core.Info("Third httpclient request") core.Info("Third httpclient request")
local httpclient3 = core.httpclient() 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 end

View File

@ -49,10 +49,15 @@ haproxy h1 -conf {
mode http mode http
http-request use-service lua.fakeserv http-request use-service lua.fakeserv
listen li1
mode http
bind unix@${testdir}/srv3
server srv3 ${s3_addr}:${s3_port}
} -start } -start
client c0 -connect ${h1_fe1_sock} { 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 rxresp
expect resp.status == 200 expect resp.status == 200
} -run } -run

View 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) if (lua_gettop(L) != 2 || lua_type(L, -1) != LUA_TTABLE)
WILL_LJMP(luaL_error(L, "'get' needs a table as argument")); 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"); ret = lua_getfield(L, -1, "url");
if (ret == LUA_TSTRING) { if (ret == LUA_TSTRING) {
url_str = lua_tostring(L, -1); 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; return 0;
} }
hlua_hc = hlua_checkhttpclient(L, 1);
hlua_hc->hc->req.url = istdup(ist(url_str)); hlua_hc->hc->req.url = istdup(ist(url_str));
hlua_hc->hc->req.meth = meth; hlua_hc->hc->req.meth = meth;