0
|
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_http.h>
|
|
11 #include <nginx.h>
|
|
12
|
10
|
13
|
0
|
14 #define NGX_HTTP_LOCATION_EXACT 1
|
|
15 #define NGX_HTTP_LOCATION_AUTO_REDIRECT 2
|
16
|
16 #define NGX_HTTP_LOCATION_NOREGEX 3
|
|
17 #define NGX_HTTP_LOCATION_REGEX 4
|
0
|
18
|
|
19
|
34
|
20 static void ngx_http_core_phase_event_handler(ngx_event_t *rev);
|
|
21 static void ngx_http_core_run_phases(ngx_http_request_t *r);
|
|
22 static ngx_int_t ngx_http_core_find_location(ngx_http_request_t *r,
|
36
|
23 ngx_array_t *locations, size_t len);
|
0
|
24
|
|
25 static void *ngx_http_core_create_main_conf(ngx_conf_t *cf);
|
|
26 static char *ngx_http_core_init_main_conf(ngx_conf_t *cf, void *conf);
|
|
27 static void *ngx_http_core_create_srv_conf(ngx_conf_t *cf);
|
|
28 static char *ngx_http_core_merge_srv_conf(ngx_conf_t *cf,
|
36
|
29 void *parent, void *child);
|
0
|
30 static void *ngx_http_core_create_loc_conf(ngx_conf_t *cf);
|
|
31 static char *ngx_http_core_merge_loc_conf(ngx_conf_t *cf,
|
36
|
32 void *parent, void *child);
|
0
|
33
|
34
|
34 static char *ngx_http_core_server(ngx_conf_t *cf, ngx_command_t *cmd,
|
36
|
35 void *dummy);
|
34
|
36 static char *ngx_http_core_location(ngx_conf_t *cf, ngx_command_t *cmd,
|
36
|
37 void *dummy);
|
52
|
38 static int ngx_libc_cdecl ngx_http_core_cmp_locations(const void *first,
|
|
39 const void *second);
|
34
|
40
|
|
41 static char *ngx_http_core_types(ngx_conf_t *cf, ngx_command_t *cmd,
|
36
|
42 void *conf);
|
34
|
43 static char *ngx_http_core_type(ngx_conf_t *cf, ngx_command_t *dummy,
|
36
|
44 void *conf);
|
0
|
45
|
34
|
46 static char *ngx_http_core_listen(ngx_conf_t *cf, ngx_command_t *cmd,
|
36
|
47 void *conf);
|
34
|
48 static char *ngx_http_core_server_name(ngx_conf_t *cf, ngx_command_t *cmd,
|
36
|
49 void *conf);
|
34
|
50 static char *ngx_http_core_root(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
|
|
51 static char *ngx_http_core_error_page(ngx_conf_t *cf, ngx_command_t *cmd,
|
36
|
52 void *conf);
|
34
|
53 static char *ngx_http_core_error_log(ngx_conf_t *cf, ngx_command_t *cmd,
|
36
|
54 void *conf);
|
34
|
55 static char *ngx_http_core_keepalive(ngx_conf_t *cf, ngx_command_t *cmd,
|
36
|
56 void *conf);
|
0
|
57
|
34
|
58 static char *ngx_http_core_lowat_check(ngx_conf_t *cf, void *post, void *data);
|
48
|
59 static ngx_int_t ngx_http_core_init(ngx_cycle_t *cycle);
|
34
|
60
|
|
61 static ngx_conf_post_t ngx_http_core_lowat_post =
|
|
62 { ngx_http_core_lowat_check };
|
0
|
63
|
|
64
|
|
65 static ngx_conf_enum_t ngx_http_restrict_host_names[] = {
|
|
66 { ngx_string("off"), NGX_HTTP_RESTRICT_HOST_OFF },
|
|
67 { ngx_string("on"), NGX_HTTP_RESTRICT_HOST_ON },
|
|
68 { ngx_string("close"), NGX_HTTP_RESTRICT_HOST_CLOSE },
|
|
69 { ngx_null_string, 0 }
|
|
70 };
|
|
71
|
|
72
|
|
73 static ngx_command_t ngx_http_core_commands[] = {
|
|
74
|
26
|
75 { ngx_string("server_names_hash"),
|
|
76 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
|
|
77 ngx_conf_set_num_slot,
|
|
78 NGX_HTTP_MAIN_CONF_OFFSET,
|
|
79 offsetof(ngx_http_core_main_conf_t, server_names_hash),
|
|
80 NULL },
|
|
81
|
|
82 { ngx_string("server_names_hash_threshold"),
|
|
83 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
|
|
84 ngx_conf_set_num_slot,
|
|
85 NGX_HTTP_MAIN_CONF_OFFSET,
|
|
86 offsetof(ngx_http_core_main_conf_t, server_names_hash_threshold),
|
|
87 NULL },
|
|
88
|
0
|
89 { ngx_string("server"),
|
|
90 NGX_HTTP_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
|
34
|
91 ngx_http_core_server,
|
0
|
92 0,
|
|
93 0,
|
|
94 NULL },
|
|
95
|
|
96 { ngx_string("connection_pool_size"),
|
|
97 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
|
|
98 ngx_conf_set_size_slot,
|
|
99 NGX_HTTP_SRV_CONF_OFFSET,
|
|
100 offsetof(ngx_http_core_srv_conf_t, connection_pool_size),
|
|
101 NULL },
|
|
102
|
|
103 { ngx_string("post_accept_timeout"),
|
|
104 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
|
|
105 ngx_conf_set_msec_slot,
|
|
106 NGX_HTTP_SRV_CONF_OFFSET,
|
|
107 offsetof(ngx_http_core_srv_conf_t, post_accept_timeout),
|
|
108 NULL },
|
|
109
|
|
110 { ngx_string("request_pool_size"),
|
|
111 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
|
|
112 ngx_conf_set_size_slot,
|
|
113 NGX_HTTP_SRV_CONF_OFFSET,
|
|
114 offsetof(ngx_http_core_srv_conf_t, request_pool_size),
|
|
115 NULL },
|
|
116
|
|
117 { ngx_string("client_header_timeout"),
|
|
118 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
|
|
119 ngx_conf_set_msec_slot,
|
|
120 NGX_HTTP_SRV_CONF_OFFSET,
|
|
121 offsetof(ngx_http_core_srv_conf_t, client_header_timeout),
|
|
122 NULL },
|
|
123
|
|
124 { ngx_string("client_header_buffer_size"),
|
|
125 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
|
|
126 ngx_conf_set_size_slot,
|
|
127 NGX_HTTP_SRV_CONF_OFFSET,
|
|
128 offsetof(ngx_http_core_srv_conf_t, client_header_buffer_size),
|
|
129 NULL },
|
|
130
|
|
131 { ngx_string("large_client_header_buffers"),
|
|
132 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE2,
|
|
133 ngx_conf_set_bufs_slot,
|
|
134 NGX_HTTP_SRV_CONF_OFFSET,
|
|
135 offsetof(ngx_http_core_srv_conf_t, large_client_header_buffers),
|
|
136 NULL },
|
|
137
|
|
138 { ngx_string("restrict_host_names"),
|
|
139 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
|
|
140 ngx_conf_set_enum_slot,
|
|
141 NGX_HTTP_SRV_CONF_OFFSET,
|
|
142 offsetof(ngx_http_core_srv_conf_t, restrict_host_names),
|
|
143 &ngx_http_restrict_host_names },
|
|
144
|
|
145 { ngx_string("location"),
|
|
146 NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE12,
|
34
|
147 ngx_http_core_location,
|
0
|
148 NGX_HTTP_SRV_CONF_OFFSET,
|
|
149 0,
|
|
150 NULL },
|
|
151
|
|
152 { ngx_string("listen"),
|
|
153 #if 0
|
|
154 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
|
|
155 #else
|
4
|
156 NGX_HTTP_SRV_CONF|NGX_CONF_TAKE12,
|
0
|
157 #endif
|
34
|
158 ngx_http_core_listen,
|
0
|
159 NGX_HTTP_SRV_CONF_OFFSET,
|
|
160 0,
|
|
161 NULL },
|
|
162
|
|
163 { ngx_string("server_name"),
|
|
164 NGX_HTTP_SRV_CONF|NGX_CONF_1MORE,
|
34
|
165 ngx_http_core_server_name,
|
0
|
166 NGX_HTTP_SRV_CONF_OFFSET,
|
|
167 0,
|
|
168 NULL },
|
|
169
|
|
170 { ngx_string("types"),
|
|
171 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF
|
|
172 |NGX_CONF_BLOCK|NGX_CONF_NOARGS,
|
34
|
173 ngx_http_core_types,
|
0
|
174 NGX_HTTP_LOC_CONF_OFFSET,
|
|
175 0,
|
|
176 NULL },
|
|
177
|
|
178 { ngx_string("default_type"),
|
|
179 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
180 ngx_conf_set_str_slot,
|
|
181 NGX_HTTP_LOC_CONF_OFFSET,
|
|
182 offsetof(ngx_http_core_loc_conf_t, default_type),
|
|
183 NULL },
|
|
184
|
|
185 { ngx_string("root"),
|
|
186 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
34
|
187 ngx_http_core_root,
|
0
|
188 NGX_HTTP_LOC_CONF_OFFSET,
|
|
189 0,
|
|
190 NULL },
|
|
191
|
|
192 { ngx_string("alias"),
|
|
193 NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
34
|
194 ngx_http_core_root,
|
0
|
195 NGX_HTTP_LOC_CONF_OFFSET,
|
|
196 0,
|
|
197 NULL },
|
|
198
|
|
199 { ngx_string("client_max_body_size"),
|
|
200 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
201 ngx_conf_set_size_slot,
|
|
202 NGX_HTTP_LOC_CONF_OFFSET,
|
|
203 offsetof(ngx_http_core_loc_conf_t, client_max_body_size),
|
|
204 NULL },
|
|
205
|
|
206 { ngx_string("client_body_buffer_size"),
|
|
207 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
208 ngx_conf_set_size_slot,
|
|
209 NGX_HTTP_LOC_CONF_OFFSET,
|
|
210 offsetof(ngx_http_core_loc_conf_t, client_body_buffer_size),
|
|
211 NULL },
|
|
212
|
|
213 { ngx_string("client_body_timeout"),
|
|
214 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
215 ngx_conf_set_msec_slot,
|
|
216 NGX_HTTP_LOC_CONF_OFFSET,
|
|
217 offsetof(ngx_http_core_loc_conf_t, client_body_timeout),
|
|
218 NULL },
|
|
219
|
28
|
220 { ngx_string("client_body_temp_path"),
|
|
221 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1234,
|
|
222 ngx_conf_set_path_slot,
|
|
223 NGX_HTTP_LOC_CONF_OFFSET,
|
|
224 offsetof(ngx_http_core_loc_conf_t, client_body_temp_path),
|
|
225 (void *) ngx_garbage_collector_temp_handler },
|
|
226
|
0
|
227 { ngx_string("sendfile"),
|
|
228 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
229 ngx_conf_set_flag_slot,
|
|
230 NGX_HTTP_LOC_CONF_OFFSET,
|
|
231 offsetof(ngx_http_core_loc_conf_t, sendfile),
|
|
232 NULL },
|
|
233
|
|
234 { ngx_string("tcp_nopush"),
|
|
235 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
236 ngx_conf_set_flag_slot,
|
|
237 NGX_HTTP_LOC_CONF_OFFSET,
|
|
238 offsetof(ngx_http_core_loc_conf_t, tcp_nopush),
|
|
239 NULL },
|
|
240
|
2
|
241 { ngx_string("tcp_nodelay"),
|
|
242 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
243 ngx_conf_set_flag_slot,
|
|
244 NGX_HTTP_LOC_CONF_OFFSET,
|
|
245 offsetof(ngx_http_core_loc_conf_t, tcp_nodelay),
|
|
246 NULL },
|
|
247
|
0
|
248 { ngx_string("send_timeout"),
|
|
249 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
250 ngx_conf_set_msec_slot,
|
|
251 NGX_HTTP_LOC_CONF_OFFSET,
|
|
252 offsetof(ngx_http_core_loc_conf_t, send_timeout),
|
|
253 NULL },
|
|
254
|
|
255 { ngx_string("send_lowat"),
|
|
256 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
257 ngx_conf_set_size_slot,
|
|
258 NGX_HTTP_LOC_CONF_OFFSET,
|
|
259 offsetof(ngx_http_core_loc_conf_t, send_lowat),
|
34
|
260 &ngx_http_core_lowat_post },
|
0
|
261
|
|
262 { ngx_string("postpone_output"),
|
|
263 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
264 ngx_conf_set_size_slot,
|
|
265 NGX_HTTP_LOC_CONF_OFFSET,
|
|
266 offsetof(ngx_http_core_loc_conf_t, postpone_output),
|
|
267 NULL },
|
|
268
|
|
269 { ngx_string("limit_rate"),
|
34
|
270 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
|
|
271 |NGX_CONF_TAKE1,
|
0
|
272 ngx_conf_set_size_slot,
|
|
273 NGX_HTTP_LOC_CONF_OFFSET,
|
|
274 offsetof(ngx_http_core_loc_conf_t, limit_rate),
|
|
275 NULL },
|
|
276
|
|
277 { ngx_string("keepalive_timeout"),
|
|
278 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
|
34
|
279 ngx_http_core_keepalive,
|
0
|
280 NGX_HTTP_LOC_CONF_OFFSET,
|
|
281 0,
|
|
282 NULL },
|
|
283
|
|
284 { ngx_string("lingering_time"),
|
|
285 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
286 ngx_conf_set_msec_slot,
|
|
287 NGX_HTTP_LOC_CONF_OFFSET,
|
|
288 offsetof(ngx_http_core_loc_conf_t, lingering_time),
|
|
289 NULL },
|
|
290
|
|
291 { ngx_string("lingering_timeout"),
|
|
292 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
293 ngx_conf_set_msec_slot,
|
|
294 NGX_HTTP_LOC_CONF_OFFSET,
|
|
295 offsetof(ngx_http_core_loc_conf_t, lingering_timeout),
|
|
296 NULL },
|
|
297
|
|
298 { ngx_string("reset_timedout_connection"),
|
|
299 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
300 ngx_conf_set_flag_slot,
|
|
301 NGX_HTTP_LOC_CONF_OFFSET,
|
|
302 offsetof(ngx_http_core_loc_conf_t, reset_timedout_connection),
|
|
303 NULL },
|
|
304
|
|
305 { ngx_string("msie_padding"),
|
|
306 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
307 ngx_conf_set_flag_slot,
|
|
308 NGX_HTTP_LOC_CONF_OFFSET,
|
|
309 offsetof(ngx_http_core_loc_conf_t, msie_padding),
|
|
310 NULL },
|
|
311
|
|
312 { ngx_string("error_page"),
|
|
313 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_2MORE,
|
34
|
314 ngx_http_core_error_page,
|
0
|
315 NGX_HTTP_LOC_CONF_OFFSET,
|
|
316 0,
|
|
317 NULL },
|
|
318
|
|
319 { ngx_string("error_log"),
|
|
320 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
|
34
|
321 ngx_http_core_error_log,
|
0
|
322 NGX_HTTP_LOC_CONF_OFFSET,
|
|
323 0,
|
|
324 NULL },
|
|
325
|
|
326 #if (NGX_HTTP_CACHE)
|
|
327
|
|
328 { ngx_string("open_file_cache"),
|
|
329 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE4,
|
|
330 ngx_http_set_cache_slot,
|
|
331 NGX_HTTP_LOC_CONF_OFFSET,
|
|
332 offsetof(ngx_http_core_loc_conf_t, open_files),
|
|
333 NULL },
|
|
334
|
|
335 #endif
|
|
336
|
|
337 ngx_null_command
|
|
338 };
|
|
339
|
|
340
|
|
341 ngx_http_module_t ngx_http_core_module_ctx = {
|
|
342 NULL, /* pre conf */
|
|
343
|
|
344 ngx_http_core_create_main_conf, /* create main configuration */
|
|
345 ngx_http_core_init_main_conf, /* init main configuration */
|
|
346
|
|
347 ngx_http_core_create_srv_conf, /* create server configuration */
|
|
348 ngx_http_core_merge_srv_conf, /* merge server configuration */
|
|
349
|
|
350 ngx_http_core_create_loc_conf, /* create location configuration */
|
|
351 ngx_http_core_merge_loc_conf /* merge location configuration */
|
|
352 };
|
|
353
|
|
354
|
|
355 ngx_module_t ngx_http_core_module = {
|
|
356 NGX_MODULE,
|
|
357 &ngx_http_core_module_ctx, /* module context */
|
|
358 ngx_http_core_commands, /* module directives */
|
|
359 NGX_HTTP_MODULE, /* module type */
|
48
|
360 ngx_http_core_init, /* init module */
|
0
|
361 NULL /* init process */
|
|
362 };
|
|
363
|
|
364
|
36
|
365 void
|
|
366 ngx_http_handler(ngx_http_request_t *r)
|
0
|
367 {
|
32
|
368 r->connection->log->action = NULL;
|
0
|
369
|
|
370 r->connection->unexpected_eof = 0;
|
|
371
|
|
372 switch (r->headers_in.connection_type) {
|
|
373 case 0:
|
|
374 if (r->http_version > NGX_HTTP_VERSION_10) {
|
|
375 r->keepalive = 1;
|
|
376 } else {
|
|
377 r->keepalive = 0;
|
|
378 }
|
|
379 break;
|
|
380
|
|
381 case NGX_HTTP_CONNECTION_CLOSE:
|
|
382 r->keepalive = 0;
|
|
383 break;
|
|
384
|
|
385 case NGX_HTTP_CONNECTION_KEEP_ALIVE:
|
|
386 r->keepalive = 1;
|
|
387 break;
|
|
388 }
|
|
389
|
|
390 if (r->keepalive && r->headers_in.msie && r->method == NGX_HTTP_POST) {
|
|
391
|
|
392 /*
|
|
393 * MSIE may wait for some time if the response for the POST request
|
|
394 * is sent over the keepalive connection
|
|
395 */
|
|
396
|
|
397 r->keepalive = 0;
|
|
398 }
|
|
399
|
|
400 #if 0
|
|
401 /* TEST STUB */ r->http_version = NGX_HTTP_VERSION_10;
|
|
402 /* TEST STUB */ r->keepalive = 0;
|
|
403 #endif
|
|
404
|
|
405 if (r->headers_in.content_length_n > 0) {
|
|
406 r->lingering_close = 1;
|
|
407
|
|
408 } else {
|
|
409 r->lingering_close = 0;
|
|
410 }
|
|
411
|
|
412 #if 0
|
|
413 /* TEST STUB */ r->lingering_close = 1;
|
|
414 #endif
|
|
415
|
34
|
416 r->connection->write->event_handler = ngx_http_core_phase_event_handler;
|
0
|
417
|
54
|
418 r->valid_unparsed_uri = 1;
|
34
|
419 r->uri_changed = 1;
|
|
420 r->uri_changes = 11;
|
54
|
421
|
38
|
422 r->phase = NGX_HTTP_REWRITE_PHASE;
|
|
423 r->phase_handler = 0;
|
34
|
424
|
|
425 ngx_http_core_run_phases(r);
|
0
|
426 }
|
|
427
|
|
428
|
36
|
429 static void
|
|
430 ngx_http_core_phase_event_handler(ngx_event_t *ev)
|
0
|
431 {
|
|
432 ngx_connection_t *c;
|
|
433 ngx_http_request_t *r;
|
|
434
|
|
435 c = ev->data;
|
|
436 r = c->data;
|
|
437
|
|
438 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ev->log, 0, "phase event handler");
|
|
439
|
34
|
440 ngx_http_core_run_phases(r);
|
0
|
441 }
|
|
442
|
|
443
|
36
|
444 static void
|
|
445 ngx_http_core_run_phases(ngx_http_request_t *r)
|
0
|
446 {
|
|
447 ngx_int_t rc;
|
|
448 ngx_http_handler_pt *h;
|
|
449 ngx_http_core_loc_conf_t *clcf;
|
|
450 ngx_http_core_main_conf_t *cmcf;
|
|
451
|
|
452 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
|
|
453
|
|
454 for (/* void */; r->phase < NGX_HTTP_LAST_PHASE; r->phase++) {
|
|
455
|
38
|
456 if (r->phase == NGX_HTTP_REWRITE_PHASE + 1 && r->uri_changed) {
|
34
|
457
|
|
458 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
459 "uri changes: %d", r->uri_changes);
|
|
460
|
|
461 /*
|
|
462 * gcc before 3.3 compiles the broken code for
|
|
463 * if (r->uri_changes-- == 0)
|
|
464 * if the r->uri_changes is defined as
|
|
465 * unsigned uri_changes:4
|
|
466 */
|
|
467
|
|
468 r->uri_changes--;
|
|
469
|
|
470 if (r->uri_changes == 0) {
|
|
471 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
472 "rewrite cycle");
|
|
473 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
474 return;
|
|
475 }
|
|
476
|
38
|
477 r->phase = NGX_HTTP_FIND_CONFIG_PHASE;
|
34
|
478 }
|
|
479
|
0
|
480 if (r->phase == NGX_HTTP_CONTENT_PHASE && r->content_handler) {
|
|
481 r->connection->write->event_handler = ngx_http_empty_handler;
|
34
|
482 ngx_http_finalize_request(r, r->content_handler(r));
|
0
|
483 return;
|
|
484 }
|
|
485
|
|
486 h = cmcf->phases[r->phase].handlers.elts;
|
|
487 for (r->phase_handler = cmcf->phases[r->phase].handlers.nelts - 1;
|
|
488 r->phase_handler >= 0;
|
|
489 r->phase_handler--)
|
|
490 {
|
|
491 rc = h[r->phase_handler](r);
|
|
492
|
|
493 if (rc == NGX_DONE) {
|
|
494
|
|
495 /*
|
|
496 * we should never use r here because
|
|
497 * it could point to already freed data
|
|
498 */
|
|
499
|
|
500 return;
|
|
501 }
|
|
502
|
|
503 if (rc == NGX_DECLINED) {
|
|
504 continue;
|
|
505 }
|
|
506
|
|
507 if (rc >= NGX_HTTP_SPECIAL_RESPONSE || rc == NGX_ERROR) {
|
|
508 ngx_http_finalize_request(r, rc);
|
|
509 return;
|
|
510 }
|
|
511
|
|
512 if (r->phase == NGX_HTTP_CONTENT_PHASE) {
|
|
513 ngx_http_finalize_request(r, rc);
|
|
514 return;
|
|
515 }
|
|
516
|
|
517 if (rc == NGX_AGAIN) {
|
|
518 return;
|
|
519 }
|
|
520
|
|
521 if (rc == NGX_OK && cmcf->phases[r->phase].type == NGX_OK) {
|
|
522 break;
|
|
523 }
|
|
524 }
|
|
525 }
|
|
526
|
34
|
527 /* no content handler was found */
|
0
|
528
|
38
|
529 if (r->uri.data[r->uri.len - 1] == '/' && !r->zero_in_uri) {
|
0
|
530
|
|
531 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
532
|
|
533 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
34
|
534 "directory index of \"%V%V\" is forbidden",
|
|
535 &clcf->root, &r->uri);
|
0
|
536
|
|
537 ngx_http_finalize_request(r, NGX_HTTP_FORBIDDEN);
|
|
538 return;
|
|
539 }
|
|
540
|
|
541 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "no handler found");
|
|
542
|
|
543 ngx_http_finalize_request(r, NGX_HTTP_NOT_FOUND);
|
|
544 }
|
|
545
|
|
546
|
36
|
547 ngx_int_t
|
|
548 ngx_http_find_location_config(ngx_http_request_t *r)
|
0
|
549 {
|
|
550 ngx_int_t rc;
|
|
551 ngx_http_core_loc_conf_t *clcf;
|
|
552 ngx_http_core_srv_conf_t *cscf;
|
|
553
|
38
|
554 r->content_handler = NULL;
|
|
555 r->uri_changed = 0;
|
|
556
|
0
|
557 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
|
|
558
|
34
|
559 rc = ngx_http_core_find_location(r, &cscf->locations, 0);
|
0
|
560
|
|
561 if (rc == NGX_HTTP_INTERNAL_SERVER_ERROR) {
|
|
562 return rc;
|
|
563 }
|
|
564
|
|
565 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
566
|
|
567 r->connection->log->file = clcf->err_log->file;
|
|
568 if (!(r->connection->log->log_level & NGX_LOG_DEBUG_CONNECTION)) {
|
|
569 r->connection->log->log_level = clcf->err_log->log_level;
|
|
570 }
|
|
571
|
2
|
572 if ((ngx_io.flags & NGX_IO_SENDFILE) && clcf->sendfile) {
|
|
573 r->connection->sendfile = 1;
|
0
|
574
|
|
575 } else {
|
2
|
576 r->connection->sendfile = 0;
|
0
|
577 }
|
|
578
|
32
|
579 if (r->keepalive && clcf->keepalive_timeout == 0) {
|
|
580 r->keepalive = 0;
|
|
581 }
|
|
582
|
0
|
583 if (!clcf->tcp_nopush) {
|
|
584 /* disable TCP_NOPUSH/TCP_CORK use */
|
|
585 r->connection->tcp_nopush = NGX_TCP_NOPUSH_DISABLED;
|
|
586 }
|
|
587
|
|
588
|
|
589 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
10
|
590 "http cl:%z max:%uz",
|
|
591 r->headers_in.content_length_n, clcf->client_max_body_size);
|
0
|
592
|
|
593 if (r->headers_in.content_length_n != -1
|
|
594 && clcf->client_max_body_size
|
|
595 && clcf->client_max_body_size < (size_t) r->headers_in.content_length_n)
|
|
596 {
|
|
597 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
10
|
598 "client intented to send too large body: %z bytes",
|
0
|
599 r->headers_in.content_length_n);
|
|
600
|
|
601 return NGX_HTTP_REQUEST_ENTITY_TOO_LARGE;
|
|
602 }
|
|
603
|
|
604
|
|
605 if (rc == NGX_HTTP_LOCATION_AUTO_REDIRECT) {
|
|
606 r->headers_out.location = ngx_list_push(&r->headers_out.headers);
|
|
607 if (r->headers_out.location == NULL) {
|
|
608 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
609 }
|
|
610
|
|
611 r->headers_out.location->value = clcf->name;
|
|
612
|
|
613 return NGX_HTTP_MOVED_PERMANENTLY;
|
|
614 }
|
|
615
|
|
616 if (clcf->handler) {
|
|
617 r->content_handler = clcf->handler;
|
|
618 }
|
|
619
|
|
620 return NGX_OK;
|
|
621 }
|
|
622
|
|
623
|
36
|
624 static ngx_int_t
|
|
625 ngx_http_core_find_location(ngx_http_request_t *r,
|
|
626 ngx_array_t *locations, size_t len)
|
0
|
627 {
|
|
628 ngx_int_t n, rc;
|
16
|
629 ngx_uint_t i, found, noregex;
|
0
|
630 ngx_http_core_loc_conf_t *clcf, **clcfp;
|
|
631
|
|
632 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "find location");
|
|
633
|
|
634 found = 0;
|
16
|
635 noregex = 0;
|
0
|
636
|
|
637 clcfp = locations->elts;
|
|
638 for (i = 0; i < locations->nelts; i++) {
|
|
639
|
4
|
640 #if (NGX_PCRE)
|
0
|
641 if (clcfp[i]->regex) {
|
|
642 break;
|
|
643 }
|
|
644 #endif
|
|
645
|
34
|
646 if (clcfp[i]->noname) {
|
|
647 break;
|
|
648 }
|
|
649
|
0
|
650 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
10
|
651 "find location: %s\"%V\"",
|
|
652 clcfp[i]->exact_match ? "= " : "", &clcfp[i]->name);
|
0
|
653
|
|
654 if (clcfp[i]->auto_redirect
|
|
655 && r->uri.len == clcfp[i]->name.len - 1
|
|
656 && ngx_strncmp(r->uri.data, clcfp[i]->name.data,
|
|
657 clcfp[i]->name.len - 1) == 0)
|
|
658 {
|
|
659 /* the locations are lexicographically sorted */
|
|
660
|
|
661 r->loc_conf = clcfp[i]->loc_conf;
|
|
662
|
|
663 return NGX_HTTP_LOCATION_AUTO_REDIRECT;
|
|
664 }
|
|
665
|
|
666 if (r->uri.len < clcfp[i]->name.len) {
|
|
667 continue;
|
|
668 }
|
|
669
|
|
670 n = ngx_strncmp(r->uri.data, clcfp[i]->name.data, clcfp[i]->name.len);
|
|
671
|
|
672 if (n < 0) {
|
|
673 /* the locations are lexicographically sorted */
|
|
674 break;
|
|
675 }
|
|
676
|
|
677 if (n == 0) {
|
34
|
678 if (clcfp[i]->exact_match) {
|
|
679
|
|
680 if (r->uri.len == clcfp[i]->name.len) {
|
|
681 r->loc_conf = clcfp[i]->loc_conf;
|
|
682 return NGX_HTTP_LOCATION_EXACT;
|
|
683 }
|
|
684
|
|
685 continue;
|
0
|
686 }
|
|
687
|
|
688 if (len > clcfp[i]->name.len) {
|
|
689 /* the previous match is longer */
|
|
690 break;
|
|
691 }
|
|
692
|
|
693 r->loc_conf = clcfp[i]->loc_conf;
|
16
|
694 noregex = clcfp[i]->noregex;
|
0
|
695 found = 1;
|
|
696 }
|
|
697 }
|
|
698
|
|
699 if (found) {
|
|
700 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
701
|
|
702 if (clcf->locations.nelts) {
|
34
|
703 rc = ngx_http_core_find_location(r, &clcf->locations, len);
|
0
|
704
|
|
705 if (rc != NGX_OK) {
|
|
706 return rc;
|
|
707 }
|
|
708 }
|
|
709 }
|
|
710
|
4
|
711 #if (NGX_PCRE)
|
0
|
712
|
16
|
713 if (noregex) {
|
|
714 return NGX_HTTP_LOCATION_NOREGEX;
|
|
715 }
|
|
716
|
0
|
717 /* regex matches */
|
|
718
|
|
719 for (/* void */; i < locations->nelts; i++) {
|
|
720
|
|
721 if (!clcfp[i]->regex) {
|
|
722 continue;
|
|
723 }
|
|
724
|
34
|
725 if (clcfp[i]->noname) {
|
|
726 break;
|
|
727 }
|
|
728
|
0
|
729 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
10
|
730 "find location: ~ \"%V\"", &clcfp[i]->name);
|
0
|
731
|
|
732 n = ngx_regex_exec(clcfp[i]->regex, &r->uri, NULL, 0);
|
|
733
|
30
|
734 if (n == NGX_REGEX_NO_MATCHED) {
|
0
|
735 continue;
|
|
736 }
|
|
737
|
|
738 if (n < 0) {
|
|
739 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
|
|
740 ngx_regex_exec_n
|
10
|
741 " failed: %d on \"%V\" using \"%V\"",
|
|
742 n, &r->uri, &clcfp[i]->name);
|
0
|
743 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
744 }
|
|
745
|
|
746 /* match */
|
|
747
|
|
748 r->loc_conf = clcfp[i]->loc_conf;
|
|
749
|
|
750 return NGX_HTTP_LOCATION_REGEX;
|
|
751 }
|
|
752
|
4
|
753 #endif /* NGX_PCRE */
|
0
|
754
|
|
755 return NGX_OK;
|
|
756 }
|
|
757
|
|
758
|
36
|
759 ngx_int_t
|
|
760 ngx_http_set_content_type(ngx_http_request_t *r)
|
0
|
761 {
|
34
|
762 u_char c, *p, *exten;
|
0
|
763 uint32_t key;
|
|
764 ngx_uint_t i;
|
|
765 ngx_http_type_t *type;
|
|
766 ngx_http_core_loc_conf_t *clcf;
|
|
767
|
|
768 r->headers_out.content_type = ngx_list_push(&r->headers_out.headers);
|
|
769 if (r->headers_out.content_type == NULL) {
|
|
770 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
771 }
|
|
772
|
|
773 r->headers_out.content_type->key.len = 0;
|
|
774 r->headers_out.content_type->key.data = NULL;
|
|
775 r->headers_out.content_type->value.len = 0;
|
|
776 r->headers_out.content_type->value.data = NULL;
|
|
777
|
|
778 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
|
|
779
|
|
780 if (r->exten.len) {
|
34
|
781
|
|
782 if (!r->low_case_exten) {
|
|
783 for (i = 0; i < r->exten.len; i++) {
|
|
784 c = r->exten.data[i];
|
|
785 if (c >= 'A' && c <= 'Z') {
|
|
786 break;
|
|
787 }
|
|
788 }
|
|
789
|
|
790 if (i < r->exten.len) {
|
50
|
791 p = ngx_palloc(r->pool, r->exten.len);
|
|
792 if (p == NULL) {
|
34
|
793 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
794 }
|
|
795
|
|
796 exten = p;
|
|
797
|
|
798 for (i = 0; i < r->exten.len; i++) {
|
|
799 c = r->exten.data[i];
|
|
800 if (c >= 'A' && c <= 'Z') {
|
|
801 *p++ = (u_char) (c | 0x20);
|
|
802 } else {
|
|
803 *p++ = c;
|
|
804 }
|
|
805 }
|
|
806
|
|
807 r->exten.data = exten;
|
|
808 }
|
|
809
|
|
810 r->low_case_exten = 1;
|
|
811 }
|
|
812
|
0
|
813 #if 0
|
|
814 key = ngx_crc(r->exten.data, r->exten.key);
|
|
815 #endif
|
|
816 ngx_http_types_hash_key(key, r->exten);
|
|
817
|
|
818 type = clcf->types[key].elts;
|
|
819 for (i = 0; i < clcf->types[key].nelts; i++) {
|
|
820 if (r->exten.len != type[i].exten.len) {
|
|
821 continue;
|
|
822 }
|
|
823
|
|
824 if (ngx_memcmp(r->exten.data, type[i].exten.data, r->exten.len)
|
|
825 == 0)
|
|
826 {
|
|
827 r->headers_out.content_type->value = type[i].type;
|
|
828 break;
|
|
829 }
|
|
830 }
|
|
831 }
|
|
832
|
|
833 if (r->headers_out.content_type->value.len == 0) {
|
|
834 r->headers_out.content_type->value = clcf->default_type;
|
|
835 }
|
|
836
|
|
837 return NGX_OK;
|
|
838 }
|
|
839
|
|
840
|
36
|
841 ngx_int_t
|
|
842 ngx_http_send_header(ngx_http_request_t *r)
|
0
|
843 {
|
|
844 if (r->main) {
|
|
845 return NGX_OK;
|
|
846 }
|
|
847
|
|
848 if (r->err_ctx) {
|
|
849 r->headers_out.status = r->err_status;
|
|
850 r->headers_out.status_line.len = 0;
|
|
851 }
|
|
852
|
36
|
853 return ngx_http_top_header_filter(r);
|
0
|
854 }
|
|
855
|
|
856
|
36
|
857 ngx_int_t
|
|
858 ngx_http_output_filter(ngx_http_request_t *r, ngx_chain_t *in)
|
0
|
859 {
|
|
860 ngx_int_t rc;
|
|
861
|
|
862 if (r->connection->write->error) {
|
|
863 return NGX_ERROR;
|
|
864 }
|
|
865
|
|
866 rc = ngx_http_top_body_filter(r, in);
|
|
867
|
|
868 if (rc == NGX_ERROR) {
|
|
869
|
36
|
870 /* NGX_ERROR may be returned by any filter */
|
0
|
871
|
|
872 r->connection->write->error = 1;
|
|
873 }
|
|
874
|
|
875 return rc;
|
|
876 }
|
|
877
|
|
878
|
36
|
879 ngx_int_t
|
|
880 ngx_http_redirect(ngx_http_request_t *r, int redirect)
|
0
|
881 {
|
|
882 /* STUB */
|
|
883
|
|
884 /* log request */
|
|
885
|
|
886 ngx_http_close_request(r, 0);
|
|
887 return NGX_OK;
|
|
888 }
|
|
889
|
|
890
|
36
|
891 ngx_int_t
|
|
892 ngx_http_set_exten(ngx_http_request_t *r)
|
0
|
893 {
|
|
894 ngx_int_t i;
|
|
895
|
|
896 r->exten.len = 0;
|
|
897 r->exten.data = NULL;
|
|
898
|
|
899 for (i = r->uri.len - 1; i > 1; i--) {
|
|
900 if (r->uri.data[i] == '.' && r->uri.data[i - 1] != '/') {
|
|
901 r->exten.len = r->uri.len - i - 1;
|
|
902
|
|
903 if (r->exten.len > 0) {
|
50
|
904 r->exten.data = ngx_palloc(r->pool, r->exten.len + 1);
|
|
905 if (r->exten.data == NULL) {
|
0
|
906 return NGX_ERROR;
|
|
907 }
|
|
908
|
|
909 ngx_cpystrn(r->exten.data, &r->uri.data[i + 1],
|
|
910 r->exten.len + 1);
|
|
911 }
|
|
912
|
|
913 break;
|
|
914
|
|
915 } else if (r->uri.data[i] == '/') {
|
|
916 break;
|
|
917 }
|
|
918 }
|
|
919
|
34
|
920 r->low_case_exten = 0;
|
|
921
|
0
|
922 return NGX_OK;
|
|
923 }
|
|
924
|
|
925
|
36
|
926 ngx_int_t
|
|
927 ngx_http_internal_redirect(ngx_http_request_t *r,
|
|
928 ngx_str_t *uri, ngx_str_t *args)
|
0
|
929 {
|
36
|
930 ngx_http_core_srv_conf_t *cscf;
|
|
931
|
0
|
932 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
10
|
933 "internal redirect: \"%V\"", uri);
|
0
|
934
|
10
|
935 r->uri = *uri;
|
0
|
936
|
|
937 if (args) {
|
10
|
938 r->args = *args;
|
0
|
939 }
|
|
940
|
|
941 if (ngx_http_set_exten(r) != NGX_OK) {
|
|
942 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
943 }
|
|
944
|
|
945 if (r->err_ctx) {
|
|
946
|
36
|
947 /* allocate the new module's contexts */
|
0
|
948
|
|
949 r->ctx = ngx_pcalloc(r->pool, sizeof(void *) * ngx_http_max_module);
|
|
950 if (r->ctx == NULL) {
|
|
951 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
952 }
|
|
953
|
|
954 } else {
|
|
955
|
|
956 /* clear the modules contexts */
|
|
957
|
|
958 ngx_memzero(r->ctx, sizeof(void *) * ngx_http_max_module);
|
|
959 }
|
|
960
|
36
|
961 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
|
|
962 r->loc_conf = cscf->ctx->loc_conf;
|
|
963
|
0
|
964 ngx_http_handler(r);
|
|
965
|
|
966 return NGX_DONE;
|
|
967 }
|
|
968
|
|
969
|
|
970 #if 0 /* STUB: test the delay http handler */
|
|
971
|
36
|
972 ngx_int_t
|
|
973 ngx_http_delay_handler(ngx_http_request_t *r)
|
0
|
974 {
|
|
975 static int on;
|
|
976
|
|
977 if (on++ == 0) {
|
|
978 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
979 "http set delay");
|
|
980 ngx_add_timer(r->connection->write, 10000);
|
|
981 return NGX_AGAIN;
|
|
982 }
|
|
983
|
|
984 r->connection->write->timedout = 0;
|
|
985 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
986 "http reset delay");
|
|
987 return NGX_DECLINED;
|
|
988 }
|
|
989
|
|
990 #endif
|
|
991
|
|
992
|
36
|
993 static char *
|
|
994 ngx_http_core_server(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy)
|
0
|
995 {
|
36
|
996 int m;
|
|
997 char *rv;
|
|
998 void *mconf;
|
|
999 ngx_conf_t save;
|
|
1000 ngx_http_module_t *module;
|
|
1001 ngx_http_conf_ctx_t *ctx, *http_ctx;
|
|
1002 ngx_http_core_srv_conf_t *cscf, **cscfp;
|
|
1003 ngx_http_core_main_conf_t *cmcf;
|
|
1004
|
50
|
1005 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
|
|
1006 if (ctx == NULL) {
|
36
|
1007 return NGX_CONF_ERROR;
|
|
1008 }
|
0
|
1009
|
|
1010 http_ctx = cf->ctx;
|
|
1011 ctx->main_conf = http_ctx->main_conf;
|
|
1012
|
|
1013 /* the server{}'s srv_conf */
|
|
1014
|
36
|
1015 ctx->srv_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
|
|
1016 if (ctx->srv_conf == NULL) {
|
|
1017 return NGX_CONF_ERROR;
|
|
1018 }
|
0
|
1019
|
|
1020 /* the server{}'s loc_conf */
|
|
1021
|
36
|
1022 ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
|
|
1023 if (ctx->loc_conf == NULL) {
|
|
1024 return NGX_CONF_ERROR;
|
|
1025 }
|
0
|
1026
|
|
1027 for (m = 0; ngx_modules[m]; m++) {
|
|
1028 if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
|
|
1029 continue;
|
|
1030 }
|
|
1031
|
|
1032 module = ngx_modules[m]->ctx;
|
|
1033
|
|
1034 if (module->create_srv_conf) {
|
50
|
1035 mconf = module->create_srv_conf(cf);
|
|
1036 if (mconf == NULL) {
|
36
|
1037 return NGX_CONF_ERROR;
|
|
1038 }
|
|
1039
|
|
1040 ctx->srv_conf[ngx_modules[m]->ctx_index] = mconf;
|
0
|
1041 }
|
|
1042
|
|
1043 if (module->create_loc_conf) {
|
50
|
1044 mconf = module->create_loc_conf(cf);
|
|
1045 if (mconf == NULL) {
|
36
|
1046 return NGX_CONF_ERROR;
|
|
1047 }
|
|
1048
|
|
1049 ctx->loc_conf[ngx_modules[m]->ctx_index] = mconf;
|
0
|
1050 }
|
|
1051 }
|
|
1052
|
36
|
1053
|
|
1054 /* the server configuration context */
|
0
|
1055
|
|
1056 cscf = ctx->srv_conf[ngx_http_core_module.ctx_index];
|
|
1057 cscf->ctx = ctx;
|
|
1058
|
36
|
1059
|
0
|
1060 cmcf = ctx->main_conf[ngx_http_core_module.ctx_index];
|
36
|
1061
|
50
|
1062 cscfp = ngx_array_push(&cmcf->servers);
|
|
1063 if (cscfp == NULL) {
|
36
|
1064 return NGX_CONF_ERROR;
|
|
1065 }
|
|
1066
|
0
|
1067 *cscfp = cscf;
|
|
1068
|
36
|
1069
|
0
|
1070 /* parse inside server{} */
|
|
1071
|
34
|
1072 save = *cf;
|
0
|
1073 cf->ctx = ctx;
|
|
1074 cf->cmd_type = NGX_HTTP_SRV_CONF;
|
34
|
1075
|
0
|
1076 rv = ngx_conf_parse(cf, NULL);
|
34
|
1077
|
|
1078 *cf = save;
|
0
|
1079
|
|
1080 if (rv != NGX_CONF_OK) {
|
|
1081 return rv;
|
|
1082 }
|
|
1083
|
|
1084 ngx_qsort(cscf->locations.elts, (size_t) cscf->locations.nelts,
|
34
|
1085 sizeof(ngx_http_core_loc_conf_t *), ngx_http_core_cmp_locations);
|
0
|
1086
|
|
1087 return rv;
|
|
1088 }
|
|
1089
|
|
1090
|
36
|
1091 static char *
|
|
1092 ngx_http_core_location(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy)
|
0
|
1093 {
|
|
1094 char *rv;
|
|
1095 ngx_int_t m;
|
|
1096 ngx_str_t *value;
|
34
|
1097 ngx_conf_t save;
|
0
|
1098 ngx_http_module_t *module;
|
|
1099 ngx_http_conf_ctx_t *ctx, *pctx;
|
|
1100 ngx_http_core_srv_conf_t *cscf;
|
|
1101 ngx_http_core_loc_conf_t *clcf, *pclcf, **clcfp;
|
4
|
1102 #if (NGX_PCRE)
|
0
|
1103 ngx_str_t err;
|
|
1104 u_char errstr[NGX_MAX_CONF_ERRSTR];
|
|
1105 #endif
|
|
1106
|
50
|
1107 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
|
|
1108 if (ctx == NULL) {
|
0
|
1109 return NGX_CONF_ERROR;
|
|
1110 }
|
|
1111
|
|
1112 pctx = cf->ctx;
|
|
1113 ctx->main_conf = pctx->main_conf;
|
|
1114 ctx->srv_conf = pctx->srv_conf;
|
|
1115
|
|
1116 ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
|
|
1117 if (ctx->loc_conf == NULL) {
|
|
1118 return NGX_CONF_ERROR;
|
|
1119 }
|
|
1120
|
|
1121 for (m = 0; ngx_modules[m]; m++) {
|
|
1122 if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
|
|
1123 continue;
|
|
1124 }
|
|
1125
|
|
1126 module = ngx_modules[m]->ctx;
|
|
1127
|
|
1128 if (module->create_loc_conf) {
|
|
1129 ctx->loc_conf[ngx_modules[m]->ctx_index] =
|
|
1130 module->create_loc_conf(cf);
|
|
1131 if (ctx->loc_conf[ngx_modules[m]->ctx_index] == NULL) {
|
|
1132 return NGX_CONF_ERROR;
|
|
1133 }
|
|
1134 }
|
|
1135 }
|
|
1136
|
|
1137 clcf = ctx->loc_conf[ngx_http_core_module.ctx_index];
|
|
1138 clcf->loc_conf = ctx->loc_conf;
|
|
1139
|
|
1140 value = cf->args->elts;
|
|
1141
|
|
1142 if (cf->args->nelts == 3) {
|
|
1143 if (value[1].len == 1 && value[1].data[0] == '=') {
|
12
|
1144 clcf->name = value[2];
|
0
|
1145 clcf->exact_match = 1;
|
|
1146
|
16
|
1147 } else if (value[1].len == 2
|
|
1148 && value[1].data[0] == '^'
|
|
1149 && value[1].data[1] == '~')
|
|
1150 {
|
|
1151 clcf->name = value[2];
|
|
1152 clcf->noregex = 1;
|
|
1153
|
0
|
1154 } else if ((value[1].len == 1 && value[1].data[0] == '~')
|
|
1155 || (value[1].len == 2
|
|
1156 && value[1].data[0] == '~'
|
|
1157 && value[1].data[1] == '*'))
|
|
1158 {
|
4
|
1159 #if (NGX_PCRE)
|
0
|
1160 err.len = NGX_MAX_CONF_ERRSTR;
|
|
1161 err.data = errstr;
|
|
1162
|
|
1163 clcf->regex = ngx_regex_compile(&value[2],
|
|
1164 value[1].len == 2 ? NGX_REGEX_CASELESS: 0,
|
|
1165 cf->pool, &err);
|
|
1166
|
|
1167 if (clcf->regex == NULL) {
|
|
1168 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "%s", err.data);
|
|
1169 return NGX_CONF_ERROR;
|
|
1170 }
|
|
1171
|
|
1172 clcf->name = value[2];
|
|
1173 #else
|
|
1174 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
10
|
1175 "the using of the regex \"%V\" "
|
|
1176 "requires PCRE library", &value[2]);
|
0
|
1177 return NGX_CONF_ERROR;
|
|
1178 #endif
|
|
1179
|
|
1180 } else {
|
|
1181 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
10
|
1182 "invalid location modifier \"%V\"", &value[1]);
|
0
|
1183 return NGX_CONF_ERROR;
|
|
1184 }
|
|
1185
|
|
1186 } else {
|
12
|
1187 clcf->name = value[1];
|
0
|
1188 }
|
|
1189
|
|
1190 pclcf = pctx->loc_conf[ngx_http_core_module.ctx_index];
|
|
1191
|
|
1192 if (pclcf->name.len == 0) {
|
|
1193 cscf = ctx->srv_conf[ngx_http_core_module.ctx_index];
|
50
|
1194
|
|
1195 clcfp = ngx_array_push(&cscf->locations);
|
|
1196 if (clcfp == NULL) {
|
0
|
1197 return NGX_CONF_ERROR;
|
|
1198 }
|
|
1199
|
|
1200 } else {
|
34
|
1201 #if 0
|
0
|
1202 clcf->prev_location = pclcf;
|
34
|
1203 #endif
|
0
|
1204
|
|
1205 if (pclcf->exact_match) {
|
|
1206 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
10
|
1207 "location \"%V\" could not be inside "
|
|
1208 "the exact location \"%V\"",
|
|
1209 &clcf->name, &pclcf->name);
|
0
|
1210 return NGX_CONF_ERROR;
|
|
1211 }
|
|
1212
|
4
|
1213 #if (NGX_PCRE)
|
0
|
1214 if (clcf->regex == NULL
|
|
1215 && ngx_strncmp(clcf->name.data, pclcf->name.data, pclcf->name.len)
|
|
1216 != 0)
|
|
1217 #else
|
|
1218 if (ngx_strncmp(clcf->name.data, pclcf->name.data, pclcf->name.len)
|
|
1219 != 0)
|
|
1220 #endif
|
|
1221 {
|
|
1222 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
10
|
1223 "location \"%V\" is outside location \"%V\"",
|
|
1224 &clcf->name, &pclcf->name);
|
0
|
1225 return NGX_CONF_ERROR;
|
|
1226 }
|
|
1227
|
|
1228 if (pclcf->locations.elts == NULL) {
|
50
|
1229 if (ngx_array_init(&pclcf->locations, cf->pool, 4, sizeof(void *))
|
|
1230 != NGX_OK)
|
|
1231 {
|
|
1232 return NGX_CONF_ERROR;
|
|
1233 }
|
0
|
1234 }
|
|
1235
|
50
|
1236 clcfp = ngx_array_push(&pclcf->locations);
|
|
1237 if (clcfp == NULL) {
|
0
|
1238 return NGX_CONF_ERROR;
|
|
1239 }
|
|
1240 }
|
|
1241
|
|
1242 *clcfp = clcf;
|
|
1243
|
34
|
1244 save = *cf;
|
0
|
1245 cf->ctx = ctx;
|
|
1246 cf->cmd_type = NGX_HTTP_LOC_CONF;
|
34
|
1247
|
0
|
1248 rv = ngx_conf_parse(cf, NULL);
|
34
|
1249
|
|
1250 *cf = save;
|
|
1251
|
|
1252 if (rv != NGX_CONF_OK) {
|
|
1253 return rv;
|
|
1254 }
|
|
1255
|
|
1256 ngx_qsort(clcf->locations.elts, (size_t) clcf->locations.nelts,
|
|
1257 sizeof(ngx_http_core_loc_conf_t *), ngx_http_core_cmp_locations);
|
0
|
1258
|
|
1259 return rv;
|
|
1260 }
|
|
1261
|
|
1262
|
52
|
1263 static int ngx_libc_cdecl
|
36
|
1264 ngx_http_core_cmp_locations(const void *one, const void *two)
|
34
|
1265 {
|
|
1266 ngx_int_t rc;
|
|
1267 ngx_http_core_loc_conf_t *first, *second;
|
|
1268
|
|
1269 first = *(ngx_http_core_loc_conf_t **) one;
|
|
1270 second = *(ngx_http_core_loc_conf_t **) two;
|
|
1271
|
|
1272 if (first->noname && !second->noname) {
|
|
1273 /* shift no named locations to the end */
|
|
1274 return 1;
|
|
1275 }
|
|
1276
|
|
1277 if (!first->noname && second->noname) {
|
|
1278 /* shift no named locations to the end */
|
|
1279 return -1;
|
|
1280 }
|
|
1281
|
|
1282 if (first->noname || second->noname) {
|
|
1283 /* do not sort no named locations */
|
|
1284 return 0;
|
|
1285 }
|
|
1286
|
|
1287 #if (NGX_PCRE)
|
|
1288
|
|
1289 if (first->regex && !second->regex) {
|
|
1290 /* shift the regex matches to the end */
|
|
1291 return 1;
|
|
1292 }
|
|
1293
|
|
1294 if (!first->regex && second->regex) {
|
|
1295 /* shift the regex matches to the end */
|
|
1296 return -1;
|
|
1297 }
|
|
1298
|
|
1299 if (first->regex || second->regex) {
|
|
1300 /* do not sort the regex matches */
|
|
1301 return 0;
|
|
1302 }
|
|
1303
|
|
1304 #endif
|
|
1305
|
|
1306 rc = ngx_strcmp(first->name.data, second->name.data);
|
|
1307
|
|
1308 if (rc == 0 && second->exact_match) {
|
|
1309 /* an exact match must be before the same inclusive one */
|
|
1310 return 1;
|
|
1311 }
|
|
1312
|
|
1313 return (int) rc;
|
|
1314 }
|
|
1315
|
|
1316
|
36
|
1317 static char *
|
|
1318 ngx_http_core_types(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
0
|
1319 {
|
|
1320 char *rv;
|
34
|
1321 ngx_conf_t save;
|
0
|
1322
|
34
|
1323 save = *cf;
|
|
1324 cf->handler = ngx_http_core_type;
|
0
|
1325 cf->handler_conf = conf;
|
34
|
1326
|
0
|
1327 rv = ngx_conf_parse(cf, NULL);
|
34
|
1328
|
|
1329 *cf = save;
|
0
|
1330
|
|
1331 return rv;
|
|
1332 }
|
|
1333
|
|
1334
|
36
|
1335 static char *
|
|
1336 ngx_http_core_type(ngx_conf_t *cf, ngx_command_t *dummy, void *conf)
|
0
|
1337 {
|
|
1338 ngx_http_core_loc_conf_t *lcf = conf;
|
|
1339
|
|
1340 uint32_t key;
|
|
1341 ngx_uint_t i;
|
34
|
1342 ngx_str_t *value;
|
0
|
1343 ngx_http_type_t *type;
|
|
1344
|
|
1345 if (lcf->types == NULL) {
|
|
1346 lcf->types = ngx_palloc(cf->pool, NGX_HTTP_TYPES_HASH_PRIME
|
|
1347 * sizeof(ngx_array_t));
|
|
1348 if (lcf->types == NULL) {
|
|
1349 return NGX_CONF_ERROR;
|
|
1350 }
|
|
1351
|
|
1352 for (i = 0; i < NGX_HTTP_TYPES_HASH_PRIME; i++) {
|
48
|
1353 if (ngx_array_init(&lcf->types[i], cf->pool, 4,
|
0
|
1354 sizeof(ngx_http_type_t)) == NGX_ERROR)
|
|
1355 {
|
|
1356 return NGX_CONF_ERROR;
|
|
1357 }
|
|
1358 }
|
|
1359 }
|
|
1360
|
34
|
1361 value = cf->args->elts;
|
0
|
1362
|
|
1363 for (i = 1; i < cf->args->nelts; i++) {
|
34
|
1364 ngx_http_types_hash_key(key, value[i]);
|
0
|
1365
|
50
|
1366 type = ngx_array_push(&lcf->types[key]);
|
|
1367 if (type == NULL) {
|
0
|
1368 return NGX_CONF_ERROR;
|
|
1369 }
|
|
1370
|
34
|
1371 type->exten = value[i];
|
|
1372 type->type = value[0];
|
0
|
1373 }
|
|
1374
|
|
1375 return NGX_CONF_OK;
|
|
1376 }
|
|
1377
|
|
1378
|
36
|
1379 static void *
|
|
1380 ngx_http_core_create_main_conf(ngx_conf_t *cf)
|
0
|
1381 {
|
34
|
1382 ngx_http_core_main_conf_t *cmcf;
|
|
1383
|
50
|
1384 cmcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_core_main_conf_t));
|
|
1385 if (cmcf == NULL) {
|
34
|
1386 return NGX_CONF_ERROR;
|
|
1387 }
|
0
|
1388
|
48
|
1389 if (ngx_array_init(&cmcf->servers, cf->pool, 4,
|
34
|
1390 sizeof(ngx_http_core_srv_conf_t *)) == NGX_ERROR)
|
|
1391 {
|
|
1392 return NGX_CONF_ERROR;
|
|
1393 }
|
0
|
1394
|
26
|
1395 cmcf->server_names_hash = NGX_CONF_UNSET_UINT;
|
|
1396 cmcf->server_names_hash_threshold = NGX_CONF_UNSET_UINT;
|
|
1397
|
0
|
1398 return cmcf;
|
|
1399 }
|
|
1400
|
|
1401
|
36
|
1402 static char *
|
|
1403 ngx_http_core_init_main_conf(ngx_conf_t *cf, void *conf)
|
0
|
1404 {
|
|
1405 ngx_http_core_main_conf_t *cmcf = conf;
|
|
1406
|
26
|
1407 if (cmcf->server_names_hash == NGX_CONF_UNSET_UINT) {
|
|
1408 cmcf->server_names_hash = 1009;
|
|
1409 }
|
|
1410
|
|
1411 if (cmcf->server_names_hash_threshold == NGX_CONF_UNSET_UINT) {
|
|
1412 cmcf->server_names_hash_threshold = 50;
|
|
1413 }
|
0
|
1414
|
|
1415 return NGX_CONF_OK;
|
|
1416 }
|
|
1417
|
|
1418
|
36
|
1419 static void *
|
|
1420 ngx_http_core_create_srv_conf(ngx_conf_t *cf)
|
0
|
1421 {
|
|
1422 ngx_http_core_srv_conf_t *cscf;
|
|
1423
|
50
|
1424 cscf = ngx_pcalloc(cf->pool, sizeof(ngx_http_core_srv_conf_t));
|
|
1425 if (cscf == NULL) {
|
36
|
1426 return NGX_CONF_ERROR;
|
|
1427 }
|
|
1428
|
0
|
1429 /*
|
36
|
1430 * set by ngx_pcalloc():
|
28
|
1431 *
|
|
1432 * conf->client_large_buffers.num = 0;
|
|
1433 */
|
0
|
1434
|
48
|
1435 if (ngx_array_init(&cscf->locations, cf->pool, 4, sizeof(void *))
|
36
|
1436 == NGX_ERROR)
|
|
1437 {
|
|
1438 return NGX_CONF_ERROR;
|
|
1439 }
|
|
1440
|
48
|
1441 if (ngx_array_init(&cscf->listen, cf->pool, 4, sizeof(ngx_http_listen_t))
|
36
|
1442 == NGX_ERROR)
|
|
1443 {
|
|
1444 return NGX_CONF_ERROR;
|
|
1445 }
|
|
1446
|
48
|
1447 if (ngx_array_init(&cscf->server_names, cf->pool, 4,
|
36
|
1448 sizeof(ngx_http_server_name_t)) == NGX_ERROR)
|
|
1449 {
|
|
1450 return NGX_CONF_ERROR;
|
|
1451 }
|
0
|
1452
|
|
1453 cscf->connection_pool_size = NGX_CONF_UNSET_SIZE;
|
|
1454 cscf->post_accept_timeout = NGX_CONF_UNSET_MSEC;
|
|
1455 cscf->request_pool_size = NGX_CONF_UNSET_SIZE;
|
|
1456 cscf->client_header_timeout = NGX_CONF_UNSET_MSEC;
|
|
1457 cscf->client_header_buffer_size = NGX_CONF_UNSET_SIZE;
|
|
1458 cscf->restrict_host_names = NGX_CONF_UNSET_UINT;
|
|
1459
|
|
1460 return cscf;
|
|
1461 }
|
|
1462
|
|
1463
|
36
|
1464 static char *
|
|
1465 ngx_http_core_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
|
0
|
1466 {
|
|
1467 ngx_http_core_srv_conf_t *prev = parent;
|
|
1468 ngx_http_core_srv_conf_t *conf = child;
|
|
1469
|
36
|
1470 ngx_http_listen_t *ls;
|
|
1471 ngx_http_server_name_t *sn;
|
0
|
1472 ngx_http_core_main_conf_t *cmcf;
|
|
1473
|
|
1474 /* TODO: it does not merge, it inits only */
|
|
1475
|
|
1476 if (conf->listen.nelts == 0) {
|
50
|
1477 ls = ngx_array_push(&conf->listen);
|
|
1478 if (ls == NULL) {
|
36
|
1479 return NGX_CONF_ERROR;
|
|
1480 }
|
|
1481
|
|
1482 ls->addr = INADDR_ANY;
|
10
|
1483 #if (NGX_WIN32)
|
36
|
1484 ls->port = 80;
|
0
|
1485 #else
|
|
1486 /* STUB: getuid() should be cached */
|
36
|
1487 ls->port = (getuid() == 0) ? 80 : 8000;
|
0
|
1488 #endif
|
36
|
1489 ls->family = AF_INET;
|
0
|
1490 }
|
|
1491
|
|
1492 if (conf->server_names.nelts == 0) {
|
50
|
1493 sn = ngx_array_push(&conf->server_names);
|
|
1494 if (sn == NULL) {
|
36
|
1495 return NGX_CONF_ERROR;
|
|
1496 }
|
|
1497
|
50
|
1498 sn->name.data = ngx_palloc(cf->pool, NGX_MAXHOSTNAMELEN);
|
|
1499 if (sn->name.data == NULL) {
|
36
|
1500 return NGX_CONF_ERROR;
|
|
1501 }
|
|
1502
|
|
1503 if (gethostname((char *) sn->name.data, NGX_MAXHOSTNAMELEN) == -1) {
|
0
|
1504 ngx_conf_log_error(NGX_LOG_EMERG, cf, ngx_errno,
|
|
1505 "gethostname() failed");
|
|
1506 return NGX_CONF_ERROR;
|
|
1507 }
|
|
1508
|
36
|
1509 sn->name.len = ngx_strlen(sn->name.data);
|
|
1510 sn->core_srv_conf = conf;
|
|
1511 sn->wildcard = 0;
|
0
|
1512
|
|
1513 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
|
|
1514
|
36
|
1515 if (cmcf->max_server_name_len < sn->name.len) {
|
|
1516 cmcf->max_server_name_len = sn->name.len;
|
0
|
1517 }
|
|
1518 }
|
|
1519
|
|
1520 ngx_conf_merge_size_value(conf->connection_pool_size,
|
|
1521 prev->connection_pool_size, 256);
|
|
1522 ngx_conf_merge_msec_value(conf->post_accept_timeout,
|
|
1523 prev->post_accept_timeout, 60000);
|
|
1524 ngx_conf_merge_size_value(conf->request_pool_size,
|
|
1525 prev->request_pool_size, 4096);
|
|
1526 ngx_conf_merge_msec_value(conf->client_header_timeout,
|
|
1527 prev->client_header_timeout, 60000);
|
|
1528 ngx_conf_merge_size_value(conf->client_header_buffer_size,
|
|
1529 prev->client_header_buffer_size, 1024);
|
|
1530 ngx_conf_merge_bufs_value(conf->large_client_header_buffers,
|
|
1531 prev->large_client_header_buffers,
|
|
1532 4, ngx_pagesize);
|
|
1533
|
|
1534 if (conf->large_client_header_buffers.size < conf->connection_pool_size) {
|
|
1535 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1536 "the \"large_client_header_buffers\" size must be "
|
|
1537 "equal to or bigger than \"connection_pool_size\"");
|
|
1538 return NGX_CONF_ERROR;
|
|
1539 }
|
|
1540
|
|
1541 ngx_conf_merge_unsigned_value(conf->restrict_host_names,
|
36
|
1542 prev->restrict_host_names, 0);
|
0
|
1543
|
|
1544 return NGX_CONF_OK;
|
|
1545 }
|
|
1546
|
|
1547
|
36
|
1548 static void *
|
|
1549 ngx_http_core_create_loc_conf(ngx_conf_t *cf)
|
0
|
1550 {
|
34
|
1551 ngx_http_core_loc_conf_t *lcf;
|
0
|
1552
|
50
|
1553 lcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_core_loc_conf_t));
|
|
1554 if (lcf == NULL) {
|
36
|
1555 return NGX_CONF_ERROR;
|
|
1556 }
|
0
|
1557
|
28
|
1558 /*
|
|
1559 * set by ngx_pcalloc():
|
|
1560 *
|
|
1561 * lcf->root.len = 0;
|
|
1562 * lcf->root.data = NULL;
|
|
1563 * lcf->types = NULL;
|
|
1564 * lcf->default_type.len = 0;
|
|
1565 * lcf->default_type.data = NULL;
|
|
1566 * lcf->err_log = NULL;
|
|
1567 * lcf->error_pages = NULL;
|
|
1568 * lcf->client_body_path = NULL;
|
|
1569 * lcf->regex = NULL;
|
|
1570 * lcf->exact_match = 0;
|
|
1571 * lcf->auto_redirect = 0;
|
|
1572 * lcf->alias = 0;
|
|
1573 */
|
0
|
1574
|
|
1575 lcf->client_max_body_size = NGX_CONF_UNSET_SIZE;
|
|
1576 lcf->client_body_buffer_size = NGX_CONF_UNSET_SIZE;
|
|
1577 lcf->client_body_timeout = NGX_CONF_UNSET_MSEC;
|
|
1578 lcf->sendfile = NGX_CONF_UNSET;
|
|
1579 lcf->tcp_nopush = NGX_CONF_UNSET;
|
2
|
1580 lcf->tcp_nodelay = NGX_CONF_UNSET;
|
0
|
1581 lcf->send_timeout = NGX_CONF_UNSET_MSEC;
|
|
1582 lcf->send_lowat = NGX_CONF_UNSET_SIZE;
|
|
1583 lcf->postpone_output = NGX_CONF_UNSET_SIZE;
|
|
1584 lcf->limit_rate = NGX_CONF_UNSET_SIZE;
|
|
1585 lcf->keepalive_timeout = NGX_CONF_UNSET_MSEC;
|
|
1586 lcf->keepalive_header = NGX_CONF_UNSET;
|
|
1587 lcf->lingering_time = NGX_CONF_UNSET_MSEC;
|
|
1588 lcf->lingering_timeout = NGX_CONF_UNSET_MSEC;
|
|
1589 lcf->reset_timedout_connection = NGX_CONF_UNSET;
|
|
1590 lcf->msie_padding = NGX_CONF_UNSET;
|
|
1591
|
|
1592 return lcf;
|
|
1593 }
|
|
1594
|
|
1595
|
36
|
1596 static ngx_http_type_t ngx_http_core_default_types[] = {
|
0
|
1597 { ngx_string("html"), ngx_string("text/html") },
|
|
1598 { ngx_string("gif"), ngx_string("image/gif") },
|
|
1599 { ngx_string("jpg"), ngx_string("image/jpeg") },
|
|
1600 { ngx_null_string, ngx_null_string }
|
|
1601 };
|
|
1602
|
|
1603
|
36
|
1604 static char *
|
|
1605 ngx_http_core_merge_loc_conf(ngx_conf_t *cf,
|
0
|
1606 void *parent, void *child)
|
|
1607 {
|
|
1608 ngx_http_core_loc_conf_t *prev = parent;
|
|
1609 ngx_http_core_loc_conf_t *conf = child;
|
|
1610
|
36
|
1611 uint32_t key;
|
|
1612 ngx_uint_t i;
|
|
1613 ngx_http_type_t *type;
|
0
|
1614
|
|
1615 ngx_conf_merge_str_value(conf->root, prev->root, "html");
|
|
1616
|
|
1617 if (ngx_conf_full_name(cf->cycle, &conf->root) == NGX_ERROR) {
|
|
1618 return NGX_CONF_ERROR;
|
|
1619 }
|
|
1620
|
|
1621 if (conf->types == NULL) {
|
|
1622 if (prev->types) {
|
|
1623 conf->types = prev->types;
|
|
1624
|
|
1625 } else {
|
36
|
1626 conf->types = ngx_palloc(cf->pool, NGX_HTTP_TYPES_HASH_PRIME
|
|
1627 * sizeof(ngx_array_t));
|
|
1628 if (conf->types == NULL) {
|
|
1629 return NGX_CONF_ERROR;
|
|
1630 }
|
0
|
1631
|
|
1632 for (i = 0; i < NGX_HTTP_TYPES_HASH_PRIME; i++) {
|
48
|
1633 if (ngx_array_init(&conf->types[i], cf->pool, 4,
|
36
|
1634 sizeof(ngx_http_type_t)) == NGX_ERROR)
|
|
1635 {
|
|
1636 return NGX_CONF_ERROR;
|
|
1637 }
|
0
|
1638 }
|
|
1639
|
36
|
1640 for (i = 0; ngx_http_core_default_types[i].exten.len; i++) {
|
|
1641 ngx_http_types_hash_key(key,
|
|
1642 ngx_http_core_default_types[i].exten);
|
|
1643
|
50
|
1644 type = ngx_array_push(&conf->types[key]);
|
|
1645 if (type == NULL) {
|
36
|
1646 return NGX_CONF_ERROR;
|
|
1647 }
|
|
1648
|
|
1649 *type = ngx_http_core_default_types[i];
|
0
|
1650 }
|
|
1651 }
|
|
1652 }
|
|
1653
|
|
1654 if (conf->err_log == NULL) {
|
|
1655 if (prev->err_log) {
|
|
1656 conf->err_log = prev->err_log;
|
|
1657 } else {
|
|
1658 conf->err_log = cf->cycle->new_log;
|
|
1659 }
|
|
1660 }
|
|
1661
|
|
1662 if (conf->error_pages == NULL && prev->error_pages) {
|
|
1663 conf->error_pages = prev->error_pages;
|
|
1664 }
|
|
1665
|
|
1666 ngx_conf_merge_str_value(conf->default_type,
|
36
|
1667 prev->default_type, "text/plain");
|
0
|
1668
|
|
1669 ngx_conf_merge_size_value(conf->client_max_body_size,
|
|
1670 prev->client_max_body_size, 1 * 1024 * 1024);
|
|
1671 ngx_conf_merge_size_value(conf->client_body_buffer_size,
|
|
1672 prev->client_body_buffer_size,
|
|
1673 (size_t) 2 * ngx_pagesize);
|
|
1674 ngx_conf_merge_msec_value(conf->client_body_timeout,
|
|
1675 prev->client_body_timeout, 60000);
|
|
1676 ngx_conf_merge_value(conf->sendfile, prev->sendfile, 0);
|
|
1677 ngx_conf_merge_value(conf->tcp_nopush, prev->tcp_nopush, 0);
|
2
|
1678 ngx_conf_merge_value(conf->tcp_nodelay, prev->tcp_nodelay, 0);
|
0
|
1679 ngx_conf_merge_msec_value(conf->send_timeout, prev->send_timeout, 60000);
|
|
1680 ngx_conf_merge_size_value(conf->send_lowat, prev->send_lowat, 0);
|
|
1681 ngx_conf_merge_size_value(conf->postpone_output, prev->postpone_output,
|
|
1682 1460);
|
|
1683 ngx_conf_merge_size_value(conf->limit_rate, prev->limit_rate, 0);
|
|
1684 ngx_conf_merge_msec_value(conf->keepalive_timeout,
|
|
1685 prev->keepalive_timeout, 75000);
|
|
1686 ngx_conf_merge_sec_value(conf->keepalive_header,
|
|
1687 prev->keepalive_header, 0);
|
|
1688 ngx_conf_merge_msec_value(conf->lingering_time,
|
|
1689 prev->lingering_time, 30000);
|
|
1690 ngx_conf_merge_msec_value(conf->lingering_timeout,
|
|
1691 prev->lingering_timeout, 5000);
|
|
1692
|
28
|
1693 ngx_conf_merge_path_value(conf->client_body_temp_path,
|
|
1694 prev->client_body_temp_path,
|
|
1695 NGX_HTTP_CLIENT_TEMP_PATH, 0, 0, 0,
|
|
1696 ngx_garbage_collector_temp_handler, cf);
|
|
1697
|
0
|
1698 ngx_conf_merge_value(conf->reset_timedout_connection,
|
36
|
1699 prev->reset_timedout_connection, 0);
|
0
|
1700 ngx_conf_merge_value(conf->msie_padding, prev->msie_padding, 1);
|
|
1701
|
|
1702 if (conf->open_files == NULL) {
|
|
1703 conf->open_files = prev->open_files;
|
|
1704 }
|
|
1705
|
|
1706 return NGX_CONF_OK;
|
|
1707 }
|
|
1708
|
|
1709
|
36
|
1710 static char *
|
|
1711 ngx_http_core_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
0
|
1712 {
|
|
1713 ngx_http_core_srv_conf_t *scf = conf;
|
|
1714
|
|
1715 u_char *addr;
|
|
1716 ngx_int_t port;
|
|
1717 ngx_uint_t p;
|
|
1718 struct hostent *h;
|
|
1719 ngx_str_t *args;
|
|
1720 ngx_http_listen_t *ls;
|
|
1721
|
|
1722 /*
|
|
1723 * TODO: check duplicate 'listen' directives,
|
|
1724 * add resolved name to server names ???
|
|
1725 */
|
|
1726
|
50
|
1727 ls = ngx_array_push(&scf->listen);
|
|
1728 if (ls == NULL) {
|
0
|
1729 return NGX_CONF_ERROR;
|
|
1730 }
|
|
1731
|
|
1732 /* AF_INET only */
|
|
1733
|
|
1734 ls->family = AF_INET;
|
|
1735 ls->default_server = 0;
|
|
1736 ls->file_name = cf->conf_file->file.name;
|
|
1737 ls->line = cf->conf_file->line;
|
|
1738
|
|
1739 args = cf->args->elts;
|
|
1740 addr = args[1].data;
|
|
1741
|
|
1742 for (p = 0; p < args[1].len; p++) {
|
|
1743 if (addr[p] == ':') {
|
|
1744 addr[p++] = '\0';
|
|
1745 break;
|
|
1746 }
|
|
1747 }
|
|
1748
|
|
1749 if (p == args[1].len) {
|
|
1750 /* no ":" in the "listen" */
|
|
1751 p = 0;
|
|
1752 }
|
|
1753
|
|
1754 port = ngx_atoi(&addr[p], args[1].len - p);
|
|
1755
|
|
1756 if (port == NGX_ERROR && p == 0) {
|
|
1757
|
|
1758 /* "listen host" */
|
|
1759 ls->port = 80;
|
|
1760
|
|
1761 } else if ((port == NGX_ERROR && p != 0) /* "listen host:NONNUMBER" */
|
|
1762 || (port < 1 || port > 65536)) { /* "listen 99999" */
|
|
1763
|
|
1764 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
10
|
1765 "invalid port \"%s\" in \"%V\" directive, "
|
0
|
1766 "it must be a number between 1 and 65535",
|
10
|
1767 &addr[p], &cmd->name);
|
0
|
1768
|
|
1769 return NGX_CONF_ERROR;
|
|
1770
|
|
1771 } else if (p == 0) {
|
|
1772 ls->addr = INADDR_ANY;
|
|
1773 ls->port = (in_port_t) port;
|
|
1774 return NGX_CONF_OK;
|
|
1775
|
|
1776 } else {
|
|
1777 ls->port = (in_port_t) port;
|
|
1778 }
|
|
1779
|
|
1780 ls->addr = inet_addr((const char *) addr);
|
36
|
1781
|
0
|
1782 if (ls->addr == INADDR_NONE) {
|
|
1783 h = gethostbyname((const char *) addr);
|
|
1784
|
|
1785 if (h == NULL || h->h_addr_list[0] == NULL) {
|
|
1786 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1787 "can not resolve host \"%s\" "
|
10
|
1788 "in \"%V\" directive", addr, &cmd->name);
|
0
|
1789 return NGX_CONF_ERROR;
|
|
1790 }
|
|
1791
|
|
1792 ls->addr = *(in_addr_t *)(h->h_addr_list[0]);
|
|
1793 }
|
|
1794
|
|
1795 return NGX_CONF_OK;
|
|
1796 }
|
|
1797
|
|
1798
|
36
|
1799 static char *
|
|
1800 ngx_http_core_server_name(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
0
|
1801 {
|
|
1802 ngx_http_core_srv_conf_t *scf = conf;
|
|
1803
|
|
1804 ngx_uint_t i;
|
|
1805 ngx_str_t *value;
|
|
1806 ngx_http_server_name_t *sn;
|
|
1807 ngx_http_core_main_conf_t *cmcf;
|
|
1808
|
|
1809 /* TODO: warn about duplicate 'server_name' directives */
|
|
1810
|
|
1811 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
|
|
1812
|
|
1813 value = cf->args->elts;
|
|
1814
|
|
1815 for (i = 1; i < cf->args->nelts; i++) {
|
|
1816 if (value[i].len == 0) {
|
|
1817 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
10
|
1818 "server name \"%V\" is invalid "
|
|
1819 "in \"%V\" directive",
|
|
1820 &value[i], &cmd->name);
|
0
|
1821 return NGX_CONF_ERROR;
|
|
1822 }
|
|
1823
|
50
|
1824 sn = ngx_array_push(&scf->server_names);
|
|
1825 if (sn == NULL) {
|
4
|
1826 return NGX_CONF_ERROR;
|
|
1827 }
|
0
|
1828
|
|
1829 sn->name.len = value[i].len;
|
|
1830 sn->name.data = value[i].data;
|
|
1831 sn->core_srv_conf = scf;
|
|
1832
|
4
|
1833 if (sn->name.data[0] == '*') {
|
|
1834 sn->name.len--;
|
|
1835 sn->name.data++;
|
|
1836 sn->wildcard = 1;
|
|
1837
|
|
1838 } else {
|
|
1839 sn->wildcard = 0;
|
|
1840 }
|
|
1841
|
0
|
1842 if (cmcf->max_server_name_len < sn->name.len) {
|
|
1843 cmcf->max_server_name_len = sn->name.len;
|
|
1844 }
|
|
1845 }
|
|
1846
|
|
1847 return NGX_CONF_OK;
|
|
1848 }
|
|
1849
|
|
1850
|
36
|
1851 static char *
|
|
1852 ngx_http_core_root(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
0
|
1853 {
|
|
1854 ngx_http_core_loc_conf_t *lcf = conf;
|
|
1855
|
|
1856 ngx_uint_t alias;
|
|
1857 ngx_str_t *value;
|
|
1858
|
|
1859 alias = (cmd->name.len == sizeof("alias") - 1) ? 1 : 0;
|
|
1860
|
|
1861 if (lcf->root.data) {
|
|
1862
|
|
1863 /* the (ngx_uint_t) cast is required by gcc 2.7.2.3 */
|
|
1864
|
|
1865 if ((ngx_uint_t) lcf->alias == alias) {
|
|
1866 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
10
|
1867 "\"%V\" directive is duplicate",
|
|
1868 &cmd->name);
|
0
|
1869 } else {
|
|
1870 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
10
|
1871 "\"%V\" directive is duplicate, "
|
0
|
1872 "\"%s\" directive is specified before",
|
10
|
1873 &cmd->name, lcf->alias ? "alias" : "root");
|
0
|
1874 }
|
|
1875
|
|
1876 return NGX_CONF_ERROR;
|
|
1877 }
|
|
1878
|
|
1879 value = cf->args->elts;
|
|
1880
|
|
1881 lcf->alias = alias;
|
|
1882 lcf->root = value[1];
|
|
1883
|
|
1884 if (!alias && lcf->root.data[lcf->root.len - 1] == '/') {
|
|
1885 lcf->root.len--;
|
|
1886 }
|
|
1887
|
|
1888 return NGX_CONF_OK;
|
|
1889 }
|
|
1890
|
|
1891
|
36
|
1892 static char *
|
|
1893 ngx_http_core_error_page(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
0
|
1894 {
|
|
1895 ngx_http_core_loc_conf_t *lcf = conf;
|
|
1896
|
36
|
1897 ngx_int_t overwrite;
|
0
|
1898 ngx_uint_t i, n;
|
|
1899 ngx_str_t *value;
|
|
1900 ngx_http_err_page_t *err;
|
|
1901
|
|
1902 if (lcf->error_pages == NULL) {
|
36
|
1903 lcf->error_pages = ngx_array_create(cf->pool, 4,
|
0
|
1904 sizeof(ngx_http_err_page_t));
|
|
1905 if (lcf->error_pages == NULL) {
|
|
1906 return NGX_CONF_ERROR;
|
|
1907 }
|
|
1908 }
|
|
1909
|
|
1910 value = cf->args->elts;
|
|
1911
|
|
1912 i = cf->args->nelts - 2;
|
|
1913
|
|
1914 if (value[i].data[0] == '=') {
|
|
1915 if (i == 1) {
|
|
1916 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
10
|
1917 "invalid value \"%V\"", &value[i]);
|
0
|
1918 return NGX_CONF_ERROR;
|
|
1919 }
|
|
1920
|
|
1921 overwrite = ngx_atoi(&value[i].data[1], value[i].len - 1);
|
|
1922
|
|
1923 if (overwrite == NGX_ERROR) {
|
|
1924 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
28
|
1925 "invalid value \"%V\"", &value[i]);
|
0
|
1926 return NGX_CONF_ERROR;
|
|
1927 }
|
|
1928
|
|
1929 n = 2;
|
|
1930
|
|
1931 } else {
|
|
1932 overwrite = 0;
|
|
1933 n = 1;
|
|
1934 }
|
|
1935
|
|
1936 for (i = 1; i < cf->args->nelts - n; i++) {
|
50
|
1937 err = ngx_array_push(lcf->error_pages);
|
|
1938 if (err == NULL) {
|
0
|
1939 return NGX_CONF_ERROR;
|
|
1940 }
|
|
1941
|
|
1942 err->status = ngx_atoi(value[i].data, value[i].len);
|
36
|
1943
|
0
|
1944 if (err->status == NGX_ERROR) {
|
|
1945 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
10
|
1946 "invalid value \"%V\"", &value[i]);
|
0
|
1947 return NGX_CONF_ERROR;
|
|
1948 }
|
|
1949
|
|
1950 if (err->status < 400 || err->status > 599) {
|
|
1951 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
10
|
1952 "value \"%V\" must be between 400 and 599",
|
|
1953 &value[i]);
|
0
|
1954 return NGX_CONF_ERROR;
|
|
1955 }
|
|
1956
|
|
1957 err->overwrite = overwrite;
|
|
1958 err->uri = value[cf->args->nelts - 1];
|
|
1959 }
|
|
1960
|
|
1961 return NGX_CONF_OK;
|
|
1962 }
|
|
1963
|
|
1964
|
36
|
1965 static char *
|
|
1966 ngx_http_core_keepalive(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
0
|
1967 {
|
|
1968 ngx_http_core_loc_conf_t *lcf = conf;
|
|
1969
|
|
1970 ngx_str_t *value;
|
|
1971
|
|
1972 if (lcf->keepalive_timeout != NGX_CONF_UNSET_MSEC) {
|
|
1973 return "is duplicate";
|
|
1974 }
|
|
1975
|
|
1976 value = cf->args->elts;
|
|
1977
|
|
1978 lcf->keepalive_timeout = ngx_parse_time(&value[1], 0);
|
36
|
1979
|
0
|
1980 if (lcf->keepalive_timeout == (ngx_msec_t) NGX_ERROR) {
|
|
1981 return "invalid value";
|
|
1982 }
|
|
1983
|
|
1984 if (lcf->keepalive_timeout == (ngx_msec_t) NGX_PARSE_LARGE_TIME) {
|
|
1985 return "value must be less than 597 hours";
|
|
1986 }
|
|
1987
|
|
1988 if (cf->args->nelts == 2) {
|
|
1989 return NGX_CONF_OK;
|
|
1990 }
|
|
1991
|
|
1992 lcf->keepalive_header = ngx_parse_time(&value[2], 1);
|
36
|
1993
|
0
|
1994 if (lcf->keepalive_header == NGX_ERROR) {
|
|
1995 return "invalid value";
|
|
1996 }
|
|
1997
|
|
1998 if (lcf->keepalive_header == NGX_PARSE_LARGE_TIME) {
|
|
1999 return "value must be less than 68 years";
|
|
2000 }
|
|
2001
|
|
2002 return NGX_CONF_OK;
|
|
2003 }
|
|
2004
|
|
2005
|
36
|
2006 static char *
|
|
2007 ngx_http_core_error_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
0
|
2008 {
|
|
2009 ngx_http_core_loc_conf_t *lcf = conf;
|
|
2010
|
50
|
2011 lcf->err_log = ngx_log_create_errlog(cf->cycle, cf->args);
|
|
2012 if (lcf->err_log == NULL) {
|
0
|
2013 return NGX_CONF_ERROR;
|
|
2014 }
|
|
2015
|
|
2016 return ngx_set_error_log_levels(cf, lcf->err_log);
|
|
2017 }
|
|
2018
|
|
2019
|
36
|
2020 static char *
|
|
2021 ngx_http_core_lowat_check(ngx_conf_t *cf, void *post, void *data)
|
0
|
2022 {
|
22
|
2023 #if (NGX_FREEBSD)
|
2
|
2024 ssize_t *np = data;
|
0
|
2025
|
|
2026 if (*np >= ngx_freebsd_net_inet_tcp_sendspace) {
|
|
2027 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
2028 "\"send_lowat\" must be less than %d "
|
|
2029 "(sysctl net.inet.tcp.sendspace)",
|
|
2030 ngx_freebsd_net_inet_tcp_sendspace);
|
|
2031
|
|
2032 return NGX_CONF_ERROR;
|
|
2033 }
|
|
2034
|
18
|
2035 #elif !(NGX_HAVE_SO_SNDLOWAT)
|
22
|
2036 ssize_t *np = data;
|
0
|
2037
|
|
2038 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
2039 "\"send_lowat\" is not supported, ignored");
|
|
2040
|
2
|
2041 *np = 0;
|
|
2042
|
0
|
2043 #endif
|
|
2044
|
|
2045 return NGX_CONF_OK;
|
|
2046 }
|
48
|
2047
|
|
2048
|
|
2049 static ngx_int_t
|
|
2050 ngx_http_core_init(ngx_cycle_t *cycle)
|
|
2051 {
|
50
|
2052 return ngx_http_variables_init(cycle);
|
48
|
2053 }
|