# HG changeset patch # User Igor Sysoev # Date 1094620731 0 # Node ID cf072d26d6d68ddd789992c9270eda5df96962af # Parent 0526206251f6d30ff9d36ef32e6aa2a8d191473e nginx-0.0.10-2004-09-08-09:18:51 import diff --git a/auto/sources b/auto/sources --- a/auto/sources +++ b/auto/sources @@ -298,4 +298,5 @@ IMAP_DEPS="src/imap/ngx_imap.h" IMAP_MODULE=ngx_imap_module IMAP_SRCS="src/imap/ngx_imap.c \ - src/imap/ngx_imap_handler.c" + src/imap/ngx_imap_handler.c \ + src/imap/ngx_imap_proxy.c" diff --git a/src/imap/ngx_imap.h b/src/imap/ngx_imap.h --- a/src/imap/ngx_imap.h +++ b/src/imap/ngx_imap.h @@ -7,22 +7,33 @@ typedef struct { - ngx_chain_t *send; -} ngx_imap_request_t; + ngx_connection_t *connection; + + ngx_buf_t *downstream_buffer; + ngx_buf_t *upstream_buffer; +} ngx_imap_proxy_ctx_t; -#define NGX_POP3_USER 1 -#define NGX_POP3_PASS 2 -#define NGX_POP3_APOP 3 -#define NGX_POP3_STAT 4 -#define NGX_POP3_LIST 5 -#define NGX_POP3_RETR 6 -#define NGX_POP3_DELE 7 -#define NGX_POP3_NOOP 8 -#define NGX_POP3_RSET 9 -#define NGX_POP3_TOP 10 -#define NGX_POP3_UIDL 11 -#define NGX_POP3_QUIT 12 +typedef struct { + uint32_t signature; /* "IMAP" */ + + ngx_connection_t *connection; + ngx_imap_proxy_ctx_t *proxy; +} ngx_imap_session_t; + + +#define NGX_POP3_USER 1 +#define NGX_POP3_PASS 2 +#define NGX_POP3_APOP 3 +#define NGX_POP3_STAT 4 +#define NGX_POP3_LIST 5 +#define NGX_POP3_RETR 6 +#define NGX_POP3_DELE 7 +#define NGX_POP3_NOOP 8 +#define NGX_POP3_RSET 9 +#define NGX_POP3_TOP 10 +#define NGX_POP3_UIDL 11 +#define NGX_POP3_QUIT 12 void ngx_imap_init_connection(ngx_connection_t *c); diff --git a/src/imap/ngx_imap_handler.c b/src/imap/ngx_imap_handler.c --- a/src/imap/ngx_imap_handler.c +++ b/src/imap/ngx_imap_handler.c @@ -15,16 +15,16 @@ static char imap_greeting[] = "* OK " NG void ngx_imap_init_connection(ngx_connection_t *c) { - ngx_int_t rc; + ngx_int_t n; ngx_log_debug0(NGX_LOG_DEBUG_IMAP, c->log, 0, "imap init connection"); c->log_error = NGX_ERROR_INFO; - rc = ngx_send(c, pop3_greeting, sizeof(pop3_greeting) - 1); + n = ngx_send(c, pop3_greeting, sizeof(pop3_greeting) - 1); - if (rc == NGX_ERROR) { + if (n == NGX_ERROR) { ngx_imap_close_connection(c); return; } diff --git a/src/imap/ngx_imap_proxy.c b/src/imap/ngx_imap_proxy.c new file mode 100644 --- /dev/null +++ b/src/imap/ngx_imap_proxy.c @@ -0,0 +1,80 @@ + +#include +#include +#include +#include + + +static void ngx_imap_proxy_close_session(ngx_imap_session_t *s); + + +void ngx_imap_proxy_handler(ngx_event_t *ev) +{ + ssize_t n; + ngx_buf_t *b; + ngx_uint_t data, do_write; + ngx_connection_t *c, *src, *dst; + ngx_imap_session_t *s; + + c = ev->data; + s = c->data; + + if (c == s->connection) { + src = c; + dst = s->proxy->connection; + b = s->proxy->downstream_buffer; + + } else { + src = s->proxy->connection; + dst = c; + b = s->proxy->upstream_buffer; + } + + do_write = ev->write ? 1 : 0; + + do { + data = 0; + + if (do_write == 1) { + if (dst->write->ready && b->pos < b->last) { + n = ngx_send(dst, b->pos, b->last - b->pos); + + if (n == NGX_ERROR) { + ngx_imap_proxy_close_session(s); + return; + } + + if (n > 0) { + data = 1; + b->pos += n; + + if (b->pos == b->last) { + b->pos = b->start; + b->last = b->start; + } + } + } + } + + if (src->read->ready && b->last < b->end) { + n = ngx_recv(src, b->last, b->end - b->last); + + if (n == NGX_ERROR || n == 0) { + ngx_imap_proxy_close_session(s); + return; + } + + if (n > 0) { + data = 1; + do_write = 1; + b->last += n; + } + } + + } while (data); +} + + +static void ngx_imap_proxy_close_session(ngx_imap_session_t *s) +{ +}