]> git.seodisparate.com - c_simple_http/commitdiff
Reload config file if cache is older than config
authorStephen Seo <seo.disparate@gmail.com>
Thu, 26 Sep 2024 02:51:42 +0000 (11:51 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Thu, 26 Sep 2024 02:51:42 +0000 (11:51 +0900)
src/html_cache.c
src/html_cache.h
src/http.c
src/http.h
src/test.c

index 212ccbac081dd7e4ccb1893eb68755df08021727..4ab6fabc23ff8c8bcd120e0ca8e0cb89d57e51e8 100644 (file)
@@ -27,6 +27,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.
@@ -206,7 +207,7 @@ int c_simple_http_cache_path(
     const char *path,
     const char *config_filename,
     const char *cache_dir,
-    const C_SIMPLE_HTTP_HTTPTemplates *templates,
+    C_SIMPLE_HTTP_HTTPTemplates *templates,
     char **buf_out) {
   if (!path) {
     fprintf(stderr, "ERROR cache_path function: path is NULL!\n");
@@ -262,6 +263,7 @@ int c_simple_http_cache_path(
   // Get "stat" info on the cache filename.
   uint_fast8_t force_cache_update = 0;
   struct stat cache_file_stat;
+  memset(&cache_file_stat, 0, sizeof(struct stat));
   ret = stat(cache_filename_full, &cache_file_stat);
   if (ret == -1) {
     if (errno == ENOENT) {
@@ -279,6 +281,7 @@ int c_simple_http_cache_path(
 
   // Get "stat" info on config file.
   struct stat config_file_stat;
+  memset(&config_file_stat, 0, sizeof(struct stat));
   ret = stat(config_filename, &config_file_stat);
   if (ret == -1) {
     if (errno == ENOENT) {
@@ -395,6 +398,24 @@ CACHE_FILE_WRITE_CHECK:
          && cache_file_stat.st_mtim.tv_nsec < config_file_stat.st_mtim.tv_nsec))
   {
     // Cache file is out of date.
+
+    if (cache_file_stat.st_mtim.tv_sec < config_file_stat.st_mtim.tv_sec
+        || (cache_file_stat.st_mtim.tv_sec == config_file_stat.st_mtim.tv_sec
+           && cache_file_stat.st_mtim.tv_nsec
+              < config_file_stat.st_mtim.tv_nsec))
+    {
+      // Reload config file.
+      C_SIMPLE_HTTP_HTTPTemplates new_parsed_config =
+        c_simple_http_parse_config(
+          config_filename,
+          "PATH",
+          NULL);
+      if (new_parsed_config.hash_map) {
+        simple_archiver_hash_map_free(&templates->hash_map);
+        *templates = new_parsed_config;
+      }
+    }
+
     __attribute__((cleanup(simple_archiver_helper_cleanup_FILE)))
     FILE *cache_fd = fopen(cache_filename_full, "w");
     if (fwrite("--- CACHE ENTRY ---\n", 1, 20, cache_fd) != 20) {
index 95a36ab84983184bc1c46fabcf671cb76f3d17ef..ebaa3850b1caf490cf313d3787dd66b4cea68421 100644 (file)
@@ -36,7 +36,7 @@ int c_simple_http_cache_path(
   const char *path,
   const char *config_filename,
   const char *cache_dir,
-  const C_SIMPLE_HTTP_HTTPTemplates *templates,
+  C_SIMPLE_HTTP_HTTPTemplates *templates,
   char **buf_out);
 
 #endif
index e32e54bc28e8de37f476bbd7e00a29bd21ee9814..9ced3989564681cac99aea512b1f6a2e0de21927 100644 (file)
@@ -62,7 +62,7 @@ const char *c_simple_http_response_code_error_to_response(
 char *c_simple_http_request_response(
     const char *request,
     uint32_t size,
-    const C_SIMPLE_HTTP_HTTPTemplates *templates,
+    C_SIMPLE_HTTP_HTTPTemplates *templates,
     size_t *out_size,
     enum C_SIMPLE_HTTP_ResponseCode *out_response_code,
     const char *cache_dir,
index 725fbf84cb424bdd4537f5544397290be947a71f..87618c425151ecaf9b8ffaa403f9fc9422c51483 100644 (file)
@@ -46,7 +46,7 @@ const char *c_simple_http_response_code_error_to_response(
 char *c_simple_http_request_response(
   const char *request,
   uint32_t size,
-  const C_SIMPLE_HTTP_HTTPTemplates *templates,
+  C_SIMPLE_HTTP_HTTPTemplates *templates,
   size_t *out_size,
   enum C_SIMPLE_HTTP_ResponseCode *out_response_code,
   const char *cache_dir,
index 8430b109785b02495b57289fd2b39db5129ce5c8..b3976792b1de313a3178d53fa8baecb920ba0982 100644 (file)
@@ -908,6 +908,40 @@ int main(void) {
     ASSERT_TRUE(cache_file_exists);
     CHECK_TRUE(cache_file_size_2 == cache_file_size_3);
 
+    // Edit config file.
+    test_file = fopen(test_http_template_filename5, "w");
+    ASSERT_TRUE(test_file);
+
+    ASSERT_TRUE(
+      fwrite(
+        "PATH=/\nHTML='''<h1>{{{VAR_FILE}}}</h1>'''\n",
+        1,
+        42,
+        test_file)
+      == 42);
+    ASSERT_TRUE(
+      fwrite(
+        "VAR_FILE=/tmp/c_simple_http_template_test_var2.html\n",
+        1,
+        52,
+        test_file)
+      == 52);
+
+    fclose(test_file);
+
+    // Re-run cache function, checking that it is invalidated.
+    int_ret = c_simple_http_cache_path(
+      "/",
+      test_http_template_filename5,
+      "/tmp/c_simple_http_cache_dir",
+      &templates,
+      &buf);
+    CHECK_TRUE(int_ret > 0);
+    ASSERT_TRUE(buf);
+    CHECK_TRUE(strcmp(buf, "<h1>Alternate test text.<br>Yep.</h1>") == 0);
+    free(buf);
+    buf = NULL;
+
     // Cleanup.
     remove("/tmp/c_simple_http_cache_dir/ROOT");
     rmdir("/tmp/c_simple_http_cache_dir");