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
|
/* $Id$ */
#include <assert.h>
#include <fcntl.h>
#include <regex.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "mandoc.h"
#include "mandocdb.h"
#include "apropos_db.h"
/*
* The page a request is trying to make.
*/
enum page {
PAGE_INDEX,
PAGE_SEARCH,
PAGE__MAX
};
/*
* Key-value pair.
* Both key and val are on the heap.
*/
struct kval {
char *key;
char *val;
};
/*
* The media type, determined by suffix, of the requesting or responding
* context.
*/
enum media {
MEDIA_HTML,
MEDIA__MAX
};
/*
* An HTTP request.
*/
struct req {
struct kval *fields; /* query fields */
size_t fieldsz;
enum media media;
enum page page;
};
#if 0
static void html_printtext(const char *);
#endif
static int kval_decode(char *);
static void kval_parse(struct kval **, size_t *, char *);
static void kval_free(struct kval *, size_t);
static void pg_index(const struct req *, char *);
static void pg_search(const struct req *, char *);
#if 0
static void pg_searchres(struct rec *, size_t, void *);
#endif
static const char * const pages[PAGE__MAX] = {
"index", /* PAGE_INDEX */
"search", /* PAGE_SEARCH */
};
static const char * const medias[MEDIA__MAX] = {
"html", /* MEDIA_HTML */
};
#if 0
static void
html_printtext(const char *p)
{
char c;
while ('\0' != *p)
switch ((c = *p++)) {
case ('"'):
printf(""e;");
break;
case ('&'):
printf("&");
break;
case ('>'):
printf(">");
break;
case ('<'):
printf("<");
break;
default:
putchar((unsigned char)c);
break;
}
}
#endif
static void
kval_free(struct kval *p, size_t sz)
{
int i;
for (i = 0; i < (int)sz; i++) {
free(p[i].key);
free(p[i].val);
}
free(p);
}
/*
* Parse out key-value pairs from an HTTP request variable.
* This can be either a cookie or a POST/GET string.
*/
static void
kval_parse(struct kval **kv, size_t *kvsz, char *p)
{
char *key, *val;
size_t sz, cur;
cur = 0;
while (p && '\0' != *p) {
while (' ' == *p)
p++;
key = p;
val = NULL;
if (NULL != (p = strchr(p, '='))) {
*p++ = '\0';
val = p;
sz = strcspn(p, ";&");
/* LINTED */
p += sz;
if ('\0' != *p)
*p++ = '\0';
} else {
p = key;
sz = strcspn(p, ";&");
/* LINTED */
p += sz;
if ('\0' != *p)
p++;
continue;
}
if ('\0' == *key || '\0' == *val)
continue;
/* Just abort handling. */
if ( ! kval_decode(key))
return;
if ( ! kval_decode(val))
return;
if (*kvsz + 1 >= cur) {
cur++;
*kv = mandoc_realloc
(*kv, cur * sizeof(struct kval));
}
(*kv)[(int)*kvsz].key = mandoc_strdup(key);
(*kv)[(int)*kvsz].val = mandoc_strdup(val);
(*kvsz)++;
}
}
/*
* In-place HTTP-decode a string. The standard explanation is that this
* turns "%4e+foo" into "n foo" in the regular way. This is done
* in-place over the allocated string.
*/
static int
kval_decode(char *p)
{
char hex[3];
int c;
hex[2] = '\0';
for ( ; '\0' != *p; p++) {
if ('%' == *p) {
if ('\0' == (hex[0] = *(p + 1)))
return(0);
if ('\0' == (hex[1] = *(p + 2)))
return(0);
if (1 != sscanf(hex, "%x", &c))
return(0);
if ('\0' == c)
return(0);
*p = (char)c;
memmove(p + 1, p + 3, strlen(p + 3) + 1);
} else
*p = '+' == *p ? ' ' : *p;
}
*p = '\0';
return(1);
}
/* ARGSUSED */
static void
pg_index(const struct req *req, char *path)
{
}
#if 0
static void
pg_searchres(struct rec *recs, size_t sz, void *arg)
{
int i;
const char *pg;
if (NULL == (pg = getenv("SCRIPT_NAME")))
pg = "";
for (i = 0; i < (int)sz; i++) {
printf("<A HREF=\"%s/show/%u.html\">",
pg, recs[i].rec);
html_printtext(recs[i].title);
putchar('(');
html_printtext(recs[i].cat);
puts(")</A>");
}
}
#endif
static void
pg_search(const struct req *req, char *path)
{
int i;
struct opts opt;
for (i = 0; i < (int)req->fieldsz; i++)
if (0 == strcmp(req->fields[i].key, "key"))
break;
if (i == (int)req->fieldsz)
return;
memset(&opt, 0, sizeof(struct opts));
/*opt.types = TYPE_NAME | TYPE_DESC;
apropos_search(&opt, req->fields[i].val, NULL, pg_searchres);*/
}
int
main(void)
{
int i;
struct req req;
char *p;
char *path, *subpath, *suffix;
memset(&req, 0, sizeof(struct req));
if (NULL != (p = getenv("QUERY_STRING")))
kval_parse(&req.fields, &req.fieldsz, p);
suffix = subpath = path = NULL;
req.media = MEDIA_HTML;
req.page = PAGE__MAX;
if (NULL == (path = getenv("PATH_INFO")) || '\0' == *path)
req.page = PAGE_INDEX;
if (NULL != path && '/' == *path && '\0' == *++path)
req.page = PAGE_INDEX;
if (NULL != path && NULL != (suffix = strrchr(path, '.')))
if (NULL != suffix && NULL == strchr(suffix, '/'))
*suffix++ = '\0';
if (NULL != path && NULL != (subpath = strchr(path, '/')))
*subpath++ = '\0';
if (NULL != suffix && '\0' != *suffix)
for (i = 0; i < (int)MEDIA__MAX; i++)
if (0 == strcmp(medias[i], suffix)) {
req.media = (enum media)i;
break;
}
if (NULL != path && '\0' != *path)
for (i = 0; i < (int)PAGE__MAX; i++)
if (0 == strcmp(pages[i], path)) {
req.page = (enum page)i;
break;
}
switch (req.page) {
case (PAGE_INDEX):
pg_index(&req, subpath);
break;
case (PAGE_SEARCH):
pg_search(&req, subpath);
break;
default:
/* Blah */
break;
}
kval_free(req.fields, req.fieldsz);
return(EXIT_SUCCESS);
}
|