src/server/daemon/httprequest.c

changeset 544
27684460629f
parent 531
9b15b1f72bef
child 557
e35829a3a6d8
equal deleted inserted replaced
543:3335f431a91b 544:27684460629f
131 int handle_request(HTTPRequest *request, threadpool_t *thrpool, EventHandler *ev) { 131 int handle_request(HTTPRequest *request, threadpool_t *thrpool, EventHandler *ev) {
132 // handle nsapi request 132 // handle nsapi request
133 133
134 // create pool 134 // create pool
135 pool_handle_t *pool = pool_create(); 135 pool_handle_t *pool = pool_create();
136 136 if(!pool) {
137 log_ereport(LOG_FAILURE, "cannot create new memory pool for http request");
138 return 1;
139 }
140
141 int ret = nsapi_start_request(request, thrpool, ev, pool);
142 if(ret) {
143 pool_destroy(pool);
144 }
145 return ret;
146 }
147
148 int nsapi_start_request(HTTPRequest *request, threadpool_t *thrpool, EventHandler *ev, pool_handle_t *pool) {
137 // create nsapi data structures 149 // create nsapi data structures
138 NSAPISession *sn = nsapisession_create(pool); 150 NSAPISession *sn = nsapisession_create(pool);
139 if(sn == NULL) { 151 if(sn == NULL) {
140 /* TODO: error */ 152 /* TODO: error */
153 return 1;
141 } 154 }
142 155
143 NSAPIRequest *rq = pool_malloc(pool, sizeof(NSAPIRequest)); 156 NSAPIRequest *rq = pool_malloc(pool, sizeof(NSAPIRequest));
144 if(rq == NULL) { 157 if(rq == NULL) {
145 /* TODO: error */ 158 /* TODO: error */
159 return 1;
146 } 160 }
147 ZERO(rq, sizeof(NSAPIRequest)); 161 ZERO(rq, sizeof(NSAPIRequest));
148 rq->rq.req_start = request->req_start; 162 rq->rq.req_start = request->req_start;
149 rq->phase = NSAPIAuthTrans; 163 rq->phase = NSAPIAuthTrans;
150 164
151 // fill session structure 165 // fill session structure
152 IOStream *io = NULL; 166 IOStream *io = NULL;
153 if(nsapisession_setconnection(sn, request->connection, request->netbuf, &io)) { 167 if(nsapisession_setconnection(sn, request->connection, request->netbuf, &io)) {
154 // TODO: error 168 // TODO: error
169 return 1;
155 } 170 }
156 171
157 if(!ev) { 172 if(!ev) {
158 ev = ev_instance(get_default_event_handler()); 173 ev = ev_instance(get_default_event_handler());
159 } 174 }
186 } 201 }
187 202
188 // init NSAPI request structure 203 // init NSAPI request structure
189 if(request_initialize(pool, request, rq) != 0) { 204 if(request_initialize(pool, request, rq) != 0) {
190 log_ereport(LOG_FAILURE, "Cannot initialize request structure"); 205 log_ereport(LOG_FAILURE, "Cannot initialize request structure");
191 pool_destroy(pool);
192 return 1; 206 return 1;
193 } 207 }
194 208
195 // set default virtual server 209 // set default virtual server
196 rq->vs = request->connection->listener->default_vs.vs; 210 rq->vs = request->connection->listener->default_vs.vs;
233 log_ereport( 247 log_ereport(
234 LOG_FAILURE, 248 LOG_FAILURE,
235 "invalid protocol version: %.*s", 249 "invalid protocol version: %.*s",
236 (int)request->httpv.length, 250 (int)request->httpv.length,
237 request->httpv.ptr); 251 request->httpv.ptr);
238 pool_destroy(pool);
239 return 1; 252 return 1;
240 } 253 }
241 254
242 /* 255 /*
243 * get absolute path and query of the request uri 256 * get absolute path and query of the request uri
289 log_ereport( 302 log_ereport(
290 LOG_WARN, 303 LOG_WARN,
291 "invalid request path: {%.*s}", 304 "invalid request path: {%.*s}",
292 (int)orig_path.length, 305 (int)orig_path.length,
293 orig_path.ptr); 306 orig_path.ptr);
294 pool_destroy(pool);
295 // TODO: 400 bad request 307 // TODO: 400 bad request
296 return 1; 308 return 1;
297 } 309 }
298 310
299 // Decode the abs_path 311 // Decode the abs_path
424 if(!strcmp(transfer_encoding, "chunked")) { 436 if(!strcmp(transfer_encoding, "chunked")) {
425 netbuf *nb = sn->netbuf; 437 netbuf *nb = sn->netbuf;
426 // a separate buffer is required for reading chunked transfer enc 438 // a separate buffer is required for reading chunked transfer enc
427 sn->buffer = pool_malloc(pool, nb->maxsize); 439 sn->buffer = pool_malloc(pool, nb->maxsize);
428 if(!sn->buffer) { 440 if(!sn->buffer) {
429 pool_destroy(pool);
430 // TODO: error 500 441 // TODO: error 500
431 return 1; 442 return 1;
432 } 443 }
433 444
434 // copy remaining bytes from inbuf to the additional buffer 445 // copy remaining bytes from inbuf to the additional buffer
442 // clear inbuf 453 // clear inbuf
443 nb->pos = 0; 454 nb->pos = 0;
444 nb->cursize = 0; 455 nb->cursize = 0;
445 456
446 if(httpstream_enable_chunked_read(sn->sn.csd, sn->buffer, nb->maxsize, &sn->cursize, &sn->pos)) { 457 if(httpstream_enable_chunked_read(sn->sn.csd, sn->buffer, nb->maxsize, &sn->cursize, &sn->pos)) {
447 pool_destroy(pool);
448 // TODO: error 500 458 // TODO: error 500
449 return 1; 459 return 1;
450 } 460 }
451 } // else: TODO: unknown transfer encoding error 461 } // else: TODO: unknown transfer encoding error
452 } 462 }

mercurial