This commit is contained in:
lxsang 2019-12-22 17:33:35 +01:00
parent c429a7dd66
commit 91d84baf01
6 changed files with 60 additions and 121 deletions

Binary file not shown.

View File

@ -110,7 +110,7 @@ void destroy_config()
{
close(cnf->sock);
}
list_free(&(cnf->rules));
freedict(cnf->rules);
}
freedict(server_config.ports);
}
@ -207,7 +207,7 @@ static int config_handler(void *conf, const char *section, const char *name,
p = (port_config_t*) malloc( sizeof(port_config_t));
p->htdocs = NULL;
p->sock = -1;
p->rules = list_init();
p->rules = dict();
dput(pconfig->ports,buf, p);
p->port = atoi(buf);
}
@ -224,8 +224,7 @@ static int config_handler(void *conf, const char *section, const char *name,
else
{
// other thing should be rules
list_put_s(&p->rules, name);
list_put_s(&p->rules, value);
dput(p->rules, name, strdup(value));
}
}
else
@ -703,7 +702,7 @@ int startup(unsigned *port)
return (httpd);
}
char *apply_rules(list_t rules, const char *host, char *url)
char *apply_rules(dictionary_t rules, const char *host, char *url)
{
// rule check
char *query_string = url;
@ -715,12 +714,13 @@ char *apply_rules(list_t rules, const char *host, char *url)
query_string++;
}
//char* oldurl = strdup(url);
int size = list_size(rules);
for (int i = 0; i < size; i += 2)
chain_t it;
char* k;
char* v;
for_each_assoc(it, rules)
{
char *k, *v;
k = list_at(rules, i)->value.s;
v = list_at(rules, i + 1)->value.s;
k = it->key;
v = (char*)it->value;
// 1 group
if (rule_check(k, v, host, url, query_string, url))
{

View File

@ -159,10 +159,13 @@ void antd_send_header(void* client, antd_response_header_t* res)
// send out cookie
if(res->cookie)
{
int size = list_size(res->cookie);
for (int i = 0; i < size; i++)
item_t el;
list_for_each(el, res->cookie)
{
__t(client,"Set-Cookie: %s", list_at(res->cookie, i)->value.s);
if(el->type == LIST_TYPE_POINTER && el->value.ptr)
{
__t(client,"Set-Cookie: %s", (char*)el->value.ptr);
}
}
list_free(&res->cookie);
res->cookie = NULL;

View File

@ -37,7 +37,7 @@ typedef struct {
int usessl;
char* htdocs;
int sock;
list_t rules;
dictionary_t rules;
} port_config_t;
typedef struct{

View File

@ -34,7 +34,8 @@ void list_put(list_t* l, item_t it)
{
if(*l == NULL || (*l)->type == LIST_TYPE_NIL)
{
free(*l);
if(*l != NULL)
free(*l);
*l = it;
return ;
}
@ -56,8 +57,8 @@ void list_put_special(list_t* l, const char* str)
}
else
{
v = list_item(LIST_TYPE_STRING);
v->value.s = strdup(str);
v = list_item(LIST_TYPE_POINTER);
v->value.ptr = strdup(str);
}
list_put(l,v);
}
@ -68,19 +69,29 @@ item_t list_last(list_t l)
p = p->next;
return p;
}
int list_remove(list_t l,int idx)
int list_remove(list_t* l,int idx)
{
if(l==NULL) return 0;
if(idx <0 || idx >= list_size(l)) return 0;
if(*l == NULL) return 0;
item_t it;
if(idx <0 || idx >= list_size(*l)) return 0;
if(idx == 0)
{
l=l->next;
it = *l;
*l=(*l)->next;
it->next = NULL;
list_free(&it);
return 1;
}
item_t np = list_at(l,idx-1);
item_t np = list_at(*l,idx-1);
if(np == NULL) return 0;
if(np->next == NULL) return 1;
np->next = np->next->next;
if(np->next != NULL)
{
item_t it = np->next;
it->next = NULL;
list_free(&it);
np->next = np->next->next;
}
return 1;
}
int list_size(list_t l)
@ -95,54 +106,6 @@ int list_size(list_t l)
}
return i;
}
char* as_string(list_t l)
{
char* str = "";
if(l != NULL && l->type != LIST_TYPE_NIL)
{
switch(l->type)
{
case LIST_TYPE_BASE64:
str = __s("b64:%s", l->value.b64);
break;
case LIST_TYPE_BOOL:
str = __s("bool:%d", l->value.b);
break;
case LIST_TYPE_DOUBLE:
str = __s("double:%lf", l->value.d);
break;
case LIST_TYPE_DATE:
str = __s("date:%s", l->value.date);
break;
case LIST_TYPE_INT:
case LIST_TYPE_I4:
str = __s("int:%d", l->value.i);
break;
case LIST_TYPE_STRING:
str = __s("string:%s", l->value.s);
break;
case LIST_TYPE_ARRAY:
str = __s("[%s]", as_string(l->value.array));
break;
default:
str = "<Unknown>";
break;
}
item_t np = l->next;
if(np)
{
str = __s("%s,\n%s", str, as_string(np));
}
return str;
}
return "[empty]";
}
item_t list_at(list_t l,int idx)
{
if(l == NULL || idx<0 || idx>= list_size(l))
@ -168,8 +131,7 @@ item_t list_item(int type)
list_t split(const char* str, const char* delim)
{
if(str == NULL || delim == NULL) return NULL;
char* str_cpy = strdup(str);
char* org_str = str_cpy;
char* str_cpy = str;
char* token;
list_t l = list_init();
while((token = strsep(&str_cpy,delim)))
@ -179,7 +141,6 @@ list_t split(const char* str, const char* delim)
list_put_special(&l,token);
}
}
free(org_str);
if(l->type== LIST_TYPE_NIL)
{
free(l);
@ -199,28 +160,11 @@ void list_put_d(list_t* l,double v)
it->value.d = v;
list_put(l,it);
}
void list_put_b(list_t* l,int v)
void list_put_ptr(list_t* l,void* v)
{
item_t it = list_item(LIST_TYPE_BOOL);
it->value.b = v;
list_put(l,it);
}
void list_put_b64(list_t* l,const char* v)
{
item_t it = list_item(LIST_TYPE_BASE64);
it->value.b64 = strdup(v);
list_put(l,it);
}
void list_put_date(list_t* l,const char* v)
{
item_t it = list_item(LIST_TYPE_DATE);
it->value.date = strdup(v);
list_put(l,it);
}
void list_put_s(list_t* l,const char* v)
{
item_t it = list_item(LIST_TYPE_STRING);
it->value.s = strdup(v);
item_t it = list_item(LIST_TYPE_POINTER);
it->value.ptr = v;
list_put(l,it);
}
void list_put_array(list_t* l,list_t v)
@ -229,19 +173,18 @@ void list_put_array(list_t* l,list_t v)
it->value.array = v;
list_put(l,it);
}
// unliked dictionary
// user have the responsibility to free
// pointer data
void list_free(list_t *l)
{
item_t curr;
while ((curr = (*l)) != NULL) {
(*l) = (*l)->next;
if(curr->type == LIST_TYPE_ARRAY)
if(curr->type == LIST_TYPE_ARRAY && curr->value.array)
list_free(&curr->value.array);
else if(curr->type == LIST_TYPE_STRING)
free(curr->value.s);
else if(curr->type == LIST_TYPE_DATE)
free(curr->value.date);
else if(curr->type == LIST_TYPE_BASE64)
free(curr->value.b64);
else if( curr->type == LIST_TYPE_POINTER && curr->value.ptr)
free(curr->value.ptr);
free (curr);
}
}

