-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.c
307 lines (271 loc) · 8.85 KB
/
config.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
/*
arfhttpd: Yet another HTTP server
Copyright (C) 2023 arf20 (Ángel Ruiz Fernandez)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
config.c: Config parser
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "strutils.h"
#include "config.h"
const char *config_type_strs[] = {
"webroot",
"header",
"mimeheader",
"index",
"autoindex"
};
/* Config */
string_node_t *listen_list = NULL, *tls_listen_list = NULL;
location_node_t *location_list = NULL, *location_current = NULL;
const char *cert_file = NULL, *cert_key_file = NULL;
string_node_t *
string_list_push(string_node_t **head, const char *str, size_t len) {
if (!head) return NULL;
string_node_t *end = NULL;
string_node_t *new = malloc(sizeof(string_node_t));
if (*head) {
end = *head;
while (end->next) end = end->next;
end->next = new;
new->prev = *head;
} else {
*head = new;
}
new->next = NULL;
new->str = stralloccpy(str, len);
return new;
}
location_node_t *
location_list_push(location_node_t **head, const char *loc, size_t len) {
if (!head) return NULL;
location_node_t *end = NULL;
location_node_t *new = malloc(sizeof(location_node_t));
if (*head) {
end = *head;
while (end->next) end = end->next;
end->next = new;
new->prev = end;
} else {
*head = new;
new->prev = NULL;
}
new->next = NULL;
new->location = stralloccpy(loc, len);
new->config = NULL;
return new;
}
location_node_t *
location_list_find(location_node_t *head, const char *loc) {
if (!head) return NULL;
location_node_t *ptr = head;
while (ptr) {
if (strcmp(ptr->location, loc) == 0) return ptr;
ptr = ptr->next;
}
return NULL;
}
config_node_t *
config_list_push(config_node_t **head, config_type_t type,
const char *p1, const char *p2)
{
if (!head) return NULL;
config_node_t *end = NULL;
config_node_t *new = malloc(sizeof(config_node_t));
if (*head) {
end = *head;
while (end->next) end = end->next;
end->next = new;
new->prev = end;
} else {
*head = new;
new->prev = NULL;
}
new->next = NULL;
new->type = type;
if (p1)
new->param1 = stralloccpy(p1, strlen(p1));
else new->param1 = NULL;
if (p2)
new->param2 = stralloccpy(p2, strlen(p2));
else new->param2 = NULL;
return new;
}
/* str utils */
int
isnotspace(char c) {
int t = (c != ' ') && (c != '\t');
return t;
}
const char *
findalnum(const char *str) {
if (!str) return NULL;
while (*str && !isnotspace(*str)) str++;
return str;
}
const char *
find_value(const char *key) {
size_t n = 0;
const char *nl = strchr(key, '\n');
if (nl) n = nl - key; else n = strlen(key);
const char *v = strnchr(key, n, ' ');
if (!v) return NULL;
return findalnum(v);
}
int
substrchk(const char *str, const char *substr) {
return strncmp(str, substr, strlen(substr)) == 0;
}
int
config_parse(const char *config) {
size_t config_length = strlen(config);
string_node_t *listen_list_current = NULL, *listen_list_prev = NULL;
int line = 0;
const char *ptr = config, *key, *value, *value_end;
size_t value_length, p1len, p2len;
char p1[1024];
char p2[1024];
int argc = 0;
while (ptr && *ptr && ptr < ptr + config_length) {
key = findalnum(ptr);
if (!key) { /* Ignore empty lines */
ptr = strchr(ptr, '\n');
continue;
}
value = find_value(key);
if (value)
value_end = strchr(value, '\n');
else
value_end = strchr(key, '\n');
value_length = value_end - value;
/* Extract arguments */
argc = 0;
if (value) {
argc++;
const char* separator = strnchr(value, value_length, ' ');
if (separator) {
p1len = separator - value;
strncpy(p1, value, p1len);
p1[p1len] = '\0';
p2len = value_end - (separator + 1);
strncpy(p2, separator + 1, p2len);
p2[p2len] = '\0';
argc++;
} else {
strncpy(p1, value, value_length);
p1[value_length] = '\0';
p1len = value_length;
}
}
/* Valid keys */
/* Context-less */
if (substrchk(key, "listen ")) { /* address/port */
if (argc != 1 && argc != 2) {
printf("Error: Wrong amount of arguments, line %d\n", line);
goto next;
}
if (argc == 2 && p2 && strncmp(p2, "tls", 3) == 0) {
string_list_push(&tls_listen_list, p1, p1len);
} else {
string_list_push(&listen_list, p1, p1len);
}
}
else if (substrchk(key, "certificate ")) { /* address/port */
if (argc != 1) {
printf("Error: Wrong amount of arguments, line %d\n", line);
goto next;
}
if (cert_file) {
printf("Error: duplicated certificate_key, line %d\n", line);
goto next;
}
cert_file = stralloccpy(p1, p1len);
}
else if (substrchk(key, "certificate_key ")) { /* address/port */
if (argc != 1) {
printf("Error: Wrong amount of arguments, line %d\n", line);
goto next;
}
if (cert_key_file) {
printf("Error: duplicated certificate_key, line %d\n", line);
goto next;
}
cert_key_file = stralloccpy(p1, p1len);
}
else if (substrchk(key, "location ")) {
if (argc != 1) {
printf("Error: Wrong amount of arguments, line %d\n", line);
goto next;
}
if (location_list_find(location_list, p1)) {
printf("Warning: Duplicated location, line %d\n", line);
} else {
location_current = location_list_push(&location_list,
p1, p1len);
}
}
/* Location context */
else if (substrchk(key, "webroot ")) { /* Root of location in fs */
if (argc != 1) {
printf("Error: Wrong amount of arguments, line %d\n", line);
goto next;
}
config_list_push(&location_current->config, CONFIG_ROOT, p1, NULL);
}
else if (substrchk(key, "index ")) { /* Default index file */
if (argc != 1) {
printf("Error: Wrong amount of arguments, line %d\n", line);
goto next;
}
config_list_push(&location_current->config, CONFIG_INDEX, p1, NULL);
}
else if (substrchk(key, "autoindex")) { /* No parameters, enable */
config_list_push(&location_current->config, CONFIG_AUTOINDEX,
NULL, NULL);
}
else if (substrchk(key, "mimeheader")) { /* No parameters, enable */
config_list_push(&location_current->config, CONFIG_MIMEHEADER,
NULL, NULL);
}
else if (substrchk(key, "header ")) { /* Header and value */
if (argc != 2) {
printf("Error: Wrong amount of arguments, line %d\n", line);
goto next;
}
config_list_push(&location_current->config, CONFIG_HEADER,
p1, p2);
}
else {
printf("Warning: Invalid config key, line %d\n", line + 1);
}
next:
ptr = value_end + 1;
line++;
}
/* Check config */
if (!location_list) printf("Error: No location\n");
location_node_t *location_current = location_list;
while (location_current) {
int haspoint = 0;
config_node_t *config_current = location_current->config;
while (config_current) {
if (config_current->type == CONFIG_ROOT) haspoint = 1;
config_current = config_current->next;
}
if (!haspoint) printf("Error: No point in location %s\n",
location_current->location);
location_current = location_current->next;
}
return 0;
}