src/server/httprequest.c

changeset 3
137197831306
parent 1
3c066d52342d
child 5
dbc01588686e
equal deleted inserted replaced
2:cee3e65e789c 3:137197831306
33 #include "pool.h" 33 #include "pool.h"
34 #include "pblock.h" 34 #include "pblock.h"
35 #include "io.h" 35 #include "io.h"
36 #include "util.h" 36 #include "util.h"
37 #include "httprequest.h" 37 #include "httprequest.h"
38 #include "conf.h"
39 #include "vserver.h"
38 40
39 HTTPRequest *http_request_new() { 41 HTTPRequest *http_request_new() {
40 HTTPRequest *req = malloc(sizeof(HTTPRequest)); 42 HTTPRequest *req = malloc(sizeof(HTTPRequest));
41 req->connection = NULL; 43 req->connection = NULL;
42 req->uri.ptr = NULL; 44 req->uri.ptr = NULL;
60 62
61 // create nsapi data structures 63 // create nsapi data structures
62 NSAPISession *sn = malloc(sizeof(NSAPISession)); 64 NSAPISession *sn = malloc(sizeof(NSAPISession));
63 NSAPIRequest *rq = malloc(sizeof(NSAPIRequest)); 65 NSAPIRequest *rq = malloc(sizeof(NSAPIRequest));
64 request->rq = rq; 66 request->rq = rq;
67 rq->phase = NSAPIAuthTrans;
65 68
66 // fill session structure 69 // fill session structure
67 sn->sn.pool = pool_create(); 70 sn->sn.pool = pool_create();
68 sn->sn.csd = stream_new_from_fd(request->connection->fd); 71 sn->sn.csd = stream_new_from_fd(request->connection->fd);
69 sn->sn.client = NULL; 72 sn->sn.client = NULL;
71 sn->sn.fill = 1; 74 sn->sn.fill = 1;
72 sn->sn.subject = NULL; 75 sn->sn.subject = NULL;
73 76
74 // init NSAPI request structure 77 // init NSAPI request structure
75 if(request_initialize(request->pool, request, rq) != 0) { 78 if(request_initialize(request->pool, request, rq) != 0) {
79 printf("Cannot initialize request structure\n");
76 return 1; 80 return 1;
77 } 81 }
82
83 // set default virtual server
84 rq->vs = conf_get_default_vs();
85
78 86
79 /* Pass request line as "clf-request" */ 87 /* Pass request line as "clf-request" */
80 pblock_kvinsert( 88 pblock_kvinsert(
81 pb_key_clf_request, 89 pb_key_clf_request,
82 request->request_line.ptr, 90 request->request_line.ptr,
127 135
128 // pass http header to the NSAPI request structure 136 // pass http header to the NSAPI request structure
129 137
130 138
131 // Send the request to the NSAPI system 139 // Send the request to the NSAPI system
140 nsapi_handle_request(sn, rq);
132 141
133 return 0; 142 return 0;
134 } 143 }
135 144
136 145
150 hd->headers[hd->len].name = name; 159 hd->headers[hd->len].name = name;
151 hd->headers[hd->len].value = value; 160 hd->headers[hd->len].value = value;
152 hd->len++; 161 hd->len++;
153 } 162 }
154 163
164
165 /*
166 * NSAPI Processing
167 * TODO: add this to new file
168 */
169
170 int nsapi_handle_request(NSAPISession *sn, NSAPIRequest *rq) {
171 // TODO: threadpool
172
173 int r = REQ_NOACTION;
174
175 do {
176 switch(rq->phase) {
177 case NSAPIAuthTrans: {
178 rq->phase++;
179 }
180 case NSAPINameTrans: {
181 printf(">>> NameTrans\n");
182 r = nsapi_nametrans(sn, rq);
183 if(r != REQ_PROCEED) {
184 break;
185 }
186 rq->phase++;
187 }
188 case NSAPIPathCheck: {
189 printf(">>> PathCheck\n");
190 rq->phase++;
191 }
192 case NSAPIService: {
193 printf(">>> Service\n");
194 r = nsapi_service(sn, rq);
195 if(r != REQ_PROCEED) {
196 break;
197 }
198 rq->phase++;
199 }
200 case REQ_FINISH: {
201 printf(">>> Finish\n");
202 r = nsapi_finish_request(sn, rq);
203 }
204 }
205 } while (r == REQ_RESTART);
206
207
208 return r;
209 }
210
211 int nsapi_finish_request(NSAPISession *sn, NSAPIRequest *rq) {
212 return 0;
213 }
214
215 int nsapi_nametrans(NSAPISession *sn, NSAPIRequest *rq) {
216 httpd_objset *objset = rq->vs->objset;
217 printf("nsapi_nametrans\n");
218
219 int ret = -1;
220 for(int i=0;i<objset->pos;i++) {
221 httpd_object *obj = objset->obj[i];
222 dtable *dt = object_get_dtable(obj, NSAPINameTrans);
223
224 printf("object[%s] dt: %d\n", obj->name, dt);
225
226 // execute directives
227 for(int j=0;j<dt->ndir;j++) {
228 directive *d = dt->directive[j];
229
230 printf("execute [%s]\n", d->func->name);
231 ret = d->func->func(d->param, (Session*)sn, (Request*)rq);
232 if(ret == REQ_PROCEED || ret == REQ_PROCESSING) {
233 break;
234 }
235 }
236
237 // TODO: stultus
238 if(ret == REQ_PROCEED || ret == REQ_PROCESSING) {
239 break;
240 }
241 }
242
243 // todo: check object, path, ...
244 char *ppath = pblock_findkeyval(pb_key_ppath, rq->rq.vars);
245 printf("ppath: [%s]\n", ppath);
246
247 return ret;
248 }
249
250 int nsapi_service(NSAPISession *sn, NSAPIRequest *rq) {
251 httpd_objset *objset = rq->vs->objset;
252
253 int ret = -1;
254 for(int i=0;i<objset->pos;i++) {
255 httpd_object *obj = objset->obj[i];
256 dtable *dt = object_get_dtable(obj, NSAPIService);
257
258 // execute directives
259 for(int j=0;j<dt->ndir;j++) {
260 directive *d = dt->directive[j];
261
262 ret = d->func->func(d->param, (Session*)sn, (Request*)rq);
263 if(ret == REQ_PROCEED || ret == REQ_PROCESSING) {
264 break;
265 }
266 }
267
268 // TODO: stultus
269 if(ret == REQ_PROCEED || ret == REQ_PROCESSING) {
270 break;
271 }
272 }
273
274 return ret;
275 }
276

mercurial