]> git.seodisparate.com - c_simple_http/commitdiff
Ensure http_template.c keeps track of _FILE files
authorStephen Seo <seo.disparate@gmail.com>
Thu, 21 Nov 2024 05:05:33 +0000 (14:05 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Thu, 21 Nov 2024 05:05:33 +0000 (14:05 +0900)
example_config/each_file_one.html [new file with mode: 0644]
example_config/each_file_two.html [new file with mode: 0644]
example_config/each_file_zero.html [new file with mode: 0644]
example_config/example.config
src/html_cache.c
src/http_template.c
src/http_template.h
src/test.c

diff --git a/example_config/each_file_one.html b/example_config/each_file_one.html
new file mode 100644 (file)
index 0000000..7de5aa7
--- /dev/null
@@ -0,0 +1 @@
+<b>Each File ONE</b><br>
diff --git a/example_config/each_file_two.html b/example_config/each_file_two.html
new file mode 100644 (file)
index 0000000..6b66147
--- /dev/null
@@ -0,0 +1 @@
+<b>Each File TWO</b><br>
diff --git a/example_config/each_file_zero.html b/example_config/each_file_zero.html
new file mode 100644 (file)
index 0000000..b97d444
--- /dev/null
@@ -0,0 +1 @@
+<b>Each File ZERO</b><br>
index c82d4d73609702fed68c48aa8261425919d864ef..92f3124602dd7c23ec608145f58c2c230eac48f9 100644 (file)
@@ -100,6 +100,9 @@ Nested FOREACH:<br>
       {{{ArrayValueThird}}}
       {{{!FOREACH ArrayValueFourth}}}
         {{{ArrayValueFourth}}}
+        {{{!FOREACH Each_FILE}}}
+          {{{Each_FILE}}}
+        {{{!ENDFOREACH}}}
       {{{!ENDFOREACH}}}
     {{{!ENDFOREACH}}}
   {{{!ENDFOREACH}}}
@@ -143,6 +146,10 @@ EachTestHead='''Third Entry Head'''
 EachTestMid='''Third Entry Mid'''
 EachTestTail='''Third Entry Tail'''
 
+Each_FILE='''example_config/each_file_zero.html'''
+Each_FILE='''example_config/each_file_one.html'''
+Each_FILE='''example_config/each_file_two.html'''
+
 PATH=/inner
 HTML_FILE='''example_config/inner.html'''
 VAR_FILE='''example_config/var.html'''
index 4b8403f1891345d1285a9797207503c935e5cba0..82ac5ffe9a42d733db4d630b3c8d8ed26139ba02 100644 (file)
 #include "helpers.h"
 #include "http_template.h"
 
-int c_simple_http_internal_write_filenames_to_cache_file(void *data, void *ud) {
-  char *filename = data;
+int c_simple_http_internal_write_filenames_to_cache_file(
+    const void *key,
+    __attribute__((unused)) size_t key_size,
+    __attribute__((unused)) const void *value,
+    void *ud) {
+  const char *filename = key;
   FILE *cache_fd = ud;
 
   const size_t filename_size = strlen(filename);
@@ -436,8 +440,8 @@ CACHE_FILE_WRITE_CHECK:
       return -5;
     }
 
-    __attribute__((cleanup(simple_archiver_list_free)))
-    SDArchiverLinkedList *used_filenames = NULL;
+    __attribute__((cleanup(simple_archiver_hash_map_free)))
+    SDArchiverHashMap *used_filenames = NULL;
 
     size_t generated_html_size = 0;
 
@@ -452,10 +456,10 @@ CACHE_FILE_WRITE_CHECK:
       return -4;
     }
 
-    if (simple_archiver_list_get(
-        used_filenames,
-        c_simple_http_internal_write_filenames_to_cache_file,
-        cache_fd)) {
+    if (simple_archiver_hash_map_iter(
+          used_filenames,
+          c_simple_http_internal_write_filenames_to_cache_file,
+          cache_fd) != 0) {
       fprintf(stderr, "ERROR Failed to write filenames to cache file!\n");
       return -6;
     } else if (fwrite("--- BEGIN HTML ---\n", 1, 19, cache_fd) != 19) {
index e5eed4b91e436dd3c495f06bf7226df12ddc7e59..829f169fae6a31f5e2ee8c0c5e5e45066646c340 100644 (file)
@@ -14,8 +14,6 @@
 // OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 // PERFORMANCE OF THIS SOFTWARE.
 
-// TODO: Update files map.
-
 #include "http_template.h"
 
 // Standard library includes.
@@ -25,6 +23,7 @@
 
 // Third party includes.
 #include <SimpleArchiver/src/data_structures/linked_list.h>
+#include <SimpleArchiver/src/data_structures/hash_map.h>
 #include <SimpleArchiver/src/helpers.h>
 
 // Local includes.
@@ -62,7 +61,22 @@ int c_simple_http_internal_handle_inside_delimeters(
     SDArchiverHashMap *array_vars,
     const C_SIMPLE_HTTP_ParsedConfig *wrapped_hash_map,
     SDArchiverLinkedList *string_part_list,
-    SDArchiverLinkedList **files_list_out);
+    SDArchiverHashMap **files_set_out);
+
+void c_simple_http_internal_set_out_insert(SDArchiverHashMap **set,
+                                           const char *str) {
+  if (set
+      && *set
+      && simple_archiver_hash_map_get(*set, str, strlen(str) + 1) == NULL) {
+    simple_archiver_hash_map_insert(
+      *set,
+      (void*)1,
+      strdup(str),
+      strlen(str) + 1,
+      simple_archiver_helper_datastructure_cleanup_nop,
+      NULL);
+  }
+}
 
 void c_simple_http_internal_cleanup_ArrayVar(
     C_SIMPLE_HTTP_ArrayVar **array_var) {
@@ -169,7 +183,7 @@ int c_simple_http_internal_parse_if_expression(
     char **left_side_out,
     char **right_side_out,
     uint_fast8_t *is_equality_out,
-    SDArchiverLinkedList **files_list_out) {
+    SDArchiverHashMap **files_set_out) {
   if (!left_side_out || !right_side_out || !is_equality_out) {
     fprintf(stderr, "ERROR Internal error! %s\n", var);
     return 1;
@@ -288,11 +302,7 @@ int c_simple_http_internal_parse_if_expression(
     }
     c_simple_http_trim_end_whitespace(*left_side_out);
     left_side_size = strlen(*left_side_out);
-    if (files_list_out) {
-      simple_archiver_list_add(*files_list_out,
-                               strdup(config_value->value),
-                               NULL);
-    }
+    c_simple_http_internal_set_out_insert(files_set_out, config_value->value);
   } else {
     *left_side_out = strdup(config_value->value);
     c_simple_http_trim_end_whitespace(*left_side_out);
@@ -346,7 +356,8 @@ int c_simple_http_internal_populate_array_vars(
     const C_SIMPLE_HTTP_HashMapWrapper *wrapped_hash_map,
     const char *var,
     SDArchiverHashMap *array_vars,
-    SDArchiverLinkedList *array_vars_keys) {
+    SDArchiverLinkedList *array_vars_keys,
+    SDArchiverHashMap **files_set_out) {
   C_SIMPLE_HTTP_ConfigValue *config_value =
     simple_archiver_hash_map_get(wrapped_hash_map->hash_map,
                                  *for_each_var_name,
@@ -378,6 +389,7 @@ int c_simple_http_internal_populate_array_vars(
       c_simple_http_internal_cleanup_ArrayVar(&array_var);
       return 1;
     }
+    c_simple_http_internal_set_out_insert(files_set_out, config_value->value);
   } else {
     array_var->value = strdup(config_value->value);
   }
@@ -400,6 +412,8 @@ int c_simple_http_internal_populate_array_vars(
           c_simple_http_internal_cleanup_ArrayVar(&array_var);
           return 1;
         }
+        c_simple_http_internal_set_out_insert(files_set_out,
+                                              config_value->value);
       } else {
         current_array_var->value = strdup(config_value->value);
       }
@@ -438,7 +452,7 @@ int c_simple_http_internal_parse_iterate(
     SDArchiverHashMap *array_vars,
     SDArchiverLinkedList *string_part_list,
     const C_SIMPLE_HTTP_ParsedConfig *wrapped_hash_map,
-    SDArchiverLinkedList **files_list_out) {
+    SDArchiverHashMap **files_set_out) {
   C_SIMPLE_HTTP_String_Part string_part;
   if (((*state) & 1) == 0) {
     // Using 0x7B instead of left curly-brace due to bug in vim navigation.
@@ -511,7 +525,7 @@ int c_simple_http_internal_parse_iterate(
               array_vars,
               wrapped_hash_map,
               string_part_list,
-              files_list_out) != 0) {
+              files_set_out) != 0) {
           return 1;
         }
       }
@@ -561,7 +575,7 @@ int c_simple_http_internal_handle_inside_delimeters(
     SDArchiverHashMap *array_vars,
     const C_SIMPLE_HTTP_ParsedConfig *wrapped_hash_map,
     SDArchiverLinkedList *string_part_list,
-    SDArchiverLinkedList **files_list_out) {
+    SDArchiverHashMap **files_set_out) {
   C_SIMPLE_HTTP_String_Part string_part;
   if (var_size == 0) {
     fprintf(stderr, "ERROR No characters within delimeters!\n");
@@ -588,7 +602,7 @@ int c_simple_http_internal_handle_inside_delimeters(
           &left_side,
           &right_side,
           &is_equality,
-          files_list_out)) {
+          files_set_out)) {
         return 1;
       } else if (!left_side || !right_side) {
         fprintf(stderr, "ERROR Internal error! %s\n", var);
@@ -662,7 +676,7 @@ int c_simple_http_internal_handle_inside_delimeters(
           &left_side,
           &right_side,
           &is_equality,
-          files_list_out)) {
+          files_set_out)) {
         return 1;
       } else if (!left_side || !right_side) {
         fprintf(stderr, "ERROR Internal error! %s\n", var);
@@ -824,11 +838,8 @@ int c_simple_http_internal_handle_inside_delimeters(
           fprintf(stderr, "ERROR Failed to get value from FILE! %s\n", var);
           return 1;
         }
-        if (files_list_out) {
-          simple_archiver_list_add(*files_list_out,
-                                   strdup(config_value->value),
-                                   NULL);
-        }
+        c_simple_http_internal_set_out_insert(files_set_out,
+                                              config_value->value);
       } else {
         value_contents = strdup(config_value->value);
         value_contents_size = strlen(value_contents);
@@ -904,7 +915,8 @@ int c_simple_http_internal_handle_inside_delimeters(
               wrapped_hash_map,
               var,
               array_vars,
-              for_state->array_vars_keys) != 0) {
+              for_state->array_vars_keys,
+              files_set_out) != 0) {
             if (for_each_var_name) {
               free(for_each_var_name);
             }
@@ -940,7 +952,8 @@ int c_simple_http_internal_handle_inside_delimeters(
             wrapped_hash_map,
             var,
             array_vars,
-            for_state->array_vars_keys) != 0) {
+            for_state->array_vars_keys,
+            files_set_out) != 0) {
           if (for_each_var_name) {
             free(for_each_var_name);
           }
@@ -1017,7 +1030,7 @@ int c_simple_http_internal_handle_inside_delimeters(
                                                    array_vars,
                                                    string_part_list,
                                                    wrapped_hash_map,
-                                                   files_list_out) != 0) {
+                                                   files_set_out) != 0) {
             return 1;
           }
         }
