summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2020-08-15 17:21:36 +0200
committerAki <please@ignore.pl>2020-08-15 17:21:36 +0200
commit41cb74fa129f5e18b6c0b99b89933869f09b585c (patch)
treeba478e0c19e9d184fd8cd6e927193c572af3350d
parent53b5ea2bafca4ea7e9bdcbe55f1ed0a458b270e1 (diff)
downloadplop-41cb74fa129f5e18b6c0b99b89933869f09b585c.zip
plop-41cb74fa129f5e18b6c0b99b89933869f09b585c.tar.gz
plop-41cb74fa129f5e18b6c0b99b89933869f09b585c.tar.bz2
Changed handler loading to be require-like
-rw-r--r--default.lua5
-rw-r--r--main.c2
-rw-r--r--plop.c32
-rw-r--r--plop.h3
4 files changed, 38 insertions, 4 deletions
diff --git a/default.lua b/default.lua
index 914c990..14cdbc9 100644
--- a/default.lua
+++ b/default.lua
@@ -1,6 +1,7 @@
--- Default client request handler.
--- @return string Body of the response
-function Handler (method, path, version, headers, data)
+local
+function handler (method, path, version, headers, data)
io.write("(", version, ") [", method, "] ", path, "\n")
for header, value in pairs(headers) do
io.write(" - ", header, " = '", value, "'\n")
@@ -8,3 +9,5 @@ function Handler (method, path, version, headers, data)
print(data)
return [["Hello, from plop/Lua"]]
end
+
+return handler
diff --git a/main.c b/main.c
index af36c5d..06f44d4 100644
--- a/main.c
+++ b/main.c
@@ -21,7 +21,7 @@ int main(int argc, char ** argv)
return 1; // TODO: Document error codes/print usage information
}
- if (LUA_OK != luaL_dofile(L, argv[2]))
+ if (LUA_OK != load_handler(L, argv[2]))
{
return 2; // TODO: Document error codes/print usage information
}
diff --git a/plop.c b/plop.c
index 4aadd2f..9ec7a5d 100644
--- a/plop.c
+++ b/plop.c
@@ -83,6 +83,36 @@ int make_server(const char * node, const char * service)
return server;
}
+// TODO: Write documentation for load_handler
+int load_handler(lua_State * L, const char * path)
+{
+ lua_getglobal(L, "package");
+ lua_pushstring(L, "loaded");
+ int result = lua_rawget(L, -2);
+
+ if (LUA_TTABLE != result)
+ {
+ lua_pop(L, 2);
+ return LUA_ERRRUN;
+ }
+
+ result = luaL_loadfile(L, path) || lua_pcall(L, 0, 1, 0);
+
+ if (LUA_OK != result)
+ {
+ lua_pop(L, 3);
+ return result;
+ }
+
+ lua_pushstring(L, "handler");
+ lua_pushvalue(L, -2);
+ lua_rawset(L, -4);
+ lua_setglobal(L, "handler");
+ lua_pop(L, 1);
+
+ return LUA_OK;
+}
+
/// Progresses the connection for a client and shifts it back in the array.
/// `pfd` must be at least `shift_by + 1`-th element of the array.
/// \param L Server's Lua state
@@ -110,7 +140,7 @@ int handle_client(lua_State * L, struct pollfd * pfd, struct request ** request,
}
// TODO: Push the handler to stack earlier to avoid shifting it.
- lua_getglobal((*request)->lua, "Handler");
+ lua_getglobal((*request)->lua, "handler");
lua_insert((*request)->lua, 1);
lua_pushlstring((*request)->lua, (*request)->data, (*request)->length);
lua_call((*request)->lua, 5, 1);
diff --git a/plop.h b/plop.h
index ef7ecfe..c12b43b 100644
--- a/plop.h
+++ b/plop.h
@@ -8,5 +8,6 @@
#include "request.h"
int make_server(const char *, const char *);
-int handle_client(lua_State * L, struct pollfd *, struct request **, const int);
+int load_handler(lua_State *, const char *);
+int handle_client(lua_State *, struct pollfd *, struct request **, const int);
int handle_server(struct pollfd *, int, const int);