From 5096685293bc814316c7a6cb527517e336927062 Mon Sep 17 00:00:00 2001 From: luozhiya Date: Sat, 4 Jan 2025 23:33:32 +0800 Subject: [PATCH] Object 104 --- lua/fittencode/http/libcurl/CMakeLists.txt | 2 +- lua/fittencode/http/libcurl/src/api.cc | 30 ++++++++++++++++------ 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/lua/fittencode/http/libcurl/CMakeLists.txt b/lua/fittencode/http/libcurl/CMakeLists.txt index 47e7b01..e306e69 100644 --- a/lua/fittencode/http/libcurl/CMakeLists.txt +++ b/lua/fittencode/http/libcurl/CMakeLists.txt @@ -4,7 +4,7 @@ cmake_minimum_required(VERSION 3.10) project(libcurl LANGUAGES CXX) # Set C++ standard -set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) # Find Lua diff --git a/lua/fittencode/http/libcurl/src/api.cc b/lua/fittencode/http/libcurl/src/api.cc index 438df9a..2d90604 100644 --- a/lua/fittencode/http/libcurl/src/api.cc +++ b/lua/fittencode/http/libcurl/src/api.cc @@ -4,6 +4,9 @@ #include #include +#include + +using namespace std::literals; extern "C" { #include @@ -75,20 +78,31 @@ static int l_fetch(lua_State *L) { luaL_error(L, "Unsupported method: %s", value.data()); } } - } - else if (key == "headers") { - } - else if (key == "body") { - } - else if (key == "timeout") { + } else if (key == "headers") { + if (lua_istable(L, -1)) { + // 遍历headers table + lua_pushnil(L); // 第一个key,nil表示从第一个元素开始 + curl_slist *headers = nullptr; + while (lua_next(L, -2) != 0) { + std::string_view key = lua_tostring(L, -2); // 获取key + std::string_view value = lua_tostring(L, -1); // 获取value + // CURLOPT_HEADER + std::string kv = std::format("%s: %s", key.data(), value.data()); + headers = curl_slist_append(headers, kv.data()); + } + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); + } else { + // + } + } else if (key == "body") { + } else if (key == "timeout") { if (lua_isnumber(L, -1)) { int timeout = lua_tointeger(L, -1); curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout); } else { luaL_error(L, "Expected a number at index 2"); } - } - else if (key == "on_create") { + } else if (key == "on_create") { // if (lua_isfunction(L, -1)) { // lua_pushvalue(L, -1); // } else {