# HG changeset patch # User Igor Sysoev # Date 1094756139 0 # Node ID 33a8253115b4b712cb76f1e96dde87752bc44440 # Parent 47709bff44680c78679f68916b16f82ca52f03f5 nginx-0.0.10-2004-09-09-22:55:39 import 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 @@ -7,17 +7,21 @@ static void ngx_imap_init_session(ngx_event_t *rev); +static void ngx_pop3_auth_state(ngx_event_t *rev); +static ngx_int_t ngx_pop3_read_command(ngx_imap_session_t *s); -static char pop3_greeting[] = "+OK " NGINX_VER " ready" CRLF; -static char imap_greeting[] = "* OK " NGINX_VER " ready" CRLF; +static u_char pop3_greeting[] = "+OK " NGINX_VER " ready" CRLF; +static u_char imap_greeting[] = "* OK " NGINX_VER " ready" CRLF; + +static u_char pop3_ok[] = "+OK" CRLF; +static u_char pop3_invalid_command[] = "-ERR invalid command" CRLF; void ngx_imap_init_connection(ngx_connection_t *c) { - char *greeting; - ssize_t size; - ngx_int_t n; + u_char *greeting; + ssize_t size; ngx_log_debug0(NGX_LOG_DEBUG_IMAP, c->log, 0, "imap init connection"); @@ -27,9 +31,7 @@ void ngx_imap_init_connection(ngx_connec greeting = pop3_greeting; size = sizeof(pop3_greeting) - 1; - n = ngx_send(c, greeting, size); - - if (n < size) { + if (ngx_send(c, greeting, size) < size) { /* * we treat the incomplete sending as NGX_ERROR * because it is very strange here @@ -50,11 +52,14 @@ void ngx_imap_init_connection(ngx_connec static void ngx_imap_init_session(ngx_event_t *rev) { + size_t size; ngx_connection_t *c; ngx_imap_session_t *s; c = rev->data; + /* TODO: timeout */ + if (!(s = ngx_pcalloc(c->pool, sizeof(ngx_imap_session_t)))) { ngx_imap_close_connection(c); return; @@ -68,16 +73,113 @@ static void ngx_imap_init_session(ngx_ev return; } - s->buffer = ngx_create_temp_buf(s->connection->pool, /* STUB */ 4096); + size = /* STUB: pop3: 128, imap: configurable 4K default */ 128; + + s->buffer = ngx_create_temp_buf(s->connection->pool, size); if (s->buffer == NULL) { ngx_imap_close_connection(s->connection); return; } - ngx_imap_proxy_init(s); + ngx_pop3_auth_state(rev); +} + + +static void ngx_pop3_auth_state(ngx_event_t *rev) +{ + u_char *text; + ssize_t size; + ngx_int_t rc; + ngx_connection_t *c; + ngx_imap_session_t *s; + + c = rev->data; + s = c->data; + + /* TODO: timeout */ + + rc = ngx_pop3_read_command(s); + + if (rc == NGX_AGAIN || rc == NGX_ERROR) { + return; + } + + s->state = 0; + + if (rc == NGX_IMAP_PARSE_INVALID_COMMAND) { + text = pop3_invalid_command; + size = sizeof(pop3_invalid_command) - 1; + + } else { + text = pop3_ok; + size = sizeof(pop3_ok) - 1; + } + + if (ngx_send(c, text, size) < size) { + /* + * we treat the incomplete sending as NGX_ERROR + * because it is very strange here + */ + ngx_imap_close_connection(c); + return; + } } +static ngx_int_t ngx_pop3_read_command(ngx_imap_session_t *s) +{ + ssize_t n; + ngx_int_t rc; + + n = ngx_recv(s->connection, s->buffer->last, + s->buffer->end - s->buffer->last); + + if (n == NGX_ERROR || n == 0) { + ngx_imap_close_connection(s->connection); + return NGX_ERROR; + } + + if (n > 0) { + s->buffer->last += n; + } + + if (n == NGX_AGAIN) { + if (ngx_handle_read_event(s->connection->read, 0) == NGX_ERROR) { + ngx_imap_close_connection(s->connection); + return NGX_ERROR; + } + + return NGX_AGAIN; + } + + rc = ngx_pop3_parse_command(s); + + if (rc == NGX_AGAIN || rc == NGX_IMAP_PARSE_INVALID_COMMAND) { + return rc; + } + + if (rc == NGX_ERROR) { + ngx_imap_close_connection(s->connection); + return NGX_ERROR; + } + + return NGX_OK; +} + + +#if 0 + +void ngx_imap_close_session(ngx_imap_session_t *s) +{ + ngx_log_debug0(NGX_LOG_DEBUG_IMAP, s->connection->log, 0, + "close imap session"); + + ngx_imap_close_connection(s->connection); +} + +#endif + + void ngx_imap_close_connection(ngx_connection_t *c) { ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0,