comparison src/http/ngx_http.c @ 44:0e81ac0bb3e2

nginx-0.0.1-2003-01-09-08:36:00 import
author Igor Sysoev <igor@sysoev.ru>
date Thu, 09 Jan 2003 05:36:00 +0000
parents 59e7c7f30d49
children f1ee46c036a4
comparison
equal deleted inserted replaced
43:53cd05892261 44:0e81ac0bb3e2
3 #include <ngx_string.h> 3 #include <ngx_string.h>
4 #include <ngx_socket.h> 4 #include <ngx_socket.h>
5 #include <ngx_listen.h> 5 #include <ngx_listen.h>
6 #include <ngx_http.h> 6 #include <ngx_http.h>
7 #include <ngx_http_config.h> 7 #include <ngx_http_config.h>
8 8 #include <ngx_http_core_module.h>
9 extern ngx_array_t *ngx_listening_sockets; 9
10 10
11 static char *ngx_http_block(ngx_conf_t *cf, ngx_command_t *cmd, char *dummy);
12
13
14 int ngx_http_max_module;
15
16 ngx_array_t ngx_http_servers; /* array of ngx_http_core_srv_conf_t */
17
18 int ngx_http_post_accept_timeout = 10000;
19 int ngx_http_connection_pool_size = 16384;
20 int ngx_http_request_pool_size = 16384;
21 int ngx_http_client_header_timeout = 20000;
22 int ngx_http_client_header_buffer_size = 1024;
23
24 /* STUB: per location */
25 int ngx_http_lingering_timeout = 5000;
26 int ngx_http_lingering_time = 30;
27 /**/
28
29 int (*ngx_http_top_header_filter) (ngx_http_request_t *r);
30
31
32 static ngx_str_t http_name = ngx_string("http");
33
34
35 static ngx_command_t ngx_http_commands[] = {
36
37 {ngx_string("http"),
38 NGX_CONF_BLOCK|NGX_CONF_NOARGS,
39 ngx_http_block,
40 0,
41 0},
42
43 {ngx_string(""), 0, NULL, 0, 0}
44 };
45
46
47 ngx_module_t ngx_http_module = {
48 0, /* module index */
49 &http_name, /* module context */
50 ngx_http_commands, /* module directives */
51 NGX_CORE_MODULE_TYPE, /* module type */
52 NULL /* init module */
53 };
54
55
56 static char *ngx_http_block(ngx_conf_t *cf, ngx_command_t *cmd, char *dummy)
57 {
58 int i, s, l, p, a, n, start;
59 int port_found, addr_found, virtual_names;
60 char *rv;
61 struct sockaddr_in *addr_in;
62 ngx_array_t in_ports;
63 ngx_listen_t *ls;
64 ngx_http_module_t *module;
65 ngx_http_conf_ctx_t *ctx, *prev;
66 ngx_http_in_port_t *in_port;
67 ngx_http_in_addr_t *in_addr, *inaddr;
68 ngx_http_core_srv_conf_t **cscf;
69 ngx_http_listen_t *lscf;
70 ngx_http_server_name_t *s_name, *name;;
71
72 ngx_init_array(ngx_http_servers, cf->pool, 10,
73 sizeof(ngx_http_core_srv_conf_t *), NGX_CONF_ERROR);
74
75 ngx_test_null(ctx,
76 ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t)),
77 NGX_CONF_ERROR);
78
79 for (i = 0; ngx_modules[i]; i++) {
80 if (ngx_modules[i]->type != NGX_HTTP_MODULE_TYPE) {
81 continue;
82 }
83
84 /* STUB */
85 module = (ngx_http_module_t *) ngx_modules[i]->ctx;
86 module->index = ngx_http_max_module;
87
88 ngx_modules[i]->index = ngx_http_max_module++;
89 }
90
91 /* null loc_conf */
92 ngx_test_null(ctx->loc_conf,
93 ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module),
94 NGX_CONF_ERROR);
95
96 for (i = 0; ngx_modules[i]; i++) {
97 if (ngx_modules[i]->type != NGX_HTTP_MODULE_TYPE) {
98 continue;
99 }
100
101 module = (ngx_http_module_t *) ngx_modules[i]->ctx;
102
103 if (module->create_loc_conf) {
104 ngx_test_null(ctx->loc_conf[module->index],
105 module->create_loc_conf(cf->pool),
106 NGX_CONF_ERROR);
107 }
108 }
109
110 prev = cf->ctx;
111 cf->ctx = ctx;
112 cf->type = NGX_HTTP_MODULE_TYPE;
113
114 rv = ngx_conf_parse(cf, NULL);
115 cf->ctx = prev;
116
117 if (rv != NGX_CONF_OK)
118 return rv;
119
120 ngx_http_init_filters(cf->pool, ngx_modules);
121
122 #if 1
123 ngx_init_array(in_ports, cf->pool, 10, sizeof(ngx_http_in_port_t),
124 NGX_CONF_ERROR);
125
126 cscf = (ngx_http_core_srv_conf_t **) ngx_http_servers.elts;
127 for (s = 0; s < ngx_http_servers.nelts; s++) {
128
129 lscf = (ngx_http_listen_t *) cscf[s]->listen.elts;
130 for (l = 0; l < cscf[s]->listen.nelts; l++) {
131
132 port_found = 0;
133
134 /* AF_INET only */
135
136 in_port = (ngx_http_in_port_t *) in_ports.elts;
137 for (p = 0; p < in_ports.nelts; p++) {
138
139 if (lscf[l].port == in_port[p].port) {
140
141 port_found = 1;
142 addr_found = 0;
143
144 in_addr = (ngx_http_in_addr_t *) in_port[p].addr.elts;
145 for (a = 0; a < in_port[p].addr.nelts; a++) {
146
147 if (lscf[l].addr == in_addr[a].addr) {
148 s_name = (ngx_http_server_name_t *)
149 cscf[s]->server_names.elts;
150 for (n = 0; n < cscf[s]->server_names.nelts; n++) {
151 ngx_test_null(name,
152 ngx_push_array(&in_addr[a].names),
153 NGX_CONF_ERROR);
154
155 name->name = s_name[n].name;
156 name->core_srv_conf = s_name[n].core_srv_conf;
157 }
158
159 if (lscf[l].flags & NGX_HTTP_DEFAULT_SERVER) {
160 if (in_addr[a].flags
161 & NGX_HTTP_DEFAULT_SERVER) {
162
163 ngx_log_error(NGX_LOG_ERR, cf->log, 0,
164 "duplicate default server in %s:%d",
165 lscf[l].conf_file->file.name.data,
166 lscf[l].line);
167
168 return NGX_CONF_ERROR;
169 }
170
171 in_addr[a].flags |= NGX_HTTP_DEFAULT_SERVER;
172 in_addr[a].core_srv_conf = cscf[s];
173 }
174
175 addr_found = 1;
176
177 break;
178
179 /* "*:XX" is the last resort */
180 } else if (in_addr[p].addr == INADDR_ANY) {
181 ngx_test_null(inaddr,
182 ngx_push_array(&in_port[p].addr),
183 NGX_CONF_ERROR);
184
185 ngx_memcpy(inaddr, &in_addr[a],
186 sizeof(ngx_http_in_addr_t));
187
188 inaddr->addr = lscf[l].addr;
189 inaddr->flags = lscf[l].flags;
190 inaddr->core_srv_conf = cscf[s];
191
192 ngx_init_array(inaddr->names, cf->pool, 10,
193 sizeof(ngx_http_server_name_t),
194 NGX_CONF_ERROR);
195
196 addr_found = 1;
197
198 break;
199 }
200 }
201
202 if (!addr_found) {
203 ngx_test_null(inaddr,
204 ngx_push_array(&in_port[p].addr),
205 NGX_CONF_ERROR);
206
207 inaddr->addr = lscf[l].addr;
208 inaddr->flags = lscf[l].flags;
209 inaddr->core_srv_conf = cscf[s];
210
211 ngx_init_array(inaddr->names, cf->pool, 10,
212 sizeof(ngx_http_server_name_t),
213 NGX_CONF_ERROR);
214 }
215 }
216 }
217
218 if (!port_found) {
219 ngx_test_null(in_port,
220 ngx_push_array(&in_ports),
221 NGX_CONF_ERROR);
222
223 in_port->port = lscf[l].port;
224
225 ngx_init_array(in_port->addr, cf->pool, 10,
226 sizeof(ngx_http_in_addr_t),
227 NGX_CONF_ERROR);
228
229 ngx_test_null(inaddr, ngx_push_array(&in_port[p].addr),
230 NGX_CONF_ERROR);
231
232 inaddr->addr = lscf[l].addr;
233 inaddr->flags = lscf[l].flags;
234 inaddr->core_srv_conf = cscf[s];
235
236 ngx_init_array(inaddr->names, cf->pool, 10,
237 sizeof(ngx_http_server_name_t),
238 NGX_CONF_ERROR);
239 }
240 }
241 }
242
243 /* AF_INET only */
244
245 in_port = (ngx_http_in_port_t *) in_ports.elts;
246 for (p = 0; p < in_ports.nelts; p++) {
247
248 in_addr = (ngx_http_in_addr_t *) in_port[p].addr.elts;
249 for (a = 0; a < in_port[p].addr.nelts; a++) {
250
251 virtual_names = 0;
252
253 name = (ngx_http_server_name_t *) in_addr[a].names.elts;
254 for (n = 0; n < in_addr[a].names.nelts; n++) {
255 if (in_addr[a].core_srv_conf != name[n].core_srv_conf) {
256 virtual_names = 1;
257 break;
258 }
259 }
260
261 /* if all server names point to the same server
262 then we do not need to check them at run time */
263 if (!virtual_names) {
264 in_addr[a].names.nelts = 0;
265 }
266 }
267
268 /* if there is binding to "*:XX" then we need to bind to "*:XX" only
269 and ignore other binding */
270 if (in_addr[a - 1].addr == INADDR_ANY) {
271 start = a - 1;
272
273 } else {
274 start = 0;
275 }
276
277 in_addr = (ngx_http_in_addr_t *) in_port[p].addr.elts;
278 for (a = start; a < in_port[p].addr.nelts; a++) {
279
280 ngx_test_null(ls, ngx_push_array(&ngx_listening_sockets),
281 NGX_CONF_ERROR);
282 ngx_memzero(ls, sizeof(ngx_listen_t));
283
284 ngx_test_null(addr_in,
285 ngx_pcalloc(cf->pool, sizeof(struct sockaddr_in)),
286 NGX_CONF_ERROR);
287
288 addr_in->sin_family = AF_INET;
289 addr_in->sin_addr.s_addr = in_addr[a].addr;
290 addr_in->sin_port = htons(in_port[p].port);
291
292 ngx_test_null(ls->addr_text.data,
293 ngx_palloc(cf->pool, INET_ADDRSTRLEN + 6),
294 NGX_CONF_ERROR);
295
296 ls->addr_text.len =
297 ngx_snprintf(ls->addr_text.data
298 + ngx_inet_ntop(AF_INET,
299 &in_addr[a].addr,
300 ls->addr_text.data,
301 INET_ADDRSTRLEN),
302 6, ":%d", in_port[p].port);
303
304 ls->family = AF_INET;
305 ls->type = SOCK_STREAM;
306 ls->protocol = IPPROTO_IP;
307 #if (NGX_OVERLAPPED)
308 ls->flags = WSA_FLAG_OVERLAPPED;
309 #endif
310 ls->sockaddr = (struct sockaddr *) addr_in;
311 ls->socklen = sizeof(struct sockaddr_in);
312 ls->addr = offsetof(struct sockaddr_in, sin_addr);
313 ls->addr_text_max_len = INET_ADDRSTRLEN;
314 ls->backlog = -1;
315 ls->post_accept_timeout = ngx_http_post_accept_timeout;
316 ls->nonblocking = 1;
317
318 ls->handler = ngx_http_init_connection;
319 ls->log = cf->log;
320 ls->ctx = ctx;
321 ls->servers = &in_port[p].addr;
322
323 if (in_port[p].addr.nelts == 1) {
324 in_addr = (ngx_http_in_addr_t *) in_port[p].addr.elts;
325
326 if (in_addr[a].names.nelts == 0) {
327 ls->ctx = in_addr->core_srv_conf->ctx;
328 ls->servers = NULL;
329 }
330 }
331 }
332 }
333 #endif
334
335 return NGX_CONF_OK;
336 }
337
338
339 #if 0
11 /* STUB */ 340 /* STUB */
12 341
13 static struct sockaddr_in addr; 342 static struct sockaddr_in addr;
14 static char addr_text[22]; 343 static char addr_text[22];
15 344
16 static ngx_http_server_t ngx_http_server;
17 345
18 int ngx_http_init(ngx_pool_t *pool, ngx_log_t *log) 346 int ngx_http_init(ngx_pool_t *pool, ngx_log_t *log)
19 { 347 {
20 ngx_listen_t *ls; 348 ngx_listen_t *ls;
21 349
38 #endif 366 #endif
39 ngx_http_server.doc_root_len = strlen(ngx_http_server.doc_root) + 1; 367 ngx_http_server.doc_root_len = strlen(ngx_http_server.doc_root) + 1;
40 368
41 369
42 ngx_http_config_modules(pool, ngx_modules); 370 ngx_http_config_modules(pool, ngx_modules);
371
43 #if 0 372 #if 0
44
45 /* STUB */ 373 /* STUB */
46 ngx_http_output_filter_set_stub(pool, ngx_http_modules); 374 ngx_http_output_filter_set_stub(pool, ngx_http_modules);
47 ngx_http_write_filter_set_stub(pool, ngx_http_modules); 375 ngx_http_write_filter_set_stub(pool, ngx_http_modules);
48 ngx_http_index_set_stub(pool, ngx_http_modules); 376 ngx_http_index_set_stub(pool, ngx_http_modules);
49 377
50 ngx_http_init_modules(pool, ngx_http_modules); 378 ngx_http_init_modules(pool, ngx_http_modules);
51 #endif 379 #endif
380
52 ngx_http_init_filters(pool, ngx_modules); 381 ngx_http_init_filters(pool, ngx_modules);
53 382
54 ls = ngx_push_array(ngx_listening_sockets); 383 ls = ngx_push_array(&ngx_listening_sockets);
55 ngx_memzero(ls, sizeof(ngx_listen_t)); 384 ngx_memzero(ls, sizeof(ngx_listen_t));
56 385
57 addr.sin_family = AF_INET; 386 addr.sin_family = AF_INET;
58 addr.sin_addr.s_addr = inet_addr("0.0.0.0"); 387 addr.sin_addr.s_addr = inet_addr("0.0.0.0");
59 addr.sin_port = htons(8000); 388 addr.sin_port = htons(8000);
82 411
83 412
84 return 1; 413 return 1;
85 } 414 }
86 415
87 /* */ 416 /**/
417 #endif