diff options
Diffstat (limited to 'request.c')
-rw-r--r-- | request.c | 48 |
1 files changed, 22 insertions, 26 deletions
@@ -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) { |