view page-api-script.c @ 2:5fa3d2f479b2

sci: initial upload support
author David Demelier <markand@malikania.fr>
date Thu, 10 Jun 2021 10:39:21 +0200
parents
children
line wrap: on
line source

#include <sys/types.h>
#include <assert.h>
#include <stdarg.h>
#include <stdint.h>

#include <kcgi.h>
#include <jansson.h>

#include "config.h"
#include "page.h"
#include "req.h"
#include "util.h"

static void
content(struct kreq *r, const char *code)
{
	json_t *doc;

	doc = json_object();
	json_object_set(doc, "code", json_string(code));
	khttp_puts(r, json_dumps(doc, JSON_COMPACT));
	json_decref(doc);
}

static void
get(struct kreq *r)
{
	struct req req;
	char script[SCI_MSG_MAX];
	const char *project = util_basename(r->path);

	if ((req = req_script_get(project, script, sizeof (script))).status)
		page(r, NULL, KHTTP_500, KMIME_APP_JSON, NULL);
	else {
		khttp_head(r, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[KMIME_APP_JSON]);
		khttp_head(r, kresps[KRESP_STATUS], "%s", khttps[KHTTP_200]);
		khttp_body(r);
		content(r, script);
		khttp_free(r);
	}
}

void
page_api_v1_script(struct kreq *r)
{
	assert(r);

	switch (r->method) {
	case KMETHOD_GET:
		get(r);
		break;
	default:
		page(r, NULL, KHTTP_400, KMIME_APP_JSON, NULL);
		break;
	}
}