src/server/util/object.c

changeset 14
b8bf95b39952
parent 13
1fdbf4170ef4
child 44
3da1f7b6847f
equal deleted inserted replaced
13:1fdbf4170ef4 14:b8bf95b39952
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 "object.h"
32
33 #include "pool.h"
34 #include "../daemon/func.h"
35
36
37
38 httpd_object* object_new(char *name) {
39 // TODO: Speicherverwaltung
40 httpd_object *obj = malloc(sizeof(httpd_object));
41 obj->name = name;
42 obj->path = NULL;
43
44 // create directive table
45 obj->dt = calloc(sizeof(struct dtable), NUM_NSAPI_TYPES - 1);
46 obj->nd = NUM_NSAPI_TYPES - 1;
47
48 return obj;
49 }
50
51 void object_free(httpd_object *obj) {
52 free(obj->name);
53 // TODO: free objects
54 free(obj->dt);
55 }
56
57
58 void object_add_directive(httpd_object *obj, directive *dir, int dt) {
59 dtable *l = object_get_dtable(obj, dt);
60 // allocate space for the new directive
61
62 //l->dirs = realloc(l->dirs, (l->ndir+1)*sizeof(void*));
63 /* TODO: aus irgend einem Grund funktioniert realloc nicht. warum?? */
64
65 directive **drs = malloc((l->ndir+1)*sizeof(void*));
66 for(int i=0;i<l->ndir;i++) {
67 drs[i] = l->dirs[i];
68 }
69 if(l->dirs != NULL) {
70 free(l->dirs);
71 }
72 l->dirs = drs;
73
74 // add directive
75 l->dirs[l->ndir] = dir;
76 l->ndir++;
77 }
78
79
80 /* objset functions */
81 httpd_objset* objset_create(pool_handle_t *pool) {
82 httpd_objset *os = pool_malloc(pool, sizeof(httpd_objset));
83
84 os->obj = pool_calloc(pool, 2, sizeof(void*));
85 os->pos = 0;
86
87 return os;
88 }
89
90 void objset_add_object(pool_handle_t *p, httpd_objset *os, httpd_object *obj) {
91 if(os->pos != 0 && os->pos % 2 == 0) {
92 os->obj = pool_realloc(p, os->obj, (os->pos + 2) * sizeof(void*));
93 }
94 os->obj[os->pos] = obj;
95 os->pos++;
96 }
97
98
99
100
101 httpd_objset* create_test_objset() {
102 httpd_objset *objset = malloc(sizeof(httpd_objset));
103 objset->obj = calloc(1, sizeof(httpd_object*));
104
105 httpd_object *obj = object_new("default");
106 objset->obj[0] = obj;
107 objset->pos = 1;
108
109 directive *d1 = malloc(sizeof(directive));
110 d1->func = get_function("test-nametrans");
111 d1->param = NULL;
112 object_add_directive(obj, d1, NSAPINameTrans);
113
114 directive *d2 = malloc(sizeof(directive));
115 d2->func = get_function("test-service");
116 d2->param = NULL;
117 object_add_directive(obj, d2, NSAPIService);
118
119 return objset;
120 }
121
122
123 void httpobjconf_add_object(HTTPObjectConfig *conf, httpd_object *obj) {
124 conf->nobj++;
125 conf->objects = realloc(conf->objects, conf->nobj * sizeof(void*));
126 conf->objects[conf->nobj - 1] = obj;
127 }
128
129
130 void nsapi_context_next_stage(NSAPIContext *context) {
131 context->dtable_index = 0;
132 context->objset_index = -1;
133 context->last_req_code = REQ_NOACTION;
134 }

mercurial