src/server/httprequest.c

changeset 6
ce8fecc9847d
parent 5
dbc01588686e
child 7
3c2ed7a7a5fd
equal deleted inserted replaced
5:dbc01588686e 6:ce8fecc9847d
65 NSAPIRequest *rq = malloc(sizeof(NSAPIRequest)); 65 NSAPIRequest *rq = malloc(sizeof(NSAPIRequest));
66 request->rq = rq; 66 request->rq = rq;
67 rq->phase = NSAPIAuthTrans; 67 rq->phase = NSAPIAuthTrans;
68 68
69 // fill session structure 69 // fill session structure
70 sn->sys_fd = request->connection->fd;
70 sn->sn.pool = pool_create(); 71 sn->sn.pool = pool_create();
71 sn->sn.csd = stream_new_from_fd(request->connection->fd); 72 sn->sn.csd = stream_new_from_fd(request->connection->fd);
72 sn->sn.client = NULL; 73 sn->sn.client = NULL;
73 sn->sn.next = NULL; 74 sn->sn.next = NULL;
74 sn->sn.fill = 1; 75 sn->sn.fill = 1;
174 175
175 do { 176 do {
176 switch(rq->phase) { 177 switch(rq->phase) {
177 case NSAPIAuthTrans: { 178 case NSAPIAuthTrans: {
178 rq->phase++; 179 rq->phase++;
180 nsapi_context_next_stage(&rq->context);
179 } 181 }
180 case NSAPINameTrans: { 182 case NSAPINameTrans: {
181 printf(">>> NameTrans\n"); 183 printf(">>> NameTrans\n");
182 r = nsapi_nametrans(sn, rq); 184 r = nsapi_nametrans(sn, rq);
183 if(r != REQ_PROCEED) { 185 if(r != REQ_PROCEED) {
184 break; 186 break;
185 } 187 }
186 rq->phase++; 188 rq->phase++;
189 nsapi_context_next_stage(&rq->context);
187 } 190 }
188 case NSAPIPathCheck: { 191 case NSAPIPathCheck: {
189 printf(">>> PathCheck\n"); 192 printf(">>> PathCheck\n");
190 rq->phase++; 193 rq->phase++;
194 nsapi_context_next_stage(&rq->context);
191 } 195 }
192 case NSAPIObjectType: { 196 case NSAPIObjectType: {
193 rq->phase++; 197 printf(">>> ObjectType\n");
198 rq->phase++;
199 nsapi_context_next_stage(&rq->context);
194 } 200 }
195 case NSAPIService: { 201 case NSAPIService: {
196 printf(">>> Service\n"); 202 printf(">>> Service\n");
197 r = nsapi_service(sn, rq); 203 r = nsapi_service(sn, rq);
198 if(r != REQ_PROCEED) { 204 if(r != REQ_PROCEED) {
199 break; 205 break;
200 } 206 }
201 rq->phase++; 207 rq->phase++;
208 nsapi_context_next_stage(&rq->context);
209 }
210 case NSAPIAddLog: {
211 printf(">>> AddLog\n");
212 rq->phase++;
213 nsapi_context_next_stage(&rq->context);
202 } 214 }
203 case REQ_FINISH: { 215 case REQ_FINISH: {
204 printf(">>> Finish\n"); 216 printf(">>> Finish\n");
205 r = nsapi_finish_request(sn, rq); 217 r = nsapi_finish_request(sn, rq);
206 } 218 }
210 222
211 return r; 223 return r;
212 } 224 }
213 225
214 int nsapi_finish_request(NSAPISession *sn, NSAPIRequest *rq) { 226 int nsapi_finish_request(NSAPISession *sn, NSAPIRequest *rq) {
227 // TODO: free memory
228 close(sn->sys_fd);
229
215 return 0; 230 return 0;
216 } 231 }
217 232
218 int nsapi_nametrans(NSAPISession *sn, NSAPIRequest *rq) { 233 int nsapi_nametrans(NSAPISession *sn, NSAPIRequest *rq) {
219 httpd_objset *objset = rq->vs->objset; 234 HTTPObjectConfig *objconf = rq->vs->objects;
220 printf("nsapi_nametrans\n"); 235 printf("nsapi_nametrans\n");
221 236 httpd_objset *objset = objset_create(sn->sn.pool);
222 int ret = -1; 237 rq->rq.os = objset;
223 for(int i=0;i<objset->pos;i++) { 238 /* first object in objconf is the default object TODO: make sure it is */
239 objset_add_object(sn->sn.pool, objset, objconf->objects[0]);
240
241 httpd_object *obj = objset->obj[0]; /* nametrans only in default object */
242 dtable *dt = object_get_dtable(obj, NSAPINameTrans);
243
244 /* execute directives */
245 int ret = rq->context.last_req_code;
246 char *name = NULL;
247 char *ppath = NULL;
248 for(int i=NCX_DI(rq);i<dt->ndir;i++) {
249 directive *d = dt->dirs[i];
250
251 printf("execute [%s]\n", d->func->name);
252 ret = d->func->func(d->param, (Session*)sn, (Request*)rq);
253
254 /* check for name or ppath */
255 name = pblock_findkeyval(pb_key_name, rq->rq.vars);
256 ppath = pblock_findkeyval(pb_key_ppath, rq->rq.vars);
257
258 /* add additional objects to the objset */
259 if(add_objects(objconf, objset, sn, rq, name, ppath) == REQ_ABORTED) {
260 fprintf(stderr, "add_objects failed\n");
261 return REQ_ABORTED;
262 }
263
264 if(ret != REQ_NOACTION) {
265 /*
266 * if a saf is still processing, we need to save the context, to
267 * process this object at a later time
268 */
269 if(ret == REQ_PROCESSING) {
270 /* save nsapi context */
271 /* add +1 to start next round with next function */
272 rq->context.dtable_index = i + 1;
273 }
274
275 return ret;
276 }
277 }
278
279 /* if no function has set the ppath var, translate it to docroot */
280 if(ret == REQ_NOACTION && ppath == NULL) {
281 sstr_t docroot = rq->vs->document_root;
282 if(docroot.length < 1) {
283 printf("docroot too short\n");
284 return REQ_ABORTED; /* docroot too short */
285 }
286 /* if there is a trailing '/', remove it */
287 if(docroot.ptr[docroot.length - 1] == '/') {
288 docroot.length--;
289 }
290
291 sstr_t uri = sstr(pblock_findkeyval(pb_key_uri, rq->rq.reqpb));
292
293 sstr_t translated;
294 translated.length = docroot.length + uri.length;
295 translated.ptr = alloca(translated.length + 1);
296 translated = sstrncat(2, translated, docroot, uri);
297
298 pblock_kvinsert(
299 pb_key_ppath,
300 translated.ptr,
301 translated.length,
302 rq->rq.vars);
303 }
304
305 return REQ_PROCEED;
306 }
307
308 int nsapi_service(NSAPISession *sn, NSAPIRequest *rq) {
309 printf("nsapi_service\n");
310 httpd_objset *objset = rq->rq.os;
311
312 if(NCX_OI(rq) == -1) {
313 NCX_OI(rq) = objset->pos - 1;
314 }
315
316 int ret = rq->context.last_req_code;
317 for(int i=NCX_OI(rq);i>=0;i--) {
224 httpd_object *obj = objset->obj[i]; 318 httpd_object *obj = objset->obj[i];
225 dtable *dt = object_get_dtable(obj, NSAPINameTrans); 319 dtable *dt = object_get_dtable(obj, NSAPIService);
226
227 printf("object[%s] dt: %d\n", obj->name, dt);
228 320
229 // execute directives 321 // execute directives
230 for(int j=0;j<dt->ndir;j++) { 322 for(int j=0;j<dt->ndir;j++) {
231 directive *d = dt->dirs[j]; 323 directive *d = dt->dirs[j];
232 324
233 printf("execute [%s]\n", d->func->name);
234 ret = d->func->func(d->param, (Session*)sn, (Request*)rq); 325 ret = d->func->func(d->param, (Session*)sn, (Request*)rq);
235 if(ret == REQ_PROCEED || ret == REQ_PROCESSING) { 326 if(ret != REQ_NOACTION) {
236 break; 327 if(ret == REQ_PROCESSING) {
237 } 328 /* save nsapi context */
238 } 329 rq->context.objset_index = i;
239 330
240 // TODO: stultus 331 /* add +1 to start next round with next function */
241 if(ret == REQ_PROCEED || ret == REQ_PROCESSING) { 332 rq->context.dtable_index = j + 1;
242 break; 333 }
243 } 334
244 } 335 return ret;
245 336 }
246 // todo: check object, path, ... 337 }
247 char *ppath = pblock_findkeyval(pb_key_ppath, rq->rq.vars); 338 }
248 printf("ppath: [%s]\n", ppath);
249 339
250 return ret; 340 return ret;
251 } 341 }
252 342
253 int nsapi_service(NSAPISession *sn, NSAPIRequest *rq) { 343 /*
254 httpd_objset *objset = rq->vs->objset; 344 * adds objects with specific name or path to the httpd_objset
255 345 */
256 int ret = -1; 346 int add_objects(
257 for(int i=0;i<objset->pos;i++) { 347 HTTPObjectConfig *objs,
258 httpd_object *obj = objset->obj[i]; 348 httpd_objset *os,
259 dtable *dt = object_get_dtable(obj, NSAPIService); 349 NSAPISession *sn,
260 350 NSAPIRequest *rq,
261 // execute directives 351 char *name,
262 for(int j=0;j<dt->ndir;j++) { 352 char *path)
263 directive *d = dt->dirs[j]; 353 {
264 354 /* first, add all objects with a matching path */
265 ret = d->func->func(d->param, (Session*)sn, (Request*)rq); 355
266 if(ret == REQ_PROCEED || ret == REQ_PROCESSING) { 356
267 break; 357 /* add object with object with matching name */
268 } 358
269 } 359
270 360 return REQ_PROCEED;
271 // TODO: stultus 361 }
272 if(ret == REQ_PROCEED || ret == REQ_PROCESSING) {
273 break;
274 }
275 }
276
277 return ret;
278 }
279

mercurial