Compare commits

...

2 commits

Author SHA1 Message Date
8974c7b31f Add WIP html_cache
All checks were successful
Run Unit Tests / build-and-run-unit-tests (push) Successful in 8s
2024-09-22 15:44:04 +09:00
6f845a7185 Add helper to create string parts and combine them 2024-09-22 15:39:35 +09:00
7 changed files with 204 additions and 2 deletions

View file

@ -11,6 +11,7 @@ set(c_simple_http_SOURCES
"${CMAKE_CURRENT_SOURCE_DIR}/src/config.c" "${CMAKE_CURRENT_SOURCE_DIR}/src/config.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/http_template.c" "${CMAKE_CURRENT_SOURCE_DIR}/src/http_template.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/helpers.c" "${CMAKE_CURRENT_SOURCE_DIR}/src/helpers.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/html_cache.c"
"${CMAKE_CURRENT_SOURCE_DIR}/third_party/SimpleArchiver/src/helpers.c" "${CMAKE_CURRENT_SOURCE_DIR}/third_party/SimpleArchiver/src/helpers.c"
"${CMAKE_CURRENT_SOURCE_DIR}/third_party/SimpleArchiver/src/data_structures/linked_list.c" "${CMAKE_CURRENT_SOURCE_DIR}/third_party/SimpleArchiver/src/data_structures/linked_list.c"
"${CMAKE_CURRENT_SOURCE_DIR}/third_party/SimpleArchiver/src/data_structures/hash_map.c" "${CMAKE_CURRENT_SOURCE_DIR}/third_party/SimpleArchiver/src/data_structures/hash_map.c"

View file

@ -42,7 +42,8 @@ HEADERS = \
src/http.h \ src/http.h \
src/config.h \ src/config.h \
src/http_template.h \ src/http_template.h \
src/helpers.h src/helpers.h \
src/html_cache.h
SOURCES = \ SOURCES = \
src/main.c \ src/main.c \
@ -55,6 +56,7 @@ SOURCES = \
src/config.c \ src/config.c \
src/http_template.c \ src/http_template.c \
src/helpers.c \ src/helpers.c \
src/html_cache.c \
third_party/SimpleArchiver/src/helpers.c \ third_party/SimpleArchiver/src/helpers.c \
third_party/SimpleArchiver/src/data_structures/linked_list.c \ third_party/SimpleArchiver/src/data_structures/linked_list.c \
third_party/SimpleArchiver/src/data_structures/hash_map.c \ third_party/SimpleArchiver/src/data_structures/hash_map.c \

View file

@ -18,6 +18,93 @@
// Standard library includes. // Standard library includes.
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <stdio.h>
int c_simple_http_internal_get_string_part_full_size(void *data, void *ud) {
C_SIMPLE_HTTP_String_Part *part = data;
size_t *count = ud;
*count += part->size - 1;
return 0;
}
int c_simple_http_internal_combine_string_parts_from_list(void *data, void *ud) {
C_SIMPLE_HTTP_String_Part *part = data;
void **ptrs = ud;
char *buf = ptrs[0];
size_t *current_count = ptrs[1];
const size_t *total_count = ptrs[2];
if (*current_count + part->size - 1 > *total_count) {
fprintf(stderr, "ERROR Invalid state combining string parts!\n");
return 1;
}
memcpy(buf + *current_count, part->buf, part->size - 1);
*current_count += part->size - 1;
return 0;
}
void c_simple_http_cleanup_string_part(void *data) {
C_SIMPLE_HTTP_String_Part *part = data;
if (part) {
if (part->buf) {
free(part->buf);
part->buf = NULL;
}
free(part);
}
}
void c_simple_http_add_string_part(
SDArchiverLinkedList *list, const char *c_string, size_t extra) {
C_SIMPLE_HTTP_String_Part *string_part =
malloc(sizeof(C_SIMPLE_HTTP_String_Part));
string_part->size = strlen(c_string) + 1;
string_part->buf = malloc(string_part->size);
memcpy(string_part->buf, c_string, string_part->size);
string_part->extra = extra;
simple_archiver_list_add(
list, string_part, c_simple_http_cleanup_string_part);
}
char *c_simple_http_combine_string_parts(const SDArchiverLinkedList *list) {
if (!list || list->count == 0) {
return NULL;
}
size_t count = 0;
simple_archiver_list_get(
list, c_simple_http_internal_get_string_part_full_size, &count);
char *buf = malloc(count + 1);
size_t current_count = 0;
void **ptrs = malloc(sizeof(void*) * 3);
ptrs[0] = buf;
ptrs[1] = &current_count;
ptrs[2] = &count;
if (simple_archiver_list_get(
list, c_simple_http_internal_combine_string_parts_from_list, ptrs)) {
free(buf);
return NULL;
}
free(ptrs);
buf[count] = 0;
return buf;
}
void c_simple_http_helper_to_lowercase_in_place(char *buf, size_t size) { void c_simple_http_helper_to_lowercase_in_place(char *buf, size_t size) {
for (size_t idx = 0; idx < size; ++idx) { for (size_t idx = 0; idx < size; ++idx) {

View file

@ -20,6 +20,27 @@
// Standard library includes. // Standard library includes.
#include <stddef.h> #include <stddef.h>
// Third-party includes.
#include <SimpleArchiver/src/data_structures/linked_list.h>
typedef struct C_SIMPLE_HTTP_String_Part {
char *buf;
size_t size;
size_t extra;
} C_SIMPLE_HTTP_String_Part;
/// Assumes "data" is a C_SIMPLE_HTTP_String_Part, "data" was malloced, and
/// "data->buf" was malloced.
void c_simple_http_cleanup_string_part(void *data);
/// Puts a malloced instance of String_Part into the list.
/// The given c_string will be copied into a newly malloced buffer.
void c_simple_http_add_string_part(
SDArchiverLinkedList *list, const char *c_string, size_t extra);
/// Combines all String_Parts in the list and returns it as a single buffer.
char *c_simple_http_combine_string_parts(const SDArchiverLinkedList *list);
/// Modifies "buf" in-place to change all uppercase to lowercase alpha chars. /// Modifies "buf" in-place to change all uppercase to lowercase alpha chars.
void c_simple_http_helper_to_lowercase_in_place(char *buf, size_t size); void c_simple_http_helper_to_lowercase_in_place(char *buf, size_t size);

36
src/html_cache.c Normal file
View file

@ -0,0 +1,36 @@
// 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.
// Third-party includes.
//#include <SimpleArchiver/src/data_structures/linked_list.h>
char *c_simple_http_path_to_cache_filename(const char *path) {
// SDArchiverLinkedList *parts = simple_archiver_list_init();
// TODO
return 0;
}
int c_simple_http_cache_path(
const char *path,
const char *config_filename,
const char *cache_dir,
char **buf_out) {
// TODO
return 0;
}
// vim: et ts=2 sts=2 sw=2

39
src/html_cache.h Normal file
View file

@ -0,0 +1,39 @@
// 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_HTML_CACHE_H_
#define SEODISPARATE_COM_C_SIMPLE_HTTP_HTML_CACHE_H_
/// Must be free'd if non-NULL.
char *c_simple_http_path_to_cache_filename(const char *path);
/// Must be free'd if non-NULL.
char *c_simple_http_cache_filename_to_path(const char *cache_filename);
/// Given a "path", returns non-zero if the cache is invalidated.
/// "config_filename" is required to check its timestamp. "cache_dir" is
/// required to actually get the cache file to check against. "buf_out" will be
/// populated if non-NULL, and will either be fetched from the cache or from the
/// config (using http_template). Note that "buf_out" will point to a c-string.
int c_simple_http_cache_path(
const char *path,
const char *config_filename,
const char *cache_dir,
char **buf_out);
#endif
// vim: et ts=2 sts=2 sw=2

View file

@ -5,14 +5,15 @@
#include <stdint.h> #include <stdint.h>
// Local includes. // Local includes.
#include "SimpleArchiver/src/data_structures/linked_list.h"
#include "config.h" #include "config.h"
#include "helpers.h"
#include "http_template.h" #include "http_template.h"
#include "http.h" #include "http.h"
// Third party includes. // Third party includes.
#include <SimpleArchiver/src/helpers.h> #include <SimpleArchiver/src/helpers.h>
#include <SimpleArchiver/src/data_structures/hash_map.h> #include <SimpleArchiver/src/data_structures/hash_map.h>
#include <SimpleArchiver/src/data_structures/linked_list.h>
static int32_t checks_checked = 0; static int32_t checks_checked = 0;
static int32_t checks_passed = 0; static int32_t checks_passed = 0;
@ -542,6 +543,21 @@ int main(void) {
free(stripped_path_buf); free(stripped_path_buf);
} }
// Test helpers.
{
__attribute__((cleanup(simple_archiver_list_free)))
SDArchiverLinkedList *list = simple_archiver_list_init();
c_simple_http_add_string_part(list, "one\n", 0);
c_simple_http_add_string_part(list, "two\n", 0);
c_simple_http_add_string_part(list, "three\n", 0);
__attribute__((cleanup(simple_archiver_helper_cleanup_c_string)))
char *buf = c_simple_http_combine_string_parts(list);
ASSERT_TRUE(buf);
ASSERT_TRUE(strcmp(buf, "one\ntwo\nthree\n") == 0);
}
RETURN() RETURN()
} }