diff --git a/src/config.c b/src/config.c index 1e999c9..e8ce6fd 100644 --- a/src/config.c +++ b/src/config.c @@ -121,8 +121,8 @@ int c_simple_http_required_iter_fn(void *data, void *ud) { } 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) { @@ -150,7 +150,7 @@ 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; diff --git a/src/config.h b/src/config.h index 02fd9e1..5794761 100644 --- a/src/config.h +++ b/src/config.h @@ -62,7 +62,7 @@ typedef C_SIMPLE_HTTP_ParsedConfig C_SIMPLE_HTTP_HashMapWrapper; C_SIMPLE_HTTP_ParsedConfig c_simple_http_parse_config( const char *config_filename, const char *separating_key, - SDArchiverLinkedList *required_names + const SDArchiverLinkedList *required_names ); void c_simple_http_clean_up_parsed_config(C_SIMPLE_HTTP_ParsedConfig *config); diff --git a/src/http_template.c b/src/http_template.c index 43d8ca0..752dcae 100644 --- a/src/http_template.c +++ b/src/http_template.c @@ -154,9 +154,12 @@ char *c_simple_http_path_to_generated( 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, @@ -176,21 +179,12 @@ char *c_simple_http_path_to_generated( 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 = diff --git a/src/test.c b/src/test.c index c7a105b..0b6fc35 100644 --- a/src/test.c +++ b/src/test.c @@ -1,9 +1,11 @@ // Standard library includes. #include #include +#include // Local includes. #include "config.h" +#include "http_template.h" // Third party includes. #include @@ -78,7 +80,7 @@ static int checks_passed = 0; } 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))) @@ -205,6 +207,76 @@ int main(void) { 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=

Test

\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, "

Test

") == 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=

{{{testVar}}}


{{{testVar2}}}

\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, "

Some text.


More text.

") == 0); + simple_archiver_helper_cleanup_c_string(&buf); + } + RETURN() }