From 94d76d9e7ed5cb9273169babfa27e60fb28c4104 Mon Sep 17 00:00:00 2001 From: amelia squires Date: Wed, 2 Oct 2024 11:54:33 -0500 Subject: server errors --- docs/net.md | 11 +++++------ src/net.c | 22 +++++++++++++++++++--- src/net/util.c | 13 +++++++++++++ src/net/util.h | 2 ++ tests/net.lua | 4 ++++ 5 files changed, 43 insertions(+), 9 deletions(-) diff --git a/docs/net.md b/docs/net.md index 9c841b3..016fe09 100644 --- a/docs/net.md +++ b/docs/net.md @@ -26,12 +26,11 @@ allows a third optional argument that offers some other options in a table forma the server will send these codes for these reasons |code|cause| |--|--| -|503**|too many current requests, more than max.connections| -|500**|anytime a server route crashes| -|431**|header is larger than max header size, more than max.header.size| -|414**|request uri is longer than max.uri.size| -|408**|request took too longer than max.request.timeout| -|404**|request has no defined route| +|503|too many current requests, more than max.connections| +|500|anytime a server route crashes| +|431|header is larger than max header size, more than max.header.size| +|414|request uri is longer than max.uri.size| +|408**|request took too long to read all of (recv timeout) than max.request.timeout| (more to come?**) diff --git a/src/net.c b/src/net.c index 9128d89..7f94047 100644 --- a/src/net.c +++ b/src/net.c @@ -3,6 +3,8 @@ #include "net/lua.h" #include "net/luai.h" +#define max_uri_size 2048 + volatile size_t threads = 0; void* handle_client(void *_arg){ //printf("--\n"); @@ -48,13 +50,19 @@ void* handle_client(void *_arg){ putchar(buffer[i]); putchar('\n');*/ //printf("hi %li:%i\n", bytes_received,header_eof); + if(bytes_received == -2) net_error(client_fd, 431); + - //ignore if header is just fucked + //ignore if header is just fucked if(bytes_received >= -1){ parray_t* table; //checks for a valid header - if(parse_header(buffer, header_eof, &table) != -1){ + int val = parse_header(buffer, header_eof, &table); + + if(val == -2) net_error(client_fd, 414); + + if(val >= 0){ str* sk = (str*)parray_get(table, "Path"); str* sR = (str*)parray_get(table, "Request"); @@ -211,7 +219,12 @@ void* handle_client(void *_arg){ lua_pushvalue(L, req_idx); //push info about the request //call the function - lua_call(L, 2, 0); + if(lua_pcall(L, 2, 0, 0) != 0){ + //send an error message if send has not been called + if(client_fd >= 0) net_error(client_fd, 500); + + goto net_end; + } //check if res:stop() was called lua_pushstring(L, "_stop"); @@ -311,7 +324,10 @@ int start_serv(lua_State* L, int port){ if(count >= max_con){ //deny request + net_error(*client_fd, 503); + close(*client_fd); free(client_fd); + continue; } count++; diff --git a/src/net/util.c b/src/net/util.c index 44a525b..150b8f9 100644 --- a/src/net/util.c +++ b/src/net/util.c @@ -63,6 +63,7 @@ int64_t recv_full_buffer(int client_fd, char** _buffer, int* header_eof, int* st return len; } +#define max_uri_len 2048 /** * @brief converts the request buffer into a parray_t * @@ -85,6 +86,11 @@ int parse_header(char* buffer, int header_eof, parray_t** _table){ item++; if(buffer[oi] == '\n') break; } else { + if(oi >= max_uri_len){ + *_table = table; + str_free(current); + return -2; + } str_pushl(current, buffer + oi, 1); } } @@ -418,3 +424,10 @@ void parse_mimetypes(){ //printf("done\n"); } + +int net_error(int fd, int code){ + char out[512] = {0}; + sprintf(out, "HTTP/1.1 %i %s\n\n", code, http_code(code)); + send(fd, out, strlen(out), 0); + return 0; +} diff --git a/src/net/util.h b/src/net/util.h index e592420..254cc32 100644 --- a/src/net/util.h +++ b/src/net/util.h @@ -53,3 +53,5 @@ parray_t* route_match(parray_t* paths, char* path, larray_t** params); int match_param(char* path, char* match, parray_t* arr); void parse_mimetypes(); + +int net_error(int fd, int code); diff --git a/tests/net.lua b/tests/net.lua index 65d9031..af66756 100644 --- a/tests/net.lua +++ b/tests/net.lua @@ -67,6 +67,10 @@ net.listen( res:send("

you would never

") end) + server:GET("/error", function(res, req) + res.a.a.a.a() + end) + end, -- cgit v1.2.3