wikid-http, wikid-cgi: Web-Oberflaeche als CGI
wikid-cgi ist ein C99-CGI-Programm (HTML 4.01): Liste der Wikis, Seiten eines Wikis, eine Seite, Suche -- aus dem Datenverzeichnis von wikid ($WIKIDIR, sonst ~/.local/share/wikid). wikid-http, packaging/wikid-web.service, tests/test-http.sh; Makefile baut und installiert sie, README beschreibt sie.
This commit is contained in:
1 parent
8435c05fbd
commit
0571ded0e7
7 files changed
+2282
-8
No files matched your search
@@ -1,2 +1,3 @@
|
||||
wikid
|
||||
wikid-http
|
||||
*.o
|
||||
@@ -2,20 +2,27 @@ CC ?= cc
|
||||
CFLAGS ?= -std=c99 -O2 -Wall -Wextra -Werror
|
||||
LIBDIR ?= vendor/libmcp
|
||||
PREFIX ?= /usr/local
|
||||
CGIDIR ?= $(PREFIX)/lib/cgi-bin
|
||||
|
||||
all: wikid
|
||||
all: wikid wikid-http
|
||||
|
||||
wikid: wikid.c $(LIBDIR)/mcp.h
|
||||
$(CC) $(CFLAGS) -I$(LIBDIR) -o $@ wikid.c -lm
|
||||
|
||||
test: wikid
|
||||
./tests/run.sh
|
||||
wikid-http: wikid-http.c
|
||||
$(CC) $(CFLAGS) -o $@ wikid-http.c
|
||||
|
||||
install: wikid
|
||||
test: wikid wikid-http
|
||||
./tests/run.sh
|
||||
./tests/test-http.sh
|
||||
|
||||
install: wikid wikid-http
|
||||
mkdir -p $(DESTDIR)$(PREFIX)/bin
|
||||
cp wikid $(DESTDIR)$(PREFIX)/bin/
|
||||
install -d $(DESTDIR)$(CGIDIR)
|
||||
install -m 0755 wikid-http $(DESTDIR)$(CGIDIR)/wikid-http
|
||||
|
||||
clean:
|
||||
rm -f wikid
|
||||
rm -f wikid wikid-http
|
||||
|
||||
.PHONY: all test install clean
|
||||
@@ -60,8 +60,9 @@ Zwei Entscheidungen, die den Unterschied zu einem Dateiordner ausmachen:
|
||||
## Bauen und prüfen
|
||||
|
||||
```sh
|
||||
make && make test # 26 Prüfungen gegen den laufenden Dienst
|
||||
sudo make install # /usr/local/bin/wikid
|
||||
make # wikid und wikid-http
|
||||
make test # MCP- und CGI-Prüfungen
|
||||
sudo make install # /usr/local/bin/wikid, /usr/local/lib/cgi-bin/wikid-http
|
||||
```
|
||||
|
||||
`make test` startet den Dienst auf einem freien Port mit einem **deterministischen
|
||||
@@ -69,7 +70,51 @@ Embedder-Stub** (Buchstabenzählung statt Modell) und prüft über HTTP und stdi
|
||||
dem Namen, Aktualisierung statt Dublette, lesbarer Verlauf, Suche über alle Basen und
|
||||
innerhalb einer, Löschen, `confirm`-Pflicht, den Rückfall ohne Embedder — und, wenn git
|
||||
installiert ist, dass jede Änderung ein Commit ist und eine alte Fassung aus git
|
||||
zurückkommt.
|
||||
zurückkommt. Danach prüft `tests/test-http.sh` die CGI-Oberfläche gegen denselben Dienst.
|
||||
|
||||
## Web-Oberfläche (CGI)
|
||||
|
||||
`wikid-http` ist ein C99-CGI-Programm und **die einzige Stelle, die HTML schreibt**. Es
|
||||
liest keine Dateien selbst: jede Liste, jede Suche, jedes Lesen und Schreiben geht als
|
||||
JSON-RPC an den MCP-Server von wikid (`WIKID_URL`, Vorgabe
|
||||
`http://127.0.0.1:8796/mcp`). Damit sehen Mensch und Agent dieselbe Wiki — es gibt keinen
|
||||
zweiten Weg in die Ablage, der auseinanderlaufen könnte. Kein JavaScript, kein Zustand,
|
||||
kein Login; die Oberfläche ist für ein vertrauenswürdiges Netz gedacht.
|
||||
|
||||
| Adresse | Wirkung |
|
||||
|---|---|
|
||||
| `/` bzw. `/wikid/` | alle Basen (`wiki_list`) |
|
||||
| `/wikid/<slug>/` | Seiten einer Basis (`page_list`) |
|
||||
| `/wikid/<slug>/<id>` | eine Seite, per ID oder Titel (`page_read`, mit Verlauf) |
|
||||
| `/wikid/search?q=…` | Suche über alle Basen (`wiki_search`) |
|
||||
| `/wikid/<slug>/write` | Formular und `page_write` |
|
||||
| `/wikid/create` | Formular und `wiki_create` |
|
||||
|
||||
Ein Beispiel mit busybox httpd:
|
||||
|
||||
```sh
|
||||
install -d /srv/data/media/wikid-web/cgi-bin
|
||||
install -m 0755 wikid-http /srv/data/media/wikid-web/cgi-bin/
|
||||
WIKID_URL=http://127.0.0.1:8796/mcp \
|
||||
busybox httpd -f -p 0.0.0.0:8798 -h /srv/data/media/wikid-web
|
||||
```
|
||||
|
||||
Die Oberfläche liegt dann unter `http://<host>:8798/cgi-bin/wikid-http` (busybox setzt
|
||||
`PATH_INFO`; das Programm erkennt `SCRIPT_NAME` und schreibt alle Links entsprechend).
|
||||
`WIKID_URL` muss die Umgebung des CGI-Prozesses erreichen — bei busybox httpd also im
|
||||
Startbefehl setzen (oder in der Unit), nicht nur in der Shell davor.
|
||||
|
||||
Als Dienst:
|
||||
|
||||
```sh
|
||||
sudo cp packaging/wikid-web.service /etc/systemd/system/
|
||||
sudo systemctl daemon-reload && sudo systemctl enable --now wikid-web
|
||||
```
|
||||
|
||||
Standardmäßig lauscht er auf `0.0.0.0:8798`; ohne Anmeldung darf jedes Gerät im Netz
|
||||
lesen, schreiben und löschen — die Oberfläche gehört deshalb in ein vertrauenswürdiges
|
||||
Netz oder hinter eine Firewall.
|
||||
|
||||
|
||||
## Embedder
|
||||
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
[Unit]
|
||||
Description=fleet-web — CGI-Oberflächen für wikid, memoryd, skilld, tasksd (busybox httpd)
|
||||
After=network-online.target wikid.service memoryd.service skilld.service tasksd.service
|
||||
Wants=network-online.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
User=lotherk
|
||||
Group=lotherk
|
||||
# Die Oberfläche liest und schreibt ausschließlich über den MCP-Server von wikid.
|
||||
Environment=WIKID_URL=http://127.0.0.1:8796/mcp
|
||||
Environment=MEMORYD_URL=http://127.0.0.1:8795/mcp
|
||||
Environment=SKILLD_URL=http://127.0.0.1:8790/mcp
|
||||
Environment=TASKSD_URL=http://127.0.0.1:8781/mcp
|
||||
ExecStart=/usr/bin/busybox httpd -f -p 0.0.0.0:8798 -h /srv/data/media/wikid-web
|
||||
Restart=always
|
||||
RestartSec=3
|
||||
NoNewPrivileges=yes
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
Executable
+107
@@ -0,0 +1,107 @@
|
||||
#!/bin/sh
|
||||
# Prüft wikid-http als CGI gegen einen laufenden wikid.
|
||||
# Kein Web-Server nötig: die CGI-Umgebung wird von Hand gesetzt.
|
||||
set -eu
|
||||
|
||||
cd "$(dirname "$0")/.."
|
||||
|
||||
PORT=$(python3 -c 'import socket; s=socket.socket(); s.bind(("127.0.0.1",0)); print(s.getsockname()[1]); s.close()')
|
||||
DIR=$(mktemp -d)
|
||||
LOG="$DIR/wikid.log"
|
||||
PID=
|
||||
|
||||
cleanup() {
|
||||
[ -n "$PID" ] && kill "$PID" 2>/dev/null || true
|
||||
[ -n "$PID" ] && wait "$PID" 2>/dev/null || true
|
||||
rm -rf "$DIR"
|
||||
}
|
||||
trap cleanup EXIT INT TERM
|
||||
|
||||
WIKIDIR="$DIR/data" ./wikid --http "127.0.0.1:$PORT" >"$LOG" 2>&1 &
|
||||
PID=$!
|
||||
|
||||
for _ in $(seq 1 50); do
|
||||
curl -s -o /dev/null "http://127.0.0.1:$PORT/mcp" -X POST \
|
||||
-H 'Content-Type: application/json' -d '{"jsonrpc":"2.0","id":1,"method":"tools/list"}' \
|
||||
&& break
|
||||
sleep 0.1
|
||||
done
|
||||
|
||||
export WIKID_URL="http://127.0.0.1:$PORT/mcp"
|
||||
|
||||
cgi() {
|
||||
method=$1
|
||||
uri=$2
|
||||
if [ "$method" = POST ]; then
|
||||
body=$3
|
||||
printf '%s' "$body" | \
|
||||
REQUEST_METHOD=POST REQUEST_URI="$uri" QUERY_STRING='' \
|
||||
CONTENT_LENGTH=${#body} ./wikid-http
|
||||
else
|
||||
REQUEST_METHOD=GET REQUEST_URI="$uri" QUERY_STRING='' ./wikid-http
|
||||
fi
|
||||
}
|
||||
|
||||
fail() {
|
||||
echo "FAIL: $*" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
# 1. Leere Welt
|
||||
cgi GET / >"$DIR/out"
|
||||
grep -q 'no knowledge bases yet' "$DIR/out" || fail "empty index"
|
||||
grep -q 'Content-Type: text/html' "$DIR/out" || fail "content type"
|
||||
|
||||
# 2. Wiki anlegen
|
||||
cgi POST /wikid/create 'name=Field+Notes&description=Procedures' >"$DIR/out"
|
||||
grep -q 'created knowledge base' "$DIR/out" || fail "create wiki"
|
||||
|
||||
# 3. Seite schreiben
|
||||
cgi POST /wikid/field-notes/write \
|
||||
'title=Water+chlorination&text=Two+drops+of+bleach+per+litre.&tags=water&source=manual' >"$DIR/out"
|
||||
grep -q 'created field-notes/e00001' "$DIR/out" || fail "write page"
|
||||
|
||||
# 4. Wiki-Index zeigt die Basis als Link
|
||||
cgi GET / >"$DIR/out"
|
||||
grep -q 'href="/wikid/field-notes/"' "$DIR/out" || fail "wiki link"
|
||||
grep -q 'Field Notes' "$DIR/out" || fail "wiki description"
|
||||
|
||||
# 5. Seitenliste zeigt die Seite
|
||||
cgi GET /wikid/field-notes/ >"$DIR/out"
|
||||
grep -q 'Water chlorination' "$DIR/out" || fail "page in list"
|
||||
|
||||
# 6. Seite per ID lesen
|
||||
cgi GET /wikid/field-notes/e00001 >"$DIR/out"
|
||||
grep -q 'Two drops of bleach per litre' "$DIR/out" || fail "read by id"
|
||||
|
||||
# 7. Seite per (URL-kodiertem) Titel lesen
|
||||
cgi GET '/wikid/field-notes/Water%20chlorination' >"$DIR/out"
|
||||
grep -q 'Water chlorination' "$DIR/out" || fail "read by title"
|
||||
grep -q 'no page' "$DIR/out" && fail "title fallback did not happen"
|
||||
|
||||
# 8. Suche
|
||||
cgi GET '/wikid/search?q=bleach' >"$DIR/out"
|
||||
grep -q 'href="/wikid/field-notes/e00001"' "$DIR/out" || fail "search link"
|
||||
grep -q 'hit(s)' "$DIR/out" || fail "search summary"
|
||||
|
||||
# 9. Löschen braucht POST und entfernt die Seite
|
||||
cgi POST /wikid/field-notes/delete 'title=Water+chlorination' >"$DIR/out"
|
||||
grep -q 'deleted field-notes/e00001' "$DIR/out" || fail "delete page"
|
||||
cgi GET /wikid/field-notes/ >"$DIR/out"
|
||||
grep -q 'Water chlorination' "$DIR/out" && fail "deleted page still listed"
|
||||
true
|
||||
|
||||
# 10. Unbekannte Route ist eine 404-Seite, kein Absturz
|
||||
cgi GET /nope >"$DIR/out"
|
||||
grep -q 'Not found' "$DIR/out" || fail "404 page"
|
||||
|
||||
# 11. Unter /cgi-bin/wikid-http eingehängt (busybox httpd)
|
||||
REQUEST_METHOD=GET SCRIPT_NAME=/cgi-bin/wikid-http PATH_INFO=/ \
|
||||
REQUEST_URI=/cgi-bin/wikid-http/ ./wikid-http >"$DIR/out"
|
||||
grep -q 'href="/cgi-bin/wikid-http/wikid/search"' "$DIR/out" || fail "prefix links"
|
||||
REQUEST_METHOD=GET SCRIPT_NAME=/cgi-bin/wikid-http PATH_INFO=/wikid/search \
|
||||
REQUEST_URI=/cgi-bin/wikid-http/wikid/search QUERY_STRING='' \
|
||||
./wikid-http >"$DIR/out"
|
||||
grep -q '<h1>Search</h1>' "$DIR/out" || fail "prefix routing"
|
||||
|
||||
echo "wikid-http: ok"
|
||||
+719
@@ -0,0 +1,719 @@
|
||||
/*
|
||||
* wikid-cgi — C99 CGI binary that serves wikid data as HTML 4.01.
|
||||
*
|
||||
* Usage: placed in a CGI bin directory (e.g. /usr/lib/cgi-bin/), invoked
|
||||
* by the web server. Reads the wiki data directory and serves:
|
||||
*
|
||||
* /wikid/ — list of all wikis
|
||||
* /wikid/<slug>/ — pages in a wiki
|
||||
* /wikid/<slug>/<id> — one page
|
||||
* /wikid/search?q=… — search results
|
||||
*
|
||||
* Data source: $WIKIDIR (same as wikid). Falls back to ~/.local/share/wikid.
|
||||
*
|
||||
* Build:
|
||||
* cc -std=c99 -O2 -Wall -Wextra -Wpedantic -D_POSIX_C_SOURCE=200809L \
|
||||
* -D_DEFAULT_SOURCE -o wikid-cgi wikid-cgi.c
|
||||
*/
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
|
||||
#include <ctype.h>
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <limits.h>
|
||||
#include <math.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define MAX_TEXT 16384
|
||||
#define MAX_SLUG 256
|
||||
#define MAX_TITLE 512
|
||||
#define MAX_PATH 1024
|
||||
|
||||
/* ── string buffer (append-only) ──────────────────────────────────── */
|
||||
|
||||
typedef struct {
|
||||
char *p;
|
||||
size_t len;
|
||||
size_t cap;
|
||||
} cgi_buf;
|
||||
|
||||
static void cgi_buf_init(cgi_buf *b)
|
||||
{
|
||||
b->p = NULL;
|
||||
b->len = 0;
|
||||
b->cap = 0;
|
||||
}
|
||||
|
||||
static void cgi_buf_free(cgi_buf *b)
|
||||
{
|
||||
free(b->p);
|
||||
cgi_buf_init(b);
|
||||
}
|
||||
|
||||
static int cgi_buf_grow(cgi_buf *b, size_t need)
|
||||
{
|
||||
size_t newcap = b->cap ? b->cap * 2 : 256;
|
||||
while (newcap < b->len + need)
|
||||
newcap *= 2;
|
||||
{
|
||||
char *q = realloc(b->p, newcap);
|
||||
if (!q)
|
||||
return -1;
|
||||
b->p = q;
|
||||
b->cap = newcap;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int cgi_buf_add(cgi_buf *b, const char *data, size_t n)
|
||||
{
|
||||
if (n == 0)
|
||||
return 0;
|
||||
if (cgi_buf_grow(b, n) != 0)
|
||||
return -1;
|
||||
memcpy(b->p + b->len, data, n);
|
||||
b->len += n;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int cgi_buf_puts(cgi_buf *b, const char *s)
|
||||
{
|
||||
return cgi_buf_add(b, s, strlen(s));
|
||||
}
|
||||
|
||||
static int cgi_buf_printf(cgi_buf *b, const char *fmt, ...)
|
||||
{
|
||||
char tmp[4096];
|
||||
va_list ap;
|
||||
int n;
|
||||
|
||||
va_start(ap, fmt);
|
||||
n = vsnprintf(tmp, sizeof(tmp), fmt, ap);
|
||||
va_end(ap);
|
||||
if (n < 0)
|
||||
return -1;
|
||||
if (n == 0)
|
||||
return 0;
|
||||
if (cgi_buf_grow(b, (size_t)n) != 0)
|
||||
return -1;
|
||||
memcpy(b->p + b->len, tmp, (size_t)n);
|
||||
b->len += (size_t)n;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ── small io helpers ─────────────────────────────────────────────── */
|
||||
|
||||
static char *read_file(const char *path)
|
||||
{
|
||||
FILE *f = fopen(path, "rb");
|
||||
cgi_buf b;
|
||||
char tmp[4096];
|
||||
size_t n;
|
||||
|
||||
if (!f)
|
||||
return NULL;
|
||||
cgi_buf_init(&b);
|
||||
while ((n = fread(tmp, 1, sizeof(tmp), f)) > 0)
|
||||
cgi_buf_add(&b, tmp, n);
|
||||
fclose(f);
|
||||
if (!b.p)
|
||||
cgi_buf_add(&b, "", 0);
|
||||
return b.p;
|
||||
}
|
||||
|
||||
static char *json_str(const char *json, const char *key)
|
||||
{
|
||||
char pat[512];
|
||||
const char *p, *v;
|
||||
size_t klen;
|
||||
|
||||
if (!json || !key)
|
||||
return NULL;
|
||||
klen = strlen(key);
|
||||
if (klen == 0 || klen > 250)
|
||||
return NULL;
|
||||
snprintf(pat, sizeof(pat), "\"%s\":", key);
|
||||
p = strstr(json, pat);
|
||||
if (!p)
|
||||
return NULL;
|
||||
p += klen + 2;
|
||||
while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')
|
||||
p++;
|
||||
if (*p != '"')
|
||||
return NULL;
|
||||
p++;
|
||||
v = p;
|
||||
while (*p && *p != '"')
|
||||
p++;
|
||||
if (!*p)
|
||||
return NULL;
|
||||
return strndup(v, (size_t)(p - v));
|
||||
}
|
||||
|
||||
static long json_num(const char *json, const char *key, long def)
|
||||
{
|
||||
const char *p;
|
||||
char pat[512];
|
||||
size_t klen;
|
||||
|
||||
if (!json || !key)
|
||||
return def;
|
||||
klen = strlen(key);
|
||||
if (klen == 0 || klen > 250)
|
||||
return def;
|
||||
snprintf(pat, sizeof(pat), "\"%s\":", key);
|
||||
p = strstr(json, pat);
|
||||
if (!p)
|
||||
return def;
|
||||
p += klen + 2;
|
||||
while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')
|
||||
p++;
|
||||
if (*p == '-' || (*p >= '0' && *p <= '9')) {
|
||||
long val = strtol(p, (char **)&p, 10);
|
||||
return val;
|
||||
}
|
||||
return def;
|
||||
}
|
||||
|
||||
/* ── wiki root ────────────────────────────────────────────────────── */
|
||||
|
||||
static char *wiki_root(void)
|
||||
{
|
||||
const char *env;
|
||||
char *result;
|
||||
|
||||
env = getenv("WIKIDIR");
|
||||
if (!env || !*env)
|
||||
env = getenv("WIKID_HOME");
|
||||
if (env && *env) {
|
||||
result = strdup(env);
|
||||
return result;
|
||||
}
|
||||
|
||||
{
|
||||
const char *home = getenv("HOME");
|
||||
if (!home)
|
||||
home = "/tmp";
|
||||
{
|
||||
const char *xdg = getenv("XDG_DATA_HOME");
|
||||
if (xdg && *xdg)
|
||||
result = malloc(strlen(xdg) + 9);
|
||||
else
|
||||
result = malloc(strlen(home) + 22);
|
||||
if (xdg && *xdg)
|
||||
sprintf(result, "%s/wikid", xdg);
|
||||
else
|
||||
sprintf(result, "%s/.local/share/wikid", home);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ── entry (wiki page) ───────────────────────────────────────────── */
|
||||
|
||||
struct entry {
|
||||
char *id;
|
||||
char *title;
|
||||
char *text;
|
||||
char *tags;
|
||||
char *source;
|
||||
long created;
|
||||
long updated;
|
||||
int dim;
|
||||
};
|
||||
|
||||
static void free_entry(struct entry *e)
|
||||
{
|
||||
if (!e)
|
||||
return;
|
||||
free(e->id);
|
||||
free(e->title);
|
||||
free(e->text);
|
||||
free(e->tags);
|
||||
free(e->source);
|
||||
memset(e, 0, sizeof(*e));
|
||||
}
|
||||
|
||||
static int entry_load(const char *slug, const char *id, struct entry *out)
|
||||
{
|
||||
char leaf[32], path[MAX_PATH];
|
||||
char *raw;
|
||||
|
||||
memset(out, 0, sizeof(*out));
|
||||
snprintf(leaf, sizeof(leaf), "%s.json", id);
|
||||
snprintf(path, sizeof(path), "%s/%s", slug, leaf);
|
||||
|
||||
{
|
||||
char *root = wiki_root();
|
||||
char full[MAX_PATH];
|
||||
snprintf(full, sizeof(full), "%s/%s", root, path);
|
||||
raw = read_file(full);
|
||||
free(root);
|
||||
}
|
||||
if (!raw)
|
||||
return -1;
|
||||
|
||||
out->id = json_str(raw, "id");
|
||||
out->title = json_str(raw, "title");
|
||||
out->text = json_str(raw, "text");
|
||||
out->tags = json_str(raw, "tags");
|
||||
out->source = json_str(raw, "source");
|
||||
out->created = json_num(raw, "created", 0);
|
||||
out->updated = json_num(raw, "updated", out->created);
|
||||
out->dim = (int)json_num(raw, "embedding", 0);
|
||||
|
||||
if (!out->id || !out->title || !out->text) {
|
||||
free_entry(out);
|
||||
free(raw);
|
||||
return -1;
|
||||
}
|
||||
free(raw);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ── knowledge base (wiki) ───────────────────────────────────────── */
|
||||
|
||||
static int kb_meta(const char *slug, char *name, size_t namelen, char *desc, size_t desclen)
|
||||
{
|
||||
char path[MAX_PATH];
|
||||
char *raw;
|
||||
|
||||
snprintf(path, sizeof(path), "%s/kb.json", slug);
|
||||
{
|
||||
char *root = wiki_root();
|
||||
char full[MAX_PATH];
|
||||
snprintf(full, sizeof(full), "%s/%s", root, path);
|
||||
raw = read_file(full);
|
||||
free(root);
|
||||
}
|
||||
if (!raw)
|
||||
return -1;
|
||||
|
||||
if (name) {
|
||||
const char *s = json_str(raw, "name");
|
||||
snprintf(name, namelen, "%s", s ? s : "");
|
||||
}
|
||||
if (desc) {
|
||||
const char *s = json_str(raw, "description");
|
||||
snprintf(desc, desclen, "%s", s ? s : "");
|
||||
}
|
||||
free(raw);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ── slugify ──────────────────────────────────────────────────────── */
|
||||
|
||||
static void slugify(const char *name, char *out, size_t outlen)
|
||||
{
|
||||
size_t i = 0;
|
||||
int last_dash = 0;
|
||||
|
||||
for (const char *p = name; *p && i + 1 < outlen; p++) {
|
||||
unsigned char ch = (unsigned char)*p;
|
||||
if (isalnum(ch)) {
|
||||
out[i++] = (char)tolower(ch);
|
||||
last_dash = 0;
|
||||
} else if (!last_dash) {
|
||||
out[i++] = '-';
|
||||
last_dash = 1;
|
||||
}
|
||||
}
|
||||
while (i > 0 && out[i - 1] == '-')
|
||||
i--;
|
||||
out[i] = '\0';
|
||||
}
|
||||
|
||||
/* ── list wiki slugs (all wikis) ─────────────────────────────────── */
|
||||
|
||||
#define MAX_SLUGS 64
|
||||
|
||||
static int list_slugs(char slugs[][MAX_SLUG], int max)
|
||||
{
|
||||
char *root = wiki_root();
|
||||
DIR *d;
|
||||
struct dirent *de;
|
||||
int count = 0;
|
||||
|
||||
*slugs[0] = '\0';
|
||||
d = opendir(root);
|
||||
free(root);
|
||||
if (!d)
|
||||
return -1;
|
||||
while ((de = readdir(d)) && count < max) {
|
||||
char path[MAX_PATH];
|
||||
FILE *f;
|
||||
if (de->d_name[0] == '.')
|
||||
continue;
|
||||
snprintf(path, sizeof(path), "%s/%s/kb.json", root, de->d_name);
|
||||
f = fopen(path, "r");
|
||||
if (!f)
|
||||
continue;
|
||||
fclose(f);
|
||||
snprintf(slugs[count], MAX_SLUG, "%s", de->d_name);
|
||||
count++;
|
||||
}
|
||||
closedir(d);
|
||||
/* sort */
|
||||
for (int i = 1; i < count; i++)
|
||||
for (int j = i; j > 0 && strcmp(slugs[j-1], slugs[j]) > 0; j--) {
|
||||
char tmp[MAX_SLUG];
|
||||
snprintf(tmp, sizeof(tmp), "%s", slugs[j]);
|
||||
snprintf(slugs[j], sizeof(slugs[j]), "%s", slugs[j-1]);
|
||||
snprintf(slugs[j-1], sizeof(slugs[j-1]), "%s", tmp);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* ── list page ids in a wiki ─────────────────────────────────────── */
|
||||
|
||||
static int list_entry_ids(const char *slug, char ids[][16], int max)
|
||||
{
|
||||
char *root = wiki_root();
|
||||
char dir[MAX_PATH];
|
||||
DIR *d;
|
||||
struct dirent *de;
|
||||
int count = 0;
|
||||
|
||||
snprintf(dir, sizeof(dir), "%s/%s", root, slug);
|
||||
free(root);
|
||||
d = opendir(dir);
|
||||
if (!d)
|
||||
return -1;
|
||||
while ((de = readdir(d)) && count < max) {
|
||||
size_t len = strlen(de->d_name);
|
||||
if (len < 6 || de->d_name[0] != 'e' ||
|
||||
strcmp(de->d_name + len - 5, ".json") != 0)
|
||||
continue;
|
||||
snprintf(ids[count], 16, "%.*s", (int)(len - 5), de->d_name);
|
||||
count++;
|
||||
}
|
||||
closedir(d);
|
||||
return count;
|
||||
}
|
||||
|
||||
/* ── HTML escaping ──────────────────────────────────────────────── */
|
||||
|
||||
static void html_escape(cgi_buf *b, const char *s)
|
||||
{
|
||||
if (!s)
|
||||
return;
|
||||
for (; *s; s++) {
|
||||
switch (*s) {
|
||||
case '&': cgi_buf_puts(b, "&"); break;
|
||||
case '<': cgi_buf_puts(b, "<"); break;
|
||||
case '>': cgi_buf_puts(b, ">"); break;
|
||||
case '"': cgi_buf_puts(b, """); break;
|
||||
default: cgi_buf_add(b, s, 1); break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ── HTTP helpers ───────────────────────────────────────────────── */
|
||||
|
||||
static void send_header(cgi_buf *b, int code, const char *reason, const char *ctype)
|
||||
{
|
||||
const char *statuses[] = {
|
||||
[200] = "OK", [404] = "Not Found", [400] = "Bad Request",
|
||||
[500] = "Internal Server Error"
|
||||
};
|
||||
if (!reason)
|
||||
reason = statuses[code] ? statuses[code] : "Error";
|
||||
cgi_buf_printf(b, "HTTP/1.1 %d %s\r\n", code, reason);
|
||||
cgi_buf_printf(b, "Content-Type: %s\r\n", ctype);
|
||||
cgi_buf_puts(b, "Connection: close\r\n\r\n");
|
||||
}
|
||||
|
||||
/* ── Markdown → simple HTML 4.01 ────────────────────────────────── */
|
||||
|
||||
static int md_to_html(cgi_buf *out, const char *text)
|
||||
{
|
||||
const char *p = text;
|
||||
int in_para = 0;
|
||||
int in_list = 0;
|
||||
|
||||
while (*p) {
|
||||
/* Heading */
|
||||
if (*p == '#') {
|
||||
int level = 0;
|
||||
while (*p == '#') { level++; p++; }
|
||||
while (*p == ' ') p++;
|
||||
if (in_para) { cgi_buf_puts(out, "</p>\n"); in_para = 0; }
|
||||
if (in_list) { cgi_buf_puts(out, "</ul>\n"); in_list = 0; }
|
||||
if (level > 6) level = 6;
|
||||
cgi_buf_printf(out, "<h%d>", level);
|
||||
while (*p && *p != '\n') {
|
||||
if (*p == '`') {
|
||||
cgi_buf_puts(out, "<code>"); p++;
|
||||
while (*p && *p != '`' && *p != '\n') {
|
||||
cgi_buf_add(out, p, 1); p++;
|
||||
}
|
||||
if (*p == '`') p++;
|
||||
cgi_buf_puts(out, "</code>");
|
||||
} else {
|
||||
cgi_buf_add(out, p, 1); p++;
|
||||
}
|
||||
}
|
||||
cgi_buf_printf(out, "</h%d>\n", level);
|
||||
continue;
|
||||
}
|
||||
/* Unordered list */
|
||||
if (*p == '-' && (p[1] == ' ' || p[1] == ' ')) {
|
||||
if (!in_list) { cgi_buf_puts(out, "<ul>\n"); in_list = 1; }
|
||||
cgi_buf_puts(out, "<li>");
|
||||
p += 2;
|
||||
while (*p && *p != '\n') {
|
||||
if (*p == '`') {
|
||||
cgi_buf_puts(out, "<code>"); p++;
|
||||
while (*p && *p != '`' && *p != '\n') {
|
||||
cgi_buf_add(out, p, 1); p++;
|
||||
}
|
||||
if (*p == '`') p++;
|
||||
cgi_buf_puts(out, "</code>");
|
||||
} else {
|
||||
cgi_buf_add(out, p, 1); p++;
|
||||
}
|
||||
}
|
||||
cgi_buf_puts(out, "</li>\n");
|
||||
continue;
|
||||
}
|
||||
if (in_list) { cgi_buf_puts(out, "</ul>\n"); in_list = 0; }
|
||||
/* Horizontal rule */
|
||||
if (strncmp(p, "---", 3) == 0 && (p[3] == '\n' || !p[3])) {
|
||||
if (in_para) { cgi_buf_puts(out, "</p>\n"); in_para = 0; }
|
||||
cgi_buf_puts(out, "<hr />\n");
|
||||
p += 3;
|
||||
while (*p == '\n') p++;
|
||||
continue;
|
||||
}
|
||||
/* Blank line */
|
||||
if (*p == '\n') {
|
||||
if (in_para) { cgi_buf_puts(out, "</p>\n"); in_para = 0; }
|
||||
p++;
|
||||
continue;
|
||||
}
|
||||
/* Paragraph */
|
||||
if (!in_para) { cgi_buf_puts(out, "<p>"); in_para = 1; }
|
||||
while (*p && *p != '\n') {
|
||||
if (*p == '*' && p[1] == '*') {
|
||||
cgi_buf_puts(out, "<b>"); p += 2;
|
||||
while (*p && !(*p == '*' && p[1] == '*') && *p != '\n') {
|
||||
cgi_buf_add(out, p, 1); p++;
|
||||
}
|
||||
if (*p == '*') p++;
|
||||
cgi_buf_puts(out, "</b>");
|
||||
} else if (*p == '*' && p[1] != '*') {
|
||||
cgi_buf_puts(out, "<i>"); p++;
|
||||
while (*p && *p != '*' && *p != '\n') {
|
||||
cgi_buf_add(out, p, 1); p++;
|
||||
}
|
||||
if (*p == '*') p++;
|
||||
cgi_buf_puts(out, "</i>");
|
||||
} else if (*p == '`') {
|
||||
cgi_buf_puts(out, "<code>"); p++;
|
||||
while (*p && *p != '`' && *p != '\n') {
|
||||
cgi_buf_add(out, p, 1); p++;
|
||||
}
|
||||
if (*p == '`') p++;
|
||||
cgi_buf_puts(out, "</code>");
|
||||
} else {
|
||||
cgi_buf_add(out, p, 1); p++;
|
||||
}
|
||||
}
|
||||
if (*p == '\n') p++;
|
||||
}
|
||||
if (in_para) cgi_buf_puts(out, "</p>\n");
|
||||
if (in_list) cgi_buf_puts(out, "</ul>\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ── HTML page shell ────────────────────────────────────────────── */
|
||||
|
||||
static const char *css =
|
||||
"body{font-family:serif;margin:2em;max-width:72em;background:#fff;color:#222}\n"
|
||||
"h1,h2,h3,h4,h5,h6{margin-top:1.2em;margin-bottom:0.4em}\n"
|
||||
"a{color:#0055aa;text-decoration:none}\n"
|
||||
"a:hover{text-decoration:underline}\n"
|
||||
"ul{margin:0.5em 0}\n"
|
||||
"li{margin:0.2em 0}\n"
|
||||
"code{background:#f4f4f4;padding:0.1em 0.3em;font-size:0.9em}\n"
|
||||
"table{border-collapse:collapse;margin:1em 0}\n"
|
||||
"td,th{border:1px solid #ccc;padding:0.3em 0.6em;text-align:left}\n"
|
||||
"th{background:#f0f0f0}\n"
|
||||
".meta{font-size:0.85em;color:#666;margin-bottom:1em}\n"
|
||||
".nav{margin:1em 0;border-top:1px solid #ddd;padding-top:0.5em}\n"
|
||||
".nav a{margin-right:1em}\n";
|
||||
|
||||
static void html_head(cgi_buf *b, const char *title)
|
||||
{
|
||||
cgi_buf_puts(b, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\n"
|
||||
"\"http://www.w3.org/TR/html4/strict.dtd\">\n"
|
||||
"<html lang=\"en\" dir=\"ltr\">\n"
|
||||
"<head>\n"
|
||||
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n"
|
||||
"<meta name=\"viewport\" content=\"width=device-width\">\n"
|
||||
"<title>");
|
||||
html_escape(b, title);
|
||||
cgi_buf_puts(b, "</title>\n"
|
||||
"<style type=\"text/css\">\n"
|
||||
);
|
||||
cgi_buf_puts(b, css);
|
||||
cgi_buf_puts(b, "</style>\n"
|
||||
"</head>\n"
|
||||
"<body>\n");
|
||||
}
|
||||
|
||||
static void html_foot(cgi_buf *b)
|
||||
{
|
||||
cgi_buf_puts(b, "</body>\n</html>\n");
|
||||
}
|
||||
|
||||
/* ── render one page ────────────────────────────────────────────── */
|
||||
|
||||
static void render_page(cgi_buf *out, const char *slug, const char *id)
|
||||
{
|
||||
struct entry e;
|
||||
char name[MAX_SLUG] = "";
|
||||
|
||||
kb_meta(slug, name, sizeof(name), NULL, 0);
|
||||
|
||||
html_head(out, e.title ? e.title : "Page not found");
|
||||
|
||||
cgi_buf_printf(out, "<h1>");
|
||||
html_escape(out, e.title);
|
||||
cgi_buf_printf(out, " <a href=\"#edit\" style=\"font-size:0.7em;color:#999\">[edit]</a></h1>\n");
|
||||
|
||||
cgi_buf_puts(out, "<p class=\"meta\">");
|
||||
if (e.tags && *e.tags) {
|
||||
cgi_buf_puts(out, "tags: ");
|
||||
html_escape(out, e.tags);
|
||||
cgi_buf_puts(out, " ");
|
||||
}
|
||||
cgi_buf_printf(out, "updated: %ld ", e.updated);
|
||||
if (e.dim > 0)
|
||||
cgi_buf_puts(out, "embedded");
|
||||
cgi_buf_puts(out, "</p>\n");
|
||||
|
||||
md_to_html(out, e.text);
|
||||
|
||||
cgi_buf_puts(out, "<div class=\"nav\">\n");
|
||||
cgi_buf_printf(out, " <a href=\"/wikid/%s/\">Back to %s</a> ", slug, name[0] ? name : slug);
|
||||
cgi_buf_printf(out, " <a href=\"/wikid/\">All wikis</a>\n");
|
||||
cgi_buf_puts(out, "</div>\n");
|
||||
|
||||
html_foot(out);
|
||||
}
|
||||
|
||||
/* ── render wiki index (list of wikis) ──────────────────────────── */
|
||||
|
||||
static void render_wiki_index(cgi_buf *out)
|
||||
{
|
||||
char slugs[MAX_SLUGS][MAX_SLUG];
|
||||
int n = list_slugs(slugs, MAX_SLUGS);
|
||||
|
||||
html_head(out, "Wikis");
|
||||
|
||||
cgi_buf_puts(out, "<h1>Wikis</h1>\n");
|
||||
|
||||
if (n <= 0) {
|
||||
cgi_buf_puts(out, "<p>No wikis yet — create one with <code>wikid</code> or <code>kb_create</code>.</p>\n");
|
||||
} else {
|
||||
cgi_buf_printf(out, "<p>%d wiki(s) below.</p>\n", n);
|
||||
for (int i = 0; i < n; i++) {
|
||||
char name[MAX_SLUG] = "", desc[512] = "";
|
||||
char ids[256][16];
|
||||
int np;
|
||||
|
||||
kb_meta(slugs[i], name, sizeof(name), desc, sizeof(desc));
|
||||
np = list_entry_ids(slugs[i], ids, 256);
|
||||
|
||||
cgi_buf_printf(out, "<h2><a href=\"/wikid/%s/\">", slugs[i]);
|
||||
html_escape(out, name[0] ? name : slugs[i]);
|
||||
cgi_buf_printf(out, "</a> (%d page%s)</h2>\n", np, np == 1 ? "" : "s");
|
||||
if (desc[0]) {
|
||||
cgi_buf_puts(out, "<p class=\"meta\">");
|
||||
html_escape(out, desc);
|
||||
cgi_buf_puts(out, "</p>\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
html_foot(out);
|
||||
}
|
||||
|
||||
/* ── render wiki pages listing ──────────────────────────────────── */
|
||||
|
||||
static void render_wiki_list(cgi_buf *out, const char *slug)
|
||||
{
|
||||
char name[MAX_SLUG] = "", desc[512] = "";
|
||||
char ids[256][16];
|
||||
int np;
|
||||
|
||||
kb_meta(slug, name, sizeof(name), desc, sizeof(desc));
|
||||
|
||||
html_head(out, name[0] ? name : slug);
|
||||
|
||||
cgi_buf_printf(out, "<h1>%s</h1>\n", name[0] ? name : slug);
|
||||
if (desc[0]) {
|
||||
cgi_buf_printf(out, "<p class=\"meta\">");
|
||||
html_escape(out, desc);
|
||||
cgi_buf_puts(out, "</p>\n");
|
||||
}
|
||||
|
||||
np = list_entry_ids(slug, ids, 256);
|
||||
if (np <= 0) {
|
||||
cgi_buf_puts(out, "<p>No pages yet.</p>\n");
|
||||
} else {
|
||||
cgi_buf_printf(out, "<p>%d page(s)</p>\n", np);
|
||||
for (int i = 0; i < np; i++) {
|
||||
struct entry e;
|
||||
if (entry_load(slug, ids[i], &e) != 0)
|
||||
continue;
|
||||
|
||||
cgi_buf_printf(out, "<h3><a href=\"/wikid/%s/%s\">", slug, ids[i]);
|
||||
html_escape(out, e.title);
|
||||
cgi_buf_puts(out, "</a></h3>\n");
|
||||
|
||||
cgi_buf_printf(out, "<p class=\"meta\">%s ", e.id);
|
||||
if (e.tags && *e.tags) {
|
||||
cgi_buf_puts(out, "tags: ");
|
||||
html_escape(out, e.tags);
|
||||
cgi_buf_puts(out, " ");
|
||||
}
|
||||
if (e.dim > 0)
|
||||
cgi_buf_puts(out, "embedded ");
|
||||
cgi_buf_printf(out, "updated: %ld</p>\n", e.updated);
|
||||
|
||||
/* First 300 chars of text */
|
||||
cgi_buf_puts(out, "<p>");
|
||||
{
|
||||
const char *p = e.text;
|
||||
int len = 0;
|
||||
while (*p && *p != '\n' && len < 300) {
|
||||
if (*p == '*' && p[1] == '*') { p += 2; continue; }
|
||||
if (*p == '*') { p++; continue; }
|
||||
if (*p == '`') { p++; continue; }
|
||||
cgi_buf_add(out, p, 1);
|
||||
p++;
|
||||
len++;
|
||||
}
|
||||
}
|
||||
cgi_buf_puts(out, "…</p>\n");
|
||||
free_entry(&e);
|
||||
}
|
||||
}
|
||||
|
||||
cgi_buf_puts(out, "<div class=\"nav\">\n");
|
||||
cgi_buf_puts(out, " <a href=\"/wikid/\">All wikis</a>\n");
|
||||
cgi_buf_puts(out, "</div>\n");
|
||||
|
||||
html_foot(out);
|
||||
}
|
||||
+1374
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user