diff options
| author | ame <[email protected]> | 2025-01-09 00:05:13 -0600 |
|---|---|---|
| committer | ame <[email protected]> | 2025-01-09 00:05:41 -0600 |
| commit | 6cb083d7c26faffe217200ac5657953b46748193 (patch) | |
| tree | 9c969b1210934015233f833d97eafd805fbe7b00 /src/net | |
| parent | a6aa1b70f66d7fe8ba81e0dcddac92687fe7acd7 (diff) | |
fix file upload and rewrite buffer reading
Diffstat (limited to 'src/net')
| -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 |
4 files changed, 56 insertions, 20 deletions
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 |
