Test using "HTML_FILE" var, fixes
This commit is contained in:
parent
1d5fb6bbbd
commit
2d96295555
3 changed files with 64 additions and 43 deletions
44
src/config.c
44
src/config.c
|
@ -395,48 +395,8 @@ C_SIMPLE_HTTP_ParsedConfig c_simple_http_parse_config(
|
|||
|
||||
unsigned char *key = malloc(key_idx);
|
||||
memcpy(key, key_buf, key_idx);
|
||||
unsigned char *value;
|
||||
if (strcmp((char*)key_buf, "HTML_FILE") == 0) {
|
||||
__attribute__((cleanup(simple_archiver_helper_cleanup_FILE)))
|
||||
FILE *html_file = fopen((char*)value_buf, "r");
|
||||
if (!html_file) {
|
||||
fprintf(stderr,
|
||||
"ERROR: Internal error failed to open HTML_FILE \"%s\"!",
|
||||
value_buf);
|
||||
c_simple_http_clean_up_parsed_config(&config);
|
||||
config.hash_map = NULL;
|
||||
return config;
|
||||
}
|
||||
|
||||
fseek(html_file, 0, SEEK_END);
|
||||
long file_size = ftell(html_file);
|
||||
if (file_size <= 0) {
|
||||
fprintf(stderr,
|
||||
"ERROR: Internal error HTML_FILE \"%s\" is invalid size!",
|
||||
value_buf);
|
||||
c_simple_http_clean_up_parsed_config(&config);
|
||||
config.hash_map = NULL;
|
||||
return config;
|
||||
}
|
||||
fseek(html_file, 0, SEEK_SET);
|
||||
unsigned long file_size_u = file_size;
|
||||
|
||||
unsigned char *read_buf = malloc(file_size_u);
|
||||
size_t read_amount = 0;
|
||||
read_amount = fread(read_buf, 1, file_size_u, html_file);
|
||||
if (read_amount != file_size_u) {
|
||||
fprintf(stderr, "ERROR: Failed to read HTML_FILE \"%s\"!\n",
|
||||
value_buf);
|
||||
free(read_buf);
|
||||
c_simple_http_clean_up_parsed_config(&config);
|
||||
config.hash_map = NULL;
|
||||
return config;
|
||||
}
|
||||
value = read_buf;
|
||||
} else {
|
||||
value = malloc(value_idx);
|
||||
memcpy(value, value_buf, value_idx);
|
||||
}
|
||||
unsigned char *value = malloc(value_idx);
|
||||
memcpy(value, value_buf, value_idx);
|
||||
|
||||
if (simple_archiver_hash_map_insert(&hash_map_wrapper->paths,
|
||||
value,
|
||||
|
|
|
@ -248,7 +248,7 @@ char *c_simple_http_path_to_generated(
|
|||
return NULL;
|
||||
}
|
||||
C_SIMPLE_HTTP_INTERNAL_Template_Node to_fill;
|
||||
to_fill.html = malloc(final_size);
|
||||
to_fill.html = malloc(final_size + 1);
|
||||
to_fill.html_size = 0;
|
||||
to_fill.html_capacity = final_size;
|
||||
if (simple_archiver_list_get(
|
||||
|
@ -259,6 +259,7 @@ char *c_simple_http_path_to_generated(
|
|||
free(to_fill.html);
|
||||
return NULL;
|
||||
}
|
||||
to_fill.html[final_size] = 0;
|
||||
return to_fill.html;
|
||||
} else {
|
||||
// Prevent cleanup fn from "free"ing html_buf and return it verbatim.
|
||||
|
|
60
src/test.c
60
src/test.c
|
@ -279,6 +279,66 @@ int main(void) {
|
|||
"<h1> Some text. </h1><br><h2> More text. </h2>")
|
||||
== 0);
|
||||
simple_archiver_helper_cleanup_c_string(&buf);
|
||||
|
||||
const char *test_http_template_filename3 =
|
||||
"/tmp/c_simple_http_template_test3.config";
|
||||
const char *test_http_template_html_filename =
|
||||
"/tmp/c_simple_http_template_test.html";
|
||||
test_file = fopen(test_http_template_filename3, "w");
|
||||
ASSERT_TRUE(test_file);
|
||||
|
||||
ASSERT_TRUE(
|
||||
fwrite(
|
||||
"PATH=/\nHTML_FILE=/tmp/c_simple_http_template_test.html\n",
|
||||
1,
|
||||
55,
|
||||
test_file)
|
||||
== 55);
|
||||
ASSERT_TRUE(
|
||||
fwrite(
|
||||
"testVar=''' testVar text. '''\ntestVar2=''' testVar2 text. '''\n",
|
||||
1,
|
||||
62,
|
||||
test_file)
|
||||
== 62);
|
||||
simple_archiver_helper_cleanup_FILE(&test_file);
|
||||
|
||||
test_file = fopen(test_http_template_html_filename, "w");
|
||||
ASSERT_TRUE(test_file);
|
||||
|
||||
ASSERT_TRUE(
|
||||
fwrite(
|
||||
"<h1>{{{testVar}}}</h1><br><h2>{{{testVar2}}}</h2>",
|
||||
1,
|
||||
49,
|
||||
test_file)
|
||||
== 49);
|
||||
simple_archiver_helper_cleanup_FILE(&test_file);
|
||||
|
||||
simple_archiver_list_free(&required_names);
|
||||
required_names = simple_archiver_list_init();
|
||||
simple_archiver_list_add(
|
||||
required_names,
|
||||
"HTML_FILE",
|
||||
simple_archiver_helper_datastructure_cleanup_nop);
|
||||
|
||||
c_simple_http_clean_up_parsed_config(&config);
|
||||
config = c_simple_http_parse_config(
|
||||
test_http_template_filename3,
|
||||
"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> testVar text. </h1><br><h2> testVar2 text. </h2>")
|
||||
== 0);
|
||||
simple_archiver_helper_cleanup_c_string(&buf);
|
||||
}
|
||||
|
||||
RETURN()
|
||||
|
|
Loading…
Reference in a new issue