Mercurial > hg > nginx-vendor-0-7
comparison src/imap/ngx_imap_handler.c @ 90:71c46860eb55 NGINX_0_1_45
nginx 0.1.45
*) Change: the "ssl_engine" directive was canceled in the
ngx_http_ssl_module and now is introduced at global level.
*) Bugfix: the responses with SSI subrequests did not transferred via
SSL connection.
*) Various bug fixes in the IMAP/POP3 proxy.
author | Igor Sysoev <http://sysoev.ru> |
---|---|
date | Thu, 08 Sep 2005 00:00:00 +0400 |
parents | e916a291e9aa |
children | 45945fa8b8ba |
comparison
equal
deleted
inserted
replaced
89:7ed9767f1c4e | 90:71c46860eb55 |
---|---|
9 #include <ngx_event.h> | 9 #include <ngx_event.h> |
10 #include <ngx_imap.h> | 10 #include <ngx_imap.h> |
11 | 11 |
12 | 12 |
13 static void ngx_imap_init_session(ngx_event_t *rev); | 13 static void ngx_imap_init_session(ngx_event_t *rev); |
14 static void ngx_imap_init_protocol(ngx_event_t *rev); | |
14 static ngx_int_t ngx_imap_read_command(ngx_imap_session_t *s); | 15 static ngx_int_t ngx_imap_read_command(ngx_imap_session_t *s); |
16 static u_char *ngx_imap_log_error(ngx_log_t *log, u_char *buf, size_t len); | |
15 | 17 |
16 #if (NGX_IMAP_SSL) | 18 #if (NGX_IMAP_SSL) |
17 static void ngx_imap_ssl_close_handler(ngx_event_t *ev); | 19 static void ngx_imap_ssl_close_handler(ngx_event_t *ev); |
18 #endif | 20 #endif |
19 | 21 |
38 | 40 |
39 | 41 |
40 void | 42 void |
41 ngx_imap_init_connection(ngx_connection_t *c) | 43 ngx_imap_init_connection(ngx_connection_t *c) |
42 { | 44 { |
45 ngx_imap_log_ctx_t *ctx; | |
46 | |
47 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, c->log, 0, "imap init connection"); | |
48 | |
49 ctx = ngx_palloc(c->pool, sizeof(ngx_imap_log_ctx_t)); | |
50 if (ctx == NULL) { | |
51 ngx_imap_close_connection(c); | |
52 return; | |
53 } | |
54 | |
55 ctx->client = &c->addr_text; | |
56 ctx->session = NULL; | |
57 | |
58 c->log->connection = c->number; | |
59 c->log->handler = ngx_imap_log_error; | |
60 c->log->data = ctx; | |
61 c->log->action = "sending client greeting line"; | |
62 | |
63 c->log_error = NGX_ERROR_INFO; | |
64 | |
65 ngx_imap_init_session(c->read); | |
66 } | |
67 | |
68 | |
69 static void | |
70 ngx_imap_init_session(ngx_event_t *rev) | |
71 { | |
72 ngx_connection_t *c; | |
43 ngx_imap_session_t *s; | 73 ngx_imap_session_t *s; |
74 ngx_imap_log_ctx_t *lctx; | |
44 ngx_imap_conf_ctx_t *ctx; | 75 ngx_imap_conf_ctx_t *ctx; |
76 ngx_imap_core_srv_conf_t *cscf; | |
45 #if (NGX_IMAP_SSL) | 77 #if (NGX_IMAP_SSL) |
46 ngx_int_t rc; | 78 ngx_int_t rc; |
47 ngx_imap_ssl_conf_t *sslcf; | 79 ngx_imap_ssl_conf_t *sslcf; |
48 #endif | 80 #endif |
49 ngx_imap_core_srv_conf_t *cscf; | 81 |
50 | 82 c = rev->data; |
51 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, c->log, 0, "imap init connection"); | |
52 | |
53 c->log_error = NGX_ERROR_INFO; | |
54 | 83 |
55 ctx = c->ctx; | 84 ctx = c->ctx; |
85 | |
86 cscf = ngx_imap_get_module_srv_conf(ctx, ngx_imap_core_module); | |
56 | 87 |
57 #if (NGX_IMAP_SSL) | 88 #if (NGX_IMAP_SSL) |
58 | 89 |
59 sslcf = ngx_imap_get_module_srv_conf(ctx, ngx_imap_ssl_module); | 90 sslcf = ngx_imap_get_module_srv_conf(ctx, ngx_imap_ssl_module); |
60 | 91 |
72 if (rc == NGX_ERROR) { | 103 if (rc == NGX_ERROR) { |
73 ngx_imap_close_connection(c); | 104 ngx_imap_close_connection(c); |
74 return; | 105 return; |
75 } | 106 } |
76 | 107 |
108 if (rc == NGX_AGAIN) { | |
109 ngx_add_timer(rev, cscf->timeout); | |
110 c->read->handler = ngx_imap_init_session; | |
111 | |
112 if (ngx_handle_read_event(rev, 0) == NGX_ERROR) { | |
113 ngx_imap_close_connection(c); | |
114 } | |
115 | |
116 return; | |
117 } | |
118 | |
77 c->recv = ngx_ssl_recv; | 119 c->recv = ngx_ssl_recv; |
78 c->send = ngx_ssl_write; | 120 c->send = ngx_ssl_write; |
79 c->send_chain = ngx_ssl_send_chain; | 121 c->send_chain = ngx_ssl_send_chain; |
80 } | 122 } |
81 | 123 |
88 } | 130 } |
89 | 131 |
90 c->data = s; | 132 c->data = s; |
91 s->connection = c; | 133 s->connection = c; |
92 | 134 |
93 cscf = ngx_imap_get_module_srv_conf(ctx, ngx_imap_core_module); | |
94 s->protocol = cscf->protocol; | 135 s->protocol = cscf->protocol; |
95 | 136 |
96 s->ctx = ngx_pcalloc(c->pool, sizeof(void *) * ngx_imap_max_module); | 137 s->ctx = ngx_pcalloc(c->pool, sizeof(void *) * ngx_imap_max_module); |
97 if (s->ctx == NULL) { | 138 if (s->ctx == NULL) { |
98 ngx_imap_session_internal_server_error(s); | 139 ngx_imap_session_internal_server_error(s); |
102 s->main_conf = ctx->main_conf; | 143 s->main_conf = ctx->main_conf; |
103 s->srv_conf = ctx->srv_conf; | 144 s->srv_conf = ctx->srv_conf; |
104 | 145 |
105 s->out = greetings[s->protocol]; | 146 s->out = greetings[s->protocol]; |
106 | 147 |
107 c->read->handler = ngx_imap_init_session; | 148 lctx = c->log->data; |
149 lctx->session = s; | |
150 | |
151 c->read->handler = ngx_imap_init_protocol; | |
108 c->write->handler = ngx_imap_send; | 152 c->write->handler = ngx_imap_send; |
109 | 153 |
110 ngx_add_timer(c->write, cscf->timeout); | 154 ngx_add_timer(rev, cscf->timeout); |
111 ngx_add_timer(c->read, cscf->timeout); | 155 |
112 | 156 if (ngx_handle_read_event(rev, 0) == NGX_ERROR) { |
113 if (ngx_handle_read_event(c->read, 0) == NGX_ERROR) { | |
114 ngx_imap_close_connection(c); | 157 ngx_imap_close_connection(c); |
115 } | 158 } |
116 | 159 |
117 ngx_imap_send(c->write); | 160 ngx_imap_send(c->write); |
118 } | 161 } |
119 | 162 |
120 | 163 |
121 void | 164 void |
122 ngx_imap_send(ngx_event_t *wev) | 165 ngx_imap_send(ngx_event_t *wev) |
123 { | 166 { |
124 ngx_int_t n; | 167 ngx_int_t n; |
125 ngx_connection_t *c; | 168 ngx_connection_t *c; |
126 ngx_imap_session_t *s; | 169 ngx_imap_session_t *s; |
170 ngx_imap_core_srv_conf_t *cscf; | |
127 | 171 |
128 c = wev->data; | 172 c = wev->data; |
129 s = c->data; | 173 s = c->data; |
130 | 174 |
131 if (wev->timedout) { | 175 if (wev->timedout) { |
145 n = c->send(c, s->out.data, s->out.len); | 189 n = c->send(c, s->out.data, s->out.len); |
146 | 190 |
147 if (n > 0) { | 191 if (n > 0) { |
148 s->out.len -= n; | 192 s->out.len -= n; |
149 | 193 |
194 if (wev->timer_set) { | |
195 ngx_del_timer(wev); | |
196 } | |
197 | |
150 if (s->quit) { | 198 if (s->quit) { |
151 ngx_imap_close_connection(c); | 199 ngx_imap_close_connection(c); |
152 return; | 200 return; |
153 } | 201 } |
154 | 202 |
164 return; | 212 return; |
165 } | 213 } |
166 | 214 |
167 /* n == NGX_AGAIN */ | 215 /* n == NGX_AGAIN */ |
168 | 216 |
217 cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module); | |
218 | |
219 ngx_add_timer(c->write, cscf->timeout); | |
220 | |
169 if (ngx_handle_write_event(c->write, 0) == NGX_ERROR) { | 221 if (ngx_handle_write_event(c->write, 0) == NGX_ERROR) { |
170 ngx_imap_close_connection(c); | 222 ngx_imap_close_connection(c); |
171 return; | 223 return; |
172 } | 224 } |
173 } | 225 } |
174 | 226 |
175 | 227 |
176 static void | 228 static void |
177 ngx_imap_init_session(ngx_event_t *rev) | 229 ngx_imap_init_protocol(ngx_event_t *rev) |
178 { | 230 { |
179 size_t size; | 231 size_t size; |
180 ngx_connection_t *c; | 232 ngx_connection_t *c; |
181 ngx_imap_session_t *s; | 233 ngx_imap_session_t *s; |
182 ngx_imap_core_srv_conf_t *cscf; | 234 ngx_imap_core_srv_conf_t *cscf; |
183 | 235 |
184 c = rev->data; | 236 c = rev->data; |
237 | |
238 c->log->action = "in auth state"; | |
185 | 239 |
186 if (rev->timedout) { | 240 if (rev->timedout) { |
187 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out"); | 241 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out"); |
188 ngx_imap_close_connection(c); | 242 ngx_imap_close_connection(c); |
189 return; | 243 return; |
664 | 718 |
665 ngx_imap_close_connection(c); | 719 ngx_imap_close_connection(c); |
666 } | 720 } |
667 | 721 |
668 #endif | 722 #endif |
723 | |
724 | |
725 static u_char * | |
726 ngx_imap_log_error(ngx_log_t *log, u_char *buf, size_t len) | |
727 { | |
728 u_char *p; | |
729 ngx_imap_session_t *s; | |
730 ngx_imap_log_ctx_t *ctx; | |
731 | |
732 if (log->action) { | |
733 p = ngx_snprintf(buf, len, " while %s", log->action); | |
734 len -= p - buf; | |
735 buf = p; | |
736 } | |
737 | |
738 ctx = log->data; | |
739 | |
740 p = ngx_snprintf(buf, len, ", client: %V", ctx->client); | |
741 len -= p - buf; | |
742 buf = p; | |
743 | |
744 s = ctx->session; | |
745 | |
746 if (s == NULL) { | |
747 return p; | |
748 } | |
749 | |
750 p = ngx_snprintf(buf, len, ", server: %V", | |
751 &s->connection->listening->addr_text); | |
752 len -= p - buf; | |
753 buf = p; | |
754 | |
755 if (s->login.len == 0) { | |
756 return p; | |
757 } | |
758 | |
759 p = ngx_snprintf(buf, len, ", login: \"%V\"", &s->login); | |
760 len -= p - buf; | |
761 buf = p; | |
762 | |
763 if (s->proxy == NULL) { | |
764 return p; | |
765 } | |
766 | |
767 p = ngx_snprintf(buf, len, ", upstream: %V", | |
768 &s->proxy->upstream.peers->peer[0].name); | |
769 | |
770 return p; | |
771 } |