From af62ac040f0fae7f423c6cc9e0ce23bde2d7d73a Mon Sep 17 00:00:00 2001 From: teppy999 Date: Mon, 3 Jul 2023 15:18:21 -0400 Subject: [PATCH] Stop using cv2pdb and improve HTTP handling a little --- luprex/Makefile | 6 ------ luprex/cpp/core/http.cpp | 18 ++++++++++++++++++ luprex/cpp/core/http.hpp | 7 +++++++ luprex/cpp/core/world-core.cpp | 21 +++++++++++---------- 4 files changed, 36 insertions(+), 16 deletions(-) diff --git a/luprex/Makefile b/luprex/Makefile index d1c5c61b..0396305a 100644 --- a/luprex/Makefile +++ b/luprex/Makefile @@ -121,7 +121,6 @@ ifeq "$(OS)" "linux" LINKDLL=g++ -Wall $(OPT) -std=c++17 -export-dynamic -Wl,--no-allow-shlib-undefined -Wl,-z,defs -shared -o LINKEXE=g++ -Wall $(OPT) -std=c++17 -o MAKEDEPS=true - MAKEPDB=true OPENSSL_INCLUDE=-I./ext/openssl-3.0.1/inc LUA_FLAGS=-DLUA_USE_APICHECK -DLUA_USE_POSIX LIBS=-L./ext/openssl-3.0.1/lib/linux -lssl -lcrypto -ldl @@ -143,7 +142,6 @@ ifeq "$(OS)" "mingw" LINKDLL=g++ -Wall $(OPT) -std=c++17 -Wl,--no-allow-shlib-undefined -shared -o LINKEXE=g++ -Wall $(OPT) -std=c++17 -o MAKEDEPS=true - MAKEPDB=./ext/cv2pdb.exe OPENSSL_INCLUDE=-I./ext/openssl-3.0.1/inc LUA_FLAGS=-DLUA_USE_APICHECK -DLUA_COMPAT_ALL LIBS=-L./ext/openssl-3.0.1/lib/mingw -lssl -lcrypto -lws2_32 -lcrypt32 -lcryptui @@ -168,7 +166,6 @@ ifeq "$(OS)" "visual" LINKDLL=CL $(OPT) /std:c++17 /EHsc /nologo /LDd /Fe: LINKEXE=CL $(OPT) /std:c++17 /EHsc /nologo /Fe: MAKEDEPS=g++ -Wall -std=c++17 -MMD -E -o - MAKEPDB=true OPENSSL_INCLUDE=-I./ext/openssl-3.1.0/inc LUA_FLAGS=-DLUA_USE_APICHECK -DLUA_COMPAT_ALL LIBS=ext/openssl-3.1.0/lib/visual/libcrypto.lib ext/openssl-3.1.0/lib/visual/libssl.lib ws2_32.lib crypt32.lib cryptui.lib user32.lib advapi32.lib @@ -188,15 +185,12 @@ build/DIRECTORY: build/$(OS)/$(LUPREX_EXE): build/$(OS)/$(LUPREXLIB_DLL) $(OBJ_DRV) $(LINKEXE) $@ $(OBJ_DRV) $(LIBS) - $(MAKEPDB) $@ build/$(OS)/$(LUPREXSTATIC_EXE): $(OBJ_DRV) $(OBJ_ERIS) $(OBJ_CORE) $(LINKEXE) $@ $^ $(LIBS) - $(MAKEPDB) $@ build/$(OS)/$(LUPREXLIB_DLL): $(OBJ_ERIS) $(OBJ_CORE) $(LINKDLL) $@ $^ - $(MAKEPDB) $@ build/$(OS)/eris/%.obj: ext/eris-master/src/%.c build/DIRECTORY $(MAKEDEPS) $@d $(LUA_FLAGS) $< diff --git a/luprex/cpp/core/http.cpp b/luprex/cpp/core/http.cpp index 7f484250..1d420fba 100644 --- a/luprex/cpp/core/http.cpp +++ b/luprex/cpp/core/http.cpp @@ -1143,6 +1143,24 @@ eng::string HttpParser::first_path_component(std::string_view defval) const { } } +eng::string HttpParser::to_lua_identifier(std::string_view pathcomp) { + eng::ostringstream oss; + for (char c : pathcomp) { + if (sv::ascii_islower(c)) { + oss << c; + } else if (sv::ascii_isupper(c)) { + oss << char(c + 'a' - 'A'); + } else if (sv::ascii_isdigit(c)) { + oss << c; + } else if ((c == '.') || (c == '_')) { + oss << '_'; + } else { + return ""; + } + } + return oss.str(); +} + void HttpParser::fail(int code, std::string_view message) { status_ = code; error_ = message; diff --git a/luprex/cpp/core/http.hpp b/luprex/cpp/core/http.hpp index 2e29c0fe..4f37c2ac 100644 --- a/luprex/cpp/core/http.hpp +++ b/luprex/cpp/core/http.hpp @@ -421,6 +421,13 @@ public: // Synthesize an error and store it in lua. // static void store_fail(LuaCoreStack &LS, LuaSlot tab, int status, std::string_view error); + + // Convert a path component into a lua identifier. + // + // Returns the empty string if the path component cannot be converted + // to a lua identifier. + // + static eng::string to_lua_identifier(std::string_view pathcomp); }; class HttpClientRequestMap : public eng::map { diff --git a/luprex/cpp/core/world-core.cpp b/luprex/cpp/core/world-core.cpp index 8c30c5f0..e167a4db 100644 --- a/luprex/cpp/core/world-core.cpp +++ b/luprex/cpp/core/world-core.cpp @@ -457,13 +457,6 @@ HttpServerResponse World::http_serve(const HttpParser &request) { return response; } - // Get the name of the desired function. - std::string_view fn = request.first_path_component("index"); - if (!sv::is_lua_id(fn)) { - response.fail(404, util::ss("not a function name: ", fn)); - return response; - } - lua_State *L = state(); LuaVar www, func, reqtab; LuaExtStack LS(L, www, func, reqtab); @@ -476,11 +469,19 @@ HttpServerResponse World::http_serve(const HttpParser &request) { return response; } + // Get the name of the desired function. + std::string_view orig_fn = request.first_path_component("index"); + eng::string lua_fn = HttpParser::to_lua_identifier(orig_fn); + if (lua_fn.empty()) { + response.fail(404, util::ss("cannot convert to lua function name: ", orig_fn)); + return response; + } + // Get the closure. If there's no such closure, // return a 404 Not Found to the client. - LS.rawget(func, www, fn); + LS.rawget(func, www, lua_fn); if (!LS.isfunction(func)) { - response.fail(404, util::ss("no such function: www.", fn)); + response.fail(404, util::ss("no such lua function: www.", lua_fn)); return response; } @@ -507,7 +508,7 @@ HttpServerResponse World::http_serve(const HttpParser &request) { // a 500 Internal Server Error to the client. int newtop = lua_gettop(L); if ((newtop != oldtop + 1) || (!lua_istable(L, newtop))) { - response.fail(500, util::ss("lua function www.", fn, " didn't return a table")); + response.fail(500, util::ss("lua function www.", lua_fn, " didn't return a table")); return response; }