2015-10-22 11:39:11 +02:00
|
|
|
#include "http_server.h"
|
2019-12-15 12:10:06 +01:00
|
|
|
|
|
|
|
//define all basic mime here
|
|
|
|
static mime_t _mimes[] = {
|
|
|
|
{"image/bmp","bmp"},
|
|
|
|
{"image/jpeg","jpg,jpeg"},
|
|
|
|
{"text/css","css"},
|
|
|
|
{"text/markdown","md"},
|
|
|
|
{"text/csv","csv"},
|
|
|
|
{"application/pdf","pdf"},
|
|
|
|
{"image/gif","gif"},
|
|
|
|
{"text/html","html"},
|
|
|
|
{"application/json","json"},
|
|
|
|
{"application/javascript","js"},
|
|
|
|
{"image/png","png"},
|
|
|
|
{"text/plain","txt"},
|
|
|
|
{"application/xhtml+xml","xhtml"},
|
|
|
|
{"application/xml","xml"},
|
|
|
|
{"image/svg+xml","svg"},
|
|
|
|
{NULL,NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
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;
|
2018-10-08 19:32:23 +02:00
|
|
|
config_t *config()
|
2018-10-05 19:01:39 +02:00
|
|
|
{
|
|
|
|
return &server_config;
|
|
|
|
}
|
|
|
|
|
2019-12-11 23:17:42 +01:00
|
|
|
void error_log(const char* fmt, ...)
|
|
|
|
{
|
|
|
|
if(server_config.errorfp)
|
|
|
|
{
|
|
|
|
va_list arguments;
|
|
|
|
char * data;
|
|
|
|
va_start( arguments, fmt);
|
|
|
|
int dlen = vsnprintf(0,0,fmt,arguments) + 1;
|
|
|
|
va_end(arguments);
|
|
|
|
if ((data = (char*)malloc(dlen*sizeof(char))) != 0)
|
|
|
|
{
|
|
|
|
va_start(arguments, fmt);
|
|
|
|
vsnprintf(data, dlen, fmt, arguments);
|
|
|
|
va_end(arguments);
|
|
|
|
fwrite(data,dlen,1,server_config.errorfp);
|
|
|
|
fflush(server_config.errorfp);
|
|
|
|
free(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
void server_log(const char* fmt, ...)
|
|
|
|
{
|
|
|
|
|
|
|
|
if(server_config.logfp)
|
|
|
|
{
|
|
|
|
va_list arguments;
|
|
|
|
char * data;
|
|
|
|
va_start( arguments, fmt);
|
|
|
|
int dlen = vsnprintf(0,0,fmt,arguments) + 1;
|
|
|
|
va_end(arguments);
|
|
|
|
if ((data = (char*)malloc(dlen*sizeof(char))) != 0)
|
|
|
|
{
|
|
|
|
va_start(arguments, fmt);
|
|
|
|
vsnprintf(data, dlen, fmt, arguments);
|
|
|
|
va_end(arguments);
|
|
|
|
fwrite(data,dlen,1,server_config.logfp);
|
|
|
|
fflush(server_config.logfp);
|
|
|
|
free(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2018-10-05 19:01:39 +02:00
|
|
|
void destroy_config()
|
|
|
|
{
|
|
|
|
freedict(server_config.handlers);
|
2018-10-08 19:32:23 +02:00
|
|
|
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.tmpdir)
|
|
|
|
free(server_config.tmpdir);
|
2019-12-15 12:10:06 +01:00
|
|
|
if(server_config.ssl_cipher)
|
|
|
|
free(server_config.ssl_cipher);
|
2019-12-11 23:17:42 +01:00
|
|
|
if(server_config.errorfp)
|
|
|
|
{
|
|
|
|
fclose(server_config.errorfp);
|
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
if(server_config.logfp)
|
|
|
|
{
|
|
|
|
fclose(server_config.logfp);
|
|
|
|
}
|
|
|
|
#endif
|
2019-12-15 12:10:06 +01:00
|
|
|
if(server_config.mimes)
|
|
|
|
freedict(server_config.mimes);
|
2019-12-20 16:49:41 +01:00
|
|
|
if(server_config.ports)
|
|
|
|
{
|
|
|
|
chain_t it;
|
|
|
|
port_config_t* cnf;
|
|
|
|
for_each_assoc(it, server_config.ports)
|
|
|
|
{
|
|
|
|
cnf = (port_config_t*)it->value;
|
|
|
|
if(cnf && cnf->htdocs)
|
|
|
|
free(cnf->htdocs);
|
|
|
|
if(cnf->sock > 0)
|
|
|
|
{
|
|
|
|
close(cnf->sock);
|
|
|
|
}
|
2019-12-20 19:26:45 +01:00
|
|
|
list_free(&(cnf->rules));
|
2019-12-20 16:49:41 +01:00
|
|
|
}
|
|
|
|
freedict(server_config.ports);
|
|
|
|
}
|
2019-12-11 23:17:42 +01:00
|
|
|
LOG("Unclosed connection: %d", server_config.connection);
|
2018-10-05 19:01:39 +02:00
|
|
|
}
|
|
|
|
|
2018-10-08 19:32:23 +02:00
|
|
|
static int config_handler(void *conf, const char *section, const char *name,
|
2019-12-20 16:49:41 +01:00
|
|
|
const char *value)
|
2018-10-05 19:01:39 +02:00
|
|
|
{
|
2018-10-08 19:32:23 +02:00
|
|
|
config_t *pconfig = (config_t *)conf;
|
2019-12-20 16:49:41 +01:00
|
|
|
regmatch_t port_matches[2];
|
|
|
|
//trim(section, ' ');
|
|
|
|
//trim(value,' ');
|
|
|
|
//trim(name,' ');
|
2018-10-05 19:01:39 +02:00
|
|
|
//char * ppath = NULL;
|
2019-12-20 16:49:41 +01:00
|
|
|
if (MATCH("SERVER", "plugins"))
|
2018-10-08 19:32:23 +02:00
|
|
|
{
|
|
|
|
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", "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);
|
|
|
|
}
|
2019-12-11 23:17:42 +01:00
|
|
|
else if (MATCH("SERVER", "error_log"))
|
|
|
|
{
|
|
|
|
pconfig->errorfp = fopen(value, "w");
|
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
else if (MATCH("SERVER", "server_log"))
|
|
|
|
{
|
|
|
|
pconfig->logfp = fopen(value, "w");
|
|
|
|
}
|
|
|
|
#endif
|
2018-10-05 19:01:39 +02:00
|
|
|
#ifdef USE_OPENSSL
|
2018-10-08 19:32:23 +02:00
|
|
|
else if (MATCH("SERVER", "ssl.cert"))
|
|
|
|
{
|
|
|
|
pconfig->sslcert = strdup(value);
|
|
|
|
}
|
|
|
|
else if (MATCH("SERVER", "ssl.key"))
|
|
|
|
{
|
|
|
|
pconfig->sslkey = strdup(value);
|
|
|
|
}
|
2019-12-15 12:10:06 +01:00
|
|
|
else if(MATCH("SERVER","ssl.cipher"))
|
|
|
|
{
|
|
|
|
pconfig->ssl_cipher = strdup(value);
|
|
|
|
}
|
2018-10-05 19:01:39 +02:00
|
|
|
#endif
|
|
|
|
else if (strcmp(section, "FILEHANDLER") == 0)
|
|
|
|
{
|
2018-10-08 19:32:23 +02:00
|
|
|
dput(pconfig->handlers, name, strdup(value));
|
|
|
|
}
|
2018-10-08 23:16:15 +02:00
|
|
|
else if (strcmp(section, "AUTOSTART") == 0 || strcmp(section, "AUTOLOAD") == 0)
|
2018-10-08 19:32:23 +02:00
|
|
|
{
|
2018-10-05 19:01:39 +02:00
|
|
|
// The server section must be added before the autostart section
|
|
|
|
// auto start plugin
|
2018-10-08 19:32:23 +02:00
|
|
|
plugin_load((char *)value);
|
|
|
|
}
|
2019-12-15 12:10:06 +01:00
|
|
|
else if(strcmp(section, "MIMES") == 0)
|
|
|
|
{
|
|
|
|
dput(pconfig->mimes,name,strdup(value));
|
|
|
|
}
|
2019-12-20 16:49:41 +01:00
|
|
|
else if( regex_match("PORT:\\s*([0-9]+)", section, 2, port_matches) )
|
|
|
|
{
|
|
|
|
char buf[20];
|
|
|
|
memset(buf, '\0', sizeof(buf));
|
|
|
|
memcpy(buf, section + port_matches[1].rm_so, port_matches[1].rm_eo - port_matches[1].rm_so);
|
|
|
|
port_config_t* p = dvalue(pconfig->ports,buf);
|
|
|
|
if(!p)
|
|
|
|
{
|
|
|
|
p = (port_config_t*) malloc( sizeof(port_config_t));
|
|
|
|
p->htdocs = NULL;
|
|
|
|
p->sock = -1;
|
2019-12-20 19:26:45 +01:00
|
|
|
p->rules = list_init();
|
2019-12-20 16:49:41 +01:00
|
|
|
dput(pconfig->ports,buf, p);
|
|
|
|
p->port = atoi(buf);
|
|
|
|
}
|
|
|
|
if(strcmp(name, "htdocs") == 0)
|
|
|
|
{
|
|
|
|
p->htdocs = strdup(value);
|
|
|
|
}
|
|
|
|
else if(strcmp(name, "ssl.enable") == 0)
|
|
|
|
{
|
|
|
|
p->usessl = atoi(value);
|
|
|
|
if(p->usessl)
|
|
|
|
pconfig->enable_ssl = 1;
|
|
|
|
}
|
2019-12-20 19:26:45 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// other thing should be rules
|
|
|
|
list_put_s(&p->rules, name);
|
|
|
|
list_put_s(&p->rules, value);
|
|
|
|
}
|
2019-12-20 16:49:41 +01:00
|
|
|
}
|
2018-10-08 19:32:23 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0; /* unknown section/name, error */
|
|
|
|
}
|
|
|
|
return 1;
|
2018-10-05 19:01:39 +02:00
|
|
|
}
|
|
|
|
void init_file_system()
|
|
|
|
{
|
|
|
|
struct stat st;
|
2019-12-20 16:49:41 +01:00
|
|
|
port_config_t* pcnf;
|
|
|
|
chain_t it;
|
2018-10-05 19:01:39 +02:00
|
|
|
if (stat(server_config.plugins_dir, &st) == -1)
|
2019-12-20 16:49:41 +01:00
|
|
|
mkdirp(server_config.plugins_dir, 0755);
|
2018-10-08 19:32:23 +02:00
|
|
|
if (stat(server_config.db_path, &st) == -1)
|
2019-12-20 16:49:41 +01:00
|
|
|
mkdirp(server_config.db_path, 0755);
|
|
|
|
for_each_assoc(it, server_config.ports)
|
|
|
|
{
|
|
|
|
pcnf = (port_config_t*) it->value;
|
|
|
|
if (stat(pcnf->htdocs, &st) == -1)
|
|
|
|
{
|
|
|
|
mkdirp(pcnf->htdocs, 0755);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-10-08 19:32:23 +02:00
|
|
|
if (stat(server_config.tmpdir, &st) == -1)
|
2019-12-20 16:49:41 +01:00
|
|
|
mkdirp(server_config.tmpdir, 0755);
|
2018-10-08 19:32:23 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
removeAll(server_config.tmpdir, 0);
|
|
|
|
}
|
2018-10-05 19:01:39 +02:00
|
|
|
}
|
2018-10-08 19:32:23 +02:00
|
|
|
void load_config(const char *file)
|
2018-10-05 19:01:39 +02:00
|
|
|
{
|
2019-12-20 16:49:41 +01:00
|
|
|
server_config.ports = dict();
|
2018-10-05 19:01:39 +02:00
|
|
|
server_config.plugins_dir = "plugins/";
|
|
|
|
server_config.plugins_ext = ".dylib";
|
|
|
|
server_config.db_path = "databases/";
|
2019-12-20 16:49:41 +01:00
|
|
|
//server_config.htdocs = "htdocs/";
|
2018-10-05 19:01:39 +02:00
|
|
|
server_config.tmpdir = "tmp/";
|
|
|
|
server_config.n_workers = 4;
|
2019-12-20 16:49:41 +01:00
|
|
|
server_config.backlog = 1000;
|
2018-10-05 19:01:39 +02:00
|
|
|
server_config.handlers = dict();
|
2019-12-20 16:49:41 +01:00
|
|
|
server_config.maxcon = 100;
|
2018-10-05 19:01:39 +02:00
|
|
|
server_config.connection = 0;
|
2019-12-15 12:10:06 +01:00
|
|
|
server_config.errorfp = NULL;
|
|
|
|
server_config.logfp = NULL;
|
|
|
|
server_config.mimes = dict();
|
2019-12-20 16:49:41 +01:00
|
|
|
server_config.enable_ssl = 0;
|
2018-10-05 19:01:39 +02:00
|
|
|
server_config.sslcert = "cert.pem";
|
|
|
|
server_config.sslkey = "key.pem";
|
2019-12-15 12:10:06 +01:00
|
|
|
server_config.ssl_cipher = NULL;
|
|
|
|
// put it default mimes
|
|
|
|
for(int i = 0; _mimes[i].type != NULL; i++)
|
|
|
|
{
|
|
|
|
dput(server_config.mimes,_mimes[i].type, strdup(_mimes[i].ext));
|
|
|
|
}
|
2018-10-08 19:32:23 +02:00
|
|
|
if (ini_parse(file, config_handler, &server_config) < 0)
|
|
|
|
{
|
2019-12-11 23:17:42 +01:00
|
|
|
ERROR("Can't load '%s'. Used defaut configuration", file);
|
2018-10-05 19:01:39 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-12-11 23:17:42 +01:00
|
|
|
LOG("Using configuration : %s", file);
|
2018-10-05 19:01:39 +02:00
|
|
|
#ifdef USE_OPENSSL
|
2019-12-20 16:49:41 +01:00
|
|
|
LOG("SSL enable %d", server_config.enable_ssl);
|
2019-12-11 23:17:42 +01:00
|
|
|
LOG("SSL cert %s", server_config.sslcert);
|
|
|
|
LOG("SSL key %s", server_config.sslkey);
|
2019-12-19 09:58:19 +01:00
|
|
|
/*if(!server_config.ssl_cipher)
|
2019-12-15 12:10:06 +01:00
|
|
|
LOG("SSL Cipher suite: %s", "HIGH");
|
|
|
|
else
|
2019-12-19 09:58:19 +01:00
|
|
|
LOG("SSL Cipher suite: %s", server_config.ssl_cipher);*/
|
2018-10-05 19:01:39 +02:00
|
|
|
#endif
|
|
|
|
}
|
2019-12-15 12:10:06 +01:00
|
|
|
LOG("%d mimes entries found", server_config.mimes->size);
|
2019-12-20 16:49:41 +01:00
|
|
|
init_file_system();
|
2018-10-08 19:32:23 +02:00
|
|
|
}
|
2018-10-05 19:01:39 +02:00
|
|
|
|
2018-10-08 19:32:23 +02:00
|
|
|
void *accept_request(void *data)
|
2018-10-04 19:47:31 +02:00
|
|
|
{
|
|
|
|
char buf[BUFFLEN];
|
2018-10-08 19:32:23 +02:00
|
|
|
char *token = NULL;
|
|
|
|
char *line = NULL;
|
|
|
|
antd_task_t *task;
|
|
|
|
antd_request_t *rq = (antd_request_t *)data;
|
|
|
|
|
2019-07-31 15:11:59 +02:00
|
|
|
task = antd_create_task(NULL, (void *)rq, NULL, rq->client->last_io);
|
2018-10-04 19:47:31 +02:00
|
|
|
task->priority++;
|
2018-10-07 01:03:05 +02:00
|
|
|
fd_set read_flags, write_flags;
|
2018-10-05 19:01:39 +02:00
|
|
|
// first verify if the socket is ready
|
2018-10-08 19:32:23 +02:00
|
|
|
antd_client_t *client = (antd_client_t *)rq->client;
|
2018-10-05 19:01:39 +02:00
|
|
|
FD_ZERO(&read_flags);
|
2018-10-08 19:32:23 +02:00
|
|
|
FD_SET(rq->client->sock, &read_flags);
|
2018-10-07 01:03:05 +02:00
|
|
|
FD_ZERO(&write_flags);
|
2018-10-08 19:32:23 +02:00
|
|
|
FD_SET(rq->client->sock, &write_flags);
|
|
|
|
struct timeval timeout;
|
2018-10-05 19:01:39 +02:00
|
|
|
timeout.tv_sec = 0;
|
|
|
|
timeout.tv_usec = 500;
|
|
|
|
// select
|
2018-10-08 19:32:23 +02:00
|
|
|
int sel = select(client->sock + 1, &read_flags, &write_flags, (fd_set *)0, &timeout);
|
|
|
|
if (sel == -1)
|
2018-10-05 19:01:39 +02:00
|
|
|
{
|
2019-12-15 12:10:06 +01:00
|
|
|
antd_error(rq->client, 400, "Bad request");
|
2018-10-05 19:01:39 +02:00
|
|
|
return task;
|
|
|
|
}
|
2018-10-08 19:32:23 +02:00
|
|
|
if (sel == 0 || (!FD_ISSET(client->sock, &read_flags) && !FD_ISSET(client->sock, &write_flags)))
|
2018-03-14 11:55:15 +01:00
|
|
|
{
|
2019-07-31 15:11:59 +02:00
|
|
|
/*if(client->last_wait == 0) client->last_wait = time(NULL);
|
2018-10-05 19:01:39 +02:00
|
|
|
// retry it later
|
2018-10-16 12:22:13 +02:00
|
|
|
if(time(NULL) - client->last_wait > MAX_WAIT_S)
|
2018-10-14 21:41:59 +02:00
|
|
|
{
|
2018-10-16 12:22:13 +02:00
|
|
|
LOG("Read and write timeout, give up on %d\n", client->sock);
|
2018-10-14 21:41:59 +02:00
|
|
|
server_config.connection++;
|
2018-10-14 21:46:30 +02:00
|
|
|
unknow(rq->client);
|
2018-10-14 21:41:59 +02:00
|
|
|
return task;
|
2019-07-31 15:11:59 +02:00
|
|
|
}*/
|
2018-10-05 19:01:39 +02:00
|
|
|
task->handle = accept_request;
|
2018-10-04 19:47:31 +02:00
|
|
|
return task;
|
2018-03-14 11:55:15 +01:00
|
|
|
}
|
2018-10-07 01:03:05 +02:00
|
|
|
// perform the ssl handshake if enabled
|
|
|
|
#ifdef USE_OPENSSL
|
2018-10-08 19:32:23 +02:00
|
|
|
int ret = -1, stat;
|
2019-12-20 16:49:41 +01:00
|
|
|
if (client->port_config->usessl == 1 && client->status == 0)
|
2018-10-07 01:03:05 +02:00
|
|
|
{
|
2018-10-14 11:31:05 +02:00
|
|
|
//LOG("Atttempt %d\n", client->attempt);
|
2018-10-08 19:32:23 +02:00
|
|
|
if (SSL_accept((SSL *)client->ssl) == -1)
|
|
|
|
{
|
|
|
|
stat = SSL_get_error((SSL *)client->ssl, ret);
|
|
|
|
switch (stat)
|
2018-10-07 01:03:05 +02:00
|
|
|
{
|
2018-10-08 19:32:23 +02:00
|
|
|
case SSL_ERROR_WANT_READ:
|
|
|
|
case SSL_ERROR_WANT_WRITE:
|
|
|
|
case SSL_ERROR_NONE:
|
2018-10-15 20:21:16 +02:00
|
|
|
//LOG("RETRY SSL %d\n", client->sock);
|
2019-07-31 15:11:59 +02:00
|
|
|
/*if(client->last_wait == 0) client->last_wait = time(NULL);
|
|
|
|
if(time(NULL) - client->last_wait > MAX_WAIT_S)
|
|
|
|
{
|
|
|
|
server_config.connection++;
|
|
|
|
unknow(rq->client);
|
|
|
|
LOG("SSL timeout, give up on %d\n", client->sock);
|
|
|
|
return task;
|
|
|
|
}
|
|
|
|
task->status = TASK_ACCEPT_SSL_CONT;*/
|
2018-10-08 19:32:23 +02:00
|
|
|
task->handle = accept_request;
|
|
|
|
return task;
|
|
|
|
default:
|
2019-12-20 16:49:41 +01:00
|
|
|
ERROR("Error performing SSL handshake %d %d %s", stat, ret, ERR_error_string(ERR_get_error(), NULL));
|
|
|
|
antd_error(rq->client, 400, "Invalid SSL request");
|
2019-07-31 15:11:59 +02:00
|
|
|
//server_config.connection++;
|
2018-10-08 19:32:23 +02:00
|
|
|
ERR_print_errors_fp(stderr);
|
|
|
|
return task;
|
2018-10-07 01:03:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
client->status = 1;
|
|
|
|
task->handle = accept_request;
|
2019-07-31 15:11:59 +02:00
|
|
|
//LOG("Handshake finish for %d\n", client->sock);
|
2018-10-07 01:03:05 +02:00
|
|
|
return task;
|
|
|
|
}
|
2018-10-15 21:35:57 +02:00
|
|
|
else
|
2018-10-07 01:03:05 +02:00
|
|
|
{
|
2018-10-08 19:32:23 +02:00
|
|
|
if (!FD_ISSET(client->sock, &read_flags))
|
2018-10-07 01:03:05 +02:00
|
|
|
{
|
2019-07-31 15:11:59 +02:00
|
|
|
/*if(client->last_wait == 0) client->last_wait = time(NULL);
|
2018-10-16 12:22:13 +02:00
|
|
|
if(time(NULL) - client->last_wait > MAX_WAIT_S)
|
|
|
|
{
|
|
|
|
server_config.connection++;
|
|
|
|
unknow(rq->client);
|
|
|
|
LOG("Read timeout, give up on %d\n", client->sock);
|
|
|
|
return task;
|
2019-07-31 15:11:59 +02:00
|
|
|
}*/
|
2018-10-07 01:03:05 +02:00
|
|
|
task->handle = accept_request;
|
|
|
|
return task;
|
|
|
|
}
|
2018-10-15 21:35:57 +02:00
|
|
|
}
|
2018-10-07 01:03:05 +02:00
|
|
|
#endif
|
2019-12-11 23:17:42 +01:00
|
|
|
//LOG("Ready for reading %d\n", client->sock);
|
2019-07-31 15:11:59 +02:00
|
|
|
//server_config.connection++;
|
2018-10-08 19:32:23 +02:00
|
|
|
read_buf(rq->client, buf, sizeof(buf));
|
2018-10-04 19:47:31 +02:00
|
|
|
line = buf;
|
|
|
|
// get the method string
|
2018-10-08 19:32:23 +02:00
|
|
|
token = strsep(&line, " ");
|
|
|
|
if (!line)
|
2015-10-22 11:39:11 +02:00
|
|
|
{
|
2019-12-15 15:40:23 +01:00
|
|
|
//LOG("No method found");
|
2019-12-15 12:10:06 +01:00
|
|
|
antd_error(rq->client, 405, "No method found");
|
2018-10-04 19:47:31 +02:00
|
|
|
return task;
|
2015-10-22 11:39:11 +02:00
|
|
|
}
|
2018-10-08 19:32:23 +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, " ");
|
2018-10-08 19:32:23 +02:00
|
|
|
if (!line)
|
2015-10-22 11:39:11 +02:00
|
|
|
{
|
2019-12-15 15:40:23 +01:00
|
|
|
//LOG("No request found");
|
2019-12-15 12:10:06 +01:00
|
|
|
antd_error(rq->client, 400, "Bad request");
|
2018-10-04 19:47:31 +02:00
|
|
|
return task;
|
2015-10-22 11:39:11 +02:00
|
|
|
}
|
2018-10-08 19:32:23 +02:00
|
|
|
trim(token, ' ');
|
|
|
|
trim(line, ' ');
|
2018-10-04 19:47:31 +02:00
|
|
|
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-08 22:01:05 +02:00
|
|
|
dput(rq->request, "REQUEST_PATH", url_decode(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-08 19:32:23 +02:00
|
|
|
void *resolve_request(void *data)
|
2018-10-04 19:47:31 +02:00
|
|
|
{
|
|
|
|
struct stat st;
|
2018-10-08 19:32:23 +02:00
|
|
|
char path[2 * BUFFLEN];
|
|
|
|
antd_request_t *rq = (antd_request_t *)data;
|
2019-07-31 15:11:59 +02:00
|
|
|
antd_task_t *task = antd_create_task(NULL, (void *)rq, NULL, rq->client->last_io);
|
2018-10-04 19:47:31 +02:00
|
|
|
task->priority++;
|
2018-10-08 19:32:23 +02:00
|
|
|
char *url = (char *)dvalue(rq->request, "RESOURCE_PATH");
|
|
|
|
char *newurl = NULL;
|
|
|
|
char *rqp = NULL;
|
|
|
|
char *oldrqp = NULL;
|
2019-12-20 16:49:41 +01:00
|
|
|
strcpy(path, rq->client->port_config->htdocs);
|
2015-10-22 11:39:11 +02:00
|
|
|
strcat(path, url);
|
2019-12-15 15:40:23 +01:00
|
|
|
//LOG("Path is : %s", path);
|
2018-02-03 18:50:07 +01:00
|
|
|
//if (path[strlen(path) - 1] == '/')
|
|
|
|
// strcat(path, "index.html");
|
2018-10-08 19:32:23 +02:00
|
|
|
if (stat(path, &st) == -1)
|
|
|
|
{
|
2018-10-04 19:47:31 +02:00
|
|
|
free(task);
|
2018-10-08 19:32:23 +02:00
|
|
|
rqp = strdup((char *)dvalue(rq->request, "REQUEST_PATH"));
|
2018-10-07 23:41:37 +02:00
|
|
|
oldrqp = rqp;
|
|
|
|
trim(rqp, '/');
|
|
|
|
newurl = strsep(&rqp, "/");
|
2018-10-08 19:32:23 +02:00
|
|
|
if (!rqp)
|
|
|
|
rqp = strdup("/");
|
|
|
|
else
|
|
|
|
rqp = strdup(rqp);
|
2018-10-07 23:41:37 +02:00
|
|
|
dput(rq->request, "RESOURCE_PATH", rqp);
|
|
|
|
task = execute_plugin(rq, newurl);
|
|
|
|
free(oldrqp);
|
|
|
|
return task;
|
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-10-08 19:32:23 +02:00
|
|
|
if (stat(path, &st) == -1)
|
2018-02-05 11:42:01 +01:00
|
|
|
{
|
2019-12-15 12:10:06 +01:00
|
|
|
chain_t it;
|
2018-02-20 19:02:31 +01:00
|
|
|
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));
|
2019-12-20 16:49:41 +01:00
|
|
|
strcat(path, rq->client->port_config->htdocs);
|
2018-10-04 19:47:31 +02:00
|
|
|
strcat(path, newurl);
|
2018-10-08 19:32:23 +02:00
|
|
|
if (stat(path, &st) != 0)
|
2018-10-04 19:47:31 +02:00
|
|
|
{
|
|
|
|
free(newurl);
|
|
|
|
newurl = NULL;
|
|
|
|
}
|
|
|
|
else
|
2018-02-20 19:02:31 +01:00
|
|
|
{
|
2018-10-17 20:10:33 +02:00
|
|
|
i = HASHSIZE;
|
2018-02-20 19:02:31 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-10-08 19:32:23 +02:00
|
|
|
if (!newurl)
|
2018-02-20 19:02:31 +01:00
|
|
|
{
|
2019-12-15 12:10:06 +01:00
|
|
|
antd_error(rq->client, 404, "Resource Not Found");
|
2018-10-04 19:47:31 +02:00
|
|
|
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
|
2018-10-08 19:32:23 +02:00
|
|
|
char *mime_type = mime(path);
|
2018-10-04 19:47:31 +02:00
|
|
|
dput(rq->request, "RESOURCE_MIME", strdup(mime_type));
|
2018-10-08 19:32:23 +02:00
|
|
|
if (strcmp(mime_type, "application/octet-stream") == 0)
|
2016-12-07 10:57:14 +01:00
|
|
|
{
|
2018-10-08 19:32:23 +02:00
|
|
|
char *ex = ext(path);
|
|
|
|
char *h = dvalue(server_config.handlers, ex);
|
|
|
|
if (ex)
|
|
|
|
free(ex);
|
|
|
|
if (h)
|
2018-02-20 19:02:31 +01:00
|
|
|
{
|
2018-10-07 23:41:37 +02:00
|
|
|
//sprintf(path,"/%s%s",h,url);
|
2019-12-15 15:40:23 +01:00
|
|
|
//LOG("WARNING::::Access octetstream via handle %s", h);
|
2018-10-04 19:47:31 +02:00
|
|
|
//if(execute_plugin(client,buf,method,rq) < 0)
|
|
|
|
// cannot_execute(client);
|
|
|
|
free(task);
|
2018-10-07 23:41:37 +02:00
|
|
|
return execute_plugin(rq, h);
|
2018-02-20 19:02:31 +01:00
|
|
|
}
|
|
|
|
else
|
2019-12-15 12:10:06 +01:00
|
|
|
antd_error(rq->client, 403, "Access forbidden");
|
2016-12-07 10:57:14 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-10-04 19:47:31 +02:00
|
|
|
task->type = HEAVY;
|
|
|
|
task->handle = serve_file;
|
2018-10-08 19:32:23 +02:00
|
|
|
}
|
2018-10-04 19:47:31 +02:00
|
|
|
return task;
|
2018-03-02 19:04:00 +01:00
|
|
|
}
|
2015-10-22 11:39:11 +02:00
|
|
|
}
|
|
|
|
|
2018-10-08 19:32:23 +02:00
|
|
|
void *finish_request(void *data)
|
2018-10-03 23:42:42 +02:00
|
|
|
{
|
2018-10-19 12:37:26 +02:00
|
|
|
destroy_request(data);
|
2018-10-08 19:32:23 +02:00
|
|
|
server_config.connection--;
|
2019-12-11 23:17:42 +01:00
|
|
|
LOG("Remaining connection %d", server_config.connection);
|
2018-10-03 23:42:42 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-10-08 19:32:23 +02: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];
|
2018-10-08 19:32:23 +02:00
|
|
|
char *query = strdup(_query);
|
|
|
|
char *url = strdup(_url);
|
2018-02-04 19:46:47 +01:00
|
|
|
int ret;
|
2018-10-08 19:32:23 +02:00
|
|
|
char *target;
|
|
|
|
char *tmp, rep[10];
|
2018-02-04 19:46:47 +01:00
|
|
|
int idx = 0;
|
2018-10-08 19:32:23 +02:00
|
|
|
memset(rep, 0, 10);
|
2018-02-04 19:46:47 +01:00
|
|
|
// 1 group
|
2018-10-08 19:32:23 +02:00
|
|
|
if (!host || !(ret = regex_match(k, host, 10, key_matches)))
|
2018-02-04 19:46:47 +01:00
|
|
|
{
|
|
|
|
target = url;
|
2018-10-08 19:32:23 +02:00
|
|
|
ret = regex_match(k, url, 10, key_matches);
|
2018-02-04 19:46:47 +01:00
|
|
|
}
|
|
|
|
else
|
2018-10-08 19:32:23 +02:00
|
|
|
target = (char *)host;
|
2018-02-04 19:46:47 +01:00
|
|
|
|
2018-10-08 19:32:23 +02:00
|
|
|
if (!ret)
|
2018-03-02 19:04:00 +01:00
|
|
|
{
|
|
|
|
free(url);
|
|
|
|
free(query);
|
|
|
|
return 0;
|
|
|
|
}
|
2018-10-08 19:32:23 +02:00
|
|
|
tmp = (char *)v;
|
|
|
|
char *search = "<([a-zA-Z0-9]+)>";
|
2018-02-05 11:42:01 +01:00
|
|
|
//printf("match again %s\n",tmp);
|
2018-10-08 19:32:23 +02:00
|
|
|
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);
|
2018-10-08 19:32:23 +02:00
|
|
|
if (strcasecmp(rep, "url") == 0)
|
2018-02-04 19:46:47 +01:00
|
|
|
{
|
2018-10-08 19:32:23 +02:00
|
|
|
memcpy(buf + idx, url, strlen(url));
|
2018-02-04 19:46:47 +01:00
|
|
|
idx += strlen(url);
|
2018-10-08 19:32:23 +02:00
|
|
|
}
|
|
|
|
else if (strcasecmp(rep, "query") == 0)
|
2018-02-04 19:46:47 +01:00
|
|
|
{
|
2018-10-08 19:32:23 +02:00
|
|
|
memcpy(buf + idx, query, strlen(query));
|
2018-02-04 19:46:47 +01:00
|
|
|
idx += strlen(query);
|
2018-10-08 19:32:23 +02:00
|
|
|
}
|
|
|
|
else if (match_int(rep))
|
2018-02-04 19:46:47 +01:00
|
|
|
{
|
|
|
|
int i = atoi(rep);
|
2018-10-08 19:32:23 +02:00
|
|
|
memcpy(buf + idx, target + key_matches[i].rm_so, key_matches[i].rm_eo - key_matches[i].rm_so);
|
2018-02-04 19:46:47 +01:00
|
|
|
idx += key_matches[i].rm_eo - key_matches[i].rm_so;
|
2018-10-08 19:32:23 +02:00
|
|
|
}
|
|
|
|
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;
|
2018-02-04 19:46:47 +01:00
|
|
|
}
|
|
|
|
tmp += val_matches[1].rm_eo + 1;
|
|
|
|
//break;
|
|
|
|
}
|
|
|
|
// now modify the match 2 group
|
2018-10-08 19:32:23 +02:00
|
|
|
if (idx > 0)
|
2018-02-23 19:54:16 +01:00
|
|
|
{
|
2018-10-08 19:32:23 +02:00
|
|
|
if (tmp)
|
2018-02-23 19:54:16 +01:00
|
|
|
{
|
|
|
|
// copy the remainning of tmp
|
2018-10-08 19:32:23 +02:00
|
|
|
memcpy(buf + idx, tmp, strlen(tmp));
|
2018-02-23 19:54:16 +01:00
|
|
|
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
|
|
|
|
2018-10-08 19:32:23 +02:00
|
|
|
void *serve_file(void *data)
|
2015-10-22 11:39:11 +02:00
|
|
|
{
|
2018-10-08 19:32:23 +02:00
|
|
|
antd_request_t *rq = (antd_request_t *)data;
|
2019-07-31 15:11:59 +02:00
|
|
|
antd_task_t *task = antd_create_task(NULL, (void *)rq, NULL, rq->client->last_io);
|
2018-10-04 19:47:31 +02:00
|
|
|
task->priority++;
|
2018-10-08 19:32:23 +02:00
|
|
|
char *path = (char *)dvalue(rq->request, "ABS_RESOURCE_PATH");
|
|
|
|
char *mime_type = (char *)dvalue(rq->request, "RESOURCE_MIME");
|
2019-12-04 19:18:48 +01:00
|
|
|
// find content length
|
2019-11-30 20:40:40 +01:00
|
|
|
/*if (is_bin(path))
|
2018-10-04 19:47:31 +02:00
|
|
|
__fb(rq->client, path);
|
2019-11-30 20:40:40 +01:00
|
|
|
else*/
|
2019-12-04 19:18:48 +01:00
|
|
|
struct stat st;
|
|
|
|
int s = stat(path, &st);
|
|
|
|
if(s == -1)
|
|
|
|
{
|
2019-12-15 12:10:06 +01:00
|
|
|
antd_error(rq->client, 404, "File not found");
|
2019-12-04 19:18:48 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int size = (int)st.st_size;
|
2019-12-15 12:10:06 +01:00
|
|
|
char ibuf[20];
|
|
|
|
snprintf (ibuf, sizeof(ibuf), "%d",size);
|
|
|
|
antd_response_header_t rhd;
|
|
|
|
rhd.cookie = NULL;
|
|
|
|
rhd.status = 200;
|
|
|
|
rhd.header = dict();
|
|
|
|
dput(rhd.header, "Content-Type", strdup(mime_type));
|
|
|
|
dput(rhd.header, "Content-Length", strdup(ibuf));
|
|
|
|
antd_send_header(rq->client, &rhd);
|
|
|
|
|
2019-12-04 19:18:48 +01:00
|
|
|
__f(rq->client, path);
|
|
|
|
}
|
|
|
|
|
2018-10-04 19:47:31 +02:00
|
|
|
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)
|
2019-12-20 16:49:41 +01:00
|
|
|
{
|
|
|
|
ERROR("Port %d - socket: %s", *port, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2015-10-22 11:39:11 +02:00
|
|
|
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)
|
2019-12-20 16:49:41 +01:00
|
|
|
{
|
|
|
|
ERROR("Port %d -bind: %s", *port, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2018-10-08 19:32:23 +02:00
|
|
|
if (*port == 0) /* if dynamically allocating a port */
|
2015-10-22 11:39:11 +02:00
|
|
|
{
|
|
|
|
socklen_t namelen = sizeof(name);
|
|
|
|
if (getsockname(httpd, (struct sockaddr *)&name, &namelen) == -1)
|
2019-12-20 16:49:41 +01:00
|
|
|
{
|
|
|
|
ERROR("Port %d - getsockname: %s", *port, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2015-10-22 11:39:11 +02:00
|
|
|
*port = ntohs(name.sin_port);
|
|
|
|
}
|
2019-12-20 16:49:41 +01:00
|
|
|
LOG("back log is %d", server_config.backlog);
|
2018-02-05 23:04:02 +01:00
|
|
|
if (listen(httpd, server_config.backlog) < 0)
|
2019-12-20 16:49:41 +01:00
|
|
|
{
|
|
|
|
ERROR("Port %d - listen: %s", *port, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2018-10-08 19:32:23 +02:00
|
|
|
return (httpd);
|
2015-10-22 11:39:11 +02:00
|
|
|
}
|
|
|
|
|
2019-12-20 19:26:45 +01:00
|
|
|
char *apply_rules(list_t rules, const char *host, char *url)
|
2018-02-04 19:46:47 +01:00
|
|
|
{
|
|
|
|
// rule check
|
2018-10-08 19:32:23 +02:00
|
|
|
char *query_string = url;
|
2018-02-04 19:46:47 +01:00
|
|
|
while ((*query_string != '?') && (*query_string != '\0'))
|
|
|
|
query_string++;
|
|
|
|
if (*query_string == '?')
|
|
|
|
{
|
|
|
|
*query_string = '\0';
|
|
|
|
query_string++;
|
|
|
|
}
|
|
|
|
//char* oldurl = strdup(url);
|
2019-12-20 19:26:45 +01:00
|
|
|
int size = list_size(rules);
|
2018-10-08 19:32:23 +02:00
|
|
|
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;
|
2019-12-20 19:26:45 +01:00
|
|
|
k = list_at(rules, i)->value.s;
|
|
|
|
v = list_at(rules, i + 1)->value.s;
|
2018-02-04 19:46:47 +01:00
|
|
|
// 1 group
|
2018-10-08 19:32:23 +02:00
|
|
|
if (rule_check(k, v, host, url, query_string, url))
|
|
|
|
{
|
2018-02-23 19:54:16 +01:00
|
|
|
query_string = url;
|
2018-10-08 19:32:23 +02:00
|
|
|
|
2018-02-23 19:54:16 +01:00
|
|
|
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-10-08 19:32:23 +02: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
|
|
|
|
2018-10-08 19:32:23 +02:00
|
|
|
void *decode_request_header(void *data)
|
2017-07-29 22:00:34 +02:00
|
|
|
{
|
2018-10-08 19:32:23 +02:00
|
|
|
antd_request_t *rq = (antd_request_t *)data;
|
2019-12-15 12:10:06 +01:00
|
|
|
dictionary_t cookie = NULL;
|
2018-10-08 19:32:23 +02:00
|
|
|
char *line;
|
|
|
|
char *token;
|
|
|
|
char *query = NULL;
|
|
|
|
char *host = NULL;
|
|
|
|
char buf[2 * BUFFLEN];
|
|
|
|
char *url = (char *)dvalue(rq->request, "REQUEST_QUERY");
|
2019-12-15 12:10:06 +01:00
|
|
|
dictionary_t xheader = dict();
|
|
|
|
dictionary_t request = dict();
|
2018-10-08 19:32:23 +02:00
|
|
|
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-08 19:32:23 +02:00
|
|
|
dput(xheader, "REMOTE_ADDR", (void *)strdup(((antd_client_t *)rq->client)->ip));
|
2019-12-20 16:49:41 +01:00
|
|
|
dput(xheader, "SERVER_PORT", (void *)__s("%d", ((antd_client_t *)rq->client)->port_config->port));
|
2018-02-09 09:21:10 +01:00
|
|
|
//while((line = read_line(client)) && strcmp("\r\n",line))
|
2018-10-08 19:32:23 +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');
|
2018-10-08 19:32:23 +02:00
|
|
|
token = strsep(&line, ":");
|
|
|
|
trim(token, ' ');
|
|
|
|
trim(line, ' ');
|
|
|
|
if (token && line && strlen(line) > 0)
|
|
|
|
dput(xheader, token, strdup(line));
|
|
|
|
if (token != NULL && strcasecmp(token, "Cookie") == 0)
|
2018-02-05 11:42:01 +01:00
|
|
|
{
|
2018-10-08 19:32:23 +02:00
|
|
|
if (!cookie)
|
|
|
|
cookie = decode_cookie(line);
|
2018-02-05 11:42:01 +01:00
|
|
|
}
|
2018-10-08 19:32:23 +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));
|
2018-10-08 19:32:23 +02:00
|
|
|
strcat(buf, url);
|
2019-12-20 16:49:41 +01:00
|
|
|
LOG("Original query: %s", url);
|
2019-12-20 19:26:45 +01:00
|
|
|
query = apply_rules(rq->client->port_config->rules, host, buf);
|
2019-12-20 16:49:41 +01:00
|
|
|
LOG("Processed query: %s", query);
|
2018-10-08 22:01:05 +02:00
|
|
|
dput(rq->request, "RESOURCE_PATH", url_decode(buf));
|
2018-10-08 19:32:23 +02:00
|
|
|
if (query)
|
2018-09-05 23:26:21 +02:00
|
|
|
{
|
|
|
|
decode_url_request(query, request);
|
|
|
|
free(query);
|
|
|
|
}
|
2018-10-08 19:32:23 +02:00
|
|
|
if (cookie)
|
|
|
|
dput(rq->request, "COOKIE", cookie);
|
|
|
|
if (host)
|
|
|
|
free(host);
|
2018-10-04 19:47:31 +02:00
|
|
|
// header ok, now checkmethod
|
2019-07-31 15:11:59 +02:00
|
|
|
antd_task_t *task = antd_create_task(decode_request, (void *)rq, NULL,rq->client->last_io);
|
2019-04-11 11:00:52 +02:00
|
|
|
|
2018-10-04 19:47:31 +02:00
|
|
|
task->priority++;
|
|
|
|
return task;
|
|
|
|
}
|
|
|
|
|
2018-10-08 19:32:23 +02:00
|
|
|
void *decode_request(void *data)
|
2018-10-04 19:47:31 +02:00
|
|
|
{
|
2018-10-08 19:32:23 +02:00
|
|
|
antd_request_t *rq = (antd_request_t *)data;
|
2019-12-15 12:10:06 +01:00
|
|
|
dictionary_t headers = dvalue(rq->request, "REQUEST_HEADER");
|
2018-10-04 19:47:31 +02:00
|
|
|
int ws = 0;
|
2018-10-08 19:32:23 +02:00
|
|
|
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");
|
2019-07-31 15:11:59 +02:00
|
|
|
task = antd_create_task(NULL, (void *)rq, NULL, rq->client->last_io);
|
2018-10-04 19:47:31 +02:00
|
|
|
task->priority++;
|
2018-10-08 19:32:23 +02:00
|
|
|
if (strcmp(method, "GET") == 0 || strcmp(method, "HEAD") == 0)
|
2018-03-02 19:04:00 +01:00
|
|
|
{
|
2018-10-08 19:32:23 +02:00
|
|
|
//if(ctype) free(ctype);
|
|
|
|
if (ws && ws_key != NULL)
|
2017-07-29 22:00:34 +02:00
|
|
|
{
|
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-08 19:32:23 +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;
|
|
|
|
}
|
2018-10-08 19:32:23 +02:00
|
|
|
else if (strcmp(method, "POST") == 0)
|
2018-10-04 19:47:31 +02:00
|
|
|
{
|
2018-10-08 19:32:23 +02:00
|
|
|
task->handle = resolve_request;
|
|
|
|
//task->type = HEAVY;
|
2018-10-04 19:47:31 +02:00
|
|
|
return task;
|
2017-07-29 22:00:34 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-12-15 12:10:06 +01:00
|
|
|
antd_error(rq->client,501, "Request Method Not Implemented");
|
2018-10-04 19:47:31 +02:00
|
|
|
return task;
|
2017-07-29 22:00:34 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-04 19:47:31 +02:00
|
|
|
|
2018-10-08 19:32:23 +02:00
|
|
|
void *decode_post_request(void *data)
|
2017-07-29 22:00:34 +02:00
|
|
|
{
|
2018-10-08 19:32:23 +02:00
|
|
|
antd_request_t *rq = (antd_request_t *)data;
|
2019-12-15 12:10:06 +01:00
|
|
|
dictionary_t request = dvalue(rq->request, "REQUEST_DATA");
|
|
|
|
dictionary_t headers = dvalue(rq->request, "REQUEST_HEADER");
|
2018-10-08 19:32:23 +02:00
|
|
|
char *ctype = NULL;
|
2018-10-04 19:47:31 +02:00
|
|
|
int clen = -1;
|
2018-10-08 19:32:23 +02:00
|
|
|
char *tmp;
|
|
|
|
antd_task_t *task = NULL;
|
|
|
|
ctype = (char *)dvalue(headers, "Content-Type");
|
|
|
|
tmp = (char *)dvalue(headers, "Content-Length");
|
|
|
|
if (tmp)
|
2018-10-04 19:47:31 +02:00
|
|
|
clen = atoi(tmp);
|
2018-10-08 19:32:23 +02:00
|
|
|
char *method = (char *)dvalue(rq->request, "METHOD");
|
2019-07-31 15:11:59 +02:00
|
|
|
task = antd_create_task(NULL, (void *)rq, NULL, rq->client->last_io);
|
2018-10-05 19:01:39 +02:00
|
|
|
task->priority++;
|
2018-10-09 17:24:00 +02:00
|
|
|
task->type = HEAVY;
|
2018-10-08 19:32:23 +02:00
|
|
|
if (!method || strcmp(method, "POST") != 0)
|
|
|
|
return task;
|
|
|
|
if (ctype == NULL || clen == -1)
|
2018-10-04 19:47:31 +02:00
|
|
|
{
|
2019-12-15 12:10:06 +01:00
|
|
|
antd_error(rq->client, 400, "Bad Request, missing content description");
|
2018-10-04 19:47:31 +02:00
|
|
|
return task;
|
|
|
|
}
|
|
|
|
// decide what to do with the data
|
2018-10-08 19:32:23 +02:00
|
|
|
if (strstr(ctype, FORM_URL_ENCODE))
|
2018-10-04 19:47:31 +02:00
|
|
|
{
|
2018-10-08 19:32:23 +02:00
|
|
|
char *pquery = post_data_decode(rq->client, clen);
|
2018-10-04 19:47:31 +02:00
|
|
|
decode_url_request(pquery, request);
|
|
|
|
free(pquery);
|
2018-10-08 19:32:23 +02:00
|
|
|
}
|
|
|
|
else if (strstr(ctype, FORM_MULTI_PART))
|
2018-10-04 19:47:31 +02:00
|
|
|
{
|
|
|
|
//printf("Multi part form : %s\n", ctype);
|
2018-10-05 19:01:39 +02:00
|
|
|
free(task);
|
2018-10-08 19:32:23 +02:00
|
|
|
return decode_multi_part_request(rq, ctype);
|
2018-10-04 19:47:31 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-10-08 19:32:23 +02:00
|
|
|
char *pquery = post_data_decode(rq->client, clen);
|
|
|
|
char *key = strstr(ctype, "/");
|
|
|
|
if (key)
|
2018-10-04 19:47:31 +02:00
|
|
|
key++;
|
|
|
|
else
|
|
|
|
key = ctype;
|
2019-12-15 12:10:06 +01:00
|
|
|
if(pquery)
|
|
|
|
{
|
|
|
|
dput(request, key, strdup(pquery));
|
|
|
|
free(pquery);
|
|
|
|
}
|
2018-10-04 19:47:31 +02:00
|
|
|
}
|
|
|
|
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-10-08 19:32:23 +02: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];
|
2018-10-08 19:32:23 +02:00
|
|
|
strcpy(rkey, key);
|
|
|
|
strcat(rkey, WS_MAGIC_STRING);
|
2017-07-29 22:00:34 +02:00
|
|
|
//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
|
2018-10-08 19:32:23 +02:00
|
|
|
|
|
|
|
SHA1_Init(&context);
|
|
|
|
SHA1_Update(&context, rkey, strlen(rkey));
|
|
|
|
SHA1_Final((uint8_t *)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));
|
2018-10-08 19:32:23 +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));
|
2018-10-08 19:32:23 +02:00
|
|
|
|
2019-12-11 23:17:42 +01:00
|
|
|
LOG("%s", "Websocket is now enabled for plugin");
|
2017-07-29 22:00:34 +02:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Decode the cookie header to a dictionary
|
|
|
|
* @param client The client socket
|
|
|
|
* @return The Dictionary socket or NULL
|
|
|
|
*/
|
2019-12-15 12:10:06 +01:00
|
|
|
dictionary_t decode_cookie(const char *line)
|
2017-07-29 22:00:34 +02:00
|
|
|
{
|
2018-10-08 19:32:23 +02:00
|
|
|
char *token, *token1;
|
2017-07-29 22:00:34 +02:00
|
|
|
char *cpstr = strdup(line);
|
2018-03-02 19:04:00 +01:00
|
|
|
char *orgcpy = cpstr;
|
2018-10-08 19:32:23 +02:00
|
|
|
trim(cpstr, ' ');
|
|
|
|
trim(cpstr, '\n');
|
|
|
|
trim(cpstr, '\r');
|
|
|
|
|
2019-12-15 12:10:06 +01:00
|
|
|
dictionary_t dic = NULL;
|
2018-10-08 19:32:23 +02:00
|
|
|
while ((token = strsep(&cpstr, ";")))
|
2017-07-29 22:00:34 +02:00
|
|
|
{
|
2018-10-08 19:32:23 +02:00
|
|
|
trim(token, ' ');
|
|
|
|
token1 = strsep(&token, "=");
|
|
|
|
if (token1 && token && strlen(token) > 0)
|
2017-07-29 22:00:34 +02:00
|
|
|
{
|
2018-10-08 19:32:23 +02:00
|
|
|
if (dic == NULL)
|
|
|
|
dic = dict();
|
|
|
|
dput(dic, token1, strdup(token));
|
2017-07-29 22:00:34 +02:00
|
|
|
}
|
|
|
|
}
|
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-08 19:32:23 +02:00
|
|
|
void *decode_multi_part_request(void *data, const char *ctype)
|
2017-07-29 22:00:34 +02:00
|
|
|
{
|
2018-10-08 19:32:23 +02:00
|
|
|
char *boundary;
|
|
|
|
char *line;
|
|
|
|
char *str_copy = strdup(ctype);
|
|
|
|
char *orgcpy = str_copy;
|
|
|
|
antd_request_t *rq = (antd_request_t *)data;
|
2019-07-31 15:11:59 +02:00
|
|
|
antd_task_t *task = antd_create_task(NULL, (void *)rq, NULL, rq->client->last_io);
|
2018-10-05 19:01:39 +02:00
|
|
|
task->priority++;
|
2018-09-05 23:26:21 +02:00
|
|
|
//dictionary dic = NULL;
|
2018-10-08 19:32:23 +02:00
|
|
|
boundary = strsep(&str_copy, "="); //discard first part
|
|
|
|
boundary = str_copy;
|
|
|
|
if (boundary && strlen(boundary) > 0)
|
2017-07-29 22:00:34 +02:00
|
|
|
{
|
2018-09-05 23:26:21 +02:00
|
|
|
//dic = dict();
|
2018-10-08 19:32:23 +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-08 19:32:23 +02:00
|
|
|
while ((line = read_line(rq->client)) && !strstr(line, boundary))
|
2018-03-02 19:04:00 +01:00
|
|
|
{
|
2018-10-08 19:32:23 +02:00
|
|
|
if (line)
|
|
|
|
free(line);
|
2018-03-02 19:04:00 +01:00
|
|
|
}
|
2018-10-08 19:32:23 +02:00
|
|
|
if (line)
|
2018-10-05 19:01:39 +02:00
|
|
|
{
|
|
|
|
task->handle = decode_multi_part_request_data;
|
|
|
|
free(line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(orgcpy);
|
2018-10-09 17:24:00 +02:00
|
|
|
task->type = HEAVY;
|
2018-10-05 19:01:39 +02:00
|
|
|
return task;
|
|
|
|
}
|
2018-10-08 19:32:23 +02:00
|
|
|
void *decode_multi_part_request_data(void *data)
|
2018-10-05 19:01:39 +02:00
|
|
|
{
|
|
|
|
// loop through each part separated by the boundary
|
2018-10-08 19:32:23 +02:00
|
|
|
char *line;
|
|
|
|
char *orgline;
|
|
|
|
char *part_name = NULL;
|
|
|
|
char *part_file = NULL;
|
|
|
|
char *file_path;
|
|
|
|
char buf[BUFFLEN];
|
|
|
|
char *field;
|
2018-10-05 19:01:39 +02:00
|
|
|
//dictionary dic = NULL;
|
|
|
|
FILE *fp = NULL;
|
2018-10-08 19:32:23 +02:00
|
|
|
char *token, *keytoken, *valtoken;
|
|
|
|
antd_request_t *rq = (antd_request_t *)data;
|
2019-07-31 15:11:59 +02:00
|
|
|
antd_task_t *task = antd_create_task(NULL, (void *)rq, NULL, rq->client->last_io);
|
2018-10-05 19:01:39 +02:00
|
|
|
task->priority++;
|
2018-10-08 19:32:23 +02:00
|
|
|
char *boundary = (char *)dvalue(rq->request, "MULTI_PART_BOUNDARY");
|
2019-12-15 12:10:06 +01:00
|
|
|
dictionary_t dic = (dictionary_t)dvalue(rq->request, "REQUEST_DATA");
|
2018-10-08 19:32:23 +02:00
|
|
|
char *boundend = __s("%s--", boundary);
|
2018-10-05 19:01:39 +02:00
|
|
|
// search for content disposition:
|
2018-10-08 19:32:23 +02:00
|
|
|
while ((line = read_line(rq->client)) &&
|
|
|
|
!strstr(line, "Content-Disposition:"))
|
2018-10-05 19:01:39 +02:00
|
|
|
{
|
|
|
|
free(line);
|
|
|
|
line = NULL;
|
|
|
|
}
|
2018-10-08 19:32:23 +02:00
|
|
|
if (!line || !strstr(line, "Content-Disposition:"))
|
2018-10-05 19:01:39 +02:00
|
|
|
{
|
2018-10-08 19:32:23 +02:00
|
|
|
if (line)
|
2018-10-05 19:01:39 +02:00
|
|
|
free(line);
|
|
|
|
free(boundend);
|
|
|
|
return task;
|
|
|
|
}
|
|
|
|
orgline = line;
|
|
|
|
// extract parameters from header
|
2018-10-08 19:32:23 +02:00
|
|
|
while ((token = strsep(&line, ";")))
|
2018-10-05 19:01:39 +02:00
|
|
|
{
|
2018-10-08 19:32:23 +02:00
|
|
|
keytoken = strsep(&token, "=");
|
|
|
|
if (keytoken && strlen(keytoken) > 0)
|
2018-10-05 19:01:39 +02:00
|
|
|
{
|
2018-10-08 19:32:23 +02:00
|
|
|
trim(keytoken, ' ');
|
|
|
|
valtoken = strsep(&token, "=");
|
|
|
|
if (valtoken)
|
2018-09-05 23:26:21 +02:00
|
|
|
{
|
2018-10-08 19:32:23 +02:00
|
|
|
trim(valtoken, ' ');
|
|
|
|
trim(valtoken, '\n');
|
|
|
|
trim(valtoken, '\r');
|
|
|
|
trim(valtoken, '\"');
|
|
|
|
if (strcmp(keytoken, "name") == 0)
|
2018-10-05 19:01:39 +02:00
|
|
|
{
|
|
|
|
part_name = strdup(valtoken);
|
2018-10-08 19:32:23 +02:00
|
|
|
}
|
|
|
|
else if (strcmp(keytoken, "filename") == 0)
|
2018-10-05 19:01:39 +02:00
|
|
|
{
|
|
|
|
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
|
2018-10-08 19:32:23 +02:00
|
|
|
if (part_name != NULL)
|
2018-10-05 19:01:39 +02:00
|
|
|
{
|
2018-10-08 19:32:23 +02:00
|
|
|
// go to the beginning of data bock
|
|
|
|
while ((line = read_line(rq->client)) && strcmp(line, "\r\n") != 0)
|
2018-10-05 19:01:39 +02:00
|
|
|
{
|
|
|
|
free(line);
|
|
|
|
line = NULL;
|
|
|
|
}
|
2018-10-08 19:32:23 +02:00
|
|
|
if (line)
|
2018-10-05 19:01:39 +02:00
|
|
|
{
|
|
|
|
free(line);
|
|
|
|
line = NULL;
|
|
|
|
}
|
2018-10-08 19:32:23 +02:00
|
|
|
if (part_file == NULL)
|
2018-10-05 19:01:39 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* 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);
|
2018-10-08 19:32:23 +02:00
|
|
|
trim(line, '\n');
|
|
|
|
trim(line, '\r');
|
|
|
|
trim(line, ' ');
|
|
|
|
dput(dic, part_name, line);
|
2018-10-05 19:01:39 +02:00
|
|
|
// find the next boundary
|
2018-10-08 19:32:23 +02:00
|
|
|
while ((line = read_line(rq->client)) && !strstr(line, boundary))
|
2018-03-02 19:04:00 +01:00
|
|
|
{
|
|
|
|
free(line);
|
|
|
|
line = NULL;
|
|
|
|
}
|
2018-10-05 19:01:39 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-10-08 19:32:23 +02:00
|
|
|
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-08 19:32:23 +02:00
|
|
|
int totalsize = 0, len = 0;
|
2018-10-05 19:01:39 +02:00
|
|
|
//read until the next boundary
|
2018-10-07 01:03:05 +02:00
|
|
|
// TODO: this is not efficient for big file
|
|
|
|
// need a solution
|
2018-10-08 19:32:23 +02:00
|
|
|
while ((len = read_buf(rq->client, buf, sizeof(buf))) > 0 && !strstr(buf, boundary))
|
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;
|
2019-12-11 23:17:42 +01:00
|
|
|
int stat = ftruncate(fileno(fp), totalsize);
|
|
|
|
UNUSED(stat);
|
2018-10-05 19:01:39 +02:00
|
|
|
fclose(fp);
|
|
|
|
line = strdup(buf);
|
2017-07-29 22:00:34 +02:00
|
|
|
|
2018-10-08 19:32:23 +02:00
|
|
|
field = __s("%s.file", part_name);
|
|
|
|
dput(dic, field, strdup(part_file));
|
2018-10-05 19:01:39 +02:00
|
|
|
free(field);
|
2018-10-08 19:32:23 +02:00
|
|
|
field = __s("%s.tmp", part_name);
|
|
|
|
dput(dic, field, strdup(file_path));
|
2018-10-05 19:01:39 +02:00
|
|
|
free(field);
|
2018-10-08 19:32:23 +02:00
|
|
|
field = __s("%s.size", part_name);
|
|
|
|
dput(dic, field, __s("%d", totalsize));
|
2018-10-05 19:01:39 +02:00
|
|
|
free(field);
|
2018-10-08 19:32:23 +02:00
|
|
|
field = __s("%s.ext", part_name);
|
|
|
|
dput(dic, field, ext(part_file));
|
2018-10-05 19:01:39 +02:00
|
|
|
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
|
|
|
{
|
2019-12-11 23:17:42 +01:00
|
|
|
ERROR("Cannot write file to :%s", 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
|
2018-10-08 19:32:23 +02:00
|
|
|
if (line && strstr(line, boundend))
|
2018-10-05 19:01:39 +02:00
|
|
|
{
|
2019-12-15 15:40:23 +01:00
|
|
|
//LOG("End request %s", boundend);
|
2018-10-05 19:01:39 +02:00
|
|
|
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-08 19:32:23 +02:00
|
|
|
if (line && strstr(line, boundary))
|
2018-10-05 19:01:39 +02:00
|
|
|
{
|
|
|
|
// 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
|
|
|
|
*/
|
2019-12-15 12:10:06 +01:00
|
|
|
void decode_url_request(const char *query, dictionary_t dic)
|
2017-07-29 22:00:34 +02:00
|
|
|
{
|
2018-10-08 19:32:23 +02:00
|
|
|
if (query == NULL)
|
|
|
|
return;
|
2017-07-29 22:00:34 +02:00
|
|
|
//str_copy = ;
|
2018-10-08 19:32:23 +02:00
|
|
|
char *token;
|
|
|
|
if (strlen(query) == 0)
|
|
|
|
return;
|
|
|
|
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, "&")))
|
|
|
|
{
|
2018-10-08 19:32:23 +02:00
|
|
|
char *key;
|
|
|
|
char *val = NULL;
|
|
|
|
if (strlen(token) > 0)
|
2017-07-29 22:00:34 +02:00
|
|
|
{
|
2018-10-08 19:32:23 +02:00
|
|
|
key = strsep(&token, "=");
|
|
|
|
if (key && strlen(key) > 0)
|
2017-07-29 22:00:34 +02:00
|
|
|
{
|
2018-10-08 19:32:23 +02:00
|
|
|
val = strsep(&token, "=");
|
|
|
|
if (!val)
|
2018-03-17 15:49:37 +01:00
|
|
|
val = "";
|
2018-10-08 19:32:23 +02:00
|
|
|
dput(dic, key, url_decode(val));
|
2017-07-29 22:00:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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-10-08 19:32:23 +02:00
|
|
|
char *post_data_decode(void *client, int len)
|
2017-07-29 22:00:34 +02:00
|
|
|
{
|
2018-10-08 19:32:23 +02:00
|
|
|
char *query = (char *)malloc((len + 1) * sizeof(char));
|
|
|
|
char *ptr = query;
|
|
|
|
int readlen = len > BUFFLEN ? BUFFLEN : len;
|
2018-10-07 13:57:52 +02:00
|
|
|
int read = 0, stat = 1;
|
2018-10-08 19:32:23 +02:00
|
|
|
while (readlen > 0 && stat > 0)
|
2018-10-07 01:03:05 +02:00
|
|
|
{
|
2018-10-08 19:32:23 +02:00
|
|
|
stat = antd_recv(client, ptr + read, readlen);
|
|
|
|
if (stat > 0)
|
2018-10-07 13:57:52 +02:00
|
|
|
{
|
|
|
|
read += stat;
|
2018-10-08 19:32:23 +02:00
|
|
|
readlen = (len - read) > BUFFLEN ? BUFFLEN : (len - read);
|
2018-10-07 13:57:52 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-08 19:32:23 +02:00
|
|
|
|
|
|
|
if (read > 0)
|
|
|
|
query[read] = '\0';
|
2018-10-07 13:57:52 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
free(query);
|
|
|
|
query = NULL;
|
2018-10-07 01:03:05 +02:00
|
|
|
}
|
2018-10-08 19:32:23 +02:00
|
|
|
return query;
|
2017-07-29 22:00:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-08 19:32:23 +02:00
|
|
|
void *execute_plugin(void *data, const char *pname)
|
2017-07-29 22:00:34 +02:00
|
|
|
{
|
2018-10-08 19:32:23 +02:00
|
|
|
void *(*fn)(void *);
|
|
|
|
plugin_header_t *(*metafn)();
|
|
|
|
plugin_header_t *meta = NULL;
|
|
|
|
struct plugin_entry *plugin;
|
2017-07-29 22:00:34 +02:00
|
|
|
char *error;
|
2018-10-08 19:32:23 +02:00
|
|
|
antd_request_t *rq = (antd_request_t *)data;
|
2019-07-31 15:11:59 +02:00
|
|
|
antd_task_t *task = antd_create_task(NULL, (void *)rq, NULL, rq->client->last_io);
|
2018-10-04 19:47:31 +02:00
|
|
|
task->priority++;
|
2019-12-15 15:40:23 +01:00
|
|
|
//LOG("Plugin name '%s'", pname);
|
2017-07-29 22:00:34 +02:00
|
|
|
|
|
|
|
//load the plugin
|
2018-10-08 19:32:23 +02:00
|
|
|
if ((plugin = plugin_lookup((char *)pname)) == NULL)
|
2018-03-14 12:41:51 +01:00
|
|
|
{
|
|
|
|
pthread_mutex_lock(&server_mux);
|
2018-10-08 19:32:23 +02:00
|
|
|
plugin = plugin_load((char *)pname);
|
2018-03-14 12:41:51 +01:00
|
|
|
pthread_mutex_unlock(&server_mux);
|
2018-10-08 19:32:23 +02:00
|
|
|
if (plugin == NULL)
|
2018-03-14 10:51:46 +01:00
|
|
|
{
|
2019-12-15 12:10:06 +01:00
|
|
|
antd_error(rq->client, 503, "Requested service not found");
|
2018-10-08 19:32:23 +02:00
|
|
|
return task;
|
2018-03-14 10:51:46 +01:00
|
|
|
}
|
2018-03-14 12:41:51 +01:00
|
|
|
}
|
2018-10-08 19:32:23 +02:00
|
|
|
// check if the plugin want rawbody or decoded body
|
|
|
|
metafn = (plugin_header_t * (*)()) dlsym(plugin->handle, "meta");
|
|
|
|
if ((error = dlerror()) == NULL)
|
|
|
|
{
|
|
|
|
meta = metafn();
|
|
|
|
}
|
2017-07-29 22:00:34 +02:00
|
|
|
// load the function
|
2018-10-08 19:32:23 +02:00
|
|
|
fn = (void *(*)(void *))dlsym(plugin->handle, PLUGIN_HANDLER);
|
|
|
|
if ((error = dlerror()) != NULL)
|
2017-07-29 22:00:34 +02:00
|
|
|
{
|
2019-12-11 23:17:42 +01:00
|
|
|
ERROR("Problem when finding %s method from %s : %s", PLUGIN_HANDLER, pname, error);
|
2019-12-15 12:10:06 +01:00
|
|
|
antd_error(rq->client, 503, "Requested service not found");
|
2018-10-04 19:47:31 +02:00
|
|
|
return task;
|
2018-10-08 19:32:23 +02:00
|
|
|
}
|
|
|
|
// check if we need the raw data or not
|
|
|
|
if (meta && meta->raw_body == 1)
|
|
|
|
{
|
|
|
|
task->handle = fn;
|
2018-10-09 17:24:00 +02:00
|
|
|
task->type = HEAVY;
|
2018-10-08 19:32:23 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
free(task);
|
2019-07-31 15:11:59 +02:00
|
|
|
task = antd_create_task(decode_post_request, (void *)rq, fn, rq->client->last_io);
|
2018-10-08 19:32:23 +02:00
|
|
|
task->priority++;
|
|
|
|
}
|
|
|
|
return task;
|
2017-07-29 22:00:34 +02:00
|
|
|
}
|
2018-02-10 11:22:41 +01:00
|
|
|
|
2019-12-15 12:10:06 +01:00
|
|
|
dictionary_t mimes_list()
|
|
|
|
{
|
|
|
|
return server_config.mimes;
|
|
|
|
}
|