Tue, 27 Dec 2011 20:12:21 +0100
improved request processing
1 | 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 <stdio.h> | |
30 | #include <stdlib.h> | |
31 | ||
32 | #include "nsapi.h" | |
33 | #include "pool.h" | |
34 | #include "pblock.h" | |
35 | #include "io.h" | |
36 | #include "util.h" | |
37 | #include "httprequest.h" | |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
38 | #include "conf.h" |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
39 | #include "vserver.h" |
1 | 40 | |
41 | HTTPRequest *http_request_new() { | |
42 | HTTPRequest *req = malloc(sizeof(HTTPRequest)); | |
43 | req->connection = NULL; | |
44 | req->uri.ptr = NULL; | |
45 | ||
46 | HeaderArray *hd = malloc(sizeof(HeaderArray)); | |
47 | hd->next = NULL; | |
48 | hd->len = 0; | |
49 | hd->headers = calloc(16, sizeof(Header)); | |
50 | hd->alloc = 16; | |
51 | ||
52 | req->headers = hd; | |
53 | ||
54 | return req; | |
55 | } | |
56 | ||
57 | int handle_request(HTTPRequest *request) { | |
58 | // handle nsapi request | |
59 | ||
60 | // create pool | |
61 | request->pool = pool_create(); | |
62 | ||
63 | // create nsapi data structures | |
64 | NSAPISession *sn = malloc(sizeof(NSAPISession)); | |
65 | NSAPIRequest *rq = malloc(sizeof(NSAPIRequest)); | |
66 | request->rq = rq; | |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
67 | rq->phase = NSAPIAuthTrans; |
1 | 68 | |
69 | // fill session structure | |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
70 | sn->sys_fd = request->connection->fd; |
1 | 71 | sn->sn.pool = pool_create(); |
72 | sn->sn.csd = stream_new_from_fd(request->connection->fd); | |
73 | sn->sn.client = NULL; | |
74 | sn->sn.next = NULL; | |
75 | sn->sn.fill = 1; | |
76 | sn->sn.subject = NULL; | |
77 | ||
78 | // init NSAPI request structure | |
79 | if(request_initialize(request->pool, request, rq) != 0) { | |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
80 | printf("Cannot initialize request structure\n"); |
1 | 81 | return 1; |
82 | } | |
83 | ||
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
84 | // set default virtual server |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
85 | rq->vs = conf_get_default_vs(); |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
86 | |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
87 | |
1 | 88 | /* Pass request line as "clf-request" */ |
89 | pblock_kvinsert( | |
90 | pb_key_clf_request, | |
91 | request->request_line.ptr, | |
92 | request->request_line.length, | |
93 | rq->rq.reqpb); | |
94 | ||
95 | /* Pass method as "method" in reqpb, and also as method_num */ | |
96 | pblock_kvinsert( | |
97 | pb_key_method, | |
98 | request->method.ptr, | |
99 | request->method.length, | |
100 | rq->rq.reqpb); | |
101 | // TODO: method num | |
102 | //rqRq.rq.method_num = rqHdr->GetMethodNumber(); | |
103 | //PR_ASSERT(rqRq.rq.method_num != -1 || iStatus); | |
104 | ||
105 | /* Pass protocol as "protocol" in reqpb, and also in protv_num */ | |
106 | pblock_kvinsert( | |
107 | pb_key_protocol, | |
108 | request->httpv.ptr, | |
109 | request->httpv.length, | |
110 | rq->rq.reqpb); | |
111 | // TODO: protocol num | |
112 | ||
113 | /* Pass any query as "query" in reqpb */ | |
114 | // TODO: query | |
115 | ||
116 | /* Get abs_path part of request URI, and canonicalize the path */ | |
117 | sstr_t absPath = request->uri; | |
118 | // TODO: get abs_path | |
119 | absPath.ptr = util_canonicalize_uri( | |
120 | request->pool, | |
121 | absPath.ptr, | |
122 | absPath.length, | |
123 | (int*)&absPath.length); | |
124 | ||
125 | /* Decode the abs_path */ | |
126 | // TODO: decode abs_path | |
127 | ||
128 | /* Pass the abs_path as "uri" in reqpb */ | |
129 | // TODO: pass abs_path to reqpb | |
130 | // TODO: replace this code | |
131 | pblock_kvinsert( | |
132 | pb_key_uri, | |
133 | absPath.ptr, | |
134 | absPath.length, | |
135 | rq->rq.reqpb); | |
136 | ||
137 | // pass http header to the NSAPI request structure | |
138 | ||
139 | ||
140 | // Send the request to the NSAPI system | |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
141 | nsapi_handle_request(sn, rq); |
1 | 142 | |
143 | return 0; | |
144 | } | |
145 | ||
146 | ||
147 | ||
148 | void header_add(HeaderArray *hd, char *name, char *value) { | |
149 | while(hd->len >= hd->alloc) { | |
150 | if(hd->next == NULL) { | |
151 | HeaderArray *block = malloc(sizeof(HeaderArray)); | |
152 | block->next = NULL; | |
153 | block->len = 0; | |
154 | block->headers = calloc(16, sizeof(Header)); | |
155 | block->alloc = 16; | |
156 | hd->next = block; | |
157 | } | |
158 | hd = hd->next; | |
159 | } | |
160 | hd->headers[hd->len].name = name; | |
161 | hd->headers[hd->len].value = value; | |
162 | hd->len++; | |
163 | } | |
164 | ||
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
165 | |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
166 | /* |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
167 | * NSAPI Processing |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
168 | * TODO: add this to new file |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
169 | */ |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
170 | |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
171 | int nsapi_handle_request(NSAPISession *sn, NSAPIRequest *rq) { |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
172 | // TODO: threadpool |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
173 | |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
174 | int r = REQ_NOACTION; |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
175 | |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
176 | do { |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
177 | switch(rq->phase) { |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
178 | case NSAPIAuthTrans: { |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
179 | rq->phase++; |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
180 | nsapi_context_next_stage(&rq->context); |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
181 | } |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
182 | case NSAPINameTrans: { |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
183 | printf(">>> NameTrans\n"); |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
184 | r = nsapi_nametrans(sn, rq); |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
185 | if(r != REQ_PROCEED) { |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
186 | break; |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
187 | } |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
188 | rq->phase++; |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
189 | nsapi_context_next_stage(&rq->context); |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
190 | } |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
191 | case NSAPIPathCheck: { |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
192 | printf(">>> PathCheck\n"); |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
193 | rq->phase++; |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
194 | nsapi_context_next_stage(&rq->context); |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
195 | } |
5
dbc01588686e
Added parser for obj.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
3
diff
changeset
|
196 | case NSAPIObjectType: { |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
197 | printf(">>> ObjectType\n"); |
5
dbc01588686e
Added parser for obj.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
3
diff
changeset
|
198 | rq->phase++; |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
199 | nsapi_context_next_stage(&rq->context); |
5
dbc01588686e
Added parser for obj.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
3
diff
changeset
|
200 | } |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
201 | case NSAPIService: { |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
202 | printf(">>> Service\n"); |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
203 | r = nsapi_service(sn, rq); |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
204 | if(r != REQ_PROCEED) { |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
205 | break; |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
206 | } |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
207 | rq->phase++; |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
208 | nsapi_context_next_stage(&rq->context); |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
209 | } |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
210 | case NSAPIAddLog: { |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
211 | printf(">>> AddLog\n"); |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
212 | rq->phase++; |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
213 | nsapi_context_next_stage(&rq->context); |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
214 | } |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
215 | case REQ_FINISH: { |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
216 | printf(">>> Finish\n"); |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
217 | r = nsapi_finish_request(sn, rq); |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
218 | } |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
219 | } |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
220 | } while (r == REQ_RESTART); |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
221 | |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
222 | |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
223 | return r; |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
224 | } |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
225 | |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
226 | int nsapi_finish_request(NSAPISession *sn, NSAPIRequest *rq) { |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
227 | // TODO: free memory |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
228 | close(sn->sys_fd); |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
229 | |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
230 | return 0; |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
231 | } |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
232 | |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
233 | int nsapi_nametrans(NSAPISession *sn, NSAPIRequest *rq) { |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
234 | HTTPObjectConfig *objconf = rq->vs->objects; |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
235 | printf("nsapi_nametrans\n"); |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
236 | httpd_objset *objset = objset_create(sn->sn.pool); |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
237 | rq->rq.os = objset; |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
238 | /* first object in objconf is the default object TODO: make sure it is */ |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
239 | objset_add_object(sn->sn.pool, objset, objconf->objects[0]); |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
240 | |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
241 | httpd_object *obj = objset->obj[0]; /* nametrans only in default object */ |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
242 | dtable *dt = object_get_dtable(obj, NSAPINameTrans); |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
243 | |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
244 | /* execute directives */ |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
245 | int ret = rq->context.last_req_code; |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
246 | char *name = NULL; |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
247 | char *ppath = NULL; |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
248 | for(int i=NCX_DI(rq);i<dt->ndir;i++) { |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
249 | directive *d = dt->dirs[i]; |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
250 | |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
251 | printf("execute [%s]\n", d->func->name); |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
252 | ret = d->func->func(d->param, (Session*)sn, (Request*)rq); |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
253 | |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
254 | /* check for name or ppath */ |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
255 | name = pblock_findkeyval(pb_key_name, rq->rq.vars); |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
256 | ppath = pblock_findkeyval(pb_key_ppath, rq->rq.vars); |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
257 | |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
258 | /* add additional objects to the objset */ |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
259 | if(add_objects(objconf, objset, sn, rq, name, ppath) == REQ_ABORTED) { |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
260 | fprintf(stderr, "add_objects failed\n"); |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
261 | return REQ_ABORTED; |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
262 | } |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
263 | |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
264 | if(ret != REQ_NOACTION) { |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
265 | /* |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
266 | * if a saf is still processing, we need to save the context, to |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
267 | * process this object at a later time |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
268 | */ |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
269 | if(ret == REQ_PROCESSING) { |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
270 | /* save nsapi context */ |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
271 | /* add +1 to start next round with next function */ |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
272 | rq->context.dtable_index = i + 1; |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
273 | } |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
274 | |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
275 | return ret; |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
276 | } |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
277 | } |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
278 | |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
279 | /* if no function has set the ppath var, translate it to docroot */ |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
280 | if(ret == REQ_NOACTION && ppath == NULL) { |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
281 | sstr_t docroot = rq->vs->document_root; |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
282 | if(docroot.length < 1) { |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
283 | printf("docroot too short\n"); |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
284 | return REQ_ABORTED; /* docroot too short */ |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
285 | } |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
286 | /* if there is a trailing '/', remove it */ |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
287 | if(docroot.ptr[docroot.length - 1] == '/') { |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
288 | docroot.length--; |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
289 | } |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
290 | |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
291 | sstr_t uri = sstr(pblock_findkeyval(pb_key_uri, rq->rq.reqpb)); |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
292 | |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
293 | sstr_t translated; |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
294 | translated.length = docroot.length + uri.length; |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
295 | translated.ptr = alloca(translated.length + 1); |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
296 | translated = sstrncat(2, translated, docroot, uri); |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
297 | |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
298 | pblock_kvinsert( |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
299 | pb_key_ppath, |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
300 | translated.ptr, |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
301 | translated.length, |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
302 | rq->rq.vars); |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
303 | } |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
304 | |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
305 | return REQ_PROCEED; |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
306 | } |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
307 | |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
308 | int nsapi_service(NSAPISession *sn, NSAPIRequest *rq) { |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
309 | printf("nsapi_service\n"); |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
310 | httpd_objset *objset = rq->rq.os; |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
311 | |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
312 | if(NCX_OI(rq) == -1) { |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
313 | NCX_OI(rq) = objset->pos - 1; |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
314 | } |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
315 | |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
316 | int ret = rq->context.last_req_code; |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
317 | for(int i=NCX_OI(rq);i>=0;i--) { |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
318 | httpd_object *obj = objset->obj[i]; |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
319 | dtable *dt = object_get_dtable(obj, NSAPIService); |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
320 | |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
321 | // execute directives |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
322 | for(int j=0;j<dt->ndir;j++) { |
5
dbc01588686e
Added parser for obj.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
3
diff
changeset
|
323 | directive *d = dt->dirs[j]; |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
324 | |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
325 | ret = d->func->func(d->param, (Session*)sn, (Request*)rq); |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
326 | if(ret != REQ_NOACTION) { |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
327 | if(ret == REQ_PROCESSING) { |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
328 | /* save nsapi context */ |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
329 | rq->context.objset_index = i; |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
330 | |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
331 | /* add +1 to start next round with next function */ |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
332 | rq->context.dtable_index = j + 1; |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
333 | } |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
334 | |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
335 | return ret; |
3
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
336 | } |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
337 | } |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
338 | } |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
339 | |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
340 | return ret; |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
341 | } |
137197831306
minimal request handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
1
diff
changeset
|
342 | |
6
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
343 | /* |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
344 | * adds objects with specific name or path to the httpd_objset |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
345 | */ |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
346 | int add_objects( |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
347 | HTTPObjectConfig *objs, |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
348 | httpd_objset *os, |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
349 | NSAPISession *sn, |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
350 | NSAPIRequest *rq, |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
351 | char *name, |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
352 | char *path) |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
353 | { |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
354 | /* first, add all objects with a matching path */ |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
355 | |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
356 | |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
357 | /* add object with object with matching name */ |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
358 | |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
359 | |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
360 | return REQ_PROCEED; |
ce8fecc9847d
improved request processing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
5
diff
changeset
|
361 | } |