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 "conf.h" |
|
44 #include "func.h" |
|
45 |
|
46 #include "vserver.h" |
|
47 #include "../util/pblock.h" |
|
48 |
|
49 #include "../config/objconf.h" |
|
50 #include "../config/initconf.h" |
|
51 |
|
52 VirtualServer *default_vs; |
|
53 |
|
54 pool_handle_t *cfg_pool; |
|
55 |
|
56 // TODO: Funktion für ConfigDirective -> directive |
|
57 // TODO: Funktion für UcxList parameter list -> pblock |
|
58 // TODO: ConfigurationManager |
|
59 // TODO: server.conf |
|
60 |
|
61 void load_init_conf(char *file) { |
|
62 printf("load_init_conf\n"); |
|
63 |
|
64 InitConfig *cfg = load_init_config("conf/init.conf"); |
|
65 if(cfg == NULL) { |
|
66 return; |
|
67 } |
|
68 |
|
69 cfg_pool = pool_create(); // one pool for one Configuration |
|
70 UcxDlist *dirs = cfg->directives; |
|
71 while(dirs != NULL) { |
|
72 ConfigDirective *dir = dirs->data; |
|
73 |
|
74 /* create NSAPI directive */ |
|
75 directive *d = malloc(sizeof(directive)); |
|
76 d->param = pblock_create_pool(cfg_pool, 8); |
|
77 UcxList *param = dir->param; |
|
78 while(param != NULL) { |
|
79 ConfigParam *p = param->data; |
|
80 pblock_nvlinsert( |
|
81 p->name.ptr, |
|
82 p->name.length, |
|
83 p->value.ptr, |
|
84 p->value.length, |
|
85 d->param); |
|
86 param = param->next; |
|
87 } |
|
88 |
|
89 /* get function */ |
|
90 char *func_name = pblock_findval("fn", d->param); |
|
91 d->func = get_function(func_name); |
|
92 if(d->func == NULL) { |
|
93 free(d); |
|
94 continue; |
|
95 } |
|
96 |
|
97 /* execute init directive */ |
|
98 d->func->func(d->param, NULL, NULL); |
|
99 |
|
100 dirs = dirs->next; |
|
101 } |
|
102 |
|
103 free_init_config(cfg); |
|
104 } |
|
105 |
|
106 void load_server_conf(char *file) { |
|
107 printf("load_server_conf\n"); |
|
108 |
|
109 ListenerConfig *conf = malloc(sizeof(ListenerConfig)); |
|
110 conf->port = 9090; |
|
111 conf->nacceptors = 1; |
|
112 conf->name = "default"; |
|
113 |
|
114 http_listener_new(conf); |
|
115 |
|
116 // virtual server |
|
117 default_vs = vs_new(); |
|
118 // load obj.conf |
|
119 default_vs->objects = load_obj_conf("conf/obj.conf"); |
|
120 default_vs->default_obj_name = "default"; |
|
121 |
|
122 } |
|
123 |
|
124 VirtualServer* conf_get_default_vs() { |
|
125 return default_vs; |
|
126 } |
|
127 |
|
128 HTTPObjectConfig* load_obj_conf(char *file) { |
|
129 printf("load_obj_conf\n"); |
|
130 |
|
131 // new conf function test |
|
132 ObjectConfig *cfg = load_object_config(file); |
|
133 if(cfg == NULL) { |
|
134 return NULL; |
|
135 } |
|
136 |
|
137 /* create object config */ |
|
138 HTTPObjectConfig *conf = calloc(sizeof(HTTPObjectConfig), 1); |
|
139 conf->pool = cfg_pool; |
|
140 |
|
141 /* convert ObjectConfig to HTTPObjectConfig */ |
|
142 |
|
143 /* add objects */ |
|
144 conf->nobj = ucx_dlist_size(cfg->objects); |
|
145 conf->objects = calloc(1, sizeof(httpd_object*)); |
|
146 |
|
147 UcxDlist *objlist = cfg->objects; |
|
148 int i = 0; |
|
149 while(objlist != NULL) { |
|
150 ConfigObject *cob = objlist->data; |
|
151 |
|
152 /* get name and ppath */ |
|
153 char *name = NULL; |
|
154 char *ppath = NULL; |
|
155 if(cob->name.length > 0) { |
|
156 name = sstrdub(cob->name).ptr; |
|
157 } |
|
158 if(cob->ppath.length > 0) { |
|
159 ppath = sstrdub(cob->ppath).ptr; |
|
160 } |
|
161 |
|
162 /* create and add object */ |
|
163 httpd_object *obj = object_new(name); |
|
164 obj->path = NULL; |
|
165 |
|
166 conf->objects[i] = obj; |
|
167 |
|
168 /* add directives */ |
|
169 for(int i=0;i<6;i++) { |
|
170 UcxDlist *dirs = cob->directives[i]; |
|
171 while(dirs != NULL) { |
|
172 ConfigDirective *cfgdir = dirs->data; |
|
173 |
|
174 directive *d = malloc(sizeof(directive)); |
|
175 d->cond = NULL; |
|
176 d->param = pblock_create_pool(conf->pool, 8); |
|
177 |
|
178 /* add params */ |
|
179 UcxList *param = cfgdir->param; |
|
180 while(param != NULL) { |
|
181 ConfigParam *p = param->data; |
|
182 pblock_nvlinsert( |
|
183 p->name.ptr, |
|
184 p->name.length, |
|
185 p->value.ptr, |
|
186 p->value.length, |
|
187 d->param); |
|
188 param = param->next; |
|
189 } |
|
190 |
|
191 /* get function */ |
|
192 char *func_name = pblock_findval("fn", d->param); |
|
193 d->func = get_function(func_name); |
|
194 |
|
195 dirs = dirs->next; |
|
196 |
|
197 /* add function to dtable */ |
|
198 object_add_directive(obj, d, cfgdir->type_num); |
|
199 } |
|
200 } |
|
201 |
|
202 /* next */ |
|
203 i++; |
|
204 objlist = objlist->next; |
|
205 } |
|
206 |
|
207 free_object_config(cfg); |
|
208 |
|
209 return conf; |
|
210 } |
|