diff options
-rw-r--r-- | default.lua | 7 | ||||
-rw-r--r-- | plop.c | 2 | ||||
-rw-r--r-- | request.c | 49 | ||||
-rw-r--r-- | request.h | 3 |
4 files changed, 23 insertions, 38 deletions
diff --git a/default.lua b/default.lua index 1dc535f..914c990 100644 --- a/default.lua +++ b/default.lua @@ -1,5 +1,10 @@ --- Default client request handler. --- @return string Body of the response -function Handler (...) +function Handler (method, path, version, headers, data) + io.write("(", version, ") [", method, "] ", path, "\n") + for header, value in pairs(headers) do + io.write(" - ", header, " = '", value, "'\n") + end + print(data) return [["Hello, from plop/Lua"]] end @@ -113,7 +113,7 @@ int handle_client(lua_State * L, struct pollfd * pfd, struct request ** request, lua_getglobal((*request)->lua, "Handler"); lua_insert((*request)->lua, 1); lua_pushlstring((*request)->lua, (*request)->data, (*request)->length); - lua_call((*request)->lua, 4, 1); + lua_call((*request)->lua, 5, 1); size_t length; const char * body = lua_tolstring((*request)->lua, -1, &length); @@ -13,7 +13,6 @@ #include "http.h" static const int REQUEST_DATA_SIZE = 4096; -static const int MAX_HEADERS = 30; /// Allocates and initializes request structure. /// \return Pointer to initialized request or NULL in case of an error @@ -54,11 +53,6 @@ void free_request(lua_State * L, struct request * request) free(request->data); } - if (NULL != request->headerv) - { - free(request->headerv); - } - luaL_unref(L, LUA_REGISTRYINDEX, request->reference); free(request); @@ -246,16 +240,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 (NULL == request->headerv) + if (0 == lua_istable(request->lua, -1)) { - request->headerv = malloc(sizeof(struct header) * MAX_HEADERS); - request->headerc = 0; - memset(request->headerv, 0, sizeof(struct header) * MAX_HEADERS); - - if (NULL == request->headerv) - { - return -1; - } + lua_newtable(request->lua); } // TODO: Check for the header section end without backtracking. @@ -275,11 +262,11 @@ int parse_step_header_name(struct request * request) request->position = position; - if (0 >= request->headerv[request->headerc].name.start) + if (0 >= request->temp.start) { - request->headerv[request->headerc].name.start = read_until_word(request); + request->temp.start = read_until_word(request); - if (0 == request->headerv[request->headerc].name.start) + if (0 == request->temp.start) { return 0; } @@ -292,13 +279,13 @@ int parse_step_header_name(struct request * request) return 0; } - request->headerv[request->headerc].name.length = request->position - request->headerv[request->headerc].name.start; - request->step = parse_step_header_value; + request->temp.length = request->position - request->temp.start; + + lua_pushlstring(request->lua, request->data + request->temp.start, request->temp.length); // Skip ':' request->position++; - - return parse_step_header_value(request); + PROCEED_TO(parse_step_header_value); } /// Parses and verifies value of a single header from the request. @@ -306,11 +293,11 @@ int parse_step_header_name(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_value(struct request * request) { - if (0 >= request->headerv[request->headerc].value.start) + if (0 >= request->temp.start) { - request->headerv[request->headerc].value.start = read_until_word(request); + request->temp.start = read_until_word(request); - if (0 == request->headerv[request->headerc].value.start) + if (0 == request->temp.start) { return 0; } @@ -323,14 +310,10 @@ int parse_step_header_value(struct request * request) return 0; } - request->headerv[request->headerc].value.length = request->position - request->headerv[request->headerc].value.start; - request->step = parse_step_header_name; - request->headerc++; + request->temp.length = request->position - request->temp.start; - if (MAX_HEADERS == request->headerc) - { - return -1; - } + lua_pushlstring(request->lua, request->data + request->temp.start, request->temp.length); + lua_rawset(request->lua, -3); - return parse_step_header_name(request); + PROCEED_TO(parse_step_header_name); } @@ -23,9 +23,6 @@ struct request lua_State * lua; int reference; struct span temp; - struct header * headerv; - int headerc; - struct span body; }; struct request * new_request(lua_State *); |