changing doc format and HTML literals

This commit is contained in:
udo
2026-04-22 09:31:00 +00:00
parent f7b066b374
commit 14ebf10a22
17 changed files with 319 additions and 30 deletions
+88
View File
@@ -0,0 +1,88 @@
String test_demo_normalize_ip(String ip)
{
if(ip.find(",") != String::npos)
ip = trim(nibble(ip, ","));
ip = trim(ip);
if(str_starts_with(ip, "::ffff:"))
ip = ip.substr(7);
return(ip);
}
bool test_demo_ip_is_private(String ip)
{
ip = trim(ip);
if(ip == "" || ip == "localhost" || ip == "::1")
return(true);
if(str_starts_with(ip, "127."))
return(true);
if(str_starts_with(ip, "10."))
return(true);
if(str_starts_with(ip, "192.168."))
return(true);
if(str_starts_with(ip, "fc") || str_starts_with(ip, "fd") || str_starts_with(ip, "fe80:"))
return(true);
auto parts = split(ip, ".");
if(parts.size() == 4 && parts[0] == "172")
{
s64 second = int_val(parts[1]);
if(second >= 16 && second <= 31)
return(true);
}
return(false);
}
String test_demo_request_ip(Request& context)
{
String remote_ip = test_demo_normalize_ip(context.params["REMOTE_ADDR"]);
String forwarded_ip = test_demo_normalize_ip(first(context.params["HTTP_X_FORWARDED_FOR"], context.params["HTTP_X_REAL_IP"]));
if(remote_ip != "" && !test_demo_ip_is_private(remote_ip))
return(remote_ip);
if(forwarded_ip != "")
return(forwarded_ip);
return(remote_ip);
}
bool test_demo_request_allowed(Request& context)
{
return(test_demo_ip_is_private(test_demo_request_ip(context)));
}
void test_demo_render_restricted_html(Request& context, String title, String risk, String style_href = "style.css", String back_href = "index.uce")
{
context.set_status(403, "Restricted");
String request_ip = first(test_demo_request_ip(context), "unknown");
print("<html><head>");
print("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"></meta>");
print("<link rel=\"stylesheet\" href='", html_escape(style_href), "?v=", time(), "'></link>");
print("</head><body>");
print("<h1><a href=\"", html_escape(back_href), "\">UCE Test</a>: ", html_escape(title), "</h1>");
print("<p>This test page is disabled for public access because it can ", html_escape(risk), ".</p>");
print("<p>It remains available from localhost or a private network for local development and server administration.</p>");
print("<p>Detected request source: <code>", html_escape(request_ip), "</code></p>");
print("</body></html>");
}
void test_demo_render_restricted_text(Request& context, String title, String risk)
{
context.set_status(403, "Restricted");
context.header["Content-Type"] = "text/plain; charset=utf-8";
print(title, ": restricted on public access\n");
print("Reason: this test can ", risk, ".\n");
print("Detected request source: ", first(test_demo_request_ip(context), "unknown"), "\n");
}
void test_demo_render_restricted_json(Request& context, String title, String risk)
{
context.set_status(403, "Restricted");
context.header["Content-Type"] = "application/json; charset=utf-8";
DTree payload;
payload["ok"].set_bool(false);
payload["error"] = "restricted";
payload["title"] = title;
payload["reason"] = "This test is disabled for public access because it can " + risk + ".";
payload["request_ip"] = first(test_demo_request_ip(context), "unknown");
print(json_encode(payload));
}
+7
View File
@@ -1,6 +1,13 @@
#include "demo_guard.h"
RENDER(Request& context)
{
if(!test_demo_request_allowed(context))
{
test_demo_render_restricted_html(context, "Error reporting", "intentionally crash or abort request workers");
return;
}
String mode = context.get["mode"];
if(mode == "exception")
+7
View File
@@ -1,9 +1,16 @@
#include "demo_guard.h"
RENDER(Request& context)
{
if(!test_demo_request_allowed(context))
{
test_demo_render_restricted_html(context, "File Append", "write to server-side files");
return;
}
DTree t;
<>
+19 -9
View File
@@ -1,4 +1,6 @@
#include "demo_guard.h"
void render_card(String url, String title, String desc)
{
@@ -12,6 +14,7 @@ RENDER(Request& context)
{
DTree p;
p.set(context.params);
bool allow_server_demos = test_demo_request_allowed(context);
<><html>
<head>
@@ -26,9 +29,9 @@ RENDER(Request& context)
<div class="test-grid">
<div class="grid-heading">Basics</div>
<? render_card("hello.uce", "Hello World", "Basic output and server time"); ?>
<? render_card("working-dir.uce", "Working Directory", "Server paths and cwd"); ?>
<? render_card("header.uce", "Headers", "HTTP response headers"); ?>
<? render_card("error-reporting.uce", "Error Reporting", "Error handling and output"); ?>
<? if(allow_server_demos) { render_card("working-dir.uce", "Working Directory", "Server paths and cwd"); } ?>
<? if(allow_server_demos) { render_card("error-reporting.uce", "Error Reporting", "Error handling and output"); } ?>
<div class="grid-heading">Data Types &amp; Parsing</div>
<? render_card("dtree.uce", "DTree", "Dynamic hierarchical data tree"); ?>
@@ -49,21 +52,27 @@ RENDER(Request& context)
<div class="grid-heading">Storage &amp; I/O</div>
<? render_card("fileio.uce", "File I/O", "Read and write files"); ?>
<? render_card("file_append.uce", "File Append", "Append data to files"); ?>
<? render_card("shell.uce", "Shell", "Execute shell commands"); ?>
<? render_card("memcached.uce", "Memcached", "Memcached key-value store"); ?>
<? render_card("mysql.uce", "MySQL", "MySQL database connector"); ?>
<? if(allow_server_demos) { render_card("file_append.uce", "File Append", "Append data to files"); } ?>
<? if(allow_server_demos) { render_card("shell.uce", "Shell", "Execute shell commands"); } ?>
<? if(allow_server_demos) { render_card("memcached.uce", "Memcached", "Memcached key-value store"); } ?>
<? if(allow_server_demos) { render_card("mysql.uce", "MySQL", "MySQL database connector"); } ?>
<div class="grid-heading">Advanced</div>
<? render_card("call_file.uce", "unit_call()", "Dynamic file inclusion"); ?>
<? render_card("unit-browser.uce", "Unit Browser", "units_list(), unit_info(), unit_compile()"); ?>
<? render_card("components.uce", "Components", "Reusable component system"); ?>
<? render_card("markdown.uce", "Markdown", "Markdown parsing with components"); ?>
<? render_card("script.uce", "Script", "UCE script integration"); ?>
<? render_card("task.uce", "Task", "Background task execution"); ?>
<? render_card("task_repeat.uce", "Task Repeat", "Recurring task scheduling"); ?>
<? render_card("websockets.ws.uce", "WebSockets", "Real-time WebSocket chat"); ?>
<? render_card("unit-browser.uce", "Unit Browser", "units_list(), unit_info(), unit_compile()"); ?>
<? if(allow_server_demos) { render_card("task.uce", "Task", "Background task execution"); } ?>
<? if(allow_server_demos) { render_card("task_repeat.uce", "Task Repeat", "Recurring task scheduling"); } ?>
</div>
<? if(!allow_server_demos) { ?>
<div class="system-info">
<h3>Restricted On Public Access</h3>
<pre>Local-only demos are hidden here because they can execute shell commands, access infrastructure services, mutate server state, compile units, or expose internal runtime details.</pre>
</div>
<? } else { ?>
<div class="system-info">
<h3>System Info</h3>
<pre><?
@@ -73,6 +82,7 @@ RENDER(Request& context)
print("Request #", context.server->request_count, "\n");
?></pre>
</div>
<? } ?>
<details>
<summary>Request Parameters</summary>
<pre><?= (var_dump(p)) ?></pre>
+7
View File
@@ -1,7 +1,14 @@
#include "demo_guard.h"
RENDER(Request& context)
{
if(!test_demo_request_allowed(context))
{
test_demo_render_restricted_html(context, "MemcacheD", "inspect and mutate local memcached state");
return;
}
DTree t;
<>
+7
View File
@@ -1,7 +1,14 @@
#include "demo_guard.h"
RENDER(Request& context)
{
if(!test_demo_request_allowed(context))
{
test_demo_render_restricted_html(context, "MySQL Connector Test", "connect to local database services and expose database contents");
return;
}
<><html>
<link rel="stylesheet" href='style.css?v=1'></link>
+7
View File
@@ -1,7 +1,14 @@
#include "demo_guard.h"
RENDER(Request& context)
{
if(!test_demo_request_allowed(context))
{
test_demo_render_restricted_html(context, "Shell Stuff", "execute shell commands and expose repository state");
return;
}
<><html>
<link rel="stylesheet" href='style.css?v=1'></link>
+7
View File
@@ -1,6 +1,13 @@
#include "demo_guard.h"
RENDER(Request& context)
{
if(!test_demo_request_allowed(context))
{
test_demo_render_restricted_text(context, "Task Status", "inspect background task state");
return;
}
String task_name = first(context.get["task-name"], "example-task");
print("Task Name: ", task_name, "\n");
+7
View File
@@ -1,6 +1,13 @@
#include "demo_guard.h"
RENDER(Request& context)
{
if(!test_demo_request_allowed(context))
{
test_demo_render_restricted_html(context, "Tasks", "spawn and inspect background tasks");
return;
}
DTree t;
String task_name = first(context.get["task-name"], "example-task");
+7
View File
@@ -1,6 +1,13 @@
#include "demo_guard.h"
RENDER(Request& context)
{
if(!test_demo_request_allowed(context))
{
test_demo_render_restricted_html(context, "Task repeat", "schedule recurring background tasks");
return;
}
DTree t;
String task_name = first(context.get["task-name"], "example-task");
+7
View File
@@ -1,6 +1,13 @@
#include "../demo_guard.h"
RENDER(Request& context)
{
if(!test_demo_request_allowed(context))
{
test_demo_render_restricted_text(context, "Working Directory Sub-Invoke", "reveal internal filesystem layout");
return;
}
print("Sub-Invoke Working dir: ", cwd_get(), "\n");
}
+20 -4
View File
@@ -1,3 +1,5 @@
#include "demo_guard.h"
String unit_query(String selected_path, String compile_path = "")
{
String result = "?";
@@ -32,6 +34,7 @@ String unit_flag_label(DTree value)
RENDER(Request& context)
{
bool allow_mutation = test_demo_request_allowed(context);
String compile_target = first(context.get["compile"], "");
String selected_path = first(context.get["selected"], "");
String compile_message = "";
@@ -39,10 +42,19 @@ RENDER(Request& context)
if(compile_target != "")
{
bool compile_ok = unit_compile(compile_target);
selected_path = compile_target;
compile_message = (compile_ok ? "Compile succeeded for " : "Compile failed for ") + compile_target;
compile_message_class = compile_ok ? "status-ok" : "status-error";
if(allow_mutation)
{
bool compile_ok = unit_compile(compile_target);
compile_message = (compile_ok ? "Compile succeeded for " : "Compile failed for ") + compile_target;
compile_message_class = compile_ok ? "status-ok" : "status-error";
}
else
{
context.set_status(403, "Restricted");
compile_message = "Manual compile is restricted to localhost and private-network access.";
compile_message_class = "status-error";
}
}
auto unit_paths = units_list();
@@ -122,7 +134,11 @@ RENDER(Request& context)
</div>
<div class="unit-actions">
<span class="status-badge <?= selected_status_class ?>"><?= selected_compile_status ?></span>
<a href="<?= selected_compile_link ?>">compile</a>
<? if(allow_mutation) { ?>
<a href="<?= selected_compile_link ?>">compile</a>
<? } else { ?>
<span class="dim">compile disabled on public access</span>
<? } ?>
</div>
</div>
+13 -1
View File
@@ -1,9 +1,21 @@
#include "demo_guard.h"
RENDER(Request& context)
{
bool allow_mutation = test_demo_request_allowed(context);
DTree payload;
if(context.get["compile"] != "")
payload["compile_ok"].set_bool(unit_compile(context.get["compile"]));
{
if(allow_mutation)
payload["compile_ok"].set_bool(unit_compile(context.get["compile"]));
else
{
context.set_status(403, "Restricted");
payload["error"] = "restricted";
payload["reason"] = "Manual compile is restricted to localhost and private-network access.";
}
}
payload["current"] = unit_info();
+7
View File
@@ -1,7 +1,14 @@
#include "demo_guard.h"
RENDER(Request& context)
{
if(!test_demo_request_allowed(context))
{
test_demo_render_restricted_html(context, "Working Directory", "reveal internal filesystem layout");
return;
}
<>
<link rel="stylesheet" href='style.css'></link>