1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-08-11 17:51:04 +00:00

Support HEAD and PATCH methods in http api

This commit is contained in:
sfan5 2025-04-26 15:17:25 +02:00
parent d937cd9b90
commit 893a74f9d7
5 changed files with 37 additions and 22 deletions

View file

@ -7,6 +7,7 @@
#include "common/c_content.h"
#include "lua_api/l_http.h"
#include "cpp_api/s_security.h"
#include "util/enum_string.h"
#include "httpfetch.h"
#include "settings.h"
#include "debug.h"
@ -19,6 +20,16 @@
lua_pushcfunction(L, l_http_##name); \
lua_settable(L, -3);
const static EnumString es_HttpMethod[] = {
{HTTP_GET, "GET"},
{HTTP_HEAD, "HEAD"},
{HTTP_POST, "POST"},
{HTTP_PUT, "PUT"},
{HTTP_PATCH, "PATCH"},
{HTTP_DELETE, "DELETE"},
{0, nullptr}
};
#if USE_CURL
void ModApiHttp::read_http_fetch_request(lua_State *L, HTTPFetchRequest &req)
{
@ -32,17 +43,8 @@ void ModApiHttp::read_http_fetch_request(lua_State *L, HTTPFetchRequest &req)
req.timeout *= 1000;
lua_getfield(L, 1, "method");
if (lua_isstring(L, -1)) {
std::string mth = getstringfield_default(L, 1, "method", "");
if (mth == "GET")
req.method = HTTP_GET;
else if (mth == "POST")
req.method = HTTP_POST;
else if (mth == "PUT")
req.method = HTTP_PUT;
else if (mth == "DELETE")
req.method = HTTP_DELETE;
}
if (lua_isstring(L, -1))
string_to_enum(es_HttpMethod, req.method, lua_tostring(L, -1));
lua_pop(L, 1);
// post_data: if table, post form data, otherwise raw data DEPRECATED use data and method instead
@ -50,8 +52,7 @@ void ModApiHttp::read_http_fetch_request(lua_State *L, HTTPFetchRequest &req)
if (lua_isnil(L, 2)) {
lua_pop(L, 1);
lua_getfield(L, 1, "data");
}
else {
} else {
req.method = HTTP_POST;
}