summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2020-08-15 20:20:33 +0200
committerAki <please@ignore.pl>2020-08-15 20:20:33 +0200
commit1cdd3cf0ec89997ea5d91100e87fdd538c7e4cf9 (patch)
treedf48713044445da68cab67fbfac6aa84a8d481c7
parent7bac9cfb5788eea26b940b4be691d1f80b6d1deb (diff)
downloadplop-1cdd3cf0ec89997ea5d91100e87fdd538c7e4cf9.zip
plop-1cdd3cf0ec89997ea5d91100e87fdd538c7e4cf9.tar.gz
plop-1cdd3cf0ec89997ea5d91100e87fdd538c7e4cf9.tar.bz2
Data from request is now passed to Lua
-rw-r--r--plop.c1
-rw-r--r--request.c52
-rw-r--r--request.h1
3 files changed, 41 insertions, 13 deletions
diff --git a/plop.c b/plop.c
index 9ec7a5d..54e64ff 100644
--- a/plop.c
+++ b/plop.c
@@ -142,7 +142,6 @@ int handle_client(lua_State * L, struct pollfd * pfd, struct request ** request,
// TODO: Push the handler to stack earlier to avoid shifting it.
lua_getglobal((*request)->lua, "handler");
lua_insert((*request)->lua, 1);
- lua_pushlstring((*request)->lua, (*request)->data, (*request)->length);
lua_call((*request)->lua, 5, 1);
size_t length;
diff --git a/request.c b/request.c
index e0956b4..15034ee 100644
--- a/request.c
+++ b/request.c
@@ -245,22 +245,23 @@ int parse_step_header_name(struct request * request)
lua_newtable(request->lua);
}
- // TODO: Check for the header section end without backtracking.
- const int position = request->position;
- int result = read_rest_of_line(request);
-
- if (0 == result)
+ if (request->position + 4 >= request->length)
{
return 0;
}
- if (2 == result - position)
+ if (
+ request->data[request->position] == '\r' &&
+ request->data[request->position + 1] == '\n' &&
+ request->data[request->position + 2] == '\r' &&
+ request->data[request->position + 3] == '\n')
{
- // Header section ended, nothing left to parse.
- return request->position + 2;
- }
+ // Skip "\r\n\r\n", end of headers.
+ request->position += 4;
- request->position = position;
+ // Early return hidden in a macro!
+ PROCEED_TO(parse_step_data);
+ }
if (0 >= request->temp.start)
{
@@ -272,10 +273,11 @@ int parse_step_header_name(struct request * request)
}
}
- result = read_until_char(request, ':');
+ int result = read_until_char(request, ':');
if (0 == result)
{
+ printf("parse_step_header_name:no ':'\n");
return 0;
}
@@ -331,5 +333,31 @@ int parse_step_header_value(struct request * request)
PROCEED_TO(parse_step_header_name);
}
-// TODO: Pass data payload to Lua state.
+/// Makes sure that the expected data is in payload and pushes it forward to Lua.
+/// \param request Request to process
+/// \return -1 if an error has occured, 0 if too little data available or total number of bytes processed
+int parse_step_data(struct request * request)
+{
+ const int bytes_left = request->length - request->position;
+
+ if (bytes_left > request->expected_data_length)
+ {
+ return -1;
+ }
+ else if (bytes_left < request->expected_data_length)
+ {
+ return 0;
+ }
+
+ lua_pushlstring(request->lua, request->data + request->position, request->expected_data_length);
+
+ if (NULL != request->data)
+ {
+ free(request->data);
+ request->data = NULL;
+ }
+
+ return request->position;
+}
+
// TODO: Make a table from the request.
diff --git a/request.h b/request.h
index 723908f..cf1ad35 100644
--- a/request.h
+++ b/request.h
@@ -36,3 +36,4 @@ int parse_step_path(struct request *);
int parse_step_version(struct request *);
int parse_step_header_name(struct request *);
int parse_step_header_value(struct request *);
+int parse_step_data(struct request *);