From b9bd5e9ad10fc18ee26ce05cd8a6bc0a8d3a9334 Mon Sep 17 00:00:00 2001 From: lxsang Date: Thu, 4 Feb 2021 19:40:02 +0100 Subject: [PATCH] fix high cpu usage when handling bad POST request --- http_server.c | 15 +++++++++++---- lib/handle.c | 8 ++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/http_server.c b/http_server.c index e93681c..5c7e1bb 100644 --- a/http_server.c +++ b/http_server.c @@ -877,6 +877,7 @@ static void *proxify(void *data) proxy->ssl = NULL; proxy->zstream = NULL; proxy->z_level = ANTD_CNONE; + time(&proxy->last_io); // store content length here dput(rq->request, "PROXY_HANDLE", proxy); @@ -1177,8 +1178,9 @@ void *decode_post_request(void *data) 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_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; } } @@ -1202,8 +1204,9 @@ void *decode_post_request(void *data) } else if (clen > 0) { - task->handle = decode_post_request; - antd_task_bind_event(task, rq->client->sock, 0, TASK_EVT_ON_READABLE | TASK_EVT_ON_WRITABLE); + //task->handle = decode_post_request; + //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; } } @@ -1512,6 +1515,10 @@ char *post_data_decode(void *client, int len) read += stat; readlen = (len - read) > BUFFLEN ? BUFFLEN : (len - read); } + if (stat == 0) + { + usleep(POLL_EVENT_TO*1000); + } } if (read > 0) diff --git a/lib/handle.c b/lib/handle.c index 3eaf5aa..79a3532 100644 --- a/lib/handle.c +++ b/lib/handle.c @@ -565,6 +565,10 @@ int antd_recv_upto(void *src, void *data, int len) } else { + if (difftime(time(NULL), source->last_io) > MAX_IO_WAIT_TIME) + { + return -1; + } switch (err) { case SSL_ERROR_NONE: @@ -603,6 +607,10 @@ int antd_recv_upto(void *src, void *data, int len) time(&source->last_io); return received; } + else if (difftime(time(NULL), source->last_io) > MAX_IO_WAIT_TIME) + { + return -1; + } if (received <= 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) { return 0;