diff --git a/Makefile b/Makefile index c4787ee..afa45a3 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ ifeq ($(UNAME_S),Darwin) endif CFLAGS=-W -Wall -g -std=c99 -D DEBUG -D USE_DB $(PF_FLAG) #-lsocket -PLUGINS= dummy.$(EXT) fileman.$(EXT) pluginsman.$(EXT) wterm.$(EXT) nodedaemon.$(EXT) +PLUGINS= dummy.$(EXT) fileman.$(EXT) pluginsman.$(EXT) wterm.$(EXT) nodedaemon.$(EXT) cookiex.$(EXT) PLUGINSDEP = plugins/ini.o plugins/plugin.o plugins/dbhelper.o plugins/dictionary.o plugins/utils.o plugins/list.o PLUGINLIBS = -lsqlite3 diff --git a/build/config.ini b/build/config.ini index 0458dae..2322310 100755 --- a/build/config.ini +++ b/build/config.ini @@ -8,6 +8,6 @@ tmpdir=/Users/mrsang/Google Drive/ushare/cwp/ant-http/build/tmp/ ; This enable some plugins to be initialised at server startup [AUTOSTART] -plugin = nodedaemon +;plugin = nodedaemon ;plugin=ffvm -;plugin=dummy \ No newline at end of file +;plugin=dummy diff --git a/build/httpd b/build/httpd index d7ebee3..4850208 100755 Binary files a/build/httpd and b/build/httpd differ diff --git a/build/httpd.dSYM/Contents/Resources/DWARF/httpd b/build/httpd.dSYM/Contents/Resources/DWARF/httpd index 250bb4b..9bde0a9 100644 Binary files a/build/httpd.dSYM/Contents/Resources/DWARF/httpd and b/build/httpd.dSYM/Contents/Resources/DWARF/httpd differ diff --git a/build/plugins/dummy.dylib b/build/plugins/dummy.dylib index ae848c9..fa8361d 100755 Binary files a/build/plugins/dummy.dylib and b/build/plugins/dummy.dylib differ diff --git a/build/plugins/fileman.dylib b/build/plugins/fileman.dylib index 3940074..86b122e 100755 Binary files a/build/plugins/fileman.dylib and b/build/plugins/fileman.dylib differ diff --git a/build/plugins/nodedaemon.dylib b/build/plugins/nodedaemon.dylib index a23e672..3d488e0 100755 Binary files a/build/plugins/nodedaemon.dylib and b/build/plugins/nodedaemon.dylib differ diff --git a/build/plugins/pluginsman.dylib b/build/plugins/pluginsman.dylib index 2370580..3b95a8c 100755 Binary files a/build/plugins/pluginsman.dylib and b/build/plugins/pluginsman.dylib differ diff --git a/build/plugins/wterm.dylib b/build/plugins/wterm.dylib index 10ce37c..bf76f8a 100755 Binary files a/build/plugins/wterm.dylib and b/build/plugins/wterm.dylib differ diff --git a/plugin_manager.c b/plugin_manager.c index 0b4137b..931192b 100644 --- a/plugin_manager.c +++ b/plugin_manager.c @@ -37,12 +37,13 @@ char* post_url_decode(int client,int len) dictionary decode_request(int client,const char* method,const char* query) { dictionary request = NULL; + dictionary cookie = NULL; char* line; if(strcmp(method,"GET") == 0) { while((line = read_line(client)) && strcmp("\r\n",line)) { - decode_cookie(line); + if(!cookie) cookie = decode_cookie(line); } request = decode_url_request(query); } @@ -70,7 +71,7 @@ dictionary decode_request(int client,const char* method,const char* query) } else { - decode_cookie(line); + if(!cookie) cookie = decode_cookie(line); } line = read_line(client); @@ -88,7 +89,7 @@ dictionary decode_request(int client,const char* method,const char* query) } else if(strstr(ctype,FORM_MULTI_PART)> 0) { //printf("Multi part form : %s\n", ctype); - return decode_multi_part_request(client,ctype); + request = decode_multi_part_request(client,ctype); } else { @@ -96,6 +97,11 @@ dictionary decode_request(int client,const char* method,const char* query) return NULL; } } + //if(cookie->key == NULL) {free(cookie);cookie= NULL;} + if(cookie && !request) + request = dict(); + + dput(request,"cookie",cookie); return request; } void __px(const char* data,int size) @@ -119,21 +125,20 @@ dictionary decode_cookie(const char* line) trim(token,' '); if(token != NULL &&strcasecmp(token,"Cookie") == 0) { - printf("%s\n", line); - token = strsep(&cpstr,":"); - if(token) + while((token = strsep(&cpstr,";"))) { - while((token1 = strsep(&token,";"))) - LOG("Found cookie : %s\n",token1); - } - else - { - //free(cpstr); - return NULL; + token1 = strsep(&token,"="); + if(token1) + { + if(dic == NULL) + dic = dict(); + LOG("Found cookie : %s = %s\n",token1,token); + dput(dic,token1,token); + } } } - //free(cpstr); return dic; + //free(cpstr); } /** * Decode the multi-part form data from the POST request diff --git a/plugins/cookiex/cookiex.c b/plugins/cookiex/cookiex.c index 5c858e2..7a36b2d 100644 --- a/plugins/cookiex/cookiex.c +++ b/plugins/cookiex/cookiex.c @@ -27,7 +27,23 @@ void execute(int client,const char* method,dictionary rq) void get(int client,const char* method,dictionary rq) { - + html(client); + if(rq) + { + dictionary ck = (dictionary)dvalue(rq,"cookie"); + if(ck) + { + association as; + for_each_assoc(as, ck) + { + __t(client,"%s -> %s
",as->key, as->value); + } + } + else + __t(client,"noo cookie"); + } + else + __t(client,"no request"); } void handler(int client, const char* method, const char* rqpth, dictionary rq) @@ -36,6 +52,10 @@ void handler(int client, const char* method, const char* rqpth, dictionary rq) { execute(client,method,rq); } + else if(EQU(rqpth,"get")) + { + get(client,method,rq); + } else { unknow(client); diff --git a/plugins/dbhelper.c b/plugins/dbhelper.c index c9b5a0e..f2fba39 100644 --- a/plugins/dbhelper.c +++ b/plugins/dbhelper.c @@ -176,11 +176,13 @@ dbrecord dbselect(sqlite3* db, const char* table,const char* fstring,...) } int hastable(sqlite3* db,const char* table) { - char * prefix = "select * from sqlite_master where type='table' and name='%s'"; - char* sql = __s(prefix,table); - int ret = dbquery(db,sql,NULL); - free(sql); - return ~ret; + char * prefix = __s("type='table' and name='%s'",table); + dbrecord rc = dbselect(db,"sqlite_master",prefix); + free(prefix); + if(!rc) return 0; + if(!rc->fields) return 0; + free(rc); + return 1; } int dbupdate(sqlite3* db,const char* table,const dbfield field,const char* fstring,...) { diff --git a/plugins/fileman/fileman.c.o b/plugins/fileman/fileman.c.o index a998d6d..b392da8 100644 Binary files a/plugins/fileman/fileman.c.o and b/plugins/fileman/fileman.c.o differ diff --git a/plugins/plugin.c b/plugins/plugin.c index af33027..0da0039 100644 --- a/plugins/plugin.c +++ b/plugins/plugin.c @@ -14,9 +14,9 @@ void __init_plugin__(const char* pl,const char*ph,const char* htdocs, const char }; #ifdef USE_DB -sqldb getdb() +sqldb __getdb(char *name) { - int plen = strlen(__plugin__.name)+strlen(__plugin__.dbpath)+4; + int plen = strlen(name)+strlen(__plugin__.dbpath)+4; char* path = (char*) malloc(plen*sizeof(char)); strcpy(path,__plugin__.dbpath); strcat(path,__plugin__.name); @@ -25,6 +25,10 @@ sqldb getdb() free(path); return ret; } +sqldb getdb() +{ + return getdb(__plugin__.name); +} #endif void header_base(int client) { @@ -243,6 +247,16 @@ void set_cookie(int client,dictionary dic) } response(client,""); } +void clear_cookie(int client, dictionary dic) +{ + header_base(client); + __t(client,"Content-Type: text/html; charset=utf-8"); + association assoc; + for_each_assoc(assoc,dic){ + __t(client,"Set-Cookie: %s=%s;expires=",assoc->key, (char*)assoc->value, server_time()); + } + response(client,""); +} char* config_dir() { struct stat st; diff --git a/plugins/plugin.h b/plugins/plugin.h index bcf5a2f..1366de3 100644 --- a/plugins/plugin.h +++ b/plugins/plugin.h @@ -55,9 +55,10 @@ char* route(const char*); char* htdocs(const char*); #ifdef USE_DB sqldb getdb(); +sqldb __getdb(char *name); #endif void set_cookie(int,dictionary); - +void clear_cookie(int, dictionary); /*Default function for plugin*/ void handler(int, const char*,const char*,dictionary); void unknow(int); diff --git a/plugins/pluginsman/pluginsman.c.o b/plugins/pluginsman/pluginsman.c.o index 55efbbb..50b3963 100644 Binary files a/plugins/pluginsman/pluginsman.c.o and b/plugins/pluginsman/pluginsman.c.o differ diff --git a/plugins/utils.c b/plugins/utils.c index 6ee596e..a4edd32 100644 --- a/plugins/utils.c +++ b/plugins/utils.c @@ -325,4 +325,9 @@ unsigned hash(const char* key, int hash_size) for (hashval = 0; *key != '\0'; key++) hashval = *key + 31 * hashval; return hashval % hash_size; +} +int is_file(const char* f) +{ + struct stat st; + return !(stat(f, &st) == -1); } \ No newline at end of file diff --git a/plugins/utils.h b/plugins/utils.h index 6945b1c..7109ea7 100644 --- a/plugins/utils.h +++ b/plugins/utils.h @@ -80,4 +80,5 @@ char *url_encode(const char *str); char from_hex(char ch); char to_hex(char code); unsigned hash(const char*, int); +int is_file(const char* f); #endif