-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlkconfig.c
364 lines (321 loc) · 11.1 KB
/
lkconfig.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <assert.h>
#include <limits.h>
#include "lklib.h"
#include "lknet.h"
#define HOSTCONFIGS_INITIAL_SIZE 10
LKConfig *lk_config_new() {
LKConfig *cfg = lk_malloc(sizeof(LKConfig), "lk_config_new");
cfg->serverhost = lk_string_new("");
cfg->port = lk_string_new("");
cfg->hostconfigs = lk_malloc(sizeof(LKHostConfig*) * HOSTCONFIGS_INITIAL_SIZE, "lk_config_new_hostconfigs");
cfg->hostconfigs_len = 0;
cfg->hostconfigs_size = HOSTCONFIGS_INITIAL_SIZE;
return cfg;
}
void lk_config_free(LKConfig *cfg) {
lk_string_free(cfg->serverhost);
lk_string_free(cfg->port);
for (int i=0; i < cfg->hostconfigs_len; i++) {
LKHostConfig *hc = cfg->hostconfigs[i];
lk_hostconfig_free(hc);
}
memset(cfg->hostconfigs, 0, sizeof(LKHostConfig*) * cfg->hostconfigs_size);
lk_free(cfg->hostconfigs);
cfg->serverhost = NULL;
cfg->port = NULL;
cfg->hostconfigs = NULL;
lk_free(cfg);
}
#define CONFIG_LINE_SIZE 255
// Read config file and set config structure.
//
// Config file format:
// -------------------
// serverhost=127.0.0.1
// port=5000
//
// # Matches all other hostnames
// hostname *
// homedir=/var/www/testsite
// alias latest=latest.html
//
// # Matches http://localhost
// hostname localhost
// homedir=/var/www/testsite
//
// # http://littlekitten.xyz
// hostname littlekitten.xyz
// homedir=/var/www/testsite
// cgidir=cgi-bin
// alias latest=latest.html
// alias about=about.html
// alias guestbook=cgi-bin/guestbook.pl
// alias blog=cgi-bin/blog.pl
//
// # http://newsboard.littlekitten.xyz
// hostname newsboard.littlekitten.xyz
// proxyhost=localhost:8001
//
// Format description:
// The host and port number is defined first, followed by one or more
// host config sections. The host config section always starts with the
// 'hostname <domain>' line followed by the settings for that hostname.
// The section ends on either EOF or when a new 'hostname <domain>' line
// is read, indicating the start of the next host config section.
//
typedef enum {CFG_ROOT, CFG_HOSTSECTION} ParseCfgState;
int lk_config_read_configfile(LKConfig *cfg, char *configfile) {
FILE *f = fopen(configfile, "r");
if (f == NULL) {
lk_print_err("lk_read_configfile fopen()");
return -1;
}
char line[CONFIG_LINE_SIZE];
ParseCfgState state = CFG_ROOT;
LKString *l = lk_string_new("");
LKString *k = lk_string_new("");
LKString *v = lk_string_new("");
LKString *aliask = lk_string_new("");
LKString *aliasv = lk_string_new("");
LKHostConfig *hc = NULL;
while (1) {
char *pz = fgets(line, sizeof(line), f);
if (pz == NULL) {
break;
}
lk_string_assign(l, line);
lk_string_trim(l);
// Skip # comment line.
if (lk_string_starts_with(l, "#")) {
continue;
}
// hostname littlekitten.xyz
lk_string_split_assign(l, " ", k, v); // l:"k v", assign k and v
if (lk_string_sz_equal(k, "hostname")) {
// hostname littlekitten.xyz
hc = lk_config_create_get_hostconfig(cfg, v->s);
state = CFG_HOSTSECTION;
continue;
}
if (state == CFG_ROOT) {
assert(hc == NULL);
// serverhost=127.0.0.1
// port=8000
lk_string_split_assign(l, "=", k, v); // l:"k=v", assign k and v
if (lk_string_sz_equal(k, "serverhost")) {
lk_string_assign(cfg->serverhost, v->s);
continue;
} else if (lk_string_sz_equal(k, "port")) {
lk_string_assign(cfg->port, v->s);
continue;
}
continue;
}
if (state == CFG_HOSTSECTION) {
assert(hc != NULL);
// homedir=testsite
// cgidir=cgi-bin
// proxyhost=localhost:8001
lk_string_split_assign(l, "=", k, v);
if (lk_string_sz_equal(k, "homedir")) {
lk_string_assign(hc->homedir, v->s);
continue;
} else if (lk_string_sz_equal(k, "cgidir")) {
lk_string_assign(hc->cgidir, v->s);
continue;
} else if (lk_string_sz_equal(k, "proxyhost")) {
lk_string_assign(hc->proxyhost, v->s);
continue;
}
// alias latest=latest.html
lk_string_split_assign(l, " ", k, v);
if (lk_string_sz_equal(k, "alias")) {
lk_string_split_assign(v, "=", aliask, aliasv);
if (!lk_string_starts_with(aliask, "/")) {
lk_string_prepend(aliask, "/");
}
if (!lk_string_starts_with(aliasv, "/")) {
lk_string_prepend(aliasv, "/");
}
lk_stringtable_set(hc->aliases, aliask->s, aliasv->s);
continue;
}
continue;
}
}
lk_string_free(l);
lk_string_free(k);
lk_string_free(v);
lk_string_free(aliask);
lk_string_free(aliasv);
fclose(f);
return 0;
}
LKHostConfig *lk_config_add_hostconfig(LKConfig *cfg, LKHostConfig *hc) {
assert(cfg->hostconfigs_len <= cfg->hostconfigs_size);
// Increase size if no more space.
if (cfg->hostconfigs_len == cfg->hostconfigs_size) {
cfg->hostconfigs_size++;
cfg->hostconfigs = lk_realloc(cfg->hostconfigs, sizeof(LKHostConfig*) * cfg->hostconfigs_size, "lk_config_add_hostconfig");
}
cfg->hostconfigs[cfg->hostconfigs_len] = hc;
cfg->hostconfigs_len++;
return hc;
}
// Return hostconfig matching hostname,
// or if hostname parameter is NULL, return hostconfig matching "*".
// Return NULL if no matching hostconfig.
LKHostConfig *lk_config_find_hostconfig(LKConfig *cfg, char *hostname) {
if (hostname != NULL) {
for (int i=0; i < cfg->hostconfigs_len; i++) {
LKHostConfig *hc = cfg->hostconfigs[i];
if (lk_string_sz_equal(hc->hostname, hostname)) {
return hc;
}
}
}
// If hostname not found, return hostname * (fallthrough hostname).
for (int i=0; i < cfg->hostconfigs_len; i++) {
LKHostConfig *hc = cfg->hostconfigs[i];
if (lk_string_sz_equal(hc->hostname, "*")) {
return hc;
}
}
return NULL;
}
// Return hostconfig with hostname or NULL if not found.
LKHostConfig *get_hostconfig(LKConfig *cfg, char *hostname) {
for (int i=0; i < cfg->hostconfigs_len; i++) {
LKHostConfig *hc = cfg->hostconfigs[i];
if (lk_string_sz_equal(hc->hostname, hostname)) {
return hc;
}
}
return NULL;
}
// Return hostconfig with hostname if it exists.
// Create new hostconfig with hostname if not found. Never returns null.
LKHostConfig *lk_config_create_get_hostconfig(LKConfig *cfg, char *hostname) {
LKHostConfig *hc = get_hostconfig(cfg, hostname);
if (hc == NULL) {
hc = lk_hostconfig_new(hostname);
lk_config_add_hostconfig(cfg, hc);
}
return hc;
}
void lk_config_print(LKConfig *cfg) {
printf("serverhost: %s\n", cfg->serverhost->s);
printf("port: %s\n", cfg->port->s);
for (int i=0; i < cfg->hostconfigs_len; i++) {
LKHostConfig *hc = cfg->hostconfigs[i];
printf("%2d. hostname %s\n", i+1, hc->hostname->s);
if (hc->homedir->s_len > 0) {
printf(" homedir: %s\n", hc->homedir->s);
}
if (hc->cgidir->s_len > 0) {
printf(" cgidir: %s\n", hc->cgidir->s);
}
if (hc->proxyhost->s_len > 0) {
printf(" proxyhost: %s\n", hc->proxyhost->s);
}
for (int j=0; j < hc->aliases->items_len; j++) {
printf(" alias %s=%s\n", hc->aliases->items[j].k->s, hc->aliases->items[j].v->s);
}
}
printf("\n");
}
// Fill in default values for unspecified settings.
void lk_config_finalize(LKConfig *cfg) {
// serverhost defaults to localhost if not specified.
if (cfg->serverhost->s_len == 0) {
lk_string_assign(cfg->serverhost, "localhost");
}
// port defaults to 8000 if not specified.
if (cfg->port->s_len == 0) {
lk_string_assign(cfg->port, "8000");
}
// Get current working directory.
LKString *current_dir = lk_string_new("");
char *s = get_current_dir_name();
if (s != NULL) {
lk_string_assign(current_dir, s);
free(s);
} else {
lk_string_assign(current_dir, ".");
}
// Set fallthrough defaults only if no other hostconfigs specified
// Note: If other hostconfigs are set, such as in a config file,
// the fallthrough '*' hostconfig should be set explicitly.
if (cfg->hostconfigs_len == 0) {
// homedir default to current directory
LKHostConfig *hc = lk_config_create_get_hostconfig(cfg, "*");
lk_string_assign(hc->homedir, current_dir->s);
// cgidir default to cgi-bin
if (hc->cgidir->s_len == 0) {
lk_string_assign(hc->cgidir, "/cgi-bin/");
}
}
// Set homedir absolute paths for hostconfigs.
// Adjust /cgi-bin/ paths.
char homedir_abspath[PATH_MAX];
for (int i=0; i < cfg->hostconfigs_len; i++) {
LKHostConfig *hc = cfg->hostconfigs[i];
// Skip hostconfigs that don't have have homedir.
if (hc->homedir->s_len == 0) {
continue;
}
// Set absolute path to homedir
char *pz = realpath(hc->homedir->s, homedir_abspath);
if (pz == NULL) {
lk_print_err("realpath()");
homedir_abspath[0] = '\0';
}
lk_string_assign(hc->homedir_abspath, homedir_abspath);
// Adjust cgidir paths.
if (hc->cgidir->s_len > 0) {
if (!lk_string_starts_with(hc->cgidir, "/")) {
lk_string_prepend(hc->cgidir, "/");
}
if (!lk_string_ends_with(hc->cgidir, "/")) {
lk_string_append(hc->cgidir, "/");
}
lk_string_assign(hc->cgidir_abspath, hc->homedir_abspath->s);
lk_string_append(hc->cgidir_abspath, hc->cgidir->s);
}
}
lk_string_free(current_dir);
}
LKHostConfig *lk_hostconfig_new(char *hostname) {
LKHostConfig *hc = lk_malloc(sizeof(LKHostConfig), "lk_hostconfig_new");
hc->hostname = lk_string_new(hostname);
hc->homedir = lk_string_new("");
hc->homedir_abspath = lk_string_new("");
hc->cgidir = lk_string_new("");
hc->cgidir_abspath = lk_string_new("");
hc->aliases = lk_stringtable_new();
hc->proxyhost = lk_string_new("");
return hc;
}
void lk_hostconfig_free(LKHostConfig *hc) {
lk_string_free(hc->hostname);
lk_string_free(hc->homedir);
lk_string_free(hc->homedir_abspath);
lk_string_free(hc->cgidir);
lk_string_free(hc->cgidir_abspath);
lk_stringtable_free(hc->aliases);
lk_string_free(hc->proxyhost);
hc->hostname = NULL;
hc->homedir = NULL;
hc->homedir_abspath = NULL;
hc->cgidir = NULL;
hc->cgidir_abspath = NULL;
hc->aliases = NULL;
hc->proxyhost = NULL;
lk_free(hc);
}