Compare commits

..

No commits in common. "aef990f3414f42a27b125001a1d034b7a7be3f9d" and "9459ec93132e3efd0d154b6dcb5a7b03ef6e7b0f" have entirely different histories.

10 changed files with 10 additions and 70 deletions

View file

@ -12,8 +12,6 @@ A simple HTTP/1.1 server written in C.
For example: --req-header-to-print=User-Agent For example: --req-header-to-print=User-Agent
Note that this option is case-insensitive Note that this option is case-insensitive
--enable-reload-config-on-change --enable-reload-config-on-change
--enable-cache-dir=<DIR>
--cache-entry-lifetime-seconds=<SECONDS>
## Before Compiling ## Before Compiling

View file

@ -29,9 +29,6 @@
// Posix includes. // Posix includes.
#include <sys/stat.h> #include <sys/stat.h>
// Local includes.
#include "constants.h"
void print_usage(void) { void print_usage(void) {
puts("Usage:"); puts("Usage:");
puts(" -p <port> | --port <port>"); puts(" -p <port> | --port <port>");
@ -42,7 +39,6 @@ void print_usage(void) {
puts(" Note that this option is case-insensitive"); puts(" Note that this option is case-insensitive");
puts(" --enable-reload-config-on-change"); puts(" --enable-reload-config-on-change");
puts(" --enable-cache-dir=<DIR>"); puts(" --enable-cache-dir=<DIR>");
puts(" --cache-entry-lifetime-seconds=<SECONDS>");
} }
Args parse_args(int32_t argc, char **argv) { Args parse_args(int32_t argc, char **argv) {
@ -53,7 +49,6 @@ Args parse_args(int32_t argc, char **argv) {
Args args; Args args;
memset(&args, 0, sizeof(Args)); memset(&args, 0, sizeof(Args));
args.list_of_headers_to_log = simple_archiver_list_init(); args.list_of_headers_to_log = simple_archiver_list_init();
args.cache_lifespan_seconds = C_SIMPLE_HTTP_DEFAULT_CACHE_LIFESPAN_SECONDS;
while (argc > 0) { while (argc > 0) {
if ((strcmp(argv[0], "-p") == 0 || strcmp(argv[0], "--port") == 0) if ((strcmp(argv[0], "-p") == 0 || strcmp(argv[0], "--port") == 0)
@ -114,20 +109,6 @@ Args parse_args(int32_t argc, char **argv) {
printf("Directory \"%s\" exists.\n", args.cache_dir); printf("Directory \"%s\" exists.\n", args.cache_dir);
} }
closedir(d); closedir(d);
} else if (strncmp(argv[0], "--cache-entry-lifetime-seconds=", 31) == 0) {
args.cache_lifespan_seconds = strtoul(argv[0] + 31, NULL, 10);
if (args.cache_lifespan_seconds == 0) {
fprintf(
stderr,
"ERROR: Invalid --cache-entry-lifetime-seconds=%s entry!\n",
argv[0] + 31);
print_usage();
exit(1);
} else {
printf(
"NOTICE set cache-entry-lifetime to %lu\n",
args.cache_lifespan_seconds);
}
} else { } else {
fprintf(stderr, "ERROR: Invalid args!\n"); fprintf(stderr, "ERROR: Invalid args!\n");
print_usage(); print_usage();

View file

@ -37,7 +37,6 @@ typedef struct Args {
// Non-NULL if cache-dir is specified and cache is to be used. // Non-NULL if cache-dir is specified and cache is to be used.
// Does not need to be free'd since it points to a string in argv. // Does not need to be free'd since it points to a string in argv.
const char *cache_dir; const char *cache_dir;
size_t cache_lifespan_seconds;
} Args; } Args;
void print_usage(void); void print_usage(void);

View file

@ -25,6 +25,5 @@
#define C_SIMPLE_HTTP_CONFIG_BUF_SIZE 1024 #define C_SIMPLE_HTTP_CONFIG_BUF_SIZE 1024
#define C_SIMPLE_HTTP_QUOTE_COUNT_MAX 3 #define C_SIMPLE_HTTP_QUOTE_COUNT_MAX 3
#define C_SIMPLE_HTTP_TRY_CONFIG_RELOAD_MAX_ATTEMPTS 20 #define C_SIMPLE_HTTP_TRY_CONFIG_RELOAD_MAX_ATTEMPTS 20
#define C_SIMPLE_HTTP_DEFAULT_CACHE_LIFESPAN_SECONDS 604800
#endif #endif

View file

@ -25,9 +25,6 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <errno.h> #include <errno.h>
// libc includes.
#include <time.h>
// Third-party includes. // Third-party includes.
#include <SimpleArchiver/src/data_structures/linked_list.h> #include <SimpleArchiver/src/data_structures/linked_list.h>
#include <SimpleArchiver/src/data_structures/hash_map.h> #include <SimpleArchiver/src/data_structures/hash_map.h>
@ -211,7 +208,6 @@ int c_simple_http_cache_path(
const char *config_filename, const char *config_filename,
const char *cache_dir, const char *cache_dir,
C_SIMPLE_HTTP_HTTPTemplates *templates, C_SIMPLE_HTTP_HTTPTemplates *templates,
size_t cache_entry_lifespan,
char **buf_out) { char **buf_out) {
if (!path) { if (!path) {
fprintf(stderr, "ERROR cache_path function: path is NULL!\n"); fprintf(stderr, "ERROR cache_path function: path is NULL!\n");
@ -395,17 +391,11 @@ int c_simple_http_cache_path(
} }
// Compare modification times. // Compare modification times.
struct timespec current_time;
if (clock_gettime(CLOCK_REALTIME, &current_time) != 0) {
memset(&current_time, 0, sizeof(struct timespec));
}
CACHE_FILE_WRITE_CHECK: CACHE_FILE_WRITE_CHECK:
if (force_cache_update if (force_cache_update
|| cache_file_stat.st_mtim.tv_sec < config_file_stat.st_mtim.tv_sec || cache_file_stat.st_mtim.tv_sec < config_file_stat.st_mtim.tv_sec
|| (cache_file_stat.st_mtim.tv_sec == config_file_stat.st_mtim.tv_sec || (cache_file_stat.st_mtim.tv_sec == config_file_stat.st_mtim.tv_sec
&& cache_file_stat.st_mtim.tv_nsec < config_file_stat.st_mtim.tv_nsec) && cache_file_stat.st_mtim.tv_nsec < config_file_stat.st_mtim.tv_nsec))
|| (current_time.tv_sec - cache_file_stat.st_mtim.tv_sec
> (ssize_t)cache_entry_lifespan))
{ {
// Cache file is out of date. // Cache file is out of date.

View file

@ -37,7 +37,6 @@ int c_simple_http_cache_path(
const char *config_filename, const char *config_filename,
const char *cache_dir, const char *cache_dir,
C_SIMPLE_HTTP_HTTPTemplates *templates, C_SIMPLE_HTTP_HTTPTemplates *templates,
size_t cache_entry_lifespan,
char **buf_out); char **buf_out);
#endif #endif

View file

@ -65,7 +65,8 @@ char *c_simple_http_request_response(
C_SIMPLE_HTTP_HTTPTemplates *templates, C_SIMPLE_HTTP_HTTPTemplates *templates,
size_t *out_size, size_t *out_size,
enum C_SIMPLE_HTTP_ResponseCode *out_response_code, enum C_SIMPLE_HTTP_ResponseCode *out_response_code,
const Args *args) { const char *cache_dir,
const char *config_filename) {
if (out_size) { if (out_size) {
*out_size = 0; *out_size = 0;
} }
@ -176,13 +177,12 @@ char *c_simple_http_request_response(
char *generated_buf = NULL; char *generated_buf = NULL;
if (args->cache_dir) { if (cache_dir) {
int ret = c_simple_http_cache_path( int ret = c_simple_http_cache_path(
stripped_path ? stripped_path : request_path_unescaped, stripped_path ? stripped_path : request_path_unescaped,
args->config_file, config_filename,
args->cache_dir, cache_dir,
templates, templates,
args->cache_lifespan_seconds,
&generated_buf); &generated_buf);
if (ret < 0) { if (ret < 0) {
fprintf(stderr, "ERROR Failed to generate template with cache!\n"); fprintf(stderr, "ERROR Failed to generate template with cache!\n");

View file

@ -25,7 +25,6 @@
#include <SimpleArchiver/src/data_structures/hash_map.h> #include <SimpleArchiver/src/data_structures/hash_map.h>
// Local includes. // Local includes.
#include "arg_parse.h"
#include "config.h" #include "config.h"
typedef C_SIMPLE_HTTP_ParsedConfig C_SIMPLE_HTTP_HTTPTemplates; typedef C_SIMPLE_HTTP_ParsedConfig C_SIMPLE_HTTP_HTTPTemplates;
@ -50,7 +49,8 @@ char *c_simple_http_request_response(
C_SIMPLE_HTTP_HTTPTemplates *templates, C_SIMPLE_HTTP_HTTPTemplates *templates,
size_t *out_size, size_t *out_size,
enum C_SIMPLE_HTTP_ResponseCode *out_response_code, enum C_SIMPLE_HTTP_ResponseCode *out_response_code,
const Args *args const char *cache_dir,
const char *config_filename
); );
/// Takes a PATH string and returns a "bare" path. /// Takes a PATH string and returns a "bare" path.

View file

@ -360,7 +360,8 @@ int main(int argc, char **argv) {
&parsed_config, &parsed_config,
&response_size, &response_size,
&response_code, &response_code,
&args); args.cache_dir,
args.config_file);
if (response && response_code == C_SIMPLE_HTTP_Response_200_OK) { if (response && response_code == C_SIMPLE_HTTP_Response_200_OK) {
CHECK_ERROR_WRITE(write(connection_fd, "HTTP/1.1 200 OK\n", 16)); CHECK_ERROR_WRITE(write(connection_fd, "HTTP/1.1 200 OK\n", 16));
CHECK_ERROR_WRITE(write(connection_fd, "Allow: GET\n", 11)); CHECK_ERROR_WRITE(write(connection_fd, "Allow: GET\n", 11));

View file

@ -15,7 +15,6 @@
#include "http_template.h" #include "http_template.h"
#include "http.h" #include "http.h"
#include "html_cache.h" #include "html_cache.h"
#include "constants.h"
// Third party includes. // Third party includes.
#include <SimpleArchiver/src/helpers.h> #include <SimpleArchiver/src/helpers.h>
@ -809,7 +808,6 @@ int main(void) {
test_http_template_filename5, test_http_template_filename5,
"/tmp/c_simple_http_cache_dir", "/tmp/c_simple_http_cache_dir",
&templates, &templates,
0xFFFFFFFF,
&buf); &buf);
CHECK_TRUE(int_ret > 0); CHECK_TRUE(int_ret > 0);
@ -832,7 +830,6 @@ int main(void) {
test_http_template_filename5, test_http_template_filename5,
"/tmp/c_simple_http_cache_dir", "/tmp/c_simple_http_cache_dir",
&templates, &templates,
0xFFFFFFFF,
&buf); &buf);
CHECK_TRUE(int_ret == 0); CHECK_TRUE(int_ret == 0);
ASSERT_TRUE(buf); ASSERT_TRUE(buf);
@ -873,7 +870,6 @@ int main(void) {
test_http_template_filename5, test_http_template_filename5,
"/tmp/c_simple_http_cache_dir", "/tmp/c_simple_http_cache_dir",
&templates, &templates,
0xFFFFFFFF,
&buf); &buf);
CHECK_TRUE(int_ret > 0); CHECK_TRUE(int_ret > 0);
ASSERT_TRUE(buf); ASSERT_TRUE(buf);
@ -896,7 +892,6 @@ int main(void) {
test_http_template_filename5, test_http_template_filename5,
"/tmp/c_simple_http_cache_dir", "/tmp/c_simple_http_cache_dir",
&templates, &templates,
0xFFFFFFFF,
&buf); &buf);
CHECK_TRUE(int_ret == 0); CHECK_TRUE(int_ret == 0);
ASSERT_TRUE(buf); ASSERT_TRUE(buf);
@ -914,10 +909,6 @@ int main(void) {
CHECK_TRUE(cache_file_size_2 == cache_file_size_3); CHECK_TRUE(cache_file_size_2 == cache_file_size_3);
// Edit config file. // Edit config file.
puts("Sleeping for two seconds to ensure edited file's timestamp has "
"changed...");
sleep(2);
puts("Done sleeping.");
test_file = fopen(test_http_template_filename5, "w"); test_file = fopen(test_http_template_filename5, "w");
ASSERT_TRUE(test_file); ASSERT_TRUE(test_file);
@ -944,24 +935,6 @@ int main(void) {
test_http_template_filename5, test_http_template_filename5,
"/tmp/c_simple_http_cache_dir", "/tmp/c_simple_http_cache_dir",
&templates, &templates,
0xFFFFFFFF,
&buf);
CHECK_TRUE(int_ret > 0);
ASSERT_TRUE(buf);
CHECK_TRUE(strcmp(buf, "<h1>Alternate test text.<br>Yep.</h1>") == 0);
free(buf);
buf = NULL;
puts("Sleeping for two seconds to ensure cache file has aged...");
sleep(2);
puts("Done sleeping.");
// Re-run cache function, checking that it is invalidated.
int_ret = c_simple_http_cache_path(
"/",
test_http_template_filename5,
"/tmp/c_simple_http_cache_dir",
&templates,
1,
&buf); &buf);
CHECK_TRUE(int_ret > 0); CHECK_TRUE(int_ret > 0);
ASSERT_TRUE(buf); ASSERT_TRUE(buf);