Skip to content

Commit

Permalink
Object 108
Browse files Browse the repository at this point in the history
  • Loading branch information
luozhiya committed Jan 5, 2025
1 parent aed7672 commit 366fb10
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 26 deletions.
4 changes: 2 additions & 2 deletions lua/fittencode/http/libcurl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ find_package(Lua51 REQUIRED)
# Find the libcurl package
find_package(CURL REQUIRED)

include_directories(${LUA_INCLUDE_DIR})
include_directories(${LUA51_INCLUDE_DIR})

# Add the MD5 source file
file(GLOB SOURCES ./src/**.cc)
Expand All @@ -26,7 +26,7 @@ add_library(libcurl SHARED ${SOURCES})
set(CMAKE_SHARED_LIBRARY_PREFIX "")

# Link the library with Lua
target_link_libraries(libcurl PRIVATE ${LUA_LIBRARIES})
target_link_libraries(libcurl PRIVATE ${LUA51_LIBRARIES})

# Link the library with libcurl
target_link_libraries(libcurl PRIVATE ${CURL_LIBRARIES})
Expand Down
102 changes: 78 additions & 24 deletions lua/fittencode/http/libcurl/src/api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
#include <stdlib.h>
#include <string.h>

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

using namespace std::literals;

Expand All @@ -31,6 +31,15 @@ static int l_global_init(lua_State *L) {
return 1;
}

static size_t l_easy_writefunction(void *ptr, size_t size, size_t nmemb, void *stream) {
lua_State *L = (lua_State *) stream;

lua_getfield(L, -1, "on_stream");
lua_pushlstring(L, (char *) ptr, nmemb * size);
lua_call(L, 1, 0);
return nmemb * size;
}

static int l_fetch(lua_State *L) {
// 1. url
std::string_view url = luaL_checkstring(L, 1);
Expand All @@ -50,27 +59,33 @@ static int l_fetch(lua_State *L) {
curl_easy_setopt(curl, CURLOPT_URL, url.data());

std::string readBuffer;
curl_slist *headers = nullptr;

int on_create = LUA_REFNIL;
int on_input = LUA_REFNIL;
int on_once = LUA_REFNIL;
int on_stream = LUA_REFNIL;
int on_error = LUA_REFNIL;
int on_exit = LUA_REFNIL;

// 3. 遍历options table
std::cout << lua_gettop(L) << std::endl;
// std::cout << lua_gettop(L) << std::endl;
lua_pushnil(L); // 第一个key,nil表示从第一个元素开始
std::cout << lua_gettop(L) << std::endl;
// std::cout << lua_gettop(L) << std::endl;
// 因为 fetch 第二个参数是options table,所以从2开始遍历
// lua_next(L, 2) 返回0表示遍历结束
// lua_next(L, 2) 返回1表示成功
// lua_next 会先弹出一个top,然后再push一个key-value对,所以需要先push一个nil
int v = lua_next(L, 2);
std::cout << "lua_next: " << v << std::endl;
// std::cout << "lua_next: " << v << std::endl;
while (v != 0) {
std::cout << lua_gettop(L) << std::endl;
// std::cout << lua_gettop(L) << std::endl;
// key在-2位置,value在-1位置
std::string_view key = lua_tostring(L, -2); // 获取key
std::cout << "key: " << key << std::endl;
// std::cout << "key: " << key << std::endl;
if (key == "method") {
if (lua_isstring(L, -1)) {
std::string_view value = lua_tostring(L, -1); // 获取string类型的value
std::cout << "method: " << value << std::endl;
// std::cout << "method: " << value << std::endl;
if (value == "GET") {
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
} else if (value == "POST") {
Expand All @@ -81,6 +96,7 @@ static int l_fetch(lua_State *L) {
}
} else if (key == "headers") {
if (lua_istable(L, -1)) {
curl_slist *headers = nullptr;
// 遍历headers table
lua_pushnil(L); // 第一个key,nil表示从第一个元素开始
while (lua_next(L, -2) != 0) {
Expand All @@ -90,38 +106,76 @@ static int l_fetch(lua_State *L) {
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") {

if (lua_isstring(L, -1)) {
std::string_view value = lua_tostring(L, -1); // 获取string类型的value
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, value.data());
} else {
// luaL_error(L, "Expected a string at index 2");
}
} 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");
// luaL_error(L, "Expected a number at index 2");
}
} else if (key == "on_create") {
// if (lua_isfunction(L, -1)) {
// lua_pushvalue(L, -1);
// } else {
// luaL_error(L, "Expected a function at index 2");
// }
// handle function
if (lua_isfunction(L, -1)) {
// 将 on_create 保存到 Lua 注册表中
// lua_pushvalue(L, -1); // 拷贝 on_create 函数到栈顶
// lua_setglobal(L, "saved_on_create"); // 将其设置为全局变量 saved_on_create
} else {
// luaL_error(L, "Expected a function at index 2");
}
} else if (key == "on_input") {
// handle function
if (lua_isfunction(L, -1)) {
} else {
// luaL_error(L, "Expected a function at index 2");
}
} else if (key == "on_once") {
// handle function
if (lua_isfunction(L, -1)) {
} else {
// luaL_error(L, "Expected a function at index 2");
}
} else if (key == "on_stream") {
// handle function
if (lua_isfunction(L, -1)) {
// lua_getfield(L, -1, "on_stream");
} else {
// luaL_error(L, "Expected a function at index 2");
}
} else if (key == "on_error") {
// handle function
if (lua_isfunction(L, -1)) {
} else {
// luaL_error(L, "Expected a function at index 2");
}
} else if (key == "on_exit") {
// handle function
if (lua_isfunction(L, -1)) {
} else {
// luaL_error(L, "Expected a function at index 2");
}
}
// 弹出value,保留key用于下一次迭代
lua_pop(L, 1);
v = lua_next(L, 2);
std::cout << "lua_next: " << v << std::endl;
// std::cout << "lua_next: " << v << std::endl;
}

curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

// Set the callback function to write data to string
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, l_easy_writefunction);

// Pass the string to store the response
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, L);

CURLcode res = curl_easy_perform(curl);
// Check for errors
Expand Down Expand Up @@ -153,7 +207,7 @@ static int l_is_active(lua_State *L) {
return 1;
}

static const luaL_Reg libcurlfunctions[] = {
static const luaL_Reg libcurl_functions[] = {
{ "global_init", l_global_init },
{ "fetch", l_fetch },
{ "abort", l_abort },
Expand All @@ -163,9 +217,9 @@ static const luaL_Reg libcurlfunctions[] = {

int luaopen_libcurl(lua_State *L) {
lua_newtable(L);
for (int i = 0; libcurlfunctions[i].name != NULL; i++) {
lua_pushcfunction(L, libcurlfunctions[i].func);
lua_setfield(L, -2, libcurlfunctions[i].name);
for (int i = 0; libcurl_functions[i].name != NULL; i++) {
lua_pushcfunction(L, libcurl_functions[i].func);
lua_setfield(L, -2, libcurl_functions[i].name);
}
return 1;
}
Expand Down

0 comments on commit 366fb10

Please sign in to comment.