1
0
mirror of https://github.com/lxsang/ant-http synced 2024-07-03 13:39:46 +02:00

request body can be decode directly or delegated to plugin

This commit is contained in:
lxsang 2018-10-08 19:32:23 +02:00
parent 6b51621f98
commit 03a0a9deea
6 changed files with 481 additions and 430 deletions

View File

@ -10,11 +10,16 @@ void destroy_config()
{
list_free(&(server_config.rules));
freedict(server_config.handlers);
if(server_config.plugins_dir) free(server_config.plugins_dir);
if(server_config.plugins_ext) free(server_config.plugins_ext);
if(server_config.db_path) free(server_config.db_path);
if(server_config.htdocs) free(server_config.htdocs);
if(server_config.tmpdir) free(server_config.tmpdir);
if (server_config.plugins_dir)
free(server_config.plugins_dir);
if (server_config.plugins_ext)
free(server_config.plugins_ext);
if (server_config.db_path)
free(server_config.db_path);
if (server_config.htdocs)
free(server_config.htdocs);
if (server_config.tmpdir)
free(server_config.tmpdir);
LOG("Unclosed connection: %d\n", server_config.connection);
}
@ -24,36 +29,53 @@ static int config_handler(void* conf, const char* section, const char* name,
{
config_t *pconfig = (config_t *)conf;
//char * ppath = NULL;
if (MATCH("SERVER", "port")) {
if (MATCH("SERVER", "port"))
{
pconfig->port = atoi(value);
} else if (MATCH("SERVER", "plugins")) {
}
else if (MATCH("SERVER", "plugins"))
{
pconfig->plugins_dir = strdup(value);
} else if (MATCH("SERVER", "plugins_ext")) {
}
else if (MATCH("SERVER", "plugins_ext"))
{
pconfig->plugins_ext = strdup(value);
} else if(MATCH("SERVER", "database")) {
}
else if (MATCH("SERVER", "database"))
{
pconfig->db_path = strdup(value);
} else if(MATCH("SERVER", "htdocs")) {
}
else if (MATCH("SERVER", "htdocs"))
{
pconfig->htdocs = strdup(value);
} else if(MATCH("SERVER", "tmpdir")) {
}
else if (MATCH("SERVER", "tmpdir"))
{
pconfig->tmpdir = strdup(value);
}
else if(MATCH("SERVER", "maxcon")) {
else if (MATCH("SERVER", "maxcon"))
{
pconfig->maxcon = atoi(value);
}
else if(MATCH("SERVER", "backlog")) {
else if (MATCH("SERVER", "backlog"))
{
pconfig->backlog = atoi(value);
}
else if(MATCH("SERVER", "workers")) {
else if (MATCH("SERVER", "workers"))
{
pconfig->n_workers = atoi(value);
}
#ifdef USE_OPENSSL
else if(MATCH("SERVER", "ssl.enable")) {
else if (MATCH("SERVER", "ssl.enable"))
{
pconfig->usessl = atoi(value);
}
else if(MATCH("SERVER", "ssl.cert")) {
else if (MATCH("SERVER", "ssl.cert"))
{
pconfig->sslcert = strdup(value);
}
else if(MATCH("SERVER", "ssl.key")) {
else if (MATCH("SERVER", "ssl.key"))
{
pconfig->sslkey = strdup(value);
}
#endif
@ -66,11 +88,14 @@ static int config_handler(void* conf, const char* section, const char* name,
{
dput(pconfig->handlers, name, strdup(value));
}
else if(strcmp(section,"AUTOSTART")==0){
else if (strcmp(section, "AUTOSTART") == 0)
{
// The server section must be added before the autostart section
// auto start plugin
plugin_load((char *)value);
} else {
}
else
{
return 0; /* unknown section/name, error */
}
return 1;
@ -90,7 +115,6 @@ void init_file_system()
{
removeAll(server_config.tmpdir, 0);
}
}
void load_config(const char *file)
{
@ -111,7 +135,8 @@ void load_config(const char* file)
server_config.sslcert = "cert.pem";
server_config.sslkey = "key.pem";
#endif
if (ini_parse(file, config_handler, &server_config) < 0) {
if (ini_parse(file, config_handler, &server_config) < 0)
{
LOG("Can't load '%s'\n. Used defaut configuration", file);
}
else
@ -126,10 +151,8 @@ void load_config(const char* file)
init_file_system();
}
void *accept_request(void *data)
{
int count;
char buf[BUFFLEN];
char *token = NULL;
char *line = NULL;
@ -166,7 +189,8 @@ void* accept_request(void* data)
int ret = -1, stat;
if (server_config.usessl == 1 && client->status == 0)
{
if (SSL_accept((SSL*)client->ssl) == -1) {
if (SSL_accept((SSL *)client->ssl) == -1)
{
stat = SSL_get_error((SSL *)client->ssl, ret);
switch (stat)
{
@ -197,8 +221,7 @@ void* accept_request(void* data)
}
#endif
server_config.connection++;
count = read_buf(rq->client, buf, sizeof(buf));
//LOG("count is %d\n", count);
read_buf(rq->client, buf, sizeof(buf));
line = buf;
// get the method string
token = strsep(&line, " ");
@ -250,14 +273,17 @@ void* resolve_request(void* data)
LOG("Path is : %s \n", path);
//if (path[strlen(path) - 1] == '/')
// strcat(path, "index.html");
if (stat(path, &st) == -1) {
if (stat(path, &st) == -1)
{
free(task);
rqp = strdup((char *)dvalue(rq->request, "REQUEST_PATH"));
oldrqp = rqp;
trim(rqp, '/');
newurl = strsep(&rqp, "/");
if(!rqp) rqp = strdup("/");
else rqp = strdup(rqp);
if (!rqp)
rqp = strdup("/");
else
rqp = strdup(rqp);
dput(rq->request, "RESOURCE_PATH", rqp);
task = execute_plugin(rq, newurl);
free(oldrqp);
@ -308,7 +334,8 @@ void* resolve_request(void* data)
{
char *ex = ext(path);
char *h = dvalue(server_config.handlers, ex);
if(ex) free(ex);
if (ex)
free(ex);
if (h)
{
//sprintf(path,"/%s%s",h,url);
@ -320,7 +347,6 @@ void* resolve_request(void* data)
}
else
unknow(rq->client);
}
else
{
@ -333,18 +359,22 @@ void* resolve_request(void* data)
void *finish_request(void *data)
{
if(!data) return NULL;
if (!data)
return NULL;
LOG("Close request\n");
antd_request_t *rq = (antd_request_t *)data;
// free all other thing
if (rq->request)
{
dictionary tmp = dvalue(rq->request, "COOKIE");
if(tmp) freedict(tmp);
if (tmp)
freedict(tmp);
tmp = dvalue(rq->request, "REQUEST_HEADER");
if(tmp) freedict(tmp);
if (tmp)
freedict(tmp);
tmp = dvalue(rq->request, "REQUEST_DATA");
if(tmp) freedict(tmp);
if (tmp)
freedict(tmp);
dput(rq->request, "REQUEST_HEADER", NULL);
dput(rq->request, "REQUEST_DATA", NULL);
dput(rq->request, "COOKIE", NULL);
@ -396,16 +426,20 @@ int rule_check(const char*k, const char* v, const char* host, const char* _url,
{
memcpy(buf + idx, url, strlen(url));
idx += strlen(url);
} else if(strcasecmp(rep,"query") == 0)
}
else if (strcasecmp(rep, "query") == 0)
{
memcpy(buf + idx, query, strlen(query));
idx += strlen(query);
} else if(match_int(rep))
}
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
}
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;
}
@ -494,7 +528,8 @@ char* apply_rules(const char* host, char*url)
k = list_at(server_config.rules, i)->value.s;
v = list_at(server_config.rules, i + 1)->value.s;
// 1 group
if(rule_check(k, v,host, url, query_string, url)){
if (rule_check(k, v, host, url, query_string, url))
{
query_string = url;
while ((*query_string != '?') && (*query_string != '\0'))
@ -545,7 +580,8 @@ void* decode_request_header(void* data)
dput(xheader, token, strdup(line));
if (token != NULL && strcasecmp(token, "Cookie") == 0)
{
if(!cookie) cookie = decode_cookie(line);
if (!cookie)
cookie = decode_cookie(line);
}
else if (token != NULL && strcasecmp(token, "Host") == 0)
{
@ -559,13 +595,13 @@ void* decode_request_header(void* data)
dput(rq->request, "RESOURCE_PATH", strdup(buf));
if (query)
{
LOG("Query: %s\n", query);
decode_url_request(query, request);
free(query);
}
if (cookie)
dput(rq->request, "COOKIE", cookie);
if(host) free(host);
if (host)
free(host);
// header ok, now checkmethod
antd_task_t *task = antd_create_task(decode_request, (void *)rq, NULL);
task->priority++;
@ -583,11 +619,12 @@ void* decode_request(void* data)
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;
if (tmp && strcasecmp(tmp, "websocket") == 0)
ws = 1;
method = (char *)dvalue(rq->request, "METHOD");
task = antd_create_task(NULL, (void *)rq, NULL);
task->priority++;
if(strcmp(method,"GET") == 0 || strcmp(method,"PUT") == 0)
if (strcmp(method, "GET") == 0 || strcmp(method, "HEAD") == 0)
{
//if(ctype) free(ctype);
if (ws && ws_key != NULL)
@ -604,8 +641,8 @@ void* decode_request(void* data)
}
else if (strcmp(method, "POST") == 0)
{
task->handle = decode_post_request;
task->type = HEAVY;
task->handle = resolve_request;
//task->type = HEAVY;
return task;
}
else
@ -628,25 +665,28 @@ void* decode_post_request(void* data)
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);
task->priority++;
if (!method || strcmp(method, "POST") != 0)
return task;
if (ctype == NULL || clen == -1)
{
LOG("Bad request\n");
badrequest(rq->client);
return task;
}
LOG("ContentType %s\n", ctype);
// decide what to do with the data
if(strstr(ctype,FORM_URL_ENCODE) > 0)
if (strstr(ctype, FORM_URL_ENCODE))
{
char *pquery = post_data_decode(rq->client, clen);
decode_url_request(pquery, request);
free(pquery);
} else if(strstr(ctype,FORM_MULTI_PART)> 0)
}
else if (strstr(ctype, FORM_MULTI_PART))
{
//printf("Multi part form : %s\n", ctype);
// TODO: split this to multiple task
free(task);
return decode_multi_part_request(rq, ctype);
}
@ -661,7 +701,6 @@ void* decode_post_request(void* data)
dput(request, key, strdup(pquery));
free(pquery);
}
task->handle = resolve_request;
return task;
}
@ -725,7 +764,8 @@ dictionary decode_cookie(const char* line)
token1 = strsep(&token, "=");
if (token1 && token && strlen(token) > 0)
{
if(dic == NULL) dic = dict();
if (dic == NULL)
dic = dict();
dput(dic, token1, strdup(token));
}
}
@ -754,9 +794,10 @@ void* decode_multi_part_request(void* data,const char* ctype)
trim(boundary, ' ');
dput(rq->request, "MULTI_PART_BOUNDARY", strdup(boundary));
//find first boundary
while((line = read_line(rq->client))&&strstr(line,boundary) <= 0)
while ((line = read_line(rq->client)) && !strstr(line, boundary))
{
if(line) free(line);
if (line)
free(line);
}
if (line)
{
@ -789,12 +830,12 @@ void* decode_multi_part_request_data(void* data)
char *boundend = __s("%s--", boundary);
// search for content disposition:
while ((line = read_line(rq->client)) &&
strstr(line,"Content-Disposition:") <= 0)
!strstr(line, "Content-Disposition:"))
{
free(line);
line = NULL;
}
if(!line || strstr(line,"Content-Disposition:") <= 0)
if (!line || !strstr(line, "Content-Disposition:"))
{
if (line)
free(line);
@ -819,7 +860,8 @@ void* decode_multi_part_request_data(void* data)
if (strcmp(keytoken, "name") == 0)
{
part_name = strdup(valtoken);
} else if(strcmp(keytoken,"filename") == 0)
}
else if (strcmp(keytoken, "filename") == 0)
{
part_file = strdup(valtoken);
}
@ -831,7 +873,7 @@ void* decode_multi_part_request_data(void* data)
// get the binary data
if (part_name != NULL)
{
// go to the beginer of data bock
// go to the beginning of data bock
while ((line = read_line(rq->client)) && strcmp(line, "\r\n") != 0)
{
free(line);
@ -855,7 +897,7 @@ void* decode_multi_part_request_data(void* data)
trim(line, ' ');
dput(dic, part_name, line);
// find the next boundary
while((line = read_line(rq->client)) && strstr(line,boundary) <= 0)
while ((line = read_line(rq->client)) && !strstr(line, boundary))
{
free(line);
line = NULL;
@ -871,7 +913,7 @@ void* decode_multi_part_request_data(void* data)
//read until the next boundary
// TODO: this is not efficient for big file
// need a solution
while((len = read_buf(rq->client,buf,sizeof(buf))) > 0 && strstr(buf,boundary) <= 0)
while ((len = read_buf(rq->client, buf, sizeof(buf))) > 0 && !strstr(buf, boundary))
{
fwrite(buf, len, 1, fp);
totalsize += len;
@ -896,7 +938,6 @@ void* decode_multi_part_request_data(void* data)
field = __s("%s.ext", part_name);
dput(dic, field, ext(part_file));
free(field);
}
else
{
@ -909,15 +950,14 @@ void* decode_multi_part_request_data(void* data)
}
//printf("[Lines]:%s\n",line);
// check if end of request
if(line&&strstr(line,boundend)>0)
if (line && strstr(line, boundend))
{
LOG("End request %s\n", boundend);
task->handle = resolve_request;
free(line);
free(boundend);
return task;
}
if(line && strstr(line,boundary) > 0)
if (line && strstr(line, boundary))
{
// continue upload
task->type = HEAVY;
@ -935,10 +975,12 @@ void* decode_multi_part_request_data(void* data)
*/
void decode_url_request(const char *query, dictionary dic)
{
if(query == NULL) return;
if (query == NULL)
return;
//str_copy = ;
char *token;
if(strlen(query) == 0) return;
if (strlen(query) == 0)
return;
char *str_copy = strdup(query);
char *org_copy = str_copy;
//dictionary dic = dict();
@ -1007,6 +1049,8 @@ char* post_data_decode(void* client,int len)
void *execute_plugin(void *data, const char *pname)
{
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;
@ -1026,6 +1070,12 @@ void* execute_plugin(void* data, const char *pname)
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)
@ -1034,8 +1084,18 @@ void* execute_plugin(void* data, const char *pname)
unknow(rq->client);
return task;
}
task->type = HEAVY;
// 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);
task->priority++;
}
task->type = HEAVY;
return task;
}

View File

@ -12,8 +12,6 @@
#include "libs/scheduler.h"
#include "plugin_manager.h"
#define FORM_URL_ENCODE "application/x-www-form-urlencoded"
#define FORM_MULTI_PART "multipart/form-data"
#define PLUGIN_HANDLER "handle"
#define WS_MAGIC_STRING "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
#define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0

17
httpd.c
View File

@ -145,10 +145,8 @@ int main(int argc, char* argv[])
client_ip = inet_ntoa(client_name.sin_addr);
client->ip = strdup(client_ip);
LOG("Client IP: %s\n", client_ip);
LOG("socket: %d\n", client_sock);
//LOG("socket: %d\n", client_sock);
}
//return &(((struct sockaddr_in6*)sa)->sin6_addr);
/* accept_request(client_sock); */
// set timeout to socket
set_nonblock(client_sock);
@ -163,8 +161,6 @@ int main(int argc, char* argv[])
perror("setsockopt failed\n");
*/
client->sock = client_sock;
// 100 times retry connection before abort
//LOG("Unclosed connection: %d\n", server_config->connection);
#ifdef USE_OPENSSL
client->ssl = NULL;
client->status = 0;
@ -184,17 +180,6 @@ int main(int argc, char* argv[])
#endif
// create callback for the server
antd_add_task(&scheduler, antd_create_task(accept_request,(void*)request, finish_request ));
/*if (pthread_create(&newthread , NULL,(void *(*)(void *))accept_request, (void *)client) != 0)
{
perror("pthread_create");
antd_close(client);
}
else
{
//reclaim the stack data when thread finish
pthread_detach(newthread) ;
}*/
//accept_request(&client);
}
close(server_sock);

View File

@ -24,7 +24,8 @@
#define R_FLOAT(d,k) ((double)atof(dvalue(d,k)))
#define R_PTR(d,k) (dvalue(d,k))
#define __RESULT__ "{\"result\":%d,\"msg\":\"%s\"}"
#define FORM_URL_ENCODE "application/x-www-form-urlencoded"
#define FORM_MULTI_PART "multipart/form-data"
#ifdef USE_OPENSSL
int __attribute__((weak)) usessl();
@ -64,6 +65,19 @@ typedef struct {
char* sslkey;
#endif
}config_t;
typedef struct {
char *name;
char *dbpath;
char * htdocs;
char*pdir;
int sport;
int raw_body;
#ifdef USE_OPENSSL
int usessl;
#endif
} plugin_header_t;
void set_nonblock(int socket);
//void set_block(int socket);
int response(void*, const char*);

View File

@ -11,9 +11,13 @@ void __init_plugin__(const char* pl,config_t* conf){
#ifdef USE_OPENSSL
__plugin__.usessl = conf->usessl;
#endif
__plugin__.raw_body = 0;
init();
};
void use_raw_body()
{
__plugin__.raw_body = 1;
}
#ifdef USE_DB
sqldb __getdb(char *name)
{

View File

@ -7,17 +7,6 @@
#include "ws.h"
#include "scheduler.h"
typedef struct {
char *name;
char *dbpath;
char * htdocs;
char*pdir;
int sport;
#ifdef USE_OPENSSL
int usessl;
#endif
} plugin_header_t;
//typedef void(*call)();
@ -40,4 +29,5 @@ void init();
void destroy();
void* handle(void*);
plugin_header_t* meta();
void use_raw_body();
#endif