}
typedef struct C_SIMPLE_HTTP_INTERNAL_RequiredCheck {
- SDArchiverHashMap *map_of_paths_and_their_vars;
- SDArchiverLinkedList *required;
+ const SDArchiverHashMap *map_of_paths_and_their_vars;
+ const SDArchiverLinkedList *required;
} C_SIMPLE_HTTP_INTERNAL_RequiredCheck;
int c_simple_http_check_required_iter_fn(void *path_void_str, void *ud) {
C_SIMPLE_HTTP_ParsedConfig c_simple_http_parse_config(
const char *config_filename,
const char *separating_key,
- SDArchiverLinkedList *required_names
+ const SDArchiverLinkedList *required_names
) {
C_SIMPLE_HTTP_ParsedConfig config;
config.hash_map = NULL;
last_template_idx = last_node->orig_end_idx;
}
template_node = malloc(sizeof(C_SIMPLE_HTTP_INTERNAL_Template_Node));
- template_node->html = malloc(idx - last_template_idx);
- memcpy(template_node->html, html_buf + idx, idx - last_template_idx);
- template_node->html_size = idx - last_template_idx;
+ template_node->html_size = idx - last_template_idx - 2;
+ template_node->html = malloc(template_node->html_size);
+ memcpy(
+ template_node->html,
+ html_buf + last_template_idx,
+ template_node->html_size);
template_node->orig_end_idx = idx + 1;
template_node->forced_next = NULL;
simple_archiver_list_add(template_html_list, template_node,
state &= 0xFFFFFFFE;
C_SIMPLE_HTTP_INTERNAL_Template_Node *last_node =
template_html_list->tail->prev->data;
- size_t end_of_var_size =
- idx - last_node->orig_end_idx;
- if (end_of_var_size <= 6) {
- fprintf(
- stderr,
- "ERROR generating from html template, invalid delimeter at index "
- "%lu!\n",
- idx);
- return NULL;
- }
- size_t var_size = end_of_var_size - 6;
+ size_t var_size = idx - 2 - last_node->orig_end_idx;
+ __attribute__((cleanup(simple_archiver_helper_cleanup_c_string)))
char *var = malloc(var_size + 1);
memcpy(
var,
- html_buf + last_node->orig_end_idx + 3,
+ html_buf + last_node->orig_end_idx,
var_size);
var[var_size] = 0;
const char *value_c_str =
// Standard library includes.
#include <stdio.h>
#include <string.h>
+#include <stdlib.h>
// Local includes.
#include "config.h"
+#include "http_template.h"
// Third party includes.
#include <SimpleArchiver/src/helpers.h>
} while (0);
int main(void) {
- // Test set up templates.
+ // Test config.
{
const char *test_config_filename = "/tmp/c_simple_http_test.config";
__attribute__((cleanup(simple_archiver_helper_cleanup_FILE)))
ASSERT_FALSE(templates.paths);
}
+ // Test http_template.
+ {
+ const char *test_http_template_filename =
+ "/tmp/c_simple_http_template_test.config";
+ __attribute__((cleanup(simple_archiver_helper_cleanup_FILE)))
+ FILE *test_file = fopen(test_http_template_filename, "w");
+ ASSERT_TRUE(test_file);
+
+ ASSERT_TRUE(
+ fwrite("PATH=/\nHTML=<h1>Test</h1>\n", 1, 26, test_file)
+ == 26);
+ simple_archiver_helper_cleanup_FILE(&test_file);
+
+ __attribute__((cleanup(simple_archiver_list_free)))
+ SDArchiverLinkedList *required_names = simple_archiver_list_init();
+ simple_archiver_list_add(
+ required_names,
+ "HTML",
+ simple_archiver_helper_datastructure_cleanup_nop
+ );
+
+ __attribute__((cleanup(c_simple_http_clean_up_parsed_config)))
+ C_SIMPLE_HTTP_ParsedConfig config = c_simple_http_parse_config(
+ test_http_template_filename,
+ "PATH",
+ required_names
+ );
+ ASSERT_TRUE(config.paths != NULL);
+
+ __attribute__((cleanup(simple_archiver_helper_cleanup_c_string)))
+ char *buf = c_simple_http_path_to_generated("/", &config);
+ ASSERT_TRUE(buf != NULL);
+ ASSERT_TRUE(strcmp(buf, "<h1>Test</h1>") == 0);
+ simple_archiver_helper_cleanup_c_string(&buf);
+
+ const char *test_http_template_filename2 =
+ "/tmp/c_simple_http_template_test2.config";
+ test_file = fopen(test_http_template_filename2, "w");
+ ASSERT_TRUE(test_file);
+
+ ASSERT_TRUE(
+ fwrite(
+ "PATH=/\nHTML=<h1>{{{testVar}}}</h1><br><h2>{{{testVar2}}}</h2>\n",
+ 1,
+ 62,
+ test_file)
+ == 62);
+ ASSERT_TRUE(
+ fwrite("testVar=''' Some text. '''\n", 1, 27, test_file)
+ == 27);
+ ASSERT_TRUE(
+ fwrite("testVar2=''' More text. '''\n", 1, 28, test_file)
+ == 28);
+ simple_archiver_helper_cleanup_FILE(&test_file);
+
+ c_simple_http_clean_up_parsed_config(&config);
+ config = c_simple_http_parse_config(
+ test_http_template_filename2,
+ "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> Some text. </h1><br><h2> More text. </h2>") == 0);
+ simple_archiver_helper_cleanup_c_string(&buf);
+ }
+
RETURN()
}