Add "output" size parameter to http_template fn

This commit is contained in:
Stephen Seo 2024-09-05 16:12:50 +09:00
parent 676b98751d
commit 8a9637d941
3 changed files with 29 additions and 7 deletions

View file

@ -98,7 +98,11 @@ int c_simple_http_internal_ends_with_FILE(const char *c_string) {
char *c_simple_http_path_to_generated( char *c_simple_http_path_to_generated(
const char *path, const char *path,
const C_SIMPLE_HTTP_HTTPTemplates *templates) { const C_SIMPLE_HTTP_HTTPTemplates *templates,
size_t *output_buf_size) {
if (output_buf_size) {
*output_buf_size = 0;
}
C_SIMPLE_HTTP_ParsedConfig *wrapped_hash_map = C_SIMPLE_HTTP_ParsedConfig *wrapped_hash_map =
simple_archiver_hash_map_get(templates->hash_map, path, strlen(path) + 1); simple_archiver_hash_map_get(templates->hash_map, path, strlen(path) + 1);
if (!wrapped_hash_map) { if (!wrapped_hash_map) {
@ -157,7 +161,7 @@ char *c_simple_http_path_to_generated(
size_t delimeter_count = 0; size_t delimeter_count = 0;
// xxxx xxx0 - Initial state, no delimeter reached. // xxxx xxx0 - Initial state, no delimeter reached.
// xxxx xxx1 - Three "{" delimeters reached. // xxxx xxx1 - Three left-curly-brace delimeters reached.
unsigned int state = 0; unsigned int state = 0;
for (; idx < html_buf_size; ++idx) { for (; idx < html_buf_size; ++idx) {
@ -321,11 +325,17 @@ char *c_simple_http_path_to_generated(
return NULL; return NULL;
} }
to_fill.html[final_size] = 0; to_fill.html[final_size] = 0;
if (output_buf_size) {
*output_buf_size = final_size;
}
return to_fill.html; return to_fill.html;
} else { } else {
// Prevent cleanup fn from "free"ing html_buf and return it verbatim. // Prevent cleanup fn from "free"ing html_buf and return it verbatim.
char *buf = html_buf; char *buf = html_buf;
html_buf = NULL; html_buf = NULL;
if (output_buf_size) {
*output_buf_size = html_buf_size;
}
return buf; return buf;
} }
} }

View file

@ -19,11 +19,17 @@
#include "http.h" #include "http.h"
// Standard library includes.
#include <stddef.h>
// Returns non-NULL on success, which must be free'd after use. // Returns non-NULL on success, which must be free'd after use.
// Takes a path string and templates and returns the generated HTML. // 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.
char *c_simple_http_path_to_generated( char *c_simple_http_path_to_generated(
const char *path, const char *path,
const C_SIMPLE_HTTP_HTTPTemplates *templates); const C_SIMPLE_HTTP_HTTPTemplates *templates,
size_t *output_buf_size);
#endif #endif

View file

@ -248,10 +248,13 @@ int main(void) {
); );
ASSERT_TRUE(config.paths != NULL); ASSERT_TRUE(config.paths != NULL);
size_t output_buf_size;
__attribute__((cleanup(simple_archiver_helper_cleanup_c_string))) __attribute__((cleanup(simple_archiver_helper_cleanup_c_string)))
char *buf = c_simple_http_path_to_generated("/", &config); char *buf = c_simple_http_path_to_generated("/", &config, &output_buf_size);
ASSERT_TRUE(buf != NULL); ASSERT_TRUE(buf != NULL);
ASSERT_TRUE(strcmp(buf, "<h1>Test</h1>") == 0); ASSERT_TRUE(strcmp(buf, "<h1>Test</h1>") == 0);
CHECK_TRUE(output_buf_size == 14);
simple_archiver_helper_cleanup_c_string(&buf); simple_archiver_helper_cleanup_c_string(&buf);
__attribute__((cleanup(test_internal_cleanup_delete_temporary_file))) __attribute__((cleanup(test_internal_cleanup_delete_temporary_file)))
@ -283,7 +286,7 @@ int main(void) {
); );
ASSERT_TRUE(config.paths != NULL); ASSERT_TRUE(config.paths != NULL);
buf = c_simple_http_path_to_generated("/", &config); buf = c_simple_http_path_to_generated("/", &config, &output_buf_size);
ASSERT_TRUE(buf != NULL); ASSERT_TRUE(buf != NULL);
printf("%s\n", buf); printf("%s\n", buf);
ASSERT_TRUE( ASSERT_TRUE(
@ -291,6 +294,7 @@ int main(void) {
buf, buf,
"<h1> Some text. </h1><br><h2> More text. </h2>") "<h1> Some text. </h1><br><h2> More text. </h2>")
== 0); == 0);
CHECK_TRUE(output_buf_size == 47);
simple_archiver_helper_cleanup_c_string(&buf); simple_archiver_helper_cleanup_c_string(&buf);
__attribute__((cleanup(test_internal_cleanup_delete_temporary_file))) __attribute__((cleanup(test_internal_cleanup_delete_temporary_file)))
@ -345,7 +349,7 @@ int main(void) {
); );
ASSERT_TRUE(config.paths != NULL); ASSERT_TRUE(config.paths != NULL);
buf = c_simple_http_path_to_generated("/", &config); buf = c_simple_http_path_to_generated("/", &config, &output_buf_size);
ASSERT_TRUE(buf != NULL); ASSERT_TRUE(buf != NULL);
printf("%s\n", buf); printf("%s\n", buf);
ASSERT_TRUE( ASSERT_TRUE(
@ -353,6 +357,7 @@ int main(void) {
buf, buf,
"<h1> testVar text. </h1><br><h2> testVar2 text. </h2>") "<h1> testVar text. </h1><br><h2> testVar2 text. </h2>")
== 0); == 0);
CHECK_TRUE(output_buf_size == 53);
simple_archiver_helper_cleanup_c_string(&buf); simple_archiver_helper_cleanup_c_string(&buf);
__attribute__((cleanup(test_internal_cleanup_delete_temporary_file))) __attribute__((cleanup(test_internal_cleanup_delete_temporary_file)))
@ -427,7 +432,7 @@ int main(void) {
); );
ASSERT_TRUE(config.paths != NULL); ASSERT_TRUE(config.paths != NULL);
buf = c_simple_http_path_to_generated("/", &config); buf = c_simple_http_path_to_generated("/", &config, &output_buf_size);
ASSERT_TRUE(buf != NULL); ASSERT_TRUE(buf != NULL);
printf("%s\n", buf); printf("%s\n", buf);
ASSERT_TRUE( ASSERT_TRUE(
@ -435,6 +440,7 @@ int main(void) {
buf, buf,
"<h1> some test text in test var file. </h1>") "<h1> some test text in test var file. </h1>")
== 0); == 0);
CHECK_TRUE(output_buf_size == 43);
simple_archiver_helper_cleanup_c_string(&buf); simple_archiver_helper_cleanup_c_string(&buf);
} }