2015-10-22 11:39:11 +02:00
|
|
|
#include "plugin.h"
|
|
|
|
|
|
|
|
plugin_header __plugin__;
|
|
|
|
// private function
|
2016-03-04 11:38:08 +01:00
|
|
|
call __init__;
|
2015-10-22 11:39:11 +02:00
|
|
|
|
2016-03-04 11:38:08 +01:00
|
|
|
void __init_plugin__(const char* pl,const char*ph,const char* htdocs, const char* pdir,int port){
|
2015-10-22 11:39:11 +02:00
|
|
|
__plugin__.name = strdup(pl);
|
|
|
|
__plugin__.dbpath= strdup(ph);
|
|
|
|
__plugin__.htdocs = strdup(htdocs);
|
|
|
|
__plugin__.pdir = strdup(pdir);
|
2016-03-04 11:38:08 +01:00
|
|
|
__plugin__.sport = port;
|
2015-10-22 11:39:11 +02:00
|
|
|
if(__init__ != NULL) __init__();
|
|
|
|
};
|
2016-03-04 11:38:08 +01:00
|
|
|
|
2016-02-26 14:32:13 +01:00
|
|
|
#ifdef USE_DB
|
2016-10-30 16:14:04 +01:00
|
|
|
sqldb __getdb(char *name)
|
2015-10-22 11:39:11 +02:00
|
|
|
{
|
2016-10-30 16:14:04 +01:00
|
|
|
int plen = strlen(name)+strlen(__plugin__.dbpath)+4;
|
2015-10-22 11:39:11 +02:00
|
|
|
char* path = (char*) malloc(plen*sizeof(char));
|
|
|
|
strcpy(path,__plugin__.dbpath);
|
|
|
|
strcat(path,__plugin__.name);
|
|
|
|
strcat(path,".db");
|
|
|
|
sqldb ret = (sqldb)database(path);
|
|
|
|
free(path);
|
|
|
|
return ret;
|
|
|
|
}
|
2016-10-30 16:14:04 +01:00
|
|
|
sqldb getdb()
|
|
|
|
{
|
|
|
|
return getdb(__plugin__.name);
|
|
|
|
}
|
2016-02-26 14:32:13 +01:00
|
|
|
#endif
|
2015-10-22 11:39:11 +02:00
|
|
|
void header_base(int client)
|
|
|
|
{
|
|
|
|
|
|
|
|
response(client, "HTTP/1.0 200 OK");
|
2016-02-29 22:48:22 +01:00
|
|
|
response(client, __s("Server: %s ", SERVER_NAME));
|
2015-10-22 11:39:11 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
void redirect(int client,const char*path)
|
|
|
|
{
|
|
|
|
__t(client,"<html><head><meta http-equiv=\"refresh\" content=\"0; url=%s\"></head><body></body></html>",path);
|
|
|
|
}
|
|
|
|
|
|
|
|
void html(int client)
|
|
|
|
{
|
|
|
|
header(client,"text/html; charset=utf-8");
|
|
|
|
}
|
|
|
|
void text(int client)
|
|
|
|
{
|
|
|
|
header(client,"text/plain; charset=utf-8");
|
|
|
|
}
|
|
|
|
void json(int client)
|
|
|
|
{
|
|
|
|
header(client,"application/json");
|
|
|
|
}
|
2015-11-24 17:58:32 +01:00
|
|
|
void textstream(int client)
|
|
|
|
{
|
|
|
|
header(client, "text/event-stream");
|
|
|
|
}
|
|
|
|
void octstream(int client, char* name)
|
|
|
|
{
|
|
|
|
header_base(client);
|
|
|
|
__t(client,"Content-Type: application/octet-stream");
|
|
|
|
__t(client,"Content-Disposition: attachment; filename=\"%s\"", name);
|
|
|
|
response(client,"");
|
|
|
|
//Content-Disposition: attachment; filename="fname.ext"
|
|
|
|
}
|
2015-10-22 11:39:11 +02:00
|
|
|
void jpeg(int client)
|
|
|
|
{
|
|
|
|
header(client,"image/jpeg");
|
|
|
|
}
|
|
|
|
void header(int client, const char* type)
|
|
|
|
{
|
|
|
|
header_base(client);
|
|
|
|
__t(client,"Content-Type: %s",type);
|
|
|
|
response(client,"");
|
|
|
|
}
|
|
|
|
|
2015-11-24 17:58:32 +01:00
|
|
|
int response(int client, const char* data)
|
2015-10-22 11:39:11 +02:00
|
|
|
{
|
|
|
|
char buf[BUFFLEN+3];
|
|
|
|
strcpy(buf, data);
|
2015-11-24 17:58:32 +01:00
|
|
|
int nbytes;
|
2015-10-22 11:39:11 +02:00
|
|
|
int size = strlen(data);
|
|
|
|
buf[size] = '\r';
|
|
|
|
buf[size+1] = '\n';
|
|
|
|
buf[size+2] = '\0';
|
2015-11-24 17:58:32 +01:00
|
|
|
nbytes = send(client, buf, strlen(buf), 0);
|
|
|
|
return (nbytes ==-1?0:1);
|
2015-10-22 11:39:11 +02:00
|
|
|
}
|
2015-11-24 17:58:32 +01:00
|
|
|
int __ti(int client,int data)
|
2015-10-22 11:39:11 +02:00
|
|
|
{
|
|
|
|
char str[15];
|
|
|
|
sprintf(str, "%d", data);
|
2015-11-24 17:58:32 +01:00
|
|
|
return response(client,str);
|
2015-10-22 11:39:11 +02:00
|
|
|
}
|
|
|
|
|
2015-11-24 17:58:32 +01:00
|
|
|
int __t(int client, const char* fstring,...)
|
2015-10-22 11:39:11 +02:00
|
|
|
{
|
2015-11-24 17:58:32 +01:00
|
|
|
int nbytes;
|
2015-10-22 11:39:11 +02:00
|
|
|
int dlen;
|
|
|
|
int sent = 0;
|
|
|
|
int buflen = 0;
|
|
|
|
va_list arguments;
|
|
|
|
char * data;
|
|
|
|
char* chunk;
|
|
|
|
va_start( arguments, fstring);
|
|
|
|
dlen = vsnprintf(0,0,fstring,arguments) + 1;
|
|
|
|
va_end(arguments);
|
|
|
|
if ((data = (char*)malloc(dlen*sizeof(char))) != 0)
|
|
|
|
{
|
|
|
|
va_start(arguments, fstring);
|
|
|
|
vsnprintf(data, dlen, fstring, arguments);
|
|
|
|
va_end(arguments);
|
|
|
|
|
|
|
|
if(dlen < BUFFLEN)
|
2015-11-24 17:58:32 +01:00
|
|
|
return response(client,data);
|
2015-10-22 11:39:11 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
while(sent < dlen - 1)
|
|
|
|
{
|
|
|
|
if(dlen - sent > BUFFLEN)
|
|
|
|
buflen = BUFFLEN;
|
|
|
|
else
|
|
|
|
buflen = dlen - sent - 1;
|
|
|
|
//LOG("BUFFLEN %d\n",buflen);
|
|
|
|
chunk = (char*) malloc((buflen)*sizeof(char));
|
|
|
|
memcpy(chunk,data+sent,buflen);
|
|
|
|
//chunk[buflen-1] = '\0';
|
|
|
|
//response(client,chunk);
|
|
|
|
sent += buflen;
|
2015-11-24 17:58:32 +01:00
|
|
|
nbytes = send(client, chunk, buflen, 0);
|
2015-10-22 11:39:11 +02:00
|
|
|
free(chunk);
|
2015-11-24 17:58:32 +01:00
|
|
|
if(nbytes == -1) return 0;
|
2015-10-22 11:39:11 +02:00
|
|
|
}
|
|
|
|
chunk = "\r\n";
|
|
|
|
send(client, chunk, strlen(chunk), 0);
|
|
|
|
}
|
|
|
|
free(data);
|
|
|
|
}
|
2015-11-24 17:58:32 +01:00
|
|
|
return 1;
|
2015-10-22 11:39:11 +02:00
|
|
|
//
|
|
|
|
}
|
2015-11-24 17:58:32 +01:00
|
|
|
int __b(int client, const unsigned char* data, int size)
|
2015-10-22 11:39:11 +02:00
|
|
|
{
|
|
|
|
char buf[BUFFLEN];
|
|
|
|
int sent = 0;
|
|
|
|
int buflen = 0;
|
2015-11-24 17:58:32 +01:00
|
|
|
int nbytes;
|
2015-10-22 11:39:11 +02:00
|
|
|
if(size <= BUFFLEN)
|
2015-11-24 17:58:32 +01:00
|
|
|
{
|
|
|
|
nbytes = send(client,data,size,0);
|
|
|
|
return (nbytes==-1?0:1);
|
|
|
|
}
|
2015-10-22 11:39:11 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
while(sent < size)
|
|
|
|
{
|
|
|
|
if(size - sent > BUFFLEN)
|
|
|
|
buflen = BUFFLEN;
|
|
|
|
else
|
|
|
|
buflen = size - sent;
|
|
|
|
memcpy(buf,data+sent,buflen);
|
2015-11-24 17:58:32 +01:00
|
|
|
nbytes = send(client,buf,buflen,0);
|
2015-10-22 11:39:11 +02:00
|
|
|
sent += buflen;
|
2015-11-24 17:58:32 +01:00
|
|
|
if(nbytes == -1) return 0;
|
2015-10-22 11:39:11 +02:00
|
|
|
}
|
|
|
|
}
|
2015-11-24 17:58:32 +01:00
|
|
|
return 1;
|
2015-10-22 11:39:11 +02:00
|
|
|
}
|
2015-11-24 17:58:32 +01:00
|
|
|
int __fb(int client, const char* file)
|
2015-10-22 11:39:11 +02:00
|
|
|
{
|
|
|
|
printf("Open file %s\n",file );
|
|
|
|
unsigned char buffer[BUFFLEN];
|
|
|
|
FILE *ptr;
|
|
|
|
ptr = fopen(file,"rb");
|
|
|
|
if(!ptr)
|
|
|
|
{
|
|
|
|
LOG("Cannot read : %s\n", file);
|
2015-11-24 17:58:32 +01:00
|
|
|
return 0;
|
2015-10-22 11:39:11 +02:00
|
|
|
}
|
2015-11-24 17:58:32 +01:00
|
|
|
size_t size;
|
2015-10-22 11:39:11 +02:00
|
|
|
while(!feof(ptr))
|
|
|
|
{
|
2015-11-24 17:58:32 +01:00
|
|
|
size = fread(buffer,1,BUFFLEN,ptr);
|
|
|
|
if(!__b(client,buffer,size)) return 0;
|
2015-10-22 11:39:11 +02:00
|
|
|
}
|
|
|
|
fclose(ptr);
|
2015-11-24 17:58:32 +01:00
|
|
|
return 1;
|
2015-10-22 11:39:11 +02:00
|
|
|
}
|
2015-11-24 17:58:32 +01:00
|
|
|
int __f(int client, const char* file)
|
2015-10-22 11:39:11 +02:00
|
|
|
{
|
|
|
|
unsigned char buf[BUFFLEN];
|
|
|
|
FILE *ptr;
|
2015-11-24 17:58:32 +01:00
|
|
|
int nbytes;
|
2015-10-22 11:39:11 +02:00
|
|
|
ptr = fopen(file,"r");
|
|
|
|
if(!ptr)
|
|
|
|
{
|
|
|
|
LOG("Cannot read : %s\n", file);
|
2015-11-24 17:58:32 +01:00
|
|
|
return 0;
|
2015-10-22 11:39:11 +02:00
|
|
|
}
|
|
|
|
fgets(buf, sizeof(buf), ptr);
|
|
|
|
while(!feof(ptr))
|
|
|
|
{
|
2015-11-24 17:58:32 +01:00
|
|
|
nbytes = send(client, buf, strlen(buf), 0);
|
|
|
|
if(nbytes == -1) return 0;
|
2015-10-22 11:39:11 +02:00
|
|
|
fgets(buf, sizeof(buf), ptr);
|
|
|
|
}
|
|
|
|
fclose(ptr);
|
2015-11-24 17:58:32 +01:00
|
|
|
return 1;
|
2015-10-22 11:39:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
char* route(const char* repath)
|
|
|
|
{
|
|
|
|
int len = strlen(__plugin__.name) + 2;
|
|
|
|
if(repath != NULL)
|
|
|
|
len += strlen(repath)+1;
|
|
|
|
char * path = (char*) malloc(len*sizeof(char));
|
|
|
|
strcpy(path,"/");
|
|
|
|
strcat(path,__plugin__.name);
|
|
|
|
if(repath != NULL)
|
|
|
|
{
|
|
|
|
strcat(path,"/");
|
|
|
|
strcat(path,repath);
|
|
|
|
}
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
char* htdocs(const char* repath)
|
|
|
|
{
|
|
|
|
if(repath != NULL)
|
|
|
|
return __s("%s/%s",__plugin__.htdocs,repath);
|
|
|
|
else
|
|
|
|
return __s("%s",__plugin__.htdocs);
|
|
|
|
}
|
|
|
|
int upload(const char* tmp, const char* path)
|
|
|
|
{
|
|
|
|
return !rename(tmp, path);
|
|
|
|
}
|
2016-11-01 01:20:23 +01:00
|
|
|
void set_cookie(int client,const char* type, dictionary dic)
|
2015-10-22 11:39:11 +02:00
|
|
|
{
|
|
|
|
header_base(client);
|
2016-11-01 01:20:23 +01:00
|
|
|
__t(client,"Content-Type: %s",type);
|
2015-10-22 11:39:11 +02:00
|
|
|
association assoc;
|
|
|
|
for_each_assoc(assoc,dic){
|
2016-11-01 13:19:56 +01:00
|
|
|
__t(client,"Set-Cookie: %s=%s; Path=/%s",assoc->key, (char*)assoc->value, __plugin__.name);
|
2015-10-22 11:39:11 +02:00
|
|
|
}
|
|
|
|
response(client,"");
|
2016-03-04 11:38:08 +01:00
|
|
|
}
|
2016-11-01 01:20:23 +01:00
|
|
|
void clear_cookie(int client, dictionary dic)
|
2016-10-30 16:14:04 +01:00
|
|
|
{
|
|
|
|
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,"");
|
|
|
|
}
|
2016-03-04 11:38:08 +01:00
|
|
|
char* config_dir()
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
char* path = __s("%s%s%s", __plugin__.pdir,DIR_SEP, __plugin__.name);
|
|
|
|
if (stat(path, &st) == -1)
|
|
|
|
mkdir(path, 0755);
|
|
|
|
return path;
|
2016-10-29 15:02:16 +02:00
|
|
|
}
|
|
|
|
void unknow(int client)
|
|
|
|
{
|
|
|
|
html(client);
|
|
|
|
__t(client,"404 API not found");
|
2016-11-12 17:39:11 +01:00
|
|
|
}
|
|
|
|
int ws_enable(dictionary dic)
|
|
|
|
{
|
|
|
|
return (dic != NULL && R_INT(dic,"__web_socket__") == 1);
|
2015-10-22 11:39:11 +02:00
|
|
|
}
|