aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authoramelia squires <[email protected]>2024-10-02 11:54:33 -0500
committeramelia squires <[email protected]>2024-10-02 11:54:33 -0500
commit7c920f7f22a26e377aa9eab0f56ca45caddf7217 (patch)
treee5b22e3a47d50125e9050052f608d62f29d32003 /src
parent39f7d8562e355d85e7795f7cd43b29ed12f9c58e (diff)
server errors
Diffstat (limited to 'src')
-rw-r--r--src/net.c22
-rw-r--r--src/net/util.c13
-rw-r--r--src/net/util.h2
3 files changed, 34 insertions, 3 deletions
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);