comparison src/imap/ngx_imap_proxy_module.c @ 132:91372f004adf NGINX_0_3_13

nginx 0.3.13 *) Feature: the IMAP/POP3 proxy supports STARTTLS and STLS. *) Bugfix: the IMAP/POP3 proxy did not work with the select, poll, and /dev/poll methods. *) Bugfix: in SSI handling. *) Bugfix: now Solaris sendfilev() is not used to transfer the client request body to FastCGI-server via the unix domain socket. *) Bugfix: the "auth_basic" directive did not disable the authorization; bug appeared in 0.3.11.
author Igor Sysoev <http://sysoev.ru>
date Mon, 05 Dec 2005 00:00:00 +0300
parents df17fbafec8f
children 3656228c0b56
comparison
equal deleted inserted replaced
131:add6b1e86d38 132:91372f004adf
284 ngx_add_timer(s->connection->read, pcf->timeout); 284 ngx_add_timer(s->connection->read, pcf->timeout);
285 ngx_del_timer(c->read); 285 ngx_del_timer(c->read);
286 286
287 c->log->action = NULL; 287 c->log->action = NULL;
288 ngx_log_error(NGX_LOG_INFO, c->log, 0, "client logged in"); 288 ngx_log_error(NGX_LOG_INFO, c->log, 0, "client logged in");
289
290 c->log->action = "proxying";
291 } 289 }
292 } 290 }
293 291
294 292
295 static void 293 static void
403 ngx_add_timer(s->connection->read, pcf->timeout); 401 ngx_add_timer(s->connection->read, pcf->timeout);
404 ngx_del_timer(c->read); 402 ngx_del_timer(c->read);
405 403
406 c->log->action = NULL; 404 c->log->action = NULL;
407 ngx_log_error(NGX_LOG_INFO, c->log, 0, "client logged in"); 405 ngx_log_error(NGX_LOG_INFO, c->log, 0, "client logged in");
408
409 c->log->action = "proxying";
410 } 406 }
411 } 407 }
412 408
413 409
414 static void 410 static void
415 ngx_imap_proxy_dummy_handler(ngx_event_t *ev) 411 ngx_imap_proxy_dummy_handler(ngx_event_t *wev)
416 { 412 {
417 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, ev->log, 0, "imap proxy dummy handler"); 413 ngx_connection_t *c;
414 ngx_imap_session_t *s;
415
416 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, wev->log, 0, "imap proxy dummy handler");
417
418 if (ngx_handle_write_event(wev, 0) == NGX_ERROR) {
419 c = wev->data;
420 s = c->data;
421
422 ngx_imap_proxy_close_session(s);
423 }
418 } 424 }
419 425
420 426
421 static ngx_int_t 427 static ngx_int_t
422 ngx_imap_proxy_read_response(ngx_imap_session_t *s, ngx_uint_t what) 428 ngx_imap_proxy_read_response(ngx_imap_session_t *s, ngx_uint_t what)
485 491
486 492
487 static void 493 static void
488 ngx_imap_proxy_handler(ngx_event_t *ev) 494 ngx_imap_proxy_handler(ngx_event_t *ev)
489 { 495 {
490 char *action; 496 char *action, *recv_action, *send_action;
491 size_t size; 497 size_t size;
492 ssize_t n; 498 ssize_t n;
493 ngx_buf_t *b; 499 ngx_buf_t *b;
494 ngx_uint_t again, do_write; 500 ngx_uint_t do_write;
495 ngx_connection_t *c, *src, *dst; 501 ngx_connection_t *c, *src, *dst;
496 ngx_imap_session_t *s; 502 ngx_imap_session_t *s;
497 ngx_imap_proxy_conf_t *pcf; 503 ngx_imap_proxy_conf_t *pcf;
498 504
499 c = ev->data; 505 c = ev->data;
500 s = c->data; 506 s = c->data;
501 507
502 if (ev->timedout) { 508 if (ev->timedout) {
509 c->log->action = "proxying";
510
503 if (c == s->connection) { 511 if (c == s->connection) {
504 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, 512 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
505 "client timed out"); 513 "client timed out");
506 c->timedout = 1; 514 c->timedout = 1;
507 515
514 return; 522 return;
515 } 523 }
516 524
517 if (c == s->connection) { 525 if (c == s->connection) {
518 if (ev->write) { 526 if (ev->write) {
527 recv_action = "proxying and reading from upstream";
528 send_action = "proxying and sending to client";
519 src = s->proxy->upstream.connection; 529 src = s->proxy->upstream.connection;
520 dst = c; 530 dst = c;
521 b = s->proxy->buffer; 531 b = s->proxy->buffer;
522 532
523 } else { 533 } else {
534 recv_action = "proxying and reading from client";
535 send_action = "proxying and sending to upstream";
524 src = c; 536 src = c;
525 dst = s->proxy->upstream.connection; 537 dst = s->proxy->upstream.connection;
526 b = s->buffer; 538 b = s->buffer;
527 } 539 }
528 540
529 } else { 541 } else {
530 if (ev->write) { 542 if (ev->write) {
543 recv_action = "proxying and reading from upstream";
544 send_action = "proxying and sending to client";
531 src = s->connection; 545 src = s->connection;
532 dst = c; 546 dst = c;
533 b = s->buffer; 547 b = s->buffer;
534 548
535 } else { 549 } else {
550 recv_action = "proxying and reading from client";
551 send_action = "proxying and sending to upstream";
536 src = c; 552 src = c;
537 dst = s->connection; 553 dst = s->connection;
538 b = s->proxy->buffer; 554 b = s->proxy->buffer;
539 } 555 }
540 } 556 }
543 559
544 ngx_log_debug3(NGX_LOG_DEBUG_IMAP, ev->log, 0, 560 ngx_log_debug3(NGX_LOG_DEBUG_IMAP, ev->log, 0,
545 "imap proxy handler: %d, #%d > #%d", 561 "imap proxy handler: %d, #%d > #%d",
546 do_write, src->fd, dst->fd); 562 do_write, src->fd, dst->fd);
547 563
548 do { 564 for ( ;; ) {
549 again = 0; 565
550 566 if (do_write) {
551 if (do_write == 1) {
552 567
553 size = b->last - b->pos; 568 size = b->last - b->pos;
554 569
555 if (size && dst->write->ready) { 570 if (size && dst->write->ready) {
571 c->log->action = send_action;
572
556 n = dst->send(dst, b->pos, size); 573 n = dst->send(dst, b->pos, size);
557 574
558 if (n == NGX_ERROR) { 575 if (n == NGX_ERROR) {
559 ngx_imap_proxy_close_session(s); 576 ngx_imap_proxy_close_session(s);
560 return; 577 return;
561 } 578 }
562 579
563 if (n > 0) { 580 if (n > 0) {
564 again = 1;
565 b->pos += n; 581 b->pos += n;
566 582
567 if (b->pos == b->last) { 583 if (b->pos == b->last) {
568 b->pos = b->start; 584 b->pos = b->start;
569 b->last = b->start; 585 b->last = b->start;
570 } 586 }
571 } 587 }
572
573 if (n == NGX_AGAIN || n < (ssize_t) size) {
574 if (ngx_handle_write_event(dst->write, /* TODO: LOWAT */ 0)
575 == NGX_ERROR)
576 {
577 ngx_imap_proxy_close_session(s);
578 return;
579 }
580 }
581 } 588 }
582 } 589 }
583 590
584 size = b->end - b->last; 591 size = b->end - b->last;
585 592
586 if (size && src->read->ready) { 593 if (size && src->read->ready) {
594 c->log->action = recv_action;
595
587 n = src->recv(src, b->last, size); 596 n = src->recv(src, b->last, size);
588 597
589 if (n == NGX_ERROR) { 598 if (n == NGX_AGAIN || n == 0) {
590 ngx_imap_proxy_close_session(s); 599 break;
591 return;
592 } 600 }
593 601
594 if (n == 0) {
595 action = c->log->action;
596 c->log->action = NULL;
597 ngx_log_error(NGX_LOG_INFO, c->log, 0, "proxied session done");
598 c->log->action = action;
599
600 ngx_imap_proxy_close_session(s);
601 return;
602 }
603
604 if (n > 0) { 602 if (n > 0) {
605 again = 1;
606 do_write = 1; 603 do_write = 1;
607 b->last += n; 604 b->last += n;
605
606 continue;
608 } 607 }
609 608
610 if (n == NGX_AGAIN || n < (ssize_t) size) { 609 if (n == NGX_ERROR) {
611 if (ngx_handle_read_event(src->read, 0) == NGX_ERROR) { 610 src->read->eof = 1;
612 ngx_imap_proxy_close_session(s);
613 return;
614 }
615 } 611 }
616 612 }
617 if (c == s->connection) { 613
618 pcf = ngx_imap_get_module_srv_conf(s, ngx_imap_proxy_module); 614 break;
619 ngx_add_timer(c->read, pcf->timeout); 615 }
620 } 616
621 } 617 c->log->action = "proxying";
622 618
623 } while (again); 619 if ((s->connection->read->eof || s->proxy->upstream.connection->read->eof)
620 && s->buffer->pos == s->buffer->last
621 && s->proxy->buffer->pos == s->proxy->buffer->last)
622 {
623 action = c->log->action;
624 c->log->action = NULL;
625 ngx_log_error(NGX_LOG_INFO, c->log, 0, "proxied session done");
626 c->log->action = action;
627
628 ngx_imap_proxy_close_session(s);
629 return;
630 }
631
632 if (ngx_handle_write_event(dst->write, 0) == NGX_ERROR) {
633 ngx_imap_proxy_close_session(s);
634 return;
635 }
636
637 if (ngx_handle_read_event(dst->read, 0) == NGX_ERROR) {
638 ngx_imap_proxy_close_session(s);
639 return;
640 }
641
642 if (ngx_handle_write_event(src->write, 0) == NGX_ERROR) {
643 ngx_imap_proxy_close_session(s);
644 return;
645 }
646
647 if (ngx_handle_read_event(src->read, 0) == NGX_ERROR) {
648 ngx_imap_proxy_close_session(s);
649 return;
650 }
651
652 if (c == s->connection) {
653 pcf = ngx_imap_get_module_srv_conf(s, ngx_imap_proxy_module);
654 ngx_add_timer(c->read, pcf->timeout);
655 }
624 } 656 }
625 657
626 658
627 static void 659 static void
628 ngx_imap_proxy_internal_server_error(ngx_imap_session_t *s) 660 ngx_imap_proxy_internal_server_error(ngx_imap_session_t *s)