summaryrefslogtreecommitdiffabout
path: root/include/sitecing/configuration.h
Unidiff
Diffstat (limited to 'include/sitecing/configuration.h') (more/less context) (ignore whitespace changes)
-rw-r--r--include/sitecing/configuration.h459
1 files changed, 459 insertions, 0 deletions
diff --git a/include/sitecing/configuration.h b/include/sitecing/configuration.h
new file mode 100644
index 0000000..330a5a6
--- a/dev/null
+++ b/include/sitecing/configuration.h
@@ -0,0 +1,459 @@
1#ifndef __SITECING_CONFIGURATION_H
2#define __SITECING_CONFIGURATION_H
3
4#include <sys/types.h>
5#include <sys/stat.h>
6#include <string>
7#include <list>
8#include <map>
9#include <pcre++.h>
10
11/**
12 * @file
13 * @brief Classes related to configuration.
14 */
15
16namespace sitecing {
17 using namespace std;
18
19 class configuration;
20
21 /**
22 * The config options container class.
23 */
24 class config_options {
25 public:
26 /**
27 * The flags enumeration.
28 */
29 enum _flags {
30 /**
31 * Skeleton has been specified in the context.
32 * @see skeleton
33 */
34 flag_skeleton = 0x0001,
35 /**
36 * CPPFLAGS have been specified in the context
37 * @see cpp_flags
38 */
39 flag_cpp_flags = 0x0002,
40 /**
41 * LDFLAGS have been specified in the context.
42 * @see ld_flags
43 */
44 flag_ld_flags = 0x0004,
45 /**
46 * Enforced intermediate dependencies have been specified in the
47 * context.
48 * @see internediate_deps
49 */
50 flag_intermediate_deps = 0x0008,
51 /**
52 * Enforced .so dependencies have been specified in the context.
53 * @see so_deps
54 */
55 flag_so_deps = 0x0010,
56 /**
57 * Whether components should be built specified in the context.
58 * @see build
59 */
60 flag_build = 0x0020,
61 /**
62 * Whether to track down cpp dependencies has been specified in
63 * the context.
64 * @see cpp_deps
65 */
66 flag_cpp_deps = 0x0040,
67 /**
68 * The exception handler has been specified in the context.
69 * @see exception_handler
70 */
71 flag_exception_handler = 0x0080,
72 /**
73 * Action handlers have been specified in the context.
74 * @see action_handlers
75 */
76 flag_action_handlers = 0x0100,
77 /**
78 * HTTP status handerls have been specified in the context.
79 * @see http_status_handlers
80 */
81 flag_http_status_handlers = 0x0200,
82 /**
83 * The files to be built are specified in the context.
84 * @see auto_build_files
85 */
86 flag_auto_build_files = 0x0400
87 };
88 /**
89 * The flags specifying what parts of configuration have been
90 * specified for the context.
91 */
92 int flags;
93
94 /**
95 * The skeleton for building components.
96 */
97 string skeleton;
98 /**
99 * The flags to pass to compiler.
100 */
101 list<string> cpp_flags;
102 /**
103 * The flags to pass to linker.
104 */
105 list<string> ld_flags;
106 /**
107 * Whether to build inexstent and outdated components.
108 */
109 bool build;
110 /**
111 * Whether to track cpp dependencies.
112 */
113 bool cpp_deps;
114 /**
115 * The component handling caught exceptions.
116 */
117 string exception_handler;
118 /**
119 * Enforced intermediate dependencies.
120 */
121 list<string> intermediate_deps;
122 /**
123 * Enforced depencies for .so objects.
124 */
125 list<string> so_deps;
126 /**
127 * The action handler type.
128 */
129 struct action_handler_t {
130 /**
131 * The regexp to check request against.
132 */
133 string s_regex;
134 /**
135 * Precompiled regex.
136 */
137 pcrepp::Pcre regex;
138 /**
139 * The action handler component.
140 */
141 string action;
142 /**
143 * Arguments for the action hander coponent.
144 */
145 list<string> args;
146
147 action_handler_t(const string& s,const string& a)
148 : s_regex(s), regex(s), action(a) { }
149 action_handler_t(const action_handler_t& s)
150 : s_regex(s.s_regex), regex(s.regex), action (s.action), args(s.args) { }
151 };
152 /**
153 * Type for the list of action handlers.
154 */
155 typedef list<action_handler_t> action_handlers_t;
156 /**
157 * The list of action handlers.
158 */
159 action_handlers_t action_handlers;
160 /**
161 * Type for the map of HTTP status handler components.
162 */
163 typedef map<string,string> http_status_handlers_t;
164 /**
165 * The map of HTTP status handler components.
166 */
167 http_status_handlers_t http_status_handlers;
168 /**
169 * Files to be built automatically.
170 */
171 list<string> auto_build_files;
172
173 config_options()
174 : flags(0) { }
175
176 /**
177 * Look up if there is an action handler for the target defined.
178 * @param target the target component.
179 * @return the pointer to handler or zero.
180 */
181 action_handler_t *lookup_action_handler(const string& target);
182 /**
183 * Look up if there is a handler defined for the HTTP status.
184 * @param status HTTP status
185 * @return the handler component.
186 */
187 string lookup_http_status_handler(const string& status);
188 /**
189 * Check whether the file should be build automatically.
190 * @param fn file name.
191 * @param rv reference to the boolean where the answer should go.
192 * @return true if we know the answer.
193 */
194 bool match_autobuild_files(const char *fn,bool &rv);
195 };
196
197 /**
198 * Configuration data container for the configuration loaded from disk file.
199 */
200 class loaded_options : public config_options {
201 public:
202 /**
203 * The file where configuration originates from.
204 */
205 string source_file;
206 /**
207 * The stat structure for the source file as it was when we have
208 * loaded it.
209 */
210 struct stat st;
211
212 /**
213 * See if the data is still valid.
214 * @return true if yes.
215 */
216 bool is_valid();
217
218 /**
219 * Load the configuration file.
220 * @param config the main configuration container.
221 * @param the configuration file.
222 */
223 void parse(configuration *config,const string& cfile);
224 };
225
226 /**
227 * The main configuration container.
228 */
229 class configuration {
230 public:
231 /**
232 * @todo TODO:: document me.
233 */
234 bool autobuild;
235 /**
236 * The flags enumeration.
237 */
238 enum _flags {
239 /**
240 * Was the source root specified?
241 * @see root_source
242 */
243 flag_root_source = 0x00000001,
244 /**
245 * Was the root for intermediate files specified?
246 * @see root_intermediate
247 */
248 flag_root_intermediate = 0x00000002,
249 /**
250 * Was the root for the resulting .so files specified?
251 * @see root_so
252 */
253 flag_root_so = 0x00000004,
254 /**
255 * Was the socket to listen to specified?
256 * @see listen_socket
257 */
258 flag_listen_socket = 0x00000008,
259 /**
260 * Was the per-dir config file name specified.
261 * @see rc_file_name
262 */
263 flag_rc_file_name = 0x00000010,
264 /**
265 * Was the minimum number of child processes specified?
266 * @see min_children
267 */
268 flag_min_children = 0x00000020,
269 /**
270 * Was the maximum number of child processes specified?
271 * @see max_children
272 */
273 flag_max_children = 0x00000040,
274 /**
275 * Was the minimum number of spare child processes specified?
276 * @see min_spare_children
277 */
278 flag_min_spare_children = 0x00000080,
279 /**
280 * Was he maximum number of spare child processes specified?
281 * @see max_spare_children
282 */
283 flag_max_spare_children = 0x00000100,
284 /**
285 * Was the number of requests to handle per child process
286 * specified?
287 * @see requests_per_child
288 */
289 flag_requests_per_child = 0x00000200,
290 /**
291 * Was the multiprocess node (or it's absences) specified?
292 * @see multi_process
293 */
294 flag_multi_process = 0x00000400,
295 /**
296 * Was the user specified?
297 * @see user
298 */
299 flag_user = 0x00000800,
300 /**
301 * @Was the group specified?
302 * @see group
303 */
304 flag_group = 0x00001000,
305 /**
306 * Was the root to change to specified?
307 * @see chroot
308 */
309 flag_chroot = 0x00002000,
310 /**
311 * Was the file for storing PID specified?
312 * @see pidfile
313 */
314 flag_pid_file = 0x00004000,
315 /**
316 * Was it specified wether we should daemonize the process?
317 * @see daemonize
318 */
319 flag_daemonize = 0x00008000
320 };
321 /**
322 * The flags specifying what parts of the configuration has been
323 * loaded into the object.
324 */
325 long flags;
326
327 /**
328 * The root for the components source code.
329 */
330 string root_source;
331 /**
332 * The root for intermediate files.
333 */
334 string root_intermediate;
335 /**
336 * The root for .so files.
337 */
338 string root_so;
339 /**
340 * Socket to bind to
341 */
342 string listen_socket;
343 /**
344 * per-dir config file name.
345 */
346 string rc_file_name;
347 /**
348 * The minimum number of child processes in multiprocess mode.
349 */
350 int min_children;
351 /**
352 * The maxium number of child processes in multiprocess mode.
353 */
354 int max_children;
355 /**
356 * The minimum number of spare chidren in multiprocess mode.
357 */
358 int min_spare_children;
359 /**
360 * The maximum number of spare children in multiprocess mode.
361 */
362 int max_spare_children;
363 /**
364 * The number of requests the child process should handle before
365 * exiting.
366 */
367 int requests_per_child;
368 /**
369 * Whether we should run in multiprocess mode or not.
370 */
371 bool multi_process;
372 /**
373 * User to change to.
374 */
375 string user;
376 /**
377 * Group to set to.
378 */
379 string group;
380 /**
381 * Directory to change root to.
382 */
383 string chroot;
384 /**
385 * The file to store PID into.
386 */
387 string pid_file;
388 /**
389 * Whether we should fork into background.
390 */
391 bool daemonize;
392
393 typedef map<string,config_options> specs_t;
394 /**
395 * The local config options map.
396 */
397 specs_t specs;
398 typedef map<string,loaded_options> loaded_specs_t;
399 /**
400 * The local config options as specified in per-dir config files
401 * map.
402 */
403 loaded_specs_t loaded_specs;
404
405 configuration();
406 /**
407 * @param cfile the configuration file.
408 * @param ab @todo TODO:: document me
409 */
410 configuration(const string& cfile,bool ab=false);
411
412 /**
413 * Parse the configuration file.
414 * @param cfile the configuration file.
415 */
416 void parse(const string& cfile);
417
418 /**
419 * Fetch the reference to options for the very root.
420 */
421 config_options& root_options() { return specs[""]; }
422 /**
423 * Lookup where the certain config option for the target lies in.
424 * @param target the target component.
425 * @param flag the flag specifying the option we're looking for.
426 * @return the destination options continer or zero.
427 */
428 config_options* lookup_config(const string& target,int flag);
429 /**
430 * Lookup the action handler for the target.
431 * @param target the target request.
432 * @return the action handler or zero.
433 */
434 config_options::action_handler_t *lookup_action_handler(const string& target);
435 /**
436 * Lookup the HTPP status handler for the target.
437 * @param target the target.
438 * @param status the HTTP status.
439 * @return the handler component.
440 */
441 string lookup_http_status_handler(const string& target,const string& status);
442 /**
443 * Lookup the options loaded from per-dir config for the target
444 * @param target the target.
445 * @return options container or zero.
446 */
447 loaded_options* lookup_loaded_options(const string& target);
448 /**
449 * Check whether the components for the target should be prebuilt.
450 * @param target the target.
451 * @param fn file name.
452 * @return true if yes.
453 */
454 bool match_autobuild_files(const string& target,const char *fn);
455 };
456
457}
458
459#endif /* __SITECING_CONFIGURATION_H */