Mercurial > sci
comparison http.c @ 2:5fa3d2f479b2
sci: initial upload support
author | David Demelier <markand@malikania.fr> |
---|---|
date | Thu, 10 Jun 2021 10:39:21 +0200 |
parents | |
children | 215c0c3b3609 |
comparison
equal
deleted
inserted
replaced
1:5afdb14df924 | 2:5fa3d2f479b2 |
---|---|
1 #include <sys/types.h> | |
2 #include <assert.h> | |
3 #include <stdarg.h> | |
4 #include <stdlib.h> | |
5 #include <stdio.h> | |
6 #include <string.h> | |
7 | |
8 #include <kcgi.h> | |
9 | |
10 #include "http.h" | |
11 #include "log.h" | |
12 #include "page.h" | |
13 #include "page-api-jobs.h" | |
14 #include "page-api-script.h" | |
15 #include "req.h" | |
16 | |
17 enum page { | |
18 PAGE_API, | |
19 PAGE_LAST /* Not used. */ | |
20 }; | |
21 | |
22 static void | |
23 dispatch_api(struct kreq *req) | |
24 { | |
25 static const struct { | |
26 const char *prefix; | |
27 void (*handler)(struct kreq *); | |
28 } apis[] = { | |
29 { "v1/jobs", page_api_v1_jobs }, | |
30 { "v1/script", page_api_v1_script }, | |
31 { NULL, NULL } | |
32 }; | |
33 | |
34 if (req_connect(VARDIR "/run/sci.sock") < 0) { | |
35 page(req, NULL, KHTTP_500, KMIME_TEXT_HTML, "pages/500.html"); | |
36 return; | |
37 } | |
38 | |
39 for (size_t i = 0; apis[i].prefix; ++i) { | |
40 if (strncmp(req->path, apis[i].prefix, strlen(apis[i].prefix)) == 0) { | |
41 apis[i].handler(req); | |
42 goto finish; | |
43 } | |
44 } | |
45 | |
46 page(req, NULL, KHTTP_404, KMIME_TEXT_HTML, "pages/404.html"); | |
47 | |
48 finish: | |
49 req_finish(); | |
50 } | |
51 | |
52 static const char *pages[] = { | |
53 [PAGE_API] = "api" | |
54 }; | |
55 | |
56 static void (*handlers[])(struct kreq *req) = { | |
57 [PAGE_API] = dispatch_api | |
58 }; | |
59 | |
60 static void | |
61 process(struct kreq *req) | |
62 { | |
63 assert(req); | |
64 | |
65 log_debug("http: accessing page '%s'", req->path); | |
66 | |
67 if (req->page == PAGE_LAST) | |
68 page(req, NULL, KHTTP_404, KMIME_TEXT_HTML, "pages/404.html"); | |
69 else | |
70 handlers[req->page](req); | |
71 } | |
72 | |
73 void | |
74 http_fcgi_run(void) | |
75 { | |
76 struct kreq req; | |
77 struct kfcgi *fcgi; | |
78 | |
79 if (khttp_fcgi_init(&fcgi, NULL, 0, pages, PAGE_LAST, 0) != KCGI_OK) | |
80 return; | |
81 | |
82 while (khttp_fcgi_parse(fcgi, &req) == KCGI_OK) | |
83 process(&req); | |
84 | |
85 khttp_fcgi_free(fcgi); | |
86 } | |
87 | |
88 void | |
89 http_cgi_run(void) | |
90 { | |
91 struct kreq req; | |
92 | |
93 if (khttp_parse(&req, NULL, 0, pages, PAGE_LAST, 0) == KCGI_OK) | |
94 process(&req); | |
95 } |