]> git.seodisparate.com - c_simple_http/commitdiff
Test using "HTML_FILE" var, fixes
authorStephen Seo <seo.disparate@gmail.com>
Wed, 4 Sep 2024 08:22:44 +0000 (17:22 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Wed, 4 Sep 2024 08:22:44 +0000 (17:22 +0900)
src/config.c
src/http_template.c
src/test.c

index e8ce6fdfa32af2637b7997df6281810e83d64638..81c756cc86805c5b1c913ec1ac76cc901cc0fb24 100644 (file)
@@ -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,
index 752dcae36552dde39906ee9434c6c2b0a6cacb0e..091eb135c5129f74c4c0f7ffdd653f1ca6119d96 100644 (file)
@@ -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.
index 45b59ce890006c47cabf44ce5c54b019580a83ba..d75f25d12daefd181a714ad04482628c6f42e306 100644 (file)
@@ -279,6 +279,66 @@ int main(void) {
         "<h1> Some text. </h1><br><h2> More text. </h2>")
       == 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(
+          "<h1>{{{testVar}}}</h1><br><h2>{{{testVar2}}}</h2>",
+          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,
+        "<h1> testVar text. </h1><br><h2> testVar2 text. </h2>")
+      == 0);
+    simple_archiver_helper_cleanup_c_string(&buf);
   }
 
   RETURN()