Consolidate config and base64 helpers

This commit is contained in:
udo
2026-05-21 10:31:21 +00:00
parent 8b37e7ea1e
commit 71ddcaf7d4
9 changed files with 113 additions and 184 deletions
+1 -13
View File
@@ -56,23 +56,11 @@ void compiler_unload_failed_shared_unit(SharedUnit* su);
bool compiler_run_unit_init(Request* context, SharedUnit* su, String* error_out = 0);
bool compiler_run_unit_once_if_needed(Request* context, SharedUnit* su, String* error_out = 0);
bool compiler_config_truthy(String raw, bool default_value)
{
raw = to_lower(trim(raw));
if(raw == "")
return(default_value);
if(raw == "1" || raw == "true" || raw == "yes" || raw == "on")
return(true);
if(raw == "0" || raw == "false" || raw == "no" || raw == "off")
return(false);
return(default_value);
}
bool compiler_jit_compile_on_request_enabled(Request* context)
{
if(!context || !context->server)
return(true);
return(compiler_config_truthy(context->server->config["JIT_COMPILE_ON_REQUEST"], true));
return(config_bool("JIT_COMPILE_ON_REQUEST", true));
}
bool compiler_is_u64_string(String value)
+54
View File
@@ -868,6 +868,60 @@ StringList ls(String dir)
return(split(trim(shell_exec("ls -1 "+shell_escape(dir))), "\n"));
}
u64 config_map_u64(StringMap& cfg, String key, u64 fallback)
{
String raw = trim(cfg[key]);
if(raw == "")
return(fallback);
return(int_val(raw));
}
f64 config_map_f64(StringMap& cfg, String key, f64 fallback)
{
String raw = trim(cfg[key]);
if(raw == "")
return(fallback);
return(float_val(raw));
}
bool config_bool_value(String raw, bool fallback)
{
raw = trim(to_lower(raw));
if(raw == "")
return(fallback);
if(raw == "1" || raw == "true" || raw == "yes" || raw == "on")
return(true);
if(raw == "0" || raw == "false" || raw == "no" || raw == "off")
return(false);
return(fallback);
}
bool config_map_bool(StringMap& cfg, String key, bool fallback)
{
return(config_bool_value(cfg[key], fallback));
}
u64 config_u64(String key, u64 fallback)
{
if(!context || !context->server)
return(fallback);
return(config_map_u64(context->server->config, key, fallback));
}
f64 config_f64(String key, f64 fallback)
{
if(!context || !context->server)
return(fallback);
return(config_map_f64(context->server->config, key, fallback));
}
bool config_bool(String key, bool fallback)
{
if(!context || !context->server)
return(fallback);
return(config_map_bool(context->server->config, key, fallback));
}
StringMap make_server_settings()
{
StringMap cfg;
+7
View File
@@ -34,6 +34,13 @@ time_t file_mtime(String file_name);
void file_unlink(String file_name);
String expand_path(String path, String relative_to_path = "");
StringList ls(String dir);
u64 config_map_u64(StringMap& cfg, String key, u64 fallback);
f64 config_map_f64(StringMap& cfg, String key, f64 fallback);
bool config_bool_value(String raw, bool fallback = true);
bool config_map_bool(StringMap& cfg, String key, bool fallback = true);
u64 config_u64(String key, u64 fallback);
f64 config_f64(String key, f64 fallback);
bool config_bool(String key, bool fallback = true);
f64 time_precise();
u64 time();
+14 -5
View File
@@ -4,7 +4,7 @@
#include <fcntl.h>
#include <unistd.h>
static String base64_encode(String raw)
String base64_encode(String raw)
{
static const char* chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
@@ -31,7 +31,9 @@ static String base64_encode(String raw)
return(result);
}
static int base64_decode_value(char c)
namespace {
int base64_decode_value(char c)
{
if(c >= 'A' && c <= 'Z')
return(c - 'A');
@@ -46,17 +48,24 @@ static int base64_decode_value(char c)
return(-1);
}
static String base64_decode(String raw, bool& ok)
}
String base64_decode(String raw, bool& ok)
{
ok = false;
String cleaned;
for(char c : raw)
{
if(!isspace(c))
if(!isspace((unsigned char)c))
cleaned.append(1, c);
}
if(cleaned.length() == 0 || (cleaned.length() % 4) != 0)
if(cleaned.length() == 0)
{
ok = true;
return("");
}
if((cleaned.length() % 4) != 0)
return("");
String result;
+2
View File
@@ -3,6 +3,8 @@
String var_dump(URI uri, String prefix = "", String postfix = "\n");
String base64_encode(String raw);
String base64_decode(String raw, bool& ok);
String uri_decode(String q);
String uri_encode(String q);
StringMap parse_query(String q);
+2 -12
View File
@@ -14,19 +14,9 @@ String zip_error(String api, String detail)
return(api + "(): " + detail);
}
u64 archive_config_u64(String key, u64 fallback)
{
if(!context || !context->server)
return(fallback);
String raw = trim(context->server->config[key]);
if(raw == "")
return(fallback);
return(int_val(raw));
}
void archive_check_size(String api, String label, u64 size, String config_key, u64 fallback)
{
u64 limit = archive_config_u64(config_key, fallback);
u64 limit = config_u64(config_key, fallback);
if(limit > 0 && size > limit)
throw std::runtime_error(zip_error(api, label + " exceeds configured limit"));
}
@@ -336,7 +326,7 @@ String gz_uncompress(String compressed)
if(!out)
throw std::runtime_error("gz_uncompress(): decompression failed");
u64 output_limit = archive_config_u64("ARCHIVE_MAX_OUTPUT_BYTES", 64 * 1024 * 1024);
u64 output_limit = config_u64("ARCHIVE_MAX_OUTPUT_BYTES", 64 * 1024 * 1024);
if(output_limit > 0 && out_len > output_limit)
{
mz_free(out);