Просмотр исходного кода

add: 添加httpsrv库,具体server需要bsp实现

Wendal Chen 3 лет назад
Родитель
Сommit
8f94976fcb

+ 33 - 0
components/network/httpsrv/inc/luat_httpsrv.h

@@ -0,0 +1,33 @@
+#include "luat_base.h"
+
+
+typedef struct luat_httpsrv_ctx
+{
+    uint16_t port;
+    uint16_t https;
+    char static_path[32];
+    int lua_ref_id;
+    int server_fd;
+    void* userdata;
+}luat_httpsrv_ctx_t;
+
+
+typedef struct http_code_str
+{
+    int code;
+    const char* msg;
+}http_code_str_t;
+
+static const http_code_str_t http_codes[] = {
+    {200, "OK"},
+    {302, "Found"},
+    {400, "Bad Request"},
+    {401, "Unauthorized"},
+    {403, "Forbidden"},
+    {404, "Not Found"},
+    {500, "Internal Server Error"},
+    {0, ""}
+};
+
+int luat_httpsrv_stop(int port);
+int luat_httpsrv_start(luat_httpsrv_ctx_t* ctx);

+ 40 - 0
components/network/httpsrv/src/luat_lib_httpsrv.c

@@ -0,0 +1,40 @@
+#include "luat_base.h"
+#include "luat_httpsrv.h"
+
+#define LUAT_LOG_TAG "httpsrv"
+#include "luat_log.h"
+
+static int l_httpsrv_start(lua_State *L) {
+    int port = luaL_checkinteger(L, 1);
+    if (!lua_isfunction(L, 2)) {
+        LLOGW("httpsrv need callback function!!!");
+        return 0;
+    }
+    lua_pushvalue(L, 2);
+    int lua_ref_id = luaL_ref(L, LUA_REGISTRYINDEX);
+    luat_httpsrv_ctx_t ctx = {
+        .port = port,
+        .lua_ref_id = lua_ref_id
+    };
+    int ret = luat_httpsrv_start(&ctx);
+    return 0;
+}
+
+static int l_httpsrv_stop(lua_State *L) {
+    int port = luaL_checkinteger(L, 1);
+    luat_httpsrv_stop(port);
+    return 0;
+}
+
+#include "rotable2.h"
+static const rotable_Reg_t reg_httpsrv[] =
+{
+    {"start",        ROREG_FUNC(l_httpsrv_start) },
+    {"stop",         ROREG_FUNC(l_httpsrv_stop) },
+	{ NULL,          ROREG_INT(0) }
+};
+
+LUAMOD_API int luaopen_httpsrv( lua_State *L ) {
+    luat_newlib2(L, reg_httpsrv);
+    return 1;
+}

+ 2 - 0
luat/include/luat_libs.h

@@ -131,4 +131,6 @@ LUAMOD_API int luaopen_mobile( lua_State *L );
 
 LUAMOD_API int luaopen_protobuf( lua_State *L );
 
+LUAMOD_API int luaopen_httpsrv( lua_State *L );
+
 #endif