1
0
mirror of https://github.com/lxsang/ant-http synced 2024-07-05 22:19:47 +02:00
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); close(cnf->sock);
} }
list_free(&(cnf->rules)); freedict(cnf->rules);
} }
freedict(server_config.ports); 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 = (port_config_t*) malloc( sizeof(port_config_t));
p->htdocs = NULL; p->htdocs = NULL;
p->sock = -1; p->sock = -1;
p->rules = list_init(); p->rules = dict();
dput(pconfig->ports,buf, p); dput(pconfig->ports,buf, p);
p->port = atoi(buf); p->port = atoi(buf);
} }
@ -224,8 +224,7 @@ static int config_handler(void *conf, const char *section, const char *name,
else else
{ {
// other thing should be rules // other thing should be rules
list_put_s(&p->rules, name); dput(p->rules, name, strdup(value));
list_put_s(&p->rules, value);
} }
} }
else else
@ -703,7 +702,7 @@ int startup(unsigned *port)
return (httpd); 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 // rule check
char *query_string = url; char *query_string = url;
@ -715,12 +714,13 @@ char *apply_rules(list_t rules, const char *host, char *url)
query_string++; query_string++;
} }
//char* oldurl = strdup(url); //char* oldurl = strdup(url);
int size = list_size(rules); chain_t it;
for (int i = 0; i < size; i += 2) char* k;
char* v;
for_each_assoc(it, rules)
{ {
char *k, *v; k = it->key;
k = list_at(rules, i)->value.s; v = (char*)it->value;
v = list_at(rules, i + 1)->value.s;
// 1 group // 1 group
if (rule_check(k, v, host, url, query_string, url)) 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 // send out cookie
if(res->cookie) if(res->cookie)
{ {
int size = list_size(res->cookie); item_t el;
for (int i = 0; i < size; i++) 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); list_free(&res->cookie);
res->cookie = NULL; res->cookie = NULL;

View File

@ -37,7 +37,7 @@ typedef struct {
int usessl; int usessl;
char* htdocs; char* htdocs;
int sock; int sock;
list_t rules; dictionary_t rules;
} port_config_t; } port_config_t;
typedef struct{ 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) if(*l == NULL || (*l)->type == LIST_TYPE_NIL)
{ {
free(*l); if(*l != NULL)
free(*l);
*l = it; *l = it;
return ; return ;
} }
@ -56,8 +57,8 @@ void list_put_special(list_t* l, const char* str)
} }
else else
{ {
v = list_item(LIST_TYPE_STRING); v = list_item(LIST_TYPE_POINTER);
v->value.s = strdup(str); v->value.ptr = strdup(str);
} }
list_put(l,v); list_put(l,v);
} }
@ -68,19 +69,29 @@ item_t list_last(list_t l)
p = p->next; p = p->next;
return p; return p;
} }
int list_remove(list_t l,int idx) int list_remove(list_t* l,int idx)
{ {
if(l==NULL) return 0; 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) if(idx == 0)
{ {
l=l->next; it = *l;
*l=(*l)->next;
it->next = NULL;
list_free(&it);
return 1; 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 == NULL) return 0;
if(np->next == NULL) return 1; if(np->next != NULL)
np->next = np->next->next; {
item_t it = np->next;
it->next = NULL;
list_free(&it);
np->next = np->next->next;
}
return 1; return 1;
} }
int list_size(list_t l) int list_size(list_t l)
@ -95,54 +106,6 @@ int list_size(list_t l)
} }
return i; 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) item_t list_at(list_t l,int idx)
{ {
if(l == NULL || idx<0 || idx>= list_size(l)) 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) list_t split(const char* str, const char* delim)
{ {
if(str == NULL || delim == NULL) return NULL; if(str == NULL || delim == NULL) return NULL;
char* str_cpy = strdup(str); char* str_cpy = str;
char* org_str = str_cpy;
char* token; char* token;
list_t l = list_init(); list_t l = list_init();
while((token = strsep(&str_cpy,delim))) while((token = strsep(&str_cpy,delim)))
@ -179,7 +141,6 @@ list_t split(const char* str, const char* delim)
list_put_special(&l,token); list_put_special(&l,token);
} }
} }
free(org_str);
if(l->type== LIST_TYPE_NIL) if(l->type== LIST_TYPE_NIL)
{ {
free(l); free(l);
@ -199,28 +160,11 @@ void list_put_d(list_t* l,double v)
it->value.d = v; it->value.d = v;
list_put(l,it); 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); item_t it = list_item(LIST_TYPE_POINTER);
it->value.b = v; it->value.ptr = 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);
list_put(l,it); list_put(l,it);
} }
void list_put_array(list_t* l,list_t v) 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; it->value.array = v;
list_put(l,it); list_put(l,it);
} }
// unliked dictionary
// user have the responsibility to free
// pointer data
void list_free(list_t *l) void list_free(list_t *l)
{ {
item_t curr; item_t curr;
while ((curr = (*l)) != NULL) { while ((curr = (*l)) != NULL) {
(*l) = (*l)->next; (*l) = (*l)->next;
if(curr->type == LIST_TYPE_ARRAY) if(curr->type == LIST_TYPE_ARRAY && curr->value.array)
list_free(&curr->value.array); list_free(&curr->value.array);
else if(curr->type == LIST_TYPE_STRING) else if( curr->type == LIST_TYPE_POINTER && curr->value.ptr)
free(curr->value.s); free(curr->value.ptr);
else if(curr->type == LIST_TYPE_DATE)
free(curr->value.date);
else if(curr->type == LIST_TYPE_BASE64)
free(curr->value.b64);
free (curr); free (curr);
} }
} }

View File

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