summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2020-08-15 14:59:51 +0200
committerAki <please@ignore.pl>2020-08-15 14:59:51 +0200
commit92be962c748d5a45da4bf207a7b17c2b25ca31cd (patch)
tree7967d63587221dfc1d55f773949f2234dc4b3bc2
parentd90d0050c2ee91d11fce3a5fd45c5fb64018392c (diff)
downloadplop-92be962c748d5a45da4bf207a7b17c2b25ca31cd.zip
plop-92be962c748d5a45da4bf207a7b17c2b25ca31cd.tar.gz
plop-92be962c748d5a45da4bf207a7b17c2b25ca31cd.tar.bz2
Added lua state to requests
-rw-r--r--plop.c5
-rw-r--r--request.c16
-rw-r--r--request.h10
3 files changed, 21 insertions, 10 deletions
diff --git a/plop.c b/plop.c
index 8665ec9..45d913f 100644
--- a/plop.c
+++ b/plop.c
@@ -13,7 +13,6 @@
#include <lauxlib.h>
#include <lua.h>
-#include <lualib.h>
#include "http.h"
#include "request.h"
@@ -104,7 +103,7 @@ int handle_client(lua_State * L, struct pollfd * pfd, struct request ** request,
return -1; // TODO: Handle errors properly
}
- if (-1 == parse_request(pfd->fd, request))
+ if (-1 == parse_request(L, pfd->fd, request))
{
respond_only_status(pfd->fd, STATUS_BAD_REQUEST);
return -1; // TODO: Handle errors properly
@@ -150,7 +149,7 @@ int handle_client(lua_State * L, struct pollfd * pfd, struct request ** request,
if (NULL != *request)
{
- free_request(*request);
+ free_request(L, *request);
*request = NULL;
}
diff --git a/request.c b/request.c
index f6f8a78..e8f47b0 100644
--- a/request.c
+++ b/request.c
@@ -7,6 +7,9 @@
#include <string.h>
#include <unistd.h>
+#include <lauxlib.h>
+#include <lua.h>
+
#include "http.h"
static const int REQUEST_DATA_SIZE = 4096;
@@ -15,7 +18,7 @@ static const int MAX_HEADERS = 30;
/// Allocates and initializes request structure.
/// \return Pointer to initialized request or NULL in case of an error
/// \see /free_request
-struct request * new_request(void)
+struct request * new_request(lua_State * L)
{
struct request * request = malloc(sizeof(struct request));
@@ -35,13 +38,16 @@ struct request * new_request(void)
return NULL;
}
+ request->lua = lua_newthread(L);
+ request->reference = luaL_ref(L, LUA_REGISTRYINDEX);
+
return request;
}
/// Releases memory used by the request and the request itself.
/// \param request Request to free
/// \see /new_request
-void free_request(struct request * request)
+void free_request(lua_State * L, struct request * request)
{
if (NULL != request->data)
{
@@ -53,6 +59,8 @@ void free_request(struct request * request)
free(request->headerv);
}
+ luaL_unref(L, LUA_REGISTRYINDEX, request->reference);
+
free(request);
}
@@ -60,11 +68,11 @@ void free_request(struct request * request)
/// \param fd Client socket
/// \param request Pointer to current request context of handled client
/// \return Number of bytes parsed, -1 if an error occured or 0 if expects more data
-int parse_request(const int fd, struct request ** request)
+int parse_request(lua_State * L, const int fd, struct request ** request)
{
if (NULL == *request)
{
- *request = new_request();
+ *request = new_request(L);
if (NULL == *request)
{
return -1;
diff --git a/request.h b/request.h
index a302d61..79aca75 100644
--- a/request.h
+++ b/request.h
@@ -1,5 +1,7 @@
#pragma once
+#include <lua.h>
+
struct span
{
int start;
@@ -18,6 +20,8 @@ struct request
char * data;
int length;
int position;
+ lua_State * lua;
+ int reference;
struct span method;
struct span path;
struct span version;
@@ -26,10 +30,10 @@ struct request
struct span body;
};
-struct request * new_request(void);
-void free_request(struct request *);
+struct request * new_request(lua_State *);
+void free_request(lua_State *, struct request *);
-int parse_request(int, struct request **);
+int parse_request(lua_State *, int, struct request **);
int parse_step_method(struct request *);
int parse_step_path(struct request *);