From 22fa21a5dcfe077a69bb4d940945fdd66429627b Mon Sep 17 00:00:00 2001 From: ame Date: Wed, 28 Feb 2024 11:29:45 -0600 Subject: todo: merge recv full and file parsing --- src/hash/adler.h | 4 ++-- src/net.c | 41 +++++++++++++++++++++++++-------------- src/types/parray.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/types/str.c | 12 ++++++++---- 4 files changed, 93 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/hash/adler.h b/src/hash/adler.h index 747fe19..3bd8925 100644 --- a/src/hash/adler.h +++ b/src/hash/adler.h @@ -1,8 +1,8 @@ #include "../lua.h" #include -/** - * calculates a adler hash of (len) bytes +/*! + * \brief calculates a adler hash of (len) bytes * * @param {uint8_t*} input bytes * @param {size_t} input length diff --git a/src/net.c b/src/net.c index f49fad8..2957d7e 100644 --- a/src/net.c +++ b/src/net.c @@ -16,6 +16,7 @@ #include #include #include +#include #include "net.h" #include "lua.h" @@ -27,7 +28,7 @@ #define max_con 200 //2^42 -#define BUFFER_SIZE 67108864 +#define BUFFER_SIZE 26000000 static int ports[65535] = { 0 }; static parray_t* paths = NULL; @@ -67,6 +68,7 @@ int64_t recv_full_buffer(int client_fd, char** _buffer, int* header_eof){ n = recv(client_fd, buffer + len, BUFFER_SIZE, 0); if(n < 0){ *_buffer = buffer; + printf("%s %i\n",strerror(errno),errno); if(*header_eof == -1) return -2; //dont even try w/ request, no header to read return -1; //well the header is fine atleast @@ -86,16 +88,18 @@ int64_t recv_full_buffer(int client_fd, char** _buffer, int* header_eof){ for(int i = 16; cont_len_raw[i] != '\r'; i++) str_pushl(cont_len_str, cont_len_raw + i, 1); content_len = strtol(cont_len_str->c, NULL, 10); str_free(cont_len_str); - + buffer = realloc(buffer, content_len + *header_eof + 4 + BUFFER_SIZE); } len += n; - buffer = realloc(buffer, len + BUFFER_SIZE); - - memset(buffer + len, 0, n); + if(*header_eof == -1){ + buffer = realloc(buffer, len + BUFFER_SIZE); + } + //memset(buffer + len, 0, n); if(content_len != -1 && len - *header_eof - 4 >= content_len) break; } + //printf("%li\n%li",len, content_len + *header_eof + 4); *_buffer = buffer; return len; } @@ -552,14 +556,14 @@ int file_parse(lua_State* L, char* buffer, str* content_type, size_t blen){ volatile size_t threads = 0; void* handle_client(void *_arg){ - + clock_t begin = clock(); //pthread_mutex_lock(&mutex); thread_arg_struct* args = (thread_arg_struct*)_arg; int client_fd = args->fd; char* buffer; char dummy[2] = {0, 0}; int header_eof; - + //sleep(1); //create state for this thread lua_State* L = luaL_newstate(); luaL_openlibs(L); @@ -574,16 +578,19 @@ void* handle_client(void *_arg){ //l_pprint(L); lua_setglobal(L, "_G"); pthread_mutex_unlock(&mutex); - + printf("start: %f\n",(double)(clock() - begin) / CLOCKS_PER_SEC); + begin = clock(); //read full request int64_t bytes_received = recv_full_buffer(client_fd, &buffer, &header_eof); - + printf("read bytes: %li, %f\n",bytes_received,(double)(clock() - begin) / CLOCKS_PER_SEC); + begin = clock(); //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){ - + printf("parsed: %f\n",(double)(clock() - begin) / CLOCKS_PER_SEC); + begin = clock(); str* sk = (str*)parray_get(table, "Path"); str* sR = (str*)parray_get(table, "Request"); str* sT = (str*)parray_get(table, "Content-Type"); @@ -597,7 +604,8 @@ void* handle_client(void *_arg){ str_push(aa, sk->c); void* v = parray_find(paths, aa->c); - + printf("found: %f\n",(double)(clock() - begin) / CLOCKS_PER_SEC); + begin = clock(); str_free(aa); if(v != NULL){ lua_newtable(L); @@ -632,7 +640,8 @@ void* handle_client(void *_arg){ parray_set(table, "Body", (void*)str_init("")); } } - + printf("cookie and file: %f\n",(double)(clock() - begin) / CLOCKS_PER_SEC); + begin = clock(); for(int i = 0; i != table->len; i+=1){ //printf("'%s' :: '%s'\n",table[i]->c, table[i+1]->c); luaI_tsets(L, req_idx, table->P[i].key->c, ((str*)table->P[i].value)->c); @@ -664,7 +673,8 @@ void* handle_client(void *_arg){ luaI_tsets(L, header_idx, "Content-Type", "text/html"); luaI_tsetv(L, res_idx, "header", header_idx); - + printf("wrote table: %f\n",(double)(clock() - begin) / CLOCKS_PER_SEC); + begin = clock(); //the function(s) //get all function that kinda match parray_t* owo = (parray_t*)v; @@ -696,7 +706,8 @@ void* handle_client(void *_arg){ } } parray_lclear(owo); //dont free the rest - + printf("out: %f\n",(double)(clock() - begin) / CLOCKS_PER_SEC); + begin = clock(); lua_pushstring(L, "client_fd"); lua_gettable(L, res_idx); client_fd = luaL_checkinteger(L, -1); @@ -712,7 +723,7 @@ void* handle_client(void *_arg){ free(args); free(buffer); lua_close(L); - + printf("closed: %f\n",(double)(clock() - begin) / CLOCKS_PER_SEC); pthread_mutex_lock(&mutex); threads--; pthread_mutex_unlock(&mutex); diff --git a/src/types/parray.c b/src/types/parray.c index fae3cb5..9066425 100644 --- a/src/types/parray.c +++ b/src/types/parray.c @@ -6,6 +6,12 @@ #include "../lua.h" #include "parray.h" +/** + * @brief internal table on how to free values + * + * @param {void*} value to be free'd + * @param {enum free_type} type of free +*/ void free_method(void* v, enum free_type free_meth){ if(v != NULL){ if(free_meth == FREE) free(v); @@ -13,6 +19,11 @@ void free_method(void* v, enum free_type free_meth){ } } +/** + * @brief defines a parray_t + * + * @return {parray_t*} allocated parray_t +*/ parray_t* parray_init(){ parray_t* awa = malloc(sizeof * awa); awa->P = malloc(sizeof * awa->P); @@ -20,6 +31,13 @@ parray_t* parray_init(){ return awa; } +/** + * @brief sets value at key to value, adds a new index if new + * + * {parray_t*} the parray to update + * {char*} key + * {void*} value +*/ void parray_set(parray_t* p, char* key, void* value){ for(int i = 0; i != p->len; i++){ if(strcmp(p->P[i].key->c, key) == 0){ @@ -34,6 +52,13 @@ void parray_set(parray_t* p, char* key, void* value){ p->P[p->len - 1].value = value; } +/** + * @brief gets item with a key + * + * @param {parray_t*} the parray to search + * @param {char*} key + * @return value at index, or NULL +*/ void* parray_get(parray_t* p, char* key){ for(int i = 0; i != p->len; i++){ if(strcmp(p->P[i].key->c, key) == 0){ @@ -43,6 +68,13 @@ void* parray_get(parray_t* p, char* key){ return NULL; } +/** + * @brief gets index with a key + * + * @param {parray_t*} the parray to search + * @param {char*} key + * @return index, or -1 if not found +*/ int parray_geti(parray_t* p, char* key){ for(int i = 0; i != p->len; i++){ if(strcmp(p->P[i].key->c, key) == 0){ @@ -52,6 +84,13 @@ int parray_geti(parray_t* p, char* key){ return -1; } +/** + * @brief remove element if found + * + * @param {parray_t*} the parray to modify + * @param {char*} key + * @param {enum free_type} method to free value +*/ void parray_remove(parray_t* p, char* key, enum free_type free){ int ind = parray_geti(p, key); if(ind == -1) return; @@ -64,11 +103,22 @@ void parray_remove(parray_t* p, char* key, enum free_type free){ p->P = realloc(p->P, sizeof * p->P * (p->len + 1)); } +/** + * @brief frees base of parray_t, leaving the values + * + * @param {parray_t*} the parray free +*/ void parray_lclear(parray_t* p){ free(p->P); free(p); } +/** + * @brief frees entire parray_t + * + * @param {parray_t*} the parray free + * @param {enum free_type} the method to free values +*/ void parray_clear(parray_t* p, enum free_type clear_val){ for(int i = 0; i != p->len; i++){ str_free(p->P[i].key); @@ -100,6 +150,13 @@ int fmatch(char* s, char* p) { return 1; } +/** + * @brief find all elements in an array that match, allowing for wildcards + * + * @param {parray_t*} the parray to search + * @param {char*} the string to search for + * @return {parray_t*} populated array of matches +*/ parray_t* parray_find(parray_t* p, char* match){ parray_t* ret = malloc(sizeof * ret); ret->P = malloc(sizeof * ret->P * p->len); diff --git a/src/types/str.c b/src/types/str.c index f790dba..2783fb0 100644 --- a/src/types/str.c +++ b/src/types/str.c @@ -1,6 +1,8 @@ #include "str.h" #include "../lua.h" +#define alloc_buffer 64 + str* str_init(char* init){ if(init == NULL){ char cc = '\0'; @@ -9,7 +11,8 @@ str* str_init(char* init){ size_t len = strlen(init); str* s = malloc(sizeof * s); - s->c = malloc(len + 1); + s->_bytes = len + 1 + alloc_buffer; + s->c = malloc(s->_bytes); s->len = len ; memcpy(s->c, init, len + 1); @@ -23,13 +26,14 @@ void str_free(str* s){ void str_push(str* s, char* insert){ s->len += strlen(insert); - s->c = realloc(s->c, s->len + 5); + if(s->len + 1 >= s->_bytes) + s->c = realloc(s->c, s->_bytes = s->len + 1 + alloc_buffer); strcat(s->c, insert); } void str_pushl(str* s, char* insert, size_t l){ - - s->c = realloc(s->c, s->len + l + 5); + if(s->len + l >= s->_bytes) + s->c = realloc(s->c, s->_bytes = s->len + l + alloc_buffer); //strcat(s->c, insert); for(int i = 0; i != l; i++){ s->c[i + s->len] = insert[i]; -- cgit v1.2.3