2021-02-24 18:12:36 +01:00
|
|
|
#ifndef _GNU_SOURCE
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#endif
|
2020-08-25 16:40:24 +02:00
|
|
|
#include <sys/socket.h>
|
2021-01-23 09:54:02 +01:00
|
|
|
#include <poll.h>
|
2020-08-25 16:40:24 +02:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
2020-09-13 01:29:55 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2022-08-27 16:57:44 +02:00
|
|
|
#include <fcntl.h>
|
2022-10-04 12:30:35 +02:00
|
|
|
#include <limits.h>
|
|
|
|
#include <stdlib.h>
|
2020-08-25 16:40:24 +02:00
|
|
|
|
|
|
|
#ifdef USE_OPENSSL
|
|
|
|
#include <openssl/ssl.h>
|
|
|
|
#include <openssl/err.h>
|
2020-09-13 01:29:55 +02:00
|
|
|
#include <openssl/sha.h>
|
|
|
|
#else
|
2021-01-01 13:30:04 +01:00
|
|
|
#include "lib/sha1.h"
|
2020-08-25 16:40:24 +02:00
|
|
|
#endif
|
|
|
|
|
2015-10-22 11:39:11 +02:00
|
|
|
#include "http_server.h"
|
2020-08-25 16:40:24 +02:00
|
|
|
#include "lib/handle.h"
|
|
|
|
#include "plugin_manager.h"
|
|
|
|
#include "lib/scheduler.h"
|
|
|
|
#include "lib/utils.h"
|
|
|
|
#include "lib/ini.h"
|
|
|
|
#include "lib/base64.h"
|
2019-12-15 12:10:06 +01:00
|
|
|
|
2020-08-27 13:31:40 +02:00
|
|
|
#define HEADER_MAX_SIZE 8192
|
|
|
|
|
2022-08-27 21:42:56 +02:00
|
|
|
// define all basic mime here
|
2019-12-15 12:10:06 +01:00
|
|
|
static mime_t _mimes[] = {
|
2022-08-27 16:57:44 +02:00
|
|
|
{"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}};
|
2019-12-15 12:10:06 +01:00
|
|
|
|
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
|
|
|
{
|
2022-08-27 16:57:44 +02:00
|
|
|
return &server_config;
|
2018-10-05 19:01:39 +02:00
|
|
|
}
|
|
|
|
void destroy_config()
|
|
|
|
{
|
2022-08-27 16:57:44 +02:00
|
|
|
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.tmpdir)
|
|
|
|
free(server_config.tmpdir);
|
|
|
|
if (server_config.ssl_cipher)
|
|
|
|
free(server_config.ssl_cipher);
|
|
|
|
if (server_config.gzip_types)
|
|
|
|
list_free(&server_config.gzip_types);
|
|
|
|
if (server_config.mimes)
|
|
|
|
freedict(server_config.mimes);
|
|
|
|
if (server_config.stat_fifo_path)
|
|
|
|
free(server_config.stat_fifo_path);
|
|
|
|
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 != NULL)
|
|
|
|
{
|
|
|
|
if (cnf->htdocs != NULL)
|
|
|
|
free(cnf->htdocs);
|
|
|
|
if (cnf->sock > 0)
|
|
|
|
{
|
|
|
|
close(cnf->sock);
|
|
|
|
}
|
|
|
|
freedict(cnf->rules);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
freedict(server_config.ports);
|
|
|
|
}
|
|
|
|
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,
|
2022-08-27 16:57:44 +02:00
|
|
|
const char *value)
|
2018-10-05 19:01:39 +02:00
|
|
|
{
|
2022-08-27 16:57:44 +02:00
|
|
|
config_t *pconfig = (config_t *)conf;
|
|
|
|
regmatch_t port_matches[2];
|
2022-10-04 12:30:35 +02:00
|
|
|
char * tmp;
|
2022-08-27 16:57:44 +02:00
|
|
|
struct stat st;
|
2022-08-27 21:42:56 +02:00
|
|
|
// trim(section, ' ');
|
|
|
|
// trim(value,' ');
|
|
|
|
// trim(name,' ');
|
|
|
|
// char * ppath = NULL;
|
2022-08-27 16:57:44 +02:00
|
|
|
if (MATCH("SERVER", "plugins"))
|
|
|
|
{
|
2022-10-04 13:02:20 +02:00
|
|
|
if (stat(value, &st) == -1)
|
|
|
|
mkdirp(value, 0755);
|
|
|
|
tmp = realpath(value, NULL);
|
|
|
|
if(!tmp)
|
|
|
|
{
|
|
|
|
ERROR("Unable to query real path for %s: %s", value, strerror(errno));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (pconfig->plugins_dir)
|
|
|
|
free(pconfig->plugins_dir);
|
|
|
|
pconfig->plugins_dir = tmp;
|
|
|
|
LOG("Database root is %s", pconfig->plugins_dir);
|
|
|
|
}
|
2022-08-27 16:57:44 +02:00
|
|
|
}
|
|
|
|
else if (MATCH("SERVER", "plugins_ext"))
|
|
|
|
{
|
|
|
|
if (pconfig->plugins_ext)
|
|
|
|
free(pconfig->plugins_ext);
|
|
|
|
pconfig->plugins_ext = strdup(value);
|
|
|
|
}
|
|
|
|
else if (MATCH("SERVER", "database"))
|
|
|
|
{
|
2022-10-04 12:30:35 +02:00
|
|
|
if (stat(value, &st) == -1)
|
|
|
|
mkdirp(value, 0700);
|
|
|
|
tmp = realpath(value, NULL);
|
|
|
|
if(!tmp)
|
|
|
|
{
|
|
|
|
ERROR("Unable to query real path for %s: %s", value, strerror(errno));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (pconfig->db_path)
|
|
|
|
free(pconfig->db_path);
|
|
|
|
pconfig->db_path = tmp;
|
|
|
|
LOG("Database root is %s", pconfig->db_path);
|
|
|
|
}
|
2022-08-27 16:57:44 +02:00
|
|
|
}
|
|
|
|
else if (MATCH("SERVER", "tmpdir"))
|
|
|
|
{
|
2022-10-04 12:30:35 +02:00
|
|
|
if (stat(value, &st) == -1)
|
|
|
|
{
|
|
|
|
mkdirp(value, 0755);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
removeAll(value, 0);
|
|
|
|
}
|
|
|
|
tmp = realpath(value, NULL);
|
|
|
|
if(!tmp)
|
|
|
|
{
|
|
|
|
ERROR("Unable to query real path for %s: %s", value, strerror(errno));
|
|
|
|
}
|
2022-08-27 16:57:44 +02:00
|
|
|
else
|
|
|
|
{
|
2022-10-04 12:30:35 +02:00
|
|
|
if (pconfig->tmpdir)
|
|
|
|
free(pconfig->tmpdir);
|
|
|
|
pconfig->tmpdir = tmp;
|
|
|
|
LOG("TMP root is %s", pconfig->tmpdir);
|
2022-08-27 16:57:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (MATCH("SERVER", "statistic_fifo"))
|
|
|
|
{
|
|
|
|
if (pconfig->stat_fifo_path)
|
|
|
|
free(pconfig->stat_fifo_path);
|
|
|
|
pconfig->stat_fifo_path = strdup(value);
|
|
|
|
}
|
|
|
|
else if (MATCH("SERVER", "max_upload_size"))
|
|
|
|
{
|
|
|
|
pconfig->max_upload_size = atoi(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);
|
|
|
|
}
|
|
|
|
else if (MATCH("SERVER", "debug_enable"))
|
|
|
|
{
|
2022-08-27 21:42:56 +02:00
|
|
|
(void)setenv("ANTD_DEBUG", value, 1);
|
2022-08-27 16:57:44 +02:00
|
|
|
pconfig->debug_enable = atoi(value);
|
|
|
|
}
|
|
|
|
else if (MATCH("SERVER", "scheduler_timeout"))
|
|
|
|
{
|
|
|
|
pconfig->scheduler_timeout = atoi(value);
|
|
|
|
}
|
2020-01-08 19:17:51 +01:00
|
|
|
#ifdef USE_ZLIB
|
2022-08-27 16:57:44 +02:00
|
|
|
else if (MATCH("SERVER", "gzip_enable"))
|
|
|
|
{
|
|
|
|
pconfig->gzip_enable = atoi(value);
|
|
|
|
}
|
|
|
|
else if (MATCH("SERVER", "gzip_types"))
|
|
|
|
{
|
|
|
|
pconfig->gzip_types = split(value, ",");
|
|
|
|
}
|
2020-01-08 19:17:51 +01:00
|
|
|
#endif
|
2018-10-05 19:01:39 +02:00
|
|
|
#ifdef USE_OPENSSL
|
2022-08-27 16:57:44 +02:00
|
|
|
else if (MATCH("SERVER", "ssl.cert"))
|
|
|
|
{
|
|
|
|
if (pconfig->sslcert)
|
|
|
|
free(pconfig->sslcert);
|
|
|
|
pconfig->sslcert = strdup(value);
|
|
|
|
}
|
|
|
|
else if (MATCH("SERVER", "ssl.key"))
|
|
|
|
{
|
|
|
|
if (pconfig->sslkey)
|
|
|
|
free(pconfig->sslkey);
|
|
|
|
pconfig->sslkey = strdup(value);
|
|
|
|
}
|
|
|
|
else if (MATCH("SERVER", "ssl.cipher"))
|
|
|
|
{
|
|
|
|
if (pconfig->ssl_cipher)
|
|
|
|
free(pconfig->ssl_cipher);
|
|
|
|
pconfig->ssl_cipher = strdup(value);
|
|
|
|
}
|
2018-10-05 19:01:39 +02:00
|
|
|
#endif
|
2022-08-27 16:57:44 +02:00
|
|
|
else if (strcmp(section, "FILEHANDLER") == 0)
|
|
|
|
{
|
|
|
|
dput(pconfig->handlers, name, strdup(value));
|
|
|
|
}
|
|
|
|
else if (strcmp(section, "AUTOSTART") == 0 || strcmp(section, "AUTOLOAD") == 0)
|
|
|
|
{
|
|
|
|
// The server section must be added before the autostart section
|
|
|
|
// auto start plugin
|
|
|
|
plugin_load((char *)value);
|
|
|
|
}
|
|
|
|
else if (strcmp(section, "MIMES") == 0)
|
|
|
|
{
|
|
|
|
dput(pconfig->mimes, name, strdup(value));
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
p->rules = dict_n(1);
|
|
|
|
dput(pconfig->ports, buf, p);
|
|
|
|
p->port = atoi(buf);
|
|
|
|
}
|
|
|
|
if (strcmp(name, "htdocs") == 0)
|
|
|
|
{
|
2022-10-04 12:30:35 +02:00
|
|
|
if (stat(value, &st) == -1)
|
|
|
|
{
|
|
|
|
mkdirp(value, 0755);
|
|
|
|
}
|
|
|
|
p->htdocs = realpath(value, NULL);
|
|
|
|
if(!p->htdocs)
|
|
|
|
{
|
|
|
|
ERROR("Unable to query real path for %s: %s", value, strerror(errno));
|
|
|
|
p->htdocs = strdup(value);
|
|
|
|
}
|
|
|
|
else
|
2022-08-27 16:57:44 +02:00
|
|
|
{
|
2022-10-04 12:30:35 +02:00
|
|
|
LOG("Server root is %s", p->htdocs);
|
2022-08-27 16:57:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (strcmp(name, "ssl.enable") == 0)
|
|
|
|
{
|
|
|
|
p->usessl = atoi(value);
|
|
|
|
if (p->usessl)
|
|
|
|
pconfig->enable_ssl = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// other thing should be rules
|
|
|
|
dput(p->rules, name, strdup(value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0; /* unknown section/name, error */
|
|
|
|
}
|
|
|
|
return 1;
|
2018-10-05 19:01:39 +02:00
|
|
|
}
|
2019-12-20 16:49:41 +01:00
|
|
|
|
2018-10-08 19:32:23 +02:00
|
|
|
void load_config(const char *file)
|
2018-10-05 19:01:39 +02:00
|
|
|
{
|
2022-08-27 16:57:44 +02:00
|
|
|
server_config.ports = dict();
|
|
|
|
server_config.plugins_dir = strdup("plugins/");
|
2022-10-04 12:30:35 +02:00
|
|
|
server_config.plugins_ext = strdup(".so");
|
2022-08-27 16:57:44 +02:00
|
|
|
server_config.db_path = strdup("databases/");
|
2022-08-27 21:42:56 +02:00
|
|
|
// server_config.htdocs = "htdocs/";
|
2022-10-04 12:30:35 +02:00
|
|
|
server_config.tmpdir = strdup("/tmp/");
|
2022-08-27 16:57:44 +02:00
|
|
|
server_config.stat_fifo_path = strdup("/var/run/antd_stat");
|
|
|
|
server_config.n_workers = 4;
|
|
|
|
server_config.backlog = 1000;
|
|
|
|
server_config.handlers = dict();
|
|
|
|
server_config.maxcon = 100;
|
2022-08-27 21:42:56 +02:00
|
|
|
server_config.max_upload_size = 10000000; // 10Mb
|
2022-08-27 16:57:44 +02:00
|
|
|
server_config.connection = 0;
|
|
|
|
server_config.mimes = dict();
|
|
|
|
server_config.enable_ssl = 0;
|
|
|
|
server_config.sslcert = strdup("cert.pem");
|
|
|
|
server_config.sslkey = strdup("key.pem");
|
|
|
|
server_config.ssl_cipher = NULL;
|
|
|
|
server_config.gzip_enable = 0;
|
|
|
|
server_config.gzip_types = NULL;
|
|
|
|
server_config.debug_enable = 0;
|
|
|
|
server_config.scheduler_timeout = 30; // 30 s
|
|
|
|
// put it default mimes
|
|
|
|
for (int i = 0; _mimes[i].type != NULL; i++)
|
|
|
|
{
|
|
|
|
dput(server_config.mimes, _mimes[i].type, strdup(_mimes[i].ext));
|
|
|
|
}
|
|
|
|
if (ini_parse(file, config_handler, &server_config) < 0)
|
|
|
|
{
|
|
|
|
ERROR("Can't load '%s'. Used defaut configuration", file);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOG("Using configuration : %s", file);
|
2018-10-05 19:01:39 +02:00
|
|
|
#ifdef USE_OPENSSL
|
2022-08-27 16:57:44 +02:00
|
|
|
LOG("SSL enable %d", server_config.enable_ssl);
|
|
|
|
LOG("SSL cert %s", server_config.sslcert);
|
|
|
|
LOG("SSL key %s", server_config.sslkey);
|
|
|
|
/*if(!server_config.ssl_cipher)
|
|
|
|
LOG("SSL Cipher suite: %s", "HIGH");
|
|
|
|
else
|
|
|
|
LOG("SSL Cipher suite: %s", server_config.ssl_cipher);*/
|
2018-10-05 19:01:39 +02:00
|
|
|
#endif
|
2022-08-27 16:57:44 +02:00
|
|
|
}
|
|
|
|
LOG("%d mimes entries found", server_config.mimes->size);
|
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
|
|
|
{
|
2022-08-27 16:57:44 +02:00
|
|
|
char buf[BUFFLEN];
|
|
|
|
char *token = NULL;
|
|
|
|
char *line = NULL;
|
|
|
|
antd_task_t *task;
|
|
|
|
antd_request_t *rq = (antd_request_t *)data;
|
2018-10-08 19:32:23 +02:00
|
|
|
|
2022-08-27 16:57:44 +02:00
|
|
|
task = antd_create_task(NULL, (void *)rq, NULL, rq->client->last_io);
|
|
|
|
antd_task_bind_event(task, rq->client->sock, 0, TASK_EVT_ON_WRITABLE | TASK_EVT_ON_READABLE);
|
|
|
|
// first verify if the socket is ready
|
|
|
|
antd_client_t *client = (antd_client_t *)rq->client;
|
2021-02-24 18:12:36 +01:00
|
|
|
|
2022-08-27 16:57:44 +02:00
|
|
|
struct pollfd pfd[1];
|
2021-01-23 09:54:02 +01:00
|
|
|
|
2022-08-27 16:57:44 +02:00
|
|
|
pfd[0].fd = client->sock;
|
|
|
|
pfd[0].events = POLLIN | POLLOUT;
|
2021-01-23 09:54:02 +01:00
|
|
|
|
2022-08-27 16:57:44 +02:00
|
|
|
int sel = poll(pfd, 1, POLL_EVENT_TO);
|
|
|
|
if (sel == -1)
|
|
|
|
{
|
|
|
|
antd_error(rq->client, 400, "Bad request");
|
|
|
|
return task;
|
|
|
|
}
|
|
|
|
if (pfd[0].revents & POLLERR || pfd[0].revents & POLLHUP)
|
|
|
|
{
|
|
|
|
antd_error(rq->client, 400, "Bad request");
|
|
|
|
return task;
|
|
|
|
}
|
|
|
|
if (sel == 0 || (!(pfd[0].revents & POLLIN) && !(pfd[0].revents & POLLOUT)))
|
|
|
|
{
|
|
|
|
task->handle = accept_request;
|
|
|
|
return task;
|
|
|
|
}
|
|
|
|
// perform the ssl handshake if enabled
|
2018-10-07 01:03:05 +02:00
|
|
|
#ifdef USE_OPENSSL
|
2022-08-27 16:57:44 +02:00
|
|
|
int ret = -1, stat;
|
|
|
|
if (client->ssl && client->state == ANTD_CLIENT_ACCEPT)
|
|
|
|
{
|
2022-08-27 21:42:56 +02:00
|
|
|
// LOG("Atttempt %d\n", client->attempt);
|
2022-08-27 16:57:44 +02:00
|
|
|
if (SSL_accept((SSL *)client->ssl) == -1)
|
|
|
|
{
|
|
|
|
stat = SSL_get_error((SSL *)client->ssl, ret);
|
|
|
|
switch (stat)
|
|
|
|
{
|
|
|
|
case SSL_ERROR_WANT_READ:
|
|
|
|
case SSL_ERROR_WANT_WRITE:
|
|
|
|
case SSL_ERROR_NONE:
|
|
|
|
task->handle = accept_request;
|
|
|
|
return task;
|
|
|
|
default:
|
|
|
|
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");
|
2022-08-27 21:42:56 +02:00
|
|
|
// server_config.connection++;
|
2022-08-27 16:57:44 +02:00
|
|
|
ERR_print_errors_fp(stderr);
|
|
|
|
return task;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
client->state = ANTD_CLIENT_HANDSHAKE;
|
|
|
|
task->handle = accept_request;
|
2022-08-27 21:42:56 +02:00
|
|
|
// LOG("Handshake finish for %d\n", client->sock);
|
2022-08-27 16:57:44 +02:00
|
|
|
return task;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!((pfd[0].revents & POLLIN)))
|
|
|
|
{
|
|
|
|
task->handle = accept_request;
|
|
|
|
return task;
|
|
|
|
}
|
|
|
|
}
|
2018-10-07 01:03:05 +02:00
|
|
|
#endif
|
2022-08-27 21:42:56 +02:00
|
|
|
// LOG("Ready for reading %d\n", client->sock);
|
|
|
|
// server_config.connection++;
|
2022-08-27 16:57:44 +02:00
|
|
|
client->state = ANTD_CLIENT_PROTO_CHECK;
|
|
|
|
read_buf(rq->client, buf, sizeof(buf));
|
|
|
|
line = buf;
|
|
|
|
LOG("Request (%d): %s", rq->client->sock, line);
|
|
|
|
// get the method string
|
|
|
|
token = strsep(&line, " ");
|
|
|
|
if (!line)
|
|
|
|
{
|
2022-08-27 21:42:56 +02:00
|
|
|
// LOG("No method found");
|
2022-08-27 16:57:44 +02:00
|
|
|
antd_error(rq->client, 405, "No method found");
|
|
|
|
return task;
|
|
|
|
}
|
|
|
|
trim(token, ' ');
|
|
|
|
trim(line, ' ');
|
|
|
|
dput(rq->request, "METHOD", strdup(token));
|
|
|
|
// get the request
|
|
|
|
token = strsep(&line, " ");
|
|
|
|
if (!line)
|
|
|
|
{
|
2022-08-27 21:42:56 +02:00
|
|
|
// LOG("No request found");
|
2022-08-27 16:57:44 +02:00
|
|
|
antd_error(rq->client, 400, "Bad request");
|
|
|
|
return task;
|
|
|
|
}
|
|
|
|
trim(token, ' ');
|
|
|
|
trim(line, ' ');
|
|
|
|
trim(line, '\n');
|
|
|
|
trim(line, '\r');
|
|
|
|
dput(rq->request, "PROTOCOL", strdup(line));
|
|
|
|
dput(rq->request, "REQUEST_QUERY", strdup(token));
|
|
|
|
line = token;
|
|
|
|
token = strsep(&line, "?");
|
|
|
|
dput(rq->request, "REQUEST_PATH", url_decode(token));
|
|
|
|
// decode request
|
|
|
|
// now return the task
|
|
|
|
task->handle = decode_request_header;
|
|
|
|
return task;
|
2018-10-04 19:47:31 +02:00
|
|
|
}
|
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
|
|
|
{
|
2022-08-27 16:57:44 +02:00
|
|
|
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, rq->client->last_io);
|
|
|
|
char *url = (char *)dvalue(rq->request, "RESOURCE_PATH");
|
|
|
|
char *newurl = NULL;
|
|
|
|
char *rqp = NULL;
|
|
|
|
char *oldrqp = NULL;
|
|
|
|
rq->client->state = ANTD_CLIENT_RESOLVE_REQUEST;
|
|
|
|
htdocs(rq, path);
|
|
|
|
strcat(path, url);
|
|
|
|
LOG("URL is : %s", url);
|
|
|
|
LOG("Resource Path is : %s", path);
|
2022-08-27 21:42:56 +02:00
|
|
|
// if (path[strlen(path) - 1] == '/')
|
|
|
|
// strcat(path, "index.html");
|
2022-08-27 16:57:44 +02:00
|
|
|
if (stat(path, &st) == -1)
|
|
|
|
{
|
|
|
|
free(task);
|
|
|
|
rqp = strdup(url);
|
|
|
|
oldrqp = rqp;
|
|
|
|
trim(rqp, '/');
|
|
|
|
newurl = strsep(&rqp, "/");
|
|
|
|
if (!rqp)
|
|
|
|
rqp = strdup("/");
|
|
|
|
else
|
|
|
|
rqp = strdup(rqp);
|
|
|
|
dput(rq->request, "RESOURCE_PATH", rqp);
|
|
|
|
LOG("Execute plugin %s", newurl);
|
|
|
|
task = execute_plugin(rq, newurl);
|
|
|
|
free(oldrqp);
|
|
|
|
return task;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (S_ISDIR(st.st_mode))
|
|
|
|
{
|
|
|
|
strcat(path, "/index.html");
|
|
|
|
if (stat(path, &st) == -1)
|
|
|
|
{
|
|
|
|
chain_t it;
|
|
|
|
for_each_assoc(it, server_config.handlers)
|
|
|
|
{
|
|
|
|
newurl = __s("%s/index.%s", url, it->key);
|
|
|
|
memset(path, 0, sizeof(path));
|
|
|
|
htdocs(rq, path);
|
|
|
|
strcat(path, newurl);
|
|
|
|
if (stat(path, &st) != 0)
|
|
|
|
{
|
|
|
|
free(newurl);
|
|
|
|
newurl = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
i = server_config.handlers->cap;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!newurl)
|
|
|
|
{
|
|
|
|
antd_error(rq->client, 404, "Resource Not Found");
|
|
|
|
return task;
|
|
|
|
}
|
2022-08-27 21:42:56 +02:00
|
|
|
// if(url) free(url); this is freed in the dput function
|
2022-08-27 16:57:44 +02:00
|
|
|
url = newurl;
|
|
|
|
dput(rq->request, "RESOURCE_PATH", url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dput(rq->request, "ABS_RESOURCE_PATH", strdup(path));
|
|
|
|
// check if the mime is supported
|
|
|
|
// if the mime is not supported
|
|
|
|
// 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);
|
|
|
|
dput(rq->request, "RESOURCE_MIME", strdup(mime_type));
|
|
|
|
if (strcmp(mime_type, "application/octet-stream") == 0)
|
|
|
|
{
|
|
|
|
char *ex = ext(path);
|
|
|
|
char *h = NULL;
|
|
|
|
if (ex)
|
|
|
|
{
|
|
|
|
h = dvalue(server_config.handlers, ex);
|
|
|
|
free(ex);
|
|
|
|
}
|
|
|
|
if (h)
|
|
|
|
{
|
2022-08-27 21:42:56 +02:00
|
|
|
// sprintf(path,"/%s%s",h,url);
|
|
|
|
// LOG("WARNING::::Access octetstream via handle %s", h);
|
|
|
|
// if(execute_plugin(client,buf,method,rq) < 0)
|
|
|
|
// cannot_execute(client);
|
2022-08-27 16:57:44 +02:00
|
|
|
free(task);
|
|
|
|
return execute_plugin(rq, h);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
antd_error(rq->client, 403, "Access forbidden");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// discard all request data
|
|
|
|
dictionary_t headers = (dictionary_t)dvalue(rq->request, "REQUEST_HEADER");
|
|
|
|
if (headers)
|
|
|
|
{
|
|
|
|
char *sclen = (char *)dvalue(headers, "Content-Length");
|
|
|
|
unsigned clen = 0;
|
|
|
|
unsigned read = 0;
|
|
|
|
int count;
|
|
|
|
if (sclen)
|
|
|
|
{
|
|
|
|
clen = atoi(sclen);
|
|
|
|
while (read < clen)
|
|
|
|
{
|
|
|
|
count = antd_recv(rq->client, path, sizeof(path) < clen ? sizeof(path) : clen);
|
|
|
|
if (count <= 0)
|
|
|
|
break;
|
|
|
|
read += count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
antd_task_bind_event(task, rq->client->sock, 0, TASK_EVT_ON_WRITABLE);
|
|
|
|
task->handle = serve_file;
|
|
|
|
}
|
|
|
|
return task;
|
|
|
|
}
|
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
|
|
|
{
|
2022-08-27 16:57:44 +02:00
|
|
|
if (!data)
|
|
|
|
return NULL;
|
|
|
|
destroy_request(data);
|
|
|
|
server_config.connection--;
|
|
|
|
LOG("Remaining connection %d", server_config.connection);
|
|
|
|
return NULL;
|
2018-10-03 23:42:42 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2022-08-27 16:57:44 +02: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);
|
|
|
|
LOG("Verify %s on %s or %s", k, url, host);
|
|
|
|
// 1 group
|
|
|
|
if (!host || !(ret = regex_match(k, host, 10, key_matches)))
|
|
|
|
{
|
|
|
|
target = url;
|
|
|
|
ret = regex_match(k, url, 10, key_matches);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
target = (char *)host;
|
2018-02-04 19:46:47 +01:00
|
|
|
|
2022-08-27 16:57:44 +02:00
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
free(url);
|
|
|
|
free(query);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
LOG("Match found on %s", target);
|
|
|
|
tmp = (char *)v;
|
|
|
|
char *search = "<([a-zA-Z0-9]+)>";
|
|
|
|
while ((ret = regex_match(search, tmp, 2, val_matches)))
|
|
|
|
{
|
|
|
|
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;
|
2022-08-27 21:42:56 +02:00
|
|
|
// break;
|
2022-08-27 16:57:44 +02:00
|
|
|
}
|
|
|
|
// now modify the match 2 group
|
|
|
|
if (idx > 0)
|
|
|
|
{
|
|
|
|
if (tmp)
|
|
|
|
{
|
|
|
|
// copy the remainning of tmp
|
|
|
|
memcpy(buf + idx, tmp, strlen(tmp));
|
|
|
|
idx += strlen(tmp);
|
|
|
|
}
|
|
|
|
buf[idx] = '\0';
|
|
|
|
}
|
|
|
|
free(url);
|
|
|
|
free(query);
|
|
|
|
LOG("New URI is %s", buf);
|
|
|
|
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
|
|
|
{
|
2022-08-27 16:57:44 +02:00
|
|
|
antd_request_t *rq = (antd_request_t *)data;
|
|
|
|
antd_task_t *task = antd_create_task(NULL, (void *)rq, NULL, rq->client->last_io);
|
|
|
|
char *path = (char *)dvalue(rq->request, "ABS_RESOURCE_PATH");
|
|
|
|
char *mime_type = (char *)dvalue(rq->request, "RESOURCE_MIME");
|
|
|
|
rq->client->state = ANTD_CLIENT_SERVE_FILE;
|
|
|
|
struct stat st;
|
|
|
|
int s = stat(path, &st);
|
2020-08-27 13:31:40 +02:00
|
|
|
|
2022-08-27 16:57:44 +02:00
|
|
|
if (s == -1)
|
|
|
|
{
|
|
|
|
antd_error(rq->client, 404, "File not found");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// check if it is modified
|
|
|
|
dictionary_t header = (dictionary_t)dvalue(rq->request, "REQUEST_HEADER");
|
|
|
|
char *last_modif_since = (char *)dvalue(header, "If-Modified-Since");
|
|
|
|
time_t t = st.st_ctime;
|
|
|
|
struct tm tm;
|
|
|
|
if (last_modif_since)
|
|
|
|
{
|
|
|
|
strptime(last_modif_since, "%a, %d %b %Y %H:%M:%S GMT", &tm);
|
|
|
|
t = timegm(&tm);
|
2022-08-27 21:42:56 +02:00
|
|
|
// t = mktime(localtime(&t));
|
2022-08-27 16:57:44 +02:00
|
|
|
}
|
2019-12-15 12:10:06 +01:00
|
|
|
|
2022-08-27 16:57:44 +02:00
|
|
|
if (last_modif_since && st.st_ctime == t)
|
|
|
|
{
|
|
|
|
// return the not changed
|
|
|
|
antd_error(rq->client, 304, "");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int size = (int)st.st_size;
|
|
|
|
char ibuf[64];
|
|
|
|
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));
|
2020-01-11 22:30:01 +01:00
|
|
|
#ifdef USE_ZLIB
|
2022-08-27 16:57:44 +02:00
|
|
|
if (!compressable(mime_type) || rq->client->z_level == ANTD_CNONE)
|
2020-01-11 22:30:01 +01:00
|
|
|
#endif
|
2022-08-27 16:57:44 +02:00
|
|
|
dput(rhd.header, "Content-Length", strdup(ibuf));
|
|
|
|
gmtime_r(&st.st_ctime, &tm);
|
|
|
|
strftime(ibuf, 64, "%a, %d %b %Y %H:%M:%S GMT", &tm);
|
|
|
|
dput(rhd.header, "Last-Modified", strdup(ibuf));
|
|
|
|
dput(rhd.header, "Cache-Control", strdup("no-cache"));
|
|
|
|
antd_send_header(rq->client, &rhd);
|
2020-01-10 17:31:49 +01:00
|
|
|
|
2022-08-27 16:57:44 +02:00
|
|
|
__f(rq->client, path);
|
|
|
|
}
|
|
|
|
}
|
2020-08-27 13:31:40 +02:00
|
|
|
|
2022-08-27 16:57:44 +02:00
|
|
|
return task;
|
2015-10-22 11:39:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int startup(unsigned *port)
|
|
|
|
{
|
2022-08-27 16:57:44 +02:00
|
|
|
int httpd = 0;
|
|
|
|
struct sockaddr_in name;
|
|
|
|
httpd = socket(PF_INET, SOCK_STREAM, 0);
|
|
|
|
if (httpd == -1)
|
|
|
|
{
|
|
|
|
ERROR("Port %d - socket: %s", *port, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2021-01-03 11:24:55 +01:00
|
|
|
|
2022-08-27 16:57:44 +02:00
|
|
|
if (setsockopt(httpd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int)) == -1)
|
|
|
|
{
|
|
|
|
ERROR("Unable to set reuse address on port %d - setsockopt: %s", *port, strerror(errno));
|
|
|
|
}
|
2020-12-28 12:26:08 +01:00
|
|
|
|
2022-08-27 16:57:44 +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)
|
|
|
|
{
|
|
|
|
ERROR("Port %d -bind: %s", *port, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (*port == 0) /* if dynamically allocating a port */
|
|
|
|
{
|
|
|
|
socklen_t namelen = sizeof(name);
|
|
|
|
if (getsockname(httpd, (struct sockaddr *)&name, &namelen) == -1)
|
|
|
|
{
|
|
|
|
ERROR("Port %d - getsockname: %s", *port, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*port = ntohs(name.sin_port);
|
|
|
|
}
|
2020-12-28 12:26:08 +01:00
|
|
|
|
2022-08-27 16:57:44 +02:00
|
|
|
LOG("back log is %d", server_config.backlog);
|
|
|
|
if (listen(httpd, server_config.backlog) < 0)
|
|
|
|
{
|
|
|
|
ERROR("Port %d - listen: %s", *port, strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return (httpd);
|
2015-10-22 11:39:11 +02:00
|
|
|
}
|
|
|
|
|
2019-12-22 17:33:35 +01:00
|
|
|
char *apply_rules(dictionary_t rules, const char *host, char *url)
|
2018-02-04 19:46:47 +01:00
|
|
|
{
|
2022-08-27 16:57:44 +02:00
|
|
|
// rule check
|
|
|
|
char *query_string = url;
|
|
|
|
while ((*query_string != '?') && (*query_string != '\0'))
|
|
|
|
query_string++;
|
|
|
|
if (*query_string == '?')
|
|
|
|
{
|
|
|
|
*query_string = '\0';
|
|
|
|
query_string++;
|
|
|
|
}
|
2022-08-27 21:42:56 +02:00
|
|
|
// char* oldurl = strdup(url);
|
2022-08-27 16:57:44 +02:00
|
|
|
chain_t it;
|
|
|
|
char *k;
|
|
|
|
char *v;
|
|
|
|
for_each_assoc(it, rules)
|
|
|
|
{
|
|
|
|
k = it->key;
|
|
|
|
v = (char *)it->value;
|
|
|
|
// 1 group
|
|
|
|
if (rule_check(k, v, host, url, query_string, url))
|
|
|
|
{
|
|
|
|
query_string = url;
|
2018-10-08 19:32:23 +02:00
|
|
|
|
2022-08-27 16:57:44 +02:00
|
|
|
while ((*query_string != '?') && (*query_string != '\0'))
|
|
|
|
query_string++;
|
|
|
|
if (*query_string == '?')
|
|
|
|
{
|
|
|
|
*query_string = '\0';
|
|
|
|
query_string++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-08 19:32:23 +02:00
|
|
|
|
2022-08-27 16:57:44 +02:00
|
|
|
return strdup(query_string);
|
2018-02-04 19:46:47 +01:00
|
|
|
}
|
2021-02-24 18:12:36 +01:00
|
|
|
|
|
|
|
static void *proxy_monitor(void *data)
|
|
|
|
{
|
2022-08-27 16:57:44 +02:00
|
|
|
antd_request_t *rq = (antd_request_t *)data;
|
|
|
|
rq->client->state = ANTD_CLIENT_PROXY_MONITOR;
|
|
|
|
antd_client_t *proxy = (antd_client_t *)dvalue(rq->request, "PROXY_HANDLE");
|
|
|
|
antd_task_t *task = antd_create_task(NULL, data, NULL, rq->client->last_io);
|
|
|
|
int pret, ret, sz1 = 0, sz2 = 0;
|
|
|
|
char *buf = NULL;
|
|
|
|
buf = (char *)malloc(BUFFLEN);
|
|
|
|
struct pollfd pfd[1];
|
|
|
|
memset(pfd, 0, sizeof(pfd));
|
|
|
|
pfd[0].fd = proxy->sock;
|
|
|
|
pfd[0].events = POLLIN;
|
|
|
|
ret = 1;
|
2021-02-24 18:12:36 +01:00
|
|
|
|
2022-08-27 16:57:44 +02:00
|
|
|
do
|
|
|
|
{
|
|
|
|
sz1 = antd_recv_upto(rq->client, buf, BUFFLEN);
|
2021-02-24 18:12:36 +01:00
|
|
|
|
2022-08-27 16:57:44 +02:00
|
|
|
if ((sz1 < 0) || (sz1 > 0 && antd_send(proxy, buf, sz1) != sz1))
|
|
|
|
{
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pret = poll(pfd, 1, 0);
|
2022-08-27 21:42:56 +02:00
|
|
|
if (pret < 0)
|
2022-08-27 16:57:44 +02:00
|
|
|
{
|
|
|
|
(void)close(proxy->sock);
|
|
|
|
return task;
|
|
|
|
}
|
|
|
|
sz2 = 0;
|
2022-08-27 21:42:56 +02:00
|
|
|
if (pret > 0 && (pfd[0].revents & POLLIN))
|
2022-08-27 16:57:44 +02:00
|
|
|
{
|
|
|
|
sz2 = antd_recv_upto(proxy, buf, BUFFLEN);
|
|
|
|
if (sz2 <= 0 || (sz2 > 0 && antd_send(rq->client, buf, sz2) != sz2))
|
|
|
|
{
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-08-27 21:42:56 +02:00
|
|
|
if ((pret > 0) && (pfd[0].revents & POLLERR ||
|
|
|
|
pfd[0].revents & POLLRDHUP ||
|
|
|
|
pfd[0].revents & POLLHUP ||
|
|
|
|
pfd[0].revents & POLLNVAL))
|
2022-08-27 16:57:44 +02:00
|
|
|
{
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (sz1 > 0 || sz2 > 0);
|
|
|
|
free(buf);
|
2021-02-24 18:12:36 +01:00
|
|
|
|
2022-08-27 16:57:44 +02:00
|
|
|
if (ret == 0)
|
|
|
|
{
|
|
|
|
(void)close(proxy->sock);
|
|
|
|
return task;
|
|
|
|
}
|
2021-03-25 21:38:09 +01:00
|
|
|
|
2022-08-27 21:42:56 +02:00
|
|
|
if (pfd[0].revents & POLLIN)
|
2022-08-27 16:57:44 +02:00
|
|
|
{
|
|
|
|
antd_task_bind_event(task, proxy->sock, 0, TASK_EVT_ON_READABLE);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
antd_task_bind_event(task, proxy->sock, 50u, TASK_EVT_ON_TIMEOUT);
|
|
|
|
}
|
|
|
|
task->handle = proxy_monitor;
|
|
|
|
task->access_time = rq->client->last_io;
|
2022-08-27 21:42:56 +02:00
|
|
|
|
2022-08-27 16:57:44 +02:00
|
|
|
antd_task_bind_event(task, rq->client->sock, 0, TASK_EVT_ON_READABLE);
|
|
|
|
return task;
|
2021-02-24 18:12:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void *proxify(void *data)
|
|
|
|
{
|
2022-08-27 16:57:44 +02:00
|
|
|
int sock_fd, size, ret;
|
|
|
|
char *str = NULL;
|
|
|
|
chain_t it;
|
|
|
|
antd_request_t *rq = (antd_request_t *)data;
|
|
|
|
antd_client_t *proxy = NULL;
|
|
|
|
rq->client->state = ANTD_CLIENT_RESOLVE_REQUEST;
|
|
|
|
char *host = dvalue(rq->request, "PROXY_HOST");
|
|
|
|
int port = atoi(dvalue(rq->request, "PROXY_PORT"));
|
|
|
|
char *path = dvalue(rq->request, "PROXY_PATH");
|
|
|
|
char *query = dvalue(rq->request, "PROXY_QUERY");
|
2022-08-27 21:42:56 +02:00
|
|
|
char *ptr, *ip;
|
2022-08-27 16:57:44 +02:00
|
|
|
dictionary_t xheader = dvalue(rq->request, "REQUEST_HEADER");
|
|
|
|
antd_task_t *task = antd_create_task(NULL, data, NULL, rq->client->last_io);
|
|
|
|
if (!xheader)
|
|
|
|
{
|
|
|
|
antd_error(rq->client, 400, "Badd Request");
|
|
|
|
return task;
|
|
|
|
}
|
|
|
|
pthread_mutex_lock(&server_mux);
|
|
|
|
ip = NULL;
|
|
|
|
// ip_from_host is not threadsafe, need to lock it
|
|
|
|
ptr = ip_from_hostname(host);
|
2022-08-27 21:42:56 +02:00
|
|
|
if (ptr)
|
2022-08-27 16:57:44 +02:00
|
|
|
{
|
|
|
|
ip = strdup(ptr);
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&server_mux);
|
2021-02-24 18:12:36 +01:00
|
|
|
|
2022-08-27 21:42:56 +02:00
|
|
|
if (!ip)
|
2022-08-27 16:57:44 +02:00
|
|
|
{
|
|
|
|
antd_error(rq->client, 502, "Badd address");
|
|
|
|
return task;
|
|
|
|
}
|
2021-02-24 18:12:36 +01:00
|
|
|
|
2022-08-27 16:57:44 +02:00
|
|
|
sock_fd = request_socket(ip, port);
|
|
|
|
free(ip);
|
|
|
|
if (sock_fd == -1)
|
|
|
|
{
|
|
|
|
antd_error(rq->client, 503, "Service Unavailable");
|
|
|
|
return task;
|
|
|
|
}
|
|
|
|
set_nonblock(sock_fd);
|
|
|
|
/*struct timeval timeout;
|
|
|
|
timeout.tv_sec = 2;
|
|
|
|
timeout.tv_usec = 0; //POLL_EVENT_TO*1000;
|
|
|
|
if (setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)) < 0)
|
|
|
|
{
|
|
|
|
ERROR("setsockopt failed:%s", strerror(errno));
|
|
|
|
antd_error(rq->client, 500, "Internal proxy error");
|
|
|
|
(void)close(sock_fd);
|
|
|
|
return task;
|
|
|
|
}
|
2021-02-24 18:12:36 +01:00
|
|
|
|
2022-08-27 16:57:44 +02:00
|
|
|
if (setsockopt(sock_fd, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout)) < 0)
|
2021-02-24 18:12:36 +01:00
|
|
|
{
|
|
|
|
ERROR("setsockopt failed:%s", strerror(errno));
|
|
|
|
antd_error(rq->client, 500, "Internal proxy error");
|
|
|
|
(void)close(sock_fd);
|
|
|
|
return task;
|
|
|
|
}*/
|
|
|
|
|
2022-08-27 16:57:44 +02:00
|
|
|
proxy = (antd_client_t *)malloc(sizeof(antd_client_t));
|
|
|
|
proxy->sock = sock_fd;
|
|
|
|
proxy->ssl = NULL;
|
|
|
|
proxy->zstream = NULL;
|
|
|
|
proxy->z_level = ANTD_CNONE;
|
|
|
|
time(&proxy->last_io);
|
2021-02-24 18:12:36 +01:00
|
|
|
|
2022-08-27 16:57:44 +02:00
|
|
|
// store content length here
|
|
|
|
dput(rq->request, "PROXY_HANDLE", proxy);
|
2021-02-24 18:12:36 +01:00
|
|
|
|
2022-08-27 16:57:44 +02:00
|
|
|
str = __s("%s %s?%s HTTP/1.1\r\n", (char *)dvalue(rq->request, "METHOD"), path, query);
|
|
|
|
size = strlen(str);
|
|
|
|
ret = antd_send(proxy, str, size);
|
|
|
|
free(str);
|
|
|
|
if (ret != size)
|
|
|
|
{
|
|
|
|
antd_error(rq->client, 500, "");
|
|
|
|
(void)close(sock_fd);
|
|
|
|
return task;
|
|
|
|
}
|
|
|
|
for_each_assoc(it, xheader)
|
|
|
|
{
|
|
|
|
str = __s("%s: %s\r\n", it->key, (char *)it->value);
|
|
|
|
size = strlen(str);
|
|
|
|
ret = antd_send(proxy, str, size);
|
|
|
|
free(str);
|
|
|
|
if (ret != size)
|
|
|
|
{
|
|
|
|
antd_error(rq->client, 500, "");
|
|
|
|
(void)close(sock_fd);
|
|
|
|
return task;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(void)antd_send(proxy, "\r\n", 2);
|
|
|
|
// now monitor the proxy
|
|
|
|
task->handle = proxy_monitor;
|
|
|
|
task->access_time = rq->client->last_io;
|
|
|
|
// register event
|
|
|
|
antd_task_bind_event(task, proxy->sock, 0, TASK_EVT_ON_READABLE | TASK_EVT_ON_WRITABLE);
|
|
|
|
return task;
|
2021-02-24 18:12:36 +01:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Check if the current request is e reverse proxy
|
|
|
|
* return a proxy task if this is the case
|
2022-08-27 21:42:56 +02:00
|
|
|
*/
|
2021-02-24 18:12:36 +01:00
|
|
|
static void *check_proxy(antd_request_t *rq, const char *path, const char *query)
|
|
|
|
{
|
2022-08-27 16:57:44 +02:00
|
|
|
char *pattern = "^(https?)://([^:]+):([0-9]+)(.*)$";
|
|
|
|
antd_task_t *task = antd_create_task(NULL, (void *)rq, NULL, rq->client->last_io);
|
|
|
|
char buff[256];
|
|
|
|
regmatch_t matches[5];
|
|
|
|
int ret, size;
|
|
|
|
ret = regex_match(pattern, path, 5, matches);
|
2021-02-24 18:12:36 +01:00
|
|
|
|
2022-08-27 16:57:44 +02:00
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2021-02-24 18:12:36 +01:00
|
|
|
|
2022-08-27 16:57:44 +02:00
|
|
|
if (matches[1].rm_eo - matches[1].rm_so == 5)
|
|
|
|
{
|
|
|
|
// https is not supported for now
|
|
|
|
// TODO add https support
|
|
|
|
antd_error(rq->client, 503, "Service Unavailable");
|
|
|
|
return task;
|
|
|
|
}
|
|
|
|
// http proxy request
|
|
|
|
size = matches[2].rm_eo - matches[2].rm_so < (int)sizeof(buff) ? matches[2].rm_eo - matches[2].rm_so : (int)sizeof(buff);
|
|
|
|
(void)memcpy(buff, path + matches[2].rm_so, size);
|
|
|
|
buff[size] = '\0';
|
|
|
|
dput(rq->request, "PROXY_HOST", strdup(buff));
|
2021-02-24 18:12:36 +01:00
|
|
|
|
2022-08-27 16:57:44 +02:00
|
|
|
size = matches[3].rm_eo - matches[3].rm_so < (int)sizeof(buff) ? matches[3].rm_eo - matches[3].rm_so : (int)sizeof(buff);
|
|
|
|
(void)memcpy(buff, path + matches[3].rm_so, size);
|
|
|
|
buff[size] = '\0';
|
|
|
|
dput(rq->request, "PROXY_PORT", strdup(buff));
|
2021-02-24 18:12:36 +01:00
|
|
|
|
2022-08-27 16:57:44 +02:00
|
|
|
dput(rq->request, "PROXY_PATH", strdup(path + matches[4].rm_so));
|
|
|
|
dput(rq->request, "PROXY_QUERY", strdup(query));
|
2021-02-24 18:12:36 +01:00
|
|
|
|
2022-08-27 16:57:44 +02:00
|
|
|
task->handle = proxify;
|
|
|
|
antd_task_bind_event(task, rq->client->sock, 0, TASK_EVT_ON_READABLE | TASK_EVT_ON_WRITABLE);
|
|
|
|
return task;
|
2021-02-24 18:12:36 +01:00
|
|
|
}
|
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
|
|
|
{
|
2022-08-27 16:57:44 +02:00
|
|
|
antd_request_t *rq = (antd_request_t *)data;
|
|
|
|
rq->client->state = ANTD_CLIENT_HEADER_DECODE;
|
|
|
|
dictionary_t cookie = NULL;
|
|
|
|
char *line;
|
|
|
|
char *token;
|
|
|
|
char *query = NULL;
|
|
|
|
char *host = NULL;
|
|
|
|
char buf[2 * BUFFLEN];
|
|
|
|
int header_size = 0;
|
|
|
|
int ret;
|
|
|
|
char *url = (char *)dvalue(rq->request, "REQUEST_QUERY");
|
|
|
|
dictionary_t xheader = dvalue(rq->request, "REQUEST_HEADER");
|
|
|
|
dictionary_t request = dvalue(rq->request, "REQUEST_DATA");
|
|
|
|
char *port_s = (char *)dvalue(rq->request, "SERVER_PORT");
|
|
|
|
port_config_t *pcnf = (port_config_t *)dvalue(server_config.ports, port_s);
|
|
|
|
antd_task_t *task;
|
|
|
|
// first real all header
|
|
|
|
// this for check if web socket is enabled
|
2020-08-27 13:31:40 +02:00
|
|
|
|
2022-08-27 16:57:44 +02:00
|
|
|
while (((ret = read_buf(rq->client, buf, sizeof(buf))) > 0) && strcmp("\r\n", buf))
|
|
|
|
{
|
|
|
|
header_size += ret;
|
|
|
|
line = buf;
|
|
|
|
trim(line, '\n');
|
|
|
|
trim(line, '\r');
|
|
|
|
token = strsep(&line, ":");
|
|
|
|
trim(token, ' ');
|
|
|
|
trim(line, ' ');
|
|
|
|
if (token && line && strlen(line) > 0)
|
|
|
|
{
|
|
|
|
verify_header(token);
|
|
|
|
dput(xheader, token, strdup(line));
|
|
|
|
}
|
|
|
|
if (token != NULL && strcasecmp(token, "Cookie") == 0)
|
|
|
|
{
|
|
|
|
if (!cookie)
|
|
|
|
{
|
|
|
|
cookie = dict();
|
|
|
|
}
|
|
|
|
decode_cookie(line, cookie);
|
|
|
|
}
|
|
|
|
else if (token != NULL && strcasecmp(token, "Host") == 0)
|
|
|
|
{
|
|
|
|
host = strdup(line);
|
|
|
|
}
|
|
|
|
if (header_size > HEADER_MAX_SIZE)
|
|
|
|
{
|
|
|
|
antd_error(rq->client, 413, "Payload Too Large");
|
|
|
|
ERROR("Header size too large (%d): %d vs %d", rq->client->sock, header_size, HEADER_MAX_SIZE);
|
|
|
|
task = antd_create_task(NULL, (void *)rq, NULL, rq->client->last_io);
|
|
|
|
return task;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ret == 0)
|
|
|
|
{
|
2022-08-27 21:42:56 +02:00
|
|
|
// antd_task_bind_event(task, rq->client->sock, 0, TASK_EVT_ON_WRITABLE | TASK_EVT_ON_READABLE);
|
2022-08-27 16:57:44 +02:00
|
|
|
task = antd_create_task(decode_request_header, (void *)rq, NULL, rq->client->last_io);
|
|
|
|
antd_task_bind_event(task, rq->client->sock, 0, TASK_EVT_ON_READABLE);
|
|
|
|
return task;
|
|
|
|
}
|
|
|
|
// check for content length size
|
|
|
|
line = (char *)dvalue(xheader, "Content-Length");
|
|
|
|
if (line)
|
|
|
|
{
|
|
|
|
int clen = atoi(line);
|
|
|
|
if (clen > server_config.max_upload_size)
|
|
|
|
{
|
|
|
|
antd_error(rq->client, 413, "Request body data is too large");
|
|
|
|
// dirty fix, wait for message to be sent
|
|
|
|
// 100 ms sleep
|
|
|
|
usleep(100000);
|
|
|
|
task = antd_create_task(NULL, (void *)rq, NULL, rq->client->last_io);
|
|
|
|
return task;
|
|
|
|
}
|
|
|
|
}
|
2020-01-08 19:17:51 +01:00
|
|
|
|
|
|
|
#ifdef USE_ZLIB
|
2022-08-27 16:57:44 +02:00
|
|
|
// check for gzip
|
|
|
|
line = (char *)dvalue(xheader, "Accept-Encoding");
|
|
|
|
if (line)
|
|
|
|
{
|
|
|
|
if (regex_match("gzip", line, 0, NULL))
|
|
|
|
{
|
|
|
|
rq->client->z_level = ANTD_CGZ;
|
|
|
|
}
|
|
|
|
else if (regex_match("deflate", line, 0, NULL))
|
|
|
|
{
|
|
|
|
rq->client->z_level = ANTD_CDEFL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rq->client->z_level = ANTD_CNONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rq->client->z_level = ANTD_CNONE;
|
|
|
|
}
|
2020-01-08 19:17:51 +01:00
|
|
|
#endif
|
2022-08-27 21:42:56 +02:00
|
|
|
// if(line) free(line);
|
2022-08-27 16:57:44 +02:00
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
strncat(buf, url, sizeof(buf) - 1);
|
|
|
|
LOG("Original query (%d): %s", rq->client->sock, url);
|
|
|
|
query = apply_rules(pcnf->rules, host, buf);
|
|
|
|
LOG("Processed query: %s", query);
|
|
|
|
if (cookie)
|
|
|
|
dput(rq->request, "COOKIE", cookie);
|
|
|
|
if (host)
|
|
|
|
free(host);
|
2021-02-24 18:12:36 +01:00
|
|
|
|
2022-08-27 16:57:44 +02:00
|
|
|
// check if this is a reverse proxy ?
|
|
|
|
task = check_proxy(rq, buf, query);
|
|
|
|
if (task)
|
|
|
|
{
|
|
|
|
if (query)
|
|
|
|
free(query);
|
|
|
|
return task;
|
|
|
|
}
|
2021-02-24 18:12:36 +01:00
|
|
|
|
2022-08-27 16:57:44 +02:00
|
|
|
dput(rq->request, "RESOURCE_PATH", url_decode(buf));
|
|
|
|
if (query)
|
|
|
|
{
|
|
|
|
decode_url_request(query, request);
|
|
|
|
free(query);
|
|
|
|
}
|
|
|
|
// header ok, now checkmethod
|
|
|
|
task = antd_create_task(decode_request, (void *)rq, NULL, rq->client->last_io);
|
|
|
|
antd_task_bind_event(task, rq->client->sock, 0, TASK_EVT_ON_WRITABLE | TASK_EVT_ON_READABLE); //
|
|
|
|
return task;
|
2018-10-04 19:47:31 +02:00
|
|
|
}
|
|
|
|
|
2018-10-08 19:32:23 +02:00
|
|
|
void *decode_request(void *data)
|
2018-10-04 19:47:31 +02:00
|
|
|
{
|
2022-08-27 16:57:44 +02:00
|
|
|
antd_request_t *rq = (antd_request_t *)data;
|
|
|
|
dictionary_t 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, rq->client->last_io);
|
2022-08-27 21:42:56 +02:00
|
|
|
// antd_task_bind_event(task, rq->client->sock, 0, TASK_EVT_ON_WRITABLE | TASK_EVT_ON_READABLE);
|
2022-08-27 16:57:44 +02:00
|
|
|
if (EQU(method, "GET"))
|
|
|
|
{
|
2022-08-27 21:42:56 +02:00
|
|
|
// if(ctype) free(ctype);
|
2022-08-27 16:57:44 +02:00
|
|
|
if (ws && ws_key != NULL)
|
|
|
|
{
|
|
|
|
ws_confirm_request(rq->client, ws_key);
|
|
|
|
// insert wsocket flag to request
|
|
|
|
// plugin should handle this ugraded connection
|
|
|
|
// not the server
|
|
|
|
dput(rq->request, "__web_socket__", strdup("1"));
|
|
|
|
}
|
|
|
|
// resolve task
|
|
|
|
task->handle = resolve_request;
|
|
|
|
return task;
|
|
|
|
}
|
|
|
|
else if (EQU(method, "HEAD") || EQU(method, "OPTIONS") || EQU(method, "DELETE"))
|
|
|
|
{
|
|
|
|
task->handle = resolve_request;
|
|
|
|
return task;
|
|
|
|
}
|
|
|
|
else if (EQU(method, "POST") || EQU(method, "PUT") || EQU(method, "PATCH"))
|
|
|
|
{
|
|
|
|
task->handle = resolve_request;
|
|
|
|
return task;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
antd_error(rq->client, 501, "Request Method Not Implemented");
|
|
|
|
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
|
|
|
{
|
2022-08-27 16:57:44 +02:00
|
|
|
antd_request_t *rq = (antd_request_t *)data;
|
|
|
|
rq->client->state = ANTD_CLIENT_RQ_DATA_DECODE;
|
|
|
|
dictionary_t request = dvalue(rq->request, "REQUEST_DATA");
|
|
|
|
dictionary_t 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);
|
|
|
|
char *method = (char *)dvalue(rq->request, "METHOD");
|
|
|
|
task = antd_create_task(NULL, (void *)rq, NULL, rq->client->last_io);
|
2022-08-27 21:42:56 +02:00
|
|
|
// antd_task_bind_event(task, rq->client->sock, 0, TASK_EVT_ON_WRITABLE | TASK_EVT_ON_READABLE);
|
2022-08-27 16:57:44 +02:00
|
|
|
if (!method || (!EQU(method, "POST") && !EQU(method, "PUT") && !EQU(method, "PATCH")))
|
|
|
|
return task;
|
|
|
|
if (ctype == NULL || clen == -1)
|
|
|
|
{
|
|
|
|
antd_error(rq->client, 400, "Bad Request, missing content description");
|
|
|
|
return task;
|
|
|
|
}
|
|
|
|
// decide what to do with the data
|
|
|
|
if (strstr(ctype, FORM_URL_ENCODE))
|
|
|
|
{
|
|
|
|
char *pquery = post_data_decode(rq->client, clen);
|
|
|
|
if (pquery)
|
|
|
|
{
|
|
|
|
decode_url_request(pquery, request);
|
|
|
|
free(pquery);
|
|
|
|
}
|
|
|
|
else if (clen > 0)
|
|
|
|
{
|
|
|
|
// WARN: this may not work on ssl socket
|
|
|
|
// antd_task_bind_event(task, rq->client->sock, 0, TASK_EVT_ON_READABLE | TASK_EVT_ON_WRITABLE);
|
|
|
|
// task->handle = decode_post_request;
|
|
|
|
antd_error(rq->client, 400, "Bad Request, missing content data");
|
|
|
|
return task;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (strstr(ctype, FORM_MULTI_PART))
|
|
|
|
{
|
|
|
|
free(task);
|
|
|
|
return decode_multi_part_request(rq, ctype);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-08-27 21:42:56 +02:00
|
|
|
/*let plugin hande this data as we dont known how to deal with it*/
|
|
|
|
dput(request, "HAS_RAW_BODY", strdup("true"));
|
2022-08-27 16:57:44 +02:00
|
|
|
}
|
|
|
|
antd_task_bind_event(task, rq->client->sock, 0, TASK_EVT_ON_WRITABLE);
|
|
|
|
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
|
|
|
/**
|
2022-08-27 21:42:56 +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
|
|
|
{
|
2022-08-27 16:57:44 +02:00
|
|
|
char buf[256];
|
|
|
|
char rkey[128];
|
|
|
|
char sha_d[20];
|
|
|
|
char base64[64];
|
|
|
|
strncpy(rkey, key, sizeof(rkey) - 1);
|
|
|
|
int n = (int)sizeof(rkey) - (int)strlen(key);
|
|
|
|
if (n < 0)
|
|
|
|
n = 0;
|
|
|
|
strncat(rkey, WS_MAGIC_STRING, n);
|
2018-02-10 11:22:41 +01:00
|
|
|
#ifdef USE_OPENSSL
|
2022-08-27 16:57:44 +02:00
|
|
|
SHA_CTX context;
|
2018-02-10 11:22:41 +01:00
|
|
|
#else
|
2022-08-27 16:57:44 +02:00
|
|
|
SHA1_CTX context;
|
2018-02-10 11:22:41 +01:00
|
|
|
#endif
|
2018-10-08 19:32:23 +02:00
|
|
|
|
2022-08-27 16:57:44 +02:00
|
|
|
SHA1_Init(&context);
|
|
|
|
SHA1_Update(&context, rkey, strlen(rkey));
|
|
|
|
SHA1_Final((uint8_t *)sha_d, &context);
|
|
|
|
Base64encode(base64, sha_d, 20);
|
|
|
|
// send accept to client
|
|
|
|
sprintf(buf, "HTTP/1.1 101 Switching Protocols\r\n");
|
|
|
|
antd_send(client, buf, strlen(buf));
|
|
|
|
sprintf(buf, "Upgrade: websocket\r\n");
|
|
|
|
antd_send(client, buf, strlen(buf));
|
|
|
|
sprintf(buf, "Connection: Upgrade\r\n");
|
|
|
|
antd_send(client, buf, strlen(buf));
|
|
|
|
sprintf(buf, "Sec-WebSocket-Accept: %s\r\n", base64);
|
|
|
|
antd_send(client, buf, strlen(buf));
|
|
|
|
sprintf(buf, "\r\n");
|
|
|
|
antd_send(client, buf, strlen(buf));
|
2018-10-08 19:32:23 +02:00
|
|
|
|
2022-08-27 16:57:44 +02: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
|
|
|
|
*/
|
2020-01-08 19:17:51 +01:00
|
|
|
void decode_cookie(const char *line, dictionary_t dic)
|
2017-07-29 22:00:34 +02:00
|
|
|
{
|
2022-08-27 16:57:44 +02:00
|
|
|
char *token, *token1;
|
|
|
|
char *cpstr = strdup(line);
|
|
|
|
char *orgcpy = cpstr;
|
|
|
|
trim(cpstr, ' ');
|
|
|
|
trim(cpstr, '\n');
|
|
|
|
trim(cpstr, '\r');
|
2018-10-08 19:32:23 +02:00
|
|
|
|
2022-08-27 16:57:44 +02:00
|
|
|
while ((token = strsep(&cpstr, ";")))
|
|
|
|
{
|
|
|
|
trim(token, ' ');
|
|
|
|
token1 = strsep(&token, "=");
|
|
|
|
if (token1 && token && strlen(token) > 0)
|
|
|
|
{
|
|
|
|
dput(dic, token1, strdup(token));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(orgcpy);
|
2017-07-29 22:00:34 +02:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
{
|
2022-08-27 16:57:44 +02:00
|
|
|
char *boundary;
|
|
|
|
char line[BUFFLEN];
|
|
|
|
char *str_copy = (char *)ctype;
|
|
|
|
int len;
|
|
|
|
antd_request_t *rq = (antd_request_t *)data;
|
|
|
|
antd_task_t *task = antd_create_task(NULL, (void *)rq, NULL, rq->client->last_io);
|
2022-08-27 21:42:56 +02:00
|
|
|
// antd_task_bind_event(task, rq->client->sock, 0, );
|
2022-08-27 16:57:44 +02:00
|
|
|
antd_task_bind_event(task, rq->client->sock, 0, TASK_EVT_ON_WRITABLE | TASK_EVT_ON_READABLE);
|
2022-08-27 21:42:56 +02:00
|
|
|
// dictionary dic = NULL;
|
|
|
|
boundary = strsep(&str_copy, "="); // discard first part
|
2022-08-27 16:57:44 +02:00
|
|
|
boundary = str_copy;
|
|
|
|
if (boundary && strlen(boundary) > 0)
|
|
|
|
{
|
2022-08-27 21:42:56 +02:00
|
|
|
// dic = dict();
|
2022-08-27 16:57:44 +02:00
|
|
|
trim(boundary, ' ');
|
|
|
|
dput(rq->request, "MULTI_PART_BOUNDARY", strdup(boundary));
|
2022-08-27 21:42:56 +02:00
|
|
|
// find first boundary
|
2022-08-27 16:57:44 +02:00
|
|
|
while (((len = read_buf(rq->client, line, sizeof(line))) > 0) && !strstr(line, boundary))
|
|
|
|
;
|
|
|
|
if (len > 0)
|
|
|
|
{
|
|
|
|
task->handle = decode_multi_part_request_data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return task;
|
2018-10-05 19:01:39 +02:00
|
|
|
}
|
2018-10-08 19:32:23 +02:00
|
|
|
void *decode_multi_part_request_data(void *data)
|
2018-10-05 19:01:39 +02:00
|
|
|
{
|
2022-08-27 16:57:44 +02:00
|
|
|
// loop through each part separated by the boundary
|
|
|
|
char *line;
|
|
|
|
char *part_name = NULL;
|
|
|
|
char *part_file = NULL;
|
|
|
|
char *file_path;
|
|
|
|
char buf[BUFFLEN];
|
|
|
|
char *field;
|
|
|
|
int len;
|
2022-08-27 21:42:56 +02:00
|
|
|
// dictionary dic = NULL;
|
2022-08-27 16:57:44 +02:00
|
|
|
int fd = -1;
|
|
|
|
char *token, *keytoken, *valtoken;
|
|
|
|
antd_request_t *rq = (antd_request_t *)data;
|
|
|
|
antd_task_t *task = antd_create_task(NULL, (void *)rq, NULL, rq->client->last_io);
|
|
|
|
antd_task_bind_event(task, rq->client->sock, 0, TASK_EVT_ON_WRITABLE | TASK_EVT_ON_READABLE);
|
|
|
|
char *boundary = (char *)dvalue(rq->request, "MULTI_PART_BOUNDARY");
|
|
|
|
dictionary_t dic = (dictionary_t)dvalue(rq->request, "REQUEST_DATA");
|
|
|
|
// search for content disposition:
|
|
|
|
while (((len = read_buf(rq->client, buf, sizeof(buf))) > 0) && !strstr(buf, "Content-Disposition:"))
|
|
|
|
;
|
|
|
|
;
|
|
|
|
if (len <= 0 || !strstr(buf, "Content-Disposition:"))
|
|
|
|
{
|
|
|
|
return task;
|
|
|
|
}
|
|
|
|
char *boundend = __s("%s--", boundary);
|
|
|
|
line = buf;
|
|
|
|
// extract parameters from header
|
|
|
|
while ((token = strsep(&line, ";")))
|
|
|
|
{
|
|
|
|
keytoken = strsep(&token, "=");
|
|
|
|
if (keytoken && strlen(keytoken) > 0)
|
|
|
|
{
|
|
|
|
trim(keytoken, ' ');
|
|
|
|
valtoken = strsep(&token, "=");
|
|
|
|
if (valtoken)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
line = NULL;
|
|
|
|
// get the binary data
|
2022-08-27 21:42:56 +02:00
|
|
|
LOG("Part file: %s part name: %s", part_file, part_name);
|
2022-08-27 16:57:44 +02:00
|
|
|
if (part_name != NULL)
|
|
|
|
{
|
|
|
|
// go to the beginning of data bock
|
2022-08-27 21:42:56 +02:00
|
|
|
while ((len = read_buf(rq->client, buf, sizeof(buf))) > 0 && strncmp(buf, "\r\n", 2) != 0)
|
2022-08-27 16:57:44 +02:00
|
|
|
;
|
|
|
|
;
|
2019-12-22 14:33:42 +01:00
|
|
|
|
2022-08-27 16:57:44 +02:00
|
|
|
if (part_file == NULL)
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* WARNING:
|
|
|
|
* This allow only 1024 bytes of data (max),
|
|
|
|
* out of this range, the data is cut out.
|
|
|
|
* Need an efficient way to handle this
|
|
|
|
*/
|
|
|
|
len = read_buf(rq->client, buf, sizeof(buf));
|
|
|
|
if (len > 0)
|
|
|
|
{
|
|
|
|
line = buf;
|
|
|
|
trim(line, '\n');
|
|
|
|
trim(line, '\r');
|
|
|
|
trim(line, ' ');
|
|
|
|
dput(dic, part_name, strdup(line));
|
|
|
|
}
|
|
|
|
// find the next boundary
|
|
|
|
while ((len = read_buf(rq->client, buf, sizeof(buf))) > 0 && !strstr(buf, boundary))
|
|
|
|
{
|
|
|
|
line = buf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
file_path = __s("%s%s.%u", server_config.tmpdir, part_file, (unsigned)time(NULL));
|
|
|
|
fd = open(file_path, O_WRONLY | O_CREAT, 0600);
|
|
|
|
if (fd > 0)
|
|
|
|
{
|
|
|
|
int totalsize = 0, len = 0;
|
2022-08-27 21:42:56 +02:00
|
|
|
// read until the next boundary
|
|
|
|
// TODO: this is not efficient for big file
|
|
|
|
// need a solution
|
2022-08-27 16:57:44 +02:00
|
|
|
while ((len = read_buf(rq->client, buf, sizeof(buf))) > 0 && !strstr(buf, boundary))
|
|
|
|
{
|
|
|
|
len = guard_write(fd, buf, len);
|
|
|
|
totalsize += len;
|
|
|
|
}
|
2022-08-27 21:42:56 +02:00
|
|
|
|
|
|
|
// remove \r\n at the end
|
2022-08-27 16:57:44 +02:00
|
|
|
lseek(fd, 0, SEEK_SET);
|
2022-08-27 21:42:56 +02:00
|
|
|
// fseek(fp,-2, SEEK_CUR);
|
2022-08-27 16:57:44 +02:00
|
|
|
totalsize -= 2;
|
|
|
|
int stat = ftruncate(fd, totalsize);
|
2022-08-27 21:42:56 +02:00
|
|
|
LOG("Write %d bytes to %s", totalsize, file_path);
|
2022-08-27 16:57:44 +02:00
|
|
|
UNUSED(stat);
|
|
|
|
close(fd);
|
|
|
|
line = buf;
|
2017-07-29 22:00:34 +02:00
|
|
|
|
2022-08-27 16:57:44 +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);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ERROR("Cannot write file to :%s", file_path);
|
|
|
|
}
|
|
|
|
free(file_path);
|
|
|
|
free(part_file);
|
|
|
|
}
|
|
|
|
free(part_name);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* The upload procedure may take time, the task access time should be updated
|
|
|
|
* after the procedure finish
|
2022-08-27 21:42:56 +02:00
|
|
|
*/
|
2022-08-27 16:57:44 +02:00
|
|
|
task->access_time = rq->client->last_io;
|
|
|
|
// check if end of request
|
|
|
|
if (line && strstr(line, boundend))
|
|
|
|
{
|
2022-08-27 21:42:56 +02:00
|
|
|
// LOG("End request %s", boundend);
|
2022-08-27 16:57:44 +02:00
|
|
|
free(boundend);
|
2022-08-27 21:42:56 +02:00
|
|
|
// antd_task_bind_event(task, rq->client->sock, 0, TASK_EVT_ON_WRITABLE);
|
2022-08-27 16:57:44 +02:00
|
|
|
return task;
|
|
|
|
}
|
|
|
|
free(boundend);
|
|
|
|
if (line && strstr(line, boundary))
|
|
|
|
{
|
|
|
|
// continue upload
|
2022-08-27 21:42:56 +02:00
|
|
|
// antd_task_bind_event(task, rq->client->sock, 0, TASK_EVT_ON_READABLE);
|
2022-08-27 16:57:44 +02:00
|
|
|
task->handle = decode_multi_part_request_data;
|
|
|
|
return task;
|
|
|
|
}
|
2022-08-27 21:42:56 +02:00
|
|
|
// antd_task_bind_event(task, rq->client->sock, 0, TASK_EVT_ON_WRITABLE);
|
2022-08-27 16:57:44 +02:00
|
|
|
return task;
|
2017-07-29 22:00:34 +02:00
|
|
|
}
|
|
|
|
/**
|
2022-08-27 21:42:56 +02:00
|
|
|
* Decode a query string (GET request or POST URL encoded) to
|
2017-07-29 22:00:34 +02:00
|
|
|
* 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
|
|
|
{
|
2022-08-27 16:57:44 +02:00
|
|
|
if (query == NULL)
|
|
|
|
return;
|
2022-08-27 21:42:56 +02:00
|
|
|
// str_copy = ;
|
2022-08-27 16:57:44 +02:00
|
|
|
char *token;
|
|
|
|
if (strlen(query) == 0)
|
|
|
|
return;
|
|
|
|
char *str_copy = strdup(query);
|
|
|
|
char *org_copy = str_copy;
|
2022-08-27 21:42:56 +02:00
|
|
|
// dictionary dic = dict();
|
2022-08-27 16:57:44 +02:00
|
|
|
while ((token = strsep(&str_copy, "&")))
|
|
|
|
{
|
|
|
|
char *key;
|
|
|
|
char *val = NULL;
|
|
|
|
if (strlen(token) > 0)
|
|
|
|
{
|
|
|
|
key = strsep(&token, "=");
|
|
|
|
if (key && strlen(key) > 0)
|
|
|
|
{
|
|
|
|
val = strsep(&token, "=");
|
|
|
|
if (!val)
|
|
|
|
val = "";
|
|
|
|
dput(dic, key, url_decode(val));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(org_copy);
|
2022-08-27 21:42:56 +02:00
|
|
|
// return dic;
|
2017-07-29 22:00:34 +02:00
|
|
|
}
|
|
|
|
/**
|
2022-08-27 21:42:56 +02:00
|
|
|
* Decode post query string to string
|
|
|
|
*/
|
2018-10-08 19:32:23 +02:00
|
|
|
char *post_data_decode(void *client, int len)
|
2017-07-29 22:00:34 +02:00
|
|
|
{
|
2022-08-27 16:57:44 +02:00
|
|
|
char *query = (char *)malloc((len + 1) * sizeof(char));
|
|
|
|
char *ptr = query;
|
|
|
|
int readlen = len > BUFFLEN ? BUFFLEN : len;
|
|
|
|
int read = 0, stat = 1;
|
|
|
|
while (readlen > 0 && stat >= 0)
|
|
|
|
{
|
|
|
|
stat = antd_recv_upto(client, ptr + read, readlen);
|
|
|
|
if (stat > 0)
|
|
|
|
{
|
|
|
|
read += stat;
|
|
|
|
readlen = (len - read) > BUFFLEN ? BUFFLEN : (len - read);
|
|
|
|
}
|
|
|
|
if (stat == 0)
|
|
|
|
{
|
2022-08-27 21:42:56 +02:00
|
|
|
if (difftime(time(NULL), ((antd_client_t *)client)->last_io) > MAX_IO_WAIT_TIME)
|
2022-08-27 16:57:44 +02:00
|
|
|
{
|
|
|
|
stat = -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-08-27 21:42:56 +02:00
|
|
|
usleep(POLL_EVENT_TO * 1000);
|
2022-08-27 16:57:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-08 19:32:23 +02:00
|
|
|
|
2022-08-27 16:57:44 +02:00
|
|
|
if (read > 0)
|
|
|
|
query[read] = '\0';
|
|
|
|
else
|
|
|
|
{
|
|
|
|
free(query);
|
|
|
|
query = NULL;
|
|
|
|
}
|
|
|
|
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
|
2022-08-27 21:42:56 +02:00
|
|
|
* Second, decode all parameters necessary of the request and pass it
|
2017-07-29 22:00:34 +02:00
|
|
|
* 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
|
|
|
{
|
2022-08-27 16:57:44 +02:00
|
|
|
void *(*fn)(void *);
|
|
|
|
plugin_header_t *(*metafn)();
|
|
|
|
plugin_header_t *meta = NULL;
|
|
|
|
struct plugin_entry *plugin;
|
|
|
|
char *error;
|
|
|
|
antd_request_t *rq = (antd_request_t *)data;
|
|
|
|
antd_task_t *task = antd_create_task(NULL, (void *)rq, NULL, rq->client->last_io);
|
|
|
|
antd_task_bind_event(task, rq->client->sock, 0, TASK_EVT_ON_WRITABLE | TASK_EVT_ON_READABLE);
|
2022-08-27 21:42:56 +02:00
|
|
|
// LOG("Plugin name '%s'", pname);
|
2022-08-27 16:57:44 +02:00
|
|
|
rq->client->state = ANTD_CLIENT_PLUGIN_EXEC;
|
2022-08-27 21:42:56 +02:00
|
|
|
// load the plugin
|
2022-08-27 16:57:44 +02:00
|
|
|
if ((plugin = plugin_lookup((char *)pname)) == NULL)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(&server_mux);
|
|
|
|
plugin = plugin_load((char *)pname);
|
|
|
|
pthread_mutex_unlock(&server_mux);
|
|
|
|
if (plugin == NULL)
|
|
|
|
{
|
|
|
|
antd_error(rq->client, 503, "Requested service not found");
|
|
|
|
return task;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// check if the plugin want rawbody or decoded body
|
|
|
|
metafn = (plugin_header_t * (*)()) dlsym(plugin->handle, "meta");
|
|
|
|
if ((error = dlerror()) == NULL)
|
|
|
|
{
|
|
|
|
meta = metafn();
|
|
|
|
}
|
|
|
|
// load the function
|
|
|
|
fn = (void *(*)(void *))dlsym(plugin->handle, PLUGIN_HANDLER);
|
|
|
|
if ((error = dlerror()) != NULL)
|
|
|
|
{
|
|
|
|
ERROR("Problem when finding %s method from %s : %s", PLUGIN_HANDLER, pname, error);
|
|
|
|
antd_error(rq->client, 503, "Requested service not found");
|
|
|
|
return task;
|
|
|
|
}
|
|
|
|
// check if we need the raw data or not
|
|
|
|
if (meta && meta->raw_body == 1)
|
|
|
|
{
|
|
|
|
task->handle = fn;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
free(task);
|
|
|
|
task = antd_create_task(decode_post_request, (void *)rq, fn, rq->client->last_io);
|
|
|
|
}
|
|
|
|
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()
|
|
|
|
{
|
2022-08-27 16:57:44 +02:00
|
|
|
return server_config.mimes;
|
2020-01-08 19:17:51 +01:00
|
|
|
}
|
|
|
|
|
2020-08-27 13:31:40 +02:00
|
|
|
void dbdir(char *dest)
|
2020-01-08 19:17:51 +01:00
|
|
|
{
|
2022-08-27 16:57:44 +02:00
|
|
|
strncpy(dest, server_config.db_path, 512);
|
2020-01-08 19:17:51 +01:00
|
|
|
}
|
2020-08-27 13:31:40 +02:00
|
|
|
void tmpdir(char *dest)
|
2020-01-08 19:17:51 +01:00
|
|
|
{
|
2022-08-27 16:57:44 +02:00
|
|
|
strncpy(dest, server_config.tmpdir, 512);
|
2020-01-08 19:17:51 +01:00
|
|
|
}
|
2020-08-27 13:31:40 +02:00
|
|
|
void plugindir(char *dest)
|
2020-01-08 19:17:51 +01:00
|
|
|
{
|
2022-08-27 16:57:44 +02:00
|
|
|
strncpy(dest, server_config.plugins_dir, 512);
|
2020-01-08 19:17:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USE_ZLIB
|
2020-08-27 13:31:40 +02:00
|
|
|
int compressable(char *ctype)
|
2020-01-08 19:17:51 +01:00
|
|
|
{
|
2022-08-27 16:57:44 +02:00
|
|
|
if (!server_config.gzip_enable || server_config.gzip_types == NULL)
|
|
|
|
return 0;
|
|
|
|
item_t it;
|
|
|
|
list_for_each(it, server_config.gzip_types)
|
|
|
|
{
|
|
|
|
if (it->type == LIST_TYPE_POINTER && it->value.ptr && regex_match((const char *)it->value.ptr, ctype, 0, NULL))
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
2020-01-08 19:17:51 +01:00
|
|
|
}
|
2021-02-24 18:12:36 +01:00
|
|
|
#endif
|