summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2020-05-08 16:37:19 +0200
committerAki <please@ignore.pl>2020-05-08 16:52:15 +0200
commit7fc6a70725d54cf767a250a4c6b3f3fcc9eff85a (patch)
tree100aa07c526104bec61a44527c0ec19ec5df973f
parent0b256dd68b7ed1e1e0875050204a3620c164d30f (diff)
downloadplop-7fc6a70725d54cf767a250a4c6b3f3fcc9eff85a.zip
plop-7fc6a70725d54cf767a250a4c6b3f3fcc9eff85a.tar.gz
plop-7fc6a70725d54cf767a250a4c6b3f3fcc9eff85a.tar.bz2
Response body is now handled by Lua
-rw-r--r--default.lua6
-rw-r--r--http.c10
-rw-r--r--http.h2
-rw-r--r--plop.c24
4 files changed, 33 insertions, 9 deletions
diff --git a/default.lua b/default.lua
new file mode 100644
index 0000000..0f9aaf4
--- /dev/null
+++ b/default.lua
@@ -0,0 +1,6 @@
+--- Default client request handler.
+--- @param request string Unparsed request from the client
+--- @return string Body of the response
+function Handler (request)
+ return [["Hello, from plop/Lua"]]
+end
diff --git a/http.c b/http.c
index 7e886b4..a966121 100644
--- a/http.c
+++ b/http.c
@@ -34,21 +34,23 @@ int respond_only_status(const int fd, const enum status status)
/// \param fd File descriptor of the client socket
/// \param status HTTP response status code
/// \param body Content that will be sent
+/// \param size Size of the content in bytes
/// \return Negative value if an error was encountered; numbers of bytes written otherwise
-int respond_with_body(const int fd, const enum status status, const char * body)
+int respond_with_body(const int fd, const enum status status, const char * body, const int size)
{
static const char * pattern =
"HTTP/1.1 %s\r\n"
"Connection: close\r\n"
- "Content-Type: text/plain\r\n"
+ "Content-Type: application/json\r\n"
+ "Content-Size: %d\r\n"
"\r\n";
- if (0 > dprintf(fd, pattern, status_str[status]))
+ if (0 > dprintf(fd, pattern, status_str[status], size))
{
return -1; // TODO: Handle errors properly
}
- return write(fd, body, strlen(body));
+ return write(fd, body, size);
}
/// Collects request between calls to `poll`.
diff --git a/http.h b/http.h
index 14a4769..e0d7891 100644
--- a/http.h
+++ b/http.h
@@ -15,7 +15,7 @@ enum status
extern const char * status_str[];
int respond_only_status(int, enum status);
-int respond_with_body(int, enum status, const char *);
+int respond_with_body(int, enum status, const char *, int);
int collect_request(int, char **);
#endif // HTTP_H
diff --git a/plop.c b/plop.c
index fb62b01..d676c4f 100644
--- a/plop.c
+++ b/plop.c
@@ -73,12 +73,13 @@ int make_server(const char * node, const char * service)
/// 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
/// \param pfd Element in array processed by poll
/// \param data Pointer to data associated with the client
/// \param shift_by Element will be moved by in array this many indices
/// \return Shift value for next handler
/// \see poll(2)
-int handle_client(struct pollfd * pfd, char ** data, const int shift_by)
+int handle_client(lua_State * L, struct pollfd * pfd, char ** data, const int shift_by)
{
if (0 == pfd->revents)
{
@@ -95,7 +96,17 @@ int handle_client(struct pollfd * pfd, char ** data, const int shift_by)
return -1; // TODO: Handle errors properly
}
- respond_with_body(pfd->fd, STATUS_OK, *data);
+ lua_getglobal(L, "Handler");
+ lua_pushstring(L, *data);
+ lua_call(L, 1, 1);
+
+ size_t length;
+ const char * body = lua_tolstring(L, -1, &length);
+
+ if (NULL != body)
+ {
+ respond_with_body(pfd->fd, STATUS_OK, body, (int) length);
+ }
close(pfd->fd);
(pfd - shift_by)->fd = -1;
@@ -111,7 +122,7 @@ int handle_client(struct pollfd * pfd, char ** data, const int shift_by)
/// \param fdv Array of descriptors for poll
/// \param fdc Number of valid descriptors in the array including the server
/// \param size Size limit for the array
-/// \param Number of valid descriptors in the array or -1 if an error occurred.
+/// \return Number of valid descriptors in the array or -1 if an error occurred.
/// \see poll(2)
int handle_server(struct pollfd * fdv, int fdc, const int size)
{
@@ -143,6 +154,11 @@ int main(int argc, char ** argv)
lua_State * L = luaL_newstate();
luaL_openlibs(L);
+ if (LUA_OK != luaL_dofile(L, "default.lua"))
+ {
+ return 2; // TODO: Document error codes/print usage information
+ }
+
if (2 != argc)
{
return 1; // TODO: Document error codes/print usage information
@@ -167,7 +183,7 @@ int main(int argc, char ** argv)
for (int i = 1; i < fdc; ++i)
{
- shift_by = handle_client(&fdv[i], &data[i], shift_by);
+ shift_by = handle_client(L, &fdv[i], &data[i], shift_by);
}
fdc = handle_server(fdv, fdc - shift_by, max_clients);