summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2021-02-13 02:35:57 +0100
committerAki <please@ignore.pl>2021-02-13 02:35:57 +0100
commit3444dac786ad9c44b549765860fa76147d2be37d (patch)
treef9b54032cd88fe24f49aa6baadaa38254fec5660
parent29693b1880010578d3716c4ac4a43b3da9f0a4d3 (diff)
downloadplop-3444dac786ad9c44b549765860fa76147d2be37d.zip
plop-3444dac786ad9c44b549765860fa76147d2be37d.tar.gz
plop-3444dac786ad9c44b549765860fa76147d2be37d.tar.bz2
Removed connection reference from request
-rw-r--r--request.c46
-rw-r--r--request.h17
2 files changed, 33 insertions, 30 deletions
diff --git a/request.c b/request.c
index 9400ae4..d8c2051 100644
--- a/request.c
+++ b/request.c
@@ -16,7 +16,7 @@ static const int REQUEST_DATA_SIZE = 4096;
/// \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(struct connection * connection)
+struct request * new_request(void)
{
struct request * request = malloc(sizeof(struct request));
@@ -36,8 +36,6 @@ struct request * new_request(struct connection * connection)
return NULL;
}
- request->connection = connection;
-
return request;
}
@@ -61,7 +59,7 @@ int parse_request(struct connection * connection)
{
if (NULL == connection->request)
{
- connection->request = new_request(connection);
+ connection->request = new_request();
if (NULL == connection->request)
{
return -1;
@@ -79,7 +77,7 @@ int parse_request(struct connection * connection)
connection->request->data[length] = 0;
connection->request->length = length;
- return connection->request->step(connection->request);
+ return connection->request->step(connection->L, connection->request);
}
@@ -148,12 +146,13 @@ static int read_rest_of_line(struct request * request)
request->temp.start = 0; \
request->temp.length = 0; \
request->step = _x; \
- return _x(request)
+ return _x(L, request)
/// Parses method field of the request.
+/// \param L Connection's Lua state
/// \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_method(struct request * request)
+int parse_step_method(lua_State * L, struct request * request)
{
// TODO: Consider reworking read_* functions to be more aware of word boundaries.
request->temp.length = read_until_char(request, ' ');
@@ -163,16 +162,17 @@ int parse_step_method(struct request * request)
return 0;
}
- lua_pushlstring(request->connection->L, request->data + request->temp.start, request->temp.length);
+ lua_pushlstring(L, request->data + request->temp.start, request->temp.length);
PROCEED_TO(parse_step_path);
}
/// Parses path field of the request.
+/// \param L Connection's Lua state
/// \param request Request to process
/// \return -1 if an error has occured, 0 if too little data available or total number of bytes processed
// TODO: Consider spliting path into an actual path and arguments in this stage
-int parse_step_path(struct request * request)
+int parse_step_path(lua_State * L, struct request * request)
{
// TODO: Rethink interface of parse_step_* and read_* functions. After a break it looks like mess; it's a bad sign.
if (0 >= request->temp.start)
@@ -195,16 +195,17 @@ 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->connection->L, request->data + request->temp.start, request->temp.length);
+ lua_pushlstring(L, request->data + request->temp.start, request->temp.length);
PROCEED_TO(parse_step_version);
}
/// Parses and verifies http version field of the request.
+/// \param L Connection's Lua state
/// \param request Request to process
/// \return -1 if an error has occured, 0 if too little data available or total number of bytes processed
// TODO: Return -1 if version is unsupported, meaning other than HTTP/1.1
-int parse_step_version(struct request * request)
+int parse_step_version(lua_State * L, struct request * request)
{
if (0 >= request->temp.start)
{
@@ -225,19 +226,20 @@ int parse_step_version(struct request * request)
request->temp.length = request->position - request->temp.start;
- lua_pushlstring(request->connection->L, request->data + request->temp.start, request->temp.length);
+ lua_pushlstring(L, request->data + request->temp.start, request->temp.length);
PROCEED_TO(parse_step_header_name);
}
/// Parses and verifies name of a single header from the request.
+/// \param L Connection's Lua state
/// \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_header_name(struct request * request)
+int parse_step_header_name(lua_State * L, struct request * request)
{
- if (0 == lua_istable(request->connection->L, -1))
+ if (0 == lua_istable(L, -1))
{
- lua_newtable(request->connection->L);
+ lua_newtable(L);
}
if (request->length - request->position < 4)
@@ -284,7 +286,7 @@ int parse_step_header_name(struct request * request)
request->expected_data_length = -1;
}
- lua_pushlstring(request->connection->L, request->data + request->temp.start, request->temp.length);
+ lua_pushlstring(L, request->data + request->temp.start, request->temp.length);
// Skip ':'
request->position++;
@@ -292,9 +294,10 @@ int parse_step_header_name(struct request * request)
}
/// Parses and verifies value of a single header from the request.
+/// \param L Connection's Lua state
/// \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_header_value(struct request * request)
+int parse_step_header_value(lua_State * L, struct request * request)
{
if (0 >= request->temp.start)
{
@@ -322,16 +325,17 @@ int parse_step_header_value(struct request * request)
*(request->data + request->temp.start + request->temp.length) = '\r';
}
- lua_pushlstring(request->connection->L, request->data + request->temp.start, request->temp.length);
- lua_rawset(request->connection->L, -3);
+ lua_pushlstring(L, request->data + request->temp.start, request->temp.length);
+ lua_rawset(L, -3);
PROCEED_TO(parse_step_header_name);
}
/// Makes sure that the expected data is in payload and pushes it forward to Lua.
+/// \param L Connection's Lua state
/// \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)
+int parse_step_data(lua_State * L, struct request * request)
{
const int bytes_left = request->length - request->position;
@@ -344,7 +348,7 @@ int parse_step_data(struct request * request)
return 0;
}
- lua_pushlstring(request->connection->L, request->data + request->position, request->expected_data_length);
+ lua_pushlstring(L, request->data + request->position, request->expected_data_length);
if (NULL != request->data)
{
diff --git a/request.h b/request.h
index 27c1d01..9963df5 100644
--- a/request.h
+++ b/request.h
@@ -12,23 +12,22 @@ struct span
struct request
{
- int (* step)(struct request *);
+ int (* step)(lua_State *, struct request *);
char * data;
int length;
int position;
struct span temp;
int expected_data_length;
- struct connection * connection;
};
-struct request * new_request(struct connection *);
+struct request * new_request(void);
void free_request(struct request *);
int parse_request(struct connection *);
-int parse_step_method(struct request *);
-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 *);
+int parse_step_method(lua_State *, struct request *);
+int parse_step_path(lua_State *, struct request *);
+int parse_step_version(lua_State *, struct request *);
+int parse_step_header_name(lua_State *, struct request *);
+int parse_step_header_value(lua_State *, struct request *);
+int parse_step_data(lua_State *, struct request *);