Allow http.post to specify form part content type

This commit is contained in:
Tamás Bálint Misius
2025-01-18 21:37:28 +01:00
parent 48fb16292a
commit 7aee6b77e3
3 changed files with 46 additions and 22 deletions

View File

@@ -16,6 +16,7 @@ namespace http
ByteString name; ByteString name;
ByteString value; ByteString value;
std::optional<ByteString> filename; std::optional<ByteString> filename;
std::optional<ByteString> contentType;
}; };
using StringData = ByteString; using StringData = ByteString;
using FormData = std::vector<FormItem>; using FormData = std::vector<FormItem>;

View File

@@ -421,6 +421,10 @@ namespace http
{ {
HandleCURLcode(curl_mime_filename(part, field.filename->c_str())); HandleCURLcode(curl_mime_filename(part, field.filename->c_str()));
} }
if (field.contentType.has_value())
{
HandleCURLcode(curl_mime_type(part, field.contentType->c_str()));
}
} }
HandleCURLcode(curl_easy_setopt(handle->curlEasy, CURLOPT_MIMEPOST, handle->curlPostFields)); HandleCURLcode(curl_easy_setopt(handle->curlEasy, CURLOPT_MIMEPOST, handle->curlPostFields));
#else #else

View File

@@ -255,37 +255,54 @@ static int request(lua_State *L, bool isPost)
{ {
for (auto i = 0U; i < size; ++i) for (auto i = 0U; i < size; ++i)
{ {
auto &formItem = formData.emplace_back();
lua_rawgeti(L, 2, i + 1); lua_rawgeti(L, 2, i + 1);
if (!lua_istable(L, -1)) if (!lua_istable(L, -1))
{ {
luaL_error(L, "form item %i is not a table", i + 1); luaL_error(L, "form item %i is not a table", i + 1);
} }
lua_rawgeti(L, -1, 1);
if (!lua_isstring(L, -1))
{
luaL_error(L, "name of form item %i is not a string", i + 1);
}
auto name = tpt_lua_toByteString(L, -1);
lua_pop(L, 1);
lua_rawgeti(L, -1, 2);
if (!lua_isstring(L, -1))
{
luaL_error(L, "value of form item %i is not a string", i + 1);
}
auto value = tpt_lua_toByteString(L, -1);
lua_pop(L, 1);
std::optional<ByteString> filename;
lua_rawgeti(L, -1, 3);
if (!lua_isnoneornil(L, -1))
{ {
lua_rawgeti(L, -1, 1);
if (!lua_isstring(L, -1)) if (!lua_isstring(L, -1))
{ {
luaL_error(L, "filename of form item %i is not a string", i + 1); luaL_error(L, "name of form item %i is not a string", i + 1);
} }
filename = tpt_lua_toByteString(L, -1); formItem.name = tpt_lua_toByteString(L, -1);
lua_pop(L, 1);
}
{
lua_rawgeti(L, -1, 2);
if (!lua_isstring(L, -1))
{
luaL_error(L, "value of form item %i is not a string", i + 1);
}
formItem.value = tpt_lua_toByteString(L, -1);
lua_pop(L, 1);
}
{
lua_rawgeti(L, -1, 3);
if (!lua_isnoneornil(L, -1))
{
if (!lua_isstring(L, -1))
{
luaL_error(L, "filename of form item %i is not a string", i + 1);
}
formItem.filename = tpt_lua_toByteString(L, -1);
}
lua_pop(L, 1);
}
{
lua_rawgeti(L, -1, 4);
if (!lua_isnoneornil(L, -1))
{
if (!lua_isstring(L, -1))
{
luaL_error(L, "content type of form item %i is not a string", i + 1);
}
formItem.contentType = tpt_lua_toByteString(L, -1);
}
lua_pop(L, 1);
} }
lua_pop(L, 1);
formData.push_back({ name, value, filename });
lua_pop(L, 1); lua_pop(L, 1);
} }
} }
@@ -295,7 +312,9 @@ static int request(lua_State *L, bool isPost)
while (lua_next(L, 2)) while (lua_next(L, 2))
{ {
lua_pushvalue(L, -2); lua_pushvalue(L, -2);
formData.push_back({ tpt_lua_toByteString(L, -1), tpt_lua_toByteString(L, -2) }); auto &formItem = formData.emplace_back();
formItem.name = tpt_lua_toByteString(L, -1);
formItem.value = tpt_lua_toByteString(L, -2);
lua_pop(L, 2); lua_pop(L, 2);
} }
} }