Klaus Demo nginx / 34a497e
nginx-0.0.10-2004-09-09-22:55:39 import Igor Sysoev 17 years ago
1 changed file(s) with 112 addition(s) and 10 deletion(s). Raw diff Collapse all Expand all
66
77
88 static void ngx_imap_init_session(ngx_event_t *rev);
9 static void ngx_pop3_auth_state(ngx_event_t *rev);
10 static ngx_int_t ngx_pop3_read_command(ngx_imap_session_t *s);
911
1012
11 static char pop3_greeting[] = "+OK " NGINX_VER " ready" CRLF;
12 static char imap_greeting[] = "* OK " NGINX_VER " ready" CRLF;
13 static u_char pop3_greeting[] = "+OK " NGINX_VER " ready" CRLF;
14 static u_char imap_greeting[] = "* OK " NGINX_VER " ready" CRLF;
15
16 static u_char pop3_ok[] = "+OK" CRLF;
17 static u_char pop3_invalid_command[] = "-ERR invalid command" CRLF;
1318
1419
1520 void ngx_imap_init_connection(ngx_connection_t *c)
1621 {
17 char *greeting;
18 ssize_t size;
19 ngx_int_t n;
22 u_char *greeting;
23 ssize_t size;
2024
2125 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, c->log, 0,
2226 "imap init connection");
2630 greeting = pop3_greeting;
2731 size = sizeof(pop3_greeting) - 1;
2832
29 n = ngx_send(c, greeting, size);
30
31 if (n < size) {
33 if (ngx_send(c, greeting, size) < size) {
3234 /*
3335 * we treat the incomplete sending as NGX_ERROR
3436 * because it is very strange here
4951
5052 static void ngx_imap_init_session(ngx_event_t *rev)
5153 {
54 size_t size;
5255 ngx_connection_t *c;
5356 ngx_imap_session_t *s;
5457
5558 c = rev->data;
59
60 /* TODO: timeout */
5661
5762 if (!(s = ngx_pcalloc(c->pool, sizeof(ngx_imap_session_t)))) {
5863 ngx_imap_close_connection(c);
6772 return;
6873 }
6974
70 s->buffer = ngx_create_temp_buf(s->connection->pool, /* STUB */ 4096);
75 size = /* STUB: pop3: 128, imap: configurable 4K default */ 128;
76
77 s->buffer = ngx_create_temp_buf(s->connection->pool, size);
7178 if (s->buffer == NULL) {
7279 ngx_imap_close_connection(s->connection);
7380 return;
7481 }
7582
76 ngx_imap_proxy_init(s);
83 ngx_pop3_auth_state(rev);
7784 }
85
86
87 static void ngx_pop3_auth_state(ngx_event_t *rev)
88 {
89 u_char *text;
90 ssize_t size;
91 ngx_int_t rc;
92 ngx_connection_t *c;
93 ngx_imap_session_t *s;
94
95 c = rev->data;
96 s = c->data;
97
98 /* TODO: timeout */
99
100 rc = ngx_pop3_read_command(s);
101
102 if (rc == NGX_AGAIN || rc == NGX_ERROR) {
103 return;
104 }
105
106 s->state = 0;
107
108 if (rc == NGX_IMAP_PARSE_INVALID_COMMAND) {
109 text = pop3_invalid_command;
110 size = sizeof(pop3_invalid_command) - 1;
111
112 } else {
113 text = pop3_ok;
114 size = sizeof(pop3_ok) - 1;
115 }
116
117 if (ngx_send(c, text, size) < size) {
118 /*
119 * we treat the incomplete sending as NGX_ERROR
120 * because it is very strange here
121 */
122 ngx_imap_close_connection(c);
123 return;
124 }
125 }
126
127
128 static ngx_int_t ngx_pop3_read_command(ngx_imap_session_t *s)
129 {
130 ssize_t n;
131 ngx_int_t rc;
132
133 n = ngx_recv(s->connection, s->buffer->last,
134 s->buffer->end - s->buffer->last);
135
136 if (n == NGX_ERROR || n == 0) {
137 ngx_imap_close_connection(s->connection);
138 return NGX_ERROR;
139 }
140
141 if (n > 0) {
142 s->buffer->last += n;
143 }
144
145 if (n == NGX_AGAIN) {
146 if (ngx_handle_read_event(s->connection->read, 0) == NGX_ERROR) {
147 ngx_imap_close_connection(s->connection);
148 return NGX_ERROR;
149 }
150
151 return NGX_AGAIN;
152 }
153
154 rc = ngx_pop3_parse_command(s);
155
156 if (rc == NGX_AGAIN || rc == NGX_IMAP_PARSE_INVALID_COMMAND) {
157 return rc;
158 }
159
160 if (rc == NGX_ERROR) {
161 ngx_imap_close_connection(s->connection);
162 return NGX_ERROR;
163 }
164
165 return NGX_OK;
166 }
167
168
169 #if 0
170
171 void ngx_imap_close_session(ngx_imap_session_t *s)
172 {
173 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, s->connection->log, 0,
174 "close imap session");
175
176 ngx_imap_close_connection(s->connection);
177 }
178
179 #endif
78180
79181
80182 void ngx_imap_close_connection(ngx_connection_t *c)