annotate src/imap/ngx_imap_proxy_module.c @ 76:da9a3b14312d NGINX_0_1_38

nginx 0.1.38 *) Feature: the "limit_rate" directive is supported in in proxy and FastCGI mode. *) Feature: the "X-Accel-Limit-Rate" response header line is supported in proxy and FastCGI mode. *) Feature: the "break" directive. *) Feature: the "log_not_found" directive. *) Bugfix: the response status code was not changed when request was redirected by the ""X-Accel-Redirect" header line. *) Bugfix: the variables set by the "set" directive could not be used in SSI. *) Bugfix: the segmentation fault may occurred if the SSI page has more than one remote subrequest. *) Bugfix: nginx treated the backend response as invalid if the status line in the header was transferred in two packets; bug appeared in 0.1.29. *) Feature: the "ssi_types" directive. *) Feature: the "autoindex_exact_size" directive. *) Bugfix: the ngx_http_autoindex_module did not support the long file names in UTF-8. *) Feature: the IMAP/POP3 proxy.
author Igor Sysoev <http://sysoev.ru>
date Fri, 08 Jul 2005 00:00:00 +0400
parents
children 9db7e0b5b27f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
76
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
1
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
2 /*
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
3 * Copyright (C) Igor Sysoev
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
4 */
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
5
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
6
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
7 #include <ngx_config.h>
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
8 #include <ngx_core.h>
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
9 #include <ngx_event.h>
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
10 #include <ngx_event_connect.h>
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
11 #include <ngx_imap.h>
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
12
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
13
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
14 typedef struct {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
15 ngx_flag_t enable;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
16 } ngx_imap_proxy_conf_t;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
17
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
18
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
19 static void ngx_imap_proxy_block_read(ngx_event_t *rev);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
20 static void ngx_imap_proxy_imap_handler(ngx_event_t *rev);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
21 static void ngx_imap_proxy_pop3_handler(ngx_event_t *rev);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
22 static void ngx_imap_proxy_dummy_handler(ngx_event_t *ev);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
23 static ngx_int_t ngx_imap_proxy_read_response(ngx_imap_session_t *s,
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
24 ngx_uint_t what);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
25 static void ngx_imap_proxy_handler(ngx_event_t *ev);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
26 static void ngx_imap_proxy_internal_server_error(ngx_imap_session_t *s);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
27 static void ngx_imap_proxy_close_session(ngx_imap_session_t *s);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
28 static void *ngx_imap_proxy_create_conf(ngx_conf_t *cf);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
29 static char *ngx_imap_proxy_merge_conf(ngx_conf_t *cf, void *parent,
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
30 void *child);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
31
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
32
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
33 #define NGX_IMAP_WAIT_OK 0
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
34 #define NGX_IMAP_WAIT_NEXT 1
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
35
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
36
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
37 static ngx_command_t ngx_imap_proxy_commands[] = {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
38 { ngx_string("proxy"),
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
39 NGX_IMAP_MAIN_CONF|NGX_IMAP_SRV_CONF|NGX_CONF_FLAG,
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
40 ngx_conf_set_flag_slot,
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
41 NGX_IMAP_SRV_CONF_OFFSET,
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
42 offsetof(ngx_imap_proxy_conf_t, enable),
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
43 NULL },
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
44
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
45 ngx_null_command
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
46 };
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
47
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
48
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
49 static ngx_imap_module_t ngx_imap_proxy_module_ctx = {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
50 NULL, /* create main configuration */
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
51 NULL, /* init main configuration */
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
52
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
53 ngx_imap_proxy_create_conf, /* create server configuration */
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
54 ngx_imap_proxy_merge_conf /* merge server configuration */
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
55 };
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
56
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
57
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
58 ngx_module_t ngx_imap_proxy_module = {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
59 NGX_MODULE_V1,
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
60 &ngx_imap_proxy_module_ctx, /* module context */
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
61 ngx_imap_proxy_commands, /* module directives */
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
62 NGX_IMAP_MODULE, /* module type */
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
63 NULL, /* init module */
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
64 NULL /* init process */
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
65 };
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
66
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
67
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
68 void
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
69 ngx_imap_proxy_init(ngx_imap_session_t *s, ngx_peers_t *peers)
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
70 {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
71 ngx_int_t rc;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
72 ngx_imap_proxy_ctx_t *p;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
73 ngx_imap_core_srv_conf_t *cscf;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
74
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
75 p = ngx_pcalloc(s->connection->pool, sizeof(ngx_imap_proxy_ctx_t));
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
76 if (p == NULL) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
77 ngx_imap_session_internal_server_error(s);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
78 return;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
79 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
80
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
81 s->proxy = p;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
82
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
83 p->upstream.peers = peers;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
84 p->upstream.log = s->connection->log;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
85 p->upstream.log_error = NGX_ERROR_ERR;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
86
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
87 rc = ngx_event_connect_peer(&p->upstream);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
88
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
89 if (rc == NGX_ERROR) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
90 ngx_imap_session_internal_server_error(s);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
91 return;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
92 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
93
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
94 cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
95 ngx_add_timer(p->upstream.connection->read, cscf->timeout);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
96
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
97 p->upstream.connection->data = s;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
98 p->upstream.connection->pool = s->connection->pool;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
99
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
100 s->connection->read->handler = ngx_imap_proxy_block_read;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
101 p->upstream.connection->write->handler = ngx_imap_proxy_dummy_handler;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
102
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
103 if (s->protocol == NGX_IMAP_POP3_PROTOCOL) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
104 p->upstream.connection->read->handler = ngx_imap_proxy_pop3_handler;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
105 s->imap_state = ngx_pop3_start;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
106
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
107 } else {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
108 p->upstream.connection->read->handler = ngx_imap_proxy_imap_handler;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
109 s->imap_state = ngx_imap_start;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
110 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
111 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
112
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
113
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
114 static void
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
115 ngx_imap_proxy_block_read(ngx_event_t *rev)
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
116 {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
117 ngx_connection_t *c;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
118 ngx_imap_session_t *s;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
119
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
120 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0, "imap proxy block read");
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
121
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
122 if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
123 c = rev->data;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
124 s = c->data;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
125
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
126 ngx_imap_proxy_close_session(s);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
127 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
128 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
129
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
130
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
131 static void
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
132 ngx_imap_proxy_imap_handler(ngx_event_t *rev)
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
133 {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
134 u_char *p;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
135 ngx_int_t rc;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
136 ngx_str_t line;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
137 ngx_connection_t *c;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
138 ngx_imap_session_t *s;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
139 ngx_imap_core_srv_conf_t *cscf;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
140
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
141 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0,
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
142 "imap proxy imap auth handler");
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
143
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
144 c = rev->data;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
145 s = c->data;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
146
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
147 if (rev->timedout) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
148 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
149 "upstream timed out");
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
150 ngx_imap_proxy_internal_server_error(s);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
151 return;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
152 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
153
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
154 if (s->proxy->buffer == NULL) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
155 cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
156
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
157 s->proxy->buffer = ngx_create_temp_buf(c->pool,
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
158 cscf->proxy_buffer_size);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
159 if (s->proxy->buffer == NULL) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
160 ngx_imap_proxy_internal_server_error(s);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
161 return;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
162 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
163 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
164
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
165 rc = ngx_imap_proxy_read_response(s, s->imap_state == ngx_imap_start ?
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
166 NGX_IMAP_WAIT_OK : NGX_IMAP_WAIT_NEXT);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
167
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
168 if (rc == NGX_AGAIN) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
169 return;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
170 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
171
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
172 if (rc == NGX_ERROR) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
173 ngx_imap_proxy_internal_server_error(s);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
174 return;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
175 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
176
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
177 switch (s->imap_state) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
178
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
179 case ngx_imap_start:
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
180 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0,
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
181 "imap proxy send login");
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
182
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
183 line.len = s->tag.len + sizeof("LOGIN ") - 1
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
184 + 1 + NGX_SIZE_T_LEN + 1 + 2;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
185 line.data = ngx_palloc(c->pool, line.len);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
186 if (line.data == NULL) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
187 ngx_imap_proxy_internal_server_error(s);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
188 return;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
189 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
190
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
191 line.len = ngx_sprintf(line.data, "%VLOGIN {%uz}" CRLF,
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
192 &s->tag, s->login.len)
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
193 - line.data;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
194
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
195 s->imap_state = ngx_imap_login;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
196 break;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
197
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
198 case ngx_imap_login:
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
199 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0, "imap proxy send user");
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
200
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
201 line.len = s->login.len + 1 + NGX_SIZE_T_LEN + 1 + 2;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
202 line.data = ngx_palloc(c->pool, line.len);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
203 if (line.data == NULL) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
204 ngx_imap_proxy_internal_server_error(s);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
205 return;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
206 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
207
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
208 line.len = ngx_sprintf(line.data, "%V{%uz}" CRLF,
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
209 &s->login, s->passwd.len)
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
210 - line.data;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
211
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
212 s->imap_state = ngx_imap_user;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
213 break;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
214
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
215 case ngx_imap_user:
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
216 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0,
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
217 "imap proxy send passwd");
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
218
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
219 line.len = s->passwd.len + 2;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
220 line.data = ngx_palloc(c->pool, line.len);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
221 if (line.data == NULL) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
222 ngx_imap_proxy_internal_server_error(s);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
223 return;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
224 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
225
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
226 p = ngx_cpymem(line.data, s->passwd.data, s->passwd.len);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
227 *p++ = CR; *p = LF;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
228
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
229 s->imap_state = ngx_imap_passwd;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
230 break;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
231
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
232 default:
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
233 #if (NGX_SUPPRESS_WARN)
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
234 line.len = 0;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
235 line.data = NULL;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
236 #endif
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
237 break;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
238 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
239
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
240 if (ngx_send(c, line.data, line.len) < (ssize_t) line.len) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
241 /*
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
242 * we treat the incomplete sending as NGX_ERROR
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
243 * because it is very strange here
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
244 */
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
245 ngx_imap_proxy_internal_server_error(s);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
246 return;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
247 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
248
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
249 s->proxy->buffer->pos = s->proxy->buffer->start;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
250 s->proxy->buffer->last = s->proxy->buffer->start;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
251
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
252 if (s->imap_state == ngx_imap_passwd) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
253 s->connection->read->handler = ngx_imap_proxy_handler;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
254 s->connection->write->handler = ngx_imap_proxy_handler;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
255 rev->handler = ngx_imap_proxy_handler;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
256 c->write->handler = ngx_imap_proxy_handler;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
257 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
258 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
259
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
260
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
261 static void
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
262 ngx_imap_proxy_pop3_handler(ngx_event_t *rev)
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
263 {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
264 u_char *p;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
265 ngx_int_t rc;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
266 ngx_str_t line;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
267 ngx_connection_t *c;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
268 ngx_imap_session_t *s;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
269 ngx_imap_core_srv_conf_t *cscf;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
270
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
271 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0,
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
272 "imap proxy pop3 auth handler");
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
273
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
274 c = rev->data;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
275 s = c->data;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
276
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
277 if (rev->timedout) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
278 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
279 "upstream timed out");
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
280 ngx_imap_proxy_internal_server_error(s);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
281 return;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
282 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
283
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
284 if (s->proxy->buffer == NULL) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
285 cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
286
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
287 s->proxy->buffer = ngx_create_temp_buf(c->pool,
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
288 cscf->proxy_buffer_size);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
289 if (s->proxy->buffer == NULL) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
290 ngx_imap_proxy_internal_server_error(s);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
291 return;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
292 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
293 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
294
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
295 rc = ngx_imap_proxy_read_response(s, NGX_IMAP_WAIT_OK);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
296
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
297 if (rc == NGX_AGAIN) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
298 return;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
299 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
300
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
301 if (rc == NGX_ERROR) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
302 ngx_imap_proxy_internal_server_error(s);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
303 return;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
304 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
305
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
306 switch (s->imap_state) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
307
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
308 case ngx_pop3_start:
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
309 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0, "imap proxy send user");
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
310
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
311 line.len = sizeof("USER ") - 1 + s->login.len + 2;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
312 line.data = ngx_palloc(c->pool, line.len);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
313 if (line.data == NULL) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
314 ngx_imap_proxy_internal_server_error(s);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
315 return;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
316 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
317
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
318 p = ngx_cpymem(line.data, "USER ", sizeof("USER ") - 1);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
319 p = ngx_cpymem(p, s->login.data, s->login.len);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
320 *p++ = CR; *p = LF;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
321
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
322 s->imap_state = ngx_pop3_user;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
323 break;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
324
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
325 case ngx_pop3_user:
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
326 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0, "imap proxy send pass");
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
327
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
328 line.len = sizeof("PASS ") - 1 + s->passwd.len + 2;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
329 line.data = ngx_palloc(c->pool, line.len);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
330 if (line.data == NULL) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
331 ngx_imap_proxy_internal_server_error(s);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
332 return;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
333 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
334
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
335 p = ngx_cpymem(line.data, "PASS ", sizeof("PASS ") - 1);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
336 p = ngx_cpymem(p, s->passwd.data, s->passwd.len);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
337 *p++ = CR; *p = LF;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
338
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
339 s->imap_state = ngx_pop3_passwd;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
340 break;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
341
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
342 default:
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
343 #if (NGX_SUPPRESS_WARN)
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
344 line.len = 0;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
345 line.data = NULL;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
346 #endif
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
347 break;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
348 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
349
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
350 if (ngx_send(c, line.data, line.len) < (ssize_t) line.len) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
351 /*
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
352 * we treat the incomplete sending as NGX_ERROR
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
353 * because it is very strange here
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
354 */
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
355 ngx_imap_proxy_internal_server_error(s);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
356 return;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
357 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
358
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
359 s->proxy->buffer->pos = s->proxy->buffer->start;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
360 s->proxy->buffer->last = s->proxy->buffer->start;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
361
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
362 if (s->imap_state == ngx_pop3_passwd) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
363 s->connection->read->handler = ngx_imap_proxy_handler;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
364 s->connection->write->handler = ngx_imap_proxy_handler;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
365 rev->handler = ngx_imap_proxy_handler;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
366 c->write->handler = ngx_imap_proxy_handler;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
367 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
368 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
369
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
370
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
371 static void
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
372 ngx_imap_proxy_dummy_handler(ngx_event_t *ev)
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
373 {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
374 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, ev->log, 0, "imap proxy dummy handler");
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
375 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
376
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
377
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
378 static ngx_int_t
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
379 ngx_imap_proxy_read_response(ngx_imap_session_t *s, ngx_uint_t what)
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
380 {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
381 u_char *p;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
382 ssize_t n;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
383 ngx_buf_t *b;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
384
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
385 b = s->proxy->buffer;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
386
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
387 n = ngx_recv(s->proxy->upstream.connection, b->last, b->end - b->last);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
388
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
389 if (n == NGX_ERROR || n == 0) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
390 return NGX_ERROR;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
391 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
392
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
393 if (n == NGX_AGAIN) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
394 return NGX_AGAIN;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
395 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
396
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
397 b->last += n;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
398
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
399 if (b->last - b->pos < 5) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
400 return NGX_AGAIN;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
401 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
402
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
403 if (*(b->last - 2) != CR || *(b->last - 1) != LF) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
404 if (b->last == b->end) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
405 *(b->last - 1) = '\0';
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
406 ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
407 "upstream sent too long response line: \"%s\"",
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
408 b->pos);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
409 return NGX_IMAP_PROXY_INVALID;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
410 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
411
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
412 return NGX_AGAIN;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
413 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
414
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
415 p = b->pos;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
416
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
417 if (s->protocol == NGX_IMAP_POP3_PROTOCOL) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
418 if (p[0] == '+' && p[1] == 'O' && p[2] == 'K') {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
419 return NGX_OK;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
420 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
421
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
422 if (p[0] == '-' && p[1] == 'E' && p[2] == 'R' && p[3] == 'R') {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
423 return NGX_IMAP_PROXY_ERROR;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
424 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
425
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
426 } else {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
427 if (what == NGX_IMAP_WAIT_OK) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
428 if (p[0] == '*' && p[1] == ' ' && p[2] == 'O' && p[3] == 'K') {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
429 return NGX_OK;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
430 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
431
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
432 } else {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
433 if (p[0] == '+' && p[1] == ' ' && p[2] == 'O' && p[3] == 'K') {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
434 return NGX_OK;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
435 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
436 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
437 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
438
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
439 *(b->last - 2) = '\0';
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
440 ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
441 "upstream sent invalid response: \"%s\"", p);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
442
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
443 return NGX_IMAP_PROXY_INVALID;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
444 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
445
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
446
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
447 static void
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
448 ngx_imap_proxy_handler(ngx_event_t *ev)
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
449 {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
450 size_t size;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
451 ssize_t n;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
452 ngx_buf_t *b;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
453 ngx_uint_t again, do_write;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
454 ngx_connection_t *c, *src, *dst;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
455 ngx_imap_session_t *s;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
456
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
457 c = ev->data;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
458 s = c->data;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
459
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
460 if (ev->timedout) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
461 if (c == s->connection) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
462 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
463 "client timed out");
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
464 } else {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
465 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
466 "upstream timed out");
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
467 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
468
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
469 ngx_imap_proxy_close_session(s);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
470 return;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
471 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
472
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
473 if (c == s->connection) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
474 if (ev->write) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
475 src = s->proxy->upstream.connection;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
476 dst = c;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
477 b = s->proxy->buffer;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
478
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
479 } else {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
480 src = c;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
481 dst = s->proxy->upstream.connection;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
482 b = s->buffer;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
483 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
484
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
485 } else {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
486 if (ev->write) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
487 src = s->connection;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
488 dst = c;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
489 b = s->buffer;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
490
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
491 } else {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
492 src = c;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
493 dst = s->connection;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
494 b = s->proxy->buffer;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
495 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
496 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
497
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
498 do_write = ev->write ? 1 : 0;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
499
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
500 ngx_log_debug3(NGX_LOG_DEBUG_IMAP, ev->log, 0,
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
501 "imap proxy handler: %d, #%d > #%d",
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
502 do_write, src->fd, dst->fd);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
503
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
504 do {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
505 again = 0;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
506
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
507 if (do_write == 1) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
508
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
509 size = b->last - b->pos;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
510
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
511 if (size && dst->write->ready) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
512 n = ngx_send(dst, b->pos, size);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
513
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
514 if (n == NGX_ERROR) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
515 ngx_imap_proxy_close_session(s);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
516 return;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
517 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
518
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
519 if (n > 0) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
520 again = 1;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
521 b->pos += n;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
522
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
523 if (b->pos == b->last) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
524 b->pos = b->start;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
525 b->last = b->start;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
526 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
527 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
528
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
529 if (n == NGX_AGAIN || n < (ssize_t) size) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
530 if (ngx_handle_write_event(dst->write, /* TODO: LOWAT */ 0)
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
531 == NGX_ERROR)
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
532 {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
533 ngx_imap_proxy_close_session(s);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
534 return;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
535 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
536 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
537 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
538 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
539
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
540 size = b->end - b->last;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
541
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
542 if (size && src->read->ready) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
543 n = ngx_recv(src, b->last, size);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
544
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
545 if (n == NGX_ERROR || n == 0) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
546 ngx_imap_proxy_close_session(s);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
547 return;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
548 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
549
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
550 if (n > 0) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
551 again = 1;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
552 do_write = 1;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
553 b->last += n;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
554 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
555
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
556 if (n == NGX_AGAIN || n < (ssize_t) size) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
557 if (ngx_handle_read_event(src->read, 0) == NGX_ERROR) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
558 ngx_imap_proxy_close_session(s);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
559 return;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
560 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
561 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
562 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
563
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
564 } while (again);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
565 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
566
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
567
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
568 static void
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
569 ngx_imap_proxy_internal_server_error(ngx_imap_session_t *s)
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
570 {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
571 if (s->proxy->upstream.connection) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
572 ngx_log_debug1(NGX_LOG_DEBUG_IMAP, s->connection->log, 0,
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
573 "close imap proxy connection: %d",
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
574 s->proxy->upstream.connection->fd);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
575
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
576 ngx_close_connection(s->proxy->upstream.connection);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
577 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
578
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
579 ngx_imap_session_internal_server_error(s);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
580 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
581
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
582
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
583 static void
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
584 ngx_imap_proxy_close_session(ngx_imap_session_t *s)
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
585 {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
586 if (s->proxy->upstream.connection) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
587 ngx_log_debug1(NGX_LOG_DEBUG_IMAP, s->connection->log, 0,
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
588 "close imap proxy connection: %d",
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
589 s->proxy->upstream.connection->fd);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
590
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
591 ngx_close_connection(s->proxy->upstream.connection);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
592 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
593
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
594 ngx_imap_close_connection(s->connection);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
595 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
596
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
597
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
598 static void *
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
599 ngx_imap_proxy_create_conf(ngx_conf_t *cf)
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
600 {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
601 ngx_imap_proxy_conf_t *pcf;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
602
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
603 pcf = ngx_pcalloc(cf->pool, sizeof(ngx_imap_proxy_conf_t));
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
604 if (pcf == NULL) {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
605 return NGX_CONF_ERROR;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
606 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
607
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
608 pcf->enable = NGX_CONF_UNSET;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
609
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
610 return pcf;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
611 }
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
612
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
613
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
614 static char *
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
615 ngx_imap_proxy_merge_conf(ngx_conf_t *cf, void *parent, void *child)
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
616 {
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
617 ngx_imap_proxy_conf_t *prev = parent;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
618 ngx_imap_proxy_conf_t *conf = child;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
619
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
620 ngx_conf_merge_msec_value(conf->enable, prev->enable, 0);
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
621
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
622 return NGX_CONF_OK;
da9a3b14312d nginx 0.1.38
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
623 }