From d6a54f904957d34f12361d5d8a7e515562cda3d5 Mon Sep 17 00:00:00 2001 From: Aki Date: Wed, 24 Feb 2021 20:58:58 +0100 Subject: Readded response for this time being --- Makefile | 3 ++- default.lua | 2 +- plop.c | 14 +++++++++++++- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 68c7d65..26bb34c 100644 --- a/Makefile +++ b/Makefile @@ -5,12 +5,13 @@ LDLIBS+=-llua5.3 PREFIX?=/usr/local SHARE?=$(PREFIX)/share -plop: connection.o main.o plop.o stream.o +plop: connection.o main.o plop.o stream.o response.o main.o: plop.h plop.o: connection.h plop.h stream.h connection.o: connection.h stream.o: stream.h +response.o: response.h clean: rm -f plop *.o diff --git a/default.lua b/default.lua index 95ba609..dc4237e 100644 --- a/default.lua +++ b/default.lua @@ -1,6 +1,6 @@ --- Default client request handler. -- TODO: Add documentation once request and response API are more stable. -return function (method, path, version, headers, data) +return function (stream) local response_data = [["Hello from plop/lua!"]] local headers = {Connection="close", ["Content-Length"]=#response_data, ["Content-Type"]="application/json"} return {status=200, headers=headers, data=response_data} diff --git a/plop.c b/plop.c index 980ce90..7859e7b 100644 --- a/plop.c +++ b/plop.c @@ -17,6 +17,7 @@ #include "connection.h" #include "stream.h" +#include "response.h" /// Initializes new Lua state for the server. /// \return Lua state @@ -158,20 +159,31 @@ int plop_handle_client(lua_State * L, struct epoll_event * event) } int result = lua_resume(c->L, NULL, nargs); - connection_free(L, c); // TODO: Allow consistent connections? switch (result) { case LUA_OK: { + int n = lua_gettop(c->L); + + if (0 == n) + { + lua_pushnil(c->L); + } + + response_send(c->L, c->fd); + connection_free(L, c); + return 0; } case LUA_YIELD: { + connection_free(L, c); // TODO: This shouldn't be the case... Right? return 0; } default: { + connection_free(L, c); return -1; } } -- cgit v1.1