comparison src/imap/ngx_imap_core_module.c @ 521:6f00349b98e5 release-0.1.35

nginx-0.1.35-RELEASE import *) Feature: the "working_directory" directive. *) Feature: the "port_in_redirect" directive. *) Bugfix: the segmentation fault was occurred if the backend response header was in several packets; the bug had appeared in 0.1.29. *) Bugfix: if more than 10 servers were configured or some server did not use the "listen" directive, then the segmentation fault was occurred on the start. *) Bugfix: the segmentation fault might occur if the response was bigger than the temporary file. *) Bugfix: nginx returned the 400 response on requests like "GET http://www.domain.com/uri HTTP/1.0"; the bug had appeared in 0.1.28.
author Igor Sysoev <igor@sysoev.ru>
date Tue, 07 Jun 2005 15:56:31 +0000
parents
children 2019117e6b38
comparison
equal deleted inserted replaced
520:1fecc7e0d717 521:6f00349b98e5
1
2 /*
3 * Copyright (C) Igor Sysoev
4 */
5
6
7 #include <ngx_config.h>
8 #include <ngx_core.h>
9 #include <ngx_event.h>
10 #include <ngx_imap.h>
11
12
13 static void *ngx_imap_core_create_main_conf(ngx_conf_t *cf);
14 static void *ngx_imap_core_create_srv_conf(ngx_conf_t *cf);
15 static char *ngx_imap_core_merge_srv_conf(ngx_conf_t *cf, void *parent,
16 void *child);
17 static char *ngx_imap_core_server(ngx_conf_t *cf, ngx_command_t *cmd,
18 void *conf);
19 static char *ngx_imap_core_listen(ngx_conf_t *cf, ngx_command_t *cmd,
20 void *conf);
21
22
23 static ngx_conf_enum_t ngx_imap_core_procotol[] = {
24 { ngx_string("pop3"), NGX_IMAP_POP3_PROTOCOL },
25 { ngx_string("imap"), NGX_IMAP_IMAP_PROTOCOL },
26 { ngx_null_string, 0 }
27 };
28
29
30 static ngx_command_t ngx_imap_core_commands[] = {
31
32 { ngx_string("server"),
33 NGX_IMAP_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
34 ngx_imap_core_server,
35 0,
36 0,
37 NULL },
38
39 { ngx_string("listen"),
40 NGX_IMAP_SRV_CONF|NGX_CONF_TAKE1,
41 ngx_imap_core_listen,
42 0,
43 0,
44 NULL },
45
46 { ngx_string("protocol"),
47 NGX_IMAP_SRV_CONF|NGX_CONF_TAKE1,
48 ngx_conf_set_enum_slot,
49 NGX_IMAP_SRV_CONF_OFFSET,
50 offsetof(ngx_imap_core_srv_conf_t, protocol),
51 &ngx_imap_core_procotol },
52
53 { ngx_string("imap_client_buffer"),
54 NGX_IMAP_MAIN_CONF|NGX_IMAP_SRV_CONF|NGX_CONF_TAKE1,
55 ngx_conf_set_size_slot,
56 NGX_IMAP_SRV_CONF_OFFSET,
57 offsetof(ngx_imap_core_srv_conf_t, imap_client_buffer_size),
58 NULL },
59
60 { ngx_string("proxy_buffer"),
61 NGX_IMAP_MAIN_CONF|NGX_IMAP_SRV_CONF|NGX_CONF_TAKE1,
62 ngx_conf_set_size_slot,
63 NGX_IMAP_SRV_CONF_OFFSET,
64 offsetof(ngx_imap_core_srv_conf_t, proxy_buffer_size),
65 NULL },
66
67 { ngx_string("timeout"),
68 NGX_IMAP_MAIN_CONF|NGX_IMAP_SRV_CONF|NGX_CONF_TAKE1,
69 ngx_conf_set_msec_slot,
70 NGX_IMAP_SRV_CONF_OFFSET,
71 offsetof(ngx_imap_core_srv_conf_t, timeout),
72 NULL },
73
74 ngx_null_command
75 };
76
77
78 static ngx_imap_module_t ngx_imap_core_module_ctx = {
79 ngx_imap_core_create_main_conf, /* create main configuration */
80 NULL, /* init main configuration */
81
82 ngx_imap_core_create_srv_conf, /* create server configuration */
83 ngx_imap_core_merge_srv_conf /* merge server configuration */
84 };
85
86
87 ngx_module_t ngx_imap_core_module = {
88 NGX_MODULE_V1,
89 &ngx_imap_core_module_ctx, /* module context */
90 ngx_imap_core_commands, /* module directives */
91 NGX_IMAP_MODULE, /* module type */
92 NULL, /* init module */
93 NULL /* init process */
94 };
95
96
97 static void *
98 ngx_imap_core_create_main_conf(ngx_conf_t *cf)
99 {
100 ngx_imap_core_main_conf_t *cmcf;
101
102 cmcf = ngx_pcalloc(cf->pool, sizeof(ngx_imap_core_main_conf_t));
103 if (cmcf == NULL) {
104 return NGX_CONF_ERROR;
105 }
106
107 if (ngx_array_init(&cmcf->servers, cf->pool, 4,
108 sizeof(ngx_imap_core_srv_conf_t *)) == NGX_ERROR)
109 {
110 return NGX_CONF_ERROR;
111 }
112
113 return cmcf;
114 }
115
116
117 static void *
118 ngx_imap_core_create_srv_conf(ngx_conf_t *cf)
119 {
120 ngx_imap_core_srv_conf_t *cscf;
121
122 cscf = ngx_pcalloc(cf->pool, sizeof(ngx_imap_core_srv_conf_t));
123 if (cscf == NULL) {
124 return NGX_CONF_ERROR;
125 }
126
127 cscf->imap_client_buffer_size = NGX_CONF_UNSET_SIZE;
128 cscf->proxy_buffer_size = NGX_CONF_UNSET_SIZE;
129 cscf->timeout = NGX_CONF_UNSET_MSEC;
130 cscf->protocol = NGX_CONF_UNSET_UINT;
131
132 return cscf;
133 }
134
135
136 static char *
137 ngx_imap_core_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
138 {
139 ngx_imap_core_srv_conf_t *prev = parent;
140 ngx_imap_core_srv_conf_t *conf = child;
141
142 ngx_conf_merge_size_value(conf->imap_client_buffer_size,
143 prev->imap_client_buffer_size,
144 (size_t) ngx_pagesize);
145 ngx_conf_merge_size_value(conf->proxy_buffer_size, prev->proxy_buffer_size,
146 (size_t) ngx_pagesize);
147 ngx_conf_merge_msec_value(conf->timeout, prev->timeout, 60000);
148 ngx_conf_merge_unsigned_value(conf->protocol, prev->protocol,
149 NGX_IMAP_IMAP_PROTOCOL);
150
151 return NGX_CONF_OK;
152 }
153
154
155 static char *
156 ngx_imap_core_server(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
157 {
158 char *rv;
159 void *mconf;
160 ngx_uint_t m;
161 ngx_conf_t pcf;
162 ngx_imap_module_t *module;
163 ngx_imap_conf_ctx_t *ctx, *imap_ctx;
164 ngx_imap_core_srv_conf_t *cscf, **cscfp;
165 ngx_imap_core_main_conf_t *cmcf;
166
167
168 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_imap_conf_ctx_t));
169 if (ctx == NULL) {
170 return NGX_CONF_ERROR;
171 }
172
173 imap_ctx = cf->ctx;
174 ctx->main_conf = imap_ctx->main_conf;
175
176 /* the server{}'s srv_conf */
177
178 ctx->srv_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_imap_max_module);
179 if (ctx->srv_conf == NULL) {
180 return NGX_CONF_ERROR;
181 }
182
183 for (m = 0; ngx_modules[m]; m++) {
184 if (ngx_modules[m]->type != NGX_IMAP_MODULE) {
185 continue;
186 }
187
188 module = ngx_modules[m]->ctx;
189
190 if (module->create_srv_conf) {
191 mconf = module->create_srv_conf(cf);
192 if (mconf == NULL) {
193 return NGX_CONF_ERROR;
194 }
195
196 ctx->srv_conf[ngx_modules[m]->ctx_index] = mconf;
197 }
198 }
199
200 /* the server configuration context */
201
202 cscf = ctx->srv_conf[ngx_imap_core_module.ctx_index];
203 cscf->ctx = ctx;
204
205 cmcf = ctx->main_conf[ngx_imap_core_module.ctx_index];
206
207 cscfp = ngx_array_push(&cmcf->servers);
208 if (cscfp == NULL) {
209 return NGX_CONF_ERROR;
210 }
211
212 *cscfp = cscf;
213
214
215 /* parse inside server{} */
216
217 pcf = *cf;
218 cf->ctx = ctx;
219 cf->cmd_type = NGX_IMAP_SRV_CONF;
220
221 rv = ngx_conf_parse(cf, NULL);
222
223 *cf = pcf;
224
225 return rv;
226 }
227
228
229 /* AF_INET only */
230
231 static char *
232 ngx_imap_core_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
233 {
234 char *err;
235 ngx_str_t *value;
236 in_addr_t in_addr;
237 struct hostent *h;
238 ngx_listening_t *ls;
239 ngx_inet_upstream_t inet_upstream;
240
241 value = cf->args->elts;
242
243 ngx_memzero(&inet_upstream, sizeof(ngx_inet_upstream_t));
244
245 inet_upstream.url = value[1];
246 inet_upstream.port_only = 1;
247
248 err = ngx_inet_parse_host_port(&inet_upstream);
249
250 if (err) {
251 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
252 "%s in \"%V\" of the \"listen\" directive", err);
253 return NGX_CONF_ERROR;
254 }
255
256 if (inet_upstream.host.len) {
257 inet_upstream.host.data[inet_upstream.host.len] = '\0';
258
259 in_addr = inet_addr((const char *) inet_upstream.host.data);
260
261 if (in_addr == INADDR_NONE) {
262 h = gethostbyname((const char *) inet_upstream.host.data);
263
264 if (h == NULL || h->h_addr_list[0] == NULL) {
265 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
266 "can not resolve host \"%s\" "
267 "in the \"listen\" directive",
268 inet_upstream.host.data);
269 return NGX_CONF_ERROR;
270 }
271
272 in_addr = *(in_addr_t *)(h->h_addr_list[0]);
273 }
274
275 } else {
276 in_addr = INADDR_ANY;
277 }
278
279
280 ls = ngx_listening_inet_stream_socket(cf, in_addr, inet_upstream.port);
281 if (ls == NULL) {
282 return NGX_CONF_ERROR;
283 }
284
285 ls->backlog = -1;
286 ls->addr_ntop = 1;
287 ls->handler = ngx_imap_init_connection;
288 ls->pool_size = 256;
289
290 ls->ctx = cf->ctx;
291
292 /* STUB */
293 ls->log = cf->cycle->new_log;
294 /**/
295
296 return NGX_CONF_OK;
297 }