fix high cpu usage when handling bad POST request

This commit is contained in:
lxsang 2021-02-04 19:40:02 +01:00
parent d0c917ce99
commit b9bd5e9ad1
2 changed files with 19 additions and 4 deletions

View File

@ -877,6 +877,7 @@ static void *proxify(void *data)
proxy->ssl = NULL; proxy->ssl = NULL;
proxy->zstream = NULL; proxy->zstream = NULL;
proxy->z_level = ANTD_CNONE; proxy->z_level = ANTD_CNONE;
time(&proxy->last_io);
// store content length here // store content length here
dput(rq->request, "PROXY_HANDLE", proxy); dput(rq->request, "PROXY_HANDLE", proxy);
@ -1177,8 +1178,9 @@ void *decode_post_request(void *data)
else if (clen > 0) else if (clen > 0)
{ {
// WARN: this may not work on ssl socket // 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); // antd_task_bind_event(task, rq->client->sock, 0, TASK_EVT_ON_READABLE | TASK_EVT_ON_WRITABLE);
task->handle = decode_post_request; // task->handle = decode_post_request;
antd_error(rq->client, 400, "Bad Request, missing content data");
return task; return task;
} }
} }
@ -1202,8 +1204,9 @@ void *decode_post_request(void *data)
} }
else if (clen > 0) else if (clen > 0)
{ {
task->handle = decode_post_request; //task->handle = decode_post_request;
antd_task_bind_event(task, rq->client->sock, 0, TASK_EVT_ON_READABLE | TASK_EVT_ON_WRITABLE); //antd_task_bind_event(task, rq->client->sock, 0, TASK_EVT_ON_READABLE | TASK_EVT_ON_WRITABLE);
antd_error(rq->client, 400, "Bad Request, missing content data");
return task; return task;
} }
} }
@ -1512,6 +1515,10 @@ char *post_data_decode(void *client, int len)
read += stat; read += stat;
readlen = (len - read) > BUFFLEN ? BUFFLEN : (len - read); readlen = (len - read) > BUFFLEN ? BUFFLEN : (len - read);
} }
if (stat == 0)
{
usleep(POLL_EVENT_TO*1000);
}
} }
if (read > 0) if (read > 0)

View File

@ -565,6 +565,10 @@ int antd_recv_upto(void *src, void *data, int len)
} }
else else
{ {
if (difftime(time(NULL), source->last_io) > MAX_IO_WAIT_TIME)
{
return -1;
}
switch (err) switch (err)
{ {
case SSL_ERROR_NONE: case SSL_ERROR_NONE:
@ -603,6 +607,10 @@ int antd_recv_upto(void *src, void *data, int len)
time(&source->last_io); time(&source->last_io);
return received; return received;
} }
else if (difftime(time(NULL), source->last_io) > MAX_IO_WAIT_TIME)
{
return -1;
}
if (received <= 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) if (received <= 0 && (errno == EAGAIN || errno == EWOULDBLOCK))
{ {
return 0; return 0;