comparison src/imap/ngx_imap_handler.c @ 420:33a8253115b4

nginx-0.0.10-2004-09-09-22:55:39 import
author Igor Sysoev <igor@sysoev.ru>
date Thu, 09 Sep 2004 18:55:39 +0000
parents 47709bff4468
children 01456a419cf9
comparison
equal deleted inserted replaced
419:47709bff4468 420:33a8253115b4
5 #include <ngx_imap.h> 5 #include <ngx_imap.h>
6 #include <nginx.h> 6 #include <nginx.h>
7 7
8 8
9 static void ngx_imap_init_session(ngx_event_t *rev); 9 static void ngx_imap_init_session(ngx_event_t *rev);
10 static void ngx_pop3_auth_state(ngx_event_t *rev);
11 static ngx_int_t ngx_pop3_read_command(ngx_imap_session_t *s);
10 12
11 13
12 static char pop3_greeting[] = "+OK " NGINX_VER " ready" CRLF; 14 static u_char pop3_greeting[] = "+OK " NGINX_VER " ready" CRLF;
13 static char imap_greeting[] = "* OK " NGINX_VER " ready" CRLF; 15 static u_char imap_greeting[] = "* OK " NGINX_VER " ready" CRLF;
16
17 static u_char pop3_ok[] = "+OK" CRLF;
18 static u_char pop3_invalid_command[] = "-ERR invalid command" CRLF;
14 19
15 20
16 void ngx_imap_init_connection(ngx_connection_t *c) 21 void ngx_imap_init_connection(ngx_connection_t *c)
17 { 22 {
18 char *greeting; 23 u_char *greeting;
19 ssize_t size; 24 ssize_t size;
20 ngx_int_t n;
21 25
22 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, c->log, 0, 26 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, c->log, 0,
23 "imap init connection"); 27 "imap init connection");
24 28
25 c->log_error = NGX_ERROR_INFO; 29 c->log_error = NGX_ERROR_INFO;
26 30
27 greeting = pop3_greeting; 31 greeting = pop3_greeting;
28 size = sizeof(pop3_greeting) - 1; 32 size = sizeof(pop3_greeting) - 1;
29 33
30 n = ngx_send(c, greeting, size); 34 if (ngx_send(c, greeting, size) < size) {
31
32 if (n < size) {
33 /* 35 /*
34 * we treat the incomplete sending as NGX_ERROR 36 * we treat the incomplete sending as NGX_ERROR
35 * because it is very strange here 37 * because it is very strange here
36 */ 38 */
37 ngx_imap_close_connection(c); 39 ngx_imap_close_connection(c);
48 } 50 }
49 51
50 52
51 static void ngx_imap_init_session(ngx_event_t *rev) 53 static void ngx_imap_init_session(ngx_event_t *rev)
52 { 54 {
55 size_t size;
53 ngx_connection_t *c; 56 ngx_connection_t *c;
54 ngx_imap_session_t *s; 57 ngx_imap_session_t *s;
55 58
56 c = rev->data; 59 c = rev->data;
60
61 /* TODO: timeout */
57 62
58 if (!(s = ngx_pcalloc(c->pool, sizeof(ngx_imap_session_t)))) { 63 if (!(s = ngx_pcalloc(c->pool, sizeof(ngx_imap_session_t)))) {
59 ngx_imap_close_connection(c); 64 ngx_imap_close_connection(c);
60 return; 65 return;
61 } 66 }
66 if (ngx_array_init(&s->args, c->pool, 2, sizeof(ngx_str_t)) == NGX_ERROR) { 71 if (ngx_array_init(&s->args, c->pool, 2, sizeof(ngx_str_t)) == NGX_ERROR) {
67 ngx_imap_close_connection(s->connection); 72 ngx_imap_close_connection(s->connection);
68 return; 73 return;
69 } 74 }
70 75
71 s->buffer = ngx_create_temp_buf(s->connection->pool, /* STUB */ 4096); 76 size = /* STUB: pop3: 128, imap: configurable 4K default */ 128;
77
78 s->buffer = ngx_create_temp_buf(s->connection->pool, size);
72 if (s->buffer == NULL) { 79 if (s->buffer == NULL) {
73 ngx_imap_close_connection(s->connection); 80 ngx_imap_close_connection(s->connection);
74 return; 81 return;
75 } 82 }
76 83
77 ngx_imap_proxy_init(s); 84 ngx_pop3_auth_state(rev);
78 } 85 }
86
87
88 static void ngx_pop3_auth_state(ngx_event_t *rev)
89 {
90 u_char *text;
91 ssize_t size;
92 ngx_int_t rc;
93 ngx_connection_t *c;
94 ngx_imap_session_t *s;
95
96 c = rev->data;
97 s = c->data;
98
99 /* TODO: timeout */
100
101 rc = ngx_pop3_read_command(s);
102
103 if (rc == NGX_AGAIN || rc == NGX_ERROR) {
104 return;
105 }
106
107 s->state = 0;
108
109 if (rc == NGX_IMAP_PARSE_INVALID_COMMAND) {
110 text = pop3_invalid_command;
111 size = sizeof(pop3_invalid_command) - 1;
112
113 } else {
114 text = pop3_ok;
115 size = sizeof(pop3_ok) - 1;
116 }
117
118 if (ngx_send(c, text, size) < size) {
119 /*
120 * we treat the incomplete sending as NGX_ERROR
121 * because it is very strange here
122 */
123 ngx_imap_close_connection(c);
124 return;
125 }
126 }
127
128
129 static ngx_int_t ngx_pop3_read_command(ngx_imap_session_t *s)
130 {
131 ssize_t n;
132 ngx_int_t rc;
133
134 n = ngx_recv(s->connection, s->buffer->last,
135 s->buffer->end - s->buffer->last);
136
137 if (n == NGX_ERROR || n == 0) {
138 ngx_imap_close_connection(s->connection);
139 return NGX_ERROR;
140 }
141
142 if (n > 0) {
143 s->buffer->last += n;
144 }
145
146 if (n == NGX_AGAIN) {
147 if (ngx_handle_read_event(s->connection->read, 0) == NGX_ERROR) {
148 ngx_imap_close_connection(s->connection);
149 return NGX_ERROR;
150 }
151
152 return NGX_AGAIN;
153 }
154
155 rc = ngx_pop3_parse_command(s);
156
157 if (rc == NGX_AGAIN || rc == NGX_IMAP_PARSE_INVALID_COMMAND) {
158 return rc;
159 }
160
161 if (rc == NGX_ERROR) {
162 ngx_imap_close_connection(s->connection);
163 return NGX_ERROR;
164 }
165
166 return NGX_OK;
167 }
168
169
170 #if 0
171
172 void ngx_imap_close_session(ngx_imap_session_t *s)
173 {
174 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, s->connection->log, 0,
175 "close imap session");
176
177 ngx_imap_close_connection(s->connection);
178 }
179
180 #endif
79 181
80 182
81 void ngx_imap_close_connection(ngx_connection_t *c) 183 void ngx_imap_close_connection(ngx_connection_t *c)
82 { 184 {
83 ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0, 185 ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0,