summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--Makefile6
-rw-r--r--connection.c1
-rw-r--r--plop.c41
-rw-r--r--plop.h2
-rw-r--r--request.c48
-rw-r--r--request.h12
6 files changed, 48 insertions, 62 deletions
diff --git a/Makefile b/Makefile
index 9b3c4f3..d28dc6d 100644
--- a/Makefile
+++ b/Makefile
@@ -3,11 +3,11 @@ CFLAGS+=-I/usr/include/lua5.3
LDLIBS+=-llua5.3
PREFIX?=/usr/local
-plop: main.o plop.o response.o request.o
+plop: connection.o main.o plop.o response.o request.o
main.o: plop.h request.h
-plop.o: plop.h request.h response.h
-request.o: request.h
+plop.o: connection.h plop.h request.h response.h
+request.o: connection.h request.h
response.o: response.h
connection.o: connection.h request.h
diff --git a/connection.c b/connection.c
index c7dd151..f987d65 100644
--- a/connection.c
+++ b/connection.c
@@ -2,6 +2,7 @@
#include <stdlib.h>
#include <string.h>
+#include <unistd.h>
#include <lauxlib.h>
#include <lua.h>
diff --git a/plop.c b/plop.c
index b1c5944..c64ad11 100644
--- a/plop.c
+++ b/plop.c
@@ -14,6 +14,7 @@
#include <lauxlib.h>
#include <lua.h>
+#include "connection.h"
#include "request.h"
#include "response.h"
@@ -121,34 +122,25 @@ int plop_load_handler(lua_State * L, const char * path)
/// \return -1 if an error occured
int plop_handle_client(lua_State * L, struct epoll_event * event)
{
- // TODO: Temporary shenanigans to avoid too much changes.
- struct request * r = (struct request *) event->data.ptr;
- struct request ** request = &r;
+ struct connection * connection = (struct connection *) event->data.ptr;
- if (-1 == parse_request(L, (*request)->fd, request))
+ if (-1 == parse_request(connection))
{
- lua_newtable((*request)->lua);
- lua_pushstring((*request)->lua, "status");
- lua_pushinteger((*request)->lua, 400); // TODO: How about a function that generates error responses?
- lua_rawset((*request)->lua, -3);
+ lua_newtable(connection->L);
+ lua_pushstring(connection->L, "status");
+ lua_pushinteger(connection->L, 400); // TODO: How about a function that generates error responses?
+ lua_rawset(connection->L, -3);
}
else // TODO: 0 may mean EAGAIN, stuff will be bad very soon from here.
{
// TODO: Push the handler to stack earlier to avoid shifting it.
- lua_getglobal((*request)->lua, "handler");
- lua_insert((*request)->lua, 1);
- lua_call((*request)->lua, 5, 1);
+ lua_getglobal(connection->L, "handler");
+ lua_insert(connection->L, 1);
+ lua_call(connection->L, 5, 1);
}
- int result = response_send((*request)->lua, (*request)->fd);
-
- close((*request)->fd);
-
- if (NULL != *request)
- {
- free_request(L, *request);
- *request = NULL;
- }
+ int result = response_send(connection->L, connection->fd);
+ connection_free(L, connection);
return result;
}
@@ -201,15 +193,14 @@ int plop_handle_server(lua_State * L, const int efd, const int server)
return 0; // TODO: Retriage this error at some point.
}
- // TODO: Request is not the enitre state of the client. Make them distinct along with responses.
- struct request * request = new_request(L);
- if (NULL == request)
+ struct connection * connection = connection_new(L, client);
+ if (NULL == connection)
{
return -1;
}
- request->fd = client;
- e.data.ptr = request;
+ connection->fd = client;
+ e.data.ptr = connection;
if (-1 == epoll_ctl(efd, EPOLL_CTL_ADD, client, &e))
{
diff --git a/plop.h b/plop.h
index 5b8e387..8003441 100644
--- a/plop.h
+++ b/plop.h
@@ -4,8 +4,6 @@
#include <lua.h>
-#include "request.h"
-
int plop_make_server(const char *, const char *);
int plop_load_handler(lua_State *, const char *);
int plop_handle_client(lua_State *, struct epoll_event *);
diff --git a/request.c b/request.c
index 114cfd5..9400ae4 100644
--- a/request.c
+++ b/request.c
@@ -8,15 +8,15 @@
#include <strings.h>
#include <unistd.h>
-#include <lauxlib.h>
#include <lua.h>
static const int REQUEST_DATA_SIZE = 4096;
/// Allocates and initializes request structure.
+/// \param connection Origin connection of the request
/// \return Pointer to initialized request or NULL in case of an error
/// \see /free_request
-struct request * new_request(lua_State * L)
+struct request * new_request(struct connection * connection)
{
struct request * request = malloc(sizeof(struct request));
@@ -36,8 +36,7 @@ struct request * new_request(lua_State * L)
return NULL;
}
- request->lua = lua_newthread(L);
- request->reference = luaL_ref(L, LUA_REGISTRYINDEX);
+ request->connection = connection;
return request;
}
@@ -45,45 +44,42 @@ struct request * new_request(lua_State * L)
/// Releases memory used by the request and the request itself.
/// \param request Request to free
/// \see /new_request
-void free_request(lua_State * L, struct request * request)
+void free_request(struct request * request)
{
if (NULL != request->data)
{
free(request->data);
}
- luaL_unref(L, LUA_REGISTRYINDEX, request->reference);
-
free(request);
}
/// Collects request between calls to `poll`.
-/// \param fd Client socket
-/// \param request Pointer to current request context of handled client
+/// \param connection Origin connection of the request
/// \return Number of bytes parsed, -1 if an error occured or 0 if expects more data
-int parse_request(lua_State * L, const int fd, struct request ** request)
+int parse_request(struct connection * connection)
{
- if (NULL == *request)
+ if (NULL == connection->request)
{
- *request = new_request(L);
- if (NULL == *request)
+ connection->request = new_request(connection);
+ if (NULL == connection->request)
{
return -1;
}
}
// TODO: Expand buffer until EAGAIN or arbitrary limit
- int length = read(fd, (*request)->data, REQUEST_DATA_SIZE - 1);
+ int length = read(connection->fd, connection->request->data, REQUEST_DATA_SIZE - 1);
if (0 == length || (-1 == length && EWOULDBLOCK != errno && EAGAIN != errno))
{
return -1; // TODO: Handle errors properly
}
- (*request)->data[length] = 0;
- (*request)->length = length;
+ connection->request->data[length] = 0;
+ connection->request->length = length;
- return (*request)->step(*request);
+ return connection->request->step(connection->request);
}
@@ -167,7 +163,7 @@ int parse_step_method(struct request * request)
return 0;
}
- lua_pushlstring(request->lua, request->data + request->temp.start, request->temp.length);
+ lua_pushlstring(request->connection->L, request->data + request->temp.start, request->temp.length);
PROCEED_TO(parse_step_path);
}
@@ -199,7 +195,7 @@ int parse_step_path(struct request * request)
request->temp.length = request->position - request->temp.start;
// TODO: Use macro for pushing temporary span. Also clear the span in that macro instead of PROCEED_TO.
- lua_pushlstring(request->lua, request->data + request->temp.start, request->temp.length);
+ lua_pushlstring(request->connection->L, request->data + request->temp.start, request->temp.length);
PROCEED_TO(parse_step_version);
}
@@ -229,7 +225,7 @@ int parse_step_version(struct request * request)
request->temp.length = request->position - request->temp.start;
- lua_pushlstring(request->lua, request->data + request->temp.start, request->temp.length);
+ lua_pushlstring(request->connection->L, request->data + request->temp.start, request->temp.length);
PROCEED_TO(parse_step_header_name);
}
@@ -239,9 +235,9 @@ int parse_step_version(struct request * request)
/// \return -1 if an error has occured, 0 if too little data available or total number of bytes processed
int parse_step_header_name(struct request * request)
{
- if (0 == lua_istable(request->lua, -1))
+ if (0 == lua_istable(request->connection->L, -1))
{
- lua_newtable(request->lua);
+ lua_newtable(request->connection->L);
}
if (request->length - request->position < 4)
@@ -288,7 +284,7 @@ int parse_step_header_name(struct request * request)
request->expected_data_length = -1;
}
- lua_pushlstring(request->lua, request->data + request->temp.start, request->temp.length);
+ lua_pushlstring(request->connection->L, request->data + request->temp.start, request->temp.length);
// Skip ':'
request->position++;
@@ -326,8 +322,8 @@ int parse_step_header_value(struct request * request)
*(request->data + request->temp.start + request->temp.length) = '\r';
}
- lua_pushlstring(request->lua, request->data + request->temp.start, request->temp.length);
- lua_rawset(request->lua, -3);
+ lua_pushlstring(request->connection->L, request->data + request->temp.start, request->temp.length);
+ lua_rawset(request->connection->L, -3);
PROCEED_TO(parse_step_header_name);
}
@@ -348,7 +344,7 @@ int parse_step_data(struct request * request)
return 0;
}
- lua_pushlstring(request->lua, request->data + request->position, request->expected_data_length);
+ lua_pushlstring(request->connection->L, request->data + request->position, request->expected_data_length);
if (NULL != request->data)
{
diff --git a/request.h b/request.h
index b08236c..27c1d01 100644
--- a/request.h
+++ b/request.h
@@ -2,6 +2,8 @@
#include <lua.h>
+#include "connection.h"
+
struct span
{
int start;
@@ -14,17 +16,15 @@ struct request
char * data;
int length;
int position;
- lua_State * lua;
- int reference;
struct span temp;
int expected_data_length;
- int fd;
+ struct connection * connection;
};
-struct request * new_request(lua_State *);
-void free_request(lua_State *, struct request *);
+struct request * new_request(struct connection *);
+void free_request(struct request *);
-int parse_request(lua_State *, int, struct request **);
+int parse_request(struct connection *);
int parse_step_method(struct request *);
int parse_step_path(struct request *);