]> git.seodisparate.com - c_simple_http/commitdiff
Change template generation: output used filenames
authorStephen Seo <seo.disparate@gmail.com>
Sun, 22 Sep 2024 05:26:07 +0000 (14:26 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Sun, 22 Sep 2024 05:26:07 +0000 (14:26 +0900)
src/http.c
src/http_template.c
src/http_template.h
src/test.c

index f79797d942e1012d91ccb647eb7b9f167fd7409f..08165dba0ef88d838348d0d34b9eeaf9ceeeb0c5 100644 (file)
@@ -162,7 +162,8 @@ char *c_simple_http_request_response(
   char *generated_buf = c_simple_http_path_to_generated(
     stripped_path ? stripped_path : request_path,
     templates,
-    &generated_size);
+    &generated_size,
+    NULL); // TODO Use the output parameter "filenames list" here.
 
   if (!generated_buf || generated_size == 0) {
     fprintf(stderr, "ERROR Unable to generate response html for path \"%s\"!\n",
index 510ee8b83bed144dbc711322d77a5aaf32d2550a..2a47a3d5cf48c0cddf6d3a114756d121013a50ec 100644 (file)
@@ -96,10 +96,14 @@ int c_simple_http_internal_ends_with_FILE(const char *c_string) {
 char *c_simple_http_path_to_generated(
     const char *path,
     const C_SIMPLE_HTTP_HTTPTemplates *templates,
-    size_t *output_buf_size) {
+    size_t *output_buf_size,
+    SDArchiverLinkedList **files_list_out) {
   if (output_buf_size) {
     *output_buf_size = 0;
   }
+  if (files_list_out) {
+    *files_list_out = simple_archiver_list_init();
+  }
   size_t path_len_size_t = strlen(path) + 1;
   if (path_len_size_t > 0xFFFFFFFF) {
     fprintf(stderr, "ERROR: Path string is too large!\n");
@@ -139,6 +143,11 @@ 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_filename) + 1);
+      strcpy(html_filename_copy, html_filename);
+      simple_archiver_list_add(*files_list_out, html_filename_copy, NULL);
+    }
   } else {
     char *stored_html =
       simple_archiver_hash_map_get(wrapped_hash_map->hash_map, "HTML", 5);
@@ -258,6 +267,12 @@ char *c_simple_http_path_to_generated(
                         value_c_str);
                 return NULL;
               }
+              if (files_list_out) {
+                char *variable_filename = malloc(strlen(value_c_str) + 1);
+                strcpy(variable_filename, value_c_str);
+                simple_archiver_list_add(
+                    *files_list_out, variable_filename, NULL);
+              }
             } else {
               // Variable data is "value_c_str".
               template_node =
index ae4586b549b48861a25b006710d21dc981dadf42..8aa12ba560ca408899fb7ace7ea778b7ae85f107 100644 (file)
 // Standard library includes.
 #include <stddef.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 non-NULL, it will be set to the size of the returned
-// buffer.
+// Third-party includes.
+#include <SimpleArchiver/src/data_structures/linked_list.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
+// non-NULL, it will be set to the size of the returned buffer. If
+// "files_list_out" is non-NULL, then the pointer will be set to a newly
+// allocated linked-list containing filenames used in generating the HTML. This
+// newly allocated linked-list must be freed after use.
 char *c_simple_http_path_to_generated(
   const char *path,
   const C_SIMPLE_HTTP_HTTPTemplates *templates,
-  size_t *output_buf_size);
+  size_t *output_buf_size,
+  SDArchiverLinkedList **files_list_out);
 
 #endif
 
index 66c4d0e0dc6e6d4392dc164122af8da379217216..241b62e1b2885131a9314c2bfd9c4f1879085171 100644 (file)
@@ -5,6 +5,7 @@
 #include <stdint.h>
 
 // Local includes.
+#include "SimpleArchiver/src/data_structures/linked_list.h"
 #include "config.h"
 #include "http_template.h"
 #include "http.h"
@@ -89,6 +90,15 @@ 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) {
+      return 1;
+    }
+  }
+  return 0;
+}
+
 int main(void) {
   // Test config.
   {
@@ -252,12 +262,18 @@ int main(void) {
 
     size_t output_buf_size;
 
+    __attribute__((cleanup(simple_archiver_list_free)))
+    SDArchiverLinkedList *filenames_list = NULL;
+
     __attribute__((cleanup(simple_archiver_helper_cleanup_c_string)))
-    char *buf = c_simple_http_path_to_generated("/", &config, &output_buf_size);
+    char *buf = c_simple_http_path_to_generated(
+        "/", &config, &output_buf_size, &filenames_list);
     ASSERT_TRUE(buf != NULL);
     ASSERT_TRUE(strcmp(buf, "<h1>Test</h1>") == 0);
     CHECK_TRUE(output_buf_size == 13);
+    CHECK_TRUE(filenames_list->count == 0);
     simple_archiver_helper_cleanup_c_string(&buf);
+    simple_archiver_list_free(&filenames_list);
 
     __attribute__((cleanup(test_internal_cleanup_delete_temporary_file)))
     const char *test_http_template_filename2 =
@@ -288,7 +304,8 @@ int main(void) {
     );
     ASSERT_TRUE(config.paths != NULL);
 
-    buf = c_simple_http_path_to_generated("/", &config, &output_buf_size);
+    buf = c_simple_http_path_to_generated(
+        "/", &config, &output_buf_size, &filenames_list);
     ASSERT_TRUE(buf != NULL);
     printf("%s\n", buf);
     ASSERT_TRUE(
@@ -297,7 +314,9 @@ int main(void) {
         "<h1> Some text. </h1><br><h2> More text. </h2>")
       == 0);
     CHECK_TRUE(output_buf_size == 46);
+    CHECK_TRUE(filenames_list->count == 0);
     simple_archiver_helper_cleanup_c_string(&buf);
+    simple_archiver_list_free(&filenames_list);
 
     __attribute__((cleanup(test_internal_cleanup_delete_temporary_file)))
     const char *test_http_template_filename3 =
@@ -351,7 +370,8 @@ int main(void) {
     );
     ASSERT_TRUE(config.paths != NULL);
 
-    buf = c_simple_http_path_to_generated("/", &config, &output_buf_size);
+    buf = c_simple_http_path_to_generated(
+        "/", &config, &output_buf_size, &filenames_list);
     ASSERT_TRUE(buf != NULL);
     printf("%s\n", buf);
     ASSERT_TRUE(
@@ -360,7 +380,14 @@ int main(void) {
         "<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,
+        test_internal_check_matching_string_in_list,
+        (void*)test_http_template_html_filename)
+      != NULL);
     simple_archiver_helper_cleanup_c_string(&buf);
+    simple_archiver_list_free(&filenames_list);
 
     __attribute__((cleanup(test_internal_cleanup_delete_temporary_file)))
     const char *test_http_template_filename4 =
@@ -434,7 +461,8 @@ int main(void) {
     );
     ASSERT_TRUE(config.paths != NULL);
 
-    buf = c_simple_http_path_to_generated("/", &config, &output_buf_size);
+    buf = c_simple_http_path_to_generated(
+        "/", &config, &output_buf_size, &filenames_list);
     ASSERT_TRUE(buf != NULL);
     printf("%s\n", buf);
     ASSERT_TRUE(
@@ -443,6 +471,17 @@ int main(void) {
         "<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,
+        test_internal_check_matching_string_in_list,
+        (void*)test_http_template_html_filename2)
+      != NULL);
+    CHECK_TRUE(simple_archiver_list_get(
+        filenames_list,
+        test_internal_check_matching_string_in_list,
+        (void*)test_http_template_html_var_filename)
+      != NULL);
     simple_archiver_helper_cleanup_c_string(&buf);
   }