diff options
| author | amelia squires <[email protected]> | 2025-01-09 15:13:14 -0600 |
|---|---|---|
| committer | amelia squires <[email protected]> | 2025-01-09 15:13:14 -0600 |
| commit | ea98d74a379dc2cc86579e58815a30fbf0be1459 (patch) | |
| tree | 9afa3e466234f1163bfccd5e44b9014f1b2ae5ac /src | |
| parent | b0fa1d1bcd4c07ce8623a5b6e8fdfb4c06ec6afc (diff) | |
| parent | cd90097eb3260b66c33fe5bb6d3f2869a90861d6 (diff) | |
merge
Diffstat (limited to 'src')
| -rw-r--r-- | src/net.c | 42 | ||||
| -rw-r--r-- | src/net/lua.c | 7 | ||||
| -rw-r--r-- | src/net/luai.c | 21 | ||||
| -rw-r--r-- | src/net/util.c | 47 | ||||
| -rw-r--r-- | src/net/util.h | 1 |
5 files changed, 85 insertions, 33 deletions
@@ -135,13 +135,14 @@ void* handle_client(void *_arg){ //printf("--\n");
//pthread_mutex_lock(&mutex);
- int read_state = 0;
+ //int read_state = 0;
thread_arg_struct* args = (thread_arg_struct*)_arg;
int client_fd = args->fd;
char* buffer;
char dummy[2] = {0, 0};
int header_eof = -1;
lua_State* L = args->L;
+ luaL_openlibs(L);
//sleep(1);
//create state for this thread
@@ -169,17 +170,34 @@ void* handle_client(void *_arg){ //printf("start: %f\n",(double)(clock() - begin) / CLOCKS_PER_SEC);
//read full request
//time_start(recv)
- int64_t bytes_received = recv_full_buffer(client_fd, &buffer, &header_eof, &read_state);
+ char* header = NULL;
+
+ int64_t bite = recv_header(client_fd, &buffer, &header);
+ header_eof = header - buffer;
+ printf("%x = %p - %p\n", header_eof, header, buffer);
- /*for(int i = 0; i != header_eof; i++)
+ if(bite == -2) net_error(client_fd, 431);
+ printf("'");
+ for(int i = bite - 20; i != bite; i++){
+ putchar(buffer[i]);
+ }
+ printf("'\n");
+ /*
+
+ return NULL;
+ int64_t bytes_received = recv_full_buffer(client_fd, &buffer, &header_eof, &read_state);
+
+
+ for(int i = 0; i != header_eof; i++)
putchar(buffer[i]);
- putchar('\n');*/
+ 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
- if(bytes_received >= -1){
+ if(bite >= -1){
parray_t* table;
//checks for a valid header
@@ -188,12 +206,11 @@ void* handle_client(void *_arg){ 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");
str* sT = (str*)parray_get(table, "Content-Type");
str* sC = (str*)parray_get(table, "Cookie");
- int some = bytes_received - header_eof - 10;
+ int some = bite - header_eof - 10;
struct file_parse* file_cont = calloc(1, sizeof * file_cont);
lua_newtable(L);
@@ -226,7 +243,7 @@ void* handle_client(void *_arg){ larray_clear(params);*/
if(sT != NULL)
- rolling_file_parse(L, &files_idx, &body_idx, buffer + header_eof + 4, sT, bytes_received - header_eof - 4, file_cont);
+ rolling_file_parse(L, &files_idx, &body_idx, header + 4, sT, bite - header_eof - 4, file_cont);
str_free(aa);
if(v != NULL){
@@ -279,12 +296,11 @@ void* handle_client(void *_arg){ luaI_tsets(L, req_idx, "ip", inet_ntoa(args->cli.sin_addr));
- if(bytes_received == -1){
+ if(bite == -1){
client_fd = -2;
}
- luaI_tsetb(L, req_idx, "partial", read_state == 1);
- luaI_tseti(L, req_idx, "_bytes", bytes_received - header_eof - 4);
+ luaI_tseti(L, req_idx, "_bytes", bite - header_eof - 4);
luaI_tseti(L, req_idx, "client_fd", client_fd);
luaI_tsetcf(L, req_idx, "roll", l_roll);
//luaI_tsetcf(L, req_idx, "continue", l_continue);
@@ -463,7 +479,7 @@ int start_serv(lua_State* L, int port){ args->cli = client_addr;
args->L = luaL_newstate();
- luaL_openlibs(args->L);
+ //luaL_openlibs(args->L);
pthread_mutex_lock(&mutex);
int old_top = lua_gettop(L);
diff --git a/src/net/lua.c b/src/net/lua.c index 6004343..d60c018 100644 --- a/src/net/lua.c +++ b/src/net/lua.c @@ -115,7 +115,7 @@ int l_stop(lua_State* L){ } int l_roll(lua_State* L){ - int alen; + int64_t alen; if(lua_gettop(L) > 2) { alen = luaL_checkinteger(L, 2); } else { @@ -125,7 +125,7 @@ int l_roll(lua_State* L){ lua_pushvalue(L, 1); lua_pushstring(L, "_bytes"); lua_gettable(L, 1); - int bytes = luaL_checkinteger(L, -1); + int64_t bytes = luaL_checkinteger(L, -1); lua_pushstring(L, "Content-Length"); lua_gettable(L, 1); @@ -133,7 +133,7 @@ int l_roll(lua_State* L){ lua_pushinteger(L, -1); return 1; } - int content_length = strtol(luaL_checkstring(L, -1), NULL, 10); + uint64_t content_length = strtol(luaL_checkstring(L, -1), NULL, 10); lua_pushstring(L, "_data"); lua_gettable(L, 1); struct file_parse* data = (void*)lua_topointer(L, -1); @@ -161,7 +161,6 @@ int l_roll(lua_State* L){ //time_start(recv) if(alen == -1) alen = content_length - bytes; - //printf("to read: %i\n", alen); char* buffer = malloc(alen * sizeof * buffer); int r = recv(client_fd, buffer, alen, 0); if(r <= 0){ diff --git a/src/net/luai.c b/src/net/luai.c index 1929818..3e16dcb 100644 --- a/src/net/luai.c +++ b/src/net/luai.c @@ -83,7 +83,6 @@ int rolling_file_parse(lua_State* L, int* files_idx, int* body_idx, char* buffer //time_end("start", start) //printf("hi\n"); if(content.status == NORMAL){ - //printf("normal\n"); //strnstr(buffer, ) //if(override) str_clear(current); //str_pushl(current, buffer, blen); @@ -102,8 +101,8 @@ int rolling_file_parse(lua_State* L, int* files_idx, int* body_idx, char* buffer //printf("\n"); if(*buffer == '\r'){ content.status = FILE_HEADER; - buffer+=2; - blen-=i+2; + buffer += 2; + blen -= i + 2; content.table_idx = lua_rawlen(L, *files_idx) + 1; lua_pushinteger(L, content.table_idx); @@ -131,8 +130,10 @@ int rolling_file_parse(lua_State* L, int* files_idx, int* body_idx, char* buffer } else if(buffer[i] == '\n'){ if(content.current->len == 0){ content.status = FILE_BODY; - buffer += i; - blen -= i; + + buffer += i + 1; + blen -= i + 1; + content.old = NULL; str_free(content.current); content.current = str_init(""); @@ -151,13 +152,11 @@ int rolling_file_parse(lua_State* L, int* files_idx, int* body_idx, char* buffer //time_end("file_header", file_header) //time_start(file_body) if(content.status == FILE_BODY){ - //printf("body\n"); - //if(content.old==NULL) content.old = str_init(""); - char* barrier_end = strnstr(buffer, content.boundary->c, blen); + char* barrier_end = memmem(buffer, blen, content.boundary->c, content.boundary->len); if(barrier_end == NULL){ str* temp = str_initl(content.current->c, content.current->len); - str_pushl(temp, buffer, blen); - barrier_end = strnstr(temp->c, content.boundary->c, temp->len); + str_pushl(temp, buffer, blen); + barrier_end = memmem(temp->c, temp->len, content.boundary->c, content.boundary->len); if(barrier_end != NULL) abort(); // todo str* temp2 = content.current; @@ -170,7 +169,7 @@ int rolling_file_parse(lua_State* L, int* files_idx, int* body_idx, char* buffer for(; *end != '\n'; end++); int clen = start - buffer; str_pushl(content.current, buffer, clen); - luaI_tsetsl(L, rfiles_idx, "content", content.current->c, content.current->len); + luaI_tsetsl(L, rfiles_idx, "content", content.current->c, content.current->len - 1); str_clear(content.current); blen-= end - buffer; buffer = end; diff --git a/src/net/util.c b/src/net/util.c index c9f359e..10a58fa 100644 --- a/src/net/util.c +++ b/src/net/util.c @@ -1,16 +1,51 @@ #include "common.h" #include "util.h" +int64_t recv_header(int client_fd, char** _buffer, char** header_eof){ + char* buffer = calloc(sizeof* buffer, BUFFER_SIZE); + *_buffer = buffer; + int64_t len = 0; + int64_t n = 0; + *header_eof = 0; + + for(;;){ + n = recv(client_fd, buffer + len, BUFFER_SIZE, 0); + + if(n < 0){ + printf("%s %i\n", strerror(errno), errno); + return -1; + } + + // search the last 4 characters too if they exist + // this could probably be changed to 3 + int64_t start_len = len - 4 > 0 ? len - 4 : 0; + int64_t search_end = len - 4 > 0 ? n + 4 : n; + if((*header_eof = memmem(buffer + start_len, search_end, "\r\n\r\n", 4)) != NULL){ + return len + n; + } + + if((len += n) >= MAX_HEADER_SIZE){ + return -2; + } + + buffer = realloc(buffer, sizeof* buffer * (len + BUFFER_SIZE + 1)); + + *_buffer = buffer; + } +} + /** * @brief calls recv into buffer until everything is read * */ +// deprecated!! replaced by recv_header (above) int64_t recv_full_buffer(int client_fd, char** _buffer, int* header_eof, int* state){ char* header, *buffer = malloc(BUFFER_SIZE * sizeof * buffer); memset(buffer, 0, BUFFER_SIZE); int64_t len = 0; *header_eof = -1; int n, content_len = -1; + uint64_t con_len_full = 0; //printf("&_\n"); for(;;){ n = recv(client_fd, buffer + len, BUFFER_SIZE, 0); @@ -35,14 +70,16 @@ int64_t recv_full_buffer(int client_fd, char** _buffer, int* header_eof, int* st if(cont_len_raw == NULL) abort(); //i is length of 'Content-Length: ' 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); + con_len_full = strtol(cont_len_str->c, NULL, 10); + //if(content_len < 0) p_fatal("idk"); str_free(cont_len_str); - if(content_len > max_content_length) { + if(con_len_full > max_content_length) { *_buffer = buffer; - *state = (len + n != content_len + *header_eof + 4); + *state = (len + n != con_len_full + *header_eof + 4); return len + n; } - buffer = realloc(buffer, content_len + *header_eof + 4 + BUFFER_SIZE); + content_len = 1; + buffer = realloc(buffer, con_len_full + *header_eof + 4 + BUFFER_SIZE); if(buffer == NULL) p_fatal("unable to allocate"); } @@ -57,7 +94,7 @@ int64_t recv_full_buffer(int client_fd, char** _buffer, int* header_eof, int* st } - if(content_len != -1 && len - *header_eof - 4 >= content_len) break; + if(content_len != -1 && len - *header_eof - 4 >= con_len_full) break; } *_buffer = buffer; return len; diff --git a/src/net/util.h b/src/net/util.h index 254cc32..cfac065 100644 --- a/src/net/util.h +++ b/src/net/util.h @@ -13,6 +13,7 @@ * @return {int64_t} bytes read, -1 if the body was damaged, -2 if the header was */ int64_t recv_full_buffer(int client_fd, char** _buffer, int* header_eof, int* state); +int64_t recv_header(int client_fd, char** _buffer, char** header_eof); /** * @brief converts the request buffer into a parray_t |