View File

@ -25,25 +25,21 @@ THE SOFTWARE.
#define LIST_H
#include "utils.h"
#define LIST_TYPE_ARRAY 601//hash("array")
#define LIST_TYPE_BASE64 335//hash("base64")
#define LIST_TYPE_BOOL 40//hash("boolean")
#define LIST_TYPE_DOUBLE 977//hash("double")
#define LIST_TYPE_DATE 49//hash("dateTime.iso8601")
#define LIST_TYPE_INT 1007//hash("int")
#define LIST_TYPE_I4 235//hash("i4")
#define LIST_TYPE_STRING 17//hash("string")
#define LIST_TYPE_NIL 529//hash("nil")
#define LIST_TYPE_ARRAY 0x5
#define LIST_TYPE_POINTER 0x4
#define LIST_TYPE_DOUBLE 0x2
#define LIST_TYPE_INT 0x1
#define LIST_TYPE_NIL 0x0
#define list_for_each(item, list) \
for(item = list;item!= NULL && item->type != LIST_TYPE_NIL; item = item->next)
typedef struct __item{
int type;
union{
int i;
int b;
char* s;
double d;
char* date;
char* b64;
void* ptr;
struct __item* array;
} value;
struct __item* next;
@ -53,19 +49,16 @@ list_t list_init();
void list_put(list_t*,item_t);
void list_put_i(list_t*,int);
void list_put_d(list_t*,double);
void list_put_b(list_t*,int);
void list_put_b64(list_t*,const char*);
void list_put_date(list_t*,const char*);
void list_put_s(list_t*,const char*);
void list_put_ptr(list_t*,void*);
void list_put_array(list_t*,list_t);
item_t list_last(list_t);
int list_remove(list_t,int);
int list_remove(list_t*,int);
int list_size(list_t);
item_t list_at(list_t,int);
int list_empty(list_t);
item_t list_item(int type);
list_t split(const char*, const char*);
char* as_string(list_t);
void list_put_special(list_t*, const char*);
void list_free(list_t *);
#endif