MINOR: httpclient/lua: ability to set a server timeout

Add the ability to set a "server timeout" on the httpclient with either
the httpclient_set_timeout() API or the timeout argument in a request.

Issue #1470.
diff --git a/doc/lua-api/index.rst b/doc/lua-api/index.rst
index 9aed82c..f22fac3 100644
--- a/doc/lua-api/index.rst
+++ b/doc/lua-api/index.rst
@@ -1870,6 +1870,7 @@
   :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 8cebc70..6efb671 100644
--- a/include/haproxy/http_client-t.h
+++ b/include/haproxy/http_client-t.h
@@ -29,6 +29,7 @@
 	} 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 96ec092..cc4c9fe 100644
--- a/include/haproxy/http_client.h
+++ b/include/haproxy/http_client.h
@@ -9,6 +9,7 @@
 
 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 0098d02..323a86b 100644
--- a/src/hlua.c
+++ b/src/hlua.c
@@ -7218,6 +7218,7 @@
 	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 @@
 	}
 	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 f2b60ae..26bfaa8 100644
--- a/src/http_client.c
+++ b/src/http_client.c
@@ -405,6 +405,12 @@
 	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 @@
 		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;