aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--readme.md8
-rw-r--r--src/net.c42
-rw-r--r--src/net/lua.c7
-rw-r--r--src/net/luai.c21
-rw-r--r--src/net/util.c47
-rw-r--r--src/net/util.h1
-rw-r--r--tests/nya.lua8
7 files changed, 101 insertions, 33 deletions
diff --git a/readme.md b/readme.md
index 5edbdd0..cf31461 100644
--- a/readme.md
+++ b/readme.md
@@ -35,6 +35,14 @@ end, port)
note: any net code is prone to memory leaks, this will be addressed and fixed soon, on the other hand it has no know memory issues
+---
+
+~~net code also has some recv issues, ill fix these when i get time:3 only issue so far is with file parsing but i plan to rewrite alot of stuff~~
+
+update:
+
+recv code has been rewritten and i have fixed file parsing (with some testing) file parsing may be rewritten but works as of now
+
## building
build with `make`, output is `./lullaby.so` or (win)`./lullaby.dll`
diff --git a/src/net.c b/src/net.c
index 91abdcb..f866c98 100644
--- a/src/net.c
+++ b/src/net.c
@@ -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
diff --git a/tests/nya.lua b/tests/nya.lua
new file mode 100644
index 0000000..ca5299c
--- /dev/null
+++ b/tests/nya.lua
@@ -0,0 +1,8 @@
+llby = require "lullaby"
+
+llby.net.listen(function(server)
+ server:GET("/", function(res, req)
+ print("hi")
+ llby.io.pprint(req)
+ end)
+end, 8887)