Sun, 15 May 2022 08:56:00 +0200
make sure the http stream is finished if headers are sent
256
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1 | /* |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
3 | * |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
4 | * Copyright 2020 Olaf Wintermann. All rights reserved. |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
5 | * |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
8 | * |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
11 | * |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
15 | * |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
27 | */ |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
28 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
29 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
30 | #include "serverconfig.h" |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
31 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
32 | #include <stdio.h> |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
33 | #include <stdlib.h> |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
34 | #include <string.h> |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
35 | #include <ctype.h> |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
36 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
37 | #include <ucx/buffer.h> |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
38 | #include <ucx/utils.h> |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
39 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
40 | ServerConfig* serverconfig_load(const char *file) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
41 | FILE *in = fopen(file, "r"); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
42 | if(in == NULL) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
43 | return NULL; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
44 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
45 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
46 | UcxBuffer *buf = ucx_buffer_new(NULL, 4096, UCX_BUFFER_AUTOEXTEND); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
47 | if(!buf) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
48 | fclose(in); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
49 | return NULL; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
50 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
51 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
52 | ucx_stream_copy(in, buf, (read_func)fread, (write_func)ucx_buffer_write); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
53 | fclose(in); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
54 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
55 | ServerConfig *scfg = serverconfig_parse(scstrn(buf->space, buf->size)); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
56 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
57 | ucx_buffer_free(buf); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
58 | return scfg; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
59 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
60 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
61 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
62 | static CFGToken get_next_token(scstr_t content, int *pos) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
63 | CFGToken token = { {NULL, 0}, CFG_NO_TOKEN }; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
64 | CFGTokenType type = CFG_TOKEN; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
65 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
66 | int start = *pos; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
67 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
68 | int token_begin = -1; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
69 | int token_end = content.length-1; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
70 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
71 | int quote = 0; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
72 | int comment = 0; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
73 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
74 | int i; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
75 | char prev = 0; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
76 | for(i=start;i<content.length;i++) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
77 | char c = content.ptr[i]; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
78 | if(c == '\n') { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
79 | if(quote) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
80 | *pos = i; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
81 | return token; // error |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
82 | } else if(start == i) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
83 | // single newline char token |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
84 | type = CFG_TOKEN_NEWLINE; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
85 | token_begin = i; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
86 | token_end = i+1; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
87 | break; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
88 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
89 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
90 | token_end = i; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
91 | if(token_begin < 0) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
92 | // only space/comment token |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
93 | token_begin = start; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
94 | type = comment ? CFG_TOKEN_COMMENT : CFG_TOKEN_SPACE; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
95 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
96 | // make sure next run will return current newline char as token |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
97 | i--; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
98 | break; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
99 | } else if(quote) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
100 | if(c == '"' && prev != '\\') { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
101 | quote = 0; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
102 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
103 | } else if(comment) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
104 | // ignore |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
105 | if(c == '\n') { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
106 | comment = 0; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
107 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
108 | } else if(c == '#') { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
109 | comment = 1; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
110 | } else if(isspace(c)) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
111 | if(token_begin >= 0) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
112 | token_end = i; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
113 | break; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
114 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
115 | } else if(c == '"') { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
116 | quote = 1; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
117 | if(token_begin < 0) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
118 | token_begin = i; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
119 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
120 | } else if(token_begin < 0) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
121 | token_begin = i; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
122 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
123 | prev = c; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
124 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
125 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
126 | *pos = i + 1; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
127 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
128 | if(token_begin < 0) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
129 | return token; // error |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
130 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
131 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
132 | token.type = type; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
133 | token.content = scstrsubsl(content, token_begin, token_end - token_begin); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
134 | return token; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
135 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
136 | |
258
134279e804b6
remove old server.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
256
diff
changeset
|
137 | /* |
256
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
138 | static void test_print_config(ConfigNode *parent) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
139 | UCX_FOREACH(elm, parent->children) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
140 | ConfigNode *node = elm->data; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
141 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
142 | if(node->type == CONFIG_NODE_SPACE) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
143 | printf("sp: %s", node->text_begin.ptr); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
144 | } else if(node->type == CONFIG_NODE_COMMENT) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
145 | printf("cm: %s", node->text_begin.ptr); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
146 | } else if(node->type == CONFIG_NODE_OBJECT) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
147 | printf("o{: %s : %s", node->name.ptr, node->text_begin.ptr); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
148 | test_print_config(node); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
149 | printf("o}: %s", node->text_end.ptr); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
150 | } else if(node->type == CONFIG_NODE_DIRECTIVE) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
151 | printf("di: %s", node->text_begin.ptr); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
152 | } else { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
153 | printf("fk: %s", node->text_begin.ptr); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
154 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
155 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
156 | } |
258
134279e804b6
remove old server.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
256
diff
changeset
|
157 | */ |
256
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
158 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
159 | ServerConfig* serverconfig_parse(scstr_t content) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
160 | UcxMempool *mp = ucx_mempool_new(512); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
161 | if(!mp) return NULL; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
162 | UcxAllocator *a = mp->allocator; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
163 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
164 | ServerConfig *config = ucx_mempool_malloc(mp, sizeof(ServerConfig)); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
165 | if(!config) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
166 | ucx_mempool_destroy(mp); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
167 | return NULL; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
168 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
169 | config->mp = mp; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
170 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
171 | // PARSE: |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
172 | // first non space/comment token is directive/object name |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
173 | // following tokens are arguments |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
174 | // newline starts new directive |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
175 | // '{' converts directive to object and following directives will |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
176 | // be placed into the object |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
177 | int pos = 0; // needed for tokenizer |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
178 | CFGToken token; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
179 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
180 | ConfigNode *root_obj = ucx_mempool_calloc(mp, 1, sizeof(ConfigNode)); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
181 | root_obj->type = CONFIG_NODE_OBJECT; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
182 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
183 | UcxList *node_stack = ucx_list_prepend(NULL, root_obj); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
184 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
185 | ConfigNode *current = ucx_mempool_calloc(mp, 1, sizeof(ConfigNode)); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
186 | current->type = CONFIG_NODE_SPACE; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
187 | ConfigNode *obj = NULL; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
188 | int obj_closed = 0; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
189 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
190 | int text_start = 0; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
191 | int err = 0; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
192 | while((token = get_next_token(content, &pos)).type != CFG_NO_TOKEN) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
193 | //printf("%s [%.*s]\n", token_type_str(token.type), (int)token.content.length, token.content.ptr); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
194 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
195 | switch(token.type) { |
269
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
258
diff
changeset
|
196 | case CFG_NO_TOKEN: break; |
256
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
197 | case CFG_TOKEN_COMMENT: { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
198 | if(current->type == CONFIG_NODE_SPACE) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
199 | current->type = CONFIG_NODE_COMMENT; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
200 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
201 | break; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
202 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
203 | case CFG_TOKEN_SPACE: break; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
204 | case CFG_TOKEN_NEWLINE: { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
205 | scstr_t line = scstrsubsl(content, text_start, pos - text_start); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
206 | text_start = pos; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
207 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
208 | sstr_t line_cp = sstrdup_a(a, line); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
209 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
210 | ConfigNode *parent = node_stack->data; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
211 | if(current->type == CONFIG_NODE_CLOSE_OBJECT) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
212 | parent->text_end = line_cp; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
213 | node_stack = ucx_list_remove_a(a, node_stack, node_stack); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
214 | } else if(current->type == CONFIG_NODE_OPEN_OBJECT) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
215 | sstr_t new_textbegin = sstrcat_a(a, 2, obj->text_begin, line_cp); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
216 | alfree(a, obj->text_begin.ptr); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
217 | alfree(a, line_cp.ptr); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
218 | obj->text_begin = new_textbegin; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
219 | } else { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
220 | current->text_begin = line_cp; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
221 | ConfigNode *parent = node_stack->data; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
222 | parent->children = ucx_list_append_a(a, parent->children, current); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
223 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
224 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
225 | if(obj && obj->type == CONFIG_NODE_OBJECT) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
226 | node_stack = ucx_list_prepend_a(a, node_stack, obj); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
227 | obj = NULL; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
228 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
229 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
230 | current = ucx_mempool_calloc(mp, 1, sizeof(ConfigNode)); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
231 | current->type = CONFIG_NODE_SPACE; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
232 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
233 | obj_closed = 0; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
234 | break; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
235 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
236 | case CFG_TOKEN: { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
237 | if(!sstrcmp(token.content, S("{"))) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
238 | if(!obj) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
239 | err = 1; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
240 | break; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
241 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
242 | obj->type = CONFIG_NODE_OBJECT; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
243 | if(current != obj) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
244 | current->type = CONFIG_NODE_OPEN_OBJECT; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
245 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
246 | } else if(!sstrcmp(token.content, S("}"))) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
247 | obj_closed = 1; // force newline before next directive |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
248 | obj = NULL; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
249 | current->type = CONFIG_NODE_CLOSE_OBJECT; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
250 | } else { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
251 | if(obj_closed) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
252 | err = 1; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
253 | break; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
254 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
255 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
256 | if(!current->name.ptr) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
257 | current->name = sstrdup_a(a, token.content); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
258 | current->type = CONFIG_NODE_DIRECTIVE; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
259 | obj = current; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
260 | } else { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
261 | ConfigArg *arg = ucx_mempool_calloc(mp, 1, sizeof(ConfigArg)); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
262 | // TODO: add support for key/value |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
263 | arg->value = sstrdup_a(a, token.content); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
264 | current->args = ucx_list_append_a(a, current->args, arg); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
265 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
266 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
267 | break; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
268 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
269 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
270 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
271 | if(err) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
272 | break; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
273 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
274 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
275 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
276 | if(pos < content.length || err) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
277 | // content not fully parsed because of an error |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
278 | ucx_mempool_destroy(mp); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
279 | return NULL; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
280 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
281 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
282 | //test_print_config(&root_obj); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
283 | config->root = root_obj; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
284 | config->tab = sstrdup_a(a, SC("\t")); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
285 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
286 | return config; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
287 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
288 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
289 | void serverconfig_free(ServerConfig *cfg) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
290 | ucx_mempool_destroy(cfg->mp); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
291 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
292 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
293 | ConfigNode* serverconfig_get_node(ConfigNode *parent, ConfigNodeType type, scstr_t name) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
294 | UCX_FOREACH(elm, parent->children) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
295 | ConfigNode *node = elm->data; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
296 | if(node->type == type && !sstrcmp(node->name, name)) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
297 | return node; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
298 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
299 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
300 | return NULL; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
301 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
302 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
303 | UcxList* serverconfig_get_node_list(ConfigNode *parent, ConfigNodeType type, scstr_t name) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
304 | UcxList *nodes = NULL; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
305 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
306 | UCX_FOREACH(elm, parent->children) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
307 | ConfigNode *node = elm->data; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
308 | if(node->type == type && !sstrcmp(node->name, name)) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
309 | nodes = ucx_list_append(nodes, node); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
310 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
311 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
312 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
313 | return nodes; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
314 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
315 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
316 | scstr_t serverconfig_directive_value(ConfigNode *obj, scstr_t name) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
317 | ConfigNode *node = serverconfig_get_node(obj, CONFIG_NODE_DIRECTIVE, name); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
318 | if(node && ucx_list_size(node->args) == 1) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
319 | ConfigArg *arg = node->args->data; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
320 | return SCSTR(arg->value); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
321 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
322 | return scstrn(NULL, 0); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
323 | } |
269
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
258
diff
changeset
|
324 | |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
258
diff
changeset
|
325 | sstr_t serverconfig_arg_name_value(UcxAllocator *a, scstr_t str, scstr_t *name) { |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
258
diff
changeset
|
326 | int valstart = 0; |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
258
diff
changeset
|
327 | for(int i=0;i<str.length;i++) { |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
258
diff
changeset
|
328 | if(str.ptr[i] == '=') { |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
258
diff
changeset
|
329 | if(name) { |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
258
diff
changeset
|
330 | name->ptr = str.ptr; |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
258
diff
changeset
|
331 | name->length = i; |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
258
diff
changeset
|
332 | } |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
258
diff
changeset
|
333 | valstart = i + 1; |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
258
diff
changeset
|
334 | break; |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
258
diff
changeset
|
335 | } |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
258
diff
changeset
|
336 | } |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
258
diff
changeset
|
337 | |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
258
diff
changeset
|
338 | sstr_t ret; |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
258
diff
changeset
|
339 | return ret; |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
258
diff
changeset
|
340 | } |