mirror of
https://github.com/luanti-org/luanti.git
synced 2025-09-15 18:57:08 +00:00
Support HEAD and PATCH methods in http api
This commit is contained in:
parent
d937cd9b90
commit
893a74f9d7
5 changed files with 37 additions and 22 deletions
|
@ -282,20 +282,28 @@ HTTPFetchOngoing::HTTPFetchOngoing(const HTTPFetchRequest &request_,
|
|||
case HTTP_GET:
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
|
||||
break;
|
||||
case HTTP_HEAD:
|
||||
// This is kinda pointless right now, since we don't return response headers (TODO?)
|
||||
curl_easy_setopt(curl, CURLOPT_NOBODY, 1);
|
||||
break;
|
||||
case HTTP_POST:
|
||||
curl_easy_setopt(curl, CURLOPT_POST, 1);
|
||||
break;
|
||||
case HTTP_PUT:
|
||||
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
|
||||
break;
|
||||
case HTTP_PATCH:
|
||||
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PATCH");
|
||||
break;
|
||||
case HTTP_DELETE:
|
||||
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE");
|
||||
break;
|
||||
}
|
||||
const bool has_request_body = request.method != HTTP_GET && request.method != HTTP_HEAD;
|
||||
|
||||
// Set data from fields or raw_data
|
||||
if (request.multipart) {
|
||||
assert(request.method != HTTP_GET);
|
||||
assert(has_request_body);
|
||||
multipart_mime = curl_mime_init(curl);
|
||||
for (auto &it : request.fields) {
|
||||
curl_mimepart *part = curl_mime_addpart(multipart_mime);
|
||||
|
@ -303,7 +311,7 @@ HTTPFetchOngoing::HTTPFetchOngoing(const HTTPFetchRequest &request_,
|
|||
curl_mime_data(part, it.second.c_str(), it.second.size());
|
||||
}
|
||||
curl_easy_setopt(curl, CURLOPT_MIMEPOST, multipart_mime);
|
||||
} else if (request.method != HTTP_GET) {
|
||||
} else if (has_request_body) {
|
||||
if (request.fields.empty()) {
|
||||
// Note that we need to set this to an empty buffer (not NULL)
|
||||
// even if no data is to be sent.
|
||||
|
|
|
@ -31,8 +31,10 @@ namespace {
|
|||
enum HttpMethod : u8
|
||||
{
|
||||
HTTP_GET,
|
||||
HTTP_HEAD,
|
||||
HTTP_POST,
|
||||
HTTP_PUT,
|
||||
HTTP_PATCH,
|
||||
HTTP_DELETE,
|
||||
};
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue