Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 325 lines (258 sloc) 6.598 kb
b72ddd2 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@622 5dc97367-97…
alobbs authored
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2
3 /* Cherokee
4 *
5 * Authors:
6 * Alvaro Lopez Ortega <alvaro@alobbs.com>
7 *
920e20b @alobbs Happy new year!
alobbs authored
8 * Copyright (C) 2001-2011 Alvaro Lopez Ortega
b72ddd2 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@622 5dc97367-97…
alobbs authored
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU General Public
12 * License as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
2c39092 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@2660 5dc97367-9…
alobbs authored
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 * 02110-1301, USA.
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
23 */
b72ddd2 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@622 5dc97367-97…
alobbs authored
24
25 #include "common-internal.h"
26 #include "config_reader.h"
27 #include "util.h"
28
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <unistd.h>
32
33 #define ENTRIES "config"
34
35
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
36 static ret_t
b72ddd2 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@622 5dc97367-97…
alobbs authored
37 do_parse_file (cherokee_config_node_t *conf, const char *file)
38 {
39 ret_t ret;
40 cherokee_buffer_t buf = CHEROKEE_BUF_INIT;
41
42 ret = cherokee_buffer_read_file (&buf, (char *)file);
9c55e1c @alobbs "Memory leak. Dynamic memory stored in 'buf.buf' allocated through
alobbs authored
43 if (ret != ret_ok) goto error;
b72ddd2 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@622 5dc97367-97…
alobbs authored
44
45 ret = cherokee_config_reader_parse_string (conf, &buf);
46 if (ret != ret_ok) goto error;
47
48 cherokee_buffer_mrproper (&buf);
49 return ret_ok;
50
51 error:
52 cherokee_buffer_mrproper (&buf);
53 return ret_error;
54 }
55
56
57 static ret_t
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
58 do_include (cherokee_config_node_t *conf, cherokee_buffer_t *path)
b72ddd2 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@622 5dc97367-97…
alobbs authored
59 {
60 int re;
61 struct stat info;
62
147fcf8 @alobbs {l,f,}stat() related fix. Ensure the EINTR signal is handler
alobbs authored
63 re = cherokee_stat (path->buf, &info);
b72ddd2 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@622 5dc97367-97…
alobbs authored
64 if (re < 0) {
165f0ad @alobbs Merges the 'newerrors' branch (r3613:HEAD). It implements a new
alobbs authored
65 LOG_CRITICAL (CHEROKEE_ERROR_CONF_READ_ACCESS_FILE, path->buf);
b72ddd2 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@622 5dc97367-97…
alobbs authored
66 return ret_error;
67 }
68
69 if (S_ISREG(info.st_mode)) {
70 return do_parse_file (conf, path->buf);
71
72 } else if (S_ISDIR(info.st_mode)) {
73 DIR *dir;
74 struct dirent *entry;
75 int entry_len;
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
76
e9aa9a9 @alobbs There were a few syscalls where EINTR was not being handled properly.
alobbs authored
77 dir = cherokee_opendir (path->buf);
b72ddd2 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@622 5dc97367-97…
alobbs authored
78 if (dir == NULL) return ret_error;
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
79
b72ddd2 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@622 5dc97367-97…
alobbs authored
80 while ((entry = readdir(dir)) != NULL) {
81 ret_t ret;
82 cherokee_buffer_t full_new = CHEROKEE_BUF_INIT;
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
83
b72ddd2 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@622 5dc97367-97…
alobbs authored
84 /* Ignore backup files
85 */
86 entry_len = strlen(entry->d_name);
da1fab1 @alobbs "Buffer overflow, array index of 'd_name' may be out of bounds. Array
alobbs authored
87 if (unlikely (entry_len <= 0)) {
88 continue;
89 }
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
90
b72ddd2 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@622 5dc97367-97…
alobbs authored
91 if ((entry->d_name[0] == '.') ||
92 (entry->d_name[0] == '#') ||
93 (entry->d_name[entry_len-1] == '~'))
94 {
95 continue;
96 }
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
97
b72ddd2 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@622 5dc97367-97…
alobbs authored
98 ret = cherokee_buffer_add_va (&full_new, "%s/%s", path->buf, entry->d_name);
1ac23e7 @alobbs "Memory leak. Dynamic memory stored in 'full_new.buf' allocated
alobbs authored
99 if (unlikely (ret != ret_ok)) {
100 cherokee_buffer_mrproper (&full_new);
101 return ret;
102 }
b72ddd2 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@622 5dc97367-97…
alobbs authored
103
104 ret = do_parse_file (conf, full_new.buf);
1ac23e7 @alobbs "Memory leak. Dynamic memory stored in 'full_new.buf' allocated
alobbs authored
105 if (unlikely (ret != ret_ok)) {
b72ddd2 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@622 5dc97367-97…
alobbs authored
106 cherokee_buffer_mrproper (&full_new);
107 return ret;
108 }
109
110 cherokee_buffer_mrproper (&full_new);
111 }
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
112
e9aa9a9 @alobbs There were a few syscalls where EINTR was not being handled properly.
alobbs authored
113 cherokee_closedir (dir);
b72ddd2 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@622 5dc97367-97…
alobbs authored
114 return ret_ok;
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
115 }
116
b72ddd2 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@622 5dc97367-97…
alobbs authored
117 SHOULDNT_HAPPEN;
118 return ret_error;
119 }
120
121
026ab9a @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1714 5dc97367-9…
alobbs authored
122 static ret_t
123 check_config_node_sanity (cherokee_config_node_t *conf)
124 {
125 cherokee_list_t *i, *j;
126 int re;
127
128 /* Finds duplicate properties written in lower/upper-case
129 */
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
130 cherokee_config_node_foreach (i, conf)
026ab9a @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1714 5dc97367-9…
alobbs authored
131 {
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
132 cherokee_config_node_foreach (j, conf)
026ab9a @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1714 5dc97367-9…
alobbs authored
133 {
134 if (i == j)
135 continue;
136
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
137 re = cherokee_buffer_case_cmp_buf (&CONFIG_NODE(i)->key,
026ab9a @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1714 5dc97367-9…
alobbs authored
138 &CONFIG_NODE(j)->key);
139 if (re == 0) {
165f0ad @alobbs Merges the 'newerrors' branch (r3613:HEAD). It implements a new
alobbs authored
140 LOG_ERROR (CHEROKEE_ERROR_CONF_READ_CHILDREN_SAME_NODE,
29d5de9 @alobbs Replace almost all the PRINT_ERROR, PRINT_ERROR_S and PRINT_ERRNO by
alobbs authored
141 CONFIG_NODE(i)->key.buf, CONFIG_NODE(j)->key.buf);
026ab9a @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1714 5dc97367-9…
alobbs authored
142 return ret_error;
143 }
144 }
145 }
146
147 return ret_ok;
148 }
149
150
f94092c @alobbs Sort numerical lists after reading the configuration file.
alobbs authored
151 static int
152 cmp_num (cherokee_list_t *a, cherokee_list_t *b)
153 {
154 int an = strtol(CONFIG_NODE(a)->key.buf, NULL, 10);
155 int bn = strtol(CONFIG_NODE(b)->key.buf, NULL, 10);
156
157 if (an == bn) return 0;
158 if (an < bn) return bn - an;
159
160 return - (an - bn);
161 }
162
163 static ret_t
164 sort_lists (cherokee_config_node_t *conf)
165 {
166 int re;
167 ret_t ret;
168 cherokee_list_t *i;
169 cherokee_boolean_t all_num = true;
170
171 /* Are all numbers
172 */
173 cherokee_config_node_foreach (i, conf) {
174 errno = 0;
175 re = strtol(CONFIG_NODE(i)->key.buf, NULL, 10);
176
177 if ((re == 0) && (errno = EINVAL)) {
178 all_num = false;
179 break;
180 }
181 }
182
183 /* Reorder entries
184 */
185 if (all_num) {
186 cherokee_list_sort (&conf->child, cmp_num);
187 }
188
189 /* Iterate through the children
190 */
191 cherokee_config_node_foreach (i, conf) {
192 ret = sort_lists (CONFIG_NODE(i));
193 if (ret != ret_ok) {
194 return ret;
195 }
196 }
197
198 return ret_ok;
199 }
200
201
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
202 ret_t
b72ddd2 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@622 5dc97367-97…
alobbs authored
203 cherokee_config_reader_parse_string (cherokee_config_node_t *conf, cherokee_buffer_t *buf)
204 {
205 ret_t ret;
206 char *eol;
207 char *begin;
208 char *equal;
209 char *tmp;
210 char *eof;
211 cherokee_buffer_t key = CHEROKEE_BUF_INIT;
212 cherokee_buffer_t val = CHEROKEE_BUF_INIT;
213
214 eof = buf->buf + buf->len;
215
216 begin = buf->buf;
217 do {
218 /* Skip whites at the begining
219 */
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
220 while ((begin < eof) &&
221 ((*begin == ' ') || (*begin == '\t') ||
222 (*begin == '\r') || (*begin == '\n')))
b72ddd2 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@622 5dc97367-97…
alobbs authored
223 {
224 begin++;
225 }
226
227 /* Look for the EOL
228 */
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
229 eol = cherokee_min_str (strchr(begin, '\n'),
b72ddd2 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@622 5dc97367-97…
alobbs authored
230 strchr(begin, '\r'));
231
c9f1e81 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3214 5dc97367-9…
alobbs authored
232 if (eol == NULL) {
233 eol = eof;
234 }
b72ddd2 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@622 5dc97367-97…
alobbs authored
235
236 /* Check that it's long enough
237 */
238 if (eol - begin <= 4) {
9aefc63 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3237 5dc97367-9…
alobbs authored
239 goto next;
b72ddd2 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@622 5dc97367-97…
alobbs authored
240 }
241 *eol = '\0';
242
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
243 /* Read the line
b72ddd2 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@622 5dc97367-97…
alobbs authored
244 */
245 if (*begin != '#') {
246 cuint_t val_len;
247
248 equal = strstr (begin, " = ");
249 if (equal == NULL) goto error;
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
250
b72ddd2 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@622 5dc97367-97…
alobbs authored
251 tmp = equal;
252
253 /* Skip whites: end of the key
254 */
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
255 while (*tmp == ' ')
a5111af @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1726 5dc97367-9…
alobbs authored
256 tmp--;
b72ddd2 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@622 5dc97367-97…
alobbs authored
257 cherokee_buffer_add (&key, begin, (tmp + 1) - begin);
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
258
b72ddd2 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@622 5dc97367-97…
alobbs authored
259 tmp = equal + 3;
a5111af @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1726 5dc97367-9…
alobbs authored
260 while (*tmp == ' ')
261 tmp++;
b72ddd2 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@622 5dc97367-97…
alobbs authored
262
263 /* Skip whites: end of the value
264 */
265 val_len = strlen(tmp);
a5111af @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1726 5dc97367-9…
alobbs authored
266 while ((val_len >= 1) &&
267 (tmp[val_len-1] == ' '))
268 {
269 val_len--;
270 }
b72ddd2 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@622 5dc97367-97…
alobbs authored
271
272 cherokee_buffer_add (&val, tmp, val_len);
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
273
b72ddd2 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@622 5dc97367-97…
alobbs authored
274 TRACE(ENTRIES, "'%s' => '%s'\n", key.buf, val.buf);
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
275
b72ddd2 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@622 5dc97367-97…
alobbs authored
276 ret = cherokee_config_node_add_buf (conf, &key, &val);
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
277 if (ret != ret_ok)
a5111af @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1726 5dc97367-9…
alobbs authored
278 goto error;
b72ddd2 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@622 5dc97367-97…
alobbs authored
279 }
280
281 /* Next loop
282 */
9aefc63 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3237 5dc97367-9…
alobbs authored
283 next:
b72ddd2 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@622 5dc97367-97…
alobbs authored
284 begin = eol + 1;
c9f1e81 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3214 5dc97367-9…
alobbs authored
285 if (begin >= eof) {
286 break;
287 }
b72ddd2 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@622 5dc97367-97…
alobbs authored
288
289 cherokee_buffer_clean (&key);
290 cherokee_buffer_clean (&val);
291
292 } while (eol != NULL);
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
293
b72ddd2 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@622 5dc97367-97…
alobbs authored
294 cherokee_buffer_mrproper (&key);
295 cherokee_buffer_mrproper (&val);
026ab9a @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1714 5dc97367-9…
alobbs authored
296
f94092c @alobbs Sort numerical lists after reading the configuration file.
alobbs authored
297 /* Sort internal lists
298 */
299 ret = sort_lists (conf);
300 if (ret != ret_ok)
301 return ret;
302
026ab9a @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1714 5dc97367-9…
alobbs authored
303 /* Sanity check
304 */
305 ret = check_config_node_sanity (conf);
306 if (ret != ret_ok)
307 return ret;
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
308
b72ddd2 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@622 5dc97367-97…
alobbs authored
309 return ret_ok;
310
311 error:
165f0ad @alobbs Merges the 'newerrors' branch (r3613:HEAD). It implements a new
alobbs authored
312 LOG_ERROR (CHEROKEE_ERROR_CONF_READ_PARSE, begin);
b72ddd2 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@622 5dc97367-97…
alobbs authored
313
314 cherokee_buffer_mrproper (&key);
315 cherokee_buffer_mrproper (&val);
316 return ret_error;
317 }
318
319
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
320 ret_t
b72ddd2 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@622 5dc97367-97…
alobbs authored
321 cherokee_config_reader_parse (cherokee_config_node_t *conf, cherokee_buffer_t *path)
322 {
323 return do_include (conf, path);
324 }
Something went wrong with that request. Please try again.