src/server/daemon/sessionhandler.c

changeset 557
e35829a3a6d8
parent 555
66b0accda0a8
child 558
0e79e17c70e2
equal deleted inserted replaced
556:b036ccad4b49 557:e35829a3a6d8
256 256
257 return 0; 257 return 0;
258 } 258 }
259 259
260 void evt_enq_conn(SessionHandler *handler, Connection *conn) { 260 void evt_enq_conn(SessionHandler *handler, Connection *conn) {
261 log_ereport(LOG_DEBUG, "trace reqid: %016llx enqueue connection", (unsigned long long int)conn->id);
262
261 Event *start_request = malloc(sizeof(Event)); 263 Event *start_request = malloc(sizeof(Event));
262 if(!start_request) { 264 if(!start_request) {
263 connection_destroy(conn); 265 connection_destroy(conn);
264 return; 266 return;
265 } 267 }
271 return; 273 return;
272 } 274 }
273 275
274 EventHttpIO *io = evt_req_init(handler, conn); 276 EventHttpIO *io = evt_req_init(handler, conn);
275 if(!io) { 277 if(!io) {
278 log_ereport(LOG_DEBUG, "trace reqid: %016llx connection destroyed | evt_req_init failed", (unsigned long long int)conn->id);
276 connection_destroy(conn); 279 connection_destroy(conn);
277 free(start_request); 280 free(start_request);
278 free(event); 281 free(event);
279 return; 282 return;
280 } 283 }
297 start_request->cookie = io; 300 start_request->cookie = io;
298 start_request->fn = evt_add_request; 301 start_request->fn = evt_add_request;
299 start_request->finish = ev_free_event; 302 start_request->finish = ev_free_event;
300 start_request->error = 0; 303 start_request->error = 0;
301 if(event_send(ev, start_request)) { 304 if(event_send(ev, start_request)) {
305 log_ereport(LOG_DEBUG, "trace reqid: %016llx connection destroyed | event_send failed", (unsigned long long int)conn->id);
302 log_ereport(LOG_FAILURE, "Cannot start request timeout: event_send failed"); 306 log_ereport(LOG_FAILURE, "Cannot start request timeout: event_send failed");
303 evt_request_error(ev, event); 307 evt_request_error(ev, event);
304 free(start_request); 308 free(start_request);
305 } 309 }
306 } 310 }
373 377
374 int evt_request_ssl_accept(EventHandler *handler, Event *event) { 378 int evt_request_ssl_accept(EventHandler *handler, Event *event) {
375 EventHttpIO *io = event->cookie; 379 EventHttpIO *io = event->cookie;
376 Connection *conn = io->request->connection; 380 Connection *conn = io->request->connection;
377 381
382 log_ereport(LOG_DEBUG, "trace reqid: %016llx ssl accept", (unsigned long long int)conn->id);
383
378 int ret = SSL_accept(conn->ssl); 384 int ret = SSL_accept(conn->ssl);
379 if(ret <= 0) { 385 if(ret <= 0) {
386 log_ereport(LOG_DEBUG, "trace reqid: %016llx ssl accept | error", (unsigned long long int)conn->id);
380 int error = SSL_get_error(conn->ssl, ret); 387 int error = SSL_get_error(conn->ssl, ret);
381 char *errstr; 388 char *errstr;
382 switch(error) { 389 switch(error) {
383 default: errstr = "unknown"; break; 390 default: errstr = "unknown"; break;
384 case SSL_ERROR_WANT_READ: { 391 case SSL_ERROR_WANT_READ: {
419 int r; 426 int r;
420 r = conn->read( 427 r = conn->read(
421 conn, 428 conn,
422 buf->inbuf + buf->pos, 429 buf->inbuf + buf->pos,
423 buf->maxsize - buf->pos); 430 buf->maxsize - buf->pos);
431
432 log_ereport(LOG_DEBUG, "trace reqid: %016llx request input | r=%d", (unsigned long long int)conn->id, r);
433
424 if(r <= 0) { 434 if(r <= 0) {
425 if(conn->ssl) { 435 if(conn->ssl) {
426 // SSL specific error handling 436 // SSL specific error handling
427 switch(conn->ssl_error) { 437 switch(conn->ssl_error) {
428 case SSL_ERROR_WANT_READ: { 438 case SSL_ERROR_WANT_READ: {
429 event->events = EVENT_POLLIN; 439 event->events = EVENT_POLLIN;
440 log_ereport(LOG_DEBUG, "trace reqid: %016llx request input | ssl want read", (unsigned long long int)conn->id);
430 return 1; 441 return 1;
431 } 442 }
432 case SSL_ERROR_WANT_WRITE: { 443 case SSL_ERROR_WANT_WRITE: {
433 event->events = EVENT_POLLOUT; 444 event->events = EVENT_POLLOUT;
445 log_ereport(LOG_DEBUG, "trace reqid: %016llx request input | ssl want write", (unsigned long long int)conn->id);
434 return 1; 446 return 1;
435 } 447 }
436 } 448 }
437 } 449 }
438 450
451 log_ereport(LOG_DEBUG, "trace reqid: %016llx request input | error", (unsigned long long int)conn->id);
452
439 event->finish = evt_request_error; 453 event->finish = evt_request_error;
440 io->error = 1; 454 io->error = 1;
441 return 0; 455 return 0;
442 } 456 }
443 //fwrite(buf->inbuf + buf->pos, 1, r, stdout); 457 //fwrite(buf->inbuf + buf->pos, 1, r, stdout);
444 //printf("\n"); 458 //printf("\n");
445 459
446 buf->cursize += r; 460 buf->cursize += r;
447 state = http_parser_process(parser); 461 state = http_parser_process(parser);
448 if(state == 2) { 462 if(state == 2) {
463 log_ereport(LOG_DEBUG, "trace reqid: %016llx request input | http parser error", (unsigned long long int)conn->id);
464
449 // parse error 465 // parse error
450 fatal_error(request, 400); 466 fatal_error(request, 400);
451 log_ereport(LOG_VERBOSE, "http parser: bad request"); 467 log_ereport(LOG_VERBOSE, "http parser: bad request");
452 //printf("\n\n%.*s\n\n", parser->request->netbuf->cursize, parser->request->netbuf->inbuf); 468 //printf("\n\n%.*s\n\n", parser->request->netbuf->cursize, parser->request->netbuf->inbuf);
453 //fflush(stdout); 469 //fflush(stdout);
457 } else if(state == 1) { 473 } else if(state == 1) {
458 /* 474 /*
459 * we need more data -> return 1 to tell the event handler to 475 * we need more data -> return 1 to tell the event handler to
460 * continue polling 476 * continue polling
461 */ 477 */
478 log_ereport(LOG_DEBUG, "trace reqid: %016llx request input | event_pollin", (unsigned long long int)conn->id);
462 event->events = EVENT_POLLIN; 479 event->events = EVENT_POLLIN;
463 return 1; 480 return 1;
464 } 481 }
465 482
466 // we are done with reading 483 // we are done with reading
470 if (-1 == (flags = fcntl(request->connection->fd, F_GETFL, 0))) { 487 if (-1 == (flags = fcntl(request->connection->fd, F_GETFL, 0))) {
471 flags = 0; 488 flags = 0;
472 } 489 }
473 if (fcntl(request->connection->fd, F_SETFL, flags & ~O_NONBLOCK) != 0) { 490 if (fcntl(request->connection->fd, F_SETFL, flags & ~O_NONBLOCK) != 0) {
474 // just close the connection if fcntl fails 491 // just close the connection if fcntl fails
492 log_ereport(LOG_DEBUG, "trace reqid: %016llx request input | fcntl error", (unsigned long long int)conn->id);
475 event->finish = evt_request_error; 493 event->finish = evt_request_error;
476 io->error = 3; 494 io->error = 3;
477 return 0; 495 return 0;
478 } 496 }
479 497
480 if(!http_parser_validate(parser)) { 498 if(!http_parser_validate(parser)) {
499 log_ereport(LOG_DEBUG, "trace reqid: %016llx request input | http parser validate error", (unsigned long long int)conn->id);
481 log_ereport(LOG_FAILURE, "http_parser_validate failed"); 500 log_ereport(LOG_FAILURE, "http_parser_validate failed");
482 fatal_error(request, 400); 501 fatal_error(request, 400);
483 event->finish = evt_request_error; 502 event->finish = evt_request_error;
484 return 0; 503 return 0;
485 } 504 }
496 int evt_request_finish(EventHandler *h, Event *event) { 515 int evt_request_finish(EventHandler *h, Event *event) {
497 EventHttpIO *io = event->cookie; 516 EventHttpIO *io = event->cookie;
498 HttpParser *parser = io->parser; 517 HttpParser *parser = io->parser;
499 HTTPRequest *request = io->request; 518 HTTPRequest *request = io->request;
500 519
520 log_ereport(LOG_DEBUG, "trace reqid: %016llx request finish", (unsigned long long int)request->connection->id);
521
501 // remove timeout 522 // remove timeout
502 if(io->watch.intdata) { 523 if(io->watch.intdata) {
503 ev_watchlist_remove(h, &io->watch); 524 ev_watchlist_remove(h, &io->watch);
504 } 525 }
505 526
506 int r = handle_request(request, NULL, h); 527 int r = handle_request(request, NULL, h);
507 if(r != 0) { 528 if(r != 0) {
529 log_ereport(LOG_DEBUG, "trace reqid: %016llx request finish | handle_request failed", (unsigned long long int)request->connection->id);
508 connection_destroy(request->connection); 530 connection_destroy(request->connection);
509 free(request->netbuf->inbuf); 531 free(request->netbuf->inbuf);
510 free(request->netbuf); 532 free(request->netbuf);
511 } 533 }
512 534
552 574
553 return 0; 575 return 0;
554 } 576 }
555 577
556 void evt_keep_alive(SessionHandler *handler, Connection *conn) { 578 void evt_keep_alive(SessionHandler *handler, Connection *conn) {
579 log_ereport(LOG_DEBUG, "trace reqid: %016llx keep alive", (unsigned long long int)conn->id);
580
581 conn->id++;
557 Event *event = malloc(sizeof(Event)); 582 Event *event = malloc(sizeof(Event));
558 if(!event) { 583 if(!event) {
559 connection_destroy(conn); 584 connection_destroy(conn);
560 return; 585 return;
561 } 586 }
565 event->finish = ev_free_event; // this will free the event obj at the end 590 event->finish = ev_free_event; // this will free the event obj at the end
566 event->cookie = conn; 591 event->cookie = conn;
567 592
568 EventHandler *ev = ev_instance(((EventSessionHandler*)handler)->eventhandler); 593 EventHandler *ev = ev_instance(((EventSessionHandler*)handler)->eventhandler);
569 if(event_send(ev, event)) { 594 if(event_send(ev, event)) {
595 log_ereport(LOG_DEBUG, "trace reqid: %016llx connection destroyed | keep_alive event error", (unsigned long long int)conn->id);
570 log_ereport(LOG_FAILURE, "Keep-Alive: ev_send failed"); 596 log_ereport(LOG_FAILURE, "Keep-Alive: ev_send failed");
571 connection_destroy(conn); 597 connection_destroy(conn);
572 free(event); 598 free(event);
573 } 599 }
574 } 600 }
603 ZERO(ioevent, sizeof(Event)); 629 ZERO(ioevent, sizeof(Event));
604 ioevent->fn = evt_keep_alive_input_event; 630 ioevent->fn = evt_keep_alive_input_event;
605 ioevent->finish = ev_free_event; 631 ioevent->finish = ev_free_event;
606 ioevent->cookie = keepalive; 632 ioevent->cookie = keepalive;
607 if(ev_pollin(h, conn->fd, ioevent) != 0) { 633 if(ev_pollin(h, conn->fd, ioevent) != 0) {
634 log_ereport(LOG_DEBUG, "trace reqid: %016llx connection destroyed | keep_alive_enqueue", (unsigned long long int)conn->id);
608 log_ereport(LOG_FAILURE, "Cannot enqueue connection"); 635 log_ereport(LOG_FAILURE, "Cannot enqueue connection");
609 ev_watchlist_remove(h, keepalive); 636 ev_watchlist_remove(h, keepalive);
610 connection_destroy(conn); 637 connection_destroy(conn);
611 free(keepalive); 638 free(keepalive);
612 free(ioevent); 639 free(ioevent);
624 free(keepalive); 651 free(keepalive);
625 652
626 // prepare http io 653 // prepare http io
627 EventHttpIO *io = evt_req_init(conn->session_handler, conn); 654 EventHttpIO *io = evt_req_init(conn->session_handler, conn);
628 if(!io) { 655 if(!io) {
656 log_ereport(LOG_DEBUG, "trace reqid: %016llx connection destroyed | keep alive input", (unsigned long long int)conn->id);
629 connection_destroy(conn); 657 connection_destroy(conn);
630 return 0; 658 return 0;
631 } 659 }
632 660
633 // pass this event to the request input function 661 // pass this event to the request input function
645 673
646 if(ev_remove_poll(h, conn->fd)) { 674 if(ev_remove_poll(h, conn->fd)) {
647 log_ereport(LOG_FAILURE, "sessionhandler: keep-alive timeout: cannot remove poll"); 675 log_ereport(LOG_FAILURE, "sessionhandler: keep-alive timeout: cannot remove poll");
648 } 676 }
649 677
678 log_ereport(LOG_DEBUG, "trace reqid: %016llx connection destroyed | keep_alive timeout", (unsigned long long int)conn->id);
650 connection_destroy(conn); 679 connection_destroy(conn);
651 free(ioevent); 680 free(ioevent);
652 free(item); 681 free(item);
653 } 682 }

mercurial