Mercurial > sci
view http.c @ 7:a7e36b32e20a
make: tests need jansson
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 23 Jun 2021 11:45:00 +0200 |
parents | 215c0c3b3609 |
children | 3051ef92173a |
line wrap: on
line source
#include <sys/types.h> #include <assert.h> #include <stdarg.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <kcgi.h> #include "http.h" #include "log.h" #include "page.h" #include "page-api-jobs.h" #include "page-api-projects.h" #include "page-api-workers.h" #include "req.h" enum page { PAGE_API, PAGE_LAST /* Not used. */ }; static void dispatch_api(struct kreq *req) { static const struct { const char *prefix; void (*handler)(struct kreq *); } apis[] = { { "v1/jobs", page_api_v1_jobs }, { "v1/projects", page_api_v1_projects }, { "v1/workers", page_api_v1_workers }, { NULL, NULL } }; for (size_t i = 0; apis[i].prefix; ++i) if (strncmp(req->path, apis[i].prefix, strlen(apis[i].prefix)) == 0) return apis[i].handler(req); page(req, NULL, KHTTP_404, KMIME_TEXT_HTML, "pages/404.html"); } static const char *pages[] = { [PAGE_API] = "api" }; static void (*handlers[])(struct kreq *req) = { [PAGE_API] = dispatch_api }; static void process(struct kreq *req) { assert(req); log_debug("http: accessing page '%s'", req->path); if (req->page == PAGE_LAST) page(req, NULL, KHTTP_404, KMIME_TEXT_HTML, "pages/404.html"); else handlers[req->page](req); } void http_fcgi_run(void) { struct kreq req; struct kfcgi *fcgi; if (khttp_fcgi_init(&fcgi, NULL, 0, pages, PAGE_LAST, 0) != KCGI_OK) return; while (khttp_fcgi_parse(fcgi, &req) == KCGI_OK) process(&req); khttp_fcgi_free(fcgi); } void http_cgi_run(void) { struct kreq req; if (khttp_parse(&req, NULL, 0, pages, PAGE_LAST, 0) == KCGI_OK) process(&req); }