@@ -1113,10 +1126,8 @@ int c_simple_http_internal_handle_inside_delimeters(
         string_part.extra = html_buf_idx + 1;
 
         string_part.buf[string_part.size - 1] = 0;
-        if (files_list_out) {
-          char *variable_filename = strdup(config_value->value);
-          simple_archiver_list_add(*files_list_out, variable_filename, NULL);
-        }
+        c_simple_http_internal_set_out_insert(files_set_out,
+                                              config_value->value);
       } else {
         // Variable data is "config_value->value".
         string_part.size = strlen(config_value->value) + 1;
@@ -1145,12 +1156,12 @@ char *c_simple_http_path_to_generated(
     const char *path,
     const C_SIMPLE_HTTP_HTTPTemplates *templates,
     size_t *output_buf_size,
-    SDArchiverLinkedList **files_list_out) {
+    SDArchiverHashMap **files_set_out) {
   if (output_buf_size) {
     *output_buf_size = 0;
   }
-  if (files_list_out) {
-    *files_list_out = simple_archiver_list_init();
+  if (files_set_out) {
+    *files_set_out = simple_archiver_hash_map_init();
   }
   size_t path_len_size_t = strlen(path) + 1;
   if (path_len_size_t > 0xFFFFFFFF) {
@@ -1191,11 +1202,8 @@ char *c_simple_http_path_to_generated(
     }
     html_buf[html_file_size] = 0;
     html_buf_size = (size_t)html_file_size;
-    if (files_list_out) {
-      char *html_filename_copy = malloc(strlen(html_file_value->value) + 1);
-      strcpy(html_filename_copy, html_file_value->value);
-      simple_archiver_list_add(*files_list_out, html_filename_copy, NULL);
-    }
+    c_simple_http_internal_set_out_insert(files_set_out,
+                                          html_file_value->value);
   } else {
     C_SIMPLE_HTTP_ConfigValue *stored_html_config_value =
       simple_archiver_hash_map_get(wrapped_hash_map->hash_map, "HTML", 5);
@@ -1255,7 +1263,7 @@ char *c_simple_http_path_to_generated(
                                             array_vars,
                                             string_part_list,
                                             wrapped_hash_map,
-                                            files_list_out) != 0) {
+                                            files_set_out) != 0) {
       return NULL;
     }
   }
index 8aa12ba560ca408899fb7ace7ea778b7ae85f107..6fecde434e1348552264e025a402b7bf62a1ea38 100644 (file)
 #ifndef SEODISPARATE_COM_C_SIMPLE_HTTP_HTTP_TEMPLATE_H_
 #define SEODISPARATE_COM_C_SIMPLE_HTTP_HTTP_TEMPLATE_H_
 
-#include "http.h"
 
 // Standard library includes.
 #include <stddef.h>
 
 // Third-party includes.
 #include <SimpleArchiver/src/data_structures/linked_list.h>
+#include <SimpleArchiver/src/data_structures/hash_map.h>
+
+// Local includes.
+#include "http.h"
 
 // Returns non-NULL on success, which must be free'd after use. Takes a path
 // string and templates and returns the generated HTML. If "output_buf_size" is
@@ -35,7 +38,7 @@ char *c_simple_http_path_to_generated(
   const char *path,
   const C_SIMPLE_HTTP_HTTPTemplates *templates,
   size_t *output_buf_size,
-  SDArchiverLinkedList **files_list_out);
+  SDArchiverHashMap **files_set_out);
 
 #endif
 
