diff --git a/simplehttpd.c b/simplehttpd.c index 05cb9f85a17e825d20566ff2b713fc8bb488ae87..b9fa44961b8c51c6fc1bd887eff2b443a3114592 100644 --- a/simplehttpd.c +++ b/simplehttpd.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -11,13 +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 HTTP_DATA_RECV_TIMEOUT 15 typedef void (*PostProcessDoneCallback)(int, const char *, int); static PostProcessDoneCallback post_process_done_callback = NULL; @@ -234,10 +236,9 @@ 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); free(tmp_buf); call_post_process_done_cb(-1, NULL, -1); return; @@ -463,7 +464,7 @@ int SetLogSavePath(const char *path) return 0; } -int StartHttpServerSync(uint16_t port) +int StartHttpServerSync(const char *ip, uint16_t port) { int server_sock = -1; int client_sock = -1; @@ -478,10 +479,21 @@ int StartHttpServerSync(uint16_t port) HTTPD_LOG_ERROR("socket create failed."); return -1; } + + 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; 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."); @@ -513,15 +525,14 @@ int StartHttpServerSync(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); } } diff --git a/simplehttpd.h b/simplehttpd.h index e73a2f673dac841c6b57130a982c438e04055a74..006dcc672e1201b0d24ea2ea077f617e911798a7 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(const char *ip, uint16_t port); #ifdef __cplusplus #if __cplusplus diff --git a/test.c b/test.c index 79589f87a0177205764724ab17bfde052964b416..f8d6421c068091d89656f5c9d5fb46d3fdb36ccf 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