remove strdup calles

This commit is contained in:
lxsang 2019-12-22 14:33:42 +01:00
parent ea07981160
commit c429a7dd66
8 changed files with 51 additions and 66 deletions

View File

@ -56,5 +56,4 @@ make distcheck
### To do ### To do
- max upload file size should be configurable
- remove static strings, replace it by configurations - remove static strings, replace it by configurations

View File

@ -101,9 +101,9 @@ audio/mpeg=mp3,mpeg
; specify a plugin for handling ; specify a plugin for handling
; a file type ; a file type
; lua page script ; lua page script
ls = lua-api ls = lua
; pure lua script ; pure lua script
lua = lua-api lua = lua
; php and o ther scripting languages can be ; php and o ther scripting languages can be
; handled by the cgi plugin ; handled by the cgi plugin
; php = cgi ; php = cgi

Binary file not shown.

View File

@ -494,7 +494,7 @@ void *resolve_request(void *data)
} }
else else
{ {
i = HASHSIZE; i = server_config.handlers->cap;
break; break;
} }
} }
@ -536,6 +536,8 @@ void *resolve_request(void *data)
else else
{ {
task->type = HEAVY; task->type = HEAVY;
//TODO empty the buff
while (read_buf(rq->client, path, sizeof(path)) > 0);;
task->handle = serve_file; task->handle = serve_file;
} }
return task; return task;
@ -638,6 +640,7 @@ void *serve_file(void *data)
else*/ else*/
struct stat st; struct stat st;
int s = stat(path, &st); int s = stat(path, &st);
if(s == -1) if(s == -1)
{ {
antd_error(rq->client, 404, "File not found"); antd_error(rq->client, 404, "File not found");
@ -758,7 +761,6 @@ void *decode_request_header(void *data)
// ip address // ip address
dput(xheader, "REMOTE_ADDR", (void *)strdup(((antd_client_t *)rq->client)->ip)); dput(xheader, "REMOTE_ADDR", (void *)strdup(((antd_client_t *)rq->client)->ip));
dput(xheader, "SERVER_PORT", (void *)__s("%d", ((antd_client_t *)rq->client)->port_config->port)); dput(xheader, "SERVER_PORT", (void *)__s("%d", ((antd_client_t *)rq->client)->port_config->port));
//while((line = read_line(client)) && strcmp("\r\n",line))
while ((read_buf(rq->client, buf, sizeof(buf))) && strcmp("\r\n", buf)) while ((read_buf(rq->client, buf, sizeof(buf))) && strcmp("\r\n", buf))
{ {
line = buf; line = buf;
@ -779,6 +781,20 @@ void *decode_request_header(void *data)
host = strdup(line); host = strdup(line);
} }
} }
// 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);
return antd_create_task(NULL, (void *)rq, NULL,rq->client->last_io);;
}
}
//if(line) free(line); //if(line) free(line);
memset(buf, 0, sizeof(buf)); memset(buf, 0, sizeof(buf));
strcat(buf, url); strcat(buf, url);
@ -879,7 +895,6 @@ void *decode_post_request(void *data)
} }
else if (strstr(ctype, FORM_MULTI_PART)) else if (strstr(ctype, FORM_MULTI_PART))
{ {
//printf("Multi part form : %s\n", ctype);
free(task); free(task);
return decode_multi_part_request(rq, ctype); return decode_multi_part_request(rq, ctype);
} }
@ -975,9 +990,9 @@ dictionary_t decode_cookie(const char *line)
void *decode_multi_part_request(void *data, const char *ctype) void *decode_multi_part_request(void *data, const char *ctype)
{ {
char *boundary; char *boundary;
char *line; char line[BUFFLEN];
char *str_copy = strdup(ctype); char *str_copy = (char*)ctype;
char *orgcpy = str_copy; int len;
antd_request_t *rq = (antd_request_t *)data; 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_t *task = antd_create_task(NULL, (void *)rq, NULL, rq->client->last_io);
task->priority++; task->priority++;
@ -990,18 +1005,12 @@ 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)) while (( (len = read_buf(rq->client, line, sizeof(line))) > 0 ) && !strstr(line, boundary));
{ if (len > 0)
if (line)
free(line);
}
if (line)
{ {
task->handle = decode_multi_part_request_data; task->handle = decode_multi_part_request_data;
free(line);
} }
} }
free(orgcpy);
task->type = HEAVY; task->type = HEAVY;
return task; return task;
} }
@ -1009,12 +1018,12 @@ void *decode_multi_part_request_data(void *data)
{ {
// loop through each part separated by the boundary // loop through each part separated by the boundary
char *line; char *line;
char *orgline;
char *part_name = NULL; char *part_name = NULL;
char *part_file = NULL; char *part_file = NULL;
char *file_path; char *file_path;
char buf[BUFFLEN]; char buf[BUFFLEN];
char *field; char *field;
int len;
//dictionary dic = NULL; //dictionary dic = NULL;
FILE *fp = NULL; FILE *fp = NULL;
char *token, *keytoken, *valtoken; char *token, *keytoken, *valtoken;
@ -1023,22 +1032,15 @@ void *decode_multi_part_request_data(void *data)
task->priority++; task->priority++;
char *boundary = (char *)dvalue(rq->request, "MULTI_PART_BOUNDARY"); char *boundary = (char *)dvalue(rq->request, "MULTI_PART_BOUNDARY");
dictionary_t dic = (dictionary_t)dvalue(rq->request, "REQUEST_DATA"); dictionary_t dic = (dictionary_t)dvalue(rq->request, "REQUEST_DATA");
char *boundend = __s("%s--", boundary);
// search for content disposition: // search for content disposition:
while ((line = read_line(rq->client)) && while ( ( (len = read_buf(rq->client, buf, sizeof(buf))) > 0 ) && !strstr(buf, "Content-Disposition:"));;
!strstr(line, "Content-Disposition:"))
if (len <= 0 || !strstr(buf, "Content-Disposition:"))
{ {
free(line);
line = NULL;
}
if (!line || !strstr(line, "Content-Disposition:"))
{
if (line)
free(line);
free(boundend);
return task; return task;
} }
orgline = line; char *boundend = __s("%s--", boundary);
line = buf;
// extract parameters from header // extract parameters from header
while ((token = strsep(&line, ";"))) while ((token = strsep(&line, ";")))
{ {
@ -1064,40 +1066,36 @@ void *decode_multi_part_request_data(void *data)
} }
} }
} }
free(orgline);
line = NULL; line = NULL;
// get the binary data // get the binary data
if (part_name != NULL) if (part_name != NULL)
{ {
// go to the beginning of data bock // go to the beginning of data bock
while ((line = read_line(rq->client)) && strcmp(line, "\r\n") != 0) while ((len = read_buf(rq->client, buf, sizeof(buf))) > 0 && strcmp(buf, "\r\n") != 0);;
{
free(line);
line = NULL;
}
if (line)
{
free(line);
line = NULL;
}
if (part_file == NULL) if (part_file == NULL)
{ {
/** /**
* WARNING:
* This allow only 1024 bytes of data (max), * This allow only 1024 bytes of data (max),
* out of this range, the data is cut out. * out of this range, the data is cut out.
* Need an efficient way to handle this * Need an efficient way to handle this
*/ */
line = read_line(rq->client); len = read_buf(rq->client, buf, sizeof(buf));
trim(line, '\n'); if(len > 0)
trim(line, '\r');
trim(line, ' ');
dput(dic, part_name, line);
// find the next boundary
while ((line = read_line(rq->client)) && !strstr(line, boundary))
{ {
free(line); line = buf;
line = NULL; 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 else
{ {
@ -1121,7 +1119,7 @@ void *decode_multi_part_request_data(void *data)
int stat = ftruncate(fileno(fp), totalsize); int stat = ftruncate(fileno(fp), totalsize);
UNUSED(stat); UNUSED(stat);
fclose(fp); fclose(fp);
line = strdup(buf); line = buf;
field = __s("%s.file", part_name); field = __s("%s.file", part_name);
dput(dic, field, strdup(part_file)); dput(dic, field, strdup(part_file));
@ -1150,7 +1148,6 @@ void *decode_multi_part_request_data(void *data)
if (line && strstr(line, boundend)) if (line && strstr(line, boundend))
{ {
//LOG("End request %s", boundend); //LOG("End request %s", boundend);
free(line);
free(boundend); free(boundend);
return task; return task;
} }
@ -1160,7 +1157,6 @@ void *decode_multi_part_request_data(void *data)
task->type = HEAVY; task->type = HEAVY;
task->handle = decode_multi_part_request_data; task->handle = decode_multi_part_request_data;
} }
free(line);
free(boundend); free(boundend);
return task; return task;
} }

View File

@ -670,17 +670,7 @@ int ws_enable(dictionary_t dic)
if(!v) return 0; if(!v) return 0;
return atoi(v) == 1; return atoi(v) == 1;
} }
/**
* read the request as a string line format
* @param sock socket
* @return a request string
*/
char* read_line(void* sock)
{
char buf[BUFFLEN];
read_buf(sock,buf,sizeof(buf));
return strdup(buf);
}
/** /**
* Read the socket request in to a buffer or size * Read the socket request in to a buffer or size
* The data is read until the buffer is full or * The data is read until the buffer is full or

View File

@ -116,7 +116,6 @@ int upload(const char*, const char*);
void antd_error(void* client, int status, const char* msg); void antd_error(void* client, int status, const char* msg);
int ws_enable(dictionary_t); int ws_enable(dictionary_t);
char* read_line(void* sock);
int read_buf(void* sock,char* buf,int i); int read_buf(void* sock,char* buf,int i);
int antd_send( void *source, const void* data, int len); int antd_send( void *source, const void* data, int len);
int antd_recv( void *source, void* data, int len); int antd_recv( void *source, void* data, int len);

View File

@ -53,7 +53,7 @@ static antd_task_item_t dequeue(antd_task_queue_t* q)
} }
static antd_callback_t* callback_of( void* (*callback)(void*) ) antd_callback_t* callback_of( void* (*callback)(void*) )
{ {
antd_callback_t* cb = NULL; antd_callback_t* cb = NULL;
if(callback) if(callback)

View File

@ -135,4 +135,5 @@ wait for event
*/ */
void antd_wait(antd_scheduler_t *); void antd_wait(antd_scheduler_t *);
antd_callback_t* callback_of( void* (*callback)(void*) );
#endif #endif