mirror of
https://github.com/lxsang/ant-http
synced 2024-07-05 22:19:47 +02:00
request body can be decode directly or delegated to plugin
This commit is contained in:
parent
6b51621f98
commit
03a0a9deea
194
http_server.c
194
http_server.c
|
@ -10,11 +10,16 @@ void destroy_config()
|
||||||
{
|
{
|
||||||
list_free(&(server_config.rules));
|
list_free(&(server_config.rules));
|
||||||
freedict(server_config.handlers);
|
freedict(server_config.handlers);
|
||||||
if(server_config.plugins_dir) free(server_config.plugins_dir);
|
if (server_config.plugins_dir)
|
||||||
if(server_config.plugins_ext) free(server_config.plugins_ext);
|
free(server_config.plugins_dir);
|
||||||
if(server_config.db_path) free(server_config.db_path);
|
if (server_config.plugins_ext)
|
||||||
if(server_config.htdocs) free(server_config.htdocs);
|
free(server_config.plugins_ext);
|
||||||
if(server_config.tmpdir) free(server_config.tmpdir);
|
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);
|
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;
|
config_t *pconfig = (config_t *)conf;
|
||||||
//char * ppath = NULL;
|
//char * ppath = NULL;
|
||||||
if (MATCH("SERVER", "port")) {
|
if (MATCH("SERVER", "port"))
|
||||||
|
{
|
||||||
pconfig->port = atoi(value);
|
pconfig->port = atoi(value);
|
||||||
} else if (MATCH("SERVER", "plugins")) {
|
}
|
||||||
|
else if (MATCH("SERVER", "plugins"))
|
||||||
|
{
|
||||||
pconfig->plugins_dir = strdup(value);
|
pconfig->plugins_dir = strdup(value);
|
||||||
} else if (MATCH("SERVER", "plugins_ext")) {
|
}
|
||||||
|
else if (MATCH("SERVER", "plugins_ext"))
|
||||||
|
{
|
||||||
pconfig->plugins_ext = strdup(value);
|
pconfig->plugins_ext = strdup(value);
|
||||||
} else if(MATCH("SERVER", "database")) {
|
}
|
||||||
|
else if (MATCH("SERVER", "database"))
|
||||||
|
{
|
||||||
pconfig->db_path = strdup(value);
|
pconfig->db_path = strdup(value);
|
||||||
} else if(MATCH("SERVER", "htdocs")) {
|
}
|
||||||
|
else if (MATCH("SERVER", "htdocs"))
|
||||||
|
{
|
||||||
pconfig->htdocs = strdup(value);
|
pconfig->htdocs = strdup(value);
|
||||||
} else if(MATCH("SERVER", "tmpdir")) {
|
}
|
||||||
|
else if (MATCH("SERVER", "tmpdir"))
|
||||||
|
{
|
||||||
pconfig->tmpdir = strdup(value);
|
pconfig->tmpdir = strdup(value);
|
||||||
}
|
}
|
||||||
else if(MATCH("SERVER", "maxcon")) {
|
else if (MATCH("SERVER", "maxcon"))
|
||||||
|
{
|
||||||
pconfig->maxcon = atoi(value);
|
pconfig->maxcon = atoi(value);
|
||||||
}
|
}
|
||||||
else if(MATCH("SERVER", "backlog")) {
|
else if (MATCH("SERVER", "backlog"))
|
||||||
|
{
|
||||||
pconfig->backlog = atoi(value);
|
pconfig->backlog = atoi(value);
|
||||||
}
|
}
|
||||||
else if(MATCH("SERVER", "workers")) {
|
else if (MATCH("SERVER", "workers"))
|
||||||
|
{
|
||||||
pconfig->n_workers = atoi(value);
|
pconfig->n_workers = atoi(value);
|
||||||
}
|
}
|
||||||
#ifdef USE_OPENSSL
|
#ifdef USE_OPENSSL
|
||||||
else if(MATCH("SERVER", "ssl.enable")) {
|
else if (MATCH("SERVER", "ssl.enable"))
|
||||||
|
{
|
||||||
pconfig->usessl = atoi(value);
|
pconfig->usessl = atoi(value);
|
||||||
}
|
}
|
||||||
else if(MATCH("SERVER", "ssl.cert")) {
|
else if (MATCH("SERVER", "ssl.cert"))
|
||||||
|
{
|
||||||
pconfig->sslcert = strdup(value);
|
pconfig->sslcert = strdup(value);
|
||||||
}
|
}
|
||||||
else if(MATCH("SERVER", "ssl.key")) {
|
else if (MATCH("SERVER", "ssl.key"))
|
||||||
|
{
|
||||||
pconfig->sslkey = strdup(value);
|
pconfig->sslkey = strdup(value);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -66,11 +88,14 @@ static int config_handler(void* conf, const char* section, const char* name,
|
||||||
{
|
{
|
||||||
dput(pconfig->handlers, name, strdup(value));
|
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
|
// The server section must be added before the autostart section
|
||||||
// auto start plugin
|
// auto start plugin
|
||||||
plugin_load((char *)value);
|
plugin_load((char *)value);
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
return 0; /* unknown section/name, error */
|
return 0; /* unknown section/name, error */
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
|
@ -90,7 +115,6 @@ void init_file_system()
|
||||||
{
|
{
|
||||||
removeAll(server_config.tmpdir, 0);
|
removeAll(server_config.tmpdir, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
void load_config(const char *file)
|
void load_config(const char *file)
|
||||||
{
|
{
|
||||||
|
@ -111,7 +135,8 @@ void load_config(const char* file)
|
||||||
server_config.sslcert = "cert.pem";
|
server_config.sslcert = "cert.pem";
|
||||||
server_config.sslkey = "key.pem";
|
server_config.sslkey = "key.pem";
|
||||||
#endif
|
#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);
|
LOG("Can't load '%s'\n. Used defaut configuration", file);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -126,10 +151,8 @@ void load_config(const char* file)
|
||||||
init_file_system();
|
init_file_system();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void *accept_request(void *data)
|
void *accept_request(void *data)
|
||||||
{
|
{
|
||||||
int count;
|
|
||||||
char buf[BUFFLEN];
|
char buf[BUFFLEN];
|
||||||
char *token = NULL;
|
char *token = NULL;
|
||||||
char *line = NULL;
|
char *line = NULL;
|
||||||
|
@ -166,7 +189,8 @@ void* accept_request(void* data)
|
||||||
int ret = -1, stat;
|
int ret = -1, stat;
|
||||||
if (server_config.usessl == 1 && client->status == 0)
|
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);
|
stat = SSL_get_error((SSL *)client->ssl, ret);
|
||||||
switch (stat)
|
switch (stat)
|
||||||
{
|
{
|
||||||
|
@ -197,8 +221,7 @@ void* accept_request(void* data)
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
server_config.connection++;
|
server_config.connection++;
|
||||||
count = read_buf(rq->client, buf, sizeof(buf));
|
read_buf(rq->client, buf, sizeof(buf));
|
||||||
//LOG("count is %d\n", count);
|
|
||||||
line = buf;
|
line = buf;
|
||||||
// get the method string
|
// get the method string
|
||||||
token = strsep(&line, " ");
|
token = strsep(&line, " ");
|
||||||
|
@ -250,14 +273,17 @@ void* resolve_request(void* data)
|
||||||
LOG("Path is : %s \n", path);
|
LOG("Path is : %s \n", path);
|
||||||
//if (path[strlen(path) - 1] == '/')
|
//if (path[strlen(path) - 1] == '/')
|
||||||
// strcat(path, "index.html");
|
// strcat(path, "index.html");
|
||||||
if (stat(path, &st) == -1) {
|
if (stat(path, &st) == -1)
|
||||||
|
{
|
||||||
free(task);
|
free(task);
|
||||||
rqp = strdup((char *)dvalue(rq->request, "REQUEST_PATH"));
|
rqp = strdup((char *)dvalue(rq->request, "REQUEST_PATH"));
|
||||||
oldrqp = rqp;
|
oldrqp = rqp;
|
||||||
trim(rqp, '/');
|
trim(rqp, '/');
|
||||||
newurl = strsep(&rqp, "/");
|
newurl = strsep(&rqp, "/");
|
||||||
if(!rqp) rqp = strdup("/");
|
if (!rqp)
|
||||||
else rqp = strdup(rqp);
|
rqp = strdup("/");
|
||||||
|
else
|
||||||
|
rqp = strdup(rqp);
|
||||||
dput(rq->request, "RESOURCE_PATH", rqp);
|
dput(rq->request, "RESOURCE_PATH", rqp);
|
||||||
task = execute_plugin(rq, newurl);
|
task = execute_plugin(rq, newurl);
|
||||||
free(oldrqp);
|
free(oldrqp);
|
||||||
|
@ -308,7 +334,8 @@ void* resolve_request(void* data)
|
||||||
{
|
{
|
||||||
char *ex = ext(path);
|
char *ex = ext(path);
|
||||||
char *h = dvalue(server_config.handlers, ex);
|
char *h = dvalue(server_config.handlers, ex);
|
||||||
if(ex) free(ex);
|
if (ex)
|
||||||
|
free(ex);
|
||||||
if (h)
|
if (h)
|
||||||
{
|
{
|
||||||
//sprintf(path,"/%s%s",h,url);
|
//sprintf(path,"/%s%s",h,url);
|
||||||
|
@ -320,7 +347,6 @@ void* resolve_request(void* data)
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
unknow(rq->client);
|
unknow(rq->client);
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -333,18 +359,22 @@ void* resolve_request(void* data)
|
||||||
|
|
||||||
void *finish_request(void *data)
|
void *finish_request(void *data)
|
||||||
{
|
{
|
||||||
if(!data) return NULL;
|
if (!data)
|
||||||
|
return NULL;
|
||||||
LOG("Close request\n");
|
LOG("Close request\n");
|
||||||
antd_request_t *rq = (antd_request_t *)data;
|
antd_request_t *rq = (antd_request_t *)data;
|
||||||
// free all other thing
|
// free all other thing
|
||||||
if (rq->request)
|
if (rq->request)
|
||||||
{
|
{
|
||||||
dictionary tmp = dvalue(rq->request, "COOKIE");
|
dictionary tmp = dvalue(rq->request, "COOKIE");
|
||||||
if(tmp) freedict(tmp);
|
if (tmp)
|
||||||
|
freedict(tmp);
|
||||||
tmp = dvalue(rq->request, "REQUEST_HEADER");
|
tmp = dvalue(rq->request, "REQUEST_HEADER");
|
||||||
if(tmp) freedict(tmp);
|
if (tmp)
|
||||||
|
freedict(tmp);
|
||||||
tmp = dvalue(rq->request, "REQUEST_DATA");
|
tmp = dvalue(rq->request, "REQUEST_DATA");
|
||||||
if(tmp) freedict(tmp);
|
if (tmp)
|
||||||
|
freedict(tmp);
|
||||||
dput(rq->request, "REQUEST_HEADER", NULL);
|
dput(rq->request, "REQUEST_HEADER", NULL);
|
||||||
dput(rq->request, "REQUEST_DATA", NULL);
|
dput(rq->request, "REQUEST_DATA", NULL);
|
||||||
dput(rq->request, "COOKIE", 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));
|
memcpy(buf + idx, url, strlen(url));
|
||||||
idx += strlen(url);
|
idx += strlen(url);
|
||||||
} else if(strcasecmp(rep,"query") == 0)
|
}
|
||||||
|
else if (strcasecmp(rep, "query") == 0)
|
||||||
{
|
{
|
||||||
memcpy(buf + idx, query, strlen(query));
|
memcpy(buf + idx, query, strlen(query));
|
||||||
idx += strlen(query);
|
idx += strlen(query);
|
||||||
} else if(match_int(rep))
|
}
|
||||||
|
else if (match_int(rep))
|
||||||
{
|
{
|
||||||
int i = atoi(rep);
|
int i = atoi(rep);
|
||||||
memcpy(buf + idx, target + key_matches[i].rm_so, key_matches[i].rm_eo - key_matches[i].rm_so);
|
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;
|
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);
|
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;
|
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;
|
k = list_at(server_config.rules, i)->value.s;
|
||||||
v = list_at(server_config.rules, i + 1)->value.s;
|
v = list_at(server_config.rules, i + 1)->value.s;
|
||||||
// 1 group
|
// 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;
|
query_string = url;
|
||||||
|
|
||||||
while ((*query_string != '?') && (*query_string != '\0'))
|
while ((*query_string != '?') && (*query_string != '\0'))
|
||||||
|
@ -545,7 +580,8 @@ void* decode_request_header(void* data)
|
||||||
dput(xheader, token, strdup(line));
|
dput(xheader, token, strdup(line));
|
||||||
if (token != NULL && strcasecmp(token, "Cookie") == 0)
|
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)
|
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));
|
dput(rq->request, "RESOURCE_PATH", strdup(buf));
|
||||||
if (query)
|
if (query)
|
||||||
{
|
{
|
||||||
LOG("Query: %s\n", query);
|
|
||||||
decode_url_request(query, request);
|
decode_url_request(query, request);
|
||||||
free(query);
|
free(query);
|
||||||
}
|
}
|
||||||
if (cookie)
|
if (cookie)
|
||||||
dput(rq->request, "COOKIE", cookie);
|
dput(rq->request, "COOKIE", cookie);
|
||||||
if(host) free(host);
|
if (host)
|
||||||
|
free(host);
|
||||||
// header ok, now checkmethod
|
// header ok, now checkmethod
|
||||||
antd_task_t *task = antd_create_task(decode_request, (void *)rq, NULL);
|
antd_task_t *task = antd_create_task(decode_request, (void *)rq, NULL);
|
||||||
task->priority++;
|
task->priority++;
|
||||||
|
@ -583,11 +619,12 @@ void* decode_request(void* data)
|
||||||
antd_task_t *task = NULL;
|
antd_task_t *task = NULL;
|
||||||
ws_key = (char *)dvalue(headers, "Sec-WebSocket-Key");
|
ws_key = (char *)dvalue(headers, "Sec-WebSocket-Key");
|
||||||
tmp = (char *)dvalue(headers, "Upgrade");
|
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");
|
method = (char *)dvalue(rq->request, "METHOD");
|
||||||
task = antd_create_task(NULL, (void *)rq, NULL);
|
task = antd_create_task(NULL, (void *)rq, NULL);
|
||||||
task->priority++;
|
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(ctype) free(ctype);
|
||||||
if (ws && ws_key != NULL)
|
if (ws && ws_key != NULL)
|
||||||
|
@ -604,8 +641,8 @@ void* decode_request(void* data)
|
||||||
}
|
}
|
||||||
else if (strcmp(method, "POST") == 0)
|
else if (strcmp(method, "POST") == 0)
|
||||||
{
|
{
|
||||||
task->handle = decode_post_request;
|
task->handle = resolve_request;
|
||||||
task->type = HEAVY;
|
//task->type = HEAVY;
|
||||||
return task;
|
return task;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -628,25 +665,28 @@ void* decode_post_request(void* data)
|
||||||
tmp = (char *)dvalue(headers, "Content-Length");
|
tmp = (char *)dvalue(headers, "Content-Length");
|
||||||
if (tmp)
|
if (tmp)
|
||||||
clen = atoi(tmp);
|
clen = atoi(tmp);
|
||||||
|
char *method = (char *)dvalue(rq->request, "METHOD");
|
||||||
task = antd_create_task(NULL, (void *)rq, NULL);
|
task = antd_create_task(NULL, (void *)rq, NULL);
|
||||||
task->priority++;
|
task->priority++;
|
||||||
|
if (!method || strcmp(method, "POST") != 0)
|
||||||
|
return task;
|
||||||
|
|
||||||
if (ctype == NULL || clen == -1)
|
if (ctype == NULL || clen == -1)
|
||||||
{
|
{
|
||||||
LOG("Bad request\n");
|
LOG("Bad request\n");
|
||||||
badrequest(rq->client);
|
badrequest(rq->client);
|
||||||
return task;
|
return task;
|
||||||
}
|
}
|
||||||
LOG("ContentType %s\n", ctype);
|
|
||||||
// decide what to do with the data
|
// 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);
|
char *pquery = post_data_decode(rq->client, clen);
|
||||||
decode_url_request(pquery, request);
|
decode_url_request(pquery, request);
|
||||||
free(pquery);
|
free(pquery);
|
||||||
} else if(strstr(ctype,FORM_MULTI_PART)> 0)
|
}
|
||||||
|
else if (strstr(ctype, FORM_MULTI_PART))
|
||||||
{
|
{
|
||||||
//printf("Multi part form : %s\n", ctype);
|
//printf("Multi part form : %s\n", ctype);
|
||||||
// TODO: split this to multiple task
|
|
||||||
free(task);
|
free(task);
|
||||||
return decode_multi_part_request(rq, ctype);
|
return decode_multi_part_request(rq, ctype);
|
||||||
}
|
}
|
||||||
|
@ -661,7 +701,6 @@ void* decode_post_request(void* data)
|
||||||
dput(request, key, strdup(pquery));
|
dput(request, key, strdup(pquery));
|
||||||
free(pquery);
|
free(pquery);
|
||||||
}
|
}
|
||||||
task->handle = resolve_request;
|
|
||||||
return task;
|
return task;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -725,7 +764,8 @@ dictionary decode_cookie(const char* line)
|
||||||
token1 = strsep(&token, "=");
|
token1 = strsep(&token, "=");
|
||||||
if (token1 && token && strlen(token) > 0)
|
if (token1 && token && strlen(token) > 0)
|
||||||
{
|
{
|
||||||
if(dic == NULL) dic = dict();
|
if (dic == NULL)
|
||||||
|
dic = dict();
|
||||||
dput(dic, token1, strdup(token));
|
dput(dic, token1, strdup(token));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -754,9 +794,10 @@ void* decode_multi_part_request(void* data,const char* ctype)
|
||||||
trim(boundary, ' ');
|
trim(boundary, ' ');
|
||||||
dput(rq->request, "MULTI_PART_BOUNDARY", strdup(boundary));
|
dput(rq->request, "MULTI_PART_BOUNDARY", strdup(boundary));
|
||||||
//find first 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)
|
if (line)
|
||||||
{
|
{
|
||||||
|
@ -789,12 +830,12 @@ void* decode_multi_part_request_data(void* data)
|
||||||
char *boundend = __s("%s--", boundary);
|
char *boundend = __s("%s--", boundary);
|
||||||
// search for content disposition:
|
// search for content disposition:
|
||||||
while ((line = read_line(rq->client)) &&
|
while ((line = read_line(rq->client)) &&
|
||||||
strstr(line,"Content-Disposition:") <= 0)
|
!strstr(line, "Content-Disposition:"))
|
||||||
{
|
{
|
||||||
free(line);
|
free(line);
|
||||||
line = NULL;
|
line = NULL;
|
||||||
}
|
}
|
||||||
if(!line || strstr(line,"Content-Disposition:") <= 0)
|
if (!line || !strstr(line, "Content-Disposition:"))
|
||||||
{
|
{
|
||||||
if (line)
|
if (line)
|
||||||
free(line);
|
free(line);
|
||||||
|
@ -819,7 +860,8 @@ void* decode_multi_part_request_data(void* data)
|
||||||
if (strcmp(keytoken, "name") == 0)
|
if (strcmp(keytoken, "name") == 0)
|
||||||
{
|
{
|
||||||
part_name = strdup(valtoken);
|
part_name = strdup(valtoken);
|
||||||
} else if(strcmp(keytoken,"filename") == 0)
|
}
|
||||||
|
else if (strcmp(keytoken, "filename") == 0)
|
||||||
{
|
{
|
||||||
part_file = strdup(valtoken);
|
part_file = strdup(valtoken);
|
||||||
}
|
}
|
||||||
|
@ -831,7 +873,7 @@ void* decode_multi_part_request_data(void* data)
|
||||||
// get the binary data
|
// get the binary data
|
||||||
if (part_name != NULL)
|
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)
|
while ((line = read_line(rq->client)) && strcmp(line, "\r\n") != 0)
|
||||||
{
|
{
|
||||||
free(line);
|
free(line);
|
||||||
|
@ -855,7 +897,7 @@ void* decode_multi_part_request_data(void* data)
|
||||||
trim(line, ' ');
|
trim(line, ' ');
|
||||||
dput(dic, part_name, line);
|
dput(dic, part_name, line);
|
||||||
// find the next boundary
|
// 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);
|
free(line);
|
||||||
line = NULL;
|
line = NULL;
|
||||||
|
@ -871,7 +913,7 @@ void* decode_multi_part_request_data(void* data)
|
||||||
//read until the next boundary
|
//read until the next boundary
|
||||||
// TODO: this is not efficient for big file
|
// TODO: this is not efficient for big file
|
||||||
// need a solution
|
// 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);
|
fwrite(buf, len, 1, fp);
|
||||||
totalsize += len;
|
totalsize += len;
|
||||||
|
@ -896,7 +938,6 @@ void* decode_multi_part_request_data(void* data)
|
||||||
field = __s("%s.ext", part_name);
|
field = __s("%s.ext", part_name);
|
||||||
dput(dic, field, ext(part_file));
|
dput(dic, field, ext(part_file));
|
||||||
free(field);
|
free(field);
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -909,15 +950,14 @@ void* decode_multi_part_request_data(void* data)
|
||||||
}
|
}
|
||||||
//printf("[Lines]:%s\n",line);
|
//printf("[Lines]:%s\n",line);
|
||||||
// check if end of request
|
// check if end of request
|
||||||
if(line&&strstr(line,boundend)>0)
|
if (line && strstr(line, boundend))
|
||||||
{
|
{
|
||||||
LOG("End request %s\n", boundend);
|
LOG("End request %s\n", boundend);
|
||||||
task->handle = resolve_request;
|
|
||||||
free(line);
|
free(line);
|
||||||
free(boundend);
|
free(boundend);
|
||||||
return task;
|
return task;
|
||||||
}
|
}
|
||||||
if(line && strstr(line,boundary) > 0)
|
if (line && strstr(line, boundary))
|
||||||
{
|
{
|
||||||
// continue upload
|
// continue upload
|
||||||
task->type = HEAVY;
|
task->type = HEAVY;
|
||||||
|
@ -935,10 +975,12 @@ void* decode_multi_part_request_data(void* data)
|
||||||
*/
|
*/
|
||||||
void decode_url_request(const char *query, dictionary dic)
|
void decode_url_request(const char *query, dictionary dic)
|
||||||
{
|
{
|
||||||
if(query == NULL) return;
|
if (query == NULL)
|
||||||
|
return;
|
||||||
//str_copy = ;
|
//str_copy = ;
|
||||||
char *token;
|
char *token;
|
||||||
if(strlen(query) == 0) return;
|
if (strlen(query) == 0)
|
||||||
|
return;
|
||||||
char *str_copy = strdup(query);
|
char *str_copy = strdup(query);
|
||||||
char *org_copy = str_copy;
|
char *org_copy = str_copy;
|
||||||
//dictionary dic = dict();
|
//dictionary dic = dict();
|
||||||
|
@ -1007,6 +1049,8 @@ char* post_data_decode(void* client,int len)
|
||||||
void *execute_plugin(void *data, const char *pname)
|
void *execute_plugin(void *data, const char *pname)
|
||||||
{
|
{
|
||||||
void *(*fn)(void *);
|
void *(*fn)(void *);
|
||||||
|
plugin_header_t *(*metafn)();
|
||||||
|
plugin_header_t *meta = NULL;
|
||||||
struct plugin_entry *plugin;
|
struct plugin_entry *plugin;
|
||||||
char *error;
|
char *error;
|
||||||
antd_request_t *rq = (antd_request_t *)data;
|
antd_request_t *rq = (antd_request_t *)data;
|
||||||
|
@ -1026,6 +1070,12 @@ void* execute_plugin(void* data, const char *pname)
|
||||||
return task;
|
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
|
// load the function
|
||||||
fn = (void *(*)(void *))dlsym(plugin->handle, PLUGIN_HANDLER);
|
fn = (void *(*)(void *))dlsym(plugin->handle, PLUGIN_HANDLER);
|
||||||
if ((error = dlerror()) != NULL)
|
if ((error = dlerror()) != NULL)
|
||||||
|
@ -1034,8 +1084,18 @@ void* execute_plugin(void* data, const char *pname)
|
||||||
unknow(rq->client);
|
unknow(rq->client);
|
||||||
return task;
|
return task;
|
||||||
}
|
}
|
||||||
task->type = HEAVY;
|
// check if we need the raw data or not
|
||||||
|
if (meta && meta->raw_body == 1)
|
||||||
|
{
|
||||||
task->handle = fn;
|
task->handle = fn;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
free(task);
|
||||||
|
task = antd_create_task(decode_post_request, (void *)rq, fn);
|
||||||
|
task->priority++;
|
||||||
|
}
|
||||||
|
task->type = HEAVY;
|
||||||
return task;
|
return task;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,8 +12,6 @@
|
||||||
#include "libs/scheduler.h"
|
#include "libs/scheduler.h"
|
||||||
#include "plugin_manager.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 PLUGIN_HANDLER "handle"
|
||||||
#define WS_MAGIC_STRING "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
|
#define WS_MAGIC_STRING "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
|
||||||
#define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0
|
#define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0
|
||||||
|
|
17
httpd.c
17
httpd.c
|
@ -145,10 +145,8 @@ int main(int argc, char* argv[])
|
||||||
client_ip = inet_ntoa(client_name.sin_addr);
|
client_ip = inet_ntoa(client_name.sin_addr);
|
||||||
client->ip = strdup(client_ip);
|
client->ip = strdup(client_ip);
|
||||||
LOG("Client IP: %s\n", 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 timeout to socket
|
||||||
set_nonblock(client_sock);
|
set_nonblock(client_sock);
|
||||||
|
@ -163,8 +161,6 @@ int main(int argc, char* argv[])
|
||||||
perror("setsockopt failed\n");
|
perror("setsockopt failed\n");
|
||||||
*/
|
*/
|
||||||
client->sock = client_sock;
|
client->sock = client_sock;
|
||||||
// 100 times retry connection before abort
|
|
||||||
//LOG("Unclosed connection: %d\n", server_config->connection);
|
|
||||||
#ifdef USE_OPENSSL
|
#ifdef USE_OPENSSL
|
||||||
client->ssl = NULL;
|
client->ssl = NULL;
|
||||||
client->status = 0;
|
client->status = 0;
|
||||||
|
@ -184,17 +180,6 @@ int main(int argc, char* argv[])
|
||||||
#endif
|
#endif
|
||||||
// create callback for the server
|
// create callback for the server
|
||||||
antd_add_task(&scheduler, antd_create_task(accept_request,(void*)request, finish_request ));
|
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);
|
close(server_sock);
|
||||||
|
|
|
@ -24,7 +24,8 @@
|
||||||
#define R_FLOAT(d,k) ((double)atof(dvalue(d,k)))
|
#define R_FLOAT(d,k) ((double)atof(dvalue(d,k)))
|
||||||
#define R_PTR(d,k) (dvalue(d,k))
|
#define R_PTR(d,k) (dvalue(d,k))
|
||||||
#define __RESULT__ "{\"result\":%d,\"msg\":\"%s\"}"
|
#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
|
#ifdef USE_OPENSSL
|
||||||
int __attribute__((weak)) usessl();
|
int __attribute__((weak)) usessl();
|
||||||
|
@ -64,6 +65,19 @@ typedef struct {
|
||||||
char* sslkey;
|
char* sslkey;
|
||||||
#endif
|
#endif
|
||||||
}config_t;
|
}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_nonblock(int socket);
|
||||||
//void set_block(int socket);
|
//void set_block(int socket);
|
||||||
int response(void*, const char*);
|
int response(void*, const char*);
|
||||||
|
|
|
@ -11,9 +11,13 @@ void __init_plugin__(const char* pl,config_t* conf){
|
||||||
#ifdef USE_OPENSSL
|
#ifdef USE_OPENSSL
|
||||||
__plugin__.usessl = conf->usessl;
|
__plugin__.usessl = conf->usessl;
|
||||||
#endif
|
#endif
|
||||||
|
__plugin__.raw_body = 0;
|
||||||
init();
|
init();
|
||||||
};
|
};
|
||||||
|
void use_raw_body()
|
||||||
|
{
|
||||||
|
__plugin__.raw_body = 1;
|
||||||
|
}
|
||||||
#ifdef USE_DB
|
#ifdef USE_DB
|
||||||
sqldb __getdb(char *name)
|
sqldb __getdb(char *name)
|
||||||
{
|
{
|
||||||
|
|
|
@ -7,17 +7,6 @@
|
||||||
#include "ws.h"
|
#include "ws.h"
|
||||||
#include "scheduler.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)();
|
//typedef void(*call)();
|
||||||
|
@ -40,4 +29,5 @@ void init();
|
||||||
void destroy();
|
void destroy();
|
||||||
void* handle(void*);
|
void* handle(void*);
|
||||||
plugin_header_t* meta();
|
plugin_header_t* meta();
|
||||||
|
void use_raw_body();
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue
Block a user