forked from mongrel2/mongrel2
/
config_file.c
365 lines (278 loc) · 10.5 KB
/
config_file.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
#include <config/db.h>
#include "constants.h"
#include <bstring.h>
#include "config_file.h"
#include "ast.h"
#include <dbg.h>
#include <stdlib.h>
#define CONFIRM_TYPE(N) check(Value_is(val, CLASS), "Not a class.");\
check(biseqcstr(Class_ident(val->as.cls), N), "Should be a " # N ".");
int SERVER_ID = 0;
int HOST_ID = 0;
struct tagbstring CACHE_TTL = bsStatic("cache_ttl");
int Dir_load(tst_t *settings, tst_t *params)
{
const char *base = AST_str(settings, params, "base", VAL_QSTRING);
tns_value_t *res = DB_exec(bdata(&DIR_SQL), base,
AST_str(settings, params, "index_file", VAL_QSTRING),
AST_str(settings, params, "default_ctype", VAL_QSTRING));
check(res != NULL, "Invalid database, couldn't query for directory: %s", base);
tns_value_destroy(res);
if(tst_search(params, bdata(&CACHE_TTL), blength(&CACHE_TTL))) {
const char *cache_ttl = AST_str(settings, params, "cache_ttl", VAL_NUMBER);
if(cache_ttl && cache_ttl[0] != '0') {
res = DB_exec(bdata(&DIR_CACHE_TTL_SQL), cache_ttl);
check(res != NULL, "Invalid database, couldn't set cache_ttl in directory: %s", base);
tns_value_destroy(res);
}
}
return DB_lastid();
error:
if(res) tns_value_destroy(res);
return -1;
}
struct tagbstring RAW_PAYLOAD = bsStatic("raw_payload");
struct tagbstring PROTOCOL = bsStatic("protocol");
int Handler_load(tst_t *settings, tst_t *params)
{
const char *send_spec = AST_str(settings, params, "send_spec", VAL_QSTRING);
tns_value_t *res = NULL;
res = DB_exec(bdata(&HANDLER_SQL),
send_spec,
AST_str(settings, params, "send_ident", VAL_QSTRING),
AST_str(settings, params, "recv_spec", VAL_QSTRING),
AST_str(settings, params, "recv_ident", VAL_QSTRING));
check(res != NULL, "Failed to load Handler: %s", send_spec);
tns_value_destroy(res);
if(tst_search(params, bdata(&RAW_PAYLOAD), blength(&RAW_PAYLOAD))) {
const char *raw_payload = AST_str(settings, params, "raw_payload", VAL_NUMBER);
if(raw_payload && raw_payload[0] == '1') {
DB_exec(bdata(&HANDLER_RAW_SQL));
check(res != NULL, "Invalid database, can't set raw payload.");
tns_value_destroy(res);
}
}
if(tst_search(params, bdata(&PROTOCOL), blength(&PROTOCOL))) {
const char *protocol = AST_str(settings, params, "protocol", VAL_QSTRING);
protocol = protocol != NULL ? protocol : "json";
res = DB_exec(bdata(&HANDLER_PROTOCOL_SQL), protocol);
check(res != NULL, "Invalid SQL with your protocol setting: '%s'", protocol);
tns_value_destroy(res);
}
return DB_lastid();
error:
if(res) tns_value_destroy(res);
return -1;
}
int Proxy_load(tst_t *settings, tst_t *params)
{
const char *addr = AST_str(settings, params, "addr", VAL_QSTRING);
const char *port = AST_str(settings, params, "port", VAL_NUMBER);
tns_value_t *res = DB_exec(bdata(&PROXY_SQL), addr, port);
check(res != NULL, "Failed to load Proxy: %s:%s", addr, port);
return DB_lastid();
error:
if(res) tns_value_destroy(res);
return -1;
}
int Mimetypes_import()
{
char *zErrMsg = NULL;
int rc = sqlite3_exec(CONFIG_DB, bdata(&MIMETYPES_DEFAULT_SQL), NULL, NULL, &zErrMsg);
check(rc == SQLITE_OK, "Failed to load initial schema: %s", zErrMsg);
return 0;
error:
if(zErrMsg) sqlite3_free(zErrMsg);
return -1;
}
int Mimetypes_load(tst_t *settings, Pair *pair)
{
const char *ext = bdata(Pair_key(pair));
Value *val = Pair_value(pair);
check(val, "Error loading Mimetype %s", bdata(Pair_key(pair)));
tns_value_t *res = NULL;
res = DB_exec(bdata(&MIMETYPE_SQL),
ext, ext, bdata(val->as.string->data));
check(res != NULL, "Failed to add mimetype: %s=%s",
ext, bdata(val->as.string->data));
tns_value_destroy(res);
return 0;
error:
if(res) tns_value_destroy(res);
return -1;
}
int Settings_load(tst_t *settings, Pair *pair)
{
const char *name = bdata(Pair_key(pair));
Value *val = Pair_value(pair);
check(val, "Error loading Setting %s", bdata(Pair_key(pair)));
tns_value_t *res = NULL;
res = DB_exec(bdata(&SETTING_SQL), name, bdata(val->as.string->data));
check(res != NULL, "Failed to add setting: %s=%s",
name, bdata(val->as.string->data));
tns_value_destroy(res);
return 0;
error:
if(res) tns_value_destroy(res);
return -1;
}
int Route_load(tst_t *settings, Pair *pair)
{
const char *name = bdata(Pair_key(pair));
tns_value_t *res = NULL;
Value *val = Pair_value(pair);
bstring type = NULL;
int rc = 0;
check(val, "Error loading route: %s", bdata(Pair_key(pair)));
check(Value_is(val, CLASS), "Expected a Class but got a %s instead.",
Value_type_name(val->type));
Class *cls = val->as.cls;
type = bstrcpy(Class_ident(cls));
btolower(type);
if(cls->id == -1) {
if(biseqcstr(type, "dir")) {
rc = Dir_load(settings, cls->params);
} else if(biseqcstr(type, "proxy")) {
rc = Proxy_load(settings, cls->params);
} else if(biseqcstr(type, "handler")) {
rc = Handler_load(settings, cls->params);
} else {
sentinel("Invalid type of route target: %s", bdata(Class_ident(cls)));
}
check(rc != -1, "Failed to create target for route %s", name);
cls->id = rc;
}
res = DB_exec(bdata(&ROUTE_SQL), name, HOST_ID, cls->id, bdata(type));
check(res != NULL, "Failed to intialize route.");
tns_value_destroy(res);
bdestroy(type);
return 0;
error:
if(res) tns_value_destroy(res);
if(type) bdestroy(type);
return -1;
}
struct tagbstring MATCHING_PARAM = bsStatic("matching");
int Host_load(tst_t *settings, Value *val)
{
CONFIRM_TYPE("Host");
Class *cls = val->as.cls;
tns_value_t *res = NULL;
struct tagbstring ROUTES_VAR = bsStatic("routes");
const char *name = AST_str(settings, cls->params, "name", VAL_QSTRING);
const char *matching = name; // default to this then change it
check(name, "No name set for Host.");
if(tst_search(cls->params, bdata(&MATCHING_PARAM), blength(&MATCHING_PARAM))) {
// specified matching so use that
matching = AST_str(settings, cls->params, bdata(&MATCHING_PARAM), VAL_QSTRING);
}
res = DB_exec(bdata(&HOST_SQL), SERVER_ID, name, matching);
check(res != NULL, "Failed to store Host: %s", name);
tns_value_destroy(res);
cls->id = HOST_ID = DB_lastid();
Value *routes = AST_get(settings, cls->params, &ROUTES_VAR, VAL_HASH);
check(routes, "Didn't find any routes for %s", name);
AST_walk_hash(settings, routes, Route_load);
return 0;
error:
if(res) tns_value_destroy(res);
return -1;
}
struct tagbstring BIND_ADDR = bsStatic("bind_addr");
struct tagbstring USE_SSL = bsStatic("use_ssl");
int Server_load(tst_t *settings, Value *val)
{
CONFIRM_TYPE("Server");
Class *cls = val->as.cls;
tns_value_t *res = NULL;
struct tagbstring HOSTS_VAR = bsStatic("hosts");
const char *bind_addr = NULL;
const char *use_ssl = NULL;
if(tst_search(cls->params, bdata(&BIND_ADDR), blength(&BIND_ADDR))) {
bind_addr = AST_str(settings, cls->params, bdata(&BIND_ADDR), VAL_QSTRING);
} else {
bind_addr = "0.0.0.0";
}
if(tst_search(cls->params, bdata(&USE_SSL), blength(&USE_SSL))) {
use_ssl = AST_str(settings, cls->params, bdata(&USE_SSL), VAL_NUMBER);
} else {
use_ssl = "0";
}
res = DB_exec(bdata(&SERVER_SQL),
AST_str(settings, cls->params, "uuid", VAL_QSTRING),
AST_str(settings, cls->params, "access_log", VAL_QSTRING),
AST_str(settings, cls->params, "error_log", VAL_QSTRING),
AST_str(settings, cls->params, "pid_file", VAL_QSTRING),
AST_str(settings, cls->params, "chroot", VAL_QSTRING),
AST_str(settings, cls->params, "default_host", VAL_QSTRING),
AST_str(settings, cls->params, "name", VAL_QSTRING),
bind_addr,
AST_str(settings, cls->params, "port", VAL_NUMBER),
use_ssl
);
check(res != NULL, "Failed to exec SQL: %s", bdata(&SERVER_SQL));
tns_value_destroy(res);
cls->id = SERVER_ID = DB_lastid();
Value *hosts = AST_get(settings, cls->params, &HOSTS_VAR, VAL_LIST);
check(hosts != NULL, "Could not find Server.hosts setting in host %s:%s",
AST_str(settings, cls->params, "uuid", VAL_QSTRING),
AST_str(settings, cls->params, "name", VAL_QSTRING));
AST_walk_list(settings, hosts->as.list, Host_load);
return 0;
error:
if(res) tns_value_destroy(res);
return -1;
}
static inline int Config_setup(const char *db_file)
{
DB_init(db_file);
tns_value_t *res = DB_exec("begin");
check(res != NULL, "Couldn't start transaction.");
tns_value_destroy(res);
char *zErrMsg = NULL;
int rc = sqlite3_exec(CONFIG_DB, bdata(&CONFIG_SCHEMA), NULL, NULL, &zErrMsg);
check(rc == SQLITE_OK, "Failed to load initial schema: %s", zErrMsg);
return 0;
error:
if(res) tns_value_destroy(res);
if(zErrMsg) sqlite3_free(zErrMsg);
return -1;
}
static inline int Config_commit()
{
return DB_exec("commit") == NULL ? -1 : 0;
}
int Config_load(const char *config_file, const char *db_file)
{
int rc = 0;
tst_t *settings = NULL;
struct tagbstring SETTINGS_VAR = bsStatic("settings");
struct tagbstring MIMETYPES_VAR = bsStatic("mimetypes");
settings = Parse_config_file(config_file);
check(settings != NULL, "Error parsing config file: %s.", config_file);
rc = Config_setup(db_file);
check(rc == 0, "Failed to configure config db: %s", db_file);
rc = AST_walk(settings, Server_load);
check(rc == 0, "Failed to process the config file: %s", config_file);
Value *set = AST_get(settings, settings, &SETTINGS_VAR, VAL_HASH);
if(set) {
rc = AST_walk_hash(settings, set, Settings_load);
check(rc == 0, "Failed to load the settings. Aborting.");
}
rc = Mimetypes_import();
check(rc == 0, "Failed to import default mimetypes.");
Value *mime = AST_get(settings, settings, &MIMETYPES_VAR, VAL_HASH);
if(mime) {
AST_walk_hash(settings, mime, Mimetypes_load);
check(rc == 0, "Failed to load the mimetypes. Aborting.");
}
rc = Config_commit();
check(rc == 0, "Failed to commit config db: %s", db_file);
AST_destroy(settings);
DB_close();
return 0;
error:
AST_destroy(settings);
DB_close();
return -1;
}