cookies, clean memeory
This commit is contained in:
parent
d6eff6de36
commit
2cc619311a
|
@ -24,8 +24,6 @@ todo:
|
|||
|
||||
* connection limit
|
||||
|
||||
* parse ~~Content-Disposition and~~ cookies (maybe a general parsing function too)
|
||||
|
||||
* more doxygen like docs, everywhere
|
||||
|
||||
* make parray_t hash based
|
||||
|
|
26
src/net.c
26
src/net.c
|
@ -513,7 +513,7 @@ int file_parse(lua_State* L, char* buffer, str* content_type, size_t blen){
|
|||
luaI_tsets(L, tt, ((str*)aw->P[i].key)->c, ((str*)aw->P[i].value)->c);
|
||||
}
|
||||
luaI_tsetv(L, file_T, "Content-Disposition", tt);
|
||||
parray_clear(aw, 2);
|
||||
parray_clear(aw, STR);
|
||||
} else {
|
||||
luaI_tsets(L, file_T , luaL_checkstring(L, key), current->c);
|
||||
key = -1;
|
||||
|
@ -587,7 +587,8 @@ void* handle_client(void *_arg){
|
|||
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");
|
||||
|
||||
char portc[10] = {0};
|
||||
sprintf(portc, "%i", args->port);
|
||||
|
||||
|
@ -604,6 +605,25 @@ void* handle_client(void *_arg){
|
|||
lua_newtable(L);
|
||||
int res_idx = lua_gettop(L);
|
||||
|
||||
//handle cookies
|
||||
if(sC != NULL){
|
||||
lua_newtable(L);
|
||||
int lcookie = lua_gettop(L);
|
||||
|
||||
parray_t* cookie = parray_init();
|
||||
printf("%i\n",gen_parse(sC->c, sC->len, &cookie));
|
||||
for(int i = 0; i != cookie->len; i++){
|
||||
//printf("%s %s\n", cookie->P[i].key->c, ((str*)cookie->P[i].value)->c);
|
||||
luaI_tsets(L, lcookie, cookie->P[i].key->c, ((str*)cookie->P[i].value)->c);
|
||||
}
|
||||
luaI_tsetv(L, req_idx, "cookies", lcookie);
|
||||
parray_clear(cookie, STR);
|
||||
|
||||
str_free(sC);
|
||||
parray_remove(table, "Cookie", NONE);
|
||||
}
|
||||
|
||||
//handle files
|
||||
if(sT != NULL && bytes_received > 0){
|
||||
int pf = file_parse(L, buffer + header_eof, sT, bytes_received - header_eof);
|
||||
|
||||
|
@ -685,7 +705,7 @@ void* handle_client(void *_arg){
|
|||
|
||||
}
|
||||
|
||||
parray_clear(table, 1);
|
||||
parray_clear(table, STR);
|
||||
}
|
||||
|
||||
if(client_fd > 0) closesocket(client_fd);
|
||||
|
|
|
@ -6,6 +6,13 @@
|
|||
#include "../lua.h"
|
||||
#include "parray.h"
|
||||
|
||||
void free_method(void* v, enum free_type free_meth){
|
||||
if(v != NULL){
|
||||
if(free_meth == FREE) free(v);
|
||||
else if(free_meth == STR) str_free(v);
|
||||
}
|
||||
}
|
||||
|
||||
parray_t* parray_init(){
|
||||
parray_t* awa = malloc(sizeof * awa);
|
||||
awa->P = malloc(sizeof * awa->P);
|
||||
|
@ -36,16 +43,36 @@ void* parray_get(parray_t* p, char* key){
|
|||
return NULL;
|
||||
}
|
||||
|
||||
int parray_geti(parray_t* p, char* key){
|
||||
for(int i = 0; i != p->len; i++){
|
||||
if(strcmp(p->P[i].key->c, key) == 0){
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
void parray_remove(parray_t* p, char* key, enum free_type free){
|
||||
int ind = parray_geti(p, key);
|
||||
if(ind == -1) return;
|
||||
|
||||
str_free(p->P[ind].key);
|
||||
free_method(p->P[ind].value, free);
|
||||
|
||||
for(int i = ind; i < p->len - 1; i++) p->P[i] = p->P[i+1];
|
||||
p->len--;
|
||||
p->P = realloc(p->P, sizeof * p->P * (p->len + 1));
|
||||
}
|
||||
|
||||
void parray_lclear(parray_t* p){
|
||||
free(p->P);
|
||||
free(p);
|
||||
}
|
||||
|
||||
void parray_clear(parray_t* p, int clear_val){
|
||||
void parray_clear(parray_t* p, enum free_type clear_val){
|
||||
for(int i = 0; i != p->len; i++){
|
||||
str_free(p->P[i].key);
|
||||
if(clear_val == 1) free(p->P[i].value);
|
||||
else if(clear_val == 2) str_free(p->P[i].value);
|
||||
free_method(p->P[i].value, clear_val);
|
||||
}
|
||||
parray_lclear(p);
|
||||
}
|
||||
|
|
|
@ -12,16 +12,17 @@ typedef struct {
|
|||
int len;
|
||||
} parray_t;
|
||||
|
||||
enum free_type {
|
||||
NONE = 0, FREE = 1, STR = 2
|
||||
};
|
||||
|
||||
parray_t* parray_init();
|
||||
|
||||
void parray_set(parray_t*, char*, void*);
|
||||
|
||||
void* parray_get(parray_t* , char*);
|
||||
|
||||
void parray_clear(parray_t*, int);
|
||||
|
||||
int parray_geti(parray_t* , char*);
|
||||
void parray_remove(parray_t* p, char* key, enum free_type free);
|
||||
void parray_clear(parray_t*, enum free_type);
|
||||
void parray_lclear(parray_t*);
|
||||
|
||||
parray_t* parray_find(parray_t*, char*);
|
||||
|
||||
#endif //parray_h
|
|
@ -27,8 +27,11 @@ int gen_parse(char* inp, int len, parray_t** _table){
|
|||
state = 0;
|
||||
} else if(current->c[0] != '\0' || inp[i] != ' ') str_pushl(current, inp + i, 1);
|
||||
}
|
||||
parray_set(table, last->c, (void*)current);
|
||||
str_free(last);
|
||||
|
||||
if(last != NULL){
|
||||
parray_set(table, last->c, (void*)current);
|
||||
str_free(last);
|
||||
}
|
||||
*_table = table;
|
||||
return 1;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue