Compare commits

..

No commits in common. "2f5ea544e573684c393db2c49cba05bae8581e4d" and "0b9f0e38f8f10429c4bcf31a5a3f4a6dc9624ca3" have entirely different histories.

6 changed files with 6 additions and 381 deletions

View file

@ -17,8 +17,7 @@ HEADERS = \
src/signal_handling.h \
src/constants.h \
src/http.h \
src/config.h \
src/http_template.h
src/config.h
SOURCES = \
src/main.c \
@ -29,7 +28,6 @@ SOURCES = \
src/globals.c \
src/http.c \
src/config.c \
src/http_template.c \
third_party/SimpleArchiver/src/helpers.c \
third_party/SimpleArchiver/src/data_structures/linked_list.c \
third_party/SimpleArchiver/src/data_structures/hash_map.c \

View file

@ -121,8 +121,8 @@ int c_simple_http_required_iter_fn(void *data, void *ud) {
}
typedef struct C_SIMPLE_HTTP_INTERNAL_RequiredCheck {
const SDArchiverHashMap *map_of_paths_and_their_vars;
const SDArchiverLinkedList *required;
SDArchiverHashMap *map_of_paths_and_their_vars;
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,
const SDArchiverLinkedList *required_names
SDArchiverLinkedList *required_names
) {
C_SIMPLE_HTTP_ParsedConfig config;
config.hash_map = NULL;

View file

@ -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,
const SDArchiverLinkedList *required_names
SDArchiverLinkedList *required_names
);
void c_simple_http_clean_up_parsed_config(C_SIMPLE_HTTP_ParsedConfig *config);

View file

