diff options
| author | ame <[email protected]> | 2024-02-22 10:17:40 -0600 |
|---|---|---|
| committer | ame <[email protected]> | 2024-02-22 10:17:40 -0600 |
| commit | 867a86a9ab31c0652a46759443fc98a49f91c8dd (patch) | |
| tree | f252e0c821a3c2cce56dd3180ea1c70fccd48d5a /src | |
| parent | 83f9dca38af329d214c7d6dd2819ec2c6621d679 (diff) | |
cleaning and switch to parray_t
Diffstat (limited to 'src')
| -rw-r--r-- | src/crypto.h | 2 | ||||
| -rw-r--r-- | src/hash/buzhash.c | 2 | ||||
| -rw-r--r-- | src/hash/djb2.c | 2 | ||||
| -rw-r--r-- | src/hash/farmhash.c | 2 | ||||
| -rw-r--r-- | src/hash/fasthash.c | 2 | ||||
| -rw-r--r-- | src/hash/fnv.c | 2 | ||||
| -rw-r--r-- | src/hash/jenkins.c | 2 | ||||
| -rw-r--r-- | src/hash/loselose.c | 2 | ||||
| -rw-r--r-- | src/hash/metrohash.c | 2 | ||||
| -rw-r--r-- | src/hash/murmur.c | 2 | ||||
| -rw-r--r-- | src/hash/pearson.c | 2 | ||||
| -rw-r--r-- | src/hash/pjw.c | 2 | ||||
| -rw-r--r-- | src/hash/sdbm.c | 2 | ||||
| -rw-r--r-- | src/hash/sha2-256.c | 2 | ||||
| -rw-r--r-- | src/hash/spookyhash.c | 2 | ||||
| -rw-r--r-- | src/i_common.c | 0 | ||||
| -rw-r--r-- | src/i_common.h | 2 | ||||
| -rw-r--r-- | src/io.c | 4 | ||||
| -rw-r--r-- | src/lua.c | 4 | ||||
| -rw-r--r-- | src/net.c | 129 | ||||
| -rw-r--r-- | src/sort.h | 3 | ||||
| -rw-r--r-- | src/table.h | 3 | ||||
| -rw-r--r-- | src/types/parray.c (renamed from src/parray.c) | 2 | ||||
| -rw-r--r-- | src/types/parray.h (renamed from src/parray.h) | 0 | ||||
| -rw-r--r-- | src/types/str.c (renamed from src/i_str.c) | 2 | ||||
| -rw-r--r-- | src/types/str.h (renamed from src/i_str.h) | 0 | ||||
| -rw-r--r-- | src/util.c (renamed from src/i_util.c) | 2 | ||||
| -rw-r--r-- | src/util.h (renamed from src/i_util.h) | 2 |
28 files changed, 86 insertions, 97 deletions
diff --git a/src/crypto.h b/src/crypto.h index 564ea58..e0a29b0 100644 --- a/src/crypto.h +++ b/src/crypto.h @@ -1,5 +1,5 @@ #include "lua.h" -#include "i_util.h" +#include "util.h" #include "hash/md5.h" #include "hash/sha01.h" #include "hash/sha2xx.h" diff --git a/src/hash/buzhash.c b/src/hash/buzhash.c index 36b39f3..9e1b560 100644 --- a/src/hash/buzhash.c +++ b/src/hash/buzhash.c @@ -1,4 +1,4 @@ -#include "../i_util.h" +#include "../util.h" #include "../crypto.h" #include <stdio.h> #include <stdlib.h> diff --git a/src/hash/djb2.c b/src/hash/djb2.c index 7ec39de..19075d2 100644 --- a/src/hash/djb2.c +++ b/src/hash/djb2.c @@ -1,4 +1,4 @@ -#include "../i_util.h" +#include "../util.h" #include "../crypto.h" #include <stdio.h> #include <stdint.h> diff --git a/src/hash/farmhash.c b/src/hash/farmhash.c index 93e194b..6533eb4 100644 --- a/src/hash/farmhash.c +++ b/src/hash/farmhash.c @@ -1,4 +1,4 @@ -#include "../i_util.h" +#include "../util.h" #include "../crypto.h" //include city hash too #include <stdint.h> diff --git a/src/hash/fasthash.c b/src/hash/fasthash.c index 3ad0ba2..0ac2cc0 100644 --- a/src/hash/fasthash.c +++ b/src/hash/fasthash.c @@ -1,4 +1,4 @@ -#include "../i_util.h" +#include "../util.h" #include "../crypto.h" #include <stdio.h> #include <stdint.h> diff --git a/src/hash/fnv.c b/src/hash/fnv.c index 1fc4d0a..ac1f88b 100644 --- a/src/hash/fnv.c +++ b/src/hash/fnv.c @@ -1,4 +1,4 @@ -#include "../i_util.h" +#include "../util.h" #include "../crypto.h" #include <stdio.h> #include <stdint.h> diff --git a/src/hash/jenkins.c b/src/hash/jenkins.c index b11de8e..a08f526 100644 --- a/src/hash/jenkins.c +++ b/src/hash/jenkins.c @@ -1,4 +1,4 @@ -#include "../i_util.h" +#include "../util.h" #include "../crypto.h" #include <stdio.h> #include <stdint.h> diff --git a/src/hash/loselose.c b/src/hash/loselose.c index a421079..f3350f5 100644 --- a/src/hash/loselose.c +++ b/src/hash/loselose.c @@ -1,4 +1,4 @@ -#include "../i_util.h" +#include "../util.h" #include "../crypto.h" #include <stdio.h> #include <stdint.h> diff --git a/src/hash/metrohash.c b/src/hash/metrohash.c index 0b6ca42..9ce08b3 100644 --- a/src/hash/metrohash.c +++ b/src/hash/metrohash.c @@ -1,4 +1,4 @@ -#include "../i_util.h" +#include "../util.h" #include "../crypto.h" #include <stdio.h> #include <stdint.h> diff --git a/src/hash/murmur.c b/src/hash/murmur.c index 9d913db..18f2887 100644 --- a/src/hash/murmur.c +++ b/src/hash/murmur.c @@ -1,4 +1,4 @@ -#include "../i_util.h" +#include "../util.h" #include "../crypto.h" #include <stdio.h> #include <stdint.h> diff --git a/src/hash/pearson.c b/src/hash/pearson.c index 9372931..4e1ea0a 100644 --- a/src/hash/pearson.c +++ b/src/hash/pearson.c @@ -1,4 +1,4 @@ -#include "../i_util.h" +#include "../util.h" #include "../crypto.h" #include <stdio.h> #include <stdint.h> diff --git a/src/hash/pjw.c b/src/hash/pjw.c index 8494ba2..50593b9 100644 --- a/src/hash/pjw.c +++ b/src/hash/pjw.c @@ -1,4 +1,4 @@ -#include "../i_util.h" +#include "../util.h" #include "../crypto.h" #include <stdio.h> diff --git a/src/hash/sdbm.c b/src/hash/sdbm.c index bec251e..d913d01 100644 --- a/src/hash/sdbm.c +++ b/src/hash/sdbm.c @@ -1,4 +1,4 @@ -#include "../i_util.h" +#include "../util.h" #include "../crypto.h" #include <stdio.h> #include <stdint.h> diff --git a/src/hash/sha2-256.c b/src/hash/sha2-256.c index 34afe63..e343825 100644 --- a/src/hash/sha2-256.c +++ b/src/hash/sha2-256.c @@ -1,4 +1,4 @@ -#include "../i_util.h" +#include "../util.h" #include "../crypto.h" #include <stdlib.h> #include <string.h> diff --git a/src/hash/spookyhash.c b/src/hash/spookyhash.c index 7e27d26..5281452 100644 --- a/src/hash/spookyhash.c +++ b/src/hash/spookyhash.c @@ -1,4 +1,4 @@ -#include "../i_util.h" +#include "../util.h" #include "../crypto.h" #include <stdint.h> #include <string.h> diff --git a/src/i_common.c b/src/i_common.c deleted file mode 100644 index e69de29..0000000 --- a/src/i_common.c +++ /dev/null diff --git a/src/i_common.h b/src/i_common.h deleted file mode 100644 index 66dc56a..0000000 --- a/src/i_common.h +++ /dev/null @@ -1,2 +0,0 @@ -#define i_swap(A,B) double temp = A; A = B; B = temp; - @@ -1,5 +1,5 @@ #include <unistd.h> -#include "i_str.h" +#include "types/str.h" #include "io.h" #include "stdlib.h" #include "stdio.h" @@ -30,7 +30,7 @@ int l_readfile(lua_State* L){ char* out = calloc(sz + 1, sizeof * out); fread(out, sizeof * out, sz, fp); - + lua_pushlstring(L, out, sz); fclose(fp); @@ -3,8 +3,8 @@ #include "io.h"
#include <stdlib.h>
#include <string.h>
-#include "i_str.h"
-#include "parray.h"
+#include "types/str.h"
+#include "types/parray.h"
static int ii = 0;
static int malloc_count = 0;
@@ -22,8 +22,8 @@ #include "io.h"
#include "table.h"
-#include "i_str.h"
-#include "parray.h"
+#include "types/str.h"
+#include "types/parray.h"
#define max_con 200
#define BUFFER_SIZE 2048
@@ -101,66 +101,52 @@ int64_t recv_full_buffer(int client_fd, char** _buffer, int* header_eof){ return len + BUFFER_SIZE;
}
-int parse_header(char* buffer, int header_eof, str*** _table, int* _len){
+int parse_header(char* buffer, int header_eof, parray_t** _table){
if(header_eof == -1) return -1;
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 + 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");
+ parray_t* table = parray_init();
str* current = str_init("");
- int ins = 1;
+
int oi = 0;
+ int item = 0;
for(; oi != header_eof; oi++){
- add[0] = buffer[oi];
- if(buffer[oi] == '\n') break;
- if(buffer[oi] == ' '){
- table[ins] = str_init(current->c);
- ins += 2;
+ if(buffer[oi] == ' ' || buffer[oi] == '\n'){
+ if(buffer[oi] == '\n') current->c[current->len - 1] = 0;
+ parray_set(table, item == 0 ? "Request" :
+ item == 1 ? "Path" : "Version", (void*)str_init(current->c));
str_clear(current);
- } else str_push(current, add);
+ item++;
+ if(buffer[oi] == '\n') break;
+ } else str_pushl(current, buffer + oi, 1);
}
- current->c[current->len - 1] = 0;
- table[ins] = str_init(current->c);
- str_clear(current);
- int tlen = 6;
int key = 1;
+ str* sw = NULL;
for(int i = oi + 1; i != header_eof; i++){
- if(key && buffer[i]==':' || !key && buffer[i]=='\n') {
- if(!key) current->c[current->len - 1] = 0;
- table[tlen] = str_init(current->c);
- str_clear(current);
- tlen++;
- i+=key;
- key = !key;
+ if(buffer[i] == ' ' && strcmp(current->c, "") == 0) continue;
+ if(key && buffer[i] == ':' || !key && buffer[i] == '\n'){
+ if(key){
+ sw = current;
+ current = str_init("");
+ key = 0;
+ } else {
+ if(buffer[oi] == '\n') current->c[current->len - 1] = 0;
+ parray_set(table, sw->c, (void*)str_init(current->c));
+ str_clear(current);
+ sw = NULL;
+ key = 1;
+ }
continue;
- }
- add[0] = buffer[i];
- str_push(current, add);
+ } else str_pushl(current, buffer + i, 1);
}
- table[tlen] = str_init(current->c);
- tlen++;
- table[tlen] = str_init("Body");
- tlen++;
- table[tlen] = str_init(buffer + header_eof + 4);
- tlen++;
+ parray_set(table, sw->c, (void*)str_init(current->c));
+ parray_set(table, "Body", (void*)str_init(buffer + header_eof + 4));
str_free(current);
- *_len = tlen / 2;
- *_table = table;
+ *_table = table;
return 0;
-}
-
-int stable_key(str** table, char* target, int flen){
- for(int i = 0; i != flen * 2; i+=2){
- if(strcmp(table[i]->c,target) == 0){
- return i + 1;
- }
- }
- return -1;
-}
+}s
void http_build(str** _dest, int code, char* code_det, char* header_vs, char* content, size_t len){
/**dest = str_init(
@@ -422,8 +408,8 @@ int l_serve(lua_State* L){ return 0;
}
-int file_parse(lua_State* L, char* buffer, str* content_type){
- str* boundary = str_init("");
+int file_parse(lua_State* L, char* buffer, str* content_type, char** end_buffer){
+ str* boundary = str_init(""); //usually add + 2 to the length when using
int state = 0;
for(int i = 0; content_type->c[i] != '\0'; i++){
if(state == 2) str_pushl(boundary, content_type->c + i, 1);
@@ -450,7 +436,7 @@ int file_parse(lua_State* L, char* buffer, str* content_type){ int key = -1;
for(char* s = ind + boundary->len + 2; s != header_eof; s++){
- if(*s == ':'){ //collapse Content-Disposition
+ if(*s == ':'){ //todo: collapse Content-Disposition
lua_pushlstring(L, current->c, current->len);
key = lua_gettop(L);
str_clear(current);
@@ -472,7 +458,7 @@ int file_parse(lua_State* L, char* buffer, str* content_type){ lua_pushstring(L, "content");
lua_pushlstring(L, file->c, file->len);
lua_settable(L, file_T);
-
+
buffer = ending_ind;
//printf("'%s'\n", buffer);
str_free(file);
@@ -484,6 +470,7 @@ int file_parse(lua_State* L, char* buffer, str* content_type){ }
str_free(boundary);
+ *end_buffer = buffer + boundary->len + 2;
lua_pushvalue(L, base_T);
return base_T;
}
@@ -517,21 +504,25 @@ void* handle_client(void *_arg){ int64_t bytes_received = recv_full_buffer(client_fd, &buffer, &header_eof);
//ignore if header is just fucked
if(bytes_received >= -1){
- str** table;
- int len = 0;
+ //str** table;
+ parray_t* table;
//checks for a valid header
- if(parse_header(buffer, header_eof, &table, &len) != -1){
+ if(parse_header(buffer, header_eof, &table) != -1){
- int k = stable_key(table, "Path", len);
- int R = stable_key(table, "Request", len);
- int T = stable_key(table, "Content-Type", len);
-
+ //int k = stable_key(table, "Path", len);
+ //int R = stable_key(table, "Request", len);
+ //int T = stable_key(table, "Content-Type", len);
+
+ str* sk = (str*)parray_get(table, "Path");
+ str* sR = (str*)parray_get(table, "Request");
+ str* sT = (str*)parray_get(table, "Content-Type");
+ //printf("%s\n", sk->c);
char portc[10] = {0};
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);
+ str_push(aa, sk->c);
//if(aa->c[aa->len - 1] != '/') str_push(aa, "/");
void* v = parray_find(paths, aa->c);
@@ -549,9 +540,14 @@ void* handle_client(void *_arg){ lua_newtable(L);
int res_idx = lua_gettop(L);
- for(int i = 0; i != len * 2; i+=2){
+ char* new_cont;
+ int pf = file_parse(L, buffer + header_eof, sT, &new_cont);
+
+ luaI_tsetv(L, req_idx, "files", pf);
+
+ for(int i = 0; i != table->len; i+=1){
//printf("'%s' :: '%s'\n",table[i]->c, table[i+1]->c);
- luaI_tsets(L, req_idx, table[i]->c, table[i+1]->c);
+ luaI_tsets(L, req_idx, table->P[i].key->c, ((str*)table->P[i].value)->c);
}
luaI_tsets(L, req_idx, "ip", inet_ntoa(args->cli.sin_addr));
@@ -563,8 +559,6 @@ void* handle_client(void *_arg){ luaI_tsetb(L, req_idx, "partial", bytes_received == -1);
luaI_tseti(L, req_idx, "_bytes", bytes_received);
//printf("%s\n",table[T]->c);
- int pf = file_parse(L, buffer + header_eof, table[T]);
- luaI_tsetv(L, req_idx, "files", pf);
//functions
luaI_tsetcf(L, res_idx, "send", l_send);
@@ -574,7 +568,7 @@ void* handle_client(void *_arg){ //values
luaI_tseti(L, res_idx, "client_fd", client_fd);
- luaI_tsets(L, res_idx, "_request", table[R]->c);
+ luaI_tsets(L, res_idx, "_request", sR->c);
//header table
lua_newtable(L);
@@ -595,8 +589,8 @@ void* handle_client(void *_arg){ 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"))){
+ if(strcmp(wowa->req, "all") == 0 || strcmp(wowa->req, sR->c) == 0 ||
+ (strcmp(sR->c, "HEAD") && strcmp(wowa->req, "GET"))){
luaI_tseti(L, res_idx, "passes", passes);
passes++;
@@ -624,10 +618,7 @@ void* handle_client(void *_arg){ }
- for(int i = 0; i != len * 2; i++){
- str_free(table[i]);
- }
- free(table);
+ parray_clear(table, 1);
}
if(client_fd > 0) closesocket(client_fd);
@@ -1,6 +1,5 @@ #include "lua.h" -#include "i_util.h" -#include "i_common.h" +#include "util.h" //comparison sorts int l_quicksort(lua_State*); //[double+int] -> arr[N] (greatest -> least) diff --git a/src/table.h b/src/table.h index 5b85e9a..704c917 100644 --- a/src/table.h +++ b/src/table.h @@ -1,6 +1,5 @@ #include "lua.h" -#include "i_util.h" -#include "i_common.h" +#include "util.h" #include "sort.h" #include <stdint.h> diff --git a/src/parray.c b/src/types/parray.c index 77c9771..3dcc57f 100644 --- a/src/parray.c +++ b/src/types/parray.c @@ -1,5 +1,5 @@ #include <stdio.h>
-#include "i_str.h"
+#include "str.h"
#include <stdlib.h>
#include <string.h>
diff --git a/src/parray.h b/src/types/parray.h index 3f33865..3f33865 100644 --- a/src/parray.h +++ b/src/types/parray.h diff --git a/src/i_str.c b/src/types/str.c index d77067b..279567f 100644 --- a/src/i_str.c +++ b/src/types/str.c @@ -1,4 +1,4 @@ -#include "i_str.h" +#include "str.h" #include "lua.h" str* str_init(char* init){ diff --git a/src/i_str.h b/src/types/str.h index 60b0cb9..60b0cb9 100644 --- a/src/i_str.h +++ b/src/types/str.h diff --git a/src/i_util.c b/src/util.c index 48749d7..3c2e279 100644 --- a/src/i_util.c +++ b/src/util.c @@ -1,4 +1,4 @@ -#include "i_util.h" +#include "util.h" #include <stdio.h> #include <stdlib.h> #include "lua.h" diff --git a/src/i_util.h b/src/util.h index b4b609a..9a5b295 100644 --- a/src/i_util.h +++ b/src/util.h @@ -16,5 +16,7 @@ #define color_white "\e[97m" #define color_reset "\e[0m" +#define i_swap(A,B) double temp = A; A = B; B = temp; + void p_fatal(const char*); void p_error(const char*); |
