src/server/object.c

Fri, 30 Dec 2011 15:19:16 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 30 Dec 2011 15:19:16 +0100
changeset 11
24d804a2799f
parent 6
ce8fecc9847d
child 12
34aa8001ea53
permissions
-rw-r--r--

Fixed response header bug

3
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2011 Olaf Wintermann. All rights reserved.
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
137197831306 minimal request handling
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
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include "nsapi.h"
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 #include "object.h"
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32
6
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
33 #include "pool.h"
3
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 #include "func.h"
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 httpd_object* object_new(char *name) {
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 // TODO: Speicherverwaltung
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 httpd_object *obj = malloc(sizeof(httpd_object));
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 obj->name = name;
5
dbc01588686e Added parser for obj.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
42 obj->path = NULL;
3
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 // create directive table
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 obj->dt = calloc(sizeof(struct dtable), NUM_NSAPI_TYPES - 1);
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 obj->nd = NUM_NSAPI_TYPES - 1;
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 return obj;
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 }
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 void object_free(httpd_object *obj) {
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 free(obj->name);
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 // TODO: free objects
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 free(obj->dt);
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 }
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 void object_add_directive(httpd_object *obj, directive *dir, int dt) {
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 dtable *l = object_get_dtable(obj, dt);
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 // allocate space for the new directive
5
dbc01588686e Added parser for obj.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
61 l->dirs = realloc(l->dirs, (l->ndir+1)*sizeof(directive*));
3
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 // add directive
5
dbc01588686e Added parser for obj.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
63 l->dirs[l->ndir] = dir;
3
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 l->ndir++;
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 }
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67
6
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
68 /* objset functions */
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
69 httpd_objset* objset_create(pool_handle_t *pool) {
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
70 httpd_objset *os = pool_malloc(pool, sizeof(httpd_objset));
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
71
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
72 os->obj = pool_calloc(pool, 2, sizeof(void*));
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
73 os->pos = 0;
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
74
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
75 return os;
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
76 }
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
77
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
78 void objset_add_object(pool_handle_t *p, httpd_objset *os, httpd_object *obj) {
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
79 if(os->pos != 0 && os->pos % 2 == 0) {
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
80 os->obj = pool_realloc(p, os->obj, (os->pos + 2) * sizeof(void*));
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
81 }
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
82 os->obj[os->pos] = obj;
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
83 os->pos++;
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
84 }
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
85
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
86
3
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 httpd_objset* create_test_objset() {
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 httpd_objset *objset = malloc(sizeof(httpd_objset));
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 objset->obj = calloc(1, sizeof(httpd_object*));
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 httpd_object *obj = object_new("default");
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94 objset->obj[0] = obj;
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95 objset->pos = 1;
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97 directive *d1 = malloc(sizeof(directive));
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98 d1->func = get_function("test-nametrans");
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99 d1->param = NULL;
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100 object_add_directive(obj, d1, NSAPINameTrans);
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102 directive *d2 = malloc(sizeof(directive));
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103 d2->func = get_function("test-service");
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 d2->param = NULL;
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 object_add_directive(obj, d2, NSAPIService);
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 return objset;
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108 }
137197831306 minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109
5
dbc01588686e Added parser for obj.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
110
dbc01588686e Added parser for obj.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
111 void httpobjconf_add_object(HTTPObjectConfig *conf, httpd_object *obj) {
dbc01588686e Added parser for obj.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
112 conf->nobj++;
dbc01588686e Added parser for obj.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
113 conf->objects = realloc(conf->objects, conf->nobj * sizeof(void*));
dbc01588686e Added parser for obj.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
114 conf->objects[conf->nobj - 1] = obj;
dbc01588686e Added parser for obj.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
115 }
6
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
116
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
117
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
118 void nsapi_context_next_stage(NSAPIContext *context) {
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
119 context->dtable_index = 0;
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
120 context->objset_index = -1;
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
121 context->last_req_code = REQ_NOACTION;
ce8fecc9847d improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
122 }

mercurial