MINOR: httpclient/lua: support more HTTP methods

Add support for HEAD/PUT/POST/DELETE method with the lua httpclient.

This patch use the httpclient_req_gen() function with a different meth
parameter to implement this.

Also change the reg-test to support a POST request with a body.
This commit is contained in:
William Lallemand 2021-10-26 11:43:26 +02:00
parent dec25c3e14
commit dc2cc9008b
2 changed files with 70 additions and 10 deletions

View File

@ -20,8 +20,14 @@ local function cron()
end end
core.Debug('CRON port:' .. vtc_port) core.Debug('CRON port:' .. vtc_port)
local body = ""
for i = 0, 200 do
body = body .. i .. ' ABCDEFGHIJKLMNOPQRSTUVWXYZ\n'
end
local httpclient = core.httpclient() local httpclient = core.httpclient()
local response = httpclient:get{url="http://127.0.0.1:" .. vtc_port, body="foobar-is-the-new-toto"} local response = httpclient:post{url="http://127.0.0.1:" .. vtc_port, body=body}
core.Info("Received: " .. response.body) core.Info("Received: " .. response.body)
end end

View File

@ -7038,7 +7038,7 @@ __LJMP static int hlua_httpclient_get_headers(lua_State *L, struct hlua_httpclie
* in the lua buffer, once the httpclient finished its job, push the result on * in the lua buffer, once the httpclient finished its job, push the result on
* the stack * the stack
*/ */
__LJMP static int hlua_httpclient_get_yield(lua_State *L, int status, lua_KContext ctx) __LJMP static int hlua_httpclient_send_yield(lua_State *L, int status, lua_KContext ctx)
{ {
struct buffer *tr; struct buffer *tr;
int res; int res;
@ -7076,7 +7076,7 @@ __LJMP static int hlua_httpclient_get_yield(lua_State *L, int status, lua_KConte
task_wakeup(hlua->task, TASK_WOKEN_MSG); task_wakeup(hlua->task, TASK_WOKEN_MSG);
MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_get_yield, TICK_ETERNITY, 0)); MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_send_yield, TICK_ETERNITY, 0));
return 0; return 0;
} }
@ -7152,12 +7152,10 @@ struct http_hdr *hlua_httpclient_table_to_hdrs(lua_State *L)
/* /*
* Sends and receive an HTTP request * Send an HTTP request and wait for a response
*
* httpclient.get(url, headers)
*/ */
__LJMP static int hlua_httpclient_get(lua_State *L) __LJMP static int hlua_httpclient_send(lua_State *L, enum http_meth_t meth)
{ {
struct hlua_httpclient *hlua_hc; struct hlua_httpclient *hlua_hc;
struct http_hdr *hdrs = NULL; struct http_hdr *hdrs = NULL;
@ -7202,7 +7200,7 @@ __LJMP static int hlua_httpclient_get(lua_State *L)
hlua_hc = hlua_checkhttpclient(L, 1); 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 = HTTP_METH_GET; hlua_hc->hc->req.meth = meth;
/* update the httpclient callbacks */ /* update the httpclient callbacks */
hlua_hc->hc->ops.res_stline = hlua_httpclient_res_cb; hlua_hc->hc->ops.res_stline = hlua_httpclient_res_cb;
@ -7211,7 +7209,7 @@ __LJMP static int hlua_httpclient_get(lua_State *L)
hlua_hc->hc->ops.res_end = hlua_httpclient_res_cb; hlua_hc->hc->ops.res_end = hlua_httpclient_res_cb;
httpclient_req_gen(hlua_hc->hc, hlua_hc->hc->req.url, HTTP_METH_GET, hdrs, ist(body_str)); httpclient_req_gen(hlua_hc->hc, hlua_hc->hc->req.url, meth, hdrs, ist(body_str));
httpclient_start(hlua_hc->hc); httpclient_start(hlua_hc->hc);
/* free the temporary headers array */ /* free the temporary headers array */
@ -7231,10 +7229,62 @@ __LJMP static int hlua_httpclient_get(lua_State *L)
luaL_buffinit(L, &hlua_hc->b); luaL_buffinit(L, &hlua_hc->b);
lua_pushstring(L, "body"); lua_pushstring(L, "body");
MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_get_yield, TICK_ETERNITY, 0)); MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_send_yield, TICK_ETERNITY, 0));
return 0; return 0;
} }
/*
* Sends an HTTP HEAD request and wait for a response
*
* httpclient:head(url, headers, payload)
*/
__LJMP static int hlua_httpclient_head(lua_State *L)
{
return hlua_httpclient_send(L, HTTP_METH_HEAD);
}
/*
* Send an HTTP GET request and wait for a response
*
* httpclient:get(url, headers, payload)
*/
__LJMP static int hlua_httpclient_get(lua_State *L)
{
return hlua_httpclient_send(L, HTTP_METH_GET);
}
/*
* Sends an HTTP PUT request and wait for a response
*
* httpclient:put(url, headers, payload)
*/
__LJMP static int hlua_httpclient_put(lua_State *L)
{
return hlua_httpclient_send(L, HTTP_METH_PUT);
}
/*
* Send an HTTP POST request and wait for a response
*
* httpclient:post(url, headers, payload)
*/
__LJMP static int hlua_httpclient_post(lua_State *L)
{
return hlua_httpclient_send(L, HTTP_METH_POST);
}
/*
* Sends an HTTP DELETE request and wait for a response
*
* httpclient:delete(url, headers, payload)
*/
__LJMP static int hlua_httpclient_delete(lua_State *L)
{
return hlua_httpclient_send(L, HTTP_METH_DELETE);
}
/* /*
* *
* *
@ -11918,6 +11968,10 @@ lua_State *hlua_init_state(int thread_num)
lua_pushstring(L, "__index"); lua_pushstring(L, "__index");
lua_newtable(L); lua_newtable(L);
hlua_class_function(L, "get", hlua_httpclient_get); hlua_class_function(L, "get", hlua_httpclient_get);
hlua_class_function(L, "head", hlua_httpclient_head);
hlua_class_function(L, "put", hlua_httpclient_put);
hlua_class_function(L, "post", hlua_httpclient_post);
hlua_class_function(L, "delete", hlua_httpclient_delete);
lua_settable(L, -3); /* Sets the __index entry. */ lua_settable(L, -3); /* Sets the __index entry. */
/* Register the garbage collector entry. */ /* Register the garbage collector entry. */
lua_pushstring(L, "__gc"); lua_pushstring(L, "__gc");