diff options
Diffstat (limited to 'plop.c')
-rw-r--r-- | plop.c | 30 |
1 files changed, 21 insertions, 9 deletions
@@ -5,6 +5,7 @@ #include <lualib.h> #include <netdb.h> #include <poll.h> +#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/socket.h> @@ -82,11 +83,11 @@ int make_server(const char * node, const char * service) /// `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 request Pointer to request context associated with current 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(lua_State * L, struct pollfd * pfd, char ** data, const int shift_by) +int handle_client(lua_State * L, struct pollfd * pfd, struct request ** request, const int shift_by) { if (0 == pfd->revents) { @@ -98,14 +99,22 @@ int handle_client(lua_State * L, struct pollfd * pfd, char ** data, const int sh return -1; // TODO: Handle errors properly } - if (-1 == collect_request(pfd->fd, data)) + if (-1 == parse_request(pfd->fd, request)) { respond_only_status(pfd->fd, STATUS_BAD_REQUEST); return -1; // TODO: Handle errors properly } + // TODO: Remove debug information or move to logging + printf( + "(%.*s) [%.*s] %.*s\n", + (*request)->version.length, (*request)->data + (*request)->version.start, + (*request)->method.length, (*request)->data + (*request)->method.start, + (*request)->path.length, (*request)->data + (*request)->path.start); + + // TODO: Use results from parsing instead of raw data lua_getglobal(L, "Handler"); - lua_pushstring(L, *data); + lua_pushlstring(L, (*request)->data, (*request)->length); lua_call(L, 1, 1); size_t length; @@ -124,8 +133,11 @@ int handle_client(lua_State * L, struct pollfd * pfd, char ** data, const int sh (pfd - shift_by)->fd = -1; (pfd - shift_by)->events = pfd->events; - free(*data); - *data = NULL; + if (NULL != *request) + { + free_request(*request); + *request = NULL; + } return shift_by + 1; } @@ -179,10 +191,10 @@ int main(int argc, char ** argv) static const int max_clients = 200; int fdc = 0; - char * data[max_clients]; + struct request * requests[max_clients]; struct pollfd fdv[max_clients]; - memset(data, 0, sizeof(data)); + memset(requests, 0, sizeof(requests)); memset(fdv, 0, sizeof(fdv)); fdv[0].fd = make_server(NULL, argv[1]); @@ -200,7 +212,7 @@ int main(int argc, char ** argv) for (int i = 1; i < fdc; ++i) { - shift_by = handle_client(L, &fdv[i], &data[i], shift_by); + shift_by = handle_client(L, &fdv[i], &requests[i], shift_by); } fdc = handle_server(fdv, fdc - shift_by, max_clients); |