-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathsample.c
340 lines (301 loc) · 8.46 KB
/
sample.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
* Copyright (c) Kristaps Dzonsons <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h> /* size_t, ssize_t */
#include <stdarg.h> /* va_list */
#include <stddef.h> /* NULL */
#include <stdint.h> /* int64_t */
#include <stdlib.h>
#include <string.h> /* memset */
#include "kcgi.h"
#include "kcgihtml.h"
/*
* Simple CGI application. Compile it with `make samples` (or using gmake) and
* install it into your web server's /cgi-bin. The "template.xml" file should
* be in the /cgi-bin directory as well and readable by the server process.
* (Obviously this is just for a sample.)
*
* Assuming localhost/cgi-bin, the script is localhost/cgi-bin/sample. The
* pages recognised are:
*
* - /cgi-bin/sample/index.html
* - /cgi-bin/sample/template.html
* - /cgi-bin/sample/senddata.html
*
* See the sendindex et al. functions for what these do.
*/
/* Recognised page requests. See pages[]. */
enum page {
PAGE_INDEX,
PAGE_TEMPLATE,
PAGE_SENDDATA,
PAGE__MAX
};
/*
* All of the keys (input field names) we accept. The key names are in the
* "keys" array. See sendindex() for how these are used.
*/
enum key {
KEY_INTEGER,
KEY_FILE,
KEY_PAGECOUNT,
KEY_PAGESIZE,
KEY__MAX
};
/*
* The elements in our template file. The element key names are in the "templs"
* array. See sendtemplate() for how this is used.
*/
enum templ {
TEMPL_TITLE,
TEMPL_NAME,
TEMPL_REMOTE_ADDR,
TEMPL__MAX
};
/*
* We need a structure because we can't get the "r" from the request. This is
* used by our template callback.
*/
struct tstrct {
struct khtmlreq req;
struct kreq *r;
};
/*
* We'll use this to route pages by creating an array indexed by our page. Then
* when the page is parsed, we'll route directly into it.
*/
typedef void (*disp)(struct kreq *);
static void senddata(struct kreq *);
static void sendindex(struct kreq *);
static void sendtemplate(struct kreq *);
static const disp disps[PAGE__MAX] = {
sendindex, /* PAGE_INDEX */
sendtemplate, /* PAGE_TEMPLATE */
senddata, /* PAGE_SENDDATA */
};
static const struct kvalid keys[KEY__MAX] = {
{ kvalid_int, "integer" }, /* KEY_INTEGER */
{ NULL, "file" }, /* KEY_FILE */
{ kvalid_uint, "count" }, /* KEY_PAGECOUNT */
{ kvalid_uint, "size" }, /* KEY_PAGESIZE */
};
/*
* Template key names (as in @@TITLE@@ in the file).
*/
static const char *const templs[TEMPL__MAX] = {
"title", /* TEMPL_TITLE */
"name", /* TEMPL_NAME */
"remote_addr", /* TEMPL_REMOTE_ADDR */
};
/*
* Page names (as in the URL component) mapped from the first name part of
* requests, e.g., /sample.cgi/index.html -> index -> PAGE_INDEX.
*/
static const char *const pages[PAGE__MAX] = {
"index", /* PAGE_INDEX */
"template", /* PAGE_TEMPLATE */
"senddata" /* PAGE_SENDDATA */
};
/*
* Open an HTTP response with a status code and a particular content-type, then
* open the HTTP content body. You can call khttp_head(3) before this: CGI
* doesn't dictate any particular header order.
*/
static void
resp_open(struct kreq *req, enum khttp http)
{
enum kmime mime;
/*
* If we've been sent an unknown suffix like '.foo', we won't know what
* it is. Default to an octet-stream response.
*/
if ((mime = req->mime) == KMIME__MAX)
mime = KMIME_APP_OCTET_STREAM;
khttp_head(req, kresps[KRESP_STATUS], "%s", khttps[http]);
khttp_head(req, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[mime]);
khttp_body(req);
}
/*
* Callback for filling in a particular template part. Let's just be simple for
* simplicity's sake.
*/
static int
template(size_t key, void *arg)
{
struct tstrct *p = arg;
switch (key) {
case TEMPL_TITLE:
khtml_puts(&p->req, "title");
break;
case TEMPL_NAME:
khtml_puts(&p->req, "name");
break;
case TEMPL_REMOTE_ADDR:
khtml_puts(&p->req, p->r->remote);
break;
default:
return 0;
}
return 1;
}
/*
* Demonstrates how to use templates. Returns HTTP 200 and the template
* content.
*/
static void
sendtemplate(struct kreq *req)
{
struct ktemplate t;
struct tstrct p;
memset(&t, 0, sizeof(struct ktemplate));
memset(&p, 0, sizeof(struct tstrct));
p.r = req;
t.key = templs;
t.keysz = TEMPL__MAX;
t.arg = &p;
t.cb = template;
resp_open(req, KHTTP_200);
khtml_open(&p.req, req, 0);
khttp_template(req, &t, "template.xml");
khtml_close(&p.req);
}
/*
* Send a random amount of data. Requires KEY_PAGECOUNT (optional),
* KEY_PAGESIZE (optional). Page count is the number of times we flush a page
* (with the given size) to the wire. Returns HTTP 200 and the random data.
*/
static void
senddata(struct kreq *req)
{
int64_t i, j, nm, sz;
char *buf;
nm = 1024 * 1024;
if (req->fieldmap[KEY_PAGECOUNT] != NULL)
nm = req->fieldmap[KEY_PAGECOUNT]->parsed.i;
if (nm == 0)
nm = 1;
sz = 1;
if (req->fieldmap[KEY_PAGESIZE] != NULL)
sz = req->fieldmap[KEY_PAGESIZE]->parsed.i;
if (sz == 0 || (uint64_t)sz > SIZE_MAX)
sz = 1;
buf = kmalloc(sz);
resp_open(req, KHTTP_200);
for (i = 0; i < nm; i++) {
for (j = 0; j < sz; j++)
#ifndef __linux__
buf[j] = 65 + arc4random_uniform(24);
#else
buf[j] = 65 + (random() % 24);
#endif
khttp_write(req, buf, sz);
}
free(buf);
}
/*
* Demonstrates how to use GET and POST forms and building with the HTML builder
* functions. Returns HTTP 200 and HTML content.
*/
static void
sendindex(struct kreq *req)
{
char *page;
struct khtmlreq r;
const char *cp;
cp = req->fieldmap[KEY_INTEGER] == NULL ?
"" : req->fieldmap[KEY_INTEGER]->val;
kasprintf(&page, "%s/%s", req->pname, pages[PAGE_INDEX]);
resp_open(req, KHTTP_200);
khtml_open(&r, req, 0);
khtml_elem(&r, KELEM_DOCTYPE);
khtml_elem(&r, KELEM_HTML);
khtml_elem(&r, KELEM_HEAD);
khtml_elem(&r, KELEM_TITLE);
khtml_puts(&r, "Welcome!");
khtml_closeelem(&r, 2);
khtml_elem(&r, KELEM_BODY);
khtml_puts(&r, "Welcome!");
khtml_attr(&r, KELEM_FORM,
KATTR_METHOD, "post",
KATTR_ENCTYPE, "multipart/form-data",
KATTR_ACTION, page,
KATTR__MAX);
khtml_elem(&r, KELEM_FIELDSET);
khtml_elem(&r, KELEM_LEGEND);
khtml_puts(&r, "Post (multipart)");
khtml_closeelem(&r, 1);
khtml_elem(&r, KELEM_P);
khtml_attr(&r, KELEM_INPUT,
KATTR_TYPE, "number",
KATTR_NAME, keys[KEY_INTEGER].name,
KATTR_VALUE, cp, KATTR__MAX);
khtml_closeelem(&r, 1);
khtml_elem(&r, KELEM_P);
khtml_attr(&r, KELEM_INPUT,
KATTR_TYPE, "file",
KATTR_MULTIPLE, "",
KATTR_NAME, keys[KEY_FILE].name,
KATTR__MAX);
if (req->fieldmap[KEY_FILE] != NULL) {
if (req->fieldmap[KEY_FILE]->file != NULL) {
khtml_puts(&r, "file: ");
khtml_puts(&r, req->fieldmap[KEY_FILE]->file);
khtml_puts(&r, " ");
}
if (req->fieldmap[KEY_FILE]->ctype != NULL) {
khtml_puts(&r, "ctype: ");
khtml_puts(&r, req->fieldmap[KEY_FILE]->ctype);
}
}
khtml_closeelem(&r, 1);
khtml_elem(&r, KELEM_P);
khtml_attr(&r, KELEM_INPUT,
KATTR_TYPE, "submit",
KATTR__MAX);
khtml_closeelem(&r, 0);
khtml_close(&r);
free(page);
}
int
main(void)
{
struct kreq r;
enum kcgi_err er;
/* Set up our main HTTP context. */
er = khttp_parse(&r, keys, KEY__MAX, pages, PAGE__MAX, PAGE_INDEX);
if (er != KCGI_OK)
return 1;
/*
* Accept only GET, POST, and OPTIONS. Restrict to text/html and a
* valid page. If all of our parameters are valid, use a dispatch array
* to send us to the page handlers. Start with CORS handling, which
* we'll allow to any origin. XXX: this ignores errors in handling,
* which is probably not what you want.
*/
if (r.reqmap[KREQU_ORIGIN] != NULL)
khttp_head(&r, kresps[KRESP_ACCESS_CONTROL_ALLOW_ORIGIN],
"%s", r.reqmap[KREQU_ORIGIN]->val);
if (r.method == KMETHOD_OPTIONS) {
khttp_head(&r, kresps[KRESP_ALLOW], "OPTIONS, GET, POST");
resp_open(&r, KHTTP_204);
} else if (r.method != KMETHOD_GET && r.method != KMETHOD_POST) {
resp_open(&r, KHTTP_405);
} else if (r.page == PAGE__MAX || r.mime != KMIME_TEXT_HTML) {
resp_open(&r, KHTTP_404);
} else
(*disps[r.page])(&r);
khttp_free(&r);
return 0;
}