index 9034a16c946cf5fc0740f09383c681bd86df29ea..5fd5feb377c14add2994a231f3f3d2d39656e62a 100644 (file)
@@ -99,9 +99,13 @@ void test_internal_cleanup_delete_temporary_file(const char **filename) {
   }
 }
 
-int test_internal_check_matching_string_in_list(void *value, void *ud) {
-  if (value && ud) {
-    if (strcmp(value, ud) == 0) {
+int test_internal_check_matching_string_in_list(
+    const void *key,
+    __attribute__((unused)) size_t key_size,
+    __attribute__((unused)) const void *value,
+    void *ud) {
+  if (key && ud) {
+    if (strcmp(key, ud) == 0) {
       return 1;
     }
   }
@@ -278,18 +282,18 @@ int main(int argc, char **argv) {
 
     size_t output_buf_size;
 
-    __attribute__((cleanup(simple_archiver_list_free)))
-    SDArchiverLinkedList *filenames_list = NULL;
+    __attribute__((cleanup(simple_archiver_hash_map_free)))
+    SDArchiverHashMap *filenames_set = NULL;
 
     __attribute__((cleanup(simple_archiver_helper_cleanup_c_string)))
     char *buf = c_simple_http_path_to_generated(
-        "/", &config, &output_buf_size, &filenames_list);
+        "/", &config, &output_buf_size, &filenames_set);
     ASSERT_TRUE(buf != NULL);
     ASSERT_TRUE(strcmp(buf, "<h1>Test</h1>") == 0);
     CHECK_TRUE(output_buf_size == 13);
-    CHECK_TRUE(filenames_list->count == 0);
+    CHECK_TRUE(filenames_set->count == 0);
     simple_archiver_helper_cleanup_c_string(&buf);
-    simple_archiver_list_free(&filenames_list);
+    simple_archiver_hash_map_free(&filenames_set);
 
     __attribute__((cleanup(test_internal_cleanup_delete_temporary_file)))
     const char *test_http_template_filename2 =
@@ -321,7 +325,7 @@ int main(int argc, char **argv) {
     ASSERT_TRUE(config.paths != NULL);
 
     buf = c_simple_http_path_to_generated(
-        "/", &config, &output_buf_size, &filenames_list);
+        "/", &config, &output_buf_size, &filenames_set);
     ASSERT_TRUE(buf != NULL);
     //printf("%s\n", buf);
     ASSERT_TRUE(
@@ -330,9 +334,9 @@ int main(int argc, char **argv) {
         "<h1> Some text. </h1><br><h2> More text. </h2>")
       == 0);
     CHECK_TRUE(output_buf_size == 46);
-    CHECK_TRUE(filenames_list->count == 0);
+    CHECK_TRUE(filenames_set->count == 0);
     simple_archiver_helper_cleanup_c_string(&buf);
-    simple_archiver_list_free(&filenames_list);
+    simple_archiver_hash_map_free(&filenames_set);
 
     __attribute__((cleanup(test_internal_cleanup_delete_temporary_file)))
     const char *test_http_template_filename3 =
@@ -387,7 +391,7 @@ int main(int argc, char **argv) {
     ASSERT_TRUE(config.paths != NULL);
 
     buf = c_simple_http_path_to_generated(
-        "/", &config, &output_buf_size, &filenames_list);
+        "/", &config, &output_buf_size, &filenames_set);
     ASSERT_TRUE(buf != NULL);
     //printf("%s\n", buf);
     ASSERT_TRUE(
@@ -396,14 +400,14 @@ int main(int argc, char **argv) {
         "<h1> testVar text. </h1><br><h2> testVar2 text. </h2>")
       == 0);
     CHECK_TRUE(output_buf_size == 53);
-    CHECK_TRUE(filenames_list->count == 1);
-    CHECK_TRUE(simple_archiver_list_get(
-        filenames_list,
+    CHECK_TRUE(filenames_set->count == 1);
+    CHECK_TRUE(simple_archiver_hash_map_iter(
+        filenames_set,
         test_internal_check_matching_string_in_list,
         (void*)test_http_template_html_filename)
-      != NULL);
+      != 0);
     simple_archiver_helper_cleanup_c_string(&buf);
-    simple_archiver_list_free(&filenames_list);
+    simple_archiver_hash_map_free(&filenames_set);
 
     __attribute__((cleanup(test_internal_cleanup_delete_temporary_file)))
     const char *test_http_template_filename4 =
@@ -478,7 +482,7 @@ int main(int argc, char **argv) {
     ASSERT_TRUE(config.paths != NULL);
 
     buf = c_simple_http_path_to_generated(
-        "/", &config, &output_buf_size, &filenames_list);
+        "/", &config, &output_buf_size, &filenames_set);
     ASSERT_TRUE(buf != NULL);
     //printf("%s\n", buf);
     ASSERT_TRUE(
@@ -487,17 +491,17 @@ int main(int argc, char **argv) {
         "<h1> some test text in test var file. </h1>")
       == 0);
     CHECK_TRUE(output_buf_size == 43);
-    CHECK_TRUE(filenames_list->count == 2);
-    CHECK_TRUE(simple_archiver_list_get(
-        filenames_list,
+    CHECK_TRUE(filenames_set->count == 2);
+    CHECK_TRUE(simple_archiver_hash_map_iter(
+        filenames_set,
         test_internal_check_matching_string_in_list,
         (void*)test_http_template_html_filename2)
-      != NULL);
-    CHECK_TRUE(simple_archiver_list_get(
-        filenames_list,
+      != 0);
+    CHECK_TRUE(simple_archiver_hash_map_iter(
+        filenames_set,
         test_internal_check_matching_string_in_list,
         (void*)test_http_template_html_var_filename)
-      != NULL);
+      != 0);
     simple_archiver_helper_cleanup_c_string(&buf);
   }