2015-10-22 11:39:11 +02:00
|
|
|
#include "http_server.h"
|
2018-03-14 12:41:51 +01:00
|
|
|
static pthread_mutex_t server_mux = PTHREAD_MUTEX_INITIALIZER;
|
2018-10-05 19:01:39 +02:00
|
|
|
config_t server_config;
|
|
|
|
config_t* config()
|
|
|
|
{
|
|
|
|
return &server_config;
|
|
|
|
}
|
|
|
|
|
|
|
|
void destroy_config()
|
|
|
|
{
|
|
|
|
list_free(&(server_config.rules));
|
|
|
|
freedict(server_config.handlers);
|
|
|
|
if(server_config.plugins_dir) free(server_config.plugins_dir);
|
|
|
|
if(server_config.plugins_ext) free(server_config.plugins_ext);
|
|
|
|
if(server_config.db_path) free(server_config.db_path);
|
|
|
|
if(server_config.htdocs) free(server_config.htdocs);
|
|
|
|
if(server_config.tmpdir) free(server_config.tmpdir);
|
|
|
|
|
|
|
|
LOG("Unclosed connection: %d\n", server_config.connection);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int config_handler(void* conf, const char* section, const char* name,
|
|
|
|
const char* value)
|
|
|
|
{
|
|
|
|
config_t* pconfig = (config_t*)conf;
|
|
|
|
//char * ppath = NULL;
|
|
|
|
if (MATCH("SERVER", "port")) {
|
|
|
|
pconfig->port = atoi(value);
|
|
|
|
} else if (MATCH("SERVER", "plugins")) {
|
|
|
|
pconfig->plugins_dir = strdup(value);
|
|
|
|
} else if (MATCH("SERVER", "plugins_ext")) {
|
|
|
|
pconfig->plugins_ext = strdup(value);
|
|
|
|
} else if(MATCH("SERVER", "database")) {
|
|
|
|
pconfig->db_path = strdup(value);
|
|
|
|
} else if(MATCH("SERVER", "htdocs")) {
|
|
|
|
pconfig->htdocs = strdup(value);
|
|
|
|
} else if(MATCH("SERVER", "tmpdir")) {
|
|
|
|
pconfig->tmpdir = strdup(value);
|
|
|
|
}
|
|
|
|
else if(MATCH("SERVER", "maxcon")) {
|
|
|
|
pconfig->maxcon = atoi(value);
|
|
|
|
}
|
|
|
|
else if(MATCH("SERVER", "backlog")) {
|
|
|
|
pconfig->backlog = atoi(value);
|
|
|
|
}
|
|
|
|
else if(MATCH("SERVER", "workers")) {
|
|
|
|
pconfig->n_workers = atoi(value);
|
|
|
|
}
|
|
|
|
#ifdef USE_OPENSSL
|
|
|
|
else if(MATCH("SERVER", "ssl.enable")) {
|
|
|
|
pconfig->usessl = atoi(value);
|
|
|
|
}
|
|
|
|
else if(MATCH("SERVER", "ssl.cert")) {
|
|
|
|
pconfig->sslcert = strdup(value);
|
|
|
|
}
|
|
|
|
else if(MATCH("SERVER", "ssl.key")) {
|
|
|
|
pconfig->sslkey = strdup(value);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
else if (strcmp(section, "RULES") == 0)
|
|
|
|
{
|
|
|
|
list_put_s(&pconfig->rules, name);
|
|
|
|
list_put_s(&pconfig->rules, value);
|
|
|
|
}
|
|
|
|
else if (strcmp(section, "FILEHANDLER") == 0)
|
|
|
|
{
|
|
|
|
dput( pconfig->handlers, name ,strdup(value));
|
|
|
|
}
|
|
|
|
else if(strcmp(section,"AUTOSTART")==0){
|
|
|
|
// The server section must be added before the autostart section
|
|
|
|
// auto start plugin
|
|
|
|
plugin_load(value);
|
|
|
|
} else {
|
|
|
|
return 0; /* unknown section/name, error */
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
void init_file_system()
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
if (stat(server_config.plugins_dir, &st) == -1)
|
|
|
|
mkdir(server_config.plugins_dir, 0755);
|
|
|
|
if (stat(server_config.db_path, &st) == -1)
|
|
|
|
mkdir(server_config.db_path, 0755);
|
|
|
|
if (stat(server_config.htdocs, &st) == -1)
|
|
|
|
mkdir(server_config.htdocs, 0755);
|
|
|
|
if (stat(server_config.tmpdir, &st) == -1)
|
|
|
|
mkdir(server_config.tmpdir, 0755);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
removeAll(server_config.tmpdir,0);
|
|
|
|
}
|
2015-10-22 11:39:11 +02:00
|
|
|
|
2018-10-05 19:01:39 +02:00
|
|
|
}
|
|
|
|
void load_config(const char* file)
|
|
|
|
{
|
|
|
|
server_config.port = 8888;
|
|
|
|
server_config.plugins_dir = "plugins/";
|
|
|
|
server_config.plugins_ext = ".dylib";
|
|
|
|
server_config.db_path = "databases/";
|
|
|
|
server_config.htdocs = "htdocs/";
|
|
|
|
server_config.tmpdir = "tmp/";
|
|
|
|
server_config.n_workers = 4;
|
|
|
|
server_config.backlog = 100;
|
|
|
|
server_config.rules = list_init();
|
|
|
|
server_config.handlers = dict();
|
|
|
|
server_config.maxcon = 1000;
|
|
|
|
server_config.connection = 0;
|
|
|
|
#ifdef USE_OPENSSL
|
|
|
|
server_config.usessl = 0;
|
|
|
|
server_config.sslcert = "cert.pem";
|
|
|
|
server_config.sslkey = "key.pem";
|
|
|
|
#endif
|
|
|
|
if (ini_parse(file, config_handler, &server_config) < 0) {
|
|
|
|
LOG("Can't load '%s'\n. Used defaut configuration", file);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOG("Using configuration : %s\n", file);
|
|
|
|
#ifdef USE_OPENSSL
|
|
|
|
LOG("SSL enable %d\n", server_config.usessl);
|
|
|
|
LOG("SSL cert %s\n", server_config.sslcert);
|
|
|
|
LOG("SSL key %s\n", server_config.sslkey);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
init_file_system();
|
|
|
|
}
|
|
|
|
void set_nonblock(int socket) {
|
|
|
|
int flags;
|
|
|
|
flags = fcntl(socket,F_GETFL,0);
|
|
|
|
//assert(flags != -1);
|
|
|
|
fcntl(socket, F_SETFL, flags | O_NONBLOCK);
|
|
|
|
}
|
|
|
|
|
|
|
|
void* accept_request(void* data)
|
2018-10-04 19:47:31 +02:00
|
|
|
{
|
|
|
|
int count;
|
|
|
|
char buf[BUFFLEN];
|
|
|
|
char* token = NULL;
|
|
|
|
char* line = NULL;
|
2018-10-05 19:01:39 +02:00
|
|
|
antd_task_t* task;
|
|
|
|
antd_request_t* rq = (antd_request_t*) data;
|
|
|
|
|
|
|
|
task = antd_create_task(NULL,(void*)rq,NULL);
|
2018-10-04 19:47:31 +02:00
|
|
|
task->priority++;
|
2018-10-05 19:01:39 +02:00
|
|
|
server_config.connection++;
|
|
|
|
fd_set read_flags;
|
|
|
|
// first verify if the socket is ready
|
|
|
|
antd_client_t* client = (antd_client_t*) rq->client;
|
|
|
|
FD_ZERO(&read_flags);
|
|
|
|
FD_SET(rq->client->sock, &read_flags);
|
|
|
|
struct timeval timeout;
|
|
|
|
timeout.tv_sec = 0;
|
|
|
|
timeout.tv_usec = 500;
|
|
|
|
// select
|
|
|
|
int sel = select(client->sock+1, &read_flags, NULL, (fd_set*)0, &timeout);
|
|
|
|
if(sel == -1)
|
|
|
|
{
|
|
|
|
unknow(rq->client);
|
|
|
|
return task;
|
|
|
|
}
|
|
|
|
if(sel == 0 || !FD_ISSET(client->sock, &read_flags) )
|
2018-03-14 11:55:15 +01:00
|
|
|
{
|
2018-10-05 19:01:39 +02:00
|
|
|
// retry it later
|
|
|
|
server_config.connection--;
|
|
|
|
task->handle = accept_request;
|
2018-10-04 19:47:31 +02:00
|
|
|
return task;
|
2018-03-14 11:55:15 +01:00
|
|
|
}
|
2018-10-05 19:01:39 +02:00
|
|
|
count = read_buf(rq->client, buf, sizeof(buf));
|
|
|
|
//LOG("count is %d\n", count);
|
2018-10-04 19:47:31 +02:00
|
|
|
line = buf;
|
|
|
|
// get the method string
|
|
|
|
token = strsep(&line," ");
|
|
|
|
if(!line)
|
2015-10-22 11:39:11 +02:00
|
|
|
{
|
2018-10-05 19:01:39 +02:00
|
|
|
LOG("No method found\n");
|
|
|
|
unknow(rq->client);
|
2018-10-04 19:47:31 +02:00
|
|
|
return task;
|
2015-10-22 11:39:11 +02:00
|
|
|
}
|
2018-10-04 19:47:31 +02:00
|
|
|
trim(token,' ');
|
|
|
|
trim(line,' ');
|
2018-10-05 19:01:39 +02:00
|
|
|
dput(rq->request, "METHOD", strdup(token));
|
2018-10-04 19:47:31 +02:00
|
|
|
// get the request
|
|
|
|
token = strsep(&line, " ");
|
|
|
|
if(!line)
|
2015-10-22 11:39:11 +02:00
|
|
|
{
|
2018-10-05 19:01:39 +02:00
|
|
|
LOG("No request found\n");
|
|
|
|
unknow(rq->client);
|
2018-10-04 19:47:31 +02:00
|
|
|
return task;
|
2015-10-22 11:39:11 +02:00
|
|
|
}
|
2018-10-04 19:47:31 +02:00
|
|
|
trim(token,' ');
|
|
|
|
trim(line,' ');
|
|
|
|
trim(line, '\n');
|
|
|
|
trim(line, '\r');
|
2018-10-05 19:01:39 +02:00
|
|
|
dput(rq->request, "PROTOCOL", strdup(line));
|
|
|
|
dput(rq->request, "REQUEST_QUERY", strdup(token));
|
2018-10-04 19:47:31 +02:00
|
|
|
line = token;
|
|
|
|
token = strsep(&line, "?");
|
2018-10-05 19:01:39 +02:00
|
|
|
dput(rq->request, "REQUEST_PATH", strdup(token));
|
2018-10-04 19:47:31 +02:00
|
|
|
// decode request
|
|
|
|
// now return the task
|
|
|
|
task->handle = decode_request_header;
|
|
|
|
return task;
|
|
|
|
}
|
2015-10-22 11:39:11 +02:00
|
|
|
|
2018-10-04 19:47:31 +02:00
|
|
|
void* resolve_request(void* data)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
char path[2*BUFFLEN];
|
|
|
|
antd_request_t* rq = (antd_request_t*) data;
|
|
|
|
antd_task_t* task = antd_create_task(NULL,(void*)rq,NULL);
|
|
|
|
task->priority++;
|
|
|
|
char* url = (char*)dvalue(rq->request, "RESOURCE_PATH");
|
|
|
|
char* newurl = NULL;
|
|
|
|
char* rqp = (char*)dvalue(rq->request, "REQUEST_PATH");
|
2015-10-22 11:39:11 +02:00
|
|
|
sprintf(path, server_config.htdocs);
|
|
|
|
strcat(path, url);
|
2018-02-05 19:06:03 +01:00
|
|
|
LOG("Path is : %s \n", path);
|
2018-02-03 18:50:07 +01:00
|
|
|
//if (path[strlen(path) - 1] == '/')
|
|
|
|
// strcat(path, "index.html");
|
2015-10-22 11:39:11 +02:00
|
|
|
if (stat(path, &st) == -1) {
|
2018-10-04 19:47:31 +02:00
|
|
|
free(task);
|
|
|
|
return execute_plugin(rq, rqp);
|
2015-10-22 11:39:11 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (S_ISDIR(st.st_mode))
|
2018-02-05 11:42:01 +01:00
|
|
|
{
|
2015-10-22 11:39:11 +02:00
|
|
|
strcat(path, "/index.html");
|
2018-02-05 11:42:01 +01:00
|
|
|
if(stat(path, &st) == -1)
|
|
|
|
{
|
2018-02-20 19:02:31 +01:00
|
|
|
association it;
|
|
|
|
for_each_assoc(it, server_config.handlers)
|
|
|
|
{
|
2018-10-04 19:47:31 +02:00
|
|
|
newurl = __s("%s/index.%s", url, it->key);
|
|
|
|
memset(path, 0, sizeof(path));
|
|
|
|
strcat(path, server_config.htdocs);
|
|
|
|
strcat(path, newurl);
|
|
|
|
if(stat(path, &st) != 0)
|
|
|
|
{
|
|
|
|
free(newurl);
|
|
|
|
newurl = NULL;
|
|
|
|
}
|
|
|
|
else
|
2018-02-20 19:02:31 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-10-04 19:47:31 +02:00
|
|
|
if(!newurl)
|
2018-02-20 19:02:31 +01:00
|
|
|
{
|
2018-10-04 19:47:31 +02:00
|
|
|
notfound(rq->client);
|
|
|
|
return task;
|
2018-02-20 19:02:31 +01:00
|
|
|
}
|
2018-10-06 01:30:38 +02:00
|
|
|
//if(url) free(url); this is freed in the dput function
|
2018-10-04 19:47:31 +02:00
|
|
|
url = newurl;
|
|
|
|
dput(rq->request, "RESOURCE_PATH", url);
|
2018-02-05 11:42:01 +01:00
|
|
|
}
|
|
|
|
}
|
2018-10-04 19:47:31 +02:00
|
|
|
dput(rq->request, "ABS_RESOURCE_PATH", strdup(path));
|
2016-12-07 10:57:14 +01:00
|
|
|
// check if the mime is supported
|
2018-02-04 19:46:47 +01:00
|
|
|
// if the mime is not supported
|
2016-12-07 10:57:14 +01:00
|
|
|
// find an handler plugin to process it
|
|
|
|
// if the plugin is not found, forbidden access to the file should be sent
|
|
|
|
char* mime_type = mime(path);
|
2018-10-04 19:47:31 +02:00
|
|
|
dput(rq->request, "RESOURCE_MIME", strdup(mime_type));
|
2016-12-07 10:57:14 +01:00
|
|
|
if(strcmp(mime_type,"application/octet-stream") == 0)
|
|
|
|
{
|
2018-03-14 11:55:15 +01:00
|
|
|
char * ex = ext(path);
|
|
|
|
char* h = dvalue(server_config.handlers,ex);
|
|
|
|
if(ex) free(ex);
|
2018-02-20 19:02:31 +01:00
|
|
|
if(h)
|
|
|
|
{
|
2018-10-04 19:47:31 +02:00
|
|
|
sprintf(path,"/%s%s",h,url);
|
|
|
|
LOG("WARNING::::Access octetstream via handler %s\n", path);
|
|
|
|
//if(execute_plugin(client,buf,method,rq) < 0)
|
|
|
|
// cannot_execute(client);
|
|
|
|
free(task);
|
|
|
|
return execute_plugin(rq, path);
|
2018-02-20 19:02:31 +01:00
|
|
|
}
|
|
|
|
else
|
2018-10-04 19:47:31 +02:00
|
|
|
unknow(rq->client);
|
2018-02-20 19:02:31 +01:00
|
|
|
|
2016-12-07 10:57:14 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-10-04 19:47:31 +02:00
|
|
|
task->type = HEAVY;
|
|
|
|
task->handle = serve_file;
|
|
|
|
}
|
|
|
|
return task;
|
2018-03-02 19:04:00 +01:00
|
|
|
}
|
2015-10-22 11:39:11 +02:00
|
|
|
}
|
|
|
|
|
2018-10-03 23:42:42 +02:00
|
|
|
void* finish_request(void* data)
|
|
|
|
{
|
2018-10-04 19:47:31 +02:00
|
|
|
if(!data) return NULL;
|
|
|
|
LOG("Close request\n");
|
|
|
|
antd_request_t* rq = (antd_request_t*)data;
|
|
|
|
// free all other thing
|
2018-10-05 19:01:39 +02:00
|
|
|
if(rq->request)
|
|
|
|
{
|
|
|
|
dictionary tmp = dvalue(rq->request, "COOKIE");
|
|
|
|
if(tmp) freedict(tmp);
|
|
|
|
tmp = dvalue(rq->request, "REQUEST_HEADER");
|
|
|
|
if(tmp) freedict(tmp);
|
|
|
|
tmp = dvalue(rq->request, "REQUEST_DATA");
|
|
|
|
if(tmp) freedict(tmp);
|
|
|
|
dput(rq->request, "REQUEST_HEADER", NULL);
|
|
|
|
dput(rq->request, "REQUEST_DATA", NULL);
|
|
|
|
dput(rq->request, "COOKIE", NULL);
|
|
|
|
freedict(rq->request);
|
|
|
|
}
|
2018-10-04 19:47:31 +02:00
|
|
|
antd_close(rq->client);
|
|
|
|
free(rq);
|
2018-10-05 19:01:39 +02:00
|
|
|
server_config.connection--;
|
|
|
|
LOG("Remaining connection %d\n", server_config.connection);
|
2018-10-03 23:42:42 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-02-23 19:54:16 +01:00
|
|
|
int rule_check(const char*k, const char* v, const char* host, const char* _url, const char* _query, char* buf)
|
2018-02-04 19:46:47 +01:00
|
|
|
{
|
|
|
|
// first perfom rule check on host, if not success, perform on url
|
|
|
|
regmatch_t key_matches[10];
|
|
|
|
regmatch_t val_matches[2];
|
|
|
|
char* query = strdup(_query);
|
|
|
|
char* url = strdup(_url);
|
|
|
|
int ret;
|
|
|
|
char* target;
|
|
|
|
char* tmp, rep[10];
|
|
|
|
int idx = 0;
|
|
|
|
memset(rep,0,10);
|
|
|
|
// 1 group
|
2018-02-23 19:54:16 +01:00
|
|
|
if(!host || !(ret = regex_match(k,host, 10, key_matches)) )
|
2018-02-04 19:46:47 +01:00
|
|
|
{
|
|
|
|
target = url;
|
2018-02-23 19:54:16 +01:00
|
|
|
ret = regex_match(k,url, 10, key_matches);
|
2018-02-04 19:46:47 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
target = host;
|
|
|
|
|
2018-03-02 19:04:00 +01:00
|
|
|
if(!ret)
|
|
|
|
{
|
|
|
|
free(url);
|
|
|
|
free(query);
|
|
|
|
return 0;
|
|
|
|
}
|
2018-02-23 19:54:16 +01:00
|
|
|
tmp = (char*) v;
|
2018-02-05 11:42:01 +01:00
|
|
|
char * search = "<([a-zA-Z0-9]+)>";
|
|
|
|
//printf("match again %s\n",tmp);
|
|
|
|
while((ret = regex_match( search,tmp, 2, val_matches)))
|
2018-02-04 19:46:47 +01:00
|
|
|
{
|
|
|
|
memcpy(buf + idx, tmp, val_matches[1].rm_so - 1);
|
|
|
|
idx += val_matches[1].rm_so - 1;
|
|
|
|
memcpy(rep, tmp + val_matches[1].rm_so, val_matches[1].rm_eo - val_matches[1].rm_so);
|
|
|
|
if(strcasecmp(rep,"url") == 0)
|
|
|
|
{
|
|
|
|
memcpy(buf+idx, url, strlen(url));
|
|
|
|
idx += strlen(url);
|
|
|
|
} else if(strcasecmp(rep,"query") == 0)
|
|
|
|
{
|
|
|
|
memcpy(buf+idx, query, strlen(query));
|
|
|
|
idx += strlen(query);
|
|
|
|
} else if(match_int(rep))
|
|
|
|
{
|
|
|
|
int i = atoi(rep);
|
|
|
|
memcpy(buf+idx, target + key_matches[i].rm_so, key_matches[i].rm_eo - key_matches[i].rm_so);
|
|
|
|
idx += key_matches[i].rm_eo - key_matches[i].rm_so;
|
|
|
|
} else { // just keep it
|
|
|
|
memcpy(buf+idx, tmp + val_matches[1].rm_so-1, val_matches[1].rm_eo + 2 - val_matches[1].rm_so);
|
|
|
|
idx+= val_matches[1].rm_eo + 2 - val_matches[1].rm_so;
|
|
|
|
}
|
|
|
|
tmp += val_matches[1].rm_eo + 1;
|
|
|
|
//break;
|
|
|
|
}
|
|
|
|
// now modify the match 2 group
|
2018-02-23 19:54:16 +01:00
|
|
|
if(idx > 0)
|
|
|
|
{
|
|
|
|
if(tmp)
|
|
|
|
{
|
|
|
|
// copy the remainning of tmp
|
|
|
|
memcpy(buf+idx, tmp, strlen(tmp));
|
|
|
|
idx += strlen(tmp);
|
|
|
|
}
|
|
|
|
buf[idx] = '\0';
|
|
|
|
}
|
2018-02-04 19:46:47 +01:00
|
|
|
free(url);
|
|
|
|
free(query);
|
2018-02-23 19:54:16 +01:00
|
|
|
return 1;
|
2018-02-04 19:46:47 +01:00
|
|
|
}
|
2018-10-05 19:01:39 +02:00
|
|
|
|
|
|
|
static void error_die(const char *sc)
|
2015-10-22 11:39:11 +02:00
|
|
|
{
|
|
|
|
perror(sc);
|
|
|
|
exit(1);
|
|
|
|
}
|
2018-10-04 19:47:31 +02:00
|
|
|
void* serve_file(void* data)
|
2015-10-22 11:39:11 +02:00
|
|
|
{
|
2018-10-04 19:47:31 +02:00
|
|
|
antd_request_t* rq = (antd_request_t*) data;
|
|
|
|
antd_task_t* task = antd_create_task(NULL,(void*)rq,NULL);
|
|
|
|
task->priority++;
|
|
|
|
char* path = (char*)dvalue(rq->request, "ABS_RESOURCE_PATH");
|
|
|
|
char* newurl = NULL;
|
|
|
|
char* mime_type = (char*)dvalue(rq->request, "RESOURCE_MIME");
|
|
|
|
ctype(rq->client,mime_type);
|
|
|
|
if(is_bin(path))
|
|
|
|
__fb(rq->client, path);
|
2015-10-22 11:39:11 +02:00
|
|
|
else
|
2018-10-04 19:47:31 +02:00
|
|
|
__f(rq->client, path);
|
|
|
|
return task;
|
2015-10-22 11:39:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int startup(unsigned *port)
|
|
|
|
{
|
|
|
|
int httpd = 0;
|
|
|
|
struct sockaddr_in name;
|
|
|
|
|
|
|
|
httpd = socket(PF_INET, SOCK_STREAM, 0);
|
|
|
|
if (httpd == -1)
|
|
|
|
error_die("socket");
|
|
|
|
memset(&name, 0, sizeof(name));
|
|
|
|
name.sin_family = AF_INET;
|
|
|
|
name.sin_port = htons(*port);
|
|
|
|
name.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
|
if (bind(httpd, (struct sockaddr *)&name, sizeof(name)) < 0)
|
|
|
|
error_die("bind");
|
|
|
|
if (*port == 0) /* if dynamically allocating a port */
|
|
|
|
{
|
|
|
|
socklen_t namelen = sizeof(name);
|
|
|
|
if (getsockname(httpd, (struct sockaddr *)&name, &namelen) == -1)
|
|
|
|
error_die("getsockname");
|
|
|
|
*port = ntohs(name.sin_port);
|
|
|
|
}
|
2018-02-05 23:04:02 +01:00
|
|
|
printf("back log is %d\n", server_config.backlog);
|
|
|
|
if (listen(httpd, server_config.backlog) < 0)
|
2015-10-22 11:39:11 +02:00
|
|
|
error_die("listen");
|
|
|
|
return(httpd);
|
|
|
|
}
|
|
|
|
|
2018-02-04 19:46:47 +01:00
|
|
|
char* apply_rules(const char* host, char*url)
|
|
|
|
{
|
|
|
|
association it;
|
|
|
|
// rule check
|
|
|
|
char* query_string = url;
|
|
|
|
while ((*query_string != '?') && (*query_string != '\0'))
|
|
|
|
query_string++;
|
|
|
|
if (*query_string == '?')
|
|
|
|
{
|
|
|
|
*query_string = '\0';
|
|
|
|
query_string++;
|
|
|
|
}
|
|
|
|
//char* oldurl = strdup(url);
|
2018-02-23 19:54:16 +01:00
|
|
|
int size = list_size(server_config.rules);
|
|
|
|
for(int i = 0; i < size; i+= 2)
|
2018-02-04 19:46:47 +01:00
|
|
|
{
|
2018-02-23 19:54:16 +01:00
|
|
|
char *k, *v;
|
|
|
|
k = list_at(server_config.rules, i)->value.s;
|
|
|
|
v = list_at(server_config.rules, i+1)->value.s;
|
2018-02-04 19:46:47 +01:00
|
|
|
// 1 group
|
2018-02-23 19:54:16 +01:00
|
|
|
if(rule_check(k, v,host, url, query_string, url)){
|
|
|
|
query_string = url;
|
|
|
|
|
|
|
|
while ((*query_string != '?') && (*query_string != '\0'))
|
|
|
|
query_string++;
|
|
|
|
if (*query_string == '?')
|
|
|
|
{
|
|
|
|
*query_string = '\0';
|
|
|
|
query_string++;
|
|
|
|
}
|
2018-02-04 19:46:47 +01:00
|
|
|
}
|
|
|
|
}
|
2018-02-23 19:54:16 +01:00
|
|
|
|
2018-02-04 19:46:47 +01:00
|
|
|
return strdup(query_string);
|
|
|
|
}
|
2017-07-29 22:00:34 +02:00
|
|
|
/**
|
2018-10-05 19:01:39 +02:00
|
|
|
* Decode the HTTP request header
|
2017-07-29 22:00:34 +02:00
|
|
|
*/
|
2018-10-04 19:47:31 +02:00
|
|
|
|
|
|
|
void* decode_request_header(void* data)
|
2017-07-29 22:00:34 +02:00
|
|
|
{
|
2018-10-04 19:47:31 +02:00
|
|
|
antd_request_t* rq = (antd_request_t*) data;
|
2017-07-29 22:00:34 +02:00
|
|
|
dictionary cookie = NULL;
|
|
|
|
char* line;
|
|
|
|
char * token;
|
2018-02-04 19:46:47 +01:00
|
|
|
char* query = NULL;
|
2018-02-09 01:59:49 +01:00
|
|
|
char* host = NULL;
|
2018-10-04 19:47:31 +02:00
|
|
|
char buf[2*BUFFLEN];
|
|
|
|
char* url = (char*)dvalue(rq->request, "REQUEST_QUERY");
|
|
|
|
dictionary xheader = dict();
|
|
|
|
dictionary request = dict();
|
|
|
|
dput(rq->request,"REQUEST_HEADER",xheader);
|
|
|
|
dput(rq->request,"REQUEST_DATA",request);
|
2018-02-05 11:42:01 +01:00
|
|
|
// first real all header
|
2018-09-12 11:06:19 +02:00
|
|
|
// this for check if web socket is enabled
|
|
|
|
// ip address
|
2018-10-04 19:47:31 +02:00
|
|
|
dput(xheader,"REMOTE_ADDR", (void*)strdup(((antd_client_t*)rq->client)->ip ));
|
2018-02-09 09:21:10 +01:00
|
|
|
//while((line = read_line(client)) && strcmp("\r\n",line))
|
2018-10-04 19:47:31 +02:00
|
|
|
while((read_buf(rq->client,buf,sizeof(buf))) && strcmp("\r\n",buf))
|
2018-02-05 11:42:01 +01:00
|
|
|
{
|
2018-02-09 09:21:10 +01:00
|
|
|
line = buf;
|
2018-02-05 11:42:01 +01:00
|
|
|
trim(line, '\n');
|
|
|
|
trim(line, '\r');
|
|
|
|
token = strsep(&line,":");
|
|
|
|
trim(token,' ');
|
|
|
|
trim(line,' ');
|
2018-03-17 20:54:38 +01:00
|
|
|
if(token && line && strlen(line) > 0)
|
|
|
|
dput(xheader,token,strdup(line));
|
2018-02-05 11:42:01 +01:00
|
|
|
if(token != NULL &&strcasecmp(token,"Cookie") == 0)
|
|
|
|
{
|
|
|
|
if(!cookie) cookie = decode_cookie(line);
|
|
|
|
}
|
2018-10-04 19:47:31 +02:00
|
|
|
else if(token != NULL && strcasecmp(token,"Host") == 0)
|
2018-02-05 11:42:01 +01:00
|
|
|
{
|
2018-02-09 09:21:10 +01:00
|
|
|
host = strdup(line);
|
2018-02-05 11:42:01 +01:00
|
|
|
}
|
|
|
|
}
|
2018-02-09 09:21:10 +01:00
|
|
|
//if(line) free(line);
|
2018-10-04 19:47:31 +02:00
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
strcat(buf,url);
|
|
|
|
query = apply_rules(host, buf);
|
|
|
|
dput(rq->request,"RESOURCE_PATH",strdup(buf));
|
2018-09-05 23:26:21 +02:00
|
|
|
if(query)
|
|
|
|
{
|
|
|
|
LOG("Query: %s\n", query);
|
|
|
|
decode_url_request(query, request);
|
|
|
|
free(query);
|
|
|
|
}
|
2018-10-04 19:47:31 +02:00
|
|
|
if(cookie)
|
2018-10-05 19:01:39 +02:00
|
|
|
dput(rq->request,"COOKIE",cookie);
|
2018-03-02 19:04:00 +01:00
|
|
|
if(host) free(host);
|
2018-10-04 19:47:31 +02:00
|
|
|
// header ok, now checkmethod
|
|
|
|
antd_task_t* task = antd_create_task(decode_request,(void*)rq, NULL);
|
|
|
|
task->priority++;
|
|
|
|
return task;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* decode_request(void* data)
|
|
|
|
{
|
|
|
|
antd_request_t* rq = (antd_request_t*) data;
|
|
|
|
dictionary request = dvalue(rq->request, "REQUEST_DATA");
|
|
|
|
dictionary headers = dvalue(rq->request, "REQUEST_HEADER");
|
|
|
|
int ws = 0;
|
|
|
|
char*ws_key = NULL;
|
|
|
|
char* method = NULL;
|
|
|
|
char* tmp;
|
|
|
|
antd_task_t* task = NULL;
|
|
|
|
ws_key = (char*) dvalue(headers, "Sec-WebSocket-Key");
|
|
|
|
tmp = (char*)dvalue(headers, "Upgrade");
|
|
|
|
if(tmp && strcasecmp(tmp, "websocket") == 0) ws = 1;
|
|
|
|
method = (char*) dvalue(rq->request, "METHOD");
|
|
|
|
task = antd_create_task(NULL,(void*)rq, NULL);
|
|
|
|
task->priority++;
|
|
|
|
if(strcmp(method,"GET") == 0 || strcmp(method,"PUT") == 0)
|
2018-03-02 19:04:00 +01:00
|
|
|
{
|
2018-09-05 23:26:21 +02:00
|
|
|
//if(ctype) free(ctype);
|
2017-07-29 22:00:34 +02:00
|
|
|
if(ws && ws_key != NULL)
|
|
|
|
{
|
2018-10-04 19:47:31 +02:00
|
|
|
ws_confirm_request(rq->client, ws_key);
|
2017-07-29 22:00:34 +02:00
|
|
|
// insert wsocket flag to request
|
|
|
|
// plugin should handle this ugraded connection
|
|
|
|
// not the server
|
2018-10-06 00:41:59 +02:00
|
|
|
dput(rq->request,"__web_socket__",strdup("1"));
|
2017-07-29 22:00:34 +02:00
|
|
|
}
|
2018-10-04 19:47:31 +02:00
|
|
|
// resolve task
|
|
|
|
task->handle = resolve_request;
|
|
|
|
return task;
|
|
|
|
}
|
|
|
|
else if(strcmp(method,"POST") == 0)
|
|
|
|
{
|
|
|
|
task->handle = decode_post_request;
|
|
|
|
task->type = HEAVY;
|
|
|
|
return task;
|
2017-07-29 22:00:34 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-10-04 19:47:31 +02:00
|
|
|
unimplemented(rq->client);
|
|
|
|
return task;
|
2017-07-29 22:00:34 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-04 19:47:31 +02:00
|
|
|
|
|
|
|
void* decode_post_request(void* data)
|
2017-07-29 22:00:34 +02:00
|
|
|
{
|
2018-10-04 19:47:31 +02:00
|
|
|
antd_request_t* rq = (antd_request_t*) data;
|
|
|
|
dictionary request = dvalue(rq->request, "REQUEST_DATA");
|
|
|
|
dictionary headers = dvalue(rq->request, "REQUEST_HEADER");
|
|
|
|
char* ctype = NULL;
|
|
|
|
int clen = -1;
|
|
|
|
char* tmp;
|
|
|
|
antd_task_t* task = NULL;
|
|
|
|
ctype = (char*) dvalue(headers, "Content-Type");
|
|
|
|
tmp = (char*)dvalue(headers, "Content-Length");
|
|
|
|
if(tmp)
|
|
|
|
clen = atoi(tmp);
|
|
|
|
task = antd_create_task(NULL,(void*)rq, NULL);
|
2018-10-05 19:01:39 +02:00
|
|
|
task->priority++;
|
2018-10-04 19:47:31 +02:00
|
|
|
if(ctype == NULL || clen == -1)
|
|
|
|
{
|
|
|
|
LOG("Bad request\n");
|
|
|
|
badrequest(rq->client);
|
|
|
|
return task;
|
|
|
|
}
|
|
|
|
LOG("ContentType %s\n", ctype);
|
|
|
|
// decide what to do with the data
|
|
|
|
if(strstr(ctype,FORM_URL_ENCODE) > 0)
|
|
|
|
{
|
|
|
|
char* pquery = post_data_decode(rq->client,clen);
|
|
|
|
decode_url_request(pquery, request);
|
|
|
|
free(pquery);
|
|
|
|
} else if(strstr(ctype,FORM_MULTI_PART)> 0)
|
|
|
|
{
|
|
|
|
//printf("Multi part form : %s\n", ctype);
|
|
|
|
// TODO: split this to multiple task
|
2018-10-05 19:01:39 +02:00
|
|
|
free(task);
|
|
|
|
return decode_multi_part_request(rq,ctype,request);
|
2018-10-04 19:47:31 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char* pquery = post_data_decode(rq->client,clen);
|
|
|
|
char* key = strstr(ctype,"/");
|
|
|
|
if(key)
|
|
|
|
key++;
|
|
|
|
else
|
|
|
|
key = ctype;
|
|
|
|
dput(request,key, strdup(pquery));
|
|
|
|
free(pquery);
|
|
|
|
}
|
|
|
|
task->handle = resolve_request;
|
|
|
|
return task;
|
2017-07-29 22:00:34 +02:00
|
|
|
}
|
2018-10-04 19:47:31 +02:00
|
|
|
|
2017-07-29 22:00:34 +02:00
|
|
|
/**
|
|
|
|
* Send header to the client to confirm
|
|
|
|
* that the websocket is accepted by
|
|
|
|
* our server
|
|
|
|
*/
|
2018-02-10 11:22:41 +01:00
|
|
|
void ws_confirm_request(void* client, const char* key)
|
2017-07-29 22:00:34 +02:00
|
|
|
{
|
|
|
|
char buf[256];
|
|
|
|
char rkey[128];
|
|
|
|
char sha_d[20];
|
|
|
|
char base64[64];
|
|
|
|
strcpy(rkey,key);
|
|
|
|
strcat(rkey,WS_MAGIC_STRING);
|
|
|
|
//printf("RESPONDKEY '%s'\n", rkey);
|
2018-02-10 11:22:41 +01:00
|
|
|
#ifdef USE_OPENSSL
|
|
|
|
SHA_CTX context;
|
|
|
|
#else
|
2017-07-29 22:00:34 +02:00
|
|
|
SHA1_CTX context;
|
2018-02-10 11:22:41 +01:00
|
|
|
#endif
|
|
|
|
|
2017-07-29 22:00:34 +02:00
|
|
|
SHA1_Init(&context);
|
|
|
|
SHA1_Update(&context, rkey, strlen(rkey));
|
2018-02-10 11:22:41 +01:00
|
|
|
SHA1_Final(sha_d, &context);
|
2017-07-29 22:00:34 +02:00
|
|
|
Base64encode(base64, sha_d, 20);
|
|
|
|
//printf("Base 64 '%s'\n", base64);
|
|
|
|
// send accept to client
|
|
|
|
sprintf(buf, "HTTP/1.1 101 Switching Protocols\r\n");
|
2018-02-10 13:44:25 +01:00
|
|
|
antd_send(client, buf, strlen(buf));
|
2017-07-29 22:00:34 +02:00
|
|
|
sprintf(buf, "Upgrade: websocket\r\n");
|
2018-02-10 13:44:25 +01:00
|
|
|
antd_send(client, buf, strlen(buf));
|
2017-07-29 22:00:34 +02:00
|
|
|
sprintf(buf, "Connection: Upgrade\r\n");
|
2018-02-10 13:44:25 +01:00
|
|
|
antd_send(client, buf, strlen(buf));
|
2017-07-29 22:00:34 +02:00
|
|
|
sprintf(buf, "Sec-WebSocket-Accept: %s\r\n",base64);
|
2018-02-10 13:44:25 +01:00
|
|
|
antd_send(client, buf, strlen(buf));
|
2017-07-29 22:00:34 +02:00
|
|
|
sprintf(buf, "\r\n");
|
2018-02-10 13:44:25 +01:00
|
|
|
antd_send(client, buf, strlen(buf));
|
2017-07-29 22:00:34 +02:00
|
|
|
|
|
|
|
LOG("%s\n", "Websocket is now enabled for plugin");
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Decode the cookie header to a dictionary
|
|
|
|
* @param client The client socket
|
|
|
|
* @return The Dictionary socket or NULL
|
|
|
|
*/
|
|
|
|
dictionary decode_cookie(const char* line)
|
|
|
|
{
|
|
|
|
char *token,*token1;
|
|
|
|
char *cpstr = strdup(line);
|
2018-03-02 19:04:00 +01:00
|
|
|
char *orgcpy = cpstr;
|
2017-07-29 22:00:34 +02:00
|
|
|
trim(cpstr,' ');
|
|
|
|
trim(cpstr,'\n');
|
|
|
|
trim(cpstr,'\r');
|
2018-03-02 19:04:00 +01:00
|
|
|
|
2017-07-29 22:00:34 +02:00
|
|
|
dictionary dic = NULL;
|
|
|
|
while((token = strsep(&cpstr,";")))
|
|
|
|
{
|
|
|
|
trim(token,' ');
|
|
|
|
token1 = strsep(&token,"=");
|
2018-03-14 15:10:59 +01:00
|
|
|
if(token1 && token && strlen(token) > 0)
|
2017-07-29 22:00:34 +02:00
|
|
|
{
|
2018-10-05 19:01:39 +02:00
|
|
|
if(dic == NULL) dic = dict();
|
2017-07-29 22:00:34 +02:00
|
|
|
dput(dic,token1,strdup(token));
|
|
|
|
}
|
|
|
|
}
|
2018-03-02 19:04:00 +01:00
|
|
|
free(orgcpy);
|
2017-07-29 22:00:34 +02:00
|
|
|
return dic;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Decode the multi-part form data from the POST request
|
|
|
|
* If it is a file upload, copy the file to tmp dir
|
|
|
|
*/
|
2018-10-05 19:01:39 +02:00
|
|
|
void* decode_multi_part_request(void* data,const char* ctype, dictionary dic)
|
2017-07-29 22:00:34 +02:00
|
|
|
{
|
|
|
|
char * boundary;
|
|
|
|
char * line;
|
|
|
|
char * str_copy = strdup(ctype);
|
2018-03-02 19:04:00 +01:00
|
|
|
char* orgcpy = str_copy;
|
2018-10-05 19:01:39 +02:00
|
|
|
antd_request_t* rq = (antd_request_t*) data;
|
|
|
|
antd_task_t* task = antd_create_task(NULL, (void*)rq, NULL);
|
|
|
|
task->priority++;
|
2018-09-05 23:26:21 +02:00
|
|
|
//dictionary dic = NULL;
|
2017-07-29 22:00:34 +02:00
|
|
|
FILE *fp = NULL;
|
|
|
|
boundary = strsep(&str_copy,"="); //discard first part
|
2018-10-05 19:01:39 +02:00
|
|
|
boundary = str_copy;
|
2017-07-29 22:00:34 +02:00
|
|
|
if(boundary && strlen(boundary)>0)
|
|
|
|
{
|
2018-09-05 23:26:21 +02:00
|
|
|
//dic = dict();
|
2017-07-29 22:00:34 +02:00
|
|
|
trim(boundary,' ');
|
2018-10-05 19:01:39 +02:00
|
|
|
dput(rq->request, "MULTI_PART_BOUNDARY", strdup(boundary));
|
2017-07-29 22:00:34 +02:00
|
|
|
//find first boundary
|
2018-10-05 19:01:39 +02:00
|
|
|
while((line = read_line(rq->client))&&strstr(line,boundary) <= 0)
|
2018-03-02 19:04:00 +01:00
|
|
|
{
|
|
|
|
if(line) free(line);
|
|
|
|
}
|
2018-10-05 19:01:39 +02:00
|
|
|
if(line)
|
|
|
|
{
|
|
|
|
task->handle = decode_multi_part_request_data;
|
|
|
|
task->type = HEAVY;
|
|
|
|
free(line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(orgcpy);
|
|
|
|
return task;
|
|
|
|
}
|
|
|
|
void* decode_multi_part_request_data(void* data)
|
|
|
|
{
|
|
|
|
// loop through each part separated by the boundary
|
|
|
|
char* line;
|
|
|
|
char* orgline;
|
|
|
|
char* part_name = NULL;
|
|
|
|
char* part_file = NULL;
|
|
|
|
char* file_path;
|
|
|
|
char buf[BUFFLEN];
|
|
|
|
char* field;
|
|
|
|
//dictionary dic = NULL;
|
|
|
|
FILE *fp = NULL;
|
|
|
|
char* token, *keytoken, *valtoken;
|
|
|
|
antd_request_t* rq = (antd_request_t*) data;
|
|
|
|
antd_task_t* task = antd_create_task(NULL, (void*)rq, NULL);
|
|
|
|
task->priority++;
|
|
|
|
char* boundary = (char*)dvalue(rq->request, "MULTI_PART_BOUNDARY");
|
|
|
|
dictionary dic = (dictionary)dvalue(rq->request, "REQUEST_DATA");
|
|
|
|
char* boundend = __s("%s--",boundary);
|
|
|
|
// search for content disposition:
|
|
|
|
while((line = read_line(rq->client)) &&
|
|
|
|
strstr(line,"Content-Disposition:") <= 0)
|
|
|
|
{
|
|
|
|
free(line);
|
|
|
|
line = NULL;
|
|
|
|
}
|
|
|
|
if(!line || strstr(line,"Content-Disposition:") <= 0)
|
|
|
|
{
|
|
|
|
if(line)
|
|
|
|
free(line);
|
|
|
|
free(boundend);
|
|
|
|
return task;
|
|
|
|
}
|
|
|
|
orgline = line;
|
|
|
|
// extract parameters from header
|
|
|
|
while((token = strsep(&line,";")))
|
|
|
|
{
|
|
|
|
keytoken = strsep(&token,"=");
|
|
|
|
if(keytoken && strlen(keytoken)>0)
|
|
|
|
{
|
|
|
|
trim(keytoken,' ');
|
|
|
|
valtoken = strsep(&token,"=");
|
|
|
|
if(valtoken)
|
2018-09-05 23:26:21 +02:00
|
|
|
{
|
2018-10-05 19:01:39 +02:00
|
|
|
trim(valtoken,' ');
|
|
|
|
trim(valtoken,'\n');
|
|
|
|
trim(valtoken,'\r');
|
|
|
|
trim(valtoken,'\"');
|
|
|
|
if(strcmp(keytoken,"name") == 0)
|
|
|
|
{
|
|
|
|
part_name = strdup(valtoken);
|
|
|
|
} else if(strcmp(keytoken,"filename") == 0)
|
|
|
|
{
|
|
|
|
part_file = strdup(valtoken);
|
|
|
|
}
|
2018-09-05 23:26:21 +02:00
|
|
|
}
|
2018-10-05 19:01:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
free(orgline);
|
|
|
|
line = NULL;
|
|
|
|
// get the binary data
|
|
|
|
if(part_name != NULL)
|
|
|
|
{
|
|
|
|
// go to the beginer of data bock
|
|
|
|
while((line = read_line(rq->client)) && strcmp(line,"\r\n") != 0)
|
|
|
|
{
|
|
|
|
free(line);
|
|
|
|
line = NULL;
|
|
|
|
}
|
|
|
|
if(line)
|
|
|
|
{
|
|
|
|
free(line);
|
|
|
|
line = NULL;
|
|
|
|
}
|
|
|
|
if(part_file == NULL)
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* This allow only 1024 bytes of data (max),
|
|
|
|
* out of this range, the data is cut out.
|
|
|
|
* Need an efficient way to handle this
|
|
|
|
*/
|
|
|
|
line = read_line(rq->client);
|
|
|
|
trim(line,'\n');
|
|
|
|
trim(line,'\r');
|
|
|
|
trim(line,' ');
|
|
|
|
dput(dic,part_name,line);
|
|
|
|
// find the next boundary
|
|
|
|
while((line = read_line(rq->client)) && strstr(line,boundary) <= 0)
|
2018-03-02 19:04:00 +01:00
|
|
|
{
|
|
|
|
free(line);
|
|
|
|
line = NULL;
|
|
|
|
}
|
2018-10-05 19:01:39 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
file_path = __s("%s%s.%u",server_config.tmpdir,part_file,(unsigned)time(NULL));
|
|
|
|
fp=fopen(file_path, "wb");
|
|
|
|
if(fp)
|
2017-07-29 22:00:34 +02:00
|
|
|
{
|
2018-10-05 19:01:39 +02:00
|
|
|
int totalsize=0,len=0;
|
|
|
|
//read until the next boundary
|
|
|
|
while((len = read_buf(rq->client,buf,sizeof(buf))) > 0 && strstr(buf,boundary) <= 0)
|
2018-09-05 23:26:21 +02:00
|
|
|
{
|
2018-10-05 19:01:39 +02:00
|
|
|
fwrite(buf, len, 1, fp);
|
|
|
|
totalsize += len;
|
2018-03-02 19:04:00 +01:00
|
|
|
}
|
2018-10-05 19:01:39 +02:00
|
|
|
//remove \r\n at the end
|
|
|
|
fseek(fp, 0, SEEK_SET);
|
|
|
|
//fseek(fp,-2, SEEK_CUR);
|
|
|
|
totalsize -= 2;
|
|
|
|
ftruncate(fileno(fp),totalsize);
|
|
|
|
fclose(fp);
|
|
|
|
line = strdup(buf);
|
2017-07-29 22:00:34 +02:00
|
|
|
|
2018-10-05 19:01:39 +02:00
|
|
|
field = __s("%s.file",part_name);
|
|
|
|
dput(dic,field, strdup(part_file));
|
|
|
|
free(field);
|
|
|
|
field = __s("%s.tmp",part_name);
|
|
|
|
dput(dic,field,strdup(file_path));
|
|
|
|
free(field);
|
|
|
|
field = __s("%s.size",part_name);
|
|
|
|
dput(dic,field,__s("%d",totalsize));
|
|
|
|
free(field);
|
|
|
|
field = __s("%s.ext",part_name);
|
|
|
|
dput(dic,field,ext(part_file));
|
|
|
|
free(field);
|
2017-07-29 22:00:34 +02:00
|
|
|
|
|
|
|
}
|
2018-10-05 19:01:39 +02:00
|
|
|
else
|
2017-07-29 22:00:34 +02:00
|
|
|
{
|
2018-10-05 19:01:39 +02:00
|
|
|
LOG("Cannot wirte file to :%s\n", file_path );
|
2017-07-29 22:00:34 +02:00
|
|
|
}
|
2018-10-05 19:01:39 +02:00
|
|
|
free(file_path);
|
|
|
|
free(part_file);
|
2018-03-02 19:04:00 +01:00
|
|
|
}
|
2018-10-05 19:01:39 +02:00
|
|
|
free(part_name);
|
|
|
|
}
|
|
|
|
//printf("[Lines]:%s\n",line);
|
|
|
|
// check if end of request
|
|
|
|
if(line&&strstr(line,boundend)>0)
|
|
|
|
{
|
|
|
|
LOG("End request %s\n", boundend);
|
|
|
|
task->handle = resolve_request;
|
|
|
|
free(line);
|
2018-03-02 19:04:00 +01:00
|
|
|
free(boundend);
|
2018-10-05 19:01:39 +02:00
|
|
|
return task;
|
2017-07-29 22:00:34 +02:00
|
|
|
}
|
2018-10-05 19:01:39 +02:00
|
|
|
if(line && strstr(line,boundary) > 0)
|
|
|
|
{
|
|
|
|
// continue upload
|
|
|
|
task->type = HEAVY;
|
|
|
|
task->handle = decode_multi_part_request_data;
|
|
|
|
}
|
|
|
|
free(line);
|
|
|
|
free(boundend);
|
|
|
|
return task;
|
2017-07-29 22:00:34 +02:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Decode a query string (GET request or POST URL encoded) to
|
|
|
|
* a dictionary of key-value
|
|
|
|
* @param query : the query string
|
|
|
|
* @return a dictionary of key-value
|
|
|
|
*/
|
2018-09-05 23:26:21 +02:00
|
|
|
void decode_url_request(const char* query, dictionary dic)
|
2017-07-29 22:00:34 +02:00
|
|
|
{
|
2018-09-05 23:26:21 +02:00
|
|
|
if(query == NULL) return;
|
2017-07-29 22:00:34 +02:00
|
|
|
//str_copy = ;
|
|
|
|
char* token;
|
2018-09-05 23:26:21 +02:00
|
|
|
if(strlen(query) == 0) return;
|
2018-03-02 19:04:00 +01:00
|
|
|
char* str_copy = strdup(query);
|
|
|
|
char* org_copy = str_copy;
|
2018-09-05 23:26:21 +02:00
|
|
|
//dictionary dic = dict();
|
2017-07-29 22:00:34 +02:00
|
|
|
while ((token = strsep(&str_copy, "&")))
|
|
|
|
{
|
|
|
|
char* key;
|
2018-03-17 15:49:37 +01:00
|
|
|
char* val = NULL;
|
2017-07-29 22:00:34 +02:00
|
|
|
if(strlen(token)>0)
|
|
|
|
{
|
|
|
|
key = strsep(&token,"=");
|
|
|
|
if(key && strlen(key)>0)
|
|
|
|
{
|
2018-03-17 15:49:37 +01:00
|
|
|
val = strsep(&token,"=");
|
|
|
|
if(!val)
|
|
|
|
val = "";
|
2017-07-29 22:00:34 +02:00
|
|
|
dput(dic,key,url_decode(val));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-02 19:04:00 +01:00
|
|
|
free(org_copy);
|
2018-09-05 23:26:21 +02:00
|
|
|
//return dic;
|
2017-07-29 22:00:34 +02:00
|
|
|
}
|
|
|
|
/**
|
2018-02-19 17:44:45 +01:00
|
|
|
* Decode post query string to string
|
2017-07-29 22:00:34 +02:00
|
|
|
*/
|
2018-02-19 17:44:45 +01:00
|
|
|
char* post_data_decode(void* client,int len)
|
2017-07-29 22:00:34 +02:00
|
|
|
{
|
|
|
|
char *query = (char*) malloc((len+1)*sizeof(char));
|
|
|
|
for (int i = 0; i < len; i++) {
|
2018-02-10 13:44:25 +01:00
|
|
|
antd_recv(client, (query+i), 1);
|
2017-07-29 22:00:34 +02:00
|
|
|
}
|
|
|
|
query[len]='\0';
|
|
|
|
//query = url_decode(query);
|
2018-02-19 17:44:45 +01:00
|
|
|
//LOG("JSON Query %s\n", query);
|
2017-07-29 22:00:34 +02:00
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute a plugin based on the http requeset
|
|
|
|
* First decode the http request header to find the correct plugin
|
|
|
|
* and the correct function on the plugin
|
|
|
|
* Second, decode all parameters necessary of the request and pass it
|
|
|
|
* to the callback function.
|
|
|
|
* Execute the callback function if sucess
|
|
|
|
* @param client soket client
|
|
|
|
* @param path request path
|
|
|
|
* @param method request method
|
|
|
|
* @param query_string GET query string
|
|
|
|
* @return -1 if failure
|
|
|
|
* 1 if sucess
|
|
|
|
*/
|
2018-10-04 19:47:31 +02:00
|
|
|
void* execute_plugin(void* data, const char *path)
|
2017-07-29 22:00:34 +02:00
|
|
|
{
|
|
|
|
char pname[255];
|
|
|
|
char pfunc[255];
|
2018-10-04 19:47:31 +02:00
|
|
|
void* (*fn)(void*);
|
2017-07-29 22:00:34 +02:00
|
|
|
struct plugin_entry *plugin ;
|
|
|
|
int plen = strlen(path);
|
|
|
|
char * rpath = (char*) malloc((plen+1)*sizeof(char));
|
2018-03-02 19:04:00 +01:00
|
|
|
char* orgs = rpath;
|
2017-07-29 22:00:34 +02:00
|
|
|
char *error;
|
|
|
|
memcpy(rpath,path+1,plen);
|
|
|
|
rpath[plen] = '\0';
|
|
|
|
trim(rpath,'/');
|
|
|
|
char * delim = strchr(rpath,'/');
|
2018-10-04 19:47:31 +02:00
|
|
|
antd_request_t* rq = (antd_request_t*) data;
|
|
|
|
antd_task_t* task = antd_create_task(NULL, (void*)rq, NULL);
|
|
|
|
task->priority++;
|
2017-07-29 22:00:34 +02:00
|
|
|
if(delim == NULL)
|
|
|
|
{
|
|
|
|
strcpy(pname,rpath);
|
|
|
|
strcpy(pfunc,"default");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int npos,fpos;
|
|
|
|
npos = delim - rpath;
|
|
|
|
fpos = strlen(rpath) - npos ;
|
|
|
|
memcpy(pname,rpath,npos);
|
|
|
|
pname[npos] = '\0';
|
|
|
|
memcpy(pfunc,rpath+npos+1,fpos);
|
|
|
|
pfunc[fpos-1]='\0';
|
|
|
|
}
|
2018-10-04 19:47:31 +02:00
|
|
|
LOG("Client %d\n",((antd_client_t*)rq->client)->sock );
|
2017-07-29 22:00:34 +02:00
|
|
|
LOG("Path : '%s'\n", rpath);
|
|
|
|
LOG("Plugin name '%s'\n",pname);
|
|
|
|
LOG("Query path. '%s'\n", pfunc);
|
2018-02-04 19:46:47 +01:00
|
|
|
//LOG("query :%s\n", query_string);
|
2017-07-29 22:00:34 +02:00
|
|
|
|
|
|
|
//load the plugin
|
|
|
|
if((plugin = plugin_lookup(pname)) == NULL)
|
2018-03-14 12:41:51 +01:00
|
|
|
{
|
|
|
|
pthread_mutex_lock(&server_mux);
|
|
|
|
plugin= plugin_load(pname);
|
|
|
|
pthread_mutex_unlock(&server_mux);
|
|
|
|
if( plugin == NULL)
|
2018-03-14 10:51:46 +01:00
|
|
|
{
|
|
|
|
if(orgs) free(orgs);
|
2018-10-04 19:47:31 +02:00
|
|
|
unknow(rq->client);
|
|
|
|
return task;
|
2018-03-14 10:51:46 +01:00
|
|
|
}
|
2018-03-14 12:41:51 +01:00
|
|
|
}
|
2017-07-29 22:00:34 +02:00
|
|
|
// load the function
|
2018-10-04 19:47:31 +02:00
|
|
|
fn = (void* (*)(void*))dlsym(plugin->handle, PLUGIN_HANDLER);
|
2017-07-29 22:00:34 +02:00
|
|
|
if ((error = dlerror()) != NULL)
|
|
|
|
{
|
2018-03-14 10:51:46 +01:00
|
|
|
if(orgs) free(orgs);
|
2017-07-29 22:00:34 +02:00
|
|
|
LOG("Problem when finding %s method from %s : %s \n", PLUGIN_HANDLER, pname,error);
|
2018-10-04 19:47:31 +02:00
|
|
|
unknow(rq->client);
|
|
|
|
return task;
|
2017-07-29 22:00:34 +02:00
|
|
|
}
|
2018-10-04 19:47:31 +02:00
|
|
|
task->type = HEAVY;
|
|
|
|
task->handle = fn;
|
2018-03-02 19:04:00 +01:00
|
|
|
free(orgs);
|
2018-10-04 19:47:31 +02:00
|
|
|
return task;
|
2017-07-29 22:00:34 +02:00
|
|
|
}
|
2018-02-10 11:22:41 +01:00
|
|
|
|
|
|
|
#ifdef USE_OPENSSL
|
|
|
|
int usessl()
|
|
|
|
{
|
|
|
|
return server_config.usessl;
|
|
|
|
}
|
|
|
|
#endif
|