From ec87ff4813299f7fff7b97f38a9a22f6e1afdc0d Mon Sep 17 00:00:00 2001 From: ame Date: Fri, 16 Feb 2024 12:43:34 -0600 Subject: hmm --- docs/net.md | 6 ++--- src/net.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++------- tests/net.lua | 1 + 3 files changed, 79 insertions(+), 12 deletions(-) diff --git a/docs/net.md b/docs/net.md index a39aaa7..da01fb2 100644 --- a/docs/net.md +++ b/docs/net.md @@ -110,13 +110,13 @@ res.header["test"] = "wowa" ... ``` -### server:static_serve ** +### res:serve ** -'takes two strings, first is server serve path, second is local path, being a file or directory +'takes one string, which is a path that will be served, file or dir ```lua ... -server:static_serve("/public", "./html/") +res:serve("./html/") ... ``` diff --git a/src/net.c b/src/net.c index 2768ab9..758f6cc 100644 --- a/src/net.c +++ b/src/net.c @@ -52,19 +52,35 @@ size_t recv_full_buffer(int client_fd, char** _buffer, int* header_eof){ size_t len = 0; *header_eof = -1; int n; - + int content_len = -1; + //printf("before\n"); for(;;){ n = recv(client_fd, buffer + len, BUFFER_SIZE, 0); if(*header_eof == -1 && (header = strstr(buffer, "\r\n\r\n")) != NULL){ + //printf("head\n"); *header_eof = header - buffer; + char* cont_len_raw = strstr(buffer, "Content-Length: "); + str* cont_len_str = str_init(""); + 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); + str_free(cont_len_str); + //printf("nut\n"); + } + //check if the recv read the whole buffer length, sometimes it wont so i peek to see if there is more + //if(n != BUFFER_SIZE && recv(client_fd, NULL, 1, MSG_PEEK) != 1) break; + len += n; + if(n != 0){ + //printf("buffer %i\n", n); + buffer = realloc(buffer, len + n); + //printf("realloc\n"); + memset(buffer + len, 0, n); + //printf("%i\n", len - *header_eof - 4); } - if(n != BUFFER_SIZE) break; - len += BUFFER_SIZE; - buffer = realloc(buffer, len + BUFFER_SIZE); - memset(buffer + len, 0, BUFFER_SIZE); + if(content_len != -1 && len - *header_eof - 4 >= content_len) break; } - //buffer[len - 1] = 0; - + //printf("%i\n",len - *header_eof - 4); *_buffer = buffer; return len + BUFFER_SIZE; } @@ -74,7 +90,7 @@ int parse_header(char* buffer, int header_eof, str*** _table, int* _len){ char add[] = {0,0}; int lines = 3; for(int i = 0; i != header_eof; i++) lines += buffer[i] == '\n'; - str** table = malloc(sizeof ** table * lines * 2); + str** table = malloc(sizeof ** table * (lines * 2 + 2)); table[0] = str_init("Request");// table[1] = str_init("Post|Get"); table[2] = str_init("Path");// table[3] = str_init("/"); table[4] = str_init("Version");// table[5] = str_init("HTTP/1.1"); @@ -111,6 +127,10 @@ int parse_header(char* buffer, int header_eof, str*** _table, int* _len){ } table[tlen] = str_init(current->c); tlen++; + table[tlen] = str_init("Body"); + tlen++; + table[tlen] = str_init(buffer + header_eof + 4); + tlen++; str_free(current); *_len = tlen / 2; *_table = table; @@ -344,7 +364,6 @@ int l_close(lua_State* L){ int client_fd = luaL_checkinteger(L, -1); if(client_fd <= 0) p_fatal("client fd already closed\n"); - return 0; lua_pushstring(L, "client_fd"); lua_pushinteger(L, -1); lua_settable(L, res_idx); @@ -353,6 +372,49 @@ int l_close(lua_State* L){ return 0; } +int l_serve(lua_State* L){ + int res_idx = 1; + + lua_pushvalue(L, res_idx); + lua_pushstring(L, "client_fd"); + lua_gettable(L, res_idx); + int client_fd = luaL_checkinteger(L, -1); + if(client_fd <= 0) + p_fatal("client fd already closed\n"); + + char* path = (char*)luaL_checkstring(L, 2); + + //continue here + + return 0; +} + +void file_parse(lua_State* L, char* buffer, str* content_type){ + str* boundary = str_init(""); + int state = 0; + for(int i = 0; content_type->c[i] != '\0'; i++){ + if(state == 2) str_pushl(boundary, content_type->c + i, 1); + if(content_type->c[i] == ';') state = 1; + if(content_type->c[i] == '=' && state == 1) state = 2; + } + //printf("%s\n",boundary->c); + + for(;;){ + str* file = str_init(""); + char* ind = strstr(buffer, boundary->c); + if(ind == NULL) break; + char* ending_ind = strstr(ind + boundary->len + 2, boundary->c); + if(ending_ind == NULL) break; + + str_pushl(file, ind + boundary->len + 2, ending_ind - ind - boundary->len - 4); + printf("'%s'\n",file->c); + buffer = ending_ind; + //printf("'%s'\n", buffer); + str_free(file); + } + str_free(boundary); +} + volatile size_t threads = 0; void* handle_client(void *_arg){ @@ -390,6 +452,7 @@ void* handle_client(void *_arg){ int k = stable_key(table, "Path", len); int R = stable_key(table, "Request", len); + int T = stable_key(table, "Content-Type", len); char portc[10] = {0}; sprintf(portc, "%i", args->port); @@ -419,9 +482,12 @@ void* handle_client(void *_arg){ } luaI_tsets(L, req_idx, "ip", inet_ntoa(args->cli.sin_addr)); + //printf("%s\n",table[T]->c); + file_parse(L, buffer + header_eof, table[T]); //functions luaI_tsetcf(L, res_idx, "send", l_send); + //luaI_tsetcf(L, res_idx, "serve", l_serve); luaI_tsetcf(L, res_idx, "write", l_write); luaI_tsetcf(L, res_idx, "close", l_close); diff --git a/tests/net.lua b/tests/net.lua index f521bc7..b115f0e 100644 --- a/tests/net.lua +++ b/tests/net.lua @@ -37,6 +37,7 @@ llib.net.listen( --_G.llib.io.pprint(_G._G._G._llib.crypto.md5("hi")) --_G.llib.io.pprint(_G._Go) --_G.llib.io.pprint(_G.wo.crypto.md5("55")) + --_G.llib.io.pprint(req) end) server:GET("/aa", function(res, req) -- cgit v1.2.3