Mercurial > hg > nginx-quic
annotate src/imap/ngx_imap_proxy_module.c @ 805:8ee450f30c25
now the "auth_http" directive uses ngx_parse_url()
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Tue, 24 Oct 2006 13:22:59 +0000 |
parents | 6d5c1535bb9d |
children | 739b997da512 |
rev | line source |
---|---|
441
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
426
diff
changeset
|
1 |
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
426
diff
changeset
|
2 /* |
444
42d11f017717
nginx-0.1.0-2004-09-29-20:00:49 import; remove years from copyright
Igor Sysoev <igor@sysoev.ru>
parents:
441
diff
changeset
|
3 * Copyright (C) Igor Sysoev |
441
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
426
diff
changeset
|
4 */ |
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
426
diff
changeset
|
5 |
418
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
6 |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
7 #include <ngx_config.h> |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
8 #include <ngx_core.h> |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
9 #include <ngx_event.h> |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
10 #include <ngx_event_connect.h> |
418
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
11 #include <ngx_imap.h> |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
12 |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
13 |
521 | 14 typedef struct { |
15 ngx_flag_t enable; | |
539 | 16 size_t buffer_size; |
17 ngx_msec_t timeout; | |
521 | 18 } ngx_imap_proxy_conf_t; |
19 | |
20 | |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
21 static void ngx_imap_proxy_block_read(ngx_event_t *rev); |
527 | 22 static void ngx_imap_proxy_imap_handler(ngx_event_t *rev); |
23 static void ngx_imap_proxy_pop3_handler(ngx_event_t *rev); | |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
24 static void ngx_imap_proxy_dummy_handler(ngx_event_t *ev); |
527 | 25 static ngx_int_t ngx_imap_proxy_read_response(ngx_imap_session_t *s, |
663 | 26 ngx_uint_t state); |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
27 static void ngx_imap_proxy_handler(ngx_event_t *ev); |
527 | 28 static void ngx_imap_proxy_internal_server_error(ngx_imap_session_t *s); |
418
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
29 static void ngx_imap_proxy_close_session(ngx_imap_session_t *s); |
521 | 30 static void *ngx_imap_proxy_create_conf(ngx_conf_t *cf); |
31 static char *ngx_imap_proxy_merge_conf(ngx_conf_t *cf, void *parent, | |
32 void *child); | |
33 | |
34 | |
35 static ngx_command_t ngx_imap_proxy_commands[] = { | |
539 | 36 |
521 | 37 { ngx_string("proxy"), |
38 NGX_IMAP_MAIN_CONF|NGX_IMAP_SRV_CONF|NGX_CONF_FLAG, | |
39 ngx_conf_set_flag_slot, | |
40 NGX_IMAP_SRV_CONF_OFFSET, | |
41 offsetof(ngx_imap_proxy_conf_t, enable), | |
42 NULL }, | |
43 | |
539 | 44 { ngx_string("proxy_buffer"), |
45 NGX_IMAP_MAIN_CONF|NGX_IMAP_SRV_CONF|NGX_CONF_TAKE1, | |
46 ngx_conf_set_size_slot, | |
47 NGX_IMAP_SRV_CONF_OFFSET, | |
48 offsetof(ngx_imap_proxy_conf_t, buffer_size), | |
49 NULL }, | |
50 | |
51 { ngx_string("proxy_timeout"), | |
52 NGX_IMAP_MAIN_CONF|NGX_IMAP_SRV_CONF|NGX_CONF_TAKE1, | |
53 ngx_conf_set_msec_slot, | |
54 NGX_IMAP_SRV_CONF_OFFSET, | |
55 offsetof(ngx_imap_proxy_conf_t, timeout), | |
56 NULL }, | |
57 | |
521 | 58 ngx_null_command |
59 }; | |
418
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
60 |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
61 |
521 | 62 static ngx_imap_module_t ngx_imap_proxy_module_ctx = { |
63 NULL, /* create main configuration */ | |
64 NULL, /* init main configuration */ | |
65 | |
66 ngx_imap_proxy_create_conf, /* create server configuration */ | |
67 ngx_imap_proxy_merge_conf /* merge server configuration */ | |
68 }; | |
69 | |
70 | |
71 ngx_module_t ngx_imap_proxy_module = { | |
72 NGX_MODULE_V1, | |
73 &ngx_imap_proxy_module_ctx, /* module context */ | |
74 ngx_imap_proxy_commands, /* module directives */ | |
75 NGX_IMAP_MODULE, /* module type */ | |
541 | 76 NULL, /* init master */ |
521 | 77 NULL, /* init module */ |
541 | 78 NULL, /* init process */ |
79 NULL, /* init thread */ | |
80 NULL, /* exit thread */ | |
81 NULL, /* exit process */ | |
82 NULL, /* exit master */ | |
83 NGX_MODULE_V1_PADDING | |
521 | 84 }; |
85 | |
86 | |
87 void | |
88 ngx_imap_proxy_init(ngx_imap_session_t *s, ngx_peers_t *peers) | |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
89 { |
587 | 90 int keepalive; |
527 | 91 ngx_int_t rc; |
92 ngx_imap_proxy_ctx_t *p; | |
93 ngx_imap_core_srv_conf_t *cscf; | |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
94 |
587 | 95 s->connection->log->action = "connecting to upstream"; |
96 | |
97 cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module); | |
98 | |
99 if (cscf->so_keepalive) { | |
100 keepalive = 1; | |
101 | |
102 if (setsockopt(s->connection->fd, SOL_SOCKET, SO_KEEPALIVE, | |
103 (const void *) &keepalive, sizeof(int)) | |
104 == -1) | |
105 { | |
106 ngx_log_error(NGX_LOG_ALERT, s->connection->log, ngx_socket_errno, | |
107 "setsockopt(SO_KEEPALIVE) failed"); | |
108 } | |
109 } | |
110 | |
501 | 111 p = ngx_pcalloc(s->connection->pool, sizeof(ngx_imap_proxy_ctx_t)); |
112 if (p == NULL) { | |
527 | 113 ngx_imap_session_internal_server_error(s); |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
114 return; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
115 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
116 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
117 s->proxy = p; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
118 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
119 p->upstream.peers = peers; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
120 p->upstream.log = s->connection->log; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
121 p->upstream.log_error = NGX_ERROR_ERR; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
122 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
123 rc = ngx_event_connect_peer(&p->upstream); |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
124 |
543 | 125 if (rc == NGX_ERROR || rc == NGX_BUSY || rc == NGX_DECLINED) { |
126 ngx_imap_proxy_internal_server_error(s); | |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
127 return; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
128 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
129 |
527 | 130 ngx_add_timer(p->upstream.connection->read, cscf->timeout); |
131 | |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
132 p->upstream.connection->data = s; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
133 p->upstream.connection->pool = s->connection->pool; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
134 |
509 | 135 s->connection->read->handler = ngx_imap_proxy_block_read; |
136 p->upstream.connection->write->handler = ngx_imap_proxy_dummy_handler; | |
527 | 137 |
138 if (s->protocol == NGX_IMAP_POP3_PROTOCOL) { | |
139 p->upstream.connection->read->handler = ngx_imap_proxy_pop3_handler; | |
140 s->imap_state = ngx_pop3_start; | |
141 | |
142 } else { | |
143 p->upstream.connection->read->handler = ngx_imap_proxy_imap_handler; | |
144 s->imap_state = ngx_imap_start; | |
145 } | |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
146 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
147 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
148 |
521 | 149 static void |
150 ngx_imap_proxy_block_read(ngx_event_t *rev) | |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
151 { |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
152 ngx_connection_t *c; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
153 ngx_imap_session_t *s; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
154 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
155 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0, "imap proxy block read"); |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
156 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
157 if (ngx_handle_read_event(rev, 0) == NGX_ERROR) { |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
158 c = rev->data; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
159 s = c->data; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
160 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
161 ngx_imap_proxy_close_session(s); |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
162 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
163 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
164 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
165 |
521 | 166 static void |
527 | 167 ngx_imap_proxy_imap_handler(ngx_event_t *rev) |
418
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
168 { |
539 | 169 u_char *p; |
170 ngx_int_t rc; | |
171 ngx_str_t line; | |
172 ngx_connection_t *c; | |
173 ngx_imap_session_t *s; | |
174 ngx_imap_proxy_conf_t *pcf; | |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
175 |
527 | 176 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0, |
177 "imap proxy imap auth handler"); | |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
178 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
179 c = rev->data; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
180 s = c->data; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
181 |
423
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
182 if (rev->timedout) { |
527 | 183 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, |
184 "upstream timed out"); | |
577 | 185 c->timedout = 1; |
527 | 186 ngx_imap_proxy_internal_server_error(s); |
423
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
187 return; |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
188 } |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
189 |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
190 if (s->proxy->buffer == NULL) { |
539 | 191 pcf = ngx_imap_get_module_srv_conf(s, ngx_imap_proxy_module); |
527 | 192 |
539 | 193 s->proxy->buffer = ngx_create_temp_buf(c->pool, pcf->buffer_size); |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
194 if (s->proxy->buffer == NULL) { |
527 | 195 ngx_imap_proxy_internal_server_error(s); |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
196 return; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
197 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
198 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
199 |
663 | 200 rc = ngx_imap_proxy_read_response(s, s->imap_state); |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
201 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
202 if (rc == NGX_AGAIN) { |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
203 return; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
204 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
205 |
571 | 206 if (rc == NGX_ERROR) { |
527 | 207 ngx_imap_proxy_internal_server_error(s); |
422
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
208 return; |
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
209 } |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
210 |
527 | 211 switch (s->imap_state) { |
212 | |
213 case ngx_imap_start: | |
214 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0, | |
215 "imap proxy send login"); | |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
216 |
587 | 217 s->connection->log->action = "sending LOGIN command to upstream"; |
218 | |
527 | 219 line.len = s->tag.len + sizeof("LOGIN ") - 1 |
220 + 1 + NGX_SIZE_T_LEN + 1 + 2; | |
221 line.data = ngx_palloc(c->pool, line.len); | |
222 if (line.data == NULL) { | |
223 ngx_imap_proxy_internal_server_error(s); | |
224 return; | |
225 } | |
226 | |
227 line.len = ngx_sprintf(line.data, "%VLOGIN {%uz}" CRLF, | |
228 &s->tag, s->login.len) | |
229 - line.data; | |
230 | |
231 s->imap_state = ngx_imap_login; | |
232 break; | |
233 | |
234 case ngx_imap_login: | |
422
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
235 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0, "imap proxy send user"); |
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
236 |
587 | 237 s->connection->log->action = "sending user name to upstream"; |
238 | |
529 | 239 line.len = s->login.len + 1 + 1 + NGX_SIZE_T_LEN + 1 + 2; |
527 | 240 line.data = ngx_palloc(c->pool, line.len); |
241 if (line.data == NULL) { | |
242 ngx_imap_proxy_internal_server_error(s); | |
243 return; | |
244 } | |
245 | |
529 | 246 line.len = ngx_sprintf(line.data, "%V {%uz}" CRLF, |
527 | 247 &s->login, s->passwd.len) |
248 - line.data; | |
249 | |
250 s->imap_state = ngx_imap_user; | |
251 break; | |
252 | |
253 case ngx_imap_user: | |
254 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0, | |
255 "imap proxy send passwd"); | |
256 | |
587 | 257 s->connection->log->action = "sending password to upstream"; |
258 | |
527 | 259 line.len = s->passwd.len + 2; |
501 | 260 line.data = ngx_palloc(c->pool, line.len); |
261 if (line.data == NULL) { | |
527 | 262 ngx_imap_proxy_internal_server_error(s); |
263 return; | |
264 } | |
265 | |
266 p = ngx_cpymem(line.data, s->passwd.data, s->passwd.len); | |
267 *p++ = CR; *p = LF; | |
268 | |
269 s->imap_state = ngx_imap_passwd; | |
270 break; | |
271 | |
663 | 272 case ngx_imap_passwd: |
273 s->connection->read->handler = ngx_imap_proxy_handler; | |
274 s->connection->write->handler = ngx_imap_proxy_handler; | |
275 rev->handler = ngx_imap_proxy_handler; | |
276 c->write->handler = ngx_imap_proxy_handler; | |
277 | |
278 pcf = ngx_imap_get_module_srv_conf(s, ngx_imap_proxy_module); | |
279 ngx_add_timer(s->connection->read, pcf->timeout); | |
280 ngx_del_timer(c->read); | |
281 | |
282 c->log->action = NULL; | |
283 ngx_log_error(NGX_LOG_INFO, c->log, 0, "client logged in"); | |
284 | |
285 ngx_imap_proxy_handler(s->connection->write); | |
286 | |
287 return; | |
288 | |
527 | 289 default: |
290 #if (NGX_SUPPRESS_WARN) | |
291 line.len = 0; | |
292 line.data = NULL; | |
293 #endif | |
294 break; | |
295 } | |
296 | |
539 | 297 if (c->send(c, line.data, line.len) < (ssize_t) line.len) { |
527 | 298 /* |
299 * we treat the incomplete sending as NGX_ERROR | |
300 * because it is very strange here | |
301 */ | |
302 ngx_imap_proxy_internal_server_error(s); | |
303 return; | |
304 } | |
305 | |
306 s->proxy->buffer->pos = s->proxy->buffer->start; | |
307 s->proxy->buffer->last = s->proxy->buffer->start; | |
308 } | |
309 | |
310 | |
311 static void | |
312 ngx_imap_proxy_pop3_handler(ngx_event_t *rev) | |
313 { | |
539 | 314 u_char *p; |
315 ngx_int_t rc; | |
316 ngx_str_t line; | |
317 ngx_connection_t *c; | |
318 ngx_imap_session_t *s; | |
319 ngx_imap_proxy_conf_t *pcf; | |
527 | 320 |
321 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0, | |
322 "imap proxy pop3 auth handler"); | |
323 | |
324 c = rev->data; | |
325 s = c->data; | |
326 | |
327 if (rev->timedout) { | |
328 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, | |
329 "upstream timed out"); | |
577 | 330 c->timedout = 1; |
527 | 331 ngx_imap_proxy_internal_server_error(s); |
332 return; | |
333 } | |
334 | |
335 if (s->proxy->buffer == NULL) { | |
539 | 336 pcf = ngx_imap_get_module_srv_conf(s, ngx_imap_proxy_module); |
527 | 337 |
539 | 338 s->proxy->buffer = ngx_create_temp_buf(c->pool, pcf->buffer_size); |
527 | 339 if (s->proxy->buffer == NULL) { |
340 ngx_imap_proxy_internal_server_error(s); | |
341 return; | |
342 } | |
343 } | |
344 | |
663 | 345 rc = ngx_imap_proxy_read_response(s, 0); |
527 | 346 |
347 if (rc == NGX_AGAIN) { | |
348 return; | |
349 } | |
350 | |
571 | 351 if (rc == NGX_ERROR) { |
527 | 352 ngx_imap_proxy_internal_server_error(s); |
353 return; | |
354 } | |
355 | |
356 switch (s->imap_state) { | |
357 | |
358 case ngx_pop3_start: | |
359 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0, "imap proxy send user"); | |
360 | |
587 | 361 s->connection->log->action = "sending user name to upstream"; |
362 | |
527 | 363 line.len = sizeof("USER ") - 1 + s->login.len + 2; |
364 line.data = ngx_palloc(c->pool, line.len); | |
365 if (line.data == NULL) { | |
366 ngx_imap_proxy_internal_server_error(s); | |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
367 return; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
368 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
369 |
422
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
370 p = ngx_cpymem(line.data, "USER ", sizeof("USER ") - 1); |
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
371 p = ngx_cpymem(p, s->login.data, s->login.len); |
501 | 372 *p++ = CR; *p = LF; |
422
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
373 |
527 | 374 s->imap_state = ngx_pop3_user; |
375 break; | |
376 | |
377 case ngx_pop3_user: | |
378 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0, "imap proxy send pass"); | |
379 | |
587 | 380 s->connection->log->action = "sending password to upstream"; |
381 | |
527 | 382 line.len = sizeof("PASS ") - 1 + s->passwd.len + 2; |
383 line.data = ngx_palloc(c->pool, line.len); | |
384 if (line.data == NULL) { | |
385 ngx_imap_proxy_internal_server_error(s); | |
422
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
386 return; |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
387 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
388 |
527 | 389 p = ngx_cpymem(line.data, "PASS ", sizeof("PASS ") - 1); |
390 p = ngx_cpymem(p, s->passwd.data, s->passwd.len); | |
391 *p++ = CR; *p = LF; | |
422
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
392 |
527 | 393 s->imap_state = ngx_pop3_passwd; |
394 break; | |
422
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
395 |
663 | 396 case ngx_pop3_passwd: |
397 s->connection->read->handler = ngx_imap_proxy_handler; | |
398 s->connection->write->handler = ngx_imap_proxy_handler; | |
399 rev->handler = ngx_imap_proxy_handler; | |
400 c->write->handler = ngx_imap_proxy_handler; | |
401 | |
402 pcf = ngx_imap_get_module_srv_conf(s, ngx_imap_proxy_module); | |
403 ngx_add_timer(s->connection->read, pcf->timeout); | |
404 ngx_del_timer(c->read); | |
405 | |
406 c->log->action = NULL; | |
407 ngx_log_error(NGX_LOG_INFO, c->log, 0, "client logged in"); | |
408 | |
409 ngx_imap_proxy_handler(s->connection->write); | |
410 | |
411 return; | |
412 | |
527 | 413 default: |
414 #if (NGX_SUPPRESS_WARN) | |
415 line.len = 0; | |
416 line.data = NULL; | |
417 #endif | |
418 break; | |
422
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
419 } |
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
420 |
539 | 421 if (c->send(c, line.data, line.len) < (ssize_t) line.len) { |
422
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
422 /* |
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
423 * we treat the incomplete sending as NGX_ERROR |
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
424 * because it is very strange here |
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
425 */ |
527 | 426 ngx_imap_proxy_internal_server_error(s); |
422
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
427 return; |
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
428 } |
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
429 |
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
430 s->proxy->buffer->pos = s->proxy->buffer->start; |
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
431 s->proxy->buffer->last = s->proxy->buffer->start; |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
432 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
433 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
434 |
521 | 435 static void |
583 | 436 ngx_imap_proxy_dummy_handler(ngx_event_t *wev) |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
437 { |
583 | 438 ngx_connection_t *c; |
439 ngx_imap_session_t *s; | |
440 | |
441 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, wev->log, 0, "imap proxy dummy handler"); | |
442 | |
443 if (ngx_handle_write_event(wev, 0) == NGX_ERROR) { | |
444 c = wev->data; | |
445 s = c->data; | |
446 | |
447 ngx_imap_proxy_close_session(s); | |
448 } | |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
449 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
450 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
451 |
521 | 452 static ngx_int_t |
663 | 453 ngx_imap_proxy_read_response(ngx_imap_session_t *s, ngx_uint_t state) |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
454 { |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
455 u_char *p; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
456 ssize_t n; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
457 ngx_buf_t *b; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
458 |
587 | 459 s->connection->log->action = "reading response from upstream"; |
460 | |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
461 b = s->proxy->buffer; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
462 |
539 | 463 n = s->proxy->upstream.connection->recv(s->proxy->upstream.connection, |
464 b->last, b->end - b->last); | |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
465 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
466 if (n == NGX_ERROR || n == 0) { |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
467 return NGX_ERROR; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
468 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
469 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
470 if (n == NGX_AGAIN) { |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
471 return NGX_AGAIN; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
472 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
473 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
474 b->last += n; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
475 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
476 if (b->last - b->pos < 5) { |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
477 return NGX_AGAIN; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
478 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
479 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
480 if (*(b->last - 2) != CR || *(b->last - 1) != LF) { |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
481 if (b->last == b->end) { |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
482 *(b->last - 1) = '\0'; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
483 ngx_log_error(NGX_LOG_ERR, s->connection->log, 0, |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
484 "upstream sent too long response line: \"%s\"", |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
485 b->pos); |
571 | 486 return NGX_ERROR; |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
487 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
488 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
489 return NGX_AGAIN; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
490 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
491 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
492 p = b->pos; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
493 |
527 | 494 if (s->protocol == NGX_IMAP_POP3_PROTOCOL) { |
495 if (p[0] == '+' && p[1] == 'O' && p[2] == 'K') { | |
496 return NGX_OK; | |
497 } | |
498 | |
499 } else { | |
663 | 500 switch (state) { |
501 | |
502 case ngx_imap_start: | |
527 | 503 if (p[0] == '*' && p[1] == ' ' && p[2] == 'O' && p[3] == 'K') { |
504 return NGX_OK; | |
505 } | |
663 | 506 break; |
527 | 507 |
663 | 508 case ngx_imap_login: |
509 case ngx_imap_user: | |
529 | 510 if (p[0] == '+') { |
527 | 511 return NGX_OK; |
512 } | |
663 | 513 break; |
514 | |
515 case ngx_imap_passwd: | |
516 if (ngx_strncmp(p, s->tag.data, s->tag.len) == 0) { | |
517 p += s->tag.len; | |
518 if (p[0] == 'O' && p[1] == 'K') { | |
519 return NGX_OK; | |
520 } | |
521 } | |
522 break; | |
527 | 523 } |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
524 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
525 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
526 *(b->last - 2) = '\0'; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
527 ngx_log_error(NGX_LOG_ERR, s->connection->log, 0, |
527 | 528 "upstream sent invalid response: \"%s\"", p); |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
529 |
571 | 530 return NGX_ERROR; |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
531 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
532 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
533 |
521 | 534 static void |
535 ngx_imap_proxy_handler(ngx_event_t *ev) | |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
536 { |
583 | 537 char *action, *recv_action, *send_action; |
539 | 538 size_t size; |
539 ssize_t n; | |
540 ngx_buf_t *b; | |
583 | 541 ngx_uint_t do_write; |
539 | 542 ngx_connection_t *c, *src, *dst; |
543 ngx_imap_session_t *s; | |
544 ngx_imap_proxy_conf_t *pcf; | |
418
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
545 |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
546 c = ev->data; |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
547 s = c->data; |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
548 |
423
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
549 if (ev->timedout) { |
583 | 550 c->log->action = "proxying"; |
551 | |
423
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
552 if (c == s->connection) { |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
553 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
554 "client timed out"); |
577 | 555 c->timedout = 1; |
556 | |
423
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
557 } else { |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
558 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
559 "upstream timed out"); |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
560 } |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
561 |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
562 ngx_imap_proxy_close_session(s); |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
563 return; |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
564 } |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
565 |
418
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
566 if (c == s->connection) { |
423
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
567 if (ev->write) { |
583 | 568 recv_action = "proxying and reading from upstream"; |
569 send_action = "proxying and sending to client"; | |
423
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
570 src = s->proxy->upstream.connection; |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
571 dst = c; |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
572 b = s->proxy->buffer; |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
573 |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
574 } else { |
583 | 575 recv_action = "proxying and reading from client"; |
576 send_action = "proxying and sending to upstream"; | |
423
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
577 src = c; |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
578 dst = s->proxy->upstream.connection; |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
579 b = s->buffer; |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
580 } |
418
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
581 |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
582 } else { |
423
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
583 if (ev->write) { |
589 | 584 recv_action = "proxying and reading from client"; |
585 send_action = "proxying and sending to upstream"; | |
423
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
586 src = s->connection; |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
587 dst = c; |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
588 b = s->buffer; |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
589 |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
590 } else { |
589 | 591 recv_action = "proxying and reading from upstream"; |
592 send_action = "proxying and sending to client"; | |
423
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
593 src = c; |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
594 dst = s->connection; |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
595 b = s->proxy->buffer; |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
596 } |
418
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
597 } |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
598 |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
599 do_write = ev->write ? 1 : 0; |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
600 |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
601 ngx_log_debug3(NGX_LOG_DEBUG_IMAP, ev->log, 0, |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
602 "imap proxy handler: %d, #%d > #%d", |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
603 do_write, src->fd, dst->fd); |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
604 |
583 | 605 for ( ;; ) { |
418
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
606 |
583 | 607 if (do_write) { |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
608 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
609 size = b->last - b->pos; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
610 |
423
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
611 if (size && dst->write->ready) { |
583 | 612 c->log->action = send_action; |
613 | |
539 | 614 n = dst->send(dst, b->pos, size); |
418
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
615 |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
616 if (n == NGX_ERROR) { |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
617 ngx_imap_proxy_close_session(s); |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
618 return; |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
619 } |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
620 |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
621 if (n > 0) { |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
622 b->pos += n; |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
623 |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
624 if (b->pos == b->last) { |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
625 b->pos = b->start; |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
626 b->last = b->start; |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
627 } |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
628 } |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
629 } |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
630 } |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
631 |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
632 size = b->end - b->last; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
633 |
423
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
634 if (size && src->read->ready) { |
583 | 635 c->log->action = recv_action; |
636 | |
539 | 637 n = src->recv(src, b->last, size); |
418
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
638 |
583 | 639 if (n == NGX_AGAIN || n == 0) { |
640 break; | |
418
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
641 } |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
642 |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
643 if (n > 0) { |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
644 do_write = 1; |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
645 b->last += n; |
583 | 646 |
647 continue; | |
418
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
648 } |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
649 |
583 | 650 if (n == NGX_ERROR) { |
651 src->read->eof = 1; | |
539 | 652 } |
418
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
653 } |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
654 |
583 | 655 break; |
656 } | |
657 | |
658 c->log->action = "proxying"; | |
659 | |
660 if ((s->connection->read->eof || s->proxy->upstream.connection->read->eof) | |
661 && s->buffer->pos == s->buffer->last | |
662 && s->proxy->buffer->pos == s->proxy->buffer->last) | |
663 { | |
664 action = c->log->action; | |
665 c->log->action = NULL; | |
666 ngx_log_error(NGX_LOG_INFO, c->log, 0, "proxied session done"); | |
667 c->log->action = action; | |
668 | |
669 ngx_imap_proxy_close_session(s); | |
670 return; | |
671 } | |
672 | |
673 if (ngx_handle_write_event(dst->write, 0) == NGX_ERROR) { | |
674 ngx_imap_proxy_close_session(s); | |
675 return; | |
676 } | |
677 | |
678 if (ngx_handle_read_event(dst->read, 0) == NGX_ERROR) { | |
679 ngx_imap_proxy_close_session(s); | |
680 return; | |
681 } | |
682 | |
683 if (ngx_handle_write_event(src->write, 0) == NGX_ERROR) { | |
684 ngx_imap_proxy_close_session(s); | |
685 return; | |
686 } | |
687 | |
688 if (ngx_handle_read_event(src->read, 0) == NGX_ERROR) { | |
689 ngx_imap_proxy_close_session(s); | |
690 return; | |
691 } | |
692 | |
693 if (c == s->connection) { | |
694 pcf = ngx_imap_get_module_srv_conf(s, ngx_imap_proxy_module); | |
695 ngx_add_timer(c->read, pcf->timeout); | |
696 } | |
418
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
697 } |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
698 |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
699 |
521 | 700 static void |
527 | 701 ngx_imap_proxy_internal_server_error(ngx_imap_session_t *s) |
702 { | |
703 if (s->proxy->upstream.connection) { | |
704 ngx_log_debug1(NGX_LOG_DEBUG_IMAP, s->connection->log, 0, | |
705 "close imap proxy connection: %d", | |
706 s->proxy->upstream.connection->fd); | |
707 | |
708 ngx_close_connection(s->proxy->upstream.connection); | |
709 } | |
710 | |
711 ngx_imap_session_internal_server_error(s); | |
712 } | |
713 | |
714 | |
715 static void | |
521 | 716 ngx_imap_proxy_close_session(ngx_imap_session_t *s) |
418
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
717 { |
521 | 718 if (s->proxy->upstream.connection) { |
719 ngx_log_debug1(NGX_LOG_DEBUG_IMAP, s->connection->log, 0, | |
720 "close imap proxy connection: %d", | |
721 s->proxy->upstream.connection->fd); | |
722 | |
723 ngx_close_connection(s->proxy->upstream.connection); | |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
724 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
725 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
726 ngx_imap_close_connection(s->connection); |
418
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
727 } |
521 | 728 |
729 | |
730 static void * | |
731 ngx_imap_proxy_create_conf(ngx_conf_t *cf) | |
577 | 732 { |
521 | 733 ngx_imap_proxy_conf_t *pcf; |
577 | 734 |
521 | 735 pcf = ngx_pcalloc(cf->pool, sizeof(ngx_imap_proxy_conf_t)); |
736 if (pcf == NULL) { | |
737 return NGX_CONF_ERROR; | |
738 } | |
739 | |
740 pcf->enable = NGX_CONF_UNSET; | |
539 | 741 pcf->buffer_size = NGX_CONF_UNSET_SIZE; |
742 pcf->timeout = NGX_CONF_UNSET_MSEC; | |
521 | 743 |
744 return pcf; | |
745 } | |
746 | |
747 | |
748 static char * | |
749 ngx_imap_proxy_merge_conf(ngx_conf_t *cf, void *parent, void *child) | |
750 { | |
751 ngx_imap_proxy_conf_t *prev = parent; | |
752 ngx_imap_proxy_conf_t *conf = child; | |
753 | |
539 | 754 ngx_conf_merge_value(conf->enable, prev->enable, 0); |
755 ngx_conf_merge_size_value(conf->buffer_size, prev->buffer_size, | |
756 (size_t) ngx_pagesize); | |
757 ngx_conf_merge_msec_value(conf->timeout, prev->timeout, 24 * 60 * 60000); | |
521 | 758 |
759 return NGX_CONF_OK; | |
760 } |