| 149 log_ereport(LOG_DEBUG, "trace reqid: %016llx nsapi_start_request", (unsigned long long int)request->connection->id); |
152 log_ereport(LOG_DEBUG, "trace reqid: %016llx nsapi_start_request", (unsigned long long int)request->connection->id); |
| 150 |
153 |
| 151 // create nsapi data structures |
154 // create nsapi data structures |
| 152 NSAPISession *sn = nsapisession_create(pool); |
155 NSAPISession *sn = nsapisession_create(pool); |
| 153 if(sn == NULL) { |
156 if(sn == NULL) { |
| 154 /* TODO: error */ |
157 log_ereport(LOG_FAILURE, "cannot start request: OOM"); |
| |
158 request->status = 503; |
| 155 return 1; |
159 return 1; |
| 156 } |
160 } |
| 157 |
161 |
| 158 NSAPIRequest *rq = pool_malloc(pool, sizeof(NSAPIRequest)); |
162 NSAPIRequest *rq = pool_malloc(pool, sizeof(NSAPIRequest)); |
| 159 if(rq == NULL) { |
163 if(rq == NULL) { |
| 160 /* TODO: error */ |
164 log_ereport(LOG_FAILURE, "cannot start request: OOM"); |
| |
165 request->status = 503; |
| 161 return 1; |
166 return 1; |
| 162 } |
167 } |
| 163 ZERO(rq, sizeof(NSAPIRequest)); |
168 ZERO(rq, sizeof(NSAPIRequest)); |
| 164 rq->rq.req_start = request->req_start; |
169 rq->rq.req_start = request->req_start; |
| 165 rq->phase = NSAPIAuthTrans; |
170 rq->phase = NSAPIAuthTrans; |
| 166 |
171 |
| 167 // fill session structure |
172 // fill session structure |
| 168 IOStream *io = NULL; |
173 IOStream *io = NULL; |
| 169 if(nsapisession_setconnection(sn, request->connection, request->netbuf, &io)) { |
174 if(nsapisession_setconnection(sn, request->connection, request->netbuf, &io)) { |
| 170 // TODO: error |
175 log_ereport(LOG_FAILURE, "cannot start request: OOM"); |
| |
176 request->status = 503; |
| 171 return 1; |
177 return 1; |
| 172 } |
178 } |
| 173 |
179 |
| 174 if(!ev) { |
180 if(!ev) { |
| 175 ev = ev_instance(get_default_event_handler()); |
181 ev = ev_instance(get_default_event_handler()); |
| 203 } |
209 } |
| 204 |
210 |
| 205 // init NSAPI request structure |
211 // init NSAPI request structure |
| 206 if(request_initialize(pool, request, rq) != 0) { |
212 if(request_initialize(pool, request, rq) != 0) { |
| 207 log_ereport(LOG_FAILURE, "Cannot initialize request structure"); |
213 log_ereport(LOG_FAILURE, "Cannot initialize request structure"); |
| |
214 request->status = 503; |
| 208 return 1; |
215 return 1; |
| 209 } |
216 } |
| 210 |
217 |
| 211 // set default virtual server |
218 // set default virtual server |
| 212 rq->vs = request->connection->listener->default_vs.vs; |
219 rq->vs = request->connection->listener->default_vs.vs; |
| 249 log_ereport( |
256 log_ereport( |
| 250 LOG_FAILURE, |
257 LOG_FAILURE, |
| 251 "invalid protocol version: %.*s", |
258 "invalid protocol version: %.*s", |
| 252 (int)request->httpv.length, |
259 (int)request->httpv.length, |
| 253 request->httpv.ptr); |
260 request->httpv.ptr); |
| |
261 request->status = 505; |
| 254 return 1; |
262 return 1; |
| 255 } |
263 } |
| 256 |
264 |
| 257 /* |
265 /* |
| 258 * get absolute path and query of the request uri |
266 * get absolute path and query of the request uri |
| 259 */ |
267 */ |
| 260 // TODO: check for '#' #72 |
268 // TODO: check for '#' #72 |
| 261 cxmutstr absPath = http_request_get_abspath(request); |
269 cxmutstr absPath = http_request_get_abspath(request); |
| 262 if(!absPath.ptr) { |
270 if(absPath.length == 0) { |
| 263 // TODO: error msg |
271 log_ereport(LOG_FAILURE, "request uri length is zero"); |
| 264 return 1; |
272 return 1; |
| 265 } else if(absPath.ptr[0] == '*') { |
273 } else if(absPath.ptr[0] == '*') { |
| 266 // TODO: implement global OPTIONS #71 |
274 // TODO: implement global OPTIONS #71 |
| 267 return 1; |
275 return 1; |
| 268 } |
276 } |
| 314 log_ereport( |
322 log_ereport( |
| 315 LOG_WARN, |
323 LOG_WARN, |
| 316 "invalid request path: {%.*s}", |
324 "invalid request path: {%.*s}", |
| 317 (int)orig_path.length, |
325 (int)orig_path.length, |
| 318 orig_path.ptr); |
326 orig_path.ptr); |
| 319 // TODO: 400 bad request |
327 request->status = 400; |
| 320 return 1; |
328 return 1; |
| 321 } |
329 } |
| 322 |
330 |
| 323 // Decode the abs_path |
331 // Decode the abs_path |
| 324 if(util_uri_unescape_strict(absPath.ptr)) { |
332 if(util_uri_unescape_strict(absPath.ptr)) { |
| 328 absPath.ptr, |
336 absPath.ptr, |
| 329 absPath.length, |
337 absPath.length, |
| 330 rq->rq.reqpb); |
338 rq->rq.reqpb); |
| 331 } else { |
339 } else { |
| 332 log_ereport( |
340 log_ereport( |
| 333 LOG_WARN, |
341 LOG_FAILURE, |
| 334 "uri unescape failed: {%.*s}", |
342 "uri unescape failed: {%.*s}", |
| 335 (int)absPath.length, |
343 (int)absPath.length, |
| 336 absPath.ptr); |
344 absPath.ptr); |
| 337 // TODO: 400 bad request |
345 request->status = 400; |
| 338 pblock_kvinsert(pb_key_uri, "/", 1, rq->rq.reqpb); |
346 return 1; |
| |
347 //pblock_kvinsert(pb_key_uri, "/", 1, rq->rq.reqpb); |
| 339 } |
348 } |
| 340 |
349 |
| 341 // pass http header to the NSAPI request structure |
350 // pass http header to the NSAPI request structure |
| 342 int hlen = request->headers->len; |
351 int hlen = request->headers->len; |
| 343 HeaderArray *ha = request->headers; |
352 HeaderArray *ha = request->headers; |
| 448 if(!strcmp(transfer_encoding, "chunked")) { |
457 if(!strcmp(transfer_encoding, "chunked")) { |
| 449 netbuf *nb = sn->netbuf; |
458 netbuf *nb = sn->netbuf; |
| 450 // a separate buffer is required for reading chunked transfer enc |
459 // a separate buffer is required for reading chunked transfer enc |
| 451 sn->buffer = pool_malloc(pool, nb->maxsize); |
460 sn->buffer = pool_malloc(pool, nb->maxsize); |
| 452 if(!sn->buffer) { |
461 if(!sn->buffer) { |
| 453 // TODO: error 500 |
462 request->status = 503; |
| 454 return 1; |
463 return 1; |
| 455 } |
464 } |
| 456 |
465 |
| 457 // copy remaining bytes from inbuf to the additional buffer |
466 // copy remaining bytes from inbuf to the additional buffer |
| 458 if(nb->cursize - nb->pos > 0) { |
467 if(nb->cursize - nb->pos > 0) { |