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.
1375 lines
34 KiB
C
1375 lines
34 KiB
C
/*
|
|
* wikid-http — HTTP front-end for wikid, via wikid's MCP server.
|
|
*
|
|
* A C99 CGI binary. It speaks JSON-RPC over HTTP to wikid (POST /mcp)
|
|
* instead of reading the data files itself: every list, search, read and
|
|
* write goes back to wikid, so the web view and the agents see the same
|
|
* wiki. No auth, no JavaScript, no state — plain HTML 4.01.
|
|
*
|
|
* Routes:
|
|
* / — all wikis (wiki_list)
|
|
* /wikid/ — same page
|
|
* /wikid/<slug>/ — pages of one wiki (page_list)
|
|
* /wikid/<slug>/<id> — read one page (page_read)
|
|
* /wikid/search?q=… — search (wiki_search)
|
|
* GET /wikid/<slug>/write — write form
|
|
* POST /wikid/<slug>/write — write a page (page_write)
|
|
* GET /wikid/create — create form
|
|
* POST /wikid/create — create a wiki (wiki_create)
|
|
*
|
|
* Environment: WIKID_URL (default http://127.0.0.1:8796/mcp)
|
|
*
|
|
* Build:
|
|
* cc -std=c99 -O2 -Wall -Wextra -Wpedantic -D_POSIX_C_SOURCE=200809L \
|
|
* -D_DEFAULT_SOURCE -o wikid-http wikid-http.c
|
|
*/
|
|
#define _POSIX_C_SOURCE 200809L
|
|
|
|
#include <ctype.h>
|
|
#include <stdarg.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#define MAX_LINE 4096
|
|
#define MAX_BODY 65536
|
|
|
|
/* Path under which the CGI is mounted: "" when it is the root, otherwise
|
|
* SCRIPT_NAME (e.g. "/cgi-bin/wikid-http"). Every internal link is emitted
|
|
* relative to this, so the same binary works at / and under /cgi-bin/. */
|
|
static char g_prefix[256];
|
|
|
|
static void url(const char *suffix)
|
|
{
|
|
fputs(g_prefix, stdout);
|
|
fputs(suffix, stdout);
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ buffer */
|
|
|
|
static char *buf_add(char *p, size_t *len, size_t *cap, const char *s, size_t n)
|
|
{
|
|
size_t newcap = *cap ? *cap : 256;
|
|
char *q;
|
|
|
|
if (n == 0)
|
|
return p;
|
|
while (newcap < *len + n + 1)
|
|
newcap *= 2;
|
|
q = realloc(p, newcap);
|
|
if (q == NULL) {
|
|
free(p);
|
|
return NULL;
|
|
}
|
|
memcpy(q + *len, s, n);
|
|
*len += n;
|
|
q[*len] = '\0';
|
|
*cap = newcap;
|
|
return q;
|
|
}
|
|
|
|
static char *buf_puts(char *p, size_t *len, size_t *cap, const char *s)
|
|
{
|
|
return buf_add(p, len, cap, s, strlen(s));
|
|
}
|
|
|
|
static char *buf_printf(char *p, size_t *len, size_t *cap, 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 p;
|
|
return buf_add(p, len, cap, tmp, (size_t)n);
|
|
}
|
|
|
|
/* ----------------------------------------------------------------- escaping */
|
|
|
|
static void html_esc(const char *s)
|
|
{
|
|
for (; s != NULL && *s; s++) {
|
|
switch (*s) {
|
|
case '&': fputs("&", stdout); break;
|
|
case '<': fputs("<", stdout); break;
|
|
case '>': fputs(">", stdout); break;
|
|
case '"': fputs(""", stdout); break;
|
|
default: putchar(*s); break;
|
|
}
|
|
}
|
|
}
|
|
|
|
/* Escape exactly n bytes (a line inside a larger buffer). */
|
|
static void html_esc_n(const char *s, size_t n)
|
|
{
|
|
size_t i;
|
|
|
|
for (i = 0; i < n; i++) {
|
|
switch (s[i]) {
|
|
case '&': fputs("&", stdout); break;
|
|
case '<': fputs("<", stdout); break;
|
|
case '>': fputs(">", stdout); break;
|
|
case '"': fputs(""", stdout); break;
|
|
default: putchar(s[i]); break;
|
|
}
|
|
}
|
|
}
|
|
|
|
static char *url_decode(char *s)
|
|
{
|
|
char *r = s, *w = s;
|
|
|
|
for (; *r; r++, w++) {
|
|
if (*r == '+') {
|
|
*w = ' ';
|
|
} else if (*r == '%' && isxdigit((unsigned char)r[1]) &&
|
|
isxdigit((unsigned char)r[2])) {
|
|
char hex[3] = { r[1], r[2], 0 };
|
|
|
|
*w = (char)strtol(hex, NULL, 16);
|
|
r += 2;
|
|
} else {
|
|
*w = *r;
|
|
}
|
|
}
|
|
*w = '\0';
|
|
return s;
|
|
}
|
|
|
|
/* Append a JSON-escaped copy of s. */
|
|
static char *buf_json(char *p, size_t *len, size_t *cap, const char *s)
|
|
{
|
|
for (; s != NULL && *s; s++) {
|
|
switch (*s) {
|
|
case '"': p = buf_puts(p, len, cap, "\\\""); break;
|
|
case '\\': p = buf_puts(p, len, cap, "\\\\"); break;
|
|
case '\n': p = buf_puts(p, len, cap, "\\n"); break;
|
|
case '\r': p = buf_puts(p, len, cap, "\\r"); break;
|
|
case '\t': p = buf_puts(p, len, cap, "\\t"); break;
|
|
default:
|
|
if ((unsigned char)*s < 0x20)
|
|
p = buf_printf(p, len, cap, "\\u%04x", (unsigned char)*s);
|
|
else
|
|
p = buf_add(p, len, cap, s, 1);
|
|
break;
|
|
}
|
|
if (p == NULL)
|
|
return NULL;
|
|
}
|
|
return p;
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ MCP RPC */
|
|
|
|
/* Extract a JSON string value for "text": "…" from a response body.
|
|
* Handles the escapes wikid can produce (\" \\ \n \r \t \u). */
|
|
static char *json_text_value(const char *json)
|
|
{
|
|
const char *pat = "\"text\":\"";
|
|
const char *p = strstr(json, pat);
|
|
char *out;
|
|
size_t len = 0, cap = 0;
|
|
|
|
if (p == NULL)
|
|
return NULL;
|
|
p += strlen(pat);
|
|
out = NULL;
|
|
while (*p != '\0' && *p != '"') {
|
|
char ch = *p;
|
|
|
|
if (ch == '\\') {
|
|
p++;
|
|
switch (*p) {
|
|
case 'n': ch = '\n'; p++; break;
|
|
case 'r': ch = '\r'; p++; break;
|
|
case 't': ch = '\t'; p++; break;
|
|
case 'u':
|
|
/* Wikid does not emit \u except control chars;
|
|
* decode the four hex digits. */
|
|
if (isxdigit((unsigned char)p[1]) &&
|
|
isxdigit((unsigned char)p[2]) &&
|
|
isxdigit((unsigned char)p[3]) &&
|
|
isxdigit((unsigned char)p[4])) {
|
|
char hex[5] = { p[1], p[2], p[3], p[4], 0 };
|
|
ch = (char)strtol(hex, NULL, 16);
|
|
p += 5;
|
|
} else {
|
|
p++;
|
|
}
|
|
break;
|
|
default: ch = *p; p++; break;
|
|
}
|
|
} else {
|
|
p++;
|
|
}
|
|
out = buf_add(out, &len, &cap, &ch, 1);
|
|
if (out == NULL)
|
|
return NULL;
|
|
}
|
|
if (out == NULL)
|
|
out = buf_add(NULL, &len, &cap, "", 0);
|
|
return out;
|
|
}
|
|
|
|
/* Extract an error message if the response is an error. */
|
|
static char *json_error_value(const char *json)
|
|
{
|
|
const char *p = strstr(json, "\"message\":\"");
|
|
|
|
if (p == NULL)
|
|
return NULL;
|
|
p += 11;
|
|
{
|
|
const char *end = strchr(p, '"');
|
|
char *msg;
|
|
|
|
if (end == NULL)
|
|
return NULL;
|
|
msg = malloc((size_t)(end - p) + 1);
|
|
if (msg == NULL)
|
|
return NULL;
|
|
memcpy(msg, p, (size_t)(end - p));
|
|
msg[end - p] = '\0';
|
|
return msg;
|
|
}
|
|
}
|
|
|
|
static char *wikid_call(const char *tool, const char *args)
|
|
{
|
|
char *body = NULL;
|
|
size_t blen = 0, bcap = 0;
|
|
const char *url;
|
|
char cmd[8192];
|
|
FILE *fp;
|
|
char chunk[2048];
|
|
size_t got;
|
|
char *raw = NULL;
|
|
size_t rlen = 0, rcap = 0;
|
|
char *text;
|
|
|
|
body = buf_printf(body, &blen, &bcap,
|
|
"{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"tools/call\","
|
|
"\"params\":{\"name\":\"%s\",\"arguments\":%s}}", tool, args);
|
|
if (body == NULL)
|
|
return NULL;
|
|
|
|
url = getenv("WIKID_URL");
|
|
if (url == NULL || *url == '\0')
|
|
url = "http://127.0.0.1:8796/mcp";
|
|
|
|
/* Single-quote the body: it contains only JSON double quotes and
|
|
* escaped backslashes; the only dangerous character is a single
|
|
* quote, which JSON bodies here cannot contain (titles/texts are
|
|
* escaped to \u0027 by json_esc? no — single quotes pass through).
|
|
* Escape them. */
|
|
{
|
|
char *qbody = NULL;
|
|
size_t qlen = 0, qcap = 0;
|
|
|
|
for (const char *p = body; *p; p++) {
|
|
if (*p == '\'') {
|
|
qbody = buf_puts(qbody, &qlen, &qcap, "'\\''");
|
|
if (qbody == NULL)
|
|
break;
|
|
} else {
|
|
qbody = buf_add(qbody, &qlen, &qcap, p, 1);
|
|
if (qbody == NULL)
|
|
break;
|
|
}
|
|
}
|
|
if (qbody == NULL && *body != '\0') {
|
|
free(body);
|
|
return NULL;
|
|
}
|
|
snprintf(cmd, sizeof(cmd),
|
|
"curl -sS -m 30 -X POST -H 'Content-Type: application/json' "
|
|
"-d '%s' '%s' 2>/dev/null", qbody != NULL ? qbody : "", url);
|
|
free(qbody);
|
|
}
|
|
free(body);
|
|
|
|
fp = popen(cmd, "r");
|
|
if (fp == NULL)
|
|
return NULL;
|
|
while ((got = fread(chunk, 1, sizeof(chunk), fp)) > 0) {
|
|
raw = buf_add(raw, &rlen, &rcap, chunk, got);
|
|
if (raw == NULL)
|
|
break;
|
|
}
|
|
pclose(fp);
|
|
if (raw == NULL)
|
|
return NULL;
|
|
|
|
text = json_text_value(raw);
|
|
if (text == NULL)
|
|
text = json_error_value(raw);
|
|
free(raw);
|
|
return text;
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ markdown */
|
|
|
|
static void md_inline(const char **pp)
|
|
{
|
|
const char *p = *pp;
|
|
|
|
while (*p && *p != '\n') {
|
|
if (*p == '*' && p[1] == '*') {
|
|
fputs("<b>", stdout);
|
|
p += 2;
|
|
while (*p && !(*p == '*' && p[1] == '*') && *p != '\n')
|
|
putchar(*p++);
|
|
if (*p == '*')
|
|
p++;
|
|
fputs("</b>", stdout);
|
|
} else if (*p == '*') {
|
|
fputs("<i>", stdout);
|
|
p++;
|
|
while (*p && *p != '*' && *p != '\n')
|
|
putchar(*p++);
|
|
if (*p == '*')
|
|
p++;
|
|
fputs("</i>", stdout);
|
|
} else if (*p == '`') {
|
|
fputs("<code>", stdout);
|
|
p++;
|
|
while (*p && *p != '`' && *p != '\n')
|
|
putchar(*p++);
|
|
if (*p == '`')
|
|
p++;
|
|
fputs("</code>", stdout);
|
|
} else if (*p == '&') {
|
|
fputs("&", stdout);
|
|
p++;
|
|
} else if (*p == '<') {
|
|
fputs("<", stdout);
|
|
p++;
|
|
} else if (*p == '>') {
|
|
fputs(">", stdout);
|
|
p++;
|
|
} else {
|
|
putchar(*p++);
|
|
}
|
|
}
|
|
*pp = p;
|
|
}
|
|
|
|
static void md_render(const char *text)
|
|
{
|
|
const char *p = text;
|
|
int in_para = 0, in_list = 0;
|
|
|
|
while (*p) {
|
|
if (*p == '#') {
|
|
int level = 0;
|
|
|
|
while (*p == '#') {
|
|
level++;
|
|
p++;
|
|
}
|
|
while (*p == ' ')
|
|
p++;
|
|
if (in_para) { fputs("</p>\n", stdout); in_para = 0; }
|
|
if (in_list) { fputs("</ul>\n", stdout); in_list = 0; }
|
|
if (level > 6)
|
|
level = 6;
|
|
printf("<h%d>", level);
|
|
md_inline(&p);
|
|
printf("</h%d>\n", level);
|
|
continue;
|
|
}
|
|
if (*p == '-' && (p[1] == ' ' || p[1] == '\t')) {
|
|
if (!in_list) { fputs("<ul>\n", stdout); in_list = 1; }
|
|
fputs("<li>", stdout);
|
|
p += 2;
|
|
md_inline(&p);
|
|
fputs("</li>\n", stdout);
|
|
continue;
|
|
}
|
|
if (in_list) { fputs("</ul>\n", stdout); in_list = 0; }
|
|
if (strncmp(p, "---", 3) == 0 && (p[3] == '\n' || p[3] == '\0')) {
|
|
if (in_para) { fputs("</p>\n", stdout); in_para = 0; }
|
|
fputs("<hr />\n", stdout);
|
|
p += 3;
|
|
while (*p == '\n')
|
|
p++;
|
|
continue;
|
|
}
|
|
if (*p == '\n') {
|
|
if (in_para) { fputs("</p>\n", stdout); in_para = 0; }
|
|
p++;
|
|
continue;
|
|
}
|
|
if (!in_para) { fputs("<p>", stdout); in_para = 1; }
|
|
md_inline(&p);
|
|
if (*p == '\n') {
|
|
if (p[1] != '\n' && p[1] != '\0')
|
|
fputs("<br>\n", stdout);
|
|
p++;
|
|
}
|
|
}
|
|
if (in_para)
|
|
fputs("</p>\n", stdout);
|
|
if (in_list)
|
|
fputs("</ul>\n", stdout);
|
|
}
|
|
|
|
/* ----------------------------------------------------------------- HTML 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"
|
|
".error{color:#a00}\n"
|
|
".listing{list-style:none;padding-left:0}\n"
|
|
".listing li{margin:0.6em 0}\n"
|
|
".snippet{color:#555;font-size:0.9em;margin-left:1.5em}\n";
|
|
|
|
static void html_head(const char *title)
|
|
{
|
|
puts("<!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"
|
|
"<title>");
|
|
html_esc(title);
|
|
puts("</title>\n"
|
|
"<style type=\"text/css\">");
|
|
fputs(css, stdout);
|
|
fputs("</style>\n"
|
|
"</head>\n"
|
|
"<body>\n"
|
|
"<div class=\"nav\"><a href=\"", stdout);
|
|
url("/");
|
|
fputs("\">Wikis</a><a href=\"", stdout);
|
|
url("/wikid/search");
|
|
fputs("\">Search</a><a href=\"", stdout);
|
|
url("/wikid/create");
|
|
fputs("\">New wiki</a></div>\n", stdout);
|
|
}
|
|
|
|
static void html_foot(void)
|
|
{
|
|
puts("</body>\n</html>\n");
|
|
}
|
|
|
|
static void error_page(const char *title, const char *msg)
|
|
{
|
|
html_head(title);
|
|
printf("<h1>");
|
|
html_esc(title);
|
|
fputs("</h1>\n<p class=\"error\">", stdout);
|
|
html_esc(msg != NULL ? msg : "unknown error");
|
|
puts("</p>");
|
|
html_foot();
|
|
}
|
|
|
|
/* ------------------------------------------------------------------- forms */
|
|
|
|
/* Copy a URL-encoded parameter out of qs (malloc'd, caller frees). */
|
|
static char *param_get(const char *qs, const char *name)
|
|
{
|
|
size_t nlen = strlen(name);
|
|
|
|
for (const char *p = qs; p != NULL && *p; ) {
|
|
if (strncmp(p, name, nlen) == 0 && p[nlen] == '=') {
|
|
const char *val = p + nlen + 1;
|
|
const char *end = strchr(val, '&');
|
|
size_t vlen = end != NULL ? (size_t)(end - val) : strlen(val);
|
|
char *out = malloc(vlen + 1);
|
|
|
|
if (out == NULL)
|
|
return NULL;
|
|
memcpy(out, val, vlen);
|
|
out[vlen] = '\0';
|
|
url_decode(out);
|
|
return out;
|
|
}
|
|
p = strchr(p, '&');
|
|
if (p != NULL)
|
|
p++;
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
static void write_form(const char *slug, const char *title,
|
|
const char *text, const char *tags, const char *source)
|
|
{
|
|
printf("<h1>Write into %s</h1>\n", slug);
|
|
fputs("<form method=\"post\" action=\"", stdout);
|
|
url("/wikid/");
|
|
html_esc(slug);
|
|
fputs("/write\">\n", stdout);
|
|
fputs("<p><label>Title<br><input type=\"text\" name=\"title\" size=\"60\" value=\"", stdout);
|
|
html_esc(title);
|
|
fputs("\"></label></p>\n<p><label>Text<br><textarea name=\"text\" rows=\"18\" cols=\"72\">", stdout);
|
|
html_esc(text);
|
|
fputs("</textarea></label></p>\n<p><label>Tags (comma separated)<br>"
|
|
"<input type=\"text\" name=\"tags\" size=\"40\" value=\"", stdout);
|
|
html_esc(tags);
|
|
fputs("\"></label></p>\n<p><label>Source<br>"
|
|
"<input type=\"text\" name=\"source\" size=\"40\" value=\"", stdout);
|
|
html_esc(source);
|
|
fputs("\"></label></p>\n"
|
|
"<p><input type=\"submit\" value=\"Write page\"></p>\n"
|
|
"</form>\n", stdout);
|
|
fputs("<div class=\"nav\"><a href=\"", stdout);
|
|
url("/wikid/");
|
|
html_esc(slug);
|
|
fputs("/\">Back to the wiki</a></div>\n", stdout);
|
|
}
|
|
|
|
static void create_form(const char *name, const char *desc)
|
|
{
|
|
fputs("<h1>Create a wiki</h1>\n<form method=\"post\" action=\"", stdout);
|
|
url("/wikid/create");
|
|
fputs("\">\n<p><label>Name<br>"
|
|
"<input type=\"text\" name=\"name\" size=\"40\" value=\"", stdout);
|
|
html_esc(name);
|
|
fputs("\"></label></p>\n"
|
|
"<p><label>Description<br><textarea name=\"description\" rows=\"3\" cols=\"50\">", stdout);
|
|
html_esc(desc);
|
|
fputs("</textarea></label></p>\n"
|
|
"<p><input type=\"submit\" value=\"Create wiki\"></p>\n"
|
|
"</form>\n<div class=\"nav\"><a href=\"", stdout);
|
|
url("/");
|
|
fputs("\">Back</a></div>\n", stdout);
|
|
}
|
|
|
|
/* --------------------------------------------------------------- list render */
|
|
|
|
/*
|
|
* wiki_list output:
|
|
* %d knowledge base(s) in %s (history: …)
|
|
* <slug> <name> <n> entries — <desc>
|
|
* The line is fixed-width: slug 24, name 20, then "%4zu entries".
|
|
* Rather than trusting the widths, take the first token as the slug and
|
|
* print the rest verbatim.
|
|
*/
|
|
static void render_wikis(const char *text)
|
|
{
|
|
const char *p = text;
|
|
int first = 1;
|
|
|
|
while (*p) {
|
|
const char *eol = strchr(p, '\n');
|
|
size_t len = eol != NULL ? (size_t)(eol - p) : strlen(p);
|
|
const char *line_end = p + len;
|
|
|
|
if (len > 2 && p[0] == ' ' && p[1] == ' ') {
|
|
const char *tok = p + 2;
|
|
const char *tok_end = tok;
|
|
char slug[128];
|
|
size_t slen;
|
|
|
|
while (tok_end < line_end && *tok_end != ' ' && *tok_end != '\t')
|
|
tok_end++;
|
|
slen = (size_t)(tok_end - tok);
|
|
if (slen >= sizeof(slug))
|
|
slen = sizeof(slug) - 1;
|
|
memcpy(slug, tok, slen);
|
|
slug[slen] = '\0';
|
|
|
|
fputs("<li><a href=\"", stdout);
|
|
url("/wikid/");
|
|
html_esc(slug);
|
|
fputs("/\"><b>", stdout);
|
|
html_esc(slug);
|
|
fputs("</b></a>", stdout);
|
|
if (tok_end < line_end) {
|
|
fputs(" · ", stdout);
|
|
{
|
|
char rest[1024];
|
|
size_t rlen = (size_t)(line_end - tok_end);
|
|
|
|
if (rlen >= sizeof(rest))
|
|
rlen = sizeof(rest) - 1;
|
|
memcpy(rest, tok_end, rlen);
|
|
rest[rlen] = '\0';
|
|
{
|
|
char *rs = rest;
|
|
|
|
while (*rs == ' ' || *rs == '\t')
|
|
rs++;
|
|
html_esc(rs);
|
|
}
|
|
}
|
|
}
|
|
fputs("</li>\n", stdout);
|
|
} else {
|
|
if (first) {
|
|
fputs("<p class=\"meta\">", stdout);
|
|
html_esc_n(p, len);
|
|
fputs("</p>\n<ul class=\"listing\">\n", stdout);
|
|
} else {
|
|
error_page("Wiki engine", "unexpected output");
|
|
return;
|
|
}
|
|
}
|
|
first = 0;
|
|
if (eol == NULL)
|
|
break;
|
|
p = eol + 1;
|
|
}
|
|
if (!first)
|
|
fputs("</ul>\n", stdout);
|
|
}
|
|
|
|
/* page_list output:
|
|
* %zu entries in "%s" (newest first):
|
|
* <id> <title> tags=… (embedded, updated N)
|
|
*/
|
|
static void render_pages(const char *text, const char *slug)
|
|
{
|
|
const char *p = text;
|
|
int first = 1;
|
|
|
|
while (*p) {
|
|
const char *eol = strchr(p, '\n');
|
|
size_t len = eol != NULL ? (size_t)(eol - p) : strlen(p);
|
|
const char *line_end = p + len;
|
|
|
|
if (len > 2 && p[0] == ' ' && p[1] == ' ') {
|
|
const char *tok = p + 2;
|
|
const char *tok_end = tok;
|
|
char id[64];
|
|
size_t tlen;
|
|
|
|
while (tok_end < line_end && *tok_end != ' ' && *tok_end != '\t')
|
|
tok_end++;
|
|
tlen = (size_t)(tok_end - tok);
|
|
if (tlen >= sizeof(id))
|
|
tlen = sizeof(id) - 1;
|
|
memcpy(id, tok, tlen);
|
|
id[tlen] = '\0';
|
|
|
|
fputs("<li><a href=\"", stdout);
|
|
url("/wikid/");
|
|
html_esc(slug);
|
|
fputs("/", stdout);
|
|
html_esc(id);
|
|
fputs("\"><b>", stdout);
|
|
html_esc(id);
|
|
fputs("</b></a>", stdout);
|
|
if (tok_end < line_end) {
|
|
char rest[1024];
|
|
size_t rlen = (size_t)(line_end - tok_end);
|
|
|
|
if (rlen >= sizeof(rest))
|
|
rlen = sizeof(rest) - 1;
|
|
memcpy(rest, tok_end, rlen);
|
|
rest[rlen] = '\0';
|
|
fputs(" · ", stdout);
|
|
{
|
|
char *rs = rest;
|
|
|
|
while (*rs == ' ' || *rs == '\t')
|
|
rs++;
|
|
html_esc(rs);
|
|
}
|
|
}
|
|
fputs("</li>\n", stdout);
|
|
} else {
|
|
if (first) {
|
|
fputs("<p class=\"meta\">", stdout);
|
|
html_esc_n(p, len);
|
|
fputs("</p>\n<ul class=\"listing\">\n", stdout);
|
|
} else {
|
|
fputs("<p>", stdout);
|
|
html_esc_n(p, len);
|
|
fputs("</p>\n", stdout);
|
|
}
|
|
}
|
|
first = 0;
|
|
if (eol == NULL)
|
|
break;
|
|
p = eol + 1;
|
|
}
|
|
if (!first)
|
|
fputs("</ul>\n", stdout);
|
|
}
|
|
|
|
/* wiki_search output:
|
|
* %zu hit(s) for "…":
|
|
* <slug>/<id> [title] score=… tags=…
|
|
* <snippet>
|
|
*/
|
|
static void render_search(const char *text)
|
|
{
|
|
const char *p = text;
|
|
int first = 1, opened = 0;
|
|
|
|
while (*p) {
|
|
const char *eol = strchr(p, '\n');
|
|
size_t len = eol != NULL ? (size_t)(eol - p) : strlen(p);
|
|
const char *line_end = p + len;
|
|
|
|
if (len > 4 && p[0] == ' ' && p[1] == ' ' && p[2] == ' ' && p[3] == ' ') {
|
|
const char *sp = p;
|
|
|
|
while (sp < line_end && (*sp == ' ' || *sp == '\t'))
|
|
sp++;
|
|
fputs("<li class=\"snippet\">", stdout);
|
|
{
|
|
const char *mp = sp;
|
|
|
|
md_inline(&mp);
|
|
}
|
|
fputs("</li>\n", stdout);
|
|
} else if (!first && strchr(p, '/') != NULL &&
|
|
(size_t)(strchr(p, ' ') - p) < len) {
|
|
/* hit line: slug/id [title] score=… tags=… */
|
|
const char *tok_end = p;
|
|
char ref[512];
|
|
size_t rlen;
|
|
|
|
while (tok_end < line_end && *tok_end != ' ' && *tok_end != '\t')
|
|
tok_end++;
|
|
rlen = (size_t)(tok_end - p);
|
|
if (rlen >= sizeof(ref))
|
|
rlen = sizeof(ref) - 1;
|
|
memcpy(ref, p, rlen);
|
|
ref[rlen] = '\0';
|
|
{
|
|
char *slash = strchr(ref, '/');
|
|
|
|
if (slash != NULL) {
|
|
char wiki[256];
|
|
size_t wlen = (size_t)(slash - ref);
|
|
const char *title = tok_end;
|
|
const char *title_end = NULL;
|
|
const char *after = tok_end;
|
|
|
|
/* rest begins with " [title] ..." — lift the title
|
|
* out of the brackets and use it as link text. */
|
|
while (title < line_end && *title == ' ')
|
|
title++;
|
|
if (title < line_end && *title == '[') {
|
|
title_end = memchr(title, ']',
|
|
(size_t)(line_end - title));
|
|
if (title_end != NULL)
|
|
after = title_end + 1;
|
|
}
|
|
|
|
if (wlen >= sizeof(wiki))
|
|
wlen = sizeof(wiki) - 1;
|
|
memcpy(wiki, ref, wlen);
|
|
wiki[wlen] = '\0';
|
|
fputs("<li><a href=\"", stdout);
|
|
url("/wikid/");
|
|
html_esc(wiki);
|
|
fputs("/", stdout);
|
|
html_esc(slash + 1);
|
|
fputs("\"><b>", stdout);
|
|
if (title_end != NULL)
|
|
html_esc_n(title + 1,
|
|
(size_t)(title_end - title - 1));
|
|
else
|
|
html_esc(ref);
|
|
fputs("</b></a>", stdout);
|
|
if (after < line_end) {
|
|
fputs(" · ", stdout);
|
|
html_esc_n(after, (size_t)(line_end - after));
|
|
}
|
|
fputs("</li>\n", stdout);
|
|
}
|
|
}
|
|
} else if (first) {
|
|
fputs("<p class=\"meta\">", stdout);
|
|
html_esc_n(p, len);
|
|
fputs("</p>\n", stdout);
|
|
if (len > 0 && p[len - 1] == ':') {
|
|
fputs("<ul class=\"listing\">\n", stdout);
|
|
opened = 1;
|
|
}
|
|
} else {
|
|
fputs("<p>", stdout);
|
|
html_esc_n(p, len);
|
|
fputs("</p>\n", stdout);
|
|
}
|
|
first = 0;
|
|
if (eol == NULL)
|
|
break;
|
|
p = eol + 1;
|
|
}
|
|
if (opened)
|
|
fputs("</ul>\n", stdout);
|
|
}
|
|
|
|
/* ------------------------------------------------------------------- routes */
|
|
|
|
static void route_index(void)
|
|
{
|
|
char *text = wikid_call("wiki_list", "{}");
|
|
|
|
if (text == NULL) {
|
|
error_page("Wikis", "The wiki engine did not answer.");
|
|
return;
|
|
}
|
|
html_head("Wikis");
|
|
fputs("<h1>Wikis</h1>\n", stdout);
|
|
render_wikis(text);
|
|
free(text);
|
|
html_foot();
|
|
}
|
|
|
|
static void route_wiki_index(const char *slug)
|
|
{
|
|
char *args = NULL;
|
|
size_t alen = 0, acap = 0;
|
|
char *text;
|
|
|
|
args = buf_puts(args, &alen, &acap, "{\"kb\":\"");
|
|
args = buf_json(args, &alen, &acap, slug);
|
|
args = buf_puts(args, &alen, &acap, "\"}");
|
|
if (args == NULL) {
|
|
error_page("Wikis", "out of memory");
|
|
return;
|
|
}
|
|
text = wikid_call("page_list", args);
|
|
free(args);
|
|
if (text == NULL) {
|
|
error_page("Wiki", "The wiki engine did not answer.");
|
|
return;
|
|
}
|
|
if (strncmp(text, "no knowledge base", 17) == 0) {
|
|
html_head("No such wiki");
|
|
fputs("<h1>No such wiki</h1>\n<p>", stdout);
|
|
html_esc(text);
|
|
fputs("</p>\n<div class=\"nav\"><a href=\"", stdout);
|
|
url("/");
|
|
fputs("\">All wikis</a></div>\n", stdout);
|
|
html_foot();
|
|
free(text);
|
|
return;
|
|
}
|
|
html_head(slug);
|
|
fputs("<h1>", stdout);
|
|
html_esc(slug);
|
|
fputs("</h1>\n", stdout);
|
|
render_pages(text, slug);
|
|
fputs("<div class=\"nav\"><a href=\"", stdout);
|
|
url("/wikid/");
|
|
html_esc(slug);
|
|
fputs("/write\">Write a page</a><a href=\"", stdout);
|
|
url("/wikid/search?q=");
|
|
fputs("\">Search</a></div>\n", stdout);
|
|
free(text);
|
|
html_foot();
|
|
}
|
|
|
|
static int looks_like_id(const char *s)
|
|
{
|
|
if (s == NULL || s[0] != 'e' || s[1] == '\0')
|
|
return 0;
|
|
for (s++; *s; s++)
|
|
if (!isdigit((unsigned char)*s))
|
|
return 0;
|
|
return 1;
|
|
}
|
|
|
|
/* POST /wikid/<slug>/delete — page_delete */
|
|
static void route_delete(const char *slug, const char *body)
|
|
{
|
|
char *id = param_get(body, "id");
|
|
char *title = param_get(body, "title");
|
|
char *args = NULL;
|
|
size_t alen = 0, acap = 0;
|
|
char *result;
|
|
|
|
if ((id == NULL || *id == '\0') && (title == NULL || *title == '\0')) {
|
|
error_page("Delete page", "No page named.");
|
|
free(id);
|
|
free(title);
|
|
return;
|
|
}
|
|
args = buf_puts(args, &alen, &acap, "{\"kb\":\"");
|
|
args = buf_json(args, &alen, &acap, slug);
|
|
if (id != NULL && *id != '\0') {
|
|
args = buf_puts(args, &alen, &acap, "\",\"id\":\"");
|
|
args = buf_json(args, &alen, &acap, id);
|
|
} else {
|
|
args = buf_puts(args, &alen, &acap, "\",\"title\":\"");
|
|
args = buf_json(args, &alen, &acap, title);
|
|
}
|
|
args = buf_puts(args, &alen, &acap, "\"}");
|
|
if (args == NULL) {
|
|
error_page("Delete page", "out of memory");
|
|
free(id);
|
|
free(title);
|
|
return;
|
|
}
|
|
result = wikid_call("page_delete", args);
|
|
free(args);
|
|
if (result == NULL) {
|
|
error_page("Delete page", "The wiki engine did not answer.");
|
|
} else {
|
|
html_head("Page deleted");
|
|
fputs("<p>", stdout);
|
|
html_esc(result);
|
|
fputs("</p>\n<div class=\"nav\"><a href=\"", stdout);
|
|
url("/wikid/");
|
|
html_esc(slug);
|
|
fputs("/\">Back to ", stdout);
|
|
html_esc(slug);
|
|
fputs("</a></div>\n", stdout);
|
|
html_foot();
|
|
free(result);
|
|
}
|
|
free(id);
|
|
free(title);
|
|
}
|
|
|
|
static void route_page(const char *slug, const char *page)
|
|
{
|
|
char *args = NULL;
|
|
size_t alen = 0, acap = 0;
|
|
char *text;
|
|
int not_found = 0;
|
|
|
|
args = buf_puts(args, &alen, &acap, "{\"kb\":\"");
|
|
args = buf_json(args, &alen, &acap, slug);
|
|
args = buf_puts(args, &alen, &acap, "\",\"id\":\"");
|
|
args = buf_json(args, &alen, &acap, page);
|
|
args = buf_puts(args, &alen, &acap, "\",\"history\":true}");
|
|
if (args == NULL) {
|
|
error_page("Page", "out of memory");
|
|
return;
|
|
}
|
|
text = wikid_call("page_read", args);
|
|
free(args);
|
|
/* A failed lookup is a text reply ("no page ..."), not a JSON error. */
|
|
if (text != NULL && strncmp(text, "no page", 7) == 0) {
|
|
free(text);
|
|
text = NULL;
|
|
}
|
|
|
|
if (text == NULL) {
|
|
alen = acap = 0;
|
|
args = NULL;
|
|
args = buf_puts(args, &alen, &acap, "{\"kb\":\"");
|
|
args = buf_json(args, &alen, &acap, slug);
|
|
args = buf_puts(args, &alen, &acap, "\",\"title\":\"");
|
|
args = buf_json(args, &alen, &acap, page);
|
|
args = buf_puts(args, &alen, &acap, "\",\"history\":true}");
|
|
if (args == NULL) {
|
|
error_page("Page", "out of memory");
|
|
return;
|
|
}
|
|
text = wikid_call("page_read", args);
|
|
free(args);
|
|
if (text != NULL && strncmp(text, "no page", 7) == 0) {
|
|
free(text);
|
|
text = NULL;
|
|
}
|
|
}
|
|
|
|
if (text == NULL) {
|
|
not_found = 1;
|
|
html_head("Page not found");
|
|
fputs("<h1>Page not found</h1>\n<p>", stdout);
|
|
html_esc(page);
|
|
fputs(" is not in ", stdout);
|
|
html_esc(slug);
|
|
fputs(".</p>\n", stdout);
|
|
fputs("<div class=\"nav\"><a href=\"", stdout);
|
|
url("/wikid/");
|
|
html_esc(slug);
|
|
fputs("/\">Back</a><a href=\"", stdout);
|
|
url("/wikid/");
|
|
html_esc(slug);
|
|
fputs("/write\">Write a page</a></div>\n", stdout);
|
|
html_foot();
|
|
}
|
|
(void)not_found;
|
|
if (text == NULL)
|
|
return;
|
|
html_head(page);
|
|
md_render(text);
|
|
fputs("<div class=\"nav\"><a href=\"", stdout);
|
|
url("/wikid/");
|
|
html_esc(slug);
|
|
fputs("/\">Back to ", stdout);
|
|
html_esc(slug);
|
|
fputs("</a><a href=\"", stdout);
|
|
url("/wikid/");
|
|
html_esc(slug);
|
|
fputs("/write\">Write</a></div>\n", stdout);
|
|
|
|
/* Delete needs a POST, so no link prefetcher can remove a page. */
|
|
fputs("<form method=\"post\" action=\"", stdout);
|
|
url("/wikid/");
|
|
html_esc(slug);
|
|
fputs("/delete\">", stdout);
|
|
if (looks_like_id(page))
|
|
fputs("<input type=\"hidden\" name=\"id\" value=\"", stdout);
|
|
else
|
|
fputs("<input type=\"hidden\" name=\"title\" value=\"", stdout);
|
|
html_esc(page);
|
|
fputs("\"><input type=\"submit\" value=\"Delete this page\"></form>\n", stdout);
|
|
free(text);
|
|
html_foot();
|
|
}
|
|
|
|
static void search_form(const char *query)
|
|
{
|
|
fputs("<form action=\"", stdout);
|
|
url("/wikid/search");
|
|
fputs("\" method=\"get\">\n"
|
|
"<p><input type=\"text\" name=\"q\" size=\"40\" value=\"", stdout);
|
|
html_esc(query);
|
|
fputs("\"> <input type=\"submit\" value=\"Search\"></p>\n"
|
|
"</form>\n", stdout);
|
|
}
|
|
|
|
static void route_search(const char *query)
|
|
{
|
|
char *args = NULL;
|
|
size_t alen = 0, acap = 0;
|
|
char *text;
|
|
|
|
if (query == NULL || *query == '\0') {
|
|
html_head("Search");
|
|
fputs("<h1>Search</h1>\n", stdout);
|
|
search_form("");
|
|
html_foot();
|
|
return;
|
|
}
|
|
args = buf_puts(args, &alen, &acap, "{\"query\":\"");
|
|
args = buf_json(args, &alen, &acap, query);
|
|
args = buf_puts(args, &alen, &acap, "\"}");
|
|
if (args == NULL) {
|
|
error_page("Search", "out of memory");
|
|
return;
|
|
}
|
|
text = wikid_call("wiki_search", args);
|
|
free(args);
|
|
if (text == NULL) {
|
|
error_page("Search", "The wiki engine did not answer.");
|
|
return;
|
|
}
|
|
html_head(query);
|
|
fputs("<h1>Search</h1>\n", stdout);
|
|
search_form(query);
|
|
render_search(text);
|
|
free(text);
|
|
html_foot();
|
|
}
|
|
|
|
static void route_write(const char *slug, const char *body)
|
|
{
|
|
char *title = param_get(body, "title");
|
|
char *text = param_get(body, "text");
|
|
char *tags = param_get(body, "tags");
|
|
char *source = param_get(body, "source");
|
|
|
|
if (title == NULL || *title == '\0' || text == NULL || *text == '\0') {
|
|
html_head("Write page");
|
|
fputs("<p class=\"error\">Title and text are required.</p>\n", stdout);
|
|
write_form(slug, title, text, tags, source);
|
|
html_foot();
|
|
free(title); free(text); free(tags); free(source);
|
|
return;
|
|
}
|
|
|
|
{
|
|
char *args = NULL;
|
|
size_t alen = 0, acap = 0;
|
|
char *result;
|
|
|
|
args = buf_puts(args, &alen, &acap, "{\"kb\":\"");
|
|
args = buf_json(args, &alen, &acap, slug);
|
|
args = buf_puts(args, &alen, &acap, "\",\"title\":\"");
|
|
args = buf_json(args, &alen, &acap, title);
|
|
args = buf_puts(args, &alen, &acap, "\",\"text\":\"");
|
|
args = buf_json(args, &alen, &acap, text);
|
|
args = buf_puts(args, &alen, &acap, "\"");
|
|
if (tags != NULL && *tags != '\0') {
|
|
args = buf_puts(args, &alen, &acap, ",\"tags\":\"");
|
|
args = buf_json(args, &alen, &acap, tags);
|
|
args = buf_puts(args, &alen, &acap, "\"");
|
|
}
|
|
if (source != NULL && *source != '\0') {
|
|
args = buf_puts(args, &alen, &acap, ",\"source\":\"");
|
|
args = buf_json(args, &alen, &acap, source);
|
|
args = buf_puts(args, &alen, &acap, "\"");
|
|
}
|
|
args = buf_puts(args, &alen, &acap, "}");
|
|
if (args == NULL) {
|
|
error_page("Write page", "out of memory");
|
|
free(title); free(text); free(tags); free(source);
|
|
return;
|
|
}
|
|
|
|
result = wikid_call("page_write", args);
|
|
free(args);
|
|
|
|
html_head(result != NULL ? "Page written" : "Error");
|
|
if (result != NULL) {
|
|
fputs("<p>", stdout);
|
|
html_esc(result);
|
|
fputs("</p>\n", stdout);
|
|
fputs("<div class=\"nav\"><a href=\"", stdout);
|
|
url("/wikid/");
|
|
html_esc(slug);
|
|
fputs("/\">Back to ", stdout);
|
|
html_esc(slug);
|
|
fputs("</a></div>\n", stdout);
|
|
free(result);
|
|
} else {
|
|
fputs("<p class=\"error\">Could not write the page.</p>\n", stdout);
|
|
write_form(slug, title, text, tags, source);
|
|
}
|
|
html_foot();
|
|
}
|
|
free(title); free(text); free(tags); free(source);
|
|
}
|
|
|
|
static void route_create(const char *body)
|
|
{
|
|
char *name = param_get(body, "name");
|
|
char *desc = param_get(body, "description");
|
|
|
|
if (name == NULL || *name == '\0') {
|
|
html_head("Create wiki");
|
|
fputs("<p class=\"error\">A name is required.</p>\n", stdout);
|
|
create_form(name, desc);
|
|
html_foot();
|
|
free(name); free(desc);
|
|
return;
|
|
}
|
|
|
|
{
|
|
char *args = NULL;
|
|
size_t alen = 0, acap = 0;
|
|
char *result;
|
|
|
|
args = buf_puts(args, &alen, &acap, "{\"name\":\"");
|
|
args = buf_json(args, &alen, &acap, name);
|
|
args = buf_puts(args, &alen, &acap, "\"");
|
|
if (desc != NULL && *desc != '\0') {
|
|
args = buf_puts(args, &alen, &acap, ",\"description\":\"");
|
|
args = buf_json(args, &alen, &acap, desc);
|
|
args = buf_puts(args, &alen, &acap, "\"");
|
|
}
|
|
args = buf_puts(args, &alen, &acap, "}");
|
|
if (args == NULL) {
|
|
error_page("Create wiki", "out of memory");
|
|
free(name); free(desc);
|
|
return;
|
|
}
|
|
|
|
result = wikid_call("wiki_create", args);
|
|
free(args);
|
|
|
|
html_head(result != NULL ? "Wiki created" : "Error");
|
|
if (result != NULL) {
|
|
fputs("<p>", stdout);
|
|
html_esc(result);
|
|
fputs("</p>\n<div class=\"nav\"><a href=\"", stdout);
|
|
url("/");
|
|
fputs("\">All wikis</a></div>\n", stdout);
|
|
free(result);
|
|
} else {
|
|
fputs("<p class=\"error\">Could not create the wiki.</p>\n", stdout);
|
|
create_form(name, desc);
|
|
}
|
|
html_foot();
|
|
}
|
|
free(name); free(desc);
|
|
}
|
|
|
|
/* ------------------------------------------------------------------- router */
|
|
|
|
static void split_path(const char *uri, char *path, size_t pathlen,
|
|
char *query, size_t querylen)
|
|
{
|
|
size_t i;
|
|
|
|
strncpy(path, uri, pathlen - 1);
|
|
path[pathlen - 1] = '\0';
|
|
for (i = 0; i < pathlen && path[i] != '\0' && path[i] != '?'; i++)
|
|
;
|
|
if (i < pathlen && path[i] == '?') {
|
|
if (querylen > 0) {
|
|
strncpy(query, path + i + 1, querylen - 1);
|
|
query[querylen - 1] = '\0';
|
|
}
|
|
path[i] = '\0';
|
|
} else if (querylen > 0) {
|
|
query[0] = '\0';
|
|
}
|
|
}
|
|
|
|
static void handle_request(const char *method, const char *path,
|
|
const char *query, const char *body)
|
|
{
|
|
if (strcmp(path, "/") == 0 || strcmp(path, "/wikid") == 0 ||
|
|
strcmp(path, "/wikid/") == 0) {
|
|
route_index();
|
|
return;
|
|
}
|
|
|
|
if (strcmp(path, "/wikid/search") == 0) {
|
|
route_search(param_get(query, "q"));
|
|
/* param_get leaks its result by design here; the process ends. */
|
|
return;
|
|
}
|
|
|
|
if (strcmp(path, "/wikid/create") == 0) {
|
|
if (strcmp(method, "POST") == 0)
|
|
route_create(body);
|
|
else {
|
|
html_head("Create wiki");
|
|
create_form(NULL, NULL);
|
|
html_foot();
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (strncmp(path, "/wikid/", 7) == 0) {
|
|
const char *slug = path + 7;
|
|
size_t slen = strcspn(slug, "/");
|
|
char slug_buf[256];
|
|
const char *rest;
|
|
|
|
if (slen == 0 || slen >= sizeof(slug_buf)) {
|
|
error_page("Not found", "No wiki named in the path.");
|
|
return;
|
|
}
|
|
memcpy(slug_buf, slug, slen);
|
|
slug_buf[slen] = '\0';
|
|
url_decode(slug_buf);
|
|
rest = slug + slen;
|
|
{
|
|
/* The page part is percent-encoded; decode into a copy so
|
|
* titles like "Water%20chlorination" reach wikid intact. */
|
|
char rest_buf[MAX_LINE];
|
|
|
|
strncpy(rest_buf, rest, sizeof(rest_buf) - 1);
|
|
rest_buf[sizeof(rest_buf) - 1] = '\0';
|
|
url_decode(rest_buf);
|
|
rest = rest_buf;
|
|
|
|
if (strcmp(rest, "/delete") == 0) {
|
|
if (strcmp(method, "POST") == 0)
|
|
route_delete(slug_buf, body);
|
|
else
|
|
error_page("Delete page", "Deletion needs a POST.");
|
|
return;
|
|
}
|
|
if (strcmp(rest, "/write") == 0) {
|
|
if (strcmp(method, "POST") == 0) {
|
|
route_write(slug_buf, body);
|
|
} else {
|
|
html_head("Write page");
|
|
write_form(slug_buf, NULL, NULL, NULL, NULL);
|
|
html_foot();
|
|
}
|
|
return;
|
|
}
|
|
if (rest[0] == '/' && rest[1] != '\0') {
|
|
route_page(slug_buf, rest + 1);
|
|
return;
|
|
}
|
|
if (rest[0] == '\0' || strcmp(rest, "/") == 0) {
|
|
route_wiki_index(slug_buf);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
error_page("Not found", "No such page.");
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ main */
|
|
|
|
static char *read_body(void)
|
|
{
|
|
const char *cl = getenv("CONTENT_LENGTH");
|
|
long len;
|
|
char *body;
|
|
size_t got = 0;
|
|
|
|
if (cl == NULL)
|
|
return NULL;
|
|
len = strtol(cl, NULL, 10);
|
|
if (len <= 0 || len > MAX_BODY)
|
|
return NULL;
|
|
body = malloc((size_t)len + 1);
|
|
if (body == NULL)
|
|
return NULL;
|
|
while (got < (size_t)len) {
|
|
size_t r = fread(body + got, 1, (size_t)len - got, stdin);
|
|
|
|
if (r == 0)
|
|
break;
|
|
got += r;
|
|
}
|
|
body[got] = '\0';
|
|
return body;
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
const char *method = getenv("REQUEST_METHOD");
|
|
const char *uri = getenv("REQUEST_URI");
|
|
const char *qs = getenv("QUERY_STRING");
|
|
const char *script = getenv("SCRIPT_NAME");
|
|
const char *pathinfo = getenv("PATH_INFO");
|
|
char pathbuf[MAX_LINE], querybuf[MAX_LINE];
|
|
char *body = NULL;
|
|
|
|
if (method == NULL)
|
|
method = "GET";
|
|
if (uri == NULL)
|
|
uri = "/";
|
|
if (qs == NULL)
|
|
qs = "";
|
|
|
|
/* Where the CGI is mounted: SCRIPT_NAME for urls, PATH_INFO for routing. */
|
|
g_prefix[0] = '\0';
|
|
if (script != NULL && *script != '\0' && strcmp(script, "/") != 0)
|
|
snprintf(g_prefix, sizeof(g_prefix), "%s", script);
|
|
|
|
if (pathinfo != NULL && *pathinfo != '\0') {
|
|
uri = pathinfo;
|
|
} else if (g_prefix[0] != '\0') {
|
|
size_t plen = strlen(g_prefix);
|
|
|
|
if (strncmp(uri, g_prefix, plen) == 0) {
|
|
uri += plen;
|
|
if (*uri == '\0')
|
|
uri = "/";
|
|
}
|
|
}
|
|
|
|
split_path(uri, pathbuf, sizeof(pathbuf), querybuf, sizeof(querybuf));
|
|
if (querybuf[0] == '\0' && qs[0] != '\0') {
|
|
strncpy(querybuf, qs, sizeof(querybuf) - 1);
|
|
querybuf[sizeof(querybuf) - 1] = '\0';
|
|
}
|
|
|
|
if (strcmp(method, "POST") == 0)
|
|
body = read_body();
|
|
|
|
printf("Content-Type: text/html; charset=utf-8\r\n\r\n");
|
|
handle_request(method, pathbuf, querybuf, body != NULL ? body : "");
|
|
free(body);
|
|
return 0;
|
|
}
|