|
1 /* |
|
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
3 * |
|
4 * Copyright 2011 Olaf Wintermann. All rights reserved. |
|
5 * |
|
6 * Redistribution and use in source and binary forms, with or without |
|
7 * modification, are permitted provided that the following conditions are met: |
|
8 * |
|
9 * 1. Redistributions of source code must retain the above copyright |
|
10 * notice, this list of conditions and the following disclaimer. |
|
11 * |
|
12 * 2. Redistributions in binary form must reproduce the above copyright |
|
13 * notice, this list of conditions and the following disclaimer in the |
|
14 * documentation and/or other materials provided with the distribution. |
|
15 * |
|
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
26 * POSSIBILITY OF SUCH DAMAGE. |
|
27 */ |
|
28 |
|
29 #include "../public/nsapi.h" |
|
30 |
|
31 #include <stdio.h> |
|
32 #include <stdlib.h> |
|
33 |
|
34 #include <fcntl.h> |
|
35 #include <sys/types.h> |
|
36 #include <sys/file.h> |
|
37 #include <sys/stat.h> |
|
38 #include <sys/mman.h> |
|
39 |
|
40 #include "../ucx/string.h" |
|
41 |
|
42 #include "httplistener.h" |
|
43 #include "config.h" |
|
44 #include "func.h" |
|
45 |
|
46 #include "vserver.h" |
|
47 #include "../util/pblock.h" |
|
48 |
|
49 VirtualServer *default_vs; |
|
50 |
|
51 pool_handle_t *cfg_pool; |
|
52 |
|
53 // TODO: Funktion für ConfigDirective -> directive |
|
54 // TODO: Funktion für UcxList parameter list -> pblock |
|
55 // TODO: ConfigurationManager |
|
56 // TODO: server.conf |
|
57 |
|
58 UcxMap *server_conf_handlers; // type: ServerConfigHandler* |
|
59 |
|
60 void load_init_conf(char *file) { |
|
61 printf("load_init_conf\n"); |
|
62 |
|
63 InitConfig *cfg = load_init_config("conf/init.conf"); |
|
64 if(cfg == NULL) { |
|
65 return; |
|
66 } |
|
67 |
|
68 cfg_pool = pool_create(); // one pool for one Configuration |
|
69 UcxDlist *dirs = cfg->directives; |
|
70 while(dirs != NULL) { |
|
71 ConfigDirective *dir = dirs->data; |
|
72 |
|
73 /* create NSAPI directive */ |
|
74 directive *d = malloc(sizeof(directive)); |
|
75 d->param = pblock_create_pool(cfg_pool, 8); |
|
76 UcxList *param = dir->param; |
|
77 while(param != NULL) { |
|
78 ConfigParam *p = param->data; |
|
79 pblock_nvlinsert( |
|
80 p->name.ptr, |
|
81 p->name.length, |
|
82 p->value.ptr, |
|
83 p->value.length, |
|
84 d->param); |
|
85 param = param->next; |
|
86 } |
|
87 |
|
88 /* get function */ |
|
89 char *func_name = pblock_findval("fn", d->param); |
|
90 d->func = get_function(func_name); |
|
91 if(d->func == NULL) { |
|
92 free(d); |
|
93 continue; |
|
94 } |
|
95 |
|
96 /* execute init directive */ |
|
97 d->func->func(d->param, NULL, NULL); |
|
98 |
|
99 dirs = dirs->next; |
|
100 } |
|
101 |
|
102 free_init_config(cfg); |
|
103 } |
|
104 |
|
105 void load_server_conf(char *file) { |
|
106 printf("load_server_conf\n"); |
|
107 |
|
108 init_default_server_conf_handlers(); |
|
109 |
|
110 ServerConfig *serverconf = load_server_config("conf/server.conf"); |
|
111 if(serverconf == NULL) { |
|
112 fprintf(stderr, "Cannot load server.conf\n"); |
|
113 } |
|
114 ServerConfiguration *serverconfig = malloc(sizeof(ServerConfiguration)); |
|
115 // TODO: init serverconfig stuff |
|
116 |
|
117 /* convert ServerConfig to ServerConfiguration */ |
|
118 for(int i=0;i<serverconf->objects->size;i++) { |
|
119 UcxMapElement *elm = &serverconf->objects->map[i]; |
|
120 while(elm != NULL) { |
|
121 UcxList *list = elm->data; |
|
122 while(list != NULL) { |
|
123 ServerConfigObject *scfgobj = list->data; |
|
124 |
|
125 /* get handler */ |
|
126 ServerConfigHandler *handler = ucx_map_sstr_get( |
|
127 server_conf_handlers, |
|
128 scfgobj->type); |
|
129 if(handler != NULL) { |
|
130 /* process the directives */ |
|
131 UcxDlist *dirs = scfgobj->directives; |
|
132 while(dirs != NULL) { |
|
133 handler->process_directive( |
|
134 handler, |
|
135 serverconfig, |
|
136 dirs->data); |
|
137 |
|
138 dirs = dirs->next; |
|
139 } |
|
140 } |
|
141 |
|
142 list = list->next; |
|
143 } |
|
144 |
|
145 elm = elm->next; |
|
146 } |
|
147 } |
|
148 |
|
149 |
|
150 ListenerConfig *conf = malloc(sizeof(ListenerConfig)); |
|
151 conf->port = 9090; |
|
152 conf->nacceptors = 1; |
|
153 conf->name = "default"; |
|
154 |
|
155 http_listener_new(conf); |
|
156 |
|
157 // virtual server |
|
158 default_vs = vs_new(); |
|
159 // load obj.conf |
|
160 default_vs->objects = load_obj_conf("conf/obj.conf"); |
|
161 default_vs->default_obj_name = "default"; |
|
162 |
|
163 } |
|
164 |
|
165 void init_default_server_conf_handlers() { |
|
166 /* create handler map */ |
|
167 server_conf_handlers = ucx_map_new(8); |
|
168 |
|
169 /* create and add default handlers */ |
|
170 |
|
171 ServerConfigHandler *runtime = malloc(sizeof(ServerConfigHandler)); |
|
172 runtime->init = NULL; |
|
173 runtime->process_directive = handle_runtime_directive; |
|
174 |
|
175 ucx_map_cstr_put(server_conf_handlers, "Runtime", runtime); |
|
176 } |
|
177 |
|
178 int handle_runtime_directive( |
|
179 ServerConfigHandler *h, |
|
180 ServerConfiguration *conf, |
|
181 ConfigDirective *dir) |
|
182 { |
|
183 |
|
184 |
|
185 return 0; |
|
186 } |
|
187 |
|
188 VirtualServer* conf_get_default_vs() { |
|
189 return default_vs; |
|
190 } |
|
191 |
|
192 HTTPObjectConfig* load_obj_conf(char *file) { |
|
193 printf("load_obj_conf\n"); |
|
194 |
|
195 // new conf function test |
|
196 ObjectConfig *cfg = load_object_config(file); |
|
197 if(cfg == NULL) { |
|
198 return NULL; |
|
199 } |
|
200 |
|
201 /* create object config */ |
|
202 HTTPObjectConfig *conf = calloc(sizeof(HTTPObjectConfig), 1); |
|
203 conf->pool = cfg_pool; |
|
204 |
|
205 /* convert ObjectConfig to HTTPObjectConfig */ |
|
206 |
|
207 /* add objects */ |
|
208 conf->nobj = ucx_dlist_size(cfg->objects); |
|
209 conf->objects = calloc(1, sizeof(httpd_object*)); |
|
210 |
|
211 UcxDlist *objlist = cfg->objects; |
|
212 int i = 0; |
|
213 while(objlist != NULL) { |
|
214 ConfigObject *cob = objlist->data; |
|
215 |
|
216 /* get name and ppath */ |
|
217 char *name = NULL; |
|
218 char *ppath = NULL; |
|
219 if(cob->name.length > 0) { |
|
220 name = sstrdub(cob->name).ptr; |
|
221 } |
|
222 if(cob->ppath.length > 0) { |
|
223 ppath = sstrdub(cob->ppath).ptr; |
|
224 } |
|
225 |
|
226 /* create and add object */ |
|
227 httpd_object *obj = object_new(name); |
|
228 obj->path = NULL; |
|
229 |
|
230 conf->objects[i] = obj; |
|
231 |
|
232 /* add directives */ |
|
233 for(int i=0;i<6;i++) { |
|
234 UcxDlist *dirs = cob->directives[i]; |
|
235 while(dirs != NULL) { |
|
236 ConfigDirective *cfgdir = dirs->data; |
|
237 |
|
238 directive *d = malloc(sizeof(directive)); |
|
239 d->cond = NULL; |
|
240 d->param = pblock_create_pool(conf->pool, 8); |
|
241 |
|
242 /* add params */ |
|
243 UcxList *param = cfgdir->param; |
|
244 while(param != NULL) { |
|
245 ConfigParam *p = param->data; |
|
246 pblock_nvlinsert( |
|
247 p->name.ptr, |
|
248 p->name.length, |
|
249 p->value.ptr, |
|
250 p->value.length, |
|
251 d->param); |
|
252 param = param->next; |
|
253 } |
|
254 |
|
255 /* get function */ |
|
256 char *func_name = pblock_findval("fn", d->param); |
|
257 d->func = get_function(func_name); |
|
258 |
|
259 dirs = dirs->next; |
|
260 |
|
261 /* add function to dtable */ |
|
262 object_add_directive(obj, d, cfgdir->type_num); |
|
263 } |
|
264 } |
|
265 |
|
266 /* next */ |
|
267 i++; |
|
268 objlist = objlist->next; |
|
269 } |
|
270 |
|
271 free_object_config(cfg); |
|
272 |
|
273 return conf; |
|
274 } |