From cd4239af3b05c55382239ff24431bf77544fe216 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=97=A0=E8=BE=9C=E7=9A=84=E7=86=8A=E6=9C=AC=E7=86=8A?= <10804640+innocent-kumamoto@user.noreply.gitee.com> Date: Wed, 8 May 2024 14:26:52 +0800 Subject: [PATCH 1/6] update --- simplehttpd.c | 23 +++++++++++++++++++++-- simplehttpd.h | 2 +- test.c | 2 +- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/simplehttpd.c b/simplehttpd.c index 05cb9f8..cc291d4 100644 --- a/simplehttpd.c +++ b/simplehttpd.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -19,6 +20,12 @@ #define SERVER_STRING "Server: simplehttpd/0.0.1\r\n" +#define TCP_KEEPALIVE_ON 1 +#define TCP_KEEPALIVE_IDLE 300 +#define TCP_KEEPALIVE_INTERVAL 10 +#define TCP_KEEPALIVE_COUNT 6 +#define TCP_TIMEOUT_MS 355000 + typedef void (*PostProcessDoneCallback)(int, const char *, int); static PostProcessDoneCallback post_process_done_callback = NULL; @@ -463,7 +470,7 @@ int SetLogSavePath(const char *path) return 0; } -int StartHttpServerSync(uint16_t port) +int StartHttpServerSync(char *ip, uint16_t port) { int server_sock = -1; int client_sock = -1; @@ -478,10 +485,22 @@ int StartHttpServerSync(uint16_t port) HTTPD_LOG_ERROR("socket create failed."); return -1; } + + int keepalive = TCP_KEEPALIVE_ON; + int keepidle = TCP_KEEPALIVE_IDLE; + int keepinterval = TCP_KEEPALIVE_INTERVAL; + int keepcount = TCP_KEEPALIVE_COUNT; + int timeout = TCP_TIMEOUT_MS; + setsockopt(server_sock, SOL_TCP, TCP_USER_TIMEOUT, &timeout, sizeof(timeout)); + setsockopt(server_sock, SOL_SOCKET, SO_KEEPALIVE, &keepalive, sizeof(keepalive)); + setsockopt(server_sock, SOL_TCP, TCP_KEEPIDLE, &keepidle, sizeof(keepidle)); + setsockopt(server_sock, SOL_TCP, TCP_KEEPINTVL, &keepinterval, sizeof(keepinterval)); + setsockopt(server_sock, SOL_TCP, TCP_KEEPCNT, &keepcount, sizeof(keepcount)); + memset(&name, 0, sizeof(name)); name.sin_family = AF_INET; name.sin_port = htons(port); - name.sin_addr.s_addr = htonl(INADDR_ANY); + name.sin_addr.s_addr = inet_addr(ip); if (bind(server_sock, (struct sockaddr *)&name, sizeof(name)) < 0) { HTTPD_LOG_ERROR("bind failed."); diff --git a/simplehttpd.h b/simplehttpd.h index e73a2f6..0fcf189 100644 --- a/simplehttpd.h +++ b/simplehttpd.h @@ -20,7 +20,7 @@ struct PrintLogFuncSet int RegisterPostProcessDoneCallback(PostProcessDoneCallback callback); int RegisterLogFunc(struct PrintLogFuncSet funcSet); int SetLogSavePath(const char *path); -int StartHttpServerSync(uint16_t port); +int StartHttpServerSync(char *ip, uint16_t port); #ifdef __cplusplus #if __cplusplus diff --git a/test.c b/test.c index 79589f8..f8d6421 100644 --- a/test.c +++ b/test.c @@ -18,6 +18,6 @@ int main() printf("SetLogSavePath ret=%d\n", ret); ret = RegisterLogFunc(set); printf("RegisterLogFunc ret=%d\n", ret); - ret = StartHttpServerSync(9999); + ret = StartHttpServerSync("127.0.0.1", 9999); return 0; } \ No newline at end of file -- Gitee From bf2b3c1cdfc2416e950a84aa24ce0ccf494cb718 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=97=A0=E8=BE=9C=E7=9A=84=E7=86=8A=E6=9C=AC=E7=86=8A?= <10804640+innocent-kumamoto@user.noreply.gitee.com> Date: Wed, 8 May 2024 14:49:44 +0800 Subject: [PATCH 2/6] update --- simplehttpd.c | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/simplehttpd.c b/simplehttpd.c index cc291d4..a4c209f 100644 --- a/simplehttpd.c +++ b/simplehttpd.c @@ -12,19 +12,14 @@ #include #include #include +#include #include #include "simplehttpd.h" #define ISspace(x) isspace((int)(x)) - #define SERVER_STRING "Server: simplehttpd/0.0.1\r\n" - -#define TCP_KEEPALIVE_ON 1 -#define TCP_KEEPALIVE_IDLE 300 -#define TCP_KEEPALIVE_INTERVAL 10 -#define TCP_KEEPALIVE_COUNT 6 -#define TCP_TIMEOUT_MS 355000 +#define HTTP_DATA_RECV_TIMEOUT 15 typedef void (*PostProcessDoneCallback)(int, const char *, int); static PostProcessDoneCallback post_process_done_callback = NULL; @@ -486,16 +481,15 @@ int StartHttpServerSync(char *ip, uint16_t port) return -1; } - int keepalive = TCP_KEEPALIVE_ON; - int keepidle = TCP_KEEPALIVE_IDLE; - int keepinterval = TCP_KEEPALIVE_INTERVAL; - int keepcount = TCP_KEEPALIVE_COUNT; - int timeout = TCP_TIMEOUT_MS; - setsockopt(server_sock, SOL_TCP, TCP_USER_TIMEOUT, &timeout, sizeof(timeout)); - setsockopt(server_sock, SOL_SOCKET, SO_KEEPALIVE, &keepalive, sizeof(keepalive)); - setsockopt(server_sock, SOL_TCP, TCP_KEEPIDLE, &keepidle, sizeof(keepidle)); - setsockopt(server_sock, SOL_TCP, TCP_KEEPINTVL, &keepinterval, sizeof(keepinterval)); - setsockopt(server_sock, SOL_TCP, TCP_KEEPCNT, &keepcount, sizeof(keepcount)); + struct timeval tv; + tv.tv_sec = HTTP_DATA_RECV_TIMEOUT; + tv.tv_usec = 0; + if (setsockopt(server_sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) + { + HTTPD_LOG_ERROR("set socket recv timeout failed."); + close(server_sock); + return -1; + } memset(&name, 0, sizeof(name)); name.sin_family = AF_INET; -- Gitee From 373df5a2f97d38fc8715a0c86b731c94ee8bde20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=97=A0=E8=BE=9C=E7=9A=84=E7=86=8A=E6=9C=AC=E7=86=8A?= <10804640+innocent-kumamoto@user.noreply.gitee.com> Date: Wed, 8 May 2024 14:59:39 +0800 Subject: [PATCH 3/6] update --- simplehttpd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/simplehttpd.c b/simplehttpd.c index a4c209f..5fafafc 100644 --- a/simplehttpd.c +++ b/simplehttpd.c @@ -236,7 +236,7 @@ void post_process(int client, const char *path, const char *method, const char * while (1) { int n = recv(client, &tmp_buf[total], content_length, 0); - if (n < 0) + if (n <= 0) { HTTPD_LOG_ERROR("recv data failed."); cannot_execute(client); -- Gitee From 993040a94538ae85dc984a1963f055868349994d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=97=A0=E8=BE=9C=E7=9A=84=E7=86=8A=E6=9C=AC=E7=86=8A?= <10804640+innocent-kumamoto@user.noreply.gitee.com> Date: Wed, 8 May 2024 15:01:40 +0800 Subject: [PATCH 4/6] update --- simplehttpd.c | 1 - 1 file changed, 1 deletion(-) diff --git a/simplehttpd.c b/simplehttpd.c index 5fafafc..4980f93 100644 --- a/simplehttpd.c +++ b/simplehttpd.c @@ -239,7 +239,6 @@ void post_process(int client, const char *path, const char *method, const char * if (n <= 0) { HTTPD_LOG_ERROR("recv data failed."); - cannot_execute(client); free(tmp_buf); call_post_process_done_cb(-1, NULL, -1); return; -- Gitee From 28ffc9410561a033430b0afea0baffa4ec33390f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=97=A0=E8=BE=9C=E7=9A=84=E7=86=8A=E6=9C=AC=E7=86=8A?= <10804640+innocent-kumamoto@user.noreply.gitee.com> Date: Wed, 8 May 2024 15:31:21 +0800 Subject: [PATCH 5/6] update --- simplehttpd.c | 2 +- simplehttpd.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/simplehttpd.c b/simplehttpd.c index 4980f93..a87712e 100644 --- a/simplehttpd.c +++ b/simplehttpd.c @@ -464,7 +464,7 @@ int SetLogSavePath(const char *path) return 0; } -int StartHttpServerSync(char *ip, uint16_t port) +int StartHttpServerSync(const char *ip, uint16_t port) { int server_sock = -1; int client_sock = -1; diff --git a/simplehttpd.h b/simplehttpd.h index 0fcf189..006dcc6 100644 --- a/simplehttpd.h +++ b/simplehttpd.h @@ -20,7 +20,7 @@ struct PrintLogFuncSet int RegisterPostProcessDoneCallback(PostProcessDoneCallback callback); int RegisterLogFunc(struct PrintLogFuncSet funcSet); int SetLogSavePath(const char *path); -int StartHttpServerSync(char *ip, uint16_t port); +int StartHttpServerSync(const char *ip, uint16_t port); #ifdef __cplusplus #if __cplusplus -- Gitee From 27bf8dc7f588aa7cd1950bb46b13eef38bf8206c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=97=A0=E8=BE=9C=E7=9A=84=E7=86=8A=E6=9C=AC=E7=86=8A?= <10804640+innocent-kumamoto@user.noreply.gitee.com> Date: Wed, 8 May 2024 16:47:36 +0800 Subject: [PATCH 6/6] update --- simplehttpd.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/simplehttpd.c b/simplehttpd.c index a87712e..b9fa449 100644 --- a/simplehttpd.c +++ b/simplehttpd.c @@ -525,15 +525,14 @@ int StartHttpServerSync(const char *ip, uint16_t port) client_sock = accept(server_sock, (struct sockaddr *)&client_name, &client_name_len); if (client_sock == -1) { - HTTPD_LOG_ERROR("accept failed."); - close(server_sock); - return -1; + HTTPD_LOG_ERROR("accept failed, %d, %s.", errno, strerror(errno)); + continue; } if (pthread_create(&newthread, NULL, accept_request, (void *)&client_sock) != 0) { - close(server_sock); - return -1; + HTTPD_LOG_ERROR("create new thread failed."); + close(client_sock); } } -- Gitee