From 5fb00dc5f02816ace965983c05e0dac6040c27b0 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: Mon, 13 May 2024 10:11:40 +0800 Subject: [PATCH 1/2] update --- src/simplehttpd.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/simplehttpd.c b/src/simplehttpd.c index 61e9e73..837ea1b 100644 --- a/src/simplehttpd.c +++ b/src/simplehttpd.c @@ -28,7 +28,7 @@ typedef int (*PrintLog)(const char *, ...); static PrintLog HTTPD_LOG_INFO = printf; static PrintLog HTTPD_LOG_ERROR = printf; -static char log_dir_path[128] = {0}; +static char log_dir_path[1024] = {0}; void response_ok(int client) { @@ -116,7 +116,7 @@ void unimplemented(int client) int save_log_file(char *filename, char *buf, unsigned int len) { - char path[255] = {0}; + char path[2048] = {0}; sprintf(path, "%s%s", log_dir_path, filename); FILE *fp = fopen(path, "ab"); if (fp == NULL) @@ -460,7 +460,7 @@ int RegisterLogFunc(struct PrintLogFuncSet funcSet) int SetLogSavePath(const char *path) { - if (strlen(path) >= 128) + if (strlen(path) > 1023) { HTTPD_LOG_ERROR("log save path is too long."); return -1; -- Gitee From a1e41f2a8c1da8b5503f88a9cf045d7327c06fce 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: Mon, 13 May 2024 14:24:12 +0800 Subject: [PATCH 2/2] update --- src/simplehttpd.c | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/src/simplehttpd.c b/src/simplehttpd.c index 837ea1b..8b9f337 100644 --- a/src/simplehttpd.c +++ b/src/simplehttpd.c @@ -20,6 +20,7 @@ #define ISspace(x) isspace((int)(x)) #define SERVER_STRING "Server: simplehttpd/0.0.1\r\n" #define HTTP_DATA_RECV_TIMEOUT 15 +#define MAX_BUFFER_SIZE 1024 typedef void (*PostProcessDoneCallback)(PostProcessRetCode, const char *, int); static PostProcessDoneCallback post_process_done_callback = NULL; @@ -28,18 +29,18 @@ typedef int (*PrintLog)(const char *, ...); static PrintLog HTTPD_LOG_INFO = printf; static PrintLog HTTPD_LOG_ERROR = printf; -static char log_dir_path[1024] = {0}; +static char log_dir_path[MAX_BUFFER_SIZE] = {0}; void response_ok(int client) { - char buf[1024]; + char buf[MAX_BUFFER_SIZE]; sprintf(buf, "HTTP/1.0 200 OK\r\n"); send(client, buf, strlen(buf), MSG_NOSIGNAL); } void not_found(int client) { - char buf[1024]; + char buf[MAX_BUFFER_SIZE]; sprintf(buf, "HTTP/1.0 404 NOT FOUND\r\n"); send(client, buf, strlen(buf), MSG_NOSIGNAL); @@ -64,7 +65,7 @@ void not_found(int client) void cannot_execute(int client) { - char buf[1024]; + char buf[MAX_BUFFER_SIZE]; sprintf(buf, "HTTP/1.0 500 Internal Server Error\r\n"); send(client, buf, strlen(buf), MSG_NOSIGNAL); @@ -78,7 +79,7 @@ void cannot_execute(int client) void bad_request(int client) { - char buf[1024]; + char buf[MAX_BUFFER_SIZE]; sprintf(buf, "HTTP/1.0 400 BAD REQUEST\r\n"); send(client, buf, sizeof(buf), MSG_NOSIGNAL); @@ -94,7 +95,7 @@ void bad_request(int client) void unimplemented(int client) { - char buf[1024]; + char buf[MAX_BUFFER_SIZE]; sprintf(buf, "HTTP/1.0 501 Method Not Implemented\r\n"); send(client, buf, strlen(buf), MSG_NOSIGNAL); @@ -116,7 +117,7 @@ void unimplemented(int client) int save_log_file(char *filename, char *buf, unsigned int len) { - char path[2048] = {0}; + char path[2 * MAX_BUFFER_SIZE] = {0}; sprintf(path, "%s%s", log_dir_path, filename); FILE *fp = fopen(path, "ab"); if (fp == NULL) @@ -177,9 +178,9 @@ void call_post_process_done_cb(PostProcessRetCode code, const char *filename, in void post_process(int client, const char *path, const char *method, const char *query_string) { - char buf[1024]; - char boundary[1024] = {0}; - char filename[1024] = {0}; + char buf[MAX_BUFFER_SIZE]; + char boundary[MAX_BUFFER_SIZE] = {0}; + char filename[MAX_BUFFER_SIZE] = {0}; char *tmp; int i; int numchars = 1; @@ -204,7 +205,7 @@ void post_process(int client, const char *path, const char *method, const char * tmp = strstr(&buf[14], "boundary="); if (tmp != NULL) { - for (i = 0; i < 1023 && tmp[i + 9] != '\n'; i++) + for (i = 0; i < MAX_BUFFER_SIZE - 1 && tmp[i + 9] != '\n'; i++) { boundary[i] = tmp[i + 9]; } @@ -260,7 +261,7 @@ void post_process(int client, const char *path, const char *method, const char * return; } - for (i = 0; i < 1023 && tmp[i + 10] != '\"'; i++) + for (i = 0; i < MAX_BUFFER_SIZE - 1 && tmp[i + 10] != '\"'; i++) { filename[i] = tmp[i + 10]; } @@ -278,7 +279,7 @@ void post_process(int client, const char *path, const char *method, const char * } data_start = &tmp[4]; - char boundary_end[2048] = {0}; + char boundary_end[2 * MAX_BUFFER_SIZE] = {0}; sprintf(boundary_end, "--%s--", boundary); int boundary_end_len = strlen(boundary_end); int boundary_end_index = content_length - boundary_end_len - 2; @@ -311,7 +312,7 @@ void post_process(int client, const char *path, const char *method, const char * void get_process(int client, const char *path, const char *method, const char *query_string) { - char buf[1024]; + char buf[MAX_BUFFER_SIZE]; int numchars = 1; buf[0] = 'A'; buf[1] = '\0'; @@ -326,10 +327,10 @@ void get_process(int client, const char *path, const char *method, const char *q void *accept_request(void *arg) { int client = *(int *)arg; - char buf[1024]; + char buf[MAX_BUFFER_SIZE]; int numchars; - char method[255]; - char url[255]; + char method[MAX_BUFFER_SIZE]; + char url[MAX_BUFFER_SIZE]; size_t i, j; char *query_string = NULL; @@ -392,7 +393,7 @@ void *accept_request(void *arg) void cat(int client, FILE *resource) { - char buf[1024]; + char buf[MAX_BUFFER_SIZE]; fgets(buf, sizeof(buf), resource); while (!feof(resource)) @@ -404,7 +405,7 @@ void cat(int client, FILE *resource) void headers(int client, const char *filename) { - char buf[1024]; + char buf[MAX_BUFFER_SIZE]; (void)filename; strcpy(buf, "HTTP/1.0 200 OK\r\n"); @@ -422,7 +423,7 @@ void serve_file(int client, const char *filename) { FILE *resource = NULL; int numchars = 1; - char buf[1024]; + char buf[MAX_BUFFER_SIZE]; buf[0] = 'A'; buf[1] = '\0'; @@ -460,7 +461,7 @@ int RegisterLogFunc(struct PrintLogFuncSet funcSet) int SetLogSavePath(const char *path) { - if (strlen(path) > 1023) + if (strlen(path) > MAX_BUFFER_SIZE - 1) { HTTPD_LOG_ERROR("log save path is too long."); return -1; -- Gitee