mirror of
https://github.com/lxsang/ant-http
synced 2024-12-26 00:38:21 +01:00
add plugins
This commit is contained in:
parent
af964b1896
commit
6f44fa9b58
32
plugins/cookiex/cookiex.c
Normal file
32
plugins/cookiex/cookiex.c
Normal file
@ -0,0 +1,32 @@
|
||||
#include "../plugin.h"
|
||||
|
||||
void init();
|
||||
call __init__ = init;
|
||||
|
||||
void init()
|
||||
{
|
||||
printf("Finish init\n");
|
||||
}
|
||||
|
||||
void execute(int 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,d);
|
||||
|
||||
LOG("%s",c);
|
||||
__t(client,"<h1>Set the cookie</h1>");
|
||||
freedict(d);
|
||||
}
|
||||
|
||||
void get(int client,const char* method,dictionary rq)
|
||||
{
|
||||
|
||||
|
||||
}
|
BIN
plugins/cookiex/cookiex.c.o
Normal file
BIN
plugins/cookiex/cookiex.c.o
Normal file
Binary file not shown.
BIN
plugins/fileman/.DS_Store
vendored
Normal file
BIN
plugins/fileman/.DS_Store
vendored
Normal file
Binary file not shown.
188
plugins/fileman/fileman.c
Normal file
188
plugins/fileman/fileman.c
Normal file
@ -0,0 +1,188 @@
|
||||
#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->e.value.s);
|
||||
if(flist == NULL)
|
||||
flist = __s(FOLLIST, np->e.value.s,route);
|
||||
else
|
||||
flist = __s("%s,%s", flist,__s(FOLLIST,np->e.value.s,route));
|
||||
}
|
||||
}
|
||||
free(path);
|
||||
//free(route);
|
||||
return flist;
|
||||
}
|
||||
|
||||
void execute(int 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(int 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(int 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(int 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");
|
||||
}
|
BIN
plugins/fileman/fileman.c.o
Normal file
BIN
plugins/fileman/fileman.c.o
Normal file
Binary file not shown.
BIN
plugins/pluginsman/.DS_Store
vendored
Normal file
BIN
plugins/pluginsman/.DS_Store
vendored
Normal file
Binary file not shown.
76
plugins/pluginsman/pluginsman.c
Normal file
76
plugins/pluginsman/pluginsman.c
Normal file
@ -0,0 +1,76 @@
|
||||
#include "../plugin.h"
|
||||
#define PEXT "dylib"
|
||||
#define MAXSIZE 500000
|
||||
|
||||
void execute(int client,const char* method,dictionary rq)
|
||||
{
|
||||
//all plugin file
|
||||
DIR *d;
|
||||
struct dirent *dir;
|
||||
struct stat st;
|
||||
d = opendir(__plugin__.pdir);
|
||||
json(client);
|
||||
__t(client,"{ \"records\":[");
|
||||
int nrec = 0;
|
||||
if (d)
|
||||
{
|
||||
while ((dir = readdir(d)) != NULL)
|
||||
{
|
||||
if(strcmp(dir->d_name,".") == 0 ||
|
||||
strcmp(dir->d_name,"..")==0 || *(dir->d_name)=='.') continue;
|
||||
if( stat(__s("%s%s",__plugin__.pdir,dir->d_name), &st) == 0 )
|
||||
{
|
||||
if(nrec != 0)
|
||||
__t(client,",");
|
||||
__t(client,"{\"name\":\"%s\",\"size\":%d,\"changed\":\"%s\"}",
|
||||
dir->d_name,
|
||||
(int)st.st_size,
|
||||
__time(st.st_mtime)
|
||||
);
|
||||
nrec++;
|
||||
}
|
||||
}
|
||||
closedir(d);
|
||||
}
|
||||
__t(client,"], \"total\":%d}",nrec);
|
||||
|
||||
}
|
||||
|
||||
void install(int c, const char* m, dictionary rq)
|
||||
{
|
||||
char * result = "{\"result\":%d,\"msg\":\"%s\"}";
|
||||
json(c);
|
||||
if(IS_GET(m))
|
||||
{
|
||||
__t(c,result,0,"Bad request:GET");
|
||||
return;
|
||||
}
|
||||
if(R_INT(rq,"test") != NULL)
|
||||
LOG("Test is :%d \n",R_INT(rq,"test"));
|
||||
char * file_name = R_STR(rq,"pfile.file");
|
||||
char* file_ext = R_STR(rq,"pfile.ext");
|
||||
if(file_name == NULL)
|
||||
{
|
||||
__t(c,result,0,"Cannot send file to server");
|
||||
return;
|
||||
}
|
||||
|
||||
if(strcasecmp(file_ext,PEXT) == 0)
|
||||
{
|
||||
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",__plugin__.pdir,file_name)))
|
||||
{
|
||||
__t(c,result,0,"Cannot move file to plugin dir");
|
||||
return;
|
||||
}
|
||||
__t(c,result,1,"OK");
|
||||
return;
|
||||
}
|
||||
|
||||
__t(c,result,0,"This is not a plugin file");
|
||||
}
|
BIN
plugins/pluginsman/pluginsman.c.o
Normal file
BIN
plugins/pluginsman/pluginsman.c.o
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user