summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2020-08-15 15:41:09 +0200
committerAki <please@ignore.pl>2020-08-15 15:41:09 +0200
commit87da5e359749cd01ff7a264fc2c15f8615ea80fd (patch)
tree69f62cf72845c3fdfbd8cc627a2204cff9b487bd
parent162c05ece12525e684d05ed620a6ffa74d561fa8 (diff)
downloadplop-87da5e359749cd01ff7a264fc2c15f8615ea80fd.zip
plop-87da5e359749cd01ff7a264fc2c15f8615ea80fd.tar.gz
plop-87da5e359749cd01ff7a264fc2c15f8615ea80fd.tar.bz2
Changed to use request lua state with handler
-rw-r--r--default.lua3
-rw-r--r--plop.c27
2 files changed, 7 insertions, 23 deletions
diff --git a/default.lua b/default.lua
index 0f9aaf4..1dc535f 100644
--- a/default.lua
+++ b/default.lua
@@ -1,6 +1,5 @@
--- Default client request handler.
---- @param request string Unparsed request from the client
--- @return string Body of the response
-function Handler (request)
+function Handler (...)
return [["Hello, from plop/Lua"]]
end
diff --git a/plop.c b/plop.c
index 51721b6..12cef19 100644
--- a/plop.c
+++ b/plop.c
@@ -109,29 +109,14 @@ int handle_client(lua_State * L, struct pollfd * pfd, struct request ** request,
return -1; // TODO: Handle errors properly
}
- // TODO: Remove debug information or move to logging
- printf(
- "(%.*s) %.*s\n",
- (*request)->version.length, (*request)->data + (*request)->version.start,
- (*request)->path.length, (*request)->data + (*request)->path.start);
-
- for (int h = 0; h < (*request)->headerc; ++h)
- {
- printf(
- "\t'%.*s' = '%.*s'\n",
- (*request)->headerv[h].name.length,
- (*request)->data + (*request)->headerv[h].name.start,
- (*request)->headerv[h].value.length,
- (*request)->data + (*request)->headerv[h].value.start);
- }
-
- // TODO: Use results from parsing instead of raw data
- lua_getglobal(L, "Handler");
- lua_pushlstring(L, (*request)->data, (*request)->length);
- lua_call(L, 1, 1);
+ // TODO: Push the handler to stack earlier to avoid shifting it.
+ lua_getglobal((*request)->lua, "Handler");
+ lua_insert((*request)->lua, 1);
+ lua_pushlstring((*request)->lua, (*request)->data, (*request)->length);
+ lua_call((*request)->lua, 2, 1);
size_t length;
- const char * body = lua_tolstring(L, -1, &length);
+ const char * body = lua_tolstring((*request)->lua, -1, &length);
if (NULL != body)
{