mirror of
https://github.com/lxsang/ant-http
synced 2024-12-26 00:38:21 +01:00
clean up
This commit is contained in:
parent
358090fb96
commit
fc2daeb620
2
Makefile
2
Makefile
@ -50,7 +50,7 @@ SERVERLIB= -ldl $(LIB_FLAG) $(DB_LIB) $(SSL_LIB) -lpthread
|
||||
SERVER_O=plugin_manager.o \
|
||||
http_server.o
|
||||
#-lsocket
|
||||
PLUGINS= dummy.$(EXT) fileman.$(EXT) pluginsman.$(EXT) wterm.$(EXT) nodedaemon.$(EXT) cookiex.$(EXT) wsimg.$(EXT)
|
||||
PLUGINS= pluginsman.$(EXT) wterm.$(EXT) nodedaemon.$(EXT) wsimg.$(EXT)
|
||||
|
||||
LIBOBJS = libs/ini.o \
|
||||
libs/handle.o \
|
||||
|
@ -1,63 +0,0 @@
|
||||
#include "../plugin.h"
|
||||
|
||||
void init();
|
||||
call __init__ = init;
|
||||
|
||||
void init()
|
||||
{
|
||||
printf("Finish init\n");
|
||||
}
|
||||
|
||||
void execute(void* client,const char* method,dictionary rq)
|
||||
{
|
||||
|
||||
/**
|
||||
* Set cookie to the browser
|
||||
*/
|
||||
char* c = server_time();
|
||||
dictionary d = dict();
|
||||
dput(d,"test",c);
|
||||
dput(d,"test1","This is another cookie");
|
||||
set_cookie(client,"text/html; charset=utf-8",d,__plugin__.name);
|
||||
|
||||
LOG("%s",c);
|
||||
__t(client,"<h1>Set the cookie</h1>");
|
||||
freedict(d);
|
||||
}
|
||||
|
||||
void get(void* 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 <br/>",as->key, as->value);
|
||||
}
|
||||
}
|
||||
else
|
||||
__t(client,"noo cookie");
|
||||
}
|
||||
else
|
||||
__t(client,"no request");
|
||||
|
||||
}
|
||||
void handler(void* client, const char* method, const char* rqpth, dictionary rq)
|
||||
{
|
||||
if(EQU(rqpth,"default"))
|
||||
{
|
||||
execute(client,method,rq);
|
||||
}
|
||||
else if(EQU(rqpth,"get"))
|
||||
{
|
||||
get(client,method,rq);
|
||||
}
|
||||
else
|
||||
{
|
||||
unknow(client);
|
||||
}
|
||||
}
|
@ -1,153 +0,0 @@
|
||||
#include "../plugin.h"
|
||||
|
||||
void init();
|
||||
call __init__ = init;
|
||||
sqldb db;
|
||||
|
||||
void init()
|
||||
{
|
||||
db = getdb();
|
||||
if(db == NULL)
|
||||
printf("Unable to get database %s \n", __plugin__.name);
|
||||
// create table
|
||||
char* sql = "CREATE TABLE dummy(id INTEGER PRIMARY KEY, color TEXT);";
|
||||
if(hastable(db,"dummy") == 0)
|
||||
dbquery(db,sql,NULL);
|
||||
printf("Finish init\n");
|
||||
}
|
||||
void pexit()
|
||||
{
|
||||
LOG("%s\n","Plugin DUMMY is exited");
|
||||
}
|
||||
void execute(void* client,const char* method,dictionary rq)
|
||||
{
|
||||
|
||||
char * question;
|
||||
|
||||
question = dvalue(rq,"color");
|
||||
dbfield field = __field();
|
||||
|
||||
if(question != NULL)
|
||||
{
|
||||
add_field(&field,"color",question);
|
||||
dbinsert(db,"dummy",field);
|
||||
}
|
||||
free(field);
|
||||
json(client);
|
||||
|
||||
dbrecord records = dball(db,"dummy");
|
||||
|
||||
__t(client,"{ \"records\":[");
|
||||
int cnt = 0;
|
||||
for(dbrecord r = records;r != NULL; r=r->next)
|
||||
{
|
||||
|
||||
if(cnt!=0) __t(client,",");
|
||||
dbfield row = r-> fields;
|
||||
__t(client,"{\"id\":%s,\"color\":\"%s\"}",
|
||||
value_of(row,"id"),
|
||||
value_of(row,"color"));
|
||||
free(row);
|
||||
cnt++;
|
||||
}
|
||||
__t(client, "],\"total\":%d}",records->idx);
|
||||
free(records);
|
||||
//__t(client, query);
|
||||
}
|
||||
|
||||
// delete record
|
||||
void delete(void* client,const char* method,dictionary rq)
|
||||
{
|
||||
char* id = dvalue(rq,"id");
|
||||
html(client);
|
||||
if(id==NULL)
|
||||
{
|
||||
__t(client,"<p>Invalid delere request <a href='%s'>Back</a></p>", route(NULL));
|
||||
}
|
||||
else
|
||||
{
|
||||
if(dbdelete(db,"dummy","id=%s",id))
|
||||
{
|
||||
redirect(client,route(NULL));
|
||||
} else
|
||||
{
|
||||
__t(client,"<p>Invalid delere request <a href='%s'>Back</a></p>", route(NULL));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
void update(void* client,const char* method,dictionary rq)
|
||||
{
|
||||
char * id;
|
||||
html(client);
|
||||
if(IS_GET(method))
|
||||
{
|
||||
id = dvalue(rq,"id");
|
||||
if(id!=NULL)
|
||||
{
|
||||
dbrecord rc = dbselect(db,"dummy","id=%s",id);
|
||||
if(rc->idx > 0)
|
||||
{
|
||||
__t(client,"<html><body><FORM ACTION=\"%s\" METHOD=\"POST\">\
|
||||
Enter a color: \
|
||||
<textarea rows=\"4\" cols=\"50\" NAME=\"color\">%s</textarea>\
|
||||
<INPUT TYPE=\"hidden\" NAME=\"id\" Value=\"%s\">\
|
||||
<INPUT id=\"button\" TYPE=\"submit\">\
|
||||
</FORM> </body></html>",route("update"),value_of(rc->fields,"color"),id);
|
||||
free(rc);
|
||||
return;
|
||||
}
|
||||
}
|
||||
__t(client,"<p>No data found. <a href='%s'>Back</a></p>",route(NULL));
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
char* color;
|
||||
id = dvalue(rq,"id");
|
||||
color = dvalue(rq,"color");
|
||||
dbfield fields = __field();
|
||||
add_field(&fields,"color",color);
|
||||
if(dbupdate(db,"dummy",fields,"id=%s",id))
|
||||
{
|
||||
redirect(client,route(NULL));
|
||||
}
|
||||
else
|
||||
{
|
||||
__t(client,"<p>Invalid Update request <a href='%s'>Back</a></p>", route(NULL));
|
||||
}
|
||||
free(fields);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void jsonex(void* client,const char* method,dictionary rq)
|
||||
{
|
||||
//json(client);
|
||||
//__t(client,"{name:\"%s\", age:%d}","Sang",30);
|
||||
jpeg(client);
|
||||
__f(client,htdocs("images/ex.jpg"));
|
||||
}
|
||||
void handler(void* client, const char* method, const char* rqpth, dictionary rq)
|
||||
{
|
||||
if(EQU(rqpth,"default"))
|
||||
{
|
||||
execute(client,method,rq);
|
||||
}
|
||||
else if(EQU(rqpth,"delete"))
|
||||
{
|
||||
delete(client,method,rq);
|
||||
}
|
||||
else if(EQU(rqpth,"update"))
|
||||
{
|
||||
execute(client,method,rq);
|
||||
}
|
||||
else if(EQU(rqpth,"jsonex"))
|
||||
{
|
||||
execute(client,method,rq);
|
||||
}
|
||||
else
|
||||
{
|
||||
unknow(client);
|
||||
}
|
||||
}
|
@ -1,215 +0,0 @@
|
||||
#include "../plugin.h"
|
||||
#define MAXSIZE 500000
|
||||
#define FRECORD "{\"recid\":%d,\"name\":\"%s\",\"size\":%d,\"changed\":\"%s\",\"type\":%d,\"style\":\"%s\",\"mime\":\"%s\"}"
|
||||
#define RRECORD "{ \"records\":[%s], \"total\":%d,\"folder\":\"%s\",\"router\":[%s]}"
|
||||
#define FOLLIST "{ \"name\":\"%s\", \"path\":\"%s\"}"
|
||||
|
||||
char* folder_list_from(const char* aPath)
|
||||
{
|
||||
if(aPath == NULL || strlen(aPath)==0 || strcmp(aPath,DIR_SEP)==0)
|
||||
return __s(FOLLIST,"HTDOCS",DIR_SEP);
|
||||
list path = split(aPath,DIR_SEP);
|
||||
char* flist=__s(FOLLIST,"HTDOCS",DIR_SEP);
|
||||
char* route = "";
|
||||
if(path)
|
||||
{
|
||||
for(list np = path; np != NULL; np=np->next)
|
||||
{
|
||||
route = __s("%s%s%s", route, DIR_SEP, np->value.s);
|
||||
if(flist == NULL)
|
||||
flist = __s(FOLLIST, np->value.s,route);
|
||||
else
|
||||
flist = __s("%s,%s", flist,__s(FOLLIST,np->value.s,route));
|
||||
}
|
||||
}
|
||||
free(path);
|
||||
//free(route);
|
||||
return flist;
|
||||
}
|
||||
|
||||
void execute(void* client,const char* method,dictionary rq)
|
||||
{
|
||||
DIR *d;
|
||||
struct dirent *dir;
|
||||
struct stat st;
|
||||
int frec=0,rrec = 0, id =0;
|
||||
char* flist = "",*dlist = "";
|
||||
char* tmp= NULL;
|
||||
char* rpath = R_STR(rq,"path");
|
||||
if(!rpath || strlen(rpath) == 0) rpath = DIR_SEP;
|
||||
char* path = __s("%s%s",__plugin__.htdocs,rpath);
|
||||
d = opendir(path);
|
||||
|
||||
if (d)
|
||||
{
|
||||
while ((dir = readdir(d)) != NULL)
|
||||
{
|
||||
//ignore curent directory, parent directory and hidden files and folders
|
||||
if(strcmp(dir->d_name,".") == 0 ||
|
||||
strcmp(dir->d_name,"..")==0|| *(dir->d_name)=='.') continue;
|
||||
if( stat(__s("%s%s%s",path,DIR_SEP,dir->d_name), &st) == 0 )
|
||||
{
|
||||
if(S_ISDIR(st.st_mode))
|
||||
{
|
||||
tmp = __s(FRECORD,id,
|
||||
dir->d_name,
|
||||
(int)st.st_size,
|
||||
__time(st.st_mtime),
|
||||
0,
|
||||
"font-weight: bold;",
|
||||
"folder");
|
||||
if(rrec != 0)
|
||||
dlist = __s("%s,%s",dlist,tmp);
|
||||
else
|
||||
dlist = tmp;
|
||||
rrec++;
|
||||
//free(tmp);
|
||||
|
||||
} else
|
||||
{
|
||||
tmp = __s(FRECORD, id,
|
||||
dir->d_name,
|
||||
(int)st.st_size,
|
||||
__time(st.st_mtime),1,"",
|
||||
mime(dir->d_name));
|
||||
if(frec != 0)
|
||||
flist = __s("%s,%s",flist,tmp);
|
||||
else
|
||||
flist = tmp;
|
||||
frec++;
|
||||
//free(tmp);
|
||||
}
|
||||
id++;
|
||||
}
|
||||
}
|
||||
closedir(d);
|
||||
if(strlen(dlist) == 0)
|
||||
dlist = flist;
|
||||
else if(strlen(flist) > 0)
|
||||
dlist = __s("%s,%s",dlist,flist);
|
||||
}
|
||||
json(client);
|
||||
__t(client,RRECORD,dlist,frec+rrec,rpath,folder_list_from(rpath));
|
||||
//if(path) free(path);
|
||||
//if(rpath) free(rpath);
|
||||
if(tmp) free(tmp);
|
||||
|
||||
}
|
||||
|
||||
void add(void* c, const char* m, dictionary rq)
|
||||
{
|
||||
json(c);
|
||||
if(IS_GET(m))
|
||||
{
|
||||
__t(c,__RESULT__,0,"Bad request:GET");
|
||||
return;
|
||||
}
|
||||
char* rpath = R_STR(rq,"path");
|
||||
if(!rpath)
|
||||
{
|
||||
__t(c,__RESULT__,0,"Unknow path");
|
||||
return;
|
||||
}
|
||||
|
||||
char * file_name = R_STR(rq,"pfile.file");
|
||||
if(file_name == NULL)
|
||||
{
|
||||
__t(c,__RESULT__,0,"Cannot send file to server");
|
||||
return;
|
||||
}
|
||||
|
||||
int size = R_INT(rq,"pfile.size");
|
||||
if(size>MAXSIZE)
|
||||
{
|
||||
__t(c,__RESULT__,0,"Cannot accept file more than 500Kb");
|
||||
return;
|
||||
}
|
||||
if(!upload(R_STR(rq,"pfile.tmp"),__s("%s/%s/%s",__plugin__.htdocs,rpath,file_name)))
|
||||
{
|
||||
__t(c,__RESULT__,0,"Cannot move file to plugin dir");
|
||||
return;
|
||||
}
|
||||
__t(c,__RESULT__,1,"OK");
|
||||
return;
|
||||
}
|
||||
|
||||
void mkfolder(void* c, const char* m, dictionary rq)
|
||||
{
|
||||
json(c);
|
||||
if(IS_GET(m))
|
||||
{
|
||||
__t(c,__RESULT__,0,"Bad request:GET");
|
||||
return;
|
||||
}
|
||||
char* dname = R_STR(rq,"dname");
|
||||
char* rpath = R_STR(rq, "path");
|
||||
if(!dname)
|
||||
{
|
||||
__t(c,__RESULT__,0,"Folder name is empty");
|
||||
return;
|
||||
}
|
||||
if(!rpath)
|
||||
{
|
||||
__t(c,__RESULT__,0,"Unknow path");
|
||||
return;
|
||||
}
|
||||
if(mkdir(__s("%s%s%s%s",__plugin__.htdocs,rpath,DIR_SEP,dname), 0755))
|
||||
{
|
||||
__t(c,__RESULT__,0,"Error when create directory.");
|
||||
return;
|
||||
}
|
||||
__t(c,__RESULT__,1,"OK");
|
||||
}
|
||||
|
||||
void rmfolder(void* c, const char* m, dictionary rq)
|
||||
{
|
||||
json(c);
|
||||
if(IS_GET(m))
|
||||
{
|
||||
__t(c,__RESULT__,0,"Bad request:GET");
|
||||
return;
|
||||
}
|
||||
char* name = R_STR(rq,"name");
|
||||
char* rpath = R_STR(rq, "path");
|
||||
if(!name)
|
||||
{
|
||||
__t(c,__RESULT__,0,"Folder name is empty");
|
||||
return;
|
||||
}
|
||||
if(!rpath)
|
||||
{
|
||||
__t(c,__RESULT__,0,"Unknow path");
|
||||
return;
|
||||
}
|
||||
removeAll(__s("%s%s%s%s",__plugin__.htdocs,rpath,DIR_SEP,name),1);
|
||||
//LOG("%s\n",name );
|
||||
//LOG("%s\n",rpath );
|
||||
__t(c,__RESULT__,1,"OK");
|
||||
}
|
||||
void pexit()
|
||||
{
|
||||
LOG("Exit file manager,plugins\n");
|
||||
}
|
||||
void handler(void* client, const char* method, const char* rqpth, dictionary rq)
|
||||
{
|
||||
if(EQU(rqpth,"default"))
|
||||
{
|
||||
execute(client,method,rq);
|
||||
}
|
||||
else if(EQU(rqpth,"add"))
|
||||
{
|
||||
add(client,method,rq);
|
||||
}
|
||||
else if(EQU(rqpth,"rmfolder"))
|
||||
{
|
||||
rmfolder(client,method,rq);
|
||||
}
|
||||
else if(EQU(rqpth,"mkfolder"))
|
||||
{
|
||||
mkfolder(client,method,rq);
|
||||
}
|
||||
else
|
||||
{
|
||||
unknow(client);
|
||||
}
|
||||
}
|
@ -1,44 +0,0 @@
|
||||
#include "../plugin.h"
|
||||
|
||||
#define TEXT "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
|
||||
void pexit()
|
||||
{
|
||||
|
||||
}
|
||||
void handler(void* cl, const char* m, const char* rqp, dictionary rq)
|
||||
{
|
||||
//html(cl);
|
||||
ws_msg_header_t* h = NULL;
|
||||
if(ws_enable(rq))
|
||||
{
|
||||
printf("Doc: %s\n","Websocket is available" );
|
||||
while(1)
|
||||
{
|
||||
h = ws_read_header(cl);
|
||||
if(h)
|
||||
{
|
||||
if(h->opcode == WS_CLOSE)
|
||||
{
|
||||
printf("WARNING: Connection close\n");
|
||||
break;
|
||||
}
|
||||
else if(h->opcode == WS_TEXT)
|
||||
{
|
||||
char buff[1025];
|
||||
int l;
|
||||
while((l = ws_read_data(cl,h,1024,buff)) != -1)
|
||||
{
|
||||
buff[l] = '\0';
|
||||
printf("Received:%d '%s'\n", l, buff);
|
||||
}
|
||||
ws_t(cl, TEXT);
|
||||
ws_t(cl, "test");
|
||||
}
|
||||
|
||||
free(h);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
printf("Child process exit\n");
|
||||
}
|
Loading…
Reference in New Issue
Block a user