tasksd-http: Web-Oberflaeche als CGI; task_update; TASKDIR
tasksd-http ist ein C99-CGI-Programm (HTML 4.01) ueber den MCP-Server von tasksd (vendor/cgi/cgi.h, tests/test-http.sh, Makefile, README). Neues Werkzeug task_update: korrigiert die beschreibenden Felder einer Task (title, goal, scope, tags, priority); Zustandswechsel bleiben bei task_next/done/pause/drop. Die Umgebungsvariable TASKDIR bleibt als alter Name gueltig. tests/run.py angepasst.
This commit is contained in:
1 parent
e8bb6d56ea
commit
0a084532bc
8 files changed
+1511
-8
No files matched your search
@@ -2,3 +2,4 @@
|
||||
tasksd
|
||||
*.bak
|
||||
*.log
|
||||
tasksd-http
|
||||
@@ -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: tasksd
|
||||
all: tasksd tasksd-http
|
||||
|
||||
tasksd: tasksd.c $(LIBDIR)/mcp.h
|
||||
$(CC) $(CFLAGS) -I$(LIBDIR) -o $@ tasksd.c
|
||||
|
||||
test: tasksd
|
||||
./tests/run.sh
|
||||
tasksd-http: tasksd-http.c vendor/cgi/cgi.h $(LIBDIR)/mcp.h
|
||||
$(CC) $(CFLAGS) -I$(LIBDIR) -Ivendor/cgi -o $@ tasksd-http.c
|
||||
|
||||
install: tasksd
|
||||
test: tasksd tasksd-http
|
||||
./tests/run.sh
|
||||
./tests/test-http.sh
|
||||
|
||||
install: tasksd tasksd-http
|
||||
mkdir -p $(DESTDIR)$(PREFIX)/bin
|
||||
cp tasksd $(DESTDIR)$(PREFIX)/bin/
|
||||
install -d $(DESTDIR)$(CGIDIR)
|
||||
install -m 0755 tasksd-http $(DESTDIR)$(CGIDIR)/tasksd-http
|
||||
|
||||
clean:
|
||||
rm -f tasksd
|
||||
rm -f tasksd tasksd-http
|
||||
|
||||
.PHONY: all test install clean
|
||||
@@ -31,3 +31,41 @@ cp ../libmcp/mcp.h vendor/libmcp/mcp.h
|
||||
|
||||
`make` uses `LIBDIR=vendor/libmcp` by default; override it to build against a
|
||||
checkout elsewhere: `make LIBDIR=../libmcp`.
|
||||
|
||||
## Web UI (CGI)
|
||||
|
||||
`tasksd-http` is a C99 CGI program: HTML 4.01, no JavaScript, no auth. Every list, read,
|
||||
create, edit and state change is a `tools/call` against tasksd (`TASKSD_URL`, default
|
||||
`http://127.0.0.1:8781/mcp`); the CGI never touches the task files.
|
||||
|
||||
| Address | Effect |
|
||||
|---|---|
|
||||
| `/` | tasks, filter with `?state=…&scope=…` |
|
||||
| `/scopes` | scopes with pending/running/paused/done/dropped counts |
|
||||
| `/new` | create a task |
|
||||
| `/t/<id>` | one task with controls |
|
||||
| `/t/<id>/edit` | title, goal, scope, priority, tags (`task_update`) |
|
||||
| `/t/<id>/pause` | POST, note optional |
|
||||
| `/t/<id>/resume` | POST, note optional |
|
||||
| `/t/<id>/done` | POST, result optional |
|
||||
| `/t/<id>/drop` | POST, result optional |
|
||||
|
||||
`task_update` exists for the edit form: state changes stay with pause/resume/done/drop,
|
||||
while a wrong title, an incomplete goal or a moved scope can be corrected.
|
||||
|
||||
```sh
|
||||
make # tasksd and tasksd-http
|
||||
sudo make install # /usr/local/bin/tasksd, /usr/local/lib/cgi-bin/tasksd-http
|
||||
```
|
||||
|
||||
With busybox httpd:
|
||||
|
||||
```sh
|
||||
install -d /srv/www/cgi-bin
|
||||
install -m 0755 tasksd-http /srv/www/cgi-bin/
|
||||
TASKSD_URL=http://127.0.0.1:8781/mcp \
|
||||
busybox httpd -f -p 0.0.0.0:8798 -h /srv/www
|
||||
```
|
||||
|
||||
`vendor/cgi/cgi.h` is the shared CGI helper (one copy each in wikid, memoryd, skilld and
|
||||
tasksd).
|
||||
+651
@@ -0,0 +1,651 @@
|
||||
/*
|
||||
* tasksd-http — CGI front-end for tasksd.
|
||||
*
|
||||
* C99, HTML 4.01, no JavaScript, no auth. Every list, read, create, edit and
|
||||
* state change goes through tasksd's MCP server. The CGI never reads the task
|
||||
* files itself.
|
||||
*
|
||||
* Routes (mounted at /cgi-bin/tasksd-http under busybox httpd):
|
||||
* / — tasks, filter with ?state=…&scope=…
|
||||
* /scopes — scope overview with counts
|
||||
* /new — create a task
|
||||
* /t/<id> — one task with controls
|
||||
* /t/<id>/edit — title, goal, scope, priority, tags
|
||||
* /t/<id>/pause — POST, note optional
|
||||
* /t/<id>/resume — POST, note optional
|
||||
* /t/<id>/done — POST, result optional
|
||||
* /t/<id>/drop — POST, result optional
|
||||
*
|
||||
* Environment: TASKSD_URL (default http://127.0.0.1:8781/mcp)
|
||||
*
|
||||
* Build:
|
||||
* cc -std=c99 -O2 -Wall -Wextra -Wpedantic -D_POSIX_C_SOURCE=200809L \
|
||||
* -Ivendor/libmcp -Ivendor/cgi -o tasksd-http tasksd-http.c
|
||||
*/
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#define MCP_IMPLEMENTATION
|
||||
#include "mcp.h"
|
||||
#include "cgi.h"
|
||||
|
||||
#define ENV_URL "TASKSD_URL"
|
||||
#define DEF_URL "http://127.0.0.1:8781/mcp"
|
||||
#define APP "tasksd-http"
|
||||
|
||||
static void state_badge(const char *state)
|
||||
{
|
||||
const char *s = state != NULL ? state : "?";
|
||||
|
||||
fputs("<span class=\"state s-", stdout);
|
||||
cg_html(s);
|
||||
fputs("\">", stdout);
|
||||
cg_html(s);
|
||||
fputs("</span>", stdout);
|
||||
}
|
||||
|
||||
static void state_tabs(const char *scope, const char *active)
|
||||
{
|
||||
static const char *states[] = { "", "pending", "running", "paused",
|
||||
"done", "dropped" };
|
||||
static const char *labels[] = { "All", "Pending", "Running", "Paused",
|
||||
"Done", "Dropped" };
|
||||
|
||||
fputs("<div class=\"actions\">", stdout);
|
||||
for (size_t i = 0; i < sizeof(states) / sizeof(states[0]); i++) {
|
||||
int on = active != NULL && strcmp(active, states[i]) == 0;
|
||||
|
||||
if (active == NULL && states[i][0] == '\0')
|
||||
on = 1;
|
||||
fputs("<a href=\"", stdout);
|
||||
cg_link("/?state=");
|
||||
fputs(states[i], stdout);
|
||||
if (scope != NULL && *scope != '\0') {
|
||||
fputs("&scope=", stdout);
|
||||
cg_urlenc(scope);
|
||||
}
|
||||
fputs("\">", stdout);
|
||||
if (on)
|
||||
fputs("<b>", stdout);
|
||||
fputs(labels[i], stdout);
|
||||
if (on)
|
||||
fputs("</b>", stdout);
|
||||
fputs("</a> ", stdout);
|
||||
}
|
||||
fputs("</div>\n", stdout);
|
||||
}
|
||||
|
||||
static void task_row(const mcp_json *t)
|
||||
{
|
||||
const char *id = mcp_arg_string(t, "id");
|
||||
const char *title = mcp_arg_string(t, "title");
|
||||
const char *scope = mcp_arg_string(t, "scope");
|
||||
const char *state = mcp_arg_string(t, "state");
|
||||
const char *tags = mcp_arg_string(t, "tags");
|
||||
double prio = mcp_arg_num(t, "priority", 0);
|
||||
|
||||
if (id == NULL)
|
||||
return;
|
||||
fputs("<tr><td><a href=\"", stdout);
|
||||
cg_link("/t/");
|
||||
cg_html(id);
|
||||
fputs("\">", stdout);
|
||||
cg_html(id);
|
||||
fputs("</a></td><td>", stdout);
|
||||
state_badge(state);
|
||||
fputs("</td><td><a href=\"", stdout);
|
||||
cg_link("/t/");
|
||||
cg_html(id);
|
||||
fputs("\"><b>", stdout);
|
||||
cg_html(title != NULL ? title : "(untitled)");
|
||||
fputs("</b></a><br><span class=\"meta\">scope ", stdout);
|
||||
cg_html(scope != NULL ? scope : "");
|
||||
printf(" · priority %.0f", prio);
|
||||
if (tags != NULL && *tags != '\0') {
|
||||
fputs(" · tags ", stdout);
|
||||
cg_html(tags);
|
||||
}
|
||||
fputs("</span></td><td>", stdout);
|
||||
cg_time((long)mcp_arg_num(t, "created", 0));
|
||||
fputs("</td></tr>\n", stdout);
|
||||
}
|
||||
|
||||
static void render_tasks(const mcp_json *arr)
|
||||
{
|
||||
size_t n = mcp_array_len(arr);
|
||||
|
||||
if (n == 0) {
|
||||
fputs("<p class=\"meta\">No tasks match.</p>\n", stdout);
|
||||
return;
|
||||
}
|
||||
fputs("<table class=\"list\" summary=\"Tasks\"><tr><th>ID</th><th>State</th><th>Task</th>"
|
||||
"<th>Created</th></tr>\n", stdout);
|
||||
for (size_t i = 0; i < n; i++)
|
||||
task_row(mcp_array_at(arr, i));
|
||||
fputs("</table>\n", stdout);
|
||||
}
|
||||
|
||||
static void route_list(const char *query)
|
||||
{
|
||||
char *scope = cg_param(query, "scope");
|
||||
char *state = cg_param(query, "state");
|
||||
char *limstr = cg_param(query, "limit");
|
||||
int limit = limstr != NULL ? atoi(limstr) : 100;
|
||||
mcp_buf args;
|
||||
int first = 1;
|
||||
mcp_json *v;
|
||||
|
||||
if (limit <= 0 || limit > 500)
|
||||
limit = 100;
|
||||
mcp_buf_init(&args);
|
||||
mcp_buf_puts(&args, "{");
|
||||
cg_args_add_str(&args, &first, "scope", scope);
|
||||
if (state != NULL && *state != '\0' && strcmp(state, "all") != 0)
|
||||
cg_args_add_str(&args, &first, "state", state);
|
||||
cg_args_add_int(&args, &first, "limit", limit);
|
||||
mcp_buf_puts(&args, "}");
|
||||
v = cg_rpc_json(ENV_URL, DEF_URL, "task_list", args.p);
|
||||
mcp_buf_free(&args);
|
||||
|
||||
cg_head(APP, "Tasks");
|
||||
fputs("<h1>Tasks</h1>\n", stdout);
|
||||
state_tabs(scope, state);
|
||||
fputs("<form class=\"inline\" action=\"", stdout);
|
||||
cg_link("/");
|
||||
fputs("\" method=\"get\"><label>Scope "
|
||||
"<input type=\"text\" name=\"scope\" size=\"28\" value=\"", stdout);
|
||||
cg_html(scope != NULL ? scope : "");
|
||||
fputs("\"></label> ", stdout);
|
||||
if (state != NULL && *state != '\0') {
|
||||
fputs("<input type=\"hidden\" name=\"state\" value=\"", stdout);
|
||||
cg_html(state);
|
||||
fputs("\">", stdout);
|
||||
}
|
||||
fputs("<input type=\"submit\" value=\"Filter\"></form>\n", stdout);
|
||||
fputs("<div class=\"actions\"><a href=\"", stdout);
|
||||
cg_link("/new");
|
||||
fputs("\">Create a task</a><a href=\"", stdout);
|
||||
cg_link("/scopes");
|
||||
fputs("\">Scopes</a></div>\n", stdout);
|
||||
if (v == NULL) {
|
||||
fputs("<div class=\"err\">tasksd did not answer.</div>\n", stdout);
|
||||
} else {
|
||||
render_tasks(mcp_arg(v, "tasks"));
|
||||
printf("<p class=\"meta\">%.0f task(s) shown</p>\n",
|
||||
mcp_arg_num(v, "count", 0));
|
||||
mcp_json_free(v);
|
||||
}
|
||||
cg_foot();
|
||||
free(scope);
|
||||
free(state);
|
||||
free(limstr);
|
||||
}
|
||||
|
||||
static void route_scopes(void)
|
||||
{
|
||||
mcp_json *v = cg_rpc_json(ENV_URL, DEF_URL, "task_scopes", "{}");
|
||||
|
||||
cg_head(APP, "Task scopes");
|
||||
fputs("<h1>Task scopes</h1>\n", stdout);
|
||||
if (v == NULL) {
|
||||
fputs("<div class=\"err\">tasksd did not answer.</div>\n", stdout);
|
||||
} else {
|
||||
const mcp_json *arr = mcp_arg(v, "scopes");
|
||||
size_t n = mcp_array_len(arr);
|
||||
|
||||
fputs("<table class=\"list\" summary=\"Task counts per scope\"><tr><th>Scope</th><th>Pending</th>"
|
||||
"<th>Running</th><th>Paused</th><th>Done</th><th>Dropped</th>"
|
||||
"</tr>\n", stdout);
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
const mcp_json *s = mcp_array_at(arr, i);
|
||||
const char *scope = mcp_arg_string(s, "scope");
|
||||
|
||||
if (scope == NULL)
|
||||
continue;
|
||||
fputs("<tr><td><a href=\"", stdout);
|
||||
cg_link("/?scope=");
|
||||
cg_urlenc(scope);
|
||||
fputs("\">", stdout);
|
||||
cg_html(scope);
|
||||
fputs("</a></td>", stdout);
|
||||
printf("<td>%.0f</td><td>%.0f</td><td>%.0f</td>"
|
||||
"<td>%.0f</td><td>%.0f</td></tr>\n",
|
||||
mcp_arg_num(s, "pending", 0),
|
||||
mcp_arg_num(s, "running", 0),
|
||||
mcp_arg_num(s, "paused", 0),
|
||||
mcp_arg_num(s, "done", 0),
|
||||
mcp_arg_num(s, "dropped", 0));
|
||||
}
|
||||
fputs("</table>\n", stdout);
|
||||
mcp_json_free(v);
|
||||
}
|
||||
fputs("<div class=\"actions\"><a href=\"", stdout);
|
||||
cg_link("/");
|
||||
fputs("\">All tasks</a></div>\n", stdout);
|
||||
cg_foot();
|
||||
}
|
||||
|
||||
static void task_kv(const char *key, const char *val)
|
||||
{
|
||||
if (val == NULL || *val == '\0')
|
||||
return;
|
||||
fputs("<div class=\"kv\"><b>", stdout);
|
||||
cg_html(key);
|
||||
fputs("</b><br>", stdout);
|
||||
cg_md(val);
|
||||
fputs("</div>\n", stdout);
|
||||
}
|
||||
|
||||
static void action_form(const char *id, const char *action, const char *label,
|
||||
const char *field, const char *placeholder)
|
||||
{
|
||||
fputs("<form method=\"post\" action=\"", stdout);
|
||||
cg_link("/t/");
|
||||
cg_html(id);
|
||||
fputs("/", stdout);
|
||||
cg_html(action);
|
||||
fputs("\"><p><label>", stdout);
|
||||
cg_html(placeholder);
|
||||
fputs("<br><textarea name=\"", stdout);
|
||||
cg_html(field);
|
||||
fputs("\" rows=\"3\" cols=\"70\"></textarea></label></p>"
|
||||
"<p><input type=\"submit\" value=\"", stdout);
|
||||
cg_html(label);
|
||||
fputs("\"></p></form>\n", stdout);
|
||||
}
|
||||
|
||||
static void task_actions(const char *id, const char *state)
|
||||
{
|
||||
int paused = state != NULL && strcmp(state, "paused") == 0;
|
||||
int closed = state != NULL &&
|
||||
(strcmp(state, "done") == 0 || strcmp(state, "dropped") == 0);
|
||||
|
||||
fputs("<h2>Controls</h2>\n", stdout);
|
||||
if (paused) {
|
||||
action_form(id, "resume", "Resume", "note",
|
||||
"Note (what was left, why now continues)");
|
||||
} else if (!closed) {
|
||||
action_form(id, "pause", "Pause", "note",
|
||||
"Note (why parked, what is left)");
|
||||
}
|
||||
if (!closed) {
|
||||
action_form(id, "done", "Mark done", "result",
|
||||
"Result (what was achieved, evidence)");
|
||||
action_form(id, "drop", "Drop", "result",
|
||||
"Reason (why it is not worth doing)");
|
||||
}
|
||||
}
|
||||
|
||||
static void route_detail(const char *id)
|
||||
{
|
||||
mcp_buf args;
|
||||
int first = 1;
|
||||
char *text;
|
||||
mcp_json *v;
|
||||
|
||||
mcp_buf_init(&args);
|
||||
mcp_buf_puts(&args, "{");
|
||||
cg_args_add_str(&args, &first, "id", id);
|
||||
mcp_buf_puts(&args, "}");
|
||||
text = cg_rpc(ENV_URL, DEF_URL, "task_get", args.p);
|
||||
mcp_buf_free(&args);
|
||||
if (text == NULL) {
|
||||
cg_error_page(APP, "Task not found", "tasksd did not answer.");
|
||||
return;
|
||||
}
|
||||
v = mcp_parse(text);
|
||||
free(text);
|
||||
if (v == NULL) {
|
||||
cg_error_page(APP, "Task not found", "tasksd returned no task.");
|
||||
return;
|
||||
}
|
||||
cg_head(APP, id);
|
||||
{
|
||||
const char *title = mcp_arg_string(v, "title");
|
||||
const char *scope = mcp_arg_string(v, "scope");
|
||||
const char *state = mcp_arg_string(v, "state");
|
||||
const char *tags = mcp_arg_string(v, "tags");
|
||||
const char *result = mcp_arg_string(v, "result");
|
||||
|
||||
fputs("<h1>", stdout);
|
||||
cg_html(title != NULL ? title : id);
|
||||
fputs("</h1>\n<p>", stdout);
|
||||
state_badge(state);
|
||||
fputs(" <span class=\"meta\">", stdout);
|
||||
cg_html(id);
|
||||
fputs(" · scope ", stdout);
|
||||
if (scope != NULL) {
|
||||
fputs("<a href=\"", stdout);
|
||||
cg_link("/?scope=");
|
||||
cg_urlenc(scope);
|
||||
fputs("\">", stdout);
|
||||
cg_html(scope);
|
||||
fputs("</a>", stdout);
|
||||
}
|
||||
printf(" · priority %.0f", mcp_arg_num(v, "priority", 0));
|
||||
if (tags != NULL && *tags != '\0') {
|
||||
fputs(" · tags ", stdout);
|
||||
cg_html(tags);
|
||||
}
|
||||
fputs("</span></p>\n", stdout);
|
||||
|
||||
task_kv("Goal", mcp_arg_string(v, "goal"));
|
||||
if (result != NULL && *result != '\0')
|
||||
task_kv("Result", result);
|
||||
|
||||
fputs("<p class=\"meta\">created ", stdout);
|
||||
cg_time((long)mcp_arg_num(v, "created", 0));
|
||||
fputs(" · started ", stdout);
|
||||
cg_time((long)mcp_arg_num(v, "started", 0));
|
||||
fputs(" · ended ", stdout);
|
||||
cg_time((long)mcp_arg_num(v, "ended", 0));
|
||||
fputs("</p>\n", stdout);
|
||||
|
||||
fputs("<div class=\"actions\"><a href=\"", stdout);
|
||||
cg_link("/t/");
|
||||
cg_html(id);
|
||||
fputs("/edit\">Edit title/goal/scope</a><a href=\"", stdout);
|
||||
cg_link("/");
|
||||
fputs("\">All tasks</a></div>\n", stdout);
|
||||
task_actions(id, state);
|
||||
}
|
||||
mcp_json_free(v);
|
||||
cg_foot();
|
||||
}
|
||||
|
||||
static void edit_form(const char *id, const mcp_json *v)
|
||||
{
|
||||
const char *title = mcp_arg_string(v, "title");
|
||||
const char *goal = mcp_arg_string(v, "goal");
|
||||
const char *scope = mcp_arg_string(v, "scope");
|
||||
const char *tags = mcp_arg_string(v, "tags");
|
||||
|
||||
fputs("<form method=\"post\" action=\"", stdout);
|
||||
cg_link("/t/");
|
||||
cg_html(id);
|
||||
fputs("/edit\">\n<p><label>Title<br>"
|
||||
"<input type=\"text\" name=\"title\" size=\"76\" value=\"", stdout);
|
||||
cg_html(title != NULL ? title : "");
|
||||
fputs("\"></label></p>\n<p><label>Goal (why it exists)<br>"
|
||||
"<textarea name=\"goal\" rows=\"8\" cols=\"76\">", stdout);
|
||||
cg_html(goal != NULL ? goal : "");
|
||||
fputs("</textarea></label></p>\n<p><label>Scope<br>"
|
||||
"<input type=\"text\" name=\"scope\" size=\"40\" value=\"", stdout);
|
||||
cg_html(scope != NULL ? scope : "");
|
||||
fputs("\"></label></p>\n<p><label>Priority<br>"
|
||||
"<input type=\"text\" name=\"priority\" size=\"8\" value=\"", stdout);
|
||||
printf("%.0f", mcp_arg_num(v, "priority", 0));
|
||||
fputs("\"></label></p>\n<p><label>Tags (comma separated)<br>"
|
||||
"<input type=\"text\" name=\"tags\" size=\"48\" value=\"", stdout);
|
||||
cg_html(tags != NULL ? tags : "");
|
||||
fputs("\"></label></p>\n<p><input type=\"submit\" value=\"Save\"></p>\n"
|
||||
"</form>\n", stdout);
|
||||
}
|
||||
|
||||
static void route_edit(const char *id, const char *body)
|
||||
{
|
||||
if (strcmp(cg_method(), "POST") == 0) {
|
||||
char *title = cg_param(body, "title");
|
||||
char *goal = cg_param(body, "goal");
|
||||
char *scope = cg_param(body, "scope");
|
||||
char *tags = cg_param(body, "tags");
|
||||
char *prio = cg_param(body, "priority");
|
||||
mcp_buf args;
|
||||
int first = 1;
|
||||
mcp_json *v;
|
||||
|
||||
mcp_buf_init(&args);
|
||||
mcp_buf_puts(&args, "{");
|
||||
cg_args_add_str(&args, &first, "id", id);
|
||||
cg_args_add_str(&args, &first, "title", title);
|
||||
cg_args_add_str(&args, &first, "goal", goal);
|
||||
cg_args_add_str(&args, &first, "scope", scope);
|
||||
cg_args_add_str(&args, &first, "tags", tags);
|
||||
if (prio != NULL && *prio != '\0')
|
||||
cg_args_add_int(&args, &first, "priority", atoi(prio));
|
||||
mcp_buf_puts(&args, "}");
|
||||
v = cg_rpc_json(ENV_URL, DEF_URL, "task_update", args.p);
|
||||
mcp_buf_free(&args);
|
||||
cg_head(APP, "Edit task");
|
||||
if (v == NULL) {
|
||||
fputs("<div class=\"err\">tasksd did not answer.</div>\n", stdout);
|
||||
} else {
|
||||
fputs("<h1>Updated</h1>\n<div class=\"msg\">", stdout);
|
||||
cg_html(mcp_arg_string(v, "id"));
|
||||
fputs(" — ", stdout);
|
||||
cg_html(mcp_arg_string(v, "title"));
|
||||
fputs("</div>\n<div class=\"actions\"><a href=\"", stdout);
|
||||
cg_link("/t/");
|
||||
cg_html(id);
|
||||
fputs("\">Back to the task</a></div>\n", stdout);
|
||||
mcp_json_free(v);
|
||||
}
|
||||
cg_foot();
|
||||
free(title); free(goal); free(scope); free(tags); free(prio);
|
||||
return;
|
||||
}
|
||||
{
|
||||
mcp_buf args;
|
||||
int first = 1;
|
||||
char *text;
|
||||
mcp_json *v;
|
||||
|
||||
mcp_buf_init(&args);
|
||||
mcp_buf_puts(&args, "{");
|
||||
cg_args_add_str(&args, &first, "id", id);
|
||||
mcp_buf_puts(&args, "}");
|
||||
text = cg_rpc(ENV_URL, DEF_URL, "task_get", args.p);
|
||||
mcp_buf_free(&args);
|
||||
cg_head(APP, "Edit task");
|
||||
fputs("<h1>Edit ", stdout);
|
||||
cg_html(id);
|
||||
fputs("</h1>\n", stdout);
|
||||
if (text == NULL) {
|
||||
fputs("<div class=\"err\">tasksd did not answer.</div>\n", stdout);
|
||||
} else {
|
||||
v = mcp_parse(text);
|
||||
free(text);
|
||||
if (v != NULL) {
|
||||
edit_form(id, v);
|
||||
mcp_json_free(v);
|
||||
} else {
|
||||
fputs("<div class=\"err\">Task not found.</div>\n", stdout);
|
||||
}
|
||||
}
|
||||
cg_foot();
|
||||
}
|
||||
}
|
||||
|
||||
static void route_new(const char *body)
|
||||
{
|
||||
if (strcmp(cg_method(), "POST") != 0) {
|
||||
cg_head(APP, "Create a task");
|
||||
fputs("<h1>Create a task</h1>\n<form method=\"post\" action=\"", stdout);
|
||||
cg_link("/new");
|
||||
fputs("\">\n<p><label>Title (what to do)<br>"
|
||||
"<input type=\"text\" name=\"title\" size=\"76\"></label></p>\n"
|
||||
"<p><label>Goal (why it exists — required)<br>"
|
||||
"<textarea name=\"goal\" rows=\"8\" cols=\"76\"></textarea>"
|
||||
"</label></p>\n"
|
||||
"<p><label>Scope (e.g. makronix, fleet/console)<br>"
|
||||
"<input type=\"text\" name=\"scope\" size=\"40\"></label></p>\n"
|
||||
"<p><label>Priority (higher runs first)<br>"
|
||||
"<input type=\"text\" name=\"priority\" size=\"8\" value=\"0\">"
|
||||
"</label></p>\n"
|
||||
"<p><label>Tags (comma separated)<br>"
|
||||
"<input type=\"text\" name=\"tags\" size=\"48\"></label></p>\n"
|
||||
"<p><input type=\"submit\" value=\"Create task\"></p>\n</form>\n",
|
||||
stdout);
|
||||
cg_foot();
|
||||
return;
|
||||
}
|
||||
{
|
||||
char *title = cg_param(body, "title");
|
||||
char *goal = cg_param(body, "goal");
|
||||
char *scope = cg_param(body, "scope");
|
||||
char *tags = cg_param(body, "tags");
|
||||
char *prio = cg_param(body, "priority");
|
||||
mcp_buf args;
|
||||
int first = 1;
|
||||
mcp_json *v;
|
||||
|
||||
if (title == NULL || *title == '\0' || goal == NULL ||
|
||||
*goal == '\0' || scope == NULL || *scope == '\0') {
|
||||
cg_error_page(APP, "Create a task",
|
||||
"Title, goal and scope are required.");
|
||||
free(title); free(goal); free(scope); free(tags); free(prio);
|
||||
return;
|
||||
}
|
||||
mcp_buf_init(&args);
|
||||
mcp_buf_puts(&args, "{");
|
||||
cg_args_add_str(&args, &first, "title", title);
|
||||
cg_args_add_str(&args, &first, "goal", goal);
|
||||
cg_args_add_str(&args, &first, "scope", scope);
|
||||
if (prio != NULL && *prio != '\0')
|
||||
cg_args_add_int(&args, &first, "priority", atoi(prio));
|
||||
cg_args_add_str(&args, &first, "tags", tags);
|
||||
mcp_buf_puts(&args, "}");
|
||||
v = cg_rpc_json(ENV_URL, DEF_URL, "task_create", args.p);
|
||||
mcp_buf_free(&args);
|
||||
cg_head(APP, "Create a task");
|
||||
if (v == NULL) {
|
||||
fputs("<div class=\"err\">tasksd did not answer.</div>\n", stdout);
|
||||
} else {
|
||||
const char *id = mcp_arg_string(v, "id");
|
||||
|
||||
fputs("<h1>Created</h1>\n<div class=\"msg\">", stdout);
|
||||
cg_html(id != NULL ? id : "");
|
||||
fputs(" — ", stdout);
|
||||
cg_html(mcp_arg_string(v, "title"));
|
||||
fputs("</div>\n<div class=\"actions\"><a href=\"", stdout);
|
||||
cg_link("/t/");
|
||||
cg_html(id != NULL ? id : "");
|
||||
fputs("\">Open the task</a><a href=\"", stdout);
|
||||
cg_link("/");
|
||||
fputs("\">All tasks</a></div>\n", stdout);
|
||||
mcp_json_free(v);
|
||||
}
|
||||
cg_foot();
|
||||
free(title); free(goal); free(scope); free(tags); free(prio);
|
||||
}
|
||||
}
|
||||
|
||||
static void route_state(const char *id, const char *action, const char *body)
|
||||
{
|
||||
const char *tool = NULL;
|
||||
const char *field = NULL;
|
||||
char *note = NULL;
|
||||
mcp_buf args;
|
||||
int first = 1;
|
||||
mcp_json *v;
|
||||
|
||||
if (strcmp(cg_method(), "POST") != 0) {
|
||||
cg_error_page(APP, "Task state", "State changes need a POST.");
|
||||
return;
|
||||
}
|
||||
if (strcmp(action, "pause") == 0) {
|
||||
tool = "task_pause";
|
||||
field = "note";
|
||||
} else if (strcmp(action, "resume") == 0) {
|
||||
tool = "task_resume";
|
||||
field = "note";
|
||||
} else if (strcmp(action, "done") == 0) {
|
||||
tool = "task_done";
|
||||
field = "result";
|
||||
} else if (strcmp(action, "drop") == 0) {
|
||||
tool = "task_drop";
|
||||
field = "result";
|
||||
}
|
||||
if (tool == NULL) {
|
||||
cg_error_page(APP, "Task state", "Unknown action.");
|
||||
return;
|
||||
}
|
||||
note = cg_param(body, field);
|
||||
mcp_buf_init(&args);
|
||||
mcp_buf_puts(&args, "{");
|
||||
cg_args_add_str(&args, &first, "id", id);
|
||||
cg_args_add_str(&args, &first, field, note);
|
||||
mcp_buf_puts(&args, "}");
|
||||
v = cg_rpc_json(ENV_URL, DEF_URL, tool, args.p);
|
||||
mcp_buf_free(&args);
|
||||
cg_head(APP, "Task state");
|
||||
if (v == NULL) {
|
||||
fputs("<div class=\"err\">tasksd did not answer.</div>\n", stdout);
|
||||
} else {
|
||||
const char *state = mcp_arg_string(v, "state");
|
||||
|
||||
fputs("<h1>", stdout);
|
||||
cg_html(state != NULL ? state : "changed");
|
||||
fputs("</h1>\n<div class=\"msg\">", stdout);
|
||||
cg_html(mcp_arg_string(v, "id"));
|
||||
fputs(" — ", stdout);
|
||||
cg_html(mcp_arg_string(v, "title"));
|
||||
fputs("</div>\n<div class=\"actions\"><a href=\"", stdout);
|
||||
cg_link("/t/");
|
||||
cg_html(id);
|
||||
fputs("\">Back to the task</a><a href=\"", stdout);
|
||||
cg_link("/");
|
||||
fputs("\">All tasks</a></div>\n", stdout);
|
||||
mcp_json_free(v);
|
||||
}
|
||||
cg_foot();
|
||||
free(note);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ router */
|
||||
|
||||
static void handle(const char *path, const char *query, const char *body)
|
||||
{
|
||||
if (strcmp(path, "/") == 0 || strcmp(path, "") == 0) {
|
||||
route_list(query);
|
||||
return;
|
||||
}
|
||||
if (strcmp(path, "/scopes") == 0) {
|
||||
route_scopes();
|
||||
return;
|
||||
}
|
||||
if (strcmp(path, "/new") == 0) {
|
||||
route_new(body);
|
||||
return;
|
||||
}
|
||||
if (strncmp(path, "/t/", 3) == 0) {
|
||||
const char *rest = path + 3;
|
||||
size_t idlen = strcspn(rest, "/");
|
||||
char id[128];
|
||||
const char *tail;
|
||||
|
||||
if (idlen == 0 || idlen >= sizeof(id)) {
|
||||
cg_error_page(APP, "Not found", "No task id in the path.");
|
||||
return;
|
||||
}
|
||||
memcpy(id, rest, idlen);
|
||||
id[idlen] = '\0';
|
||||
cg_url_decode(id);
|
||||
tail = rest + idlen;
|
||||
if (strcmp(tail, "/edit") == 0) {
|
||||
route_edit(id, body);
|
||||
return;
|
||||
}
|
||||
if (tail[0] == '/' && tail[1] != '\0') {
|
||||
route_state(id, tail + 1, body);
|
||||
return;
|
||||
}
|
||||
if (*tail == '\0') {
|
||||
route_detail(id);
|
||||
return;
|
||||
}
|
||||
}
|
||||
cg_error_page(APP, "Not found", "No such page.");
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char path[4096], query[4096];
|
||||
char *body = NULL;
|
||||
|
||||
cg_route(path, sizeof(path), query, sizeof(query));
|
||||
if (strcmp(cg_method(), "POST") == 0)
|
||||
body = cg_read_body();
|
||||
cg_header();
|
||||
handle(path, query, body != NULL ? body : "");
|
||||
free(body);
|
||||
return 0;
|
||||
}
|
||||
@@ -52,10 +52,12 @@
|
||||
|
||||
static char *tasks_root(void)
|
||||
{
|
||||
const char *env = getenv("TASKDIR");
|
||||
const char *env = getenv("TASKD_HOME");
|
||||
const char *home;
|
||||
mcp_buf b;
|
||||
|
||||
if (env == NULL || *env == '\0')
|
||||
env = getenv("TASKDIR"); /* alter Name, bleibt gueltig */
|
||||
mcp_buf_init(&b);
|
||||
if (env != NULL && *env != '\0') {
|
||||
mcp_buf_puts(&b, env);
|
||||
@@ -582,6 +584,67 @@ static void t_get(mcp_conn *c, const mcp_json *args, void *user)
|
||||
free_task(&t);
|
||||
}
|
||||
|
||||
/* Correct the descriptive fields of a task. State changes stay with
|
||||
* pause/resume/done/drop; this is for a title that turned out wrong, a goal
|
||||
* that needs the reason added, or a moved scope. */
|
||||
static void t_update(mcp_conn *c, const mcp_json *args, void *user)
|
||||
{
|
||||
const char *id = mcp_arg_string(args, "id");
|
||||
const char *title = mcp_arg_string(args, "title");
|
||||
const char *goal = mcp_arg_string(args, "goal");
|
||||
const char *scope = mcp_arg_string(args, "scope");
|
||||
const char *tags = mcp_arg_string(args, "tags");
|
||||
const mcp_json *prio = mcp_arg(args, "priority");
|
||||
struct task t;
|
||||
int changed = 0;
|
||||
mcp_buf b;
|
||||
|
||||
(void)user;
|
||||
if (id == NULL || task_load(id, &t) != 0) {
|
||||
mcp_reply_error(c, "no such task");
|
||||
return;
|
||||
}
|
||||
if (title != NULL && *title != '\0') {
|
||||
free(t.title);
|
||||
t.title = strdup(title);
|
||||
changed = 1;
|
||||
}
|
||||
if (goal != NULL && *goal != '\0') {
|
||||
free(t.goal);
|
||||
t.goal = strdup(goal);
|
||||
changed = 1;
|
||||
}
|
||||
if (scope != NULL && *scope != '\0') {
|
||||
free(t.scope);
|
||||
t.scope = strdup(scope);
|
||||
changed = 1;
|
||||
}
|
||||
if (tags != NULL) {
|
||||
free(t.tags);
|
||||
t.tags = strdup(tags);
|
||||
changed = 1;
|
||||
}
|
||||
if (prio != NULL && prio->t == MCP_NUM) {
|
||||
t.priority = (int)prio->n;
|
||||
changed = 1;
|
||||
}
|
||||
if (!changed) {
|
||||
say(c, "nothing to change: give title, goal, scope, priority or tags");
|
||||
free_task(&t);
|
||||
return;
|
||||
}
|
||||
if (task_save(&t) != 0) {
|
||||
free_task(&t);
|
||||
mcp_reply_error(c, "cannot write the task file");
|
||||
return;
|
||||
}
|
||||
mcp_buf_init(&b);
|
||||
task_json(&b, &t);
|
||||
mcp_reply_json(c, b.p);
|
||||
mcp_buf_free(&b);
|
||||
free_task(&t);
|
||||
}
|
||||
|
||||
static void t_next(mcp_conn *c, const mcp_json *args, void *user)
|
||||
{
|
||||
const char *scope = mcp_arg_string(args, "scope");
|
||||
@@ -835,6 +898,19 @@ static const mcp_tool TOOLS[] = {
|
||||
"\"tags\":{\"type\":\"string\",\"description\":\"extra keywords, comma separated\"}},"
|
||||
"\"required\":[\"title\",\"goal\",\"scope\"]}",
|
||||
t_create },
|
||||
{ "task_update",
|
||||
"Correct a task's title, goal, scope, priority or tags. State changes stay "
|
||||
"with task_pause, task_resume, task_done and task_drop. Only the fields you "
|
||||
"pass are changed; pass tags=\"\" to clear tags.",
|
||||
"{\"type\":\"object\",\"properties\":{"
|
||||
"\"id\":{\"type\":\"string\"},"
|
||||
"\"title\":{\"type\":\"string\",\"description\":\"what to do, one line\"},"
|
||||
"\"goal\":{\"type\":\"string\",\"description\":\"why it exists\"},"
|
||||
"\"scope\":{\"type\":\"string\"},"
|
||||
"\"priority\":{\"type\":\"integer\"},"
|
||||
"\"tags\":{\"type\":\"string\",\"description\":\"empty string clears\"}},"
|
||||
"\"required\":[\"id\"]}",
|
||||
t_update },
|
||||
{ "task_next",
|
||||
"Claim the next pending task in a scope and mark it running. Use this instead "
|
||||
"of picking work by eye: it honours priority and never hands out a task "
|
||||
|
||||
+2
-2
@@ -67,7 +67,7 @@ res = run([
|
||||
by = {r.get("id"): r for r in res}
|
||||
|
||||
tools = by.get(1, {}).get("result", {}).get("tools", [])
|
||||
check("nine tools", len(tools) == 9, len(tools))
|
||||
check("ten tools", len(tools) == 10, len(tools))
|
||||
check("create says the scope is free-form",
|
||||
any(t["name"] == "task_create" and "free string you choose" in t["description"] for t in tools))
|
||||
check("notification got no answer", len(res) == 16,
|
||||
@@ -160,7 +160,7 @@ if ready:
|
||||
check("http: serverInfo is tasksd", init["result"]["serverInfo"]["name"] == "tasksd", init)
|
||||
check("http: a session id comes back", bool(sess), sess)
|
||||
st2, _, b2 = post({"jsonrpc": "2.0", "id": 2, "method": "tools/list"}, session=sess)
|
||||
check("http: tools/list over http", st2 == 200 and len(json.loads(b2)["result"]["tools"]) == 9)
|
||||
check("http: tools/list over http", st2 == 200 and len(json.loads(b2)["result"]["tools"]) == 10)
|
||||
st3, _, b3 = post(call(3, "task_scopes"), session=sess)
|
||||
scopes_http = json.loads(json.loads(b3)["result"]["content"][0]["text"])["scopes"]
|
||||
check("http: the SAME store answers", len(scopes_http) == 3, scopes_http)
|
||||
|
||||
Executable
+88
@@ -0,0 +1,88 @@
|
||||
#!/bin/sh
|
||||
# Prüft tasksd-http als CGI gegen einen eigenen tasksd auf freiem Port.
|
||||
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)
|
||||
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
|
||||
|
||||
TASKDIR="$DIR/store" ./tasksd --http "127.0.0.1:$PORT" >"$DIR/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 TASKSD_URL="http://127.0.0.1:$PORT/mcp"
|
||||
|
||||
fail() { echo "FAIL: $*" >&2; exit 1; }
|
||||
|
||||
cgi() {
|
||||
method=$1
|
||||
uri=$2
|
||||
qs=''
|
||||
case "$uri" in
|
||||
*\?*) qs=${uri#*\?} ;;
|
||||
esac
|
||||
if [ "$method" = POST ]; then
|
||||
body=$3
|
||||
printf '%s' "$body" | REQUEST_METHOD=POST REQUEST_URI="$uri" \
|
||||
QUERY_STRING="$qs" CONTENT_LENGTH=${#body} ./tasksd-http
|
||||
else
|
||||
REQUEST_METHOD=GET REQUEST_URI="$uri" QUERY_STRING="$qs" ./tasksd-http
|
||||
fi
|
||||
}
|
||||
|
||||
# 1. leere Ablage
|
||||
cgi GET / >"$DIR/out"
|
||||
grep -q 'No tasks match' "$DIR/out" || fail "empty list"
|
||||
|
||||
# 2. anlegen
|
||||
cgi POST /new 'title=Test+task&goal=Verify+the+CGI&scope=fleet/test&priority=5&tags=web' >"$DIR/out"
|
||||
ID=$(sed -n 's/.*<div class="msg">\(t[0-9]*\) .*/\1/p' "$DIR/out" | head -1)
|
||||
[ -n "$ID" ] || fail "no task id in create reply"
|
||||
|
||||
# 3. Liste und Detail
|
||||
cgi GET / >"$DIR/out"
|
||||
grep -q 'Test task' "$DIR/out" || fail "list title"
|
||||
grep -q "href=\"/t/$ID\"" "$DIR/out" || fail "list link"
|
||||
cgi GET "/t/$ID" >"$DIR/out"
|
||||
grep -q 'Test task' "$DIR/out" || fail "detail title"
|
||||
grep -q 's-pending' "$DIR/out" || fail "detail state"
|
||||
|
||||
# 4. pause / resume
|
||||
cgi POST "/t/$ID/pause" 'note=parked' >"$DIR/out"
|
||||
grep -q '<h1>paused</h1>' "$DIR/out" || fail "pause"
|
||||
cgi POST "/t/$ID/resume" 'note=go' >"$DIR/out"
|
||||
grep -q '<h1>running</h1>' "$DIR/out" || fail "resume"
|
||||
|
||||
# 5. bearbeiten (task_update)
|
||||
cgi GET "/t/$ID/edit" >"$DIR/out"
|
||||
grep -q 'name="goal"' "$DIR/out" || fail "edit form"
|
||||
cgi POST "/t/$ID/edit" 'title=Edited+task&goal=Now+edited&scope=fleet/test&priority=7&tags=' >"$DIR/out"
|
||||
grep -q 'Updated' "$DIR/out" || fail "update"
|
||||
cgi GET "/t/$ID" >"$DIR/out"
|
||||
grep -q 'Edited task' "$DIR/out" || fail "update visible"
|
||||
grep -q 'Now edited' "$DIR/out" || fail "goal update visible"
|
||||
|
||||
# 6. done und Filter
|
||||
cgi POST "/t/$ID/done" 'result=worked' >"$DIR/out"
|
||||
grep -q '<h1>done</h1>' "$DIR/out" || fail "done"
|
||||
cgi GET '/?state=done' >"$DIR/out"
|
||||
grep -q 'Edited task' "$DIR/out" || fail "state filter"
|
||||
|
||||
# 7. Scopes
|
||||
cgi GET /scopes >"$DIR/out"
|
||||
grep -q 'fleet/test' "$DIR/out" || fail "scopes"
|
||||
|
||||
echo "tasksd-http: ok"
|
||||
Vendored
+642
@@ -0,0 +1,642 @@
|
||||
/*
|
||||
* cgi.h — shared CGI helpers for the fleet's MCP daemons.
|
||||
*
|
||||
* Header-only. Include libmcp's mcp.h first (with MCP_IMPLEMENTATION in
|
||||
* exactly one translation unit); this file uses mcp_buf and the JSON parser.
|
||||
*
|
||||
* What it gives a CGI program:
|
||||
* - CGI environment: base URL, PATH_INFO/REQUEST_URI routing, POST body
|
||||
* - HTML 4.01 shell with a common navigation across the fleet UIs
|
||||
* - escaping, URL decoding, form parameters
|
||||
* - a minimal Markdown renderer
|
||||
* - cg_rpc(): one MCP tools/call over HTTP via curl
|
||||
*
|
||||
* Every daemon answers with result.content[0].text. Text replies stay text;
|
||||
* JSON replies are parsed by the caller with cg_rpc_json().
|
||||
*/
|
||||
#ifndef FLEET_CGI_H
|
||||
#define FLEET_CGI_H
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#if defined(__GNUC__)
|
||||
#define CG_UNUSED __attribute__((unused))
|
||||
#else
|
||||
#define CG_UNUSED
|
||||
#endif
|
||||
|
||||
/* ------------------------------------------------------------------ escaping */
|
||||
|
||||
static CG_UNUSED void cg_html(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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static CG_UNUSED void cg_html_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 CG_UNUSED char *cg_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;
|
||||
}
|
||||
|
||||
/* Copy one URL-encoded parameter out of qs. malloc'd; caller frees. */
|
||||
static CG_UNUSED char *cg_param(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';
|
||||
cg_url_decode(out);
|
||||
return out;
|
||||
}
|
||||
p = strchr(p, '&');
|
||||
if (p != NULL)
|
||||
p++;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------- env */
|
||||
|
||||
static CG_UNUSED const char *cg_method(void)
|
||||
{
|
||||
const char *m = getenv("REQUEST_METHOD");
|
||||
|
||||
return (m != NULL && *m != '\0') ? m : "GET";
|
||||
}
|
||||
|
||||
/* "" at the root, otherwise SCRIPT_NAME ("/cgi-bin/memoryd-http"). */
|
||||
static CG_UNUSED const char *cg_base(void)
|
||||
{
|
||||
static char base[256];
|
||||
static int done;
|
||||
|
||||
if (!done) {
|
||||
const char *s = getenv("SCRIPT_NAME");
|
||||
|
||||
base[0] = '\0';
|
||||
if (s != NULL && *s != '\0' && strcmp(s, "/") != 0)
|
||||
snprintf(base, sizeof(base), "%s", s);
|
||||
done = 1;
|
||||
}
|
||||
return base;
|
||||
}
|
||||
|
||||
/* Percent-encode a query-string value (letters, digits and -_.~/ stay). */
|
||||
static CG_UNUSED void cg_urlenc(const char *s)
|
||||
{
|
||||
for (; s != NULL && *s; s++) {
|
||||
unsigned char c = (unsigned char)*s;
|
||||
|
||||
if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~' ||
|
||||
c == '/')
|
||||
putchar(c);
|
||||
else
|
||||
printf("%%%02X", c);
|
||||
}
|
||||
}
|
||||
|
||||
/* Print an internal link target (base + suffix). */
|
||||
static CG_UNUSED void cg_link(const char *suffix)
|
||||
{
|
||||
fputs(cg_base(), stdout);
|
||||
fputs(suffix, stdout);
|
||||
}
|
||||
|
||||
/* PATH_INFO when the server provides it, else REQUEST_URI minus base. */
|
||||
static CG_UNUSED void cg_route(char *path, size_t pathlen, char *query, size_t querylen)
|
||||
{
|
||||
const char *uri = getenv("REQUEST_URI");
|
||||
const char *pi = getenv("PATH_INFO");
|
||||
const char *qs = getenv("QUERY_STRING");
|
||||
const char *base = cg_base();
|
||||
size_t i;
|
||||
|
||||
if (uri == NULL)
|
||||
uri = "/";
|
||||
if (pi != NULL && *pi != '\0') {
|
||||
uri = pi;
|
||||
} else if (*base != '\0' && strncmp(uri, base, strlen(base)) == 0) {
|
||||
uri += strlen(base);
|
||||
if (*uri == '\0')
|
||||
uri = "/";
|
||||
}
|
||||
snprintf(path, pathlen, "%s", uri);
|
||||
for (i = 0; path[i] != '\0' && path[i] != '?'; i++)
|
||||
;
|
||||
if (path[i] == '?') {
|
||||
if (querylen > 0)
|
||||
snprintf(query, querylen, "%s", path + i + 1);
|
||||
path[i] = '\0';
|
||||
} else if (querylen > 0) {
|
||||
query[0] = '\0';
|
||||
}
|
||||
if (querylen > 0 && query[0] == '\0' && qs != NULL && *qs != '\0')
|
||||
snprintf(query, querylen, "%s", qs);
|
||||
}
|
||||
|
||||
static CG_UNUSED char *cg_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 > 1024 * 1024)
|
||||
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;
|
||||
}
|
||||
|
||||
static CG_UNUSED void cg_header(void)
|
||||
{
|
||||
printf("Content-Type: text/html; charset=utf-8\r\n\r\n");
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------- chrome */
|
||||
|
||||
struct cg_app {
|
||||
const char *file;
|
||||
const char *label;
|
||||
};
|
||||
|
||||
static CG_UNUSED const struct cg_app cg_apps[] = {
|
||||
{ "wikid-http", "Wiki" },
|
||||
{ "memoryd-http", "Memories" },
|
||||
{ "skilld-http", "Skills" },
|
||||
{ "tasksd-http", "Tasks" },
|
||||
};
|
||||
|
||||
static CG_UNUSED void cg_nav(const char *active)
|
||||
{
|
||||
char dir[256] = "";
|
||||
const char *base = cg_base();
|
||||
const char *slash = strrchr(base, '/');
|
||||
const char *here = slash != NULL ? slash + 1 : base;
|
||||
|
||||
if (slash != NULL) {
|
||||
size_t n = (size_t)(slash - base) + 1;
|
||||
|
||||
if (n >= sizeof(dir))
|
||||
n = sizeof(dir) - 1;
|
||||
memcpy(dir, base, n);
|
||||
dir[n] = '\0';
|
||||
}
|
||||
fputs("<div class=\"topnav\">", stdout);
|
||||
for (size_t i = 0; i < sizeof(cg_apps) / sizeof(cg_apps[0]); i++) {
|
||||
int on = active != NULL && strcmp(active, cg_apps[i].file) == 0;
|
||||
|
||||
fputs("<a href=\"", stdout);
|
||||
fputs(dir, stdout);
|
||||
fputs(cg_apps[i].file, stdout);
|
||||
fputs("\">", stdout);
|
||||
if (on)
|
||||
fputs("<b>", stdout);
|
||||
fputs(cg_apps[i].label, stdout);
|
||||
if (on)
|
||||
fputs("</b>", stdout);
|
||||
fputs("</a> ", stdout);
|
||||
}
|
||||
(void)here;
|
||||
fputs("</div>\n", stdout);
|
||||
}
|
||||
|
||||
static CG_UNUSED const char *cg_css =
|
||||
"body{font-family:serif;margin:1.5em;max-width:76em;background:#fff;color:#222}\n"
|
||||
"h1,h2,h3,h4{margin-top:1.1em;margin-bottom:0.35em}\n"
|
||||
"a{color:#0055aa;text-decoration:none}\n"
|
||||
"a:hover{text-decoration:underline}\n"
|
||||
"code{background:#f4f4f4;padding:0.08em 0.25em;font-size:0.92em}\n"
|
||||
"pre{background:#f7f7f7;border:1px solid #ddd;padding:0.7em;overflow:auto;font-size:0.9em}\n"
|
||||
"textarea{width:100%;max-width:72em;font-family:monospace;font-size:0.95em}\n"
|
||||
"input[type=text]{max-width:62em;width:100%;font-family:monospace}\n"
|
||||
".topnav{border-bottom:2px solid #ccc;padding-bottom:0.45em;margin-bottom:1.2em}\n"
|
||||
".topnav a{margin-right:1.2em;font-size:1.05em}\n"
|
||||
".meta{color:#666;font-size:0.86em}\n"
|
||||
".item{border-bottom:1px solid #e6e6e6;padding:0.65em 0}\n"
|
||||
".head{font-size:0.95em}\n"
|
||||
".scope{color:#0055aa}\n"
|
||||
".actions{margin-top:0.35em;font-size:0.9em}\n"
|
||||
".actions a,.actions form{display:inline-block;margin-right:0.9em}\n"
|
||||
".actions input[type=submit]{font-size:0.85em}\n"
|
||||
"form.inline{display:inline}\n"
|
||||
"table.list{border-collapse:collapse;width:100%;margin:0.7em 0}\n"
|
||||
"table.list td,table.list th{border-bottom:1px solid #ddd;padding:0.3em 0.55em;text-align:left;vertical-align:top}\n"
|
||||
"table.list th{background:#f2f2f2}\n"
|
||||
".state{display:inline-block;padding:0.02em 0.45em;border:1px solid #999;font-size:0.78em;text-transform:uppercase;letter-spacing:0.03em}\n"
|
||||
".s-pending{background:#fff;color:#333}\n"
|
||||
".s-running{background:#e6f2ff;color:#004c99}\n"
|
||||
".s-paused{background:#fff6d8;color:#7a5c00}\n"
|
||||
".s-done{background:#e8f8e8;color:#1a661a}\n"
|
||||
".s-dropped{background:#f0f0f0;color:#777}\n"
|
||||
".msg{border-left:4px solid #9c9;background:#f4faf4;padding:0.5em 0.8em;margin:0.8em 0}\n"
|
||||
".err{border-left:4px solid #c66;background:#fdf4f4;padding:0.5em 0.8em;margin:0.8em 0;color:#800}\n";
|
||||
|
||||
static CG_UNUSED void cg_head(const char *active, const char *title)
|
||||
{
|
||||
fputs("<!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>", stdout);
|
||||
cg_html(title);
|
||||
fputs("</title>\n<style type=\"text/css\">", stdout);
|
||||
fputs(cg_css, stdout);
|
||||
fputs("</style>\n</head>\n<body>\n", stdout);
|
||||
cg_nav(active);
|
||||
}
|
||||
|
||||
static CG_UNUSED void cg_foot(void)
|
||||
{
|
||||
fputs("</body>\n</html>\n", stdout);
|
||||
}
|
||||
|
||||
static CG_UNUSED void cg_error_page(const char *active, const char *title, const char *msg)
|
||||
{
|
||||
cg_head(active, title);
|
||||
fputs("<h1>", stdout);
|
||||
cg_html(title);
|
||||
fputs("</h1>\n<div class=\"err\">", stdout);
|
||||
cg_html(msg != NULL ? msg : "unknown error");
|
||||
fputs("</div>\n", stdout);
|
||||
cg_foot();
|
||||
}
|
||||
|
||||
static CG_UNUSED void cg_pre(const char *s)
|
||||
{
|
||||
fputs("<pre>", stdout);
|
||||
cg_html(s != NULL ? s : "");
|
||||
fputs("</pre>\n", stdout);
|
||||
}
|
||||
|
||||
static CG_UNUSED void cg_time(long t)
|
||||
{
|
||||
time_t tt = (time_t)t;
|
||||
struct tm tm;
|
||||
char buf[32];
|
||||
|
||||
if (t <= 0) {
|
||||
fputs("-", stdout);
|
||||
return;
|
||||
}
|
||||
if (localtime_r(&tt, &tm) == NULL) {
|
||||
printf("%ld", t);
|
||||
return;
|
||||
}
|
||||
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M", &tm);
|
||||
fputs(buf, stdout);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------- markdown */
|
||||
|
||||
static CG_UNUSED void cg_put_esc(char c)
|
||||
{
|
||||
switch (c) {
|
||||
case '&': fputs("&", stdout); break;
|
||||
case '<': fputs("<", stdout); break;
|
||||
case '>': fputs(">", stdout); break;
|
||||
case '"': fputs(""", stdout); break;
|
||||
default: putchar(c); break;
|
||||
}
|
||||
}
|
||||
|
||||
static CG_UNUSED void cg_md_inline(const char **pp)
|
||||
{
|
||||
const char *p = *pp;
|
||||
|
||||
while (*p != '\0' && *p != '\n') {
|
||||
if (*p == '*' && p[1] == '*') {
|
||||
fputs("<b>", stdout);
|
||||
p += 2;
|
||||
while (*p != '\0' && !(*p == '*' && p[1] == '*') && *p != '\n')
|
||||
cg_put_esc(*p++);
|
||||
if (*p == '*')
|
||||
p++;
|
||||
fputs("</b>", stdout);
|
||||
} else if (*p == '*') {
|
||||
fputs("<i>", stdout);
|
||||
p++;
|
||||
while (*p != '\0' && *p != '*' && *p != '\n')
|
||||
cg_put_esc(*p++);
|
||||
if (*p == '*')
|
||||
p++;
|
||||
fputs("</i>", stdout);
|
||||
} else if (*p == '`') {
|
||||
if (p[1] == '`') {
|
||||
p += 2; /* empty span, emit nothing */
|
||||
continue;
|
||||
}
|
||||
fputs("<code>", stdout);
|
||||
p++;
|
||||
while (*p != '\0' && *p != '`' && *p != '\n')
|
||||
cg_put_esc(*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 CG_UNUSED void cg_md(const char *text)
|
||||
{
|
||||
const char *p = text;
|
||||
int in_para = 0, in_list = 0;
|
||||
|
||||
if (text == NULL)
|
||||
return;
|
||||
while (*p != '\0') {
|
||||
if (strncmp(p, "```", 3) == 0) {
|
||||
const char *eol = strchr(p, '\n');
|
||||
const char *code, *end, *stop;
|
||||
|
||||
if (eol == NULL) {
|
||||
p += 3;
|
||||
continue;
|
||||
}
|
||||
code = eol + 1;
|
||||
stop = strstr(code, "\n```");
|
||||
end = stop != NULL ? stop : code + strlen(code);
|
||||
if (in_para) { fputs("</p>\n", stdout); in_para = 0; }
|
||||
if (in_list) { fputs("</ul>\n", stdout); in_list = 0; }
|
||||
fputs("<pre>", stdout);
|
||||
cg_html_n(code, (size_t)(end - code));
|
||||
fputs("</pre>\n", stdout);
|
||||
if (stop != NULL) {
|
||||
p = stop + 4;
|
||||
while (*p == '\n')
|
||||
p++;
|
||||
} else {
|
||||
p = end;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
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);
|
||||
cg_md_inline(&p);
|
||||
printf("</h%d>\n", level);
|
||||
continue;
|
||||
}
|
||||
if (*p == '-' && (p[1] == ' ' || p[1] == '\t')) {
|
||||
if (in_para) { fputs("</p>\n", stdout); in_para = 0; }
|
||||
if (!in_list) { fputs("<ul>\n", stdout); in_list = 1; }
|
||||
fputs("<li>", stdout);
|
||||
p += 2;
|
||||
cg_md_inline(&p);
|
||||
fputs("</li>\n", stdout);
|
||||
continue;
|
||||
}
|
||||
if (strncmp(p, "---", 3) == 0 && (p[3] == '\n' || p[3] == '\0')) {
|
||||
if (in_para) { fputs("</p>\n", stdout); in_para = 0; }
|
||||
if (in_list) { fputs("</ul>\n", stdout); in_list = 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;
|
||||
} else if (in_list && (p[1] == '\n' || p[1] == '\0')) {
|
||||
fputs("</ul>\n", stdout);
|
||||
in_list = 0;
|
||||
}
|
||||
p++;
|
||||
continue;
|
||||
}
|
||||
if (!in_para) {
|
||||
if (in_list) { fputs("</ul>\n", stdout); in_list = 0; }
|
||||
fputs("<p>", stdout);
|
||||
in_para = 1;
|
||||
}
|
||||
cg_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);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ JSON args */
|
||||
|
||||
static CG_UNUSED void cg_args_add_str(mcp_buf *b, int *first, const char *key,
|
||||
const char *val)
|
||||
{
|
||||
if (val == NULL)
|
||||
return;
|
||||
if (!*first)
|
||||
mcp_buf_puts(b, ",");
|
||||
*first = 0;
|
||||
mcp_buf_printf(b, "\"%s\":\"", key);
|
||||
mcp_json_escape(b, val);
|
||||
mcp_buf_puts(b, "\"");
|
||||
}
|
||||
|
||||
static CG_UNUSED void cg_args_add_int(mcp_buf *b, int *first, const char *key, int val)
|
||||
{
|
||||
if (!*first)
|
||||
mcp_buf_puts(b, ",");
|
||||
*first = 0;
|
||||
mcp_buf_printf(b, "\"%s\":%d", key, val);
|
||||
}
|
||||
|
||||
static CG_UNUSED void cg_args_add_bool(mcp_buf *b, int *first, const char *key, int val)
|
||||
{
|
||||
if (!*first)
|
||||
mcp_buf_puts(b, ",");
|
||||
*first = 0;
|
||||
mcp_buf_printf(b, "\"%s\":%s", key, val ? "true" : "false");
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------- RPC */
|
||||
|
||||
static CG_UNUSED void cg_shell_quote(mcp_buf *b, const char *s)
|
||||
{
|
||||
mcp_buf_puts(b, "'");
|
||||
for (; s != NULL && *s; s++) {
|
||||
if (*s == '\'')
|
||||
mcp_buf_puts(b, "'\\''");
|
||||
else
|
||||
mcp_buf_add(b, s, 1);
|
||||
}
|
||||
mcp_buf_puts(b, "'");
|
||||
}
|
||||
|
||||
/* One tools/call. Returns the text reply (malloc'd) or NULL. */
|
||||
static CG_UNUSED char *cg_rpc(const char *env, const char *dflt, const char *tool,
|
||||
const char *args)
|
||||
{
|
||||
mcp_buf body, cmd, raw;
|
||||
const char *url;
|
||||
FILE *fp;
|
||||
char chunk[4096];
|
||||
size_t got;
|
||||
mcp_json *v;
|
||||
const mcp_json *result, *content, *first;
|
||||
const char *text;
|
||||
char *out = NULL;
|
||||
|
||||
url = (env != NULL) ? getenv(env) : NULL;
|
||||
if (url == NULL || *url == '\0')
|
||||
url = dflt;
|
||||
|
||||
mcp_buf_init(&body);
|
||||
mcp_buf_puts(&body, "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"tools/call\","
|
||||
"\"params\":{\"name\":\"");
|
||||
mcp_json_escape(&body, tool);
|
||||
mcp_buf_puts(&body, "\",\"arguments\":");
|
||||
mcp_buf_puts(&body, (args != NULL && *args != '\0') ? args : "{}");
|
||||
mcp_buf_puts(&body, "}}");
|
||||
|
||||
mcp_buf_init(&cmd);
|
||||
mcp_buf_puts(&cmd, "curl -sS -m 120 -X POST "
|
||||
"-H 'Content-Type: application/json' -d ");
|
||||
cg_shell_quote(&cmd, body.p != NULL ? body.p : "{}");
|
||||
mcp_buf_puts(&cmd, " ");
|
||||
cg_shell_quote(&cmd, url);
|
||||
mcp_buf_puts(&cmd, " 2>/dev/null");
|
||||
mcp_buf_free(&body);
|
||||
|
||||
fp = popen(cmd.p != NULL ? cmd.p : "true", "r");
|
||||
mcp_buf_free(&cmd);
|
||||
if (fp == NULL)
|
||||
return NULL;
|
||||
mcp_buf_init(&raw);
|
||||
while ((got = fread(chunk, 1, sizeof(chunk), fp)) > 0) {
|
||||
mcp_buf_add(&raw, chunk, got);
|
||||
if (raw.p == NULL)
|
||||
break;
|
||||
}
|
||||
pclose(fp);
|
||||
if (raw.p == NULL) {
|
||||
mcp_buf_free(&raw);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
v = mcp_parse(raw.p);
|
||||
mcp_buf_free(&raw);
|
||||
if (v == NULL)
|
||||
return NULL;
|
||||
result = mcp_arg(v, "result");
|
||||
content = mcp_arg(result, "content");
|
||||
first = mcp_array_at(content, 0);
|
||||
text = mcp_arg_string(first, "text");
|
||||
if (text == NULL) {
|
||||
const mcp_json *err = mcp_arg(v, "error");
|
||||
|
||||
text = mcp_arg_string(err, "message");
|
||||
}
|
||||
if (text != NULL)
|
||||
out = strdup(text);
|
||||
mcp_json_free(v);
|
||||
return out;
|
||||
}
|
||||
|
||||
/* tools/call whose text reply is JSON. Caller frees with mcp_json_free(). */
|
||||
static CG_UNUSED mcp_json *cg_rpc_json(const char *env, const char *dflt,
|
||||
const char *tool, const char *args)
|
||||
{
|
||||
char *text = cg_rpc(env, dflt, tool, args);
|
||||
mcp_json *v;
|
||||
|
||||
if (text == NULL)
|
||||
return NULL;
|
||||
v = mcp_parse(text);
|
||||
free(text);
|
||||
return v;
|
||||
}
|
||||
|
||||
#endif /* FLEET_CGI_H */
|
||||
Reference in new issue
Block a user