Skip to content

Commit

Permalink
Object 104
Browse files Browse the repository at this point in the history
  • Loading branch information
luozhiya committed Jan 4, 2025
1 parent cfe1b53 commit 5096685
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lua/fittencode/http/libcurl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 22 additions & 8 deletions lua/fittencode/http/libcurl/src/api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

#include <iostream>
#include <string_view>
#include <format>

using namespace std::literals;

extern "C" {
#include <lauxlib.h>
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 5096685

Please sign in to comment.