From 16aa142c29d027f1f6e33658dd6fddeaa4e04231 Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Tue, 23 Nov 2021 19:25:18 +0100 Subject: [PATCH] examples: add ucode handler example Signed-off-by: Jo-Philipp Wich --- examples/ucode/dump-env.uc | 22 ++++++++++++++++++++++ examples/ucode/handler.uc | 7 +++++++ 2 files changed, 29 insertions(+) create mode 100644 examples/ucode/dump-env.uc create mode 100644 examples/ucode/handler.uc diff --git a/examples/ucode/dump-env.uc b/examples/ucode/dump-env.uc new file mode 100644 index 0000000..6bd7317 --- /dev/null +++ b/examples/ucode/dump-env.uc @@ -0,0 +1,22 @@ +Status: 200 OK +Content-Type: text/html + +

Headers

+ +{% for (let k, v in env.headers): %} +{{ replace(k, /(^|-)(.)/g, (m0, d, c) => d + uc(c)) }}: {{ v }}
+{% endfor %} + +

Environment

+ +{% for (let k, v in env): if (type(v) == 'string'): %} +{{ k }}={{ v }}
+{% endif; endfor %} + +{% if (env.CONTENT_LENGTH > 0): %} +

Body Contents

+ +{% for (let chunk = uhttpd.recv(64); chunk != null; chunk = uhttpd.recv(64)): %} +{{ replace(chunk, /[^[:graph:]]/g, '.') }}
+{% endfor %} +{% endif %} diff --git a/examples/ucode/handler.uc b/examples/ucode/handler.uc new file mode 100644 index 0000000..e71ac26 --- /dev/null +++ b/examples/ucode/handler.uc @@ -0,0 +1,7 @@ +{% + +'use strict'; + +global.handle_request = function(env) { + include("dump-env.uc", { env }); +}; -- 2.30.2