diff --git a/doc/lua-api/index.rst b/doc/lua-api/index.rst index 9aed82c4a..f22fac3fb 100644 --- a/doc/lua-api/index.rst +++ b/doc/lua-api/index.rst @@ -1870,6 +1870,7 @@ HTTPClient class :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. + :param integer request.timeout: Optional timeout parameter, set a "timeout server" on the connections. :returns: Lua table containing the response diff --git a/include/haproxy/http_client-t.h b/include/haproxy/http_client-t.h index 8cebc70a3..6efb671a9 100644 --- a/include/haproxy/http_client-t.h +++ b/include/haproxy/http_client-t.h @@ -29,6 +29,7 @@ struct httpclient { } ops; struct sockaddr_storage *dst; /* destination address */ struct appctx *appctx; /* HTTPclient appctx */ + int timeout_server; /* server timeout in ms */ void *caller; /* ptr of the caller */ unsigned int flags; /* other flags */ }; diff --git a/include/haproxy/http_client.h b/include/haproxy/http_client.h index 96ec09294..cc4c9fe62 100644 --- a/include/haproxy/http_client.h +++ b/include/haproxy/http_client.h @@ -9,6 +9,7 @@ struct httpclient *httpclient_new(void *caller, enum http_meth_t meth, struct is struct appctx *httpclient_start(struct httpclient *hc); int httpclient_set_dst(struct httpclient *hc, const char *dst); +void httpclient_set_timeout(struct httpclient *hc, int timeout); int httpclient_res_xfer(struct httpclient *hc, struct buffer *dst); int httpclient_req_gen(struct httpclient *hc, const struct ist url, enum http_meth_t meth, const struct http_hdr *hdrs, const struct ist payload); int httpclient_req_xfer(struct httpclient *hc, struct ist src, int end); diff --git a/src/hlua.c b/src/hlua.c index 0098d02b8..323a86bcd 100644 --- a/src/hlua.c +++ b/src/hlua.c @@ -7218,6 +7218,7 @@ __LJMP static int hlua_httpclient_send(lua_State *L, enum http_meth_t meth) struct hlua *hlua; const char *url_str = NULL; const char *body_str = NULL; + int timeout; size_t buf_len; int ret; @@ -7245,6 +7246,12 @@ __LJMP static int hlua_httpclient_send(lua_State *L, enum http_meth_t meth) } lua_pop(L, 1); + ret = lua_getfield(L, -1, "timeout"); + if (ret == LUA_TNUMBER) { + timeout = lua_tointeger(L, -1); + httpclient_set_timeout(hlua_hc->hc, timeout); + } + ret = lua_getfield(L, -1, "headers"); if (ret == LUA_TTABLE) { hdrs = hlua_httpclient_table_to_hdrs(L); diff --git a/src/http_client.c b/src/http_client.c index f2b60ae2f..26bfaa8fc 100644 --- a/src/http_client.c +++ b/src/http_client.c @@ -405,6 +405,12 @@ error: return ret; } +/* Set the 'timeout server' in ms for the next httpclient request */ +void httpclient_set_timeout(struct httpclient *hc, int timeout) +{ + hc->timeout_server = timeout; +} + /* * Sets a destination for the httpclient from an HAProxy addr format * This will prevent to determine the destination from the URL @@ -484,6 +490,10 @@ struct appctx *httpclient_start(struct httpclient *hc) goto out_free_appctx; } + /* set the "timeout server" */ + s->req.wto = hc->timeout_server; + s->res.rto = hc->timeout_server; + /* if httpclient_set_dst() was used, sets the alternative address */ if (hc->dst) ss_dst = hc->dst;