From ff76d854fc286e683a7ec5d16ac24fb03f232941 Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Wed, 28 Aug 2024 01:37:02 +0100 Subject: [PATCH] extend libfetch lua glue to other HTTP methods --- pkgs/fetch-freebsd/lua-glue.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/pkgs/fetch-freebsd/lua-glue.c b/pkgs/fetch-freebsd/lua-glue.c index fbb03ba..23a4392 100644 --- a/pkgs/fetch-freebsd/lua-glue.c +++ b/pkgs/fetch-freebsd/lua-glue.c @@ -57,8 +57,38 @@ int lp_fetch(lua_State *L) { return lp_fetched(L, fp, &stat); } +int lp_request(lua_State *L) { + const char * request_method = luaL_checkstring(L, 1); + const char * url_string = luaL_checkstring(L, 2); + const char * flags = luaL_optstring(L, 3, ""); + time_t if_modified_since = luaL_optinteger(L, 4, 0); + const char * content_type = lua_tostring(L, 5); + const char * body = lua_tostring(L, 6); + + struct url *url = fetchParseURL(url_string); + if(url == NULL) { + return lp_error(L, -1, "url not parseable"); + } + url->ims_time = if_modified_since; + struct url_stat stat; + + FXRETTYPE fp = http_request_body(url, + request_method, + &stat, + NULL, /* no proxy */ + flags, + content_type, + body); + fetchFreeURL(url); + if(fp == NULL) { + return lp_error(L, fetchLastErrCode, fetchLastErrString); + } + return lp_fetched(L, fp, &stat); +} + static const struct luaL_Reg funcs [] = { {"fetch", lp_fetch}, + {"request", lp_request}, {NULL, NULL} /* sentinel */ };