From 47264d3249a3af571170410e0a1c34007e108aa4 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: Thu, 9 May 2024 09:27:18 +0800 Subject: [PATCH] update --- simplehttpd.c | 30 ++++++++++++++++++------------ simplehttpd.h | 14 +++++++++++--- test.c | 2 +- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/simplehttpd.c b/simplehttpd.c index b9fa449..a5470b2 100644 --- a/simplehttpd.c +++ b/simplehttpd.c @@ -21,7 +21,7 @@ #define SERVER_STRING "Server: simplehttpd/0.0.1\r\n" #define HTTP_DATA_RECV_TIMEOUT 15 -typedef void (*PostProcessDoneCallback)(int, const char *, int); +typedef void (*PostProcessDoneCallback)(PostProcessRetCode, const char *, int); static PostProcessDoneCallback post_process_done_callback = NULL; typedef int (*PrintLog)(const char *, ...); @@ -167,11 +167,11 @@ int get_line(int sock, char *buf, int size) return (i); } -void call_post_process_done_cb(int ret, const char *filename, int datalen) +void call_post_process_done_cb(PostProcessRetCode code, const char *filename, int datalen) { if (post_process_done_callback != NULL) { - post_process_done_callback(ret, filename, datalen); + post_process_done_callback(code, filename, datalen); } } @@ -219,7 +219,7 @@ void post_process(int client, const char *path, const char *method, const char * { HTTPD_LOG_ERROR("bad http post request."); bad_request(client); - call_post_process_done_cb(-1, NULL, -1); + call_post_process_done_cb(POST_PROCESS_BAD_REQUEST, NULL, -1); return; } @@ -228,7 +228,7 @@ void post_process(int client, const char *path, const char *method, const char * { HTTPD_LOG_ERROR("malloc buffer failed."); cannot_execute(client); - call_post_process_done_cb(-1, NULL, -1); + call_post_process_done_cb(POST_PROCESS_MALLOC_ERROR, NULL, -1); return; } memset(tmp_buf, 0, 4096 * 1024); @@ -240,7 +240,7 @@ void post_process(int client, const char *path, const char *method, const char * { HTTPD_LOG_ERROR("recv data failed."); free(tmp_buf); - call_post_process_done_cb(-1, NULL, -1); + call_post_process_done_cb(POST_PROCESS_CONN_ERROR, NULL, -1); return; } total += n; @@ -256,7 +256,7 @@ void post_process(int client, const char *path, const char *method, const char * HTTPD_LOG_ERROR("log filename not found."); bad_request(client); free(tmp_buf); - call_post_process_done_cb(-1, NULL, -1); + call_post_process_done_cb(POST_PROCESS_BAD_REQUEST, NULL, -1); return; } @@ -273,7 +273,7 @@ void post_process(int client, const char *path, const char *method, const char * HTTPD_LOG_ERROR("log file data start flag not found."); bad_request(client); free(tmp_buf); - call_post_process_done_cb(-1, filename, -1); + call_post_process_done_cb(POST_PROCESS_BAD_REQUEST, filename, -1); return; } data_start = &tmp[4]; @@ -289,7 +289,7 @@ void post_process(int client, const char *path, const char *method, const char * HTTPD_LOG_ERROR("log file data end flag not found."); bad_request(client); free(tmp_buf); - call_post_process_done_cb(-1, filename, -1); + call_post_process_done_cb(POST_PROCESS_BAD_REQUEST, filename, -1); return; } @@ -300,8 +300,14 @@ void post_process(int client, const char *path, const char *method, const char * HTTPD_LOG_INFO("recv log file datalen: %d.", data_len); int ret = save_log_file(filename, data_start, data_len); free(tmp_buf); - - call_post_process_done_cb(ret, filename, data_len); + if (ret != 0) + { + call_post_process_done_cb(POST_PROCESS_SAVE_ERROR, filename, data_len); + } + else + { + call_post_process_done_cb(POST_PROCESS_OK, filename, data_len); + } } void get_process(int client, const char *path, const char *method, const char *query_string) @@ -489,7 +495,7 @@ int StartHttpServerSync(const char *ip, uint16_t port) close(server_sock); return -1; } - + memset(&name, 0, sizeof(name)); name.sin_family = AF_INET; name.sin_port = htons(port); diff --git a/simplehttpd.h b/simplehttpd.h index 006dcc6..3d9dd31 100644 --- a/simplehttpd.h +++ b/simplehttpd.h @@ -8,11 +8,19 @@ extern "C" { #endif #endif -typedef void (*PostProcessDoneCallback)(int code, const char * filename, int datalen); + +typedef enum { + POST_PROCESS_OK = 0, + POST_PROCESS_BAD_REQUEST, + POST_PROCESS_MALLOC_ERROR, + POST_PROCESS_CONN_ERROR, + POST_PROCESS_SAVE_ERROR +} PostProcessRetCode; + +typedef void (*PostProcessDoneCallback)(PostProcessRetCode code, const char * filename, int datalen); typedef int (*PrintLog)(const char* format, ...); -struct PrintLogFuncSet -{ +struct PrintLogFuncSet { PrintLog infoLog; PrintLog errorLog; }; diff --git a/test.c b/test.c index f8d6421..06e6c5f 100644 --- a/test.c +++ b/test.c @@ -3,7 +3,7 @@ #include #include "simplehttpd.h" -void ProcessDoneCallback(int code, const char *filename, int datalen) +void ProcessDoneCallback(PostProcessRetCode code, const char *filename, int datalen) { printf("code: %d, filename: %s, datalen: %d\n", code, filename, datalen); } -- Gitee