From 2d9629555514de1035412f6003351f9b9c6b4191 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Wed, 4 Sep 2024 17:22:44 +0900 Subject: [PATCH] Test using "HTML_FILE" var, fixes --- src/config.c | 44 ++------------------------------- src/http_template.c | 3 ++- src/test.c | 60 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 43 deletions(-) diff --git a/src/config.c b/src/config.c index e8ce6fd..81c756c 100644 --- a/src/config.c +++ b/src/config.c @@ -395,48 +395,8 @@ C_SIMPLE_HTTP_ParsedConfig c_simple_http_parse_config( unsigned char *key = malloc(key_idx); memcpy(key, key_buf, key_idx); - unsigned char *value; - if (strcmp((char*)key_buf, "HTML_FILE") == 0) { - __attribute__((cleanup(simple_archiver_helper_cleanup_FILE))) - FILE *html_file = fopen((char*)value_buf, "r"); - if (!html_file) { - fprintf(stderr, - "ERROR: Internal error failed to open HTML_FILE \"%s\"!", - value_buf); - c_simple_http_clean_up_parsed_config(&config); - config.hash_map = NULL; - return config; - } - - fseek(html_file, 0, SEEK_END); - long file_size = ftell(html_file); - if (file_size <= 0) { - fprintf(stderr, - "ERROR: Internal error HTML_FILE \"%s\" is invalid size!", - value_buf); - c_simple_http_clean_up_parsed_config(&config); - config.hash_map = NULL; - return config; - } - fseek(html_file, 0, SEEK_SET); - unsigned long file_size_u = file_size; - - unsigned char *read_buf = malloc(file_size_u); - size_t read_amount = 0; - read_amount = fread(read_buf, 1, file_size_u, html_file); - if (read_amount != file_size_u) { - fprintf(stderr, "ERROR: Failed to read HTML_FILE \"%s\"!\n", - value_buf); - free(read_buf); - c_simple_http_clean_up_parsed_config(&config); - config.hash_map = NULL; - return config; - } - value = read_buf; - } else { - value = malloc(value_idx); - memcpy(value, value_buf, value_idx); - } + unsigned char *value = malloc(value_idx); + memcpy(value, value_buf, value_idx); if (simple_archiver_hash_map_insert(&hash_map_wrapper->paths, value, diff --git a/src/http_template.c b/src/http_template.c index 752dcae..091eb13 100644 --- a/src/http_template.c +++ b/src/http_template.c @@ -248,7 +248,7 @@ char *c_simple_http_path_to_generated( return NULL; } C_SIMPLE_HTTP_INTERNAL_Template_Node to_fill; - to_fill.html = malloc(final_size); + to_fill.html = malloc(final_size + 1); to_fill.html_size = 0; to_fill.html_capacity = final_size; if (simple_archiver_list_get( @@ -259,6 +259,7 @@ char *c_simple_http_path_to_generated( free(to_fill.html); return NULL; } + to_fill.html[final_size] = 0; return to_fill.html; } else { // Prevent cleanup fn from "free"ing html_buf and return it verbatim. diff --git a/src/test.c b/src/test.c index 45b59ce..d75f25d 100644 --- a/src/test.c +++ b/src/test.c @@ -279,6 +279,66 @@ int main(void) { "

Some text.


More text.

") == 0); simple_archiver_helper_cleanup_c_string(&buf); + + const char *test_http_template_filename3 = + "/tmp/c_simple_http_template_test3.config"; + const char *test_http_template_html_filename = + "/tmp/c_simple_http_template_test.html"; + test_file = fopen(test_http_template_filename3, "w"); + ASSERT_TRUE(test_file); + + ASSERT_TRUE( + fwrite( + "PATH=/\nHTML_FILE=/tmp/c_simple_http_template_test.html\n", + 1, + 55, + test_file) + == 55); + ASSERT_TRUE( + fwrite( + "testVar=''' testVar text. '''\ntestVar2=''' testVar2 text. '''\n", + 1, + 62, + test_file) + == 62); + simple_archiver_helper_cleanup_FILE(&test_file); + + test_file = fopen(test_http_template_html_filename, "w"); + ASSERT_TRUE(test_file); + + ASSERT_TRUE( + fwrite( + "

{{{testVar}}}


{{{testVar2}}}

", + 1, + 49, + test_file) + == 49); + simple_archiver_helper_cleanup_FILE(&test_file); + + simple_archiver_list_free(&required_names); + required_names = simple_archiver_list_init(); + simple_archiver_list_add( + required_names, + "HTML_FILE", + simple_archiver_helper_datastructure_cleanup_nop); + + c_simple_http_clean_up_parsed_config(&config); + config = c_simple_http_parse_config( + test_http_template_filename3, + "PATH", + required_names + ); + ASSERT_TRUE(config.paths != NULL); + + buf = c_simple_http_path_to_generated("/", &config); + ASSERT_TRUE(buf != NULL); + printf("%s\n", buf); + ASSERT_TRUE( + strcmp( + buf, + "

testVar text.


testVar2 text.

") + == 0); + simple_archiver_helper_cleanup_c_string(&buf); } RETURN()