Mercurial > sci
comparison scid/http.c @ 18:600204c31bf0
misc: refactor
author | David Demelier <markand@malikania.fr> |
---|---|
date | Tue, 12 Jul 2022 20:20:51 +0200 |
parents | http.c@3051ef92173a |
children | f98ea578b1ef |
comparison
equal
deleted
inserted
replaced
17:40fe70256fb0 | 18:600204c31bf0 |
---|---|
1 /* | |
2 * http.c -- HTTP parsing and rendering | |
3 * | |
4 * Copyright (c) 2021 David Demelier <markand@malikania.fr> | |
5 * | |
6 * Permission to use, copy, modify, and/or distribute this software for any | |
7 * purpose with or without fee is hereby granted, provided that the above | |
8 * copyright notice and this permission notice appear in all copies. | |
9 * | |
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
17 */ | |
18 | |
19 #include <sys/types.h> | |
20 #include <assert.h> | |
21 #include <stdarg.h> | |
22 #include <stdlib.h> | |
23 #include <stdio.h> | |
24 #include <string.h> | |
25 | |
26 #include <kcgi.h> | |
27 | |
28 #include "http.h" | |
29 #include "log.h" | |
30 #include "page.h" | |
31 #include "page-api-jobs.h" | |
32 #include "page-api-projects.h" | |
33 #include "page-api-workers.h" | |
34 | |
35 enum page { | |
36 PAGE_API, | |
37 PAGE_LAST /* Not used. */ | |
38 }; | |
39 | |
40 static void | |
41 dispatch_api(struct kreq *req) | |
42 { | |
43 static const struct { | |
44 const char *prefix; | |
45 void (*handler)(struct kreq *); | |
46 } apis[] = { | |
47 { "v1/jobs", page_api_v1_jobs }, | |
48 { "v1/projects", page_api_v1_projects }, | |
49 { "v1/workers", page_api_v1_workers }, | |
50 { NULL, NULL } | |
51 }; | |
52 | |
53 for (size_t i = 0; apis[i].prefix; ++i) | |
54 if (strncmp(req->path, apis[i].prefix, strlen(apis[i].prefix)) == 0) | |
55 return apis[i].handler(req); | |
56 | |
57 page(req, NULL, KHTTP_404, KMIME_TEXT_HTML, "pages/404.html"); | |
58 } | |
59 | |
60 static const char *pages[] = { | |
61 [PAGE_API] = "api" | |
62 }; | |
63 | |
64 static void (*handlers[])(struct kreq *req) = { | |
65 [PAGE_API] = dispatch_api | |
66 }; | |
67 | |
68 static void | |
69 process(struct kreq *req) | |
70 { | |
71 assert(req); | |
72 | |
73 log_debug("http: accessing page '%s'", req->path); | |
74 | |
75 if (req->page == PAGE_LAST) | |
76 page(req, NULL, KHTTP_404, KMIME_TEXT_HTML, "pages/404.html"); | |
77 else | |
78 handlers[req->page](req); | |
79 } | |
80 | |
81 void | |
82 http_fcgi_run(void) | |
83 { | |
84 struct kreq req; | |
85 struct kfcgi *fcgi; | |
86 | |
87 if (khttp_fcgi_init(&fcgi, NULL, 0, pages, PAGE_LAST, 0) != KCGI_OK) | |
88 return; | |
89 | |
90 while (khttp_fcgi_parse(fcgi, &req) == KCGI_OK) | |
91 process(&req); | |
92 | |
93 khttp_fcgi_free(fcgi); | |
94 } | |
95 | |
96 void | |
97 http_cgi_run(void) | |
98 { | |
99 struct kreq req; | |
100 | |
101 if (khttp_parse(&req, NULL, 0, pages, PAGE_LAST, 0) == KCGI_OK) | |
102 process(&req); | |
103 } |