aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--docs/net.md2
-rw-r--r--src/io.c13
-rw-r--r--src/net.c81
3 files changed, 56 insertions, 40 deletions
diff --git a/docs/net.md b/docs/net.md
index 6387589..a39aaa7 100644
--- a/docs/net.md
+++ b/docs/net.md
@@ -19,7 +19,7 @@ llib.net.listen(function(server)
end, 80)
```
-### server:lock server:unlock **
+### server:lock server:unlock
continues on the current thread, but pauses all other threads at that point
diff --git a/src/io.c b/src/io.c
index 5eb9de8..674c081 100644
--- a/src/io.c
+++ b/src/io.c
@@ -113,6 +113,7 @@ void i_pprint(lua_State* L, int indent, int skip_indent){
printf(" '%s'"color_gray": "color_reset, lua_tostring(L,-2));
} else {
if(!skip) print_indentation(indent + 1);
+ //printf(" '%i'"color_gray": "color_reset, luaL_checknumber(L,-2));
}
int tuwu = lua_gettop(L);
i_pprint(L,indent+1,1);
@@ -161,7 +162,7 @@ enum json_state {
#define push() \
if(is_array){\
- lua_pushinteger(L, i);\
+ lua_pushinteger(L, iter_count);\
} else {\
char kbuf[key->len];\
memset(kbuf, 0, key->len);\
@@ -198,14 +199,22 @@ void json_parse(lua_State* L, str* raw){
int last_idx = lua_gettop(L);
int is_array = raw->c[0] == '[';
int i = 1;
+ int iter_count = 1;
+ int escape_next = 0;
for(i = 1; i != raw->len - 1; i++){
topush[0] = *(raw->c + i);
if(state == normal && (topush[0] == ' ' || topush[0] == '\n' || topush[0] <= 10)) continue;
+ if(escape_next > 0) escape_next--;
switch(topush[0]){
+ case '\\':
+ escape_next = 2;
+ break;
case '"':
+ if(escape_next == 1) break;
case '{': case '}':
case '[': case ']':
+
if(last == '{' && topush[0] == '}' || last == '[' && topush[0] == ']') token_depth--;
if((last == '\0' || last == '"' && topush[0] == '"'
@@ -228,6 +237,7 @@ void json_parse(lua_State* L, str* raw){
str_clear(key);
str_clear(value);
count = 0;
+ iter_count++;
continue;
}
break;
@@ -238,6 +248,7 @@ void json_parse(lua_State* L, str* raw){
}
break;
}
+ //if(escape_next == 2) continue;
if(last == '{' && topush[0] == '{' || last == '[' && topush[0] == '[') token_depth++;
if(count == 0 && !is_array) str_push(key, topush);
diff --git a/src/net.c b/src/net.c
index c3239e5..7e856ec 100644
--- a/src/net.c
+++ b/src/net.c
@@ -15,6 +15,8 @@
#include <pthread.h>
#include <string.h>
#include <unistd.h>
+#include <sys/stat.h>
+
#include "net.h"
#include "lua.h"
@@ -33,6 +35,7 @@ struct lchar {
char* c;
int len;
char req[20];
+ enum route_type type;
};
struct sarray_t {
@@ -263,7 +266,8 @@ int l_write(lua_State* L){
lua_pushstring(L, "client_fd");
lua_gettable(L, res_idx);
int client_fd = luaL_checkinteger(L, -1);
- if(client_fd <= 0) abort(); // add error message
+ if(client_fd <= 0)
+ p_fatal("client fd already closed\n");
char* content = (char*)luaL_checkstring(L, 2);
@@ -301,7 +305,8 @@ int l_send(lua_State* L){
lua_pushstring(L, "client_fd");
lua_gettable(L, res_idx);
int client_fd = luaL_checkinteger(L, -1);
- if(client_fd <= 0) abort(); // add error message
+ if(client_fd <= 0)
+ p_fatal("client fd already closed\n");
char* content = (char*)luaL_checkstring(L, 2);
@@ -338,10 +343,8 @@ int l_close(lua_State* L){
lua_pushstring(L, "client_fd");
lua_gettable(L, res_idx);
int client_fd = luaL_checkinteger(L, -1);
- if(client_fd <= 0){
- printf("already closed\n");
- abort();
- }// add error message
+ if(client_fd <= 0)
+ p_fatal("client fd already closed\n");
return 0;
lua_pushstring(L, "client_fd");
lua_pushinteger(L, -1);
@@ -393,7 +396,9 @@ void* handle_client(void *_arg){
sprintf(portc, "%i", args->port);
str* aa = str_init(portc);
+ if(table[k]->c[table[k]->len - 1] != '/') str_push(table[k], "/");
str_push(aa, table[k]->c);
+ //if(aa->c[aa->len - 1] != '/') str_push(aa, "/");
void* v = parray_find(paths, aa->c);
@@ -442,23 +447,24 @@ void* handle_client(void *_arg){
struct sarray_t* awa = (struct sarray_t*)owo->P[i].value;
for(int z = 0; z != awa->len; z++){
-
+ char* path;
struct lchar* wowa = awa->cs[z];
if(strcmp(wowa->req, "all") == 0 || strcmp(wowa->req, table[R]->c) == 0 ||
(strcmp(table[R]->c, "HEAD") && strcmp(wowa->req, "GET"))){
- luaI_tseti(L, res_idx, "passes", passes);
- passes++;
+ luaI_tseti(L, res_idx, "passes", passes);
+ passes++;
- luaL_loadbuffer(L, wowa->c, wowa->len, wowa->c);
+ luaL_loadbuffer(L, wowa->c, wowa->len, wowa->c);
- int func = lua_gettop(L);
+ int func = lua_gettop(L);
- lua_pushvalue(L, func); // push function call
- lua_pushvalue(L, res_idx); //push methods related to dealing with the request
- lua_pushvalue(L, req_idx); //push info about the request
+ lua_pushvalue(L, func); // push function call
+ lua_pushvalue(L, res_idx); //push methods related to dealing with the request
+ lua_pushvalue(L, req_idx); //push info about the request
- //call the function
- lua_call(L, 2, 0);
+ //call the function
+ lua_call(L, 2, 0);
+
}
}
}
@@ -500,25 +506,19 @@ int start_serv(lua_State* L, int port){
struct sockaddr_in server_addr;
//open the socket
- if((server_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
- printf("error opening socket\n");
- abort();
- }
+ if((server_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ p_fatal("error opening socket\n");
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(port);
//bind to port
- if(bind(server_fd, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0){
- printf("failed to bind to port\n");
- abort();
- }
+ if(bind(server_fd, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0)
+ p_fatal("failed to bind to port\n");
- if(listen(server_fd, max_con) < 0){
- printf("failed to listen\n");
- abort();
- }
+ if(listen(server_fd, max_con) < 0)
+ p_fatal("failed to listen\n");
/*
lua_rawgeti(L, LUA_REGISTRYINDEX, ports[port]);
lua_pushstring(L, "/");
@@ -527,17 +527,18 @@ int start_serv(lua_State* L, int port){
lua_gettable(L, -2);
int aa = lua_gettop(L);*/
if (pthread_mutex_init(&mutex, NULL) != 0)
- printf("mutex init failed\n");
+ p_fatal("mutex init failed\n");
+
+ if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&(int){1}, sizeof(int)) < 0)
+ p_fatal("SO_REUSEADDR refused\n");
for(;;){
struct sockaddr_in client_addr;
socklen_t client_addr_len = sizeof(client_addr);
int* client_fd = malloc(sizeof(int));
- if((*client_fd = accept(server_fd, (struct sockaddr*)&client_addr, &client_addr_len)) < 0){
- printf("failed to accept\n");
- abort();
- }
+ if((*client_fd = accept(server_fd, (struct sockaddr*)&client_addr, &client_addr_len)) < 0)
+ p_fatal("failed to accept\n");
//printf("%s\n",inet_ntoa(client_addr.sin_addr));
//printf("%i\n",threads);
//open a state to call shit, should be somewhat thread safe
@@ -568,9 +569,12 @@ int l_req_com(lua_State* L, char* req){
int port = luaL_checkinteger(L, -1);
char portc[10] = {0};
- sprintf(portc, "%i%s", port, lua_tostring(L, 2));
+ sprintf(portc, "%i", port);//, lua_tostring(L, 2));
+ str* portss = str_init(portc);
+ str_push(portss, (char*)lua_tostring(L, 2));
+ if(portss->c[portss->len - 1] != '/') str_push(portss, "/");
-
+ struct lchar* awa;
lua_getglobal(L, "string");
lua_pushstring(L, "dump");
lua_gettable(L, -2);
@@ -579,7 +583,7 @@ int l_req_com(lua_State* L, char* req){
size_t len;
char* a = (char*)luaL_checklstring(L, -1, &len);
- struct lchar* awa = malloc(len + 1);
+ awa = malloc(len + 1);
awa->c = a;
awa->len = len;
strcpy(awa->req, req);
@@ -589,7 +593,7 @@ int l_req_com(lua_State* L, char* req){
paths = parray_init();
//please free this
- void* v_old_paths = parray_get(paths, portc);
+ void* v_old_paths = parray_get(paths, portss->c);
struct sarray_t* old_paths;
if(v_old_paths == NULL){
old_paths = malloc(sizeof * old_paths);
@@ -601,7 +605,8 @@ int l_req_com(lua_State* L, char* req){
old_paths->cs = realloc(old_paths->cs, sizeof * old_paths->cs * old_paths->len);
old_paths->cs[old_paths->len - 1] = awa;
- parray_set(paths, portc, (void*)old_paths);
+ parray_set(paths, portss->c, (void*)old_paths);
+ str_free(portss);
return 1;
}