@ -1,271 +0,0 @@
// ISC License
//
// Copyright (c) 2024 Stephen Seo
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
#include "http_template.h"
// Standard library includes.
#include <string.h>
#include <stdlib.h>
// Third party includes.
#include <SimpleArchiver/src/data_structures/linked_list.h>
#include <SimpleArchiver/src/helpers.h>
typedef struct C_SIMPLE_HTTP_INTERNAL_Template_Node {
char *html;
size_t html_size;
union {
size_t orig_end_idx;
size_t html_capacity;
};
SDArchiverLinkedList *forced_next;
} C_SIMPLE_HTTP_INTERNAL_Template_Node;
void c_simple_http_internal_free_template_node(void *data) {
C_SIMPLE_HTTP_INTERNAL_Template_Node *node = data;
if (node) {
if (node->html) {
free(node->html);
}
if (node->forced_next) {
simple_archiver_list_free(&node->forced_next);
}
free(node);
}
}
void c_simple_http_internal_cleanup_template_node(
C_SIMPLE_HTTP_INTERNAL_Template_Node **node) {
if (node && *node) {
c_simple_http_internal_free_template_node(*node);
*node = NULL;
}
}
int c_simple_http_internal_get_final_size_fn(void *data, void *ud) {
size_t *final_size = ud;
C_SIMPLE_HTTP_INTERNAL_Template_Node *node = data;
*final_size += node->html_size;
return 0;
}
int c_simple_http_internal_fill_buf_fn(void *data, void *ud) {
C_SIMPLE_HTTP_INTERNAL_Template_Node *node = data;
C_SIMPLE_HTTP_INTERNAL_Template_Node *to_fill = ud;
if (to_fill->html_capacity < to_fill->html_size + node->html_size) {
return 1;
}
memcpy(
to_fill->html + to_fill->html_size,
node->html,
node->html_size);
to_fill->html_size += node->html_size;
return 0;
}
char *c_simple_http_path_to_generated(
const char *path,
const C_SIMPLE_HTTP_HTTPTemplates *templates) {
C_SIMPLE_HTTP_ParsedConfig *wrapped_hash_map =
simple_archiver_hash_map_get(templates->hash_map, path, strlen(path) + 1);
if (!wrapped_hash_map) {
return NULL;
}
__attribute__((cleanup(simple_archiver_helper_cleanup_c_string)))
char *html_buf = NULL;
size_t html_buf_size = 0;
const char *html_filename =
simple_archiver_hash_map_get(wrapped_hash_map->hash_map, "HTML_FILE", 10);
if (html_filename) {
__attribute__((cleanup(simple_archiver_helper_cleanup_FILE)))
FILE *f = fopen(html_filename, "r");
if (!f) {
return NULL;
}
if (fseek(f, 0, SEEK_END) != 0) {
return NULL;
}
long html_file_size = ftell(f);
if (html_file_size <= 0) {
return NULL;
}
if (fseek(f, 0, SEEK_SET) != 0) {
return NULL;
}
html_buf = malloc(html_file_size + 1);
size_t ret = fread(html_buf, 1, html_file_size, f);
if (ret != (size_t)html_file_size) {
return NULL;
}
html_buf[html_file_size] = 0;
html_buf_size = html_file_size;
} else {
char *stored_html =
simple_archiver_hash_map_get(wrapped_hash_map->hash_map, "HTML", 5);
if (!stored_html) {
return NULL;
}
size_t stored_html_size = strlen(stored_html) + 1;
html_buf = malloc(stored_html_size);
memcpy(html_buf, stored_html, stored_html_size);
html_buf_size = stored_html_size;
}
// At this point, html_buf contains the raw HTML as a C-string.
__attribute__((cleanup(simple_archiver_list_free)))
SDArchiverLinkedList *template_html_list = simple_archiver_list_init();
size_t idx = 0;
size_t last_template_idx = 0;
__attribute__((cleanup(c_simple_http_internal_cleanup_template_node)))
C_SIMPLE_HTTP_INTERNAL_Template_Node *template_node = NULL;
size_t delimeter_count = 0;
// xxxx xxx0 - Initial state, no delimeter reached.
// xxxx xxx1 - Three "{" delimeters reached.
unsigned int state = 0;
for (; idx < html_buf_size; ++idx) {
if ((state & 1) == 0) {
// Using 0x7B instead of left curly-brace due to bug in vim navigation.
if (html_buf[idx] == 0x7B) {
++delimeter_count;
if (delimeter_count >= 3) {
delimeter_count = 0;
state |= 1;
if (template_html_list->count != 0) {
C_SIMPLE_HTTP_INTERNAL_Template_Node *last_node =
template_html_list->tail->prev->data;
last_template_idx = last_node->orig_end_idx;
}
template_node = malloc(sizeof(C_SIMPLE_HTTP_INTERNAL_Template_Node));
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,
c_simple_http_internal_free_template_node);
template_node = NULL;
}
} else {
delimeter_count = 0;
}
} else {
// (state & 1) is 1
// Using 0x7D instead of right curly-brace due to bug in vim navigation.
if (html_buf[idx] == 0x7D) {
++delimeter_count;
if (delimeter_count >= 3) {
delimeter_count = 0;
state &= 0xFFFFFFFE;
C_SIMPLE_HTTP_INTERNAL_Template_Node *last_node =
template_html_list->tail->prev->data;
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,
var_size);
var[var_size] = 0;
const char *value_c_str =
simple_archiver_hash_map_get(
wrapped_hash_map->hash_map,
var,
var_size + 1);
// TODO Impl. loading from file instead of directly from value.
// Perhaps do this if "var" ends with "_FILE".
if (value_c_str) {
template_node =
malloc(sizeof(C_SIMPLE_HTTP_INTERNAL_Template_Node));
size_t size = strlen(value_c_str);
template_node->html = malloc(size);
memcpy(template_node->html, value_c_str, size);
template_node->html_size = size;
template_node->orig_end_idx = idx + 1;
template_node->forced_next = NULL;
} else {
template_node =
malloc(sizeof(C_SIMPLE_HTTP_INTERNAL_Template_Node));
template_node->html = NULL;
template_node->html_size = 0;
template_node->orig_end_idx = idx + 1;
template_node->forced_next = NULL;
}
simple_archiver_list_add(template_html_list, template_node,
c_simple_http_internal_free_template_node);
template_node = NULL;
}
} else {
delimeter_count = 0;
}
}
}
if (template_html_list->count != 0) {
C_SIMPLE_HTTP_INTERNAL_Template_Node *last_node =
template_html_list->tail->prev->data;
if (idx > last_node->orig_end_idx) {
template_node = malloc(sizeof(C_SIMPLE_HTTP_INTERNAL_Template_Node));
size_t size = idx - last_node->orig_end_idx;
template_node->html = malloc(size);
memcpy(template_node->html, html_buf + last_node->orig_end_idx, size);
template_node->html_size = size;
template_node->orig_end_idx = idx + 1;
template_node->forced_next = NULL;
simple_archiver_list_add(template_html_list, template_node,
c_simple_http_internal_free_template_node);
template_node = NULL;
last_node = template_html_list->tail->prev->data;
}
size_t final_size = 0;
simple_archiver_list_get(
template_html_list,
c_simple_http_internal_get_final_size_fn,
&final_size);
if (final_size == 0) {
fprintf(stderr, "ERROR final_size calculated as ZERO from templates!\n");
return NULL;
}
C_SIMPLE_HTTP_INTERNAL_Template_Node to_fill;
to_fill.html = malloc(final_size);
to_fill.html_size = 0;
to_fill.html_capacity = final_size;
if (simple_archiver_list_get(
template_html_list,
c_simple_http_internal_fill_buf_fn,
&to_fill) != NULL) {
fprintf(stderr, "ERROR internal issue processing final html buffer!\n");
free(to_fill.html);
return NULL;
}
return to_fill.html;
} else {
// Prevent cleanup fn from "free"ing html_buf and return it verbatim.
char *buf = html_buf;
html_buf = NULL;
return buf;
}
}
// vim: ts=2 sts=2 sw=2

View file

@ -1,30 +0,0 @@
// ISC License
//
// Copyright (c) 2024 Stephen Seo
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
#ifndef SEODISPARATE_COM_C_SIMPLE_HTTP_HTTP_TEMPLATE_H_
#define SEODISPARATE_COM_C_SIMPLE_HTTP_HTTP_TEMPLATE_H_
#include "http.h"
// Returns non-NULL on success, which must be free'd after use.
// Takes a path string and templates and returns the generated HTML.
char *c_simple_http_path_to_generated(
const char *path,
const C_SIMPLE_HTTP_HTTPTemplates *templates);
#endif
// vim: ts=2 sts=2 sw=2

View file

@ -1,11 +1,9 @@
// 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>
@ -80,7 +78,7 @@ static int checks_passed = 0;
} while (0);
int main(void) {
// Test config.
// Test set up templates.
{
const char *test_config_filename = "/tmp/c_simple_http_test.config";
__attribute__((cleanup(simple_archiver_helper_cleanup_FILE)))
@ -207,76 +205,6 @@ 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=<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()
}