Update and clean Tomato RAF files
[tomato.git] / release / src / router / nginx / src / http / ngx_http_core_module.c
blob25d3dc97de375282fcb3487c158a31d2278503d0
2 /*
3 * Copyright (C) Igor Sysoev
4 * Copyright (C) Nginx, Inc.
5 */
8 #include <ngx_config.h>
9 #include <ngx_core.h>
10 #include <ngx_http.h>
13 typedef struct {
14 u_char *name;
15 uint32_t method;
16 } ngx_http_method_name_t;
19 #define NGX_HTTP_REQUEST_BODY_FILE_OFF 0
20 #define NGX_HTTP_REQUEST_BODY_FILE_ON 1
21 #define NGX_HTTP_REQUEST_BODY_FILE_CLEAN 2
24 static ngx_int_t ngx_http_core_find_location(ngx_http_request_t *r);
25 static ngx_int_t ngx_http_core_find_static_location(ngx_http_request_t *r,
26 ngx_http_location_tree_node_t *node);
28 static ngx_int_t ngx_http_core_preconfiguration(ngx_conf_t *cf);
29 static void *ngx_http_core_create_main_conf(ngx_conf_t *cf);
30 static char *ngx_http_core_init_main_conf(ngx_conf_t *cf, void *conf);
31 static void *ngx_http_core_create_srv_conf(ngx_conf_t *cf);
32 static char *ngx_http_core_merge_srv_conf(ngx_conf_t *cf,
33 void *parent, void *child);
34 static void *ngx_http_core_create_loc_conf(ngx_conf_t *cf);
35 static char *ngx_http_core_merge_loc_conf(ngx_conf_t *cf,
36 void *parent, void *child);
38 static char *ngx_http_core_server(ngx_conf_t *cf, ngx_command_t *cmd,
39 void *dummy);
40 static char *ngx_http_core_location(ngx_conf_t *cf, ngx_command_t *cmd,
41 void *dummy);
42 static ngx_int_t ngx_http_core_regex_location(ngx_conf_t *cf,
43 ngx_http_core_loc_conf_t *clcf, ngx_str_t *regex, ngx_uint_t caseless);
45 static char *ngx_http_core_types(ngx_conf_t *cf, ngx_command_t *cmd,
46 void *conf);
47 static char *ngx_http_core_type(ngx_conf_t *cf, ngx_command_t *dummy,
48 void *conf);
50 static char *ngx_http_core_listen(ngx_conf_t *cf, ngx_command_t *cmd,
51 void *conf);
52 static char *ngx_http_core_server_name(ngx_conf_t *cf, ngx_command_t *cmd,
53 void *conf);
54 static char *ngx_http_core_root(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
55 static char *ngx_http_core_limit_except(ngx_conf_t *cf, ngx_command_t *cmd,
56 void *conf);
57 static char *ngx_http_core_directio(ngx_conf_t *cf, ngx_command_t *cmd,
58 void *conf);
59 static char *ngx_http_core_error_page(ngx_conf_t *cf, ngx_command_t *cmd,
60 void *conf);
61 static char *ngx_http_core_try_files(ngx_conf_t *cf, ngx_command_t *cmd,
62 void *conf);
63 static char *ngx_http_core_open_file_cache(ngx_conf_t *cf, ngx_command_t *cmd,
64 void *conf);
65 static char *ngx_http_core_error_log(ngx_conf_t *cf, ngx_command_t *cmd,
66 void *conf);
67 static char *ngx_http_core_keepalive(ngx_conf_t *cf, ngx_command_t *cmd,
68 void *conf);
69 static char *ngx_http_core_internal(ngx_conf_t *cf, ngx_command_t *cmd,
70 void *conf);
71 static char *ngx_http_core_resolver(ngx_conf_t *cf, ngx_command_t *cmd,
72 void *conf);
73 #if (NGX_HTTP_GZIP)
74 static ngx_int_t ngx_http_gzip_accept_encoding(ngx_str_t *ae);
75 static ngx_uint_t ngx_http_gzip_quantity(u_char *p, u_char *last);
76 static char *ngx_http_gzip_disable(ngx_conf_t *cf, ngx_command_t *cmd,
77 void *conf);
78 #endif
79 static ngx_int_t ngx_http_get_forwarded_addr_internal(ngx_http_request_t *r,
80 ngx_addr_t *addr, u_char *xff, size_t xfflen, ngx_array_t *proxies,
81 int recursive);
82 #if (NGX_HAVE_OPENAT)
83 static char *ngx_http_disable_symlinks(ngx_conf_t *cf, ngx_command_t *cmd,
84 void *conf);
85 #endif
87 static char *ngx_http_core_lowat_check(ngx_conf_t *cf, void *post, void *data);
88 static char *ngx_http_core_pool_size(ngx_conf_t *cf, void *post, void *data);
90 static ngx_conf_post_t ngx_http_core_lowat_post =
91 { ngx_http_core_lowat_check };
93 static ngx_conf_post_handler_pt ngx_http_core_pool_size_p =
94 ngx_http_core_pool_size;
96 static ngx_conf_deprecated_t ngx_conf_deprecated_optimize_server_names = {
97 ngx_conf_deprecated, "optimize_server_names", "server_name_in_redirect"
100 static ngx_conf_deprecated_t ngx_conf_deprecated_open_file_cache_retest = {
101 ngx_conf_deprecated, "open_file_cache_retest", "open_file_cache_valid"
104 static ngx_conf_deprecated_t ngx_conf_deprecated_satisfy_any = {
105 ngx_conf_deprecated, "satisfy_any", "satisfy"
109 static ngx_conf_enum_t ngx_http_core_request_body_in_file[] = {
110 { ngx_string("off"), NGX_HTTP_REQUEST_BODY_FILE_OFF },
111 { ngx_string("on"), NGX_HTTP_REQUEST_BODY_FILE_ON },
112 { ngx_string("clean"), NGX_HTTP_REQUEST_BODY_FILE_CLEAN },
113 { ngx_null_string, 0 }
117 #if (NGX_HAVE_FILE_AIO)
119 static ngx_conf_enum_t ngx_http_core_aio[] = {
120 { ngx_string("off"), NGX_HTTP_AIO_OFF },
121 { ngx_string("on"), NGX_HTTP_AIO_ON },
122 #if (NGX_HAVE_AIO_SENDFILE)
123 { ngx_string("sendfile"), NGX_HTTP_AIO_SENDFILE },
124 #endif
125 { ngx_null_string, 0 }
128 #endif
131 static ngx_conf_enum_t ngx_http_core_satisfy[] = {
132 { ngx_string("all"), NGX_HTTP_SATISFY_ALL },
133 { ngx_string("any"), NGX_HTTP_SATISFY_ANY },
134 { ngx_null_string, 0 }
138 static ngx_conf_enum_t ngx_http_core_lingering_close[] = {
139 { ngx_string("off"), NGX_HTTP_LINGERING_OFF },
140 { ngx_string("on"), NGX_HTTP_LINGERING_ON },
141 { ngx_string("always"), NGX_HTTP_LINGERING_ALWAYS },
142 { ngx_null_string, 0 }
146 static ngx_conf_enum_t ngx_http_core_if_modified_since[] = {
147 { ngx_string("off"), NGX_HTTP_IMS_OFF },
148 { ngx_string("exact"), NGX_HTTP_IMS_EXACT },
149 { ngx_string("before"), NGX_HTTP_IMS_BEFORE },
150 { ngx_null_string, 0 }
154 static ngx_conf_bitmask_t ngx_http_core_keepalive_disable[] = {
155 { ngx_string("none"), NGX_HTTP_KEEPALIVE_DISABLE_NONE },
156 { ngx_string("msie6"), NGX_HTTP_KEEPALIVE_DISABLE_MSIE6 },
157 { ngx_string("safari"), NGX_HTTP_KEEPALIVE_DISABLE_SAFARI },
158 { ngx_null_string, 0 }
162 static ngx_path_init_t ngx_http_client_temp_path = {
163 ngx_string(NGX_HTTP_CLIENT_TEMP_PATH), { 0, 0, 0 }
167 #if (NGX_HTTP_GZIP)
169 static ngx_conf_enum_t ngx_http_gzip_http_version[] = {
170 { ngx_string("1.0"), NGX_HTTP_VERSION_10 },
171 { ngx_string("1.1"), NGX_HTTP_VERSION_11 },
172 { ngx_null_string, 0 }
176 static ngx_conf_bitmask_t ngx_http_gzip_proxied_mask[] = {
177 { ngx_string("off"), NGX_HTTP_GZIP_PROXIED_OFF },
178 { ngx_string("expired"), NGX_HTTP_GZIP_PROXIED_EXPIRED },
179 { ngx_string("no-cache"), NGX_HTTP_GZIP_PROXIED_NO_CACHE },
180 { ngx_string("no-store"), NGX_HTTP_GZIP_PROXIED_NO_STORE },
181 { ngx_string("private"), NGX_HTTP_GZIP_PROXIED_PRIVATE },
182 { ngx_string("no_last_modified"), NGX_HTTP_GZIP_PROXIED_NO_LM },
183 { ngx_string("no_etag"), NGX_HTTP_GZIP_PROXIED_NO_ETAG },
184 { ngx_string("auth"), NGX_HTTP_GZIP_PROXIED_AUTH },
185 { ngx_string("any"), NGX_HTTP_GZIP_PROXIED_ANY },
186 { ngx_null_string, 0 }
190 static ngx_str_t ngx_http_gzip_no_cache = ngx_string("no-cache");
191 static ngx_str_t ngx_http_gzip_no_store = ngx_string("no-store");
192 static ngx_str_t ngx_http_gzip_private = ngx_string("private");
194 #endif
197 static ngx_command_t ngx_http_core_commands[] = {
199 { ngx_string("variables_hash_max_size"),
200 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
201 ngx_conf_set_num_slot,
202 NGX_HTTP_MAIN_CONF_OFFSET,
203 offsetof(ngx_http_core_main_conf_t, variables_hash_max_size),
204 NULL },
206 { ngx_string("variables_hash_bucket_size"),
207 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
208 ngx_conf_set_num_slot,
209 NGX_HTTP_MAIN_CONF_OFFSET,
210 offsetof(ngx_http_core_main_conf_t, variables_hash_bucket_size),
211 NULL },
213 { ngx_string("server_names_hash_max_size"),
214 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
215 ngx_conf_set_num_slot,
216 NGX_HTTP_MAIN_CONF_OFFSET,
217 offsetof(ngx_http_core_main_conf_t, server_names_hash_max_size),
218 NULL },
220 { ngx_string("server_names_hash_bucket_size"),
221 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
222 ngx_conf_set_num_slot,
223 NGX_HTTP_MAIN_CONF_OFFSET,
224 offsetof(ngx_http_core_main_conf_t, server_names_hash_bucket_size),
225 NULL },
227 { ngx_string("server"),
228 NGX_HTTP_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
229 ngx_http_core_server,
232 NULL },
234 { ngx_string("connection_pool_size"),
235 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
236 ngx_conf_set_size_slot,
237 NGX_HTTP_SRV_CONF_OFFSET,
238 offsetof(ngx_http_core_srv_conf_t, connection_pool_size),
239 &ngx_http_core_pool_size_p },
241 { ngx_string("request_pool_size"),
242 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
243 ngx_conf_set_size_slot,
244 NGX_HTTP_SRV_CONF_OFFSET,
245 offsetof(ngx_http_core_srv_conf_t, request_pool_size),
246 &ngx_http_core_pool_size_p },
248 { ngx_string("client_header_timeout"),
249 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
250 ngx_conf_set_msec_slot,
251 NGX_HTTP_SRV_CONF_OFFSET,
252 offsetof(ngx_http_core_srv_conf_t, client_header_timeout),
253 NULL },
255 { ngx_string("client_header_buffer_size"),
256 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
257 ngx_conf_set_size_slot,
258 NGX_HTTP_SRV_CONF_OFFSET,
259 offsetof(ngx_http_core_srv_conf_t, client_header_buffer_size),
260 NULL },
262 { ngx_string("large_client_header_buffers"),
263 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE2,
264 ngx_conf_set_bufs_slot,
265 NGX_HTTP_SRV_CONF_OFFSET,
266 offsetof(ngx_http_core_srv_conf_t, large_client_header_buffers),
267 NULL },
269 { ngx_string("optimize_server_names"),
270 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_FLAG,
271 ngx_conf_set_flag_slot,
272 NGX_HTTP_LOC_CONF_OFFSET,
273 offsetof(ngx_http_core_loc_conf_t, server_name_in_redirect),
274 &ngx_conf_deprecated_optimize_server_names },
276 { ngx_string("ignore_invalid_headers"),
277 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_FLAG,
278 ngx_conf_set_flag_slot,
279 NGX_HTTP_SRV_CONF_OFFSET,
280 offsetof(ngx_http_core_srv_conf_t, ignore_invalid_headers),
281 NULL },
283 { ngx_string("merge_slashes"),
284 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_FLAG,
285 ngx_conf_set_flag_slot,
286 NGX_HTTP_SRV_CONF_OFFSET,
287 offsetof(ngx_http_core_srv_conf_t, merge_slashes),
288 NULL },
290 { ngx_string("underscores_in_headers"),
291 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_FLAG,
292 ngx_conf_set_flag_slot,
293 NGX_HTTP_SRV_CONF_OFFSET,
294 offsetof(ngx_http_core_srv_conf_t, underscores_in_headers),
295 NULL },
297 { ngx_string("location"),
298 NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE12,
299 ngx_http_core_location,
300 NGX_HTTP_SRV_CONF_OFFSET,
302 NULL },
304 { ngx_string("listen"),
305 NGX_HTTP_SRV_CONF|NGX_CONF_1MORE,
306 ngx_http_core_listen,
307 NGX_HTTP_SRV_CONF_OFFSET,
309 NULL },
311 { ngx_string("server_name"),
312 NGX_HTTP_SRV_CONF|NGX_CONF_1MORE,
313 ngx_http_core_server_name,
314 NGX_HTTP_SRV_CONF_OFFSET,
316 NULL },
318 { ngx_string("types_hash_max_size"),
319 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
320 ngx_conf_set_num_slot,
321 NGX_HTTP_LOC_CONF_OFFSET,
322 offsetof(ngx_http_core_loc_conf_t, types_hash_max_size),
323 NULL },
325 { ngx_string("types_hash_bucket_size"),
326 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
327 ngx_conf_set_num_slot,
328 NGX_HTTP_LOC_CONF_OFFSET,
329 offsetof(ngx_http_core_loc_conf_t, types_hash_bucket_size),
330 NULL },
332 { ngx_string("types"),
333 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF
334 |NGX_CONF_BLOCK|NGX_CONF_NOARGS,
335 ngx_http_core_types,
336 NGX_HTTP_LOC_CONF_OFFSET,
338 NULL },
340 { ngx_string("default_type"),
341 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
342 ngx_conf_set_str_slot,
343 NGX_HTTP_LOC_CONF_OFFSET,
344 offsetof(ngx_http_core_loc_conf_t, default_type),
345 NULL },
347 { ngx_string("root"),
348 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
349 |NGX_CONF_TAKE1,
350 ngx_http_core_root,
351 NGX_HTTP_LOC_CONF_OFFSET,
353 NULL },
355 { ngx_string("alias"),
356 NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
357 ngx_http_core_root,
358 NGX_HTTP_LOC_CONF_OFFSET,
360 NULL },
362 { ngx_string("limit_except"),
363 NGX_HTTP_LOC_CONF|NGX_CONF_BLOCK|NGX_CONF_1MORE,
364 ngx_http_core_limit_except,
365 NGX_HTTP_LOC_CONF_OFFSET,
367 NULL },
369 { ngx_string("client_max_body_size"),
370 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
371 ngx_conf_set_off_slot,
372 NGX_HTTP_LOC_CONF_OFFSET,
373 offsetof(ngx_http_core_loc_conf_t, client_max_body_size),
374 NULL },
376 { ngx_string("client_body_buffer_size"),
377 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
378 ngx_conf_set_size_slot,
379 NGX_HTTP_LOC_CONF_OFFSET,
380 offsetof(ngx_http_core_loc_conf_t, client_body_buffer_size),
381 NULL },
383 { ngx_string("client_body_timeout"),
384 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
385 ngx_conf_set_msec_slot,
386 NGX_HTTP_LOC_CONF_OFFSET,
387 offsetof(ngx_http_core_loc_conf_t, client_body_timeout),
388 NULL },
390 { ngx_string("client_body_temp_path"),
391 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1234,
392 ngx_conf_set_path_slot,
393 NGX_HTTP_LOC_CONF_OFFSET,
394 offsetof(ngx_http_core_loc_conf_t, client_body_temp_path),
395 NULL },
397 { ngx_string("client_body_in_file_only"),
398 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
399 ngx_conf_set_enum_slot,
400 NGX_HTTP_LOC_CONF_OFFSET,
401 offsetof(ngx_http_core_loc_conf_t, client_body_in_file_only),
402 &ngx_http_core_request_body_in_file },
404 { ngx_string("client_body_in_single_buffer"),
405 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
406 ngx_conf_set_flag_slot,
407 NGX_HTTP_LOC_CONF_OFFSET,
408 offsetof(ngx_http_core_loc_conf_t, client_body_in_single_buffer),
409 NULL },
411 { ngx_string("sendfile"),
412 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
413 |NGX_CONF_FLAG,
414 ngx_conf_set_flag_slot,
415 NGX_HTTP_LOC_CONF_OFFSET,
416 offsetof(ngx_http_core_loc_conf_t, sendfile),
417 NULL },
419 { ngx_string("sendfile_max_chunk"),
420 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
421 ngx_conf_set_size_slot,
422 NGX_HTTP_LOC_CONF_OFFSET,
423 offsetof(ngx_http_core_loc_conf_t, sendfile_max_chunk),
424 NULL },
426 #if (NGX_HAVE_FILE_AIO)
428 { ngx_string("aio"),
429 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
430 ngx_conf_set_enum_slot,
431 NGX_HTTP_LOC_CONF_OFFSET,
432 offsetof(ngx_http_core_loc_conf_t, aio),
433 &ngx_http_core_aio },
435 #endif
437 { ngx_string("read_ahead"),
438 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
439 ngx_conf_set_size_slot,
440 NGX_HTTP_LOC_CONF_OFFSET,
441 offsetof(ngx_http_core_loc_conf_t, read_ahead),
442 NULL },
444 { ngx_string("directio"),
445 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
446 ngx_http_core_directio,
447 NGX_HTTP_LOC_CONF_OFFSET,
449 NULL },
451 { ngx_string("directio_alignment"),
452 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
453 ngx_conf_set_off_slot,
454 NGX_HTTP_LOC_CONF_OFFSET,
455 offsetof(ngx_http_core_loc_conf_t, directio_alignment),
456 NULL },
458 { ngx_string("tcp_nopush"),
459 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
460 ngx_conf_set_flag_slot,
461 NGX_HTTP_LOC_CONF_OFFSET,
462 offsetof(ngx_http_core_loc_conf_t, tcp_nopush),
463 NULL },
465 { ngx_string("tcp_nodelay"),
466 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
467 ngx_conf_set_flag_slot,
468 NGX_HTTP_LOC_CONF_OFFSET,
469 offsetof(ngx_http_core_loc_conf_t, tcp_nodelay),
470 NULL },
472 { ngx_string("send_timeout"),
473 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
474 ngx_conf_set_msec_slot,
475 NGX_HTTP_LOC_CONF_OFFSET,
476 offsetof(ngx_http_core_loc_conf_t, send_timeout),
477 NULL },
479 { ngx_string("send_lowat"),
480 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
481 ngx_conf_set_size_slot,
482 NGX_HTTP_LOC_CONF_OFFSET,
483 offsetof(ngx_http_core_loc_conf_t, send_lowat),
484 &ngx_http_core_lowat_post },
486 { ngx_string("postpone_output"),
487 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
488 ngx_conf_set_size_slot,
489 NGX_HTTP_LOC_CONF_OFFSET,
490 offsetof(ngx_http_core_loc_conf_t, postpone_output),
491 NULL },
493 { ngx_string("limit_rate"),
494 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
495 |NGX_CONF_TAKE1,
496 ngx_conf_set_size_slot,
497 NGX_HTTP_LOC_CONF_OFFSET,
498 offsetof(ngx_http_core_loc_conf_t, limit_rate),
499 NULL },
501 { ngx_string("limit_rate_after"),
502 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
503 |NGX_CONF_TAKE1,
504 ngx_conf_set_size_slot,
505 NGX_HTTP_LOC_CONF_OFFSET,
506 offsetof(ngx_http_core_loc_conf_t, limit_rate_after),
507 NULL },
509 { ngx_string("keepalive_timeout"),
510 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
511 ngx_http_core_keepalive,
512 NGX_HTTP_LOC_CONF_OFFSET,
514 NULL },
516 { ngx_string("keepalive_requests"),
517 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
518 ngx_conf_set_num_slot,
519 NGX_HTTP_LOC_CONF_OFFSET,
520 offsetof(ngx_http_core_loc_conf_t, keepalive_requests),
521 NULL },
523 { ngx_string("keepalive_disable"),
524 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
525 ngx_conf_set_bitmask_slot,
526 NGX_HTTP_LOC_CONF_OFFSET,
527 offsetof(ngx_http_core_loc_conf_t, keepalive_disable),
528 &ngx_http_core_keepalive_disable },
530 { ngx_string("satisfy"),
531 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
532 ngx_conf_set_enum_slot,
533 NGX_HTTP_LOC_CONF_OFFSET,
534 offsetof(ngx_http_core_loc_conf_t, satisfy),
535 &ngx_http_core_satisfy },
537 { ngx_string("satisfy_any"),
538 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
539 ngx_conf_set_flag_slot,
540 NGX_HTTP_LOC_CONF_OFFSET,
541 offsetof(ngx_http_core_loc_conf_t, satisfy),
542 &ngx_conf_deprecated_satisfy_any },
544 { ngx_string("internal"),
545 NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,
546 ngx_http_core_internal,
547 NGX_HTTP_LOC_CONF_OFFSET,
549 NULL },
551 { ngx_string("lingering_close"),
552 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
553 ngx_conf_set_enum_slot,
554 NGX_HTTP_LOC_CONF_OFFSET,
555 offsetof(ngx_http_core_loc_conf_t, lingering_close),
556 &ngx_http_core_lingering_close },
558 { ngx_string("lingering_time"),
559 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
560 ngx_conf_set_msec_slot,
561 NGX_HTTP_LOC_CONF_OFFSET,
562 offsetof(ngx_http_core_loc_conf_t, lingering_time),
563 NULL },
565 { ngx_string("lingering_timeout"),
566 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
567 ngx_conf_set_msec_slot,
568 NGX_HTTP_LOC_CONF_OFFSET,
569 offsetof(ngx_http_core_loc_conf_t, lingering_timeout),
570 NULL },
572 { ngx_string("reset_timedout_connection"),
573 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
574 ngx_conf_set_flag_slot,
575 NGX_HTTP_LOC_CONF_OFFSET,
576 offsetof(ngx_http_core_loc_conf_t, reset_timedout_connection),
577 NULL },
579 { ngx_string("server_name_in_redirect"),
580 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
581 ngx_conf_set_flag_slot,
582 NGX_HTTP_LOC_CONF_OFFSET,
583 offsetof(ngx_http_core_loc_conf_t, server_name_in_redirect),
584 NULL },
586 { ngx_string("port_in_redirect"),
587 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
588 ngx_conf_set_flag_slot,
589 NGX_HTTP_LOC_CONF_OFFSET,
590 offsetof(ngx_http_core_loc_conf_t, port_in_redirect),
591 NULL },
593 { ngx_string("msie_padding"),
594 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
595 ngx_conf_set_flag_slot,
596 NGX_HTTP_LOC_CONF_OFFSET,
597 offsetof(ngx_http_core_loc_conf_t, msie_padding),
598 NULL },
600 { ngx_string("msie_refresh"),
601 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
602 ngx_conf_set_flag_slot,
603 NGX_HTTP_LOC_CONF_OFFSET,
604 offsetof(ngx_http_core_loc_conf_t, msie_refresh),
605 NULL },
607 { ngx_string("log_not_found"),
608 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
609 ngx_conf_set_flag_slot,
610 NGX_HTTP_LOC_CONF_OFFSET,
611 offsetof(ngx_http_core_loc_conf_t, log_not_found),
612 NULL },
614 { ngx_string("log_subrequest"),
615 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
616 ngx_conf_set_flag_slot,
617 NGX_HTTP_LOC_CONF_OFFSET,
618 offsetof(ngx_http_core_loc_conf_t, log_subrequest),
619 NULL },
621 { ngx_string("recursive_error_pages"),
622 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
623 ngx_conf_set_flag_slot,
624 NGX_HTTP_LOC_CONF_OFFSET,
625 offsetof(ngx_http_core_loc_conf_t, recursive_error_pages),
626 NULL },
628 { ngx_string("server_tokens"),
629 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
630 ngx_conf_set_flag_slot,
631 NGX_HTTP_LOC_CONF_OFFSET,
632 offsetof(ngx_http_core_loc_conf_t, server_tokens),
633 NULL },
635 { ngx_string("if_modified_since"),
636 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
637 ngx_conf_set_enum_slot,
638 NGX_HTTP_LOC_CONF_OFFSET,
639 offsetof(ngx_http_core_loc_conf_t, if_modified_since),
640 &ngx_http_core_if_modified_since },
642 { ngx_string("max_ranges"),
643 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
644 ngx_conf_set_num_slot,
645 NGX_HTTP_LOC_CONF_OFFSET,
646 offsetof(ngx_http_core_loc_conf_t, max_ranges),
647 NULL },
649 { ngx_string("chunked_transfer_encoding"),
650 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
651 ngx_conf_set_flag_slot,
652 NGX_HTTP_LOC_CONF_OFFSET,
653 offsetof(ngx_http_core_loc_conf_t, chunked_transfer_encoding),
654 NULL },
656 { ngx_string("etag"),
657 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
658 ngx_conf_set_flag_slot,
659 NGX_HTTP_LOC_CONF_OFFSET,
660 offsetof(ngx_http_core_loc_conf_t, etag),
661 NULL },
663 { ngx_string("error_page"),
664 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
665 |NGX_CONF_2MORE,
666 ngx_http_core_error_page,
667 NGX_HTTP_LOC_CONF_OFFSET,
669 NULL },
671 { ngx_string("try_files"),
672 NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_2MORE,
673 ngx_http_core_try_files,
674 NGX_HTTP_LOC_CONF_OFFSET,
676 NULL },
678 { ngx_string("post_action"),
679 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
680 |NGX_CONF_TAKE1,
681 ngx_conf_set_str_slot,
682 NGX_HTTP_LOC_CONF_OFFSET,
683 offsetof(ngx_http_core_loc_conf_t, post_action),
684 NULL },
686 { ngx_string("error_log"),
687 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
688 ngx_http_core_error_log,
689 NGX_HTTP_LOC_CONF_OFFSET,
691 NULL },
693 { ngx_string("open_file_cache"),
694 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
695 ngx_http_core_open_file_cache,
696 NGX_HTTP_LOC_CONF_OFFSET,
697 offsetof(ngx_http_core_loc_conf_t, open_file_cache),
698 NULL },
700 { ngx_string("open_file_cache_valid"),
701 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
702 ngx_conf_set_sec_slot,
703 NGX_HTTP_LOC_CONF_OFFSET,
704 offsetof(ngx_http_core_loc_conf_t, open_file_cache_valid),
705 NULL },
707 { ngx_string("open_file_cache_retest"),
708 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
709 ngx_conf_set_sec_slot,
710 NGX_HTTP_LOC_CONF_OFFSET,
711 offsetof(ngx_http_core_loc_conf_t, open_file_cache_valid),
712 &ngx_conf_deprecated_open_file_cache_retest },
714 { ngx_string("open_file_cache_min_uses"),
715 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
716 ngx_conf_set_num_slot,
717 NGX_HTTP_LOC_CONF_OFFSET,
718 offsetof(ngx_http_core_loc_conf_t, open_file_cache_min_uses),
719 NULL },
721 { ngx_string("open_file_cache_errors"),
722 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
723 ngx_conf_set_flag_slot,
724 NGX_HTTP_LOC_CONF_OFFSET,
725 offsetof(ngx_http_core_loc_conf_t, open_file_cache_errors),
726 NULL },
728 { ngx_string("open_file_cache_events"),
729 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
730 ngx_conf_set_flag_slot,
731 NGX_HTTP_LOC_CONF_OFFSET,
732 offsetof(ngx_http_core_loc_conf_t, open_file_cache_events),
733 NULL },
735 { ngx_string("resolver"),
736 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
737 ngx_http_core_resolver,
738 NGX_HTTP_LOC_CONF_OFFSET,
740 NULL },
742 { ngx_string("resolver_timeout"),
743 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
744 ngx_conf_set_msec_slot,
745 NGX_HTTP_LOC_CONF_OFFSET,
746 offsetof(ngx_http_core_loc_conf_t, resolver_timeout),
747 NULL },
749 #if (NGX_HTTP_GZIP)
751 { ngx_string("gzip_vary"),
752 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
753 ngx_conf_set_flag_slot,
754 NGX_HTTP_LOC_CONF_OFFSET,
755 offsetof(ngx_http_core_loc_conf_t, gzip_vary),
756 NULL },
758 { ngx_string("gzip_http_version"),
759 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
760 ngx_conf_set_enum_slot,
761 NGX_HTTP_LOC_CONF_OFFSET,
762 offsetof(ngx_http_core_loc_conf_t, gzip_http_version),
763 &ngx_http_gzip_http_version },
765 { ngx_string("gzip_proxied"),
766 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
767 ngx_conf_set_bitmask_slot,
768 NGX_HTTP_LOC_CONF_OFFSET,
769 offsetof(ngx_http_core_loc_conf_t, gzip_proxied),
770 &ngx_http_gzip_proxied_mask },
772 { ngx_string("gzip_disable"),
773 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
774 ngx_http_gzip_disable,
775 NGX_HTTP_LOC_CONF_OFFSET,
777 NULL },
779 #endif
781 #if (NGX_HAVE_OPENAT)
783 { ngx_string("disable_symlinks"),
784 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
785 ngx_http_disable_symlinks,
786 NGX_HTTP_LOC_CONF_OFFSET,
788 NULL },
790 #endif
792 ngx_null_command
796 static ngx_http_module_t ngx_http_core_module_ctx = {
797 ngx_http_core_preconfiguration, /* preconfiguration */
798 NULL, /* postconfiguration */
800 ngx_http_core_create_main_conf, /* create main configuration */
801 ngx_http_core_init_main_conf, /* init main configuration */
803 ngx_http_core_create_srv_conf, /* create server configuration */
804 ngx_http_core_merge_srv_conf, /* merge server configuration */
806 ngx_http_core_create_loc_conf, /* create location configuration */
807 ngx_http_core_merge_loc_conf /* merge location configuration */
811 ngx_module_t ngx_http_core_module = {
812 NGX_MODULE_V1,
813 &ngx_http_core_module_ctx, /* module context */
814 ngx_http_core_commands, /* module directives */
815 NGX_HTTP_MODULE, /* module type */
816 NULL, /* init master */
817 NULL, /* init module */
818 NULL, /* init process */
819 NULL, /* init thread */
820 NULL, /* exit thread */
821 NULL, /* exit process */
822 NULL, /* exit master */
823 NGX_MODULE_V1_PADDING
827 ngx_str_t ngx_http_core_get_method = { 3, (u_char *) "GET " };
830 void
831 ngx_http_handler(ngx_http_request_t *r)
833 ngx_http_core_main_conf_t *cmcf;
835 r->connection->log->action = NULL;
837 r->connection->unexpected_eof = 0;
839 if (!r->internal) {
840 switch (r->headers_in.connection_type) {
841 case 0:
842 r->keepalive = (r->http_version > NGX_HTTP_VERSION_10);
843 break;
845 case NGX_HTTP_CONNECTION_CLOSE:
846 r->keepalive = 0;
847 break;
849 case NGX_HTTP_CONNECTION_KEEP_ALIVE:
850 r->keepalive = 1;
851 break;
854 r->lingering_close = (r->headers_in.content_length_n > 0
855 || r->headers_in.chunked);
856 r->phase_handler = 0;
858 } else {
859 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
860 r->phase_handler = cmcf->phase_engine.server_rewrite_index;
863 r->valid_location = 1;
864 #if (NGX_HTTP_GZIP)
865 r->gzip_tested = 0;
866 r->gzip_ok = 0;
867 r->gzip_vary = 0;
868 #endif
870 r->write_event_handler = ngx_http_core_run_phases;
871 ngx_http_core_run_phases(r);
875 void
876 ngx_http_core_run_phases(ngx_http_request_t *r)
878 ngx_int_t rc;
879 ngx_http_phase_handler_t *ph;
880 ngx_http_core_main_conf_t *cmcf;
882 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
884 ph = cmcf->phase_engine.handlers;
886 while (ph[r->phase_handler].checker) {
888 rc = ph[r->phase_handler].checker(r, &ph[r->phase_handler]);
890 if (rc == NGX_OK) {
891 return;
897 ngx_int_t
898 ngx_http_core_generic_phase(ngx_http_request_t *r, ngx_http_phase_handler_t *ph)
900 ngx_int_t rc;
903 * generic phase checker,
904 * used by the post read and pre-access phases
907 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
908 "generic phase: %ui", r->phase_handler);
910 rc = ph->handler(r);
912 if (rc == NGX_OK) {
913 r->phase_handler = ph->next;
914 return NGX_AGAIN;
917 if (rc == NGX_DECLINED) {
918 r->phase_handler++;
919 return NGX_AGAIN;
922 if (rc == NGX_AGAIN || rc == NGX_DONE) {
923 return NGX_OK;
926 /* rc == NGX_ERROR || rc == NGX_HTTP_... */
928 ngx_http_finalize_request(r, rc);
930 return NGX_OK;
934 ngx_int_t
935 ngx_http_core_rewrite_phase(ngx_http_request_t *r, ngx_http_phase_handler_t *ph)
937 ngx_int_t rc;
939 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
940 "rewrite phase: %ui", r->phase_handler);
942 rc = ph->handler(r);
944 if (rc == NGX_DECLINED) {
945 r->phase_handler++;
946 return NGX_AGAIN;
949 if (rc == NGX_DONE) {
950 return NGX_OK;
953 /* NGX_OK, NGX_AGAIN, NGX_ERROR, NGX_HTTP_... */
955 ngx_http_finalize_request(r, rc);
957 return NGX_OK;
961 ngx_int_t
962 ngx_http_core_find_config_phase(ngx_http_request_t *r,
963 ngx_http_phase_handler_t *ph)
965 u_char *p;
966 size_t len;
967 ngx_int_t rc;
968 ngx_http_core_loc_conf_t *clcf;
970 r->content_handler = NULL;
971 r->uri_changed = 0;
973 rc = ngx_http_core_find_location(r);
975 if (rc == NGX_ERROR) {
976 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
977 return NGX_OK;
980 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
982 if (!r->internal && clcf->internal) {
983 ngx_http_finalize_request(r, NGX_HTTP_NOT_FOUND);
984 return NGX_OK;
987 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
988 "using configuration \"%s%V\"",
989 (clcf->noname ? "*" : (clcf->exact_match ? "=" : "")),
990 &clcf->name);
992 ngx_http_update_location_config(r);
994 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
995 "http cl:%O max:%O",
996 r->headers_in.content_length_n, clcf->client_max_body_size);
998 if (r->headers_in.content_length_n != -1
999 && !r->discard_body
1000 && clcf->client_max_body_size
1001 && clcf->client_max_body_size < r->headers_in.content_length_n)
1003 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1004 "client intended to send too large body: %O bytes",
1005 r->headers_in.content_length_n);
1007 r->expect_tested = 1;
1008 (void) ngx_http_discard_request_body(r);
1009 ngx_http_finalize_request(r, NGX_HTTP_REQUEST_ENTITY_TOO_LARGE);
1010 return NGX_OK;
1013 if (rc == NGX_DONE) {
1014 ngx_http_clear_location(r);
1016 r->headers_out.location = ngx_list_push(&r->headers_out.headers);
1017 if (r->headers_out.location == NULL) {
1018 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
1019 return NGX_OK;
1023 * we do not need to set the r->headers_out.location->hash and
1024 * r->headers_out.location->key fields
1027 if (r->args.len == 0) {
1028 r->headers_out.location->value = clcf->name;
1030 } else {
1031 len = clcf->name.len + 1 + r->args.len;
1032 p = ngx_pnalloc(r->pool, len);
1034 if (p == NULL) {
1035 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
1036 return NGX_OK;
1039 r->headers_out.location->value.len = len;
1040 r->headers_out.location->value.data = p;
1042 p = ngx_cpymem(p, clcf->name.data, clcf->name.len);
1043 *p++ = '?';
1044 ngx_memcpy(p, r->args.data, r->args.len);
1047 ngx_http_finalize_request(r, NGX_HTTP_MOVED_PERMANENTLY);
1048 return NGX_OK;
1051 r->phase_handler++;
1052 return NGX_AGAIN;
1056 ngx_int_t
1057 ngx_http_core_post_rewrite_phase(ngx_http_request_t *r,
1058 ngx_http_phase_handler_t *ph)
1060 ngx_http_core_srv_conf_t *cscf;
1062 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1063 "post rewrite phase: %ui", r->phase_handler);
1065 if (!r->uri_changed) {
1066 r->phase_handler++;
1067 return NGX_AGAIN;
1070 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1071 "uri changes: %d", r->uri_changes);
1074 * gcc before 3.3 compiles the broken code for
1075 * if (r->uri_changes-- == 0)
1076 * if the r->uri_changes is defined as
1077 * unsigned uri_changes:4
1080 r->uri_changes--;
1082 if (r->uri_changes == 0) {
1083 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1084 "rewrite or internal redirection cycle "
1085 "while processing \"%V\"", &r->uri);
1087 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
1088 return NGX_OK;
1091 r->phase_handler = ph->next;
1093 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
1094 r->loc_conf = cscf->ctx->loc_conf;
1096 return NGX_AGAIN;
1100 ngx_int_t
1101 ngx_http_core_access_phase(ngx_http_request_t *r, ngx_http_phase_handler_t *ph)
1103 ngx_int_t rc;
1104 ngx_http_core_loc_conf_t *clcf;
1106 if (r != r->main) {
1107 r->phase_handler = ph->next;
1108 return NGX_AGAIN;
1111 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1112 "access phase: %ui", r->phase_handler);
1114 rc = ph->handler(r);
1116 if (rc == NGX_DECLINED) {
1117 r->phase_handler++;
1118 return NGX_AGAIN;
1121 if (rc == NGX_AGAIN || rc == NGX_DONE) {
1122 return NGX_OK;
1125 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1127 if (clcf->satisfy == NGX_HTTP_SATISFY_ALL) {
1129 if (rc == NGX_OK) {
1130 r->phase_handler++;
1131 return NGX_AGAIN;
1134 } else {
1135 if (rc == NGX_OK) {
1136 r->access_code = 0;
1138 if (r->headers_out.www_authenticate) {
1139 r->headers_out.www_authenticate->hash = 0;
1142 r->phase_handler = ph->next;
1143 return NGX_AGAIN;
1146 if (rc == NGX_HTTP_FORBIDDEN || rc == NGX_HTTP_UNAUTHORIZED) {
1147 r->access_code = rc;
1149 r->phase_handler++;
1150 return NGX_AGAIN;
1154 /* rc == NGX_ERROR || rc == NGX_HTTP_... */
1156 ngx_http_finalize_request(r, rc);
1157 return NGX_OK;
1161 ngx_int_t
1162 ngx_http_core_post_access_phase(ngx_http_request_t *r,
1163 ngx_http_phase_handler_t *ph)
1165 ngx_int_t access_code;
1167 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1168 "post access phase: %ui", r->phase_handler);
1170 access_code = r->access_code;
1172 if (access_code) {
1173 if (access_code == NGX_HTTP_FORBIDDEN) {
1174 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1175 "access forbidden by rule");
1178 r->access_code = 0;
1179 ngx_http_finalize_request(r, access_code);
1180 return NGX_OK;
1183 r->phase_handler++;
1184 return NGX_AGAIN;
1188 ngx_int_t
1189 ngx_http_core_try_files_phase(ngx_http_request_t *r,
1190 ngx_http_phase_handler_t *ph)
1192 size_t len, root, alias, reserve, allocated;
1193 u_char *p, *name;
1194 ngx_str_t path, args;
1195 ngx_uint_t test_dir;
1196 ngx_http_try_file_t *tf;
1197 ngx_open_file_info_t of;
1198 ngx_http_script_code_pt code;
1199 ngx_http_script_engine_t e;
1200 ngx_http_core_loc_conf_t *clcf;
1201 ngx_http_script_len_code_pt lcode;
1203 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1204 "try files phase: %ui", r->phase_handler);
1206 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1208 if (clcf->try_files == NULL) {
1209 r->phase_handler++;
1210 return NGX_AGAIN;
1213 allocated = 0;
1214 root = 0;
1215 name = NULL;
1216 /* suppress MSVC warning */
1217 path.data = NULL;
1219 tf = clcf->try_files;
1221 alias = clcf->alias;
1223 for ( ;; ) {
1225 if (tf->lengths) {
1226 ngx_memzero(&e, sizeof(ngx_http_script_engine_t));
1228 e.ip = tf->lengths->elts;
1229 e.request = r;
1231 /* 1 is for terminating '\0' as in static names */
1232 len = 1;
1234 while (*(uintptr_t *) e.ip) {
1235 lcode = *(ngx_http_script_len_code_pt *) e.ip;
1236 len += lcode(&e);
1239 } else {
1240 len = tf->name.len;
1243 if (!alias) {
1244 reserve = len > r->uri.len ? len - r->uri.len : 0;
1246 #if (NGX_PCRE)
1247 } else if (clcf->regex) {
1248 reserve = len;
1249 #endif
1251 } else {
1252 reserve = len > r->uri.len - alias ? len - (r->uri.len - alias) : 0;
1255 if (reserve > allocated || !allocated) {
1257 /* 16 bytes are preallocation */
1258 allocated = reserve + 16;
1260 if (ngx_http_map_uri_to_path(r, &path, &root, allocated) == NULL) {
1261 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
1262 return NGX_OK;
1265 name = path.data + root;
1268 if (tf->values == NULL) {
1270 /* tf->name.len includes the terminating '\0' */
1272 ngx_memcpy(name, tf->name.data, tf->name.len);
1274 path.len = (name + tf->name.len - 1) - path.data;
1276 } else {
1277 e.ip = tf->values->elts;
1278 e.pos = name;
1279 e.flushed = 1;
1281 while (*(uintptr_t *) e.ip) {
1282 code = *(ngx_http_script_code_pt *) e.ip;
1283 code((ngx_http_script_engine_t *) &e);
1286 path.len = e.pos - path.data;
1288 *e.pos = '\0';
1290 if (alias && ngx_strncmp(name, clcf->name.data, alias) == 0) {
1291 ngx_memmove(name, name + alias, len - alias);
1292 path.len -= alias;
1296 test_dir = tf->test_dir;
1298 tf++;
1300 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1301 "trying to use %s: \"%s\" \"%s\"",
1302 test_dir ? "dir" : "file", name, path.data);
1304 if (tf->lengths == NULL && tf->name.len == 0) {
1306 if (tf->code) {
1307 ngx_http_finalize_request(r, tf->code);
1308 return NGX_OK;
1311 path.len -= root;
1312 path.data += root;
1314 if (path.data[0] == '@') {
1315 (void) ngx_http_named_location(r, &path);
1317 } else {
1318 ngx_http_split_args(r, &path, &args);
1320 (void) ngx_http_internal_redirect(r, &path, &args);
1323 ngx_http_finalize_request(r, NGX_DONE);
1324 return NGX_OK;
1327 ngx_memzero(&of, sizeof(ngx_open_file_info_t));
1329 of.read_ahead = clcf->read_ahead;
1330 of.directio = clcf->directio;
1331 of.valid = clcf->open_file_cache_valid;
1332 of.min_uses = clcf->open_file_cache_min_uses;
1333 of.test_only = 1;
1334 of.errors = clcf->open_file_cache_errors;
1335 of.events = clcf->open_file_cache_events;
1337 if (ngx_http_set_disable_symlinks(r, clcf, &path, &of) != NGX_OK) {
1338 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
1339 return NGX_OK;
1342 if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool)
1343 != NGX_OK)
1345 if (of.err != NGX_ENOENT
1346 && of.err != NGX_ENOTDIR
1347 && of.err != NGX_ENAMETOOLONG)
1349 ngx_log_error(NGX_LOG_CRIT, r->connection->log, of.err,
1350 "%s \"%s\" failed", of.failed, path.data);
1353 continue;
1356 if (of.is_dir && !test_dir) {
1357 continue;
1360 path.len -= root;
1361 path.data += root;
1363 if (!alias) {
1364 r->uri = path;
1366 #if (NGX_PCRE)
1367 } else if (clcf->regex) {
1368 if (!test_dir) {
1369 r->uri = path;
1370 r->add_uri_to_alias = 1;
1372 #endif
1373 } else {
1374 r->uri.len = alias + path.len;
1375 r->uri.data = ngx_pnalloc(r->pool, r->uri.len);
1376 if (r->uri.data == NULL) {
1377 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
1378 return NGX_OK;
1381 p = ngx_copy(r->uri.data, clcf->name.data, alias);
1382 ngx_memcpy(p, name, path.len);
1385 ngx_http_set_exten(r);
1387 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1388 "try file uri: \"%V\"", &r->uri);
1390 r->phase_handler++;
1391 return NGX_AGAIN;
1394 /* not reached */
1398 ngx_int_t
1399 ngx_http_core_content_phase(ngx_http_request_t *r,
1400 ngx_http_phase_handler_t *ph)
1402 size_t root;
1403 ngx_int_t rc;
1404 ngx_str_t path;
1406 if (r->content_handler) {
1407 r->write_event_handler = ngx_http_request_empty_handler;
1408 ngx_http_finalize_request(r, r->content_handler(r));
1409 return NGX_OK;
1412 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1413 "content phase: %ui", r->phase_handler);
1415 rc = ph->handler(r);
1417 if (rc != NGX_DECLINED) {
1418 ngx_http_finalize_request(r, rc);
1419 return NGX_OK;
1422 /* rc == NGX_DECLINED */
1424 ph++;
1426 if (ph->checker) {
1427 r->phase_handler++;
1428 return NGX_AGAIN;
1431 /* no content handler was found */
1433 if (r->uri.data[r->uri.len - 1] == '/') {
1435 if (ngx_http_map_uri_to_path(r, &path, &root, 0) != NULL) {
1436 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1437 "directory index of \"%s\" is forbidden", path.data);
1440 ngx_http_finalize_request(r, NGX_HTTP_FORBIDDEN);
1441 return NGX_OK;
1444 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "no handler found");
1446 ngx_http_finalize_request(r, NGX_HTTP_NOT_FOUND);
1447 return NGX_OK;
1451 void
1452 ngx_http_update_location_config(ngx_http_request_t *r)
1454 ngx_http_core_loc_conf_t *clcf;
1456 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1458 if (r->method & clcf->limit_except) {
1459 r->loc_conf = clcf->limit_except_loc_conf;
1460 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1463 if (r == r->main) {
1464 ngx_http_set_connection_log(r->connection, clcf->error_log);
1467 if ((ngx_io.flags & NGX_IO_SENDFILE) && clcf->sendfile) {
1468 r->connection->sendfile = 1;
1470 } else {
1471 r->connection->sendfile = 0;
1474 if (clcf->client_body_in_file_only) {
1475 r->request_body_in_file_only = 1;
1476 r->request_body_in_persistent_file = 1;
1477 r->request_body_in_clean_file =
1478 clcf->client_body_in_file_only == NGX_HTTP_REQUEST_BODY_FILE_CLEAN;
1479 r->request_body_file_log_level = NGX_LOG_NOTICE;
1481 } else {
1482 r->request_body_file_log_level = NGX_LOG_WARN;
1485 r->request_body_in_single_buf = clcf->client_body_in_single_buffer;
1487 if (r->keepalive) {
1488 if (clcf->keepalive_timeout == 0) {
1489 r->keepalive = 0;
1491 } else if (r->connection->requests >= clcf->keepalive_requests) {
1492 r->keepalive = 0;
1494 } else if (r->headers_in.msie6
1495 && r->method == NGX_HTTP_POST
1496 && (clcf->keepalive_disable
1497 & NGX_HTTP_KEEPALIVE_DISABLE_MSIE6))
1500 * MSIE may wait for some time if an response for
1501 * a POST request was sent over a keepalive connection
1503 r->keepalive = 0;
1505 } else if (r->headers_in.safari
1506 && (clcf->keepalive_disable
1507 & NGX_HTTP_KEEPALIVE_DISABLE_SAFARI))
1510 * Safari may send a POST request to a closed keepalive
1511 * connection and may stall for some time, see
1512 * https://bugs.webkit.org/show_bug.cgi?id=5760
1514 r->keepalive = 0;
1518 if (!clcf->tcp_nopush) {
1519 /* disable TCP_NOPUSH/TCP_CORK use */
1520 r->connection->tcp_nopush = NGX_TCP_NOPUSH_DISABLED;
1523 if (r->limit_rate == 0) {
1524 r->limit_rate = clcf->limit_rate;
1527 if (clcf->handler) {
1528 r->content_handler = clcf->handler;
1534 * NGX_OK - exact or regex match
1535 * NGX_DONE - auto redirect
1536 * NGX_AGAIN - inclusive match
1537 * NGX_ERROR - regex error
1538 * NGX_DECLINED - no match
1541 static ngx_int_t
1542 ngx_http_core_find_location(ngx_http_request_t *r)
1544 ngx_int_t rc;
1545 ngx_http_core_loc_conf_t *pclcf;
1546 #if (NGX_PCRE)
1547 ngx_int_t n;
1548 ngx_uint_t noregex;
1549 ngx_http_core_loc_conf_t *clcf, **clcfp;
1551 noregex = 0;
1552 #endif
1554 pclcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1556 rc = ngx_http_core_find_static_location(r, pclcf->static_locations);
1558 if (rc == NGX_AGAIN) {
1560 #if (NGX_PCRE)
1561 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1563 noregex = clcf->noregex;
1564 #endif
1566 /* look up nested locations */
1568 rc = ngx_http_core_find_location(r);
1571 if (rc == NGX_OK || rc == NGX_DONE) {
1572 return rc;
1575 /* rc == NGX_DECLINED or rc == NGX_AGAIN in nested location */
1577 #if (NGX_PCRE)
1579 if (noregex == 0 && pclcf->regex_locations) {
1581 for (clcfp = pclcf->regex_locations; *clcfp; clcfp++) {
1583 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1584 "test location: ~ \"%V\"", &(*clcfp)->name);
1586 n = ngx_http_regex_exec(r, (*clcfp)->regex, &r->uri);
1588 if (n == NGX_OK) {
1589 r->loc_conf = (*clcfp)->loc_conf;
1591 /* look up nested locations */
1593 rc = ngx_http_core_find_location(r);
1595 return (rc == NGX_ERROR) ? rc : NGX_OK;
1598 if (n == NGX_DECLINED) {
1599 continue;
1602 return NGX_ERROR;
1605 #endif
1607 return rc;
1612 * NGX_OK - exact match
1613 * NGX_DONE - auto redirect
1614 * NGX_AGAIN - inclusive match
1615 * NGX_DECLINED - no match
1618 static ngx_int_t
1619 ngx_http_core_find_static_location(ngx_http_request_t *r,
1620 ngx_http_location_tree_node_t *node)
1622 u_char *uri;
1623 size_t len, n;
1624 ngx_int_t rc, rv;
1626 len = r->uri.len;
1627 uri = r->uri.data;
1629 rv = NGX_DECLINED;
1631 for ( ;; ) {
1633 if (node == NULL) {
1634 return rv;
1637 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1638 "test location: \"%*s\"", node->len, node->name);
1640 n = (len <= (size_t) node->len) ? len : node->len;
1642 rc = ngx_filename_cmp(uri, node->name, n);
1644 if (rc != 0) {
1645 node = (rc < 0) ? node->left : node->right;
1647 continue;
1650 if (len > (size_t) node->len) {
1652 if (node->inclusive) {
1654 r->loc_conf = node->inclusive->loc_conf;
1655 rv = NGX_AGAIN;
1657 node = node->tree;
1658 uri += n;
1659 len -= n;
1661 continue;
1664 /* exact only */
1666 node = node->right;
1668 continue;
1671 if (len == (size_t) node->len) {
1673 if (node->exact) {
1674 r->loc_conf = node->exact->loc_conf;
1675 return NGX_OK;
1677 } else {
1678 r->loc_conf = node->inclusive->loc_conf;
1679 return NGX_AGAIN;
1683 /* len < node->len */
1685 if (len + 1 == (size_t) node->len && node->auto_redirect) {
1687 r->loc_conf = (node->exact) ? node->exact->loc_conf:
1688 node->inclusive->loc_conf;
1689 rv = NGX_DONE;
1692 node = node->left;
1697 void *
1698 ngx_http_test_content_type(ngx_http_request_t *r, ngx_hash_t *types_hash)
1700 u_char c, *lowcase;
1701 size_t len;
1702 ngx_uint_t i, hash;
1704 if (types_hash->size == 0) {
1705 return (void *) 4;
1708 if (r->headers_out.content_type.len == 0) {
1709 return NULL;
1712 len = r->headers_out.content_type_len;
1714 if (r->headers_out.content_type_lowcase == NULL) {
1716 lowcase = ngx_pnalloc(r->pool, len);
1717 if (lowcase == NULL) {
1718 return NULL;
1721 r->headers_out.content_type_lowcase = lowcase;
1723 hash = 0;
1725 for (i = 0; i < len; i++) {
1726 c = ngx_tolower(r->headers_out.content_type.data[i]);
1727 hash = ngx_hash(hash, c);
1728 lowcase[i] = c;
1731 r->headers_out.content_type_hash = hash;
1734 return ngx_hash_find(types_hash, r->headers_out.content_type_hash,
1735 r->headers_out.content_type_lowcase, len);
1739 ngx_int_t
1740 ngx_http_set_content_type(ngx_http_request_t *r)
1742 u_char c, *exten;
1743 ngx_str_t *type;
1744 ngx_uint_t i, hash;
1745 ngx_http_core_loc_conf_t *clcf;
1747 if (r->headers_out.content_type.len) {
1748 return NGX_OK;
1751 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1753 if (r->exten.len) {
1755 hash = 0;
1757 for (i = 0; i < r->exten.len; i++) {
1758 c = r->exten.data[i];
1760 if (c >= 'A' && c <= 'Z') {
1762 exten = ngx_pnalloc(r->pool, r->exten.len);
1763 if (exten == NULL) {
1764 return NGX_ERROR;
1767 hash = ngx_hash_strlow(exten, r->exten.data, r->exten.len);
1769 r->exten.data = exten;
1771 break;
1774 hash = ngx_hash(hash, c);
1777 type = ngx_hash_find(&clcf->types_hash, hash,
1778 r->exten.data, r->exten.len);
1780 if (type) {
1781 r->headers_out.content_type_len = type->len;
1782 r->headers_out.content_type = *type;
1784 return NGX_OK;
1788 r->headers_out.content_type_len = clcf->default_type.len;
1789 r->headers_out.content_type = clcf->default_type;
1791 return NGX_OK;
1795 void
1796 ngx_http_set_exten(ngx_http_request_t *r)
1798 ngx_int_t i;
1800 ngx_str_null(&r->exten);
1802 for (i = r->uri.len - 1; i > 1; i--) {
1803 if (r->uri.data[i] == '.' && r->uri.data[i - 1] != '/') {
1805 r->exten.len = r->uri.len - i - 1;
1806 r->exten.data = &r->uri.data[i + 1];
1808 return;
1810 } else if (r->uri.data[i] == '/') {
1811 return;
1815 return;
1819 ngx_int_t
1820 ngx_http_set_etag(ngx_http_request_t *r)
1822 ngx_table_elt_t *etag;
1823 ngx_http_core_loc_conf_t *clcf;
1825 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1827 if (!clcf->etag) {
1828 return NGX_OK;
1831 etag = ngx_list_push(&r->headers_out.headers);
1832 if (etag == NULL) {
1833 return NGX_ERROR;
1836 etag->hash = 1;
1837 ngx_str_set(&etag->key, "ETag");
1839 etag->value.data = ngx_pnalloc(r->pool, NGX_OFF_T_LEN + NGX_TIME_T_LEN + 3);
1840 if (etag->value.data == NULL) {
1841 return NGX_ERROR;
1844 etag->value.len = ngx_sprintf(etag->value.data, "\"%xT-%xO\"",
1845 r->headers_out.last_modified_time,
1846 r->headers_out.content_length_n)
1847 - etag->value.data;
1849 r->headers_out.etag = etag;
1851 return NGX_OK;
1855 ngx_int_t
1856 ngx_http_send_response(ngx_http_request_t *r, ngx_uint_t status,
1857 ngx_str_t *ct, ngx_http_complex_value_t *cv)
1859 ngx_int_t rc;
1860 ngx_str_t val;
1861 ngx_buf_t *b;
1862 ngx_chain_t out;
1864 if (ngx_http_discard_request_body(r) != NGX_OK) {
1865 return NGX_HTTP_INTERNAL_SERVER_ERROR;
1868 r->headers_out.status = status;
1870 if (ngx_http_complex_value(r, cv, &val) != NGX_OK) {
1871 return NGX_HTTP_INTERNAL_SERVER_ERROR;
1874 if (status == NGX_HTTP_MOVED_PERMANENTLY
1875 || status == NGX_HTTP_MOVED_TEMPORARILY
1876 || status == NGX_HTTP_SEE_OTHER
1877 || status == NGX_HTTP_TEMPORARY_REDIRECT)
1879 ngx_http_clear_location(r);
1881 r->headers_out.location = ngx_list_push(&r->headers_out.headers);
1882 if (r->headers_out.location == NULL) {
1883 return NGX_HTTP_INTERNAL_SERVER_ERROR;
1886 r->headers_out.location->hash = 1;
1887 ngx_str_set(&r->headers_out.location->key, "Location");
1888 r->headers_out.location->value = val;
1890 return status;
1893 r->headers_out.content_length_n = val.len;
1895 if (ct) {
1896 r->headers_out.content_type_len = ct->len;
1897 r->headers_out.content_type = *ct;
1899 } else {
1900 if (ngx_http_set_content_type(r) != NGX_OK) {
1901 return NGX_HTTP_INTERNAL_SERVER_ERROR;
1905 if (r->method == NGX_HTTP_HEAD || (r != r->main && val.len == 0)) {
1906 return ngx_http_send_header(r);
1909 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
1910 if (b == NULL) {
1911 return NGX_HTTP_INTERNAL_SERVER_ERROR;
1914 b->pos = val.data;
1915 b->last = val.data + val.len;
1916 b->memory = val.len ? 1 : 0;
1917 b->last_buf = (r == r->main) ? 1 : 0;
1918 b->last_in_chain = 1;
1920 out.buf = b;
1921 out.next = NULL;
1923 rc = ngx_http_send_header(r);
1925 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
1926 return rc;
1929 return ngx_http_output_filter(r, &out);
1933 ngx_int_t
1934 ngx_http_send_header(ngx_http_request_t *r)
1936 if (r->err_status) {
1937 r->headers_out.status = r->err_status;
1938 r->headers_out.status_line.len = 0;
1941 return ngx_http_top_header_filter(r);
1945 ngx_int_t
1946 ngx_http_output_filter(ngx_http_request_t *r, ngx_chain_t *in)
1948 ngx_int_t rc;
1949 ngx_connection_t *c;
1951 c = r->connection;
1953 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
1954 "http output filter \"%V?%V\"", &r->uri, &r->args);
1956 rc = ngx_http_top_body_filter(r, in);
1958 if (rc == NGX_ERROR) {
1959 /* NGX_ERROR may be returned by any filter */
1960 c->error = 1;
1963 return rc;
1967 u_char *
1968 ngx_http_map_uri_to_path(ngx_http_request_t *r, ngx_str_t *path,
1969 size_t *root_length, size_t reserved)
1971 u_char *last;
1972 size_t alias;
1973 ngx_http_core_loc_conf_t *clcf;
1975 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1977 alias = clcf->alias;
1979 if (alias && !r->valid_location) {
1980 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
1981 "\"alias\" cannot be used in location \"%V\" "
1982 "where URI was rewritten", &clcf->name);
1983 return NULL;
1986 if (clcf->root_lengths == NULL) {
1988 *root_length = clcf->root.len;
1990 path->len = clcf->root.len + reserved + r->uri.len - alias + 1;
1992 path->data = ngx_pnalloc(r->pool, path->len);
1993 if (path->data == NULL) {
1994 return NULL;
1997 last = ngx_copy(path->data, clcf->root.data, clcf->root.len);
1999 } else {
2001 #if (NGX_PCRE)
2002 ngx_uint_t captures;
2004 captures = alias && clcf->regex;
2006 reserved += captures ? r->add_uri_to_alias ? r->uri.len + 1 : 1
2007 : r->uri.len - alias + 1;
2008 #else
2009 reserved += r->uri.len - alias + 1;
2010 #endif
2012 if (ngx_http_script_run(r, path, clcf->root_lengths->elts, reserved,
2013 clcf->root_values->elts)
2014 == NULL)
2016 return NULL;
2019 if (ngx_conf_full_name((ngx_cycle_t *) ngx_cycle, path, 0) != NGX_OK) {
2020 return NULL;
2023 *root_length = path->len - reserved;
2024 last = path->data + *root_length;
2026 #if (NGX_PCRE)
2027 if (captures) {
2028 if (!r->add_uri_to_alias) {
2029 *last = '\0';
2030 return last;
2033 alias = 0;
2035 #endif
2038 last = ngx_cpystrn(last, r->uri.data + alias, r->uri.len - alias + 1);
2040 return last;
2044 ngx_int_t
2045 ngx_http_auth_basic_user(ngx_http_request_t *r)
2047 ngx_str_t auth, encoded;
2048 ngx_uint_t len;
2050 if (r->headers_in.user.len == 0 && r->headers_in.user.data != NULL) {
2051 return NGX_DECLINED;
2054 if (r->headers_in.authorization == NULL) {
2055 r->headers_in.user.data = (u_char *) "";
2056 return NGX_DECLINED;
2059 encoded = r->headers_in.authorization->value;
2061 if (encoded.len < sizeof("Basic ") - 1
2062 || ngx_strncasecmp(encoded.data, (u_char *) "Basic ",
2063 sizeof("Basic ") - 1)
2064 != 0)
2066 r->headers_in.user.data = (u_char *) "";
2067 return NGX_DECLINED;
2070 encoded.len -= sizeof("Basic ") - 1;
2071 encoded.data += sizeof("Basic ") - 1;
2073 while (encoded.len && encoded.data[0] == ' ') {
2074 encoded.len--;
2075 encoded.data++;
2078 if (encoded.len == 0) {
2079 r->headers_in.user.data = (u_char *) "";
2080 return NGX_DECLINED;
2083 auth.len = ngx_base64_decoded_length(encoded.len);
2084 auth.data = ngx_pnalloc(r->pool, auth.len + 1);
2085 if (auth.data == NULL) {
2086 return NGX_ERROR;
2089 if (ngx_decode_base64(&auth, &encoded) != NGX_OK) {
2090 r->headers_in.user.data = (u_char *) "";
2091 return NGX_DECLINED;
2094 auth.data[auth.len] = '\0';
2096 for (len = 0; len < auth.len; len++) {
2097 if (auth.data[len] == ':') {
2098 break;
2102 if (len == 0 || len == auth.len) {
2103 r->headers_in.user.data = (u_char *) "";
2104 return NGX_DECLINED;
2107 r->headers_in.user.len = len;
2108 r->headers_in.user.data = auth.data;
2109 r->headers_in.passwd.len = auth.len - len - 1;
2110 r->headers_in.passwd.data = &auth.data[len + 1];
2112 return NGX_OK;
2116 #if (NGX_HTTP_GZIP)
2118 ngx_int_t
2119 ngx_http_gzip_ok(ngx_http_request_t *r)
2121 time_t date, expires;
2122 ngx_uint_t p;
2123 ngx_array_t *cc;
2124 ngx_table_elt_t *e, *d, *ae;
2125 ngx_http_core_loc_conf_t *clcf;
2127 r->gzip_tested = 1;
2129 if (r != r->main) {
2130 return NGX_DECLINED;
2133 #if (NGX_HTTP_SPDY)
2134 if (r->spdy_stream) {
2135 r->gzip_ok = 1;
2136 return NGX_OK;
2138 #endif
2140 ae = r->headers_in.accept_encoding;
2141 if (ae == NULL) {
2142 return NGX_DECLINED;
2145 if (ae->value.len < sizeof("gzip") - 1) {
2146 return NGX_DECLINED;
2150 * test first for the most common case "gzip,...":
2151 * MSIE: "gzip, deflate"
2152 * Firefox: "gzip,deflate"
2153 * Chrome: "gzip,deflate,sdch"
2154 * Safari: "gzip, deflate"
2155 * Opera: "gzip, deflate"
2158 if (ngx_memcmp(ae->value.data, "gzip,", 5) != 0
2159 && ngx_http_gzip_accept_encoding(&ae->value) != NGX_OK)
2161 return NGX_DECLINED;
2164 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
2166 if (r->headers_in.msie6 && clcf->gzip_disable_msie6) {
2167 return NGX_DECLINED;
2170 if (r->http_version < clcf->gzip_http_version) {
2171 return NGX_DECLINED;
2174 if (r->headers_in.via == NULL) {
2175 goto ok;
2178 p = clcf->gzip_proxied;
2180 if (p & NGX_HTTP_GZIP_PROXIED_OFF) {
2181 return NGX_DECLINED;
2184 if (p & NGX_HTTP_GZIP_PROXIED_ANY) {
2185 goto ok;
2188 if (r->headers_in.authorization && (p & NGX_HTTP_GZIP_PROXIED_AUTH)) {
2189 goto ok;
2192 e = r->headers_out.expires;
2194 if (e) {
2196 if (!(p & NGX_HTTP_GZIP_PROXIED_EXPIRED)) {
2197 return NGX_DECLINED;
2200 expires = ngx_http_parse_time(e->value.data, e->value.len);
2201 if (expires == NGX_ERROR) {
2202 return NGX_DECLINED;
2205 d = r->headers_out.date;
2207 if (d) {
2208 date = ngx_http_parse_time(d->value.data, d->value.len);
2209 if (date == NGX_ERROR) {
2210 return NGX_DECLINED;
2213 } else {
2214 date = ngx_time();
2217 if (expires < date) {
2218 goto ok;
2221 return NGX_DECLINED;
2224 cc = &r->headers_out.cache_control;
2226 if (cc->elts) {
2228 if ((p & NGX_HTTP_GZIP_PROXIED_NO_CACHE)
2229 && ngx_http_parse_multi_header_lines(cc, &ngx_http_gzip_no_cache,
2230 NULL)
2231 >= 0)
2233 goto ok;
2236 if ((p & NGX_HTTP_GZIP_PROXIED_NO_STORE)
2237 && ngx_http_parse_multi_header_lines(cc, &ngx_http_gzip_no_store,
2238 NULL)
2239 >= 0)
2241 goto ok;
2244 if ((p & NGX_HTTP_GZIP_PROXIED_PRIVATE)
2245 && ngx_http_parse_multi_header_lines(cc, &ngx_http_gzip_private,
2246 NULL)
2247 >= 0)
2249 goto ok;
2252 return NGX_DECLINED;
2255 if ((p & NGX_HTTP_GZIP_PROXIED_NO_LM) && r->headers_out.last_modified) {
2256 return NGX_DECLINED;
2259 if ((p & NGX_HTTP_GZIP_PROXIED_NO_ETAG) && r->headers_out.etag) {
2260 return NGX_DECLINED;
2265 #if (NGX_PCRE)
2267 if (clcf->gzip_disable && r->headers_in.user_agent) {
2269 if (ngx_regex_exec_array(clcf->gzip_disable,
2270 &r->headers_in.user_agent->value,
2271 r->connection->log)
2272 != NGX_DECLINED)
2274 return NGX_DECLINED;
2278 #endif
2280 r->gzip_ok = 1;
2282 return NGX_OK;
2287 * gzip is enabled for the following quantities:
2288 * "gzip; q=0.001" ... "gzip; q=1.000"
2289 * gzip is disabled for the following quantities:
2290 * "gzip; q=0" ... "gzip; q=0.000", and for any invalid cases
2293 static ngx_int_t
2294 ngx_http_gzip_accept_encoding(ngx_str_t *ae)
2296 u_char *p, *start, *last;
2298 start = ae->data;
2299 last = start + ae->len;
2301 for ( ;; ) {
2302 p = ngx_strcasestrn(start, "gzip", 4 - 1);
2303 if (p == NULL) {
2304 return NGX_DECLINED;
2307 if (p == start || (*(p - 1) == ',' || *(p - 1) == ' ')) {
2308 break;
2311 start = p + 4;
2314 p += 4;
2316 while (p < last) {
2317 switch(*p++) {
2318 case ',':
2319 return NGX_OK;
2320 case ';':
2321 goto quantity;
2322 case ' ':
2323 continue;
2324 default:
2325 return NGX_DECLINED;
2329 return NGX_OK;
2331 quantity:
2333 while (p < last) {
2334 switch(*p++) {
2335 case 'q':
2336 case 'Q':
2337 goto equal;
2338 case ' ':
2339 continue;
2340 default:
2341 return NGX_DECLINED;
2345 return NGX_OK;
2347 equal:
2349 if (p + 2 > last || *p++ != '=') {
2350 return NGX_DECLINED;
2353 if (ngx_http_gzip_quantity(p, last) == 0) {
2354 return NGX_DECLINED;
2357 return NGX_OK;
2361 ngx_uint_t
2362 ngx_http_gzip_quantity(u_char *p, u_char *last)
2364 u_char c;
2365 ngx_uint_t n, q;
2367 c = *p++;
2369 if (c != '0' && c != '1') {
2370 return 0;
2373 q = (c - '0') * 100;
2375 if (p == last) {
2376 return q;
2379 c = *p++;
2381 if (c == ',' || c == ' ') {
2382 return q;
2385 if (c != '.') {
2386 return 0;
2389 n = 0;
2391 while (p < last) {
2392 c = *p++;
2394 if (c == ',' || c == ' ') {
2395 break;
2398 if (c >= '0' && c <= '9') {
2399 q += c - '0';
2400 n++;
2401 continue;
2404 return 0;
2407 if (q > 100 || n > 3) {
2408 return 0;
2411 return q;
2414 #endif
2417 ngx_int_t
2418 ngx_http_subrequest(ngx_http_request_t *r,
2419 ngx_str_t *uri, ngx_str_t *args, ngx_http_request_t **psr,
2420 ngx_http_post_subrequest_t *ps, ngx_uint_t flags)
2422 ngx_time_t *tp;
2423 ngx_connection_t *c;
2424 ngx_http_request_t *sr;
2425 ngx_http_core_srv_conf_t *cscf;
2426 ngx_http_postponed_request_t *pr, *p;
2428 r->main->subrequests--;
2430 if (r->main->subrequests == 0) {
2431 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
2432 "subrequests cycle while processing \"%V\"", uri);
2433 r->main->subrequests = 1;
2434 return NGX_ERROR;
2437 sr = ngx_pcalloc(r->pool, sizeof(ngx_http_request_t));
2438 if (sr == NULL) {
2439 return NGX_ERROR;
2442 sr->signature = NGX_HTTP_MODULE;
2444 c = r->connection;
2445 sr->connection = c;
2447 sr->ctx = ngx_pcalloc(r->pool, sizeof(void *) * ngx_http_max_module);
2448 if (sr->ctx == NULL) {
2449 return NGX_ERROR;
2452 if (ngx_list_init(&sr->headers_out.headers, r->pool, 20,
2453 sizeof(ngx_table_elt_t))
2454 != NGX_OK)
2456 return NGX_ERROR;
2459 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
2460 sr->main_conf = cscf->ctx->main_conf;
2461 sr->srv_conf = cscf->ctx->srv_conf;
2462 sr->loc_conf = cscf->ctx->loc_conf;
2464 sr->pool = r->pool;
2466 sr->headers_in = r->headers_in;
2468 ngx_http_clear_content_length(sr);
2469 ngx_http_clear_accept_ranges(sr);
2470 ngx_http_clear_last_modified(sr);
2472 sr->request_body = r->request_body;
2474 #if (NGX_HTTP_SPDY)
2475 sr->spdy_stream = r->spdy_stream;
2476 #endif
2478 sr->method = NGX_HTTP_GET;
2479 sr->http_version = r->http_version;
2481 sr->request_line = r->request_line;
2482 sr->uri = *uri;
2484 if (args) {
2485 sr->args = *args;
2488 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
2489 "http subrequest \"%V?%V\"", uri, &sr->args);
2491 sr->subrequest_in_memory = (flags & NGX_HTTP_SUBREQUEST_IN_MEMORY) != 0;
2492 sr->waited = (flags & NGX_HTTP_SUBREQUEST_WAITED) != 0;
2494 sr->unparsed_uri = r->unparsed_uri;
2495 sr->method_name = ngx_http_core_get_method;
2496 sr->http_protocol = r->http_protocol;
2498 ngx_http_set_exten(sr);
2500 sr->main = r->main;
2501 sr->parent = r;
2502 sr->post_subrequest = ps;
2503 sr->read_event_handler = ngx_http_request_empty_handler;
2504 sr->write_event_handler = ngx_http_handler;
2506 if (c->data == r && r->postponed == NULL) {
2507 c->data = sr;
2510 sr->variables = r->variables;
2512 sr->log_handler = r->log_handler;
2514 pr = ngx_palloc(r->pool, sizeof(ngx_http_postponed_request_t));
2515 if (pr == NULL) {
2516 return NGX_ERROR;
2519 pr->request = sr;
2520 pr->out = NULL;
2521 pr->next = NULL;
2523 if (r->postponed) {
2524 for (p = r->postponed; p->next; p = p->next) { /* void */ }
2525 p->next = pr;
2527 } else {
2528 r->postponed = pr;
2531 sr->internal = 1;
2533 sr->discard_body = r->discard_body;
2534 sr->expect_tested = 1;
2535 sr->main_filter_need_in_memory = r->main_filter_need_in_memory;
2537 sr->uri_changes = NGX_HTTP_MAX_URI_CHANGES + 1;
2539 tp = ngx_timeofday();
2540 sr->start_sec = tp->sec;
2541 sr->start_msec = tp->msec;
2543 r->main->count++;
2545 *psr = sr;
2547 return ngx_http_post_request(sr, NULL);
2551 ngx_int_t
2552 ngx_http_internal_redirect(ngx_http_request_t *r,
2553 ngx_str_t *uri, ngx_str_t *args)
2555 ngx_http_core_srv_conf_t *cscf;
2557 r->uri_changes--;
2559 if (r->uri_changes == 0) {
2560 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
2561 "rewrite or internal redirection cycle "
2562 "while internally redirecting to \"%V\"", uri);
2564 r->main->count++;
2565 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
2566 return NGX_DONE;
2569 r->uri = *uri;
2571 if (args) {
2572 r->args = *args;
2574 } else {
2575 ngx_str_null(&r->args);
2578 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2579 "internal redirect: \"%V?%V\"", uri, &r->args);
2581 ngx_http_set_exten(r);
2583 /* clear the modules contexts */
2584 ngx_memzero(r->ctx, sizeof(void *) * ngx_http_max_module);
2586 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
2587 r->loc_conf = cscf->ctx->loc_conf;
2589 ngx_http_update_location_config(r);
2591 #if (NGX_HTTP_CACHE)
2592 r->cache = NULL;
2593 #endif
2595 r->internal = 1;
2596 r->valid_unparsed_uri = 0;
2597 r->add_uri_to_alias = 0;
2598 r->main->count++;
2600 ngx_http_handler(r);
2602 return NGX_DONE;
2606 ngx_int_t
2607 ngx_http_named_location(ngx_http_request_t *r, ngx_str_t *name)
2609 ngx_http_core_srv_conf_t *cscf;
2610 ngx_http_core_loc_conf_t **clcfp;
2611 ngx_http_core_main_conf_t *cmcf;
2613 r->main->count++;
2614 r->uri_changes--;
2616 if (r->uri_changes == 0) {
2617 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
2618 "rewrite or internal redirection cycle "
2619 "while redirect to named location \"%V\"", name);
2621 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
2622 return NGX_DONE;
2625 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
2627 if (cscf->named_locations) {
2629 for (clcfp = cscf->named_locations; *clcfp; clcfp++) {
2631 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2632 "test location: \"%V\"", &(*clcfp)->name);
2634 if (name->len != (*clcfp)->name.len
2635 || ngx_strncmp(name->data, (*clcfp)->name.data, name->len) != 0)
2637 continue;
2640 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2641 "using location: %V \"%V?%V\"",
2642 name, &r->uri, &r->args);
2644 r->internal = 1;
2645 r->content_handler = NULL;
2646 r->uri_changed = 0;
2647 r->loc_conf = (*clcfp)->loc_conf;
2649 /* clear the modules contexts */
2650 ngx_memzero(r->ctx, sizeof(void *) * ngx_http_max_module);
2652 ngx_http_update_location_config(r);
2654 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
2656 r->phase_handler = cmcf->phase_engine.location_rewrite_index;
2658 r->write_event_handler = ngx_http_core_run_phases;
2659 ngx_http_core_run_phases(r);
2661 return NGX_DONE;
2665 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
2666 "could not find named location \"%V\"", name);
2668 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
2670 return NGX_DONE;
2674 ngx_http_cleanup_t *
2675 ngx_http_cleanup_add(ngx_http_request_t *r, size_t size)
2677 ngx_http_cleanup_t *cln;
2679 r = r->main;
2681 cln = ngx_palloc(r->pool, sizeof(ngx_http_cleanup_t));
2682 if (cln == NULL) {
2683 return NULL;
2686 if (size) {
2687 cln->data = ngx_palloc(r->pool, size);
2688 if (cln->data == NULL) {
2689 return NULL;
2692 } else {
2693 cln->data = NULL;
2696 cln->handler = NULL;
2697 cln->next = r->cleanup;
2699 r->cleanup = cln;
2701 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2702 "http cleanup add: %p", cln);
2704 return cln;
2708 ngx_int_t
2709 ngx_http_set_disable_symlinks(ngx_http_request_t *r,
2710 ngx_http_core_loc_conf_t *clcf, ngx_str_t *path, ngx_open_file_info_t *of)
2712 #if (NGX_HAVE_OPENAT)
2713 u_char *p;
2714 ngx_str_t from;
2716 of->disable_symlinks = clcf->disable_symlinks;
2718 if (clcf->disable_symlinks_from == NULL) {
2719 return NGX_OK;
2722 if (ngx_http_complex_value(r, clcf->disable_symlinks_from, &from)
2723 != NGX_OK)
2725 return NGX_ERROR;
2728 if (from.len == 0
2729 || from.len > path->len
2730 || ngx_memcmp(path->data, from.data, from.len) != 0)
2732 return NGX_OK;
2735 if (from.len == path->len) {
2736 of->disable_symlinks = NGX_DISABLE_SYMLINKS_OFF;
2737 return NGX_OK;
2740 p = path->data + from.len;
2742 if (*p == '/') {
2743 of->disable_symlinks_from = from.len;
2744 return NGX_OK;
2747 p--;
2749 if (*p == '/') {
2750 of->disable_symlinks_from = from.len - 1;
2752 #endif
2754 return NGX_OK;
2758 ngx_int_t
2759 ngx_http_get_forwarded_addr(ngx_http_request_t *r, ngx_addr_t *addr,
2760 ngx_array_t *headers, ngx_str_t *value, ngx_array_t *proxies,
2761 int recursive)
2763 ngx_int_t rc;
2764 ngx_uint_t i, found;
2765 ngx_table_elt_t **h;
2767 if (headers == NULL) {
2768 return ngx_http_get_forwarded_addr_internal(r, addr, value->data,
2769 value->len, proxies,
2770 recursive);
2773 i = headers->nelts;
2774 h = headers->elts;
2776 rc = NGX_DECLINED;
2778 found = 0;
2780 while (i-- > 0) {
2781 rc = ngx_http_get_forwarded_addr_internal(r, addr, h[i]->value.data,
2782 h[i]->value.len, proxies,
2783 recursive);
2785 if (!recursive) {
2786 break;
2789 if (rc == NGX_DECLINED && found) {
2790 rc = NGX_DONE;
2791 break;
2794 if (rc != NGX_OK) {
2795 break;
2798 found = 1;
2801 return rc;
2805 static ngx_int_t
2806 ngx_http_get_forwarded_addr_internal(ngx_http_request_t *r, ngx_addr_t *addr,
2807 u_char *xff, size_t xfflen, ngx_array_t *proxies, int recursive)
2809 u_char *p;
2810 in_addr_t inaddr;
2811 ngx_int_t rc;
2812 ngx_addr_t paddr;
2813 ngx_cidr_t *cidr;
2814 ngx_uint_t family, i;
2815 #if (NGX_HAVE_INET6)
2816 ngx_uint_t n;
2817 struct in6_addr *inaddr6;
2818 #endif
2820 #if (NGX_SUPPRESS_WARN)
2821 inaddr = 0;
2822 #if (NGX_HAVE_INET6)
2823 inaddr6 = NULL;
2824 #endif
2825 #endif
2827 family = addr->sockaddr->sa_family;
2829 if (family == AF_INET) {
2830 inaddr = ((struct sockaddr_in *) addr->sockaddr)->sin_addr.s_addr;
2833 #if (NGX_HAVE_INET6)
2834 else if (family == AF_INET6) {
2835 inaddr6 = &((struct sockaddr_in6 *) addr->sockaddr)->sin6_addr;
2837 if (IN6_IS_ADDR_V4MAPPED(inaddr6)) {
2838 family = AF_INET;
2840 p = inaddr6->s6_addr;
2842 inaddr = p[12] << 24;
2843 inaddr += p[13] << 16;
2844 inaddr += p[14] << 8;
2845 inaddr += p[15];
2847 inaddr = htonl(inaddr);
2850 #endif
2852 for (cidr = proxies->elts, i = 0; i < proxies->nelts; i++) {
2853 if (cidr[i].family != family) {
2854 goto next;
2857 switch (family) {
2859 #if (NGX_HAVE_INET6)
2860 case AF_INET6:
2861 for (n = 0; n < 16; n++) {
2862 if ((inaddr6->s6_addr[n] & cidr[i].u.in6.mask.s6_addr[n])
2863 != cidr[i].u.in6.addr.s6_addr[n])
2865 goto next;
2868 break;
2869 #endif
2871 #if (NGX_HAVE_UNIX_DOMAIN)
2872 case AF_UNIX:
2873 break;
2874 #endif
2876 default: /* AF_INET */
2877 if ((inaddr & cidr[i].u.in.mask) != cidr[i].u.in.addr) {
2878 goto next;
2880 break;
2883 for (p = xff + xfflen - 1; p > xff; p--, xfflen--) {
2884 if (*p != ' ' && *p != ',') {
2885 break;
2889 for ( /* void */ ; p > xff; p--) {
2890 if (*p == ' ' || *p == ',') {
2891 p++;
2892 break;
2896 if (ngx_parse_addr(r->pool, &paddr, p, xfflen - (p - xff)) != NGX_OK) {
2897 return NGX_DECLINED;
2900 *addr = paddr;
2902 if (recursive && p > xff) {
2903 rc = ngx_http_get_forwarded_addr_internal(r, addr, xff, p - 1 - xff,
2904 proxies, 1);
2906 if (rc == NGX_DECLINED) {
2907 return NGX_DONE;
2910 /* rc == NGX_OK || rc == NGX_DONE */
2911 return rc;
2914 return NGX_OK;
2916 next:
2917 continue;
2920 return NGX_DECLINED;
2924 static char *
2925 ngx_http_core_server(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy)
2927 char *rv;
2928 void *mconf;
2929 ngx_uint_t i;
2930 ngx_conf_t pcf;
2931 ngx_http_module_t *module;
2932 struct sockaddr_in *sin;
2933 ngx_http_conf_ctx_t *ctx, *http_ctx;
2934 ngx_http_listen_opt_t lsopt;
2935 ngx_http_core_srv_conf_t *cscf, **cscfp;
2936 ngx_http_core_main_conf_t *cmcf;
2938 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
2939 if (ctx == NULL) {
2940 return NGX_CONF_ERROR;
2943 http_ctx = cf->ctx;
2944 ctx->main_conf = http_ctx->main_conf;
2946 /* the server{}'s srv_conf */
2948 ctx->srv_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
2949 if (ctx->srv_conf == NULL) {
2950 return NGX_CONF_ERROR;
2953 /* the server{}'s loc_conf */
2955 ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
2956 if (ctx->loc_conf == NULL) {
2957 return NGX_CONF_ERROR;
2960 for (i = 0; ngx_modules[i]; i++) {
2961 if (ngx_modules[i]->type != NGX_HTTP_MODULE) {
2962 continue;
2965 module = ngx_modules[i]->ctx;
2967 if (module->create_srv_conf) {
2968 mconf = module->create_srv_conf(cf);
2969 if (mconf == NULL) {
2970 return NGX_CONF_ERROR;
2973 ctx->srv_conf[ngx_modules[i]->ctx_index] = mconf;
2976 if (module->create_loc_conf) {
2977 mconf = module->create_loc_conf(cf);
2978 if (mconf == NULL) {
2979 return NGX_CONF_ERROR;
2982 ctx->loc_conf[ngx_modules[i]->ctx_index] = mconf;
2987 /* the server configuration context */
2989 cscf = ctx->srv_conf[ngx_http_core_module.ctx_index];
2990 cscf->ctx = ctx;
2993 cmcf = ctx->main_conf[ngx_http_core_module.ctx_index];
2995 cscfp = ngx_array_push(&cmcf->servers);
2996 if (cscfp == NULL) {
2997 return NGX_CONF_ERROR;
3000 *cscfp = cscf;
3003 /* parse inside server{} */
3005 pcf = *cf;
3006 cf->ctx = ctx;
3007 cf->cmd_type = NGX_HTTP_SRV_CONF;
3009 rv = ngx_conf_parse(cf, NULL);
3011 *cf = pcf;
3013 if (rv == NGX_CONF_OK && !cscf->listen) {
3014 ngx_memzero(&lsopt, sizeof(ngx_http_listen_opt_t));
3016 sin = &lsopt.u.sockaddr_in;
3018 sin->sin_family = AF_INET;
3019 #if (NGX_WIN32)
3020 sin->sin_port = htons(80);
3021 #else
3022 sin->sin_port = htons((getuid() == 0) ? 80 : 8000);
3023 #endif
3024 sin->sin_addr.s_addr = INADDR_ANY;
3026 lsopt.socklen = sizeof(struct sockaddr_in);
3028 lsopt.backlog = NGX_LISTEN_BACKLOG;
3029 lsopt.rcvbuf = -1;
3030 lsopt.sndbuf = -1;
3031 #if (NGX_HAVE_SETFIB)
3032 lsopt.setfib = -1;
3033 #endif
3034 lsopt.wildcard = 1;
3036 (void) ngx_sock_ntop(&lsopt.u.sockaddr, lsopt.addr,
3037 NGX_SOCKADDR_STRLEN, 1);
3039 if (ngx_http_add_listen(cf, cscf, &lsopt) != NGX_OK) {
3040 return NGX_CONF_ERROR;
3044 return rv;
3048 static char *
3049 ngx_http_core_location(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy)
3051 char *rv;
3052 u_char *mod;
3053 size_t len;
3054 ngx_str_t *value, *name;
3055 ngx_uint_t i;
3056 ngx_conf_t save;
3057 ngx_http_module_t *module;
3058 ngx_http_conf_ctx_t *ctx, *pctx;
3059 ngx_http_core_loc_conf_t *clcf, *pclcf;
3061 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
3062 if (ctx == NULL) {
3063 return NGX_CONF_ERROR;
3066 pctx = cf->ctx;
3067 ctx->main_conf = pctx->main_conf;
3068 ctx->srv_conf = pctx->srv_conf;
3070 ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
3071 if (ctx->loc_conf == NULL) {
3072 return NGX_CONF_ERROR;
3075 for (i = 0; ngx_modules[i]; i++) {
3076 if (ngx_modules[i]->type != NGX_HTTP_MODULE) {
3077 continue;
3080 module = ngx_modules[i]->ctx;
3082 if (module->create_loc_conf) {
3083 ctx->loc_conf[ngx_modules[i]->ctx_index] =
3084 module->create_loc_conf(cf);
3085 if (ctx->loc_conf[ngx_modules[i]->ctx_index] == NULL) {
3086 return NGX_CONF_ERROR;
3091 clcf = ctx->loc_conf[ngx_http_core_module.ctx_index];
3092 clcf->loc_conf = ctx->loc_conf;
3094 value = cf->args->elts;
3096 if (cf->args->nelts == 3) {
3098 len = value[1].len;
3099 mod = value[1].data;
3100 name = &value[2];
3102 if (len == 1 && mod[0] == '=') {
3104 clcf->name = *name;
3105 clcf->exact_match = 1;
3107 } else if (len == 2 && mod[0] == '^' && mod[1] == '~') {
3109 clcf->name = *name;
3110 clcf->noregex = 1;
3112 } else if (len == 1 && mod[0] == '~') {
3114 if (ngx_http_core_regex_location(cf, clcf, name, 0) != NGX_OK) {
3115 return NGX_CONF_ERROR;
3118 } else if (len == 2 && mod[0] == '~' && mod[1] == '*') {
3120 if (ngx_http_core_regex_location(cf, clcf, name, 1) != NGX_OK) {
3121 return NGX_CONF_ERROR;
3124 } else {
3125 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3126 "invalid location modifier \"%V\"", &value[1]);
3127 return NGX_CONF_ERROR;
3130 } else {
3132 name = &value[1];
3134 if (name->data[0] == '=') {
3136 clcf->name.len = name->len - 1;
3137 clcf->name.data = name->data + 1;
3138 clcf->exact_match = 1;
3140 } else if (name->data[0] == '^' && name->data[1] == '~') {
3142 clcf->name.len = name->len - 2;
3143 clcf->name.data = name->data + 2;
3144 clcf->noregex = 1;
3146 } else if (name->data[0] == '~') {
3148 name->len--;
3149 name->data++;
3151 if (name->data[0] == '*') {
3153 name->len--;
3154 name->data++;
3156 if (ngx_http_core_regex_location(cf, clcf, name, 1) != NGX_OK) {
3157 return NGX_CONF_ERROR;
3160 } else {
3161 if (ngx_http_core_regex_location(cf, clcf, name, 0) != NGX_OK) {
3162 return NGX_CONF_ERROR;
3166 } else {
3168 clcf->name = *name;
3170 if (name->data[0] == '@') {
3171 clcf->named = 1;
3176 pclcf = pctx->loc_conf[ngx_http_core_module.ctx_index];
3178 if (pclcf->name.len) {
3180 /* nested location */
3182 #if 0
3183 clcf->prev_location = pclcf;
3184 #endif
3186 if (pclcf->exact_match) {
3187 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3188 "location \"%V\" cannot be inside "
3189 "the exact location \"%V\"",
3190 &clcf->name, &pclcf->name);
3191 return NGX_CONF_ERROR;
3194 if (pclcf->named) {
3195 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3196 "location \"%V\" cannot be inside "
3197 "the named location \"%V\"",
3198 &clcf->name, &pclcf->name);
3199 return NGX_CONF_ERROR;
3202 if (clcf->named) {
3203 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3204 "named location \"%V\" can be "
3205 "on the server level only",
3206 &clcf->name);
3207 return NGX_CONF_ERROR;
3210 len = pclcf->name.len;
3212 #if (NGX_PCRE)
3213 if (clcf->regex == NULL
3214 && ngx_strncmp(clcf->name.data, pclcf->name.data, len) != 0)
3215 #else
3216 if (ngx_strncmp(clcf->name.data, pclcf->name.data, len) != 0)
3217 #endif
3219 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3220 "location \"%V\" is outside location \"%V\"",
3221 &clcf->name, &pclcf->name);
3222 return NGX_CONF_ERROR;
3226 if (ngx_http_add_location(cf, &pclcf->locations, clcf) != NGX_OK) {
3227 return NGX_CONF_ERROR;
3230 save = *cf;
3231 cf->ctx = ctx;
3232 cf->cmd_type = NGX_HTTP_LOC_CONF;
3234 rv = ngx_conf_parse(cf, NULL);
3236 *cf = save;
3238 return rv;
3242 static ngx_int_t
3243 ngx_http_core_regex_location(ngx_conf_t *cf, ngx_http_core_loc_conf_t *clcf,
3244 ngx_str_t *regex, ngx_uint_t caseless)
3246 #if (NGX_PCRE)
3247 ngx_regex_compile_t rc;
3248 u_char errstr[NGX_MAX_CONF_ERRSTR];
3250 ngx_memzero(&rc, sizeof(ngx_regex_compile_t));
3252 rc.pattern = *regex;
3253 rc.err.len = NGX_MAX_CONF_ERRSTR;
3254 rc.err.data = errstr;
3256 #if (NGX_HAVE_CASELESS_FILESYSTEM)
3257 rc.options = NGX_REGEX_CASELESS;
3258 #else
3259 rc.options = caseless ? NGX_REGEX_CASELESS : 0;
3260 #endif
3262 clcf->regex = ngx_http_regex_compile(cf, &rc);
3263 if (clcf->regex == NULL) {
3264 return NGX_ERROR;
3267 clcf->name = *regex;
3269 return NGX_OK;
3271 #else
3273 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3274 "using regex \"%V\" requires PCRE library",
3275 regex);
3276 return NGX_ERROR;
3278 #endif
3282 static char *
3283 ngx_http_core_types(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
3285 ngx_http_core_loc_conf_t *clcf = conf;
3287 char *rv;
3288 ngx_conf_t save;
3290 if (clcf->types == NULL) {
3291 clcf->types = ngx_array_create(cf->pool, 64, sizeof(ngx_hash_key_t));
3292 if (clcf->types == NULL) {
3293 return NGX_CONF_ERROR;
3297 save = *cf;
3298 cf->handler = ngx_http_core_type;
3299 cf->handler_conf = conf;
3301 rv = ngx_conf_parse(cf, NULL);
3303 *cf = save;
3305 return rv;
3309 static char *
3310 ngx_http_core_type(ngx_conf_t *cf, ngx_command_t *dummy, void *conf)
3312 ngx_http_core_loc_conf_t *clcf = conf;
3314 ngx_str_t *value, *content_type, *old;
3315 ngx_uint_t i, n, hash;
3316 ngx_hash_key_t *type;
3318 value = cf->args->elts;
3320 if (ngx_strcmp(value[0].data, "include") == 0) {
3321 if (cf->args->nelts != 2) {
3322 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3323 "invalid number of arguments"
3324 " in \"include\" directive");
3325 return NGX_CONF_ERROR;
3328 return ngx_conf_include(cf, dummy, conf);
3331 content_type = ngx_palloc(cf->pool, sizeof(ngx_str_t));
3332 if (content_type == NULL) {
3333 return NGX_CONF_ERROR;
3336 *content_type = value[0];
3338 for (i = 1; i < cf->args->nelts; i++) {
3340 hash = ngx_hash_strlow(value[i].data, value[i].data, value[i].len);
3342 type = clcf->types->elts;
3343 for (n = 0; n < clcf->types->nelts; n++) {
3344 if (ngx_strcmp(value[i].data, type[n].key.data) == 0) {
3345 old = type[n].value;
3346 type[n].value = content_type;
3348 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
3349 "duplicate extension \"%V\", "
3350 "content type: \"%V\", "
3351 "previous content type: \"%V\"",
3352 &value[i], content_type, old);
3353 goto next;
3358 type = ngx_array_push(clcf->types);
3359 if (type == NULL) {
3360 return NGX_CONF_ERROR;
3363 type->key = value[i];
3364 type->key_hash = hash;
3365 type->value = content_type;
3367 next:
3368 continue;
3371 return NGX_CONF_OK;
3375 static ngx_int_t
3376 ngx_http_core_preconfiguration(ngx_conf_t *cf)
3378 return ngx_http_variables_add_core_vars(cf);
3382 static void *
3383 ngx_http_core_create_main_conf(ngx_conf_t *cf)
3385 ngx_http_core_main_conf_t *cmcf;
3387 cmcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_core_main_conf_t));
3388 if (cmcf == NULL) {
3389 return NULL;
3392 if (ngx_array_init(&cmcf->servers, cf->pool, 4,
3393 sizeof(ngx_http_core_srv_conf_t *))
3394 != NGX_OK)
3396 return NULL;
3399 cmcf->server_names_hash_max_size = NGX_CONF_UNSET_UINT;
3400 cmcf->server_names_hash_bucket_size = NGX_CONF_UNSET_UINT;
3402 cmcf->variables_hash_max_size = NGX_CONF_UNSET_UINT;
3403 cmcf->variables_hash_bucket_size = NGX_CONF_UNSET_UINT;
3405 return cmcf;
3409 static char *
3410 ngx_http_core_init_main_conf(ngx_conf_t *cf, void *conf)
3412 ngx_http_core_main_conf_t *cmcf = conf;
3414 if (cmcf->server_names_hash_max_size == NGX_CONF_UNSET_UINT) {
3415 cmcf->server_names_hash_max_size = 512;
3418 if (cmcf->server_names_hash_bucket_size == NGX_CONF_UNSET_UINT) {
3419 cmcf->server_names_hash_bucket_size = ngx_cacheline_size;
3422 cmcf->server_names_hash_bucket_size =
3423 ngx_align(cmcf->server_names_hash_bucket_size, ngx_cacheline_size);
3426 if (cmcf->variables_hash_max_size == NGX_CONF_UNSET_UINT) {
3427 cmcf->variables_hash_max_size = 512;
3430 if (cmcf->variables_hash_bucket_size == NGX_CONF_UNSET_UINT) {
3431 cmcf->variables_hash_bucket_size = 64;
3434 cmcf->variables_hash_bucket_size =
3435 ngx_align(cmcf->variables_hash_bucket_size, ngx_cacheline_size);
3437 if (cmcf->ncaptures) {
3438 cmcf->ncaptures = (cmcf->ncaptures + 1) * 3;
3441 return NGX_CONF_OK;
3445 static void *
3446 ngx_http_core_create_srv_conf(ngx_conf_t *cf)
3448 ngx_http_core_srv_conf_t *cscf;
3450 cscf = ngx_pcalloc(cf->pool, sizeof(ngx_http_core_srv_conf_t));
3451 if (cscf == NULL) {
3452 return NULL;
3456 * set by ngx_pcalloc():
3458 * conf->client_large_buffers.num = 0;
3461 if (ngx_array_init(&cscf->server_names, cf->temp_pool, 4,
3462 sizeof(ngx_http_server_name_t))
3463 != NGX_OK)
3465 return NULL;
3468 cscf->connection_pool_size = NGX_CONF_UNSET_SIZE;
3469 cscf->request_pool_size = NGX_CONF_UNSET_SIZE;
3470 cscf->client_header_timeout = NGX_CONF_UNSET_MSEC;
3471 cscf->client_header_buffer_size = NGX_CONF_UNSET_SIZE;
3472 cscf->ignore_invalid_headers = NGX_CONF_UNSET;
3473 cscf->merge_slashes = NGX_CONF_UNSET;
3474 cscf->underscores_in_headers = NGX_CONF_UNSET;
3476 return cscf;
3480 static char *
3481 ngx_http_core_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
3483 ngx_http_core_srv_conf_t *prev = parent;
3484 ngx_http_core_srv_conf_t *conf = child;
3486 ngx_str_t name;
3487 ngx_http_server_name_t *sn;
3489 /* TODO: it does not merge, it inits only */
3491 ngx_conf_merge_size_value(conf->connection_pool_size,
3492 prev->connection_pool_size, 256);
3493 ngx_conf_merge_size_value(conf->request_pool_size,
3494 prev->request_pool_size, 4096);
3495 ngx_conf_merge_msec_value(conf->client_header_timeout,
3496 prev->client_header_timeout, 60000);
3497 ngx_conf_merge_size_value(conf->client_header_buffer_size,
3498 prev->client_header_buffer_size, 1024);
3499 ngx_conf_merge_bufs_value(conf->large_client_header_buffers,
3500 prev->large_client_header_buffers,
3501 4, 8192);
3503 if (conf->large_client_header_buffers.size < conf->connection_pool_size) {
3504 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3505 "the \"large_client_header_buffers\" size must be "
3506 "equal to or greater than \"connection_pool_size\"");
3507 return NGX_CONF_ERROR;
3510 ngx_conf_merge_value(conf->ignore_invalid_headers,
3511 prev->ignore_invalid_headers, 1);
3513 ngx_conf_merge_value(conf->merge_slashes, prev->merge_slashes, 1);
3515 ngx_conf_merge_value(conf->underscores_in_headers,
3516 prev->underscores_in_headers, 0);
3518 if (conf->server_names.nelts == 0) {
3519 /* the array has 4 empty preallocated elements, so push cannot fail */
3520 sn = ngx_array_push(&conf->server_names);
3521 #if (NGX_PCRE)
3522 sn->regex = NULL;
3523 #endif
3524 sn->server = conf;
3525 ngx_str_set(&sn->name, "");
3528 sn = conf->server_names.elts;
3529 name = sn[0].name;
3531 #if (NGX_PCRE)
3532 if (sn->regex) {
3533 name.len++;
3534 name.data--;
3535 } else
3536 #endif
3538 if (name.data[0] == '.') {
3539 name.len--;
3540 name.data++;
3543 conf->server_name.len = name.len;
3544 conf->server_name.data = ngx_pstrdup(cf->pool, &name);
3545 if (conf->server_name.data == NULL) {
3546 return NGX_CONF_ERROR;
3549 return NGX_CONF_OK;
3553 static void *
3554 ngx_http_core_create_loc_conf(ngx_conf_t *cf)
3556 ngx_http_core_loc_conf_t *clcf;
3558 clcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_core_loc_conf_t));
3559 if (clcf == NULL) {
3560 return NULL;
3564 * set by ngx_pcalloc():
3566 * clcf->root = { 0, NULL };
3567 * clcf->limit_except = 0;
3568 * clcf->post_action = { 0, NULL };
3569 * clcf->types = NULL;
3570 * clcf->default_type = { 0, NULL };
3571 * clcf->error_log = NULL;
3572 * clcf->error_pages = NULL;
3573 * clcf->try_files = NULL;
3574 * clcf->client_body_path = NULL;
3575 * clcf->regex = NULL;
3576 * clcf->exact_match = 0;
3577 * clcf->auto_redirect = 0;
3578 * clcf->alias = 0;
3579 * clcf->gzip_proxied = 0;
3580 * clcf->keepalive_disable = 0;
3583 clcf->client_max_body_size = NGX_CONF_UNSET;
3584 clcf->client_body_buffer_size = NGX_CONF_UNSET_SIZE;
3585 clcf->client_body_timeout = NGX_CONF_UNSET_MSEC;
3586 clcf->satisfy = NGX_CONF_UNSET_UINT;
3587 clcf->if_modified_since = NGX_CONF_UNSET_UINT;
3588 clcf->max_ranges = NGX_CONF_UNSET_UINT;
3589 clcf->client_body_in_file_only = NGX_CONF_UNSET_UINT;
3590 clcf->client_body_in_single_buffer = NGX_CONF_UNSET;
3591 clcf->internal = NGX_CONF_UNSET;
3592 clcf->sendfile = NGX_CONF_UNSET;
3593 clcf->sendfile_max_chunk = NGX_CONF_UNSET_SIZE;
3594 #if (NGX_HAVE_FILE_AIO)
3595 clcf->aio = NGX_CONF_UNSET;
3596 #endif
3597 clcf->read_ahead = NGX_CONF_UNSET_SIZE;
3598 clcf->directio = NGX_CONF_UNSET;
3599 clcf->directio_alignment = NGX_CONF_UNSET;
3600 clcf->tcp_nopush = NGX_CONF_UNSET;
3601 clcf->tcp_nodelay = NGX_CONF_UNSET;
3602 clcf->send_timeout = NGX_CONF_UNSET_MSEC;
3603 clcf->send_lowat = NGX_CONF_UNSET_SIZE;
3604 clcf->postpone_output = NGX_CONF_UNSET_SIZE;
3605 clcf->limit_rate = NGX_CONF_UNSET_SIZE;
3606 clcf->limit_rate_after = NGX_CONF_UNSET_SIZE;
3607 clcf->keepalive_timeout = NGX_CONF_UNSET_MSEC;
3608 clcf->keepalive_header = NGX_CONF_UNSET;
3609 clcf->keepalive_requests = NGX_CONF_UNSET_UINT;
3610 clcf->lingering_close = NGX_CONF_UNSET_UINT;
3611 clcf->lingering_time = NGX_CONF_UNSET_MSEC;
3612 clcf->lingering_timeout = NGX_CONF_UNSET_MSEC;
3613 clcf->resolver_timeout = NGX_CONF_UNSET_MSEC;
3614 clcf->reset_timedout_connection = NGX_CONF_UNSET;
3615 clcf->server_name_in_redirect = NGX_CONF_UNSET;
3616 clcf->port_in_redirect = NGX_CONF_UNSET;
3617 clcf->msie_padding = NGX_CONF_UNSET;
3618 clcf->msie_refresh = NGX_CONF_UNSET;
3619 clcf->log_not_found = NGX_CONF_UNSET;
3620 clcf->log_subrequest = NGX_CONF_UNSET;
3621 clcf->recursive_error_pages = NGX_CONF_UNSET;
3622 clcf->server_tokens = NGX_CONF_UNSET;
3623 clcf->chunked_transfer_encoding = NGX_CONF_UNSET;
3624 clcf->etag = NGX_CONF_UNSET;
3625 clcf->types_hash_max_size = NGX_CONF_UNSET_UINT;
3626 clcf->types_hash_bucket_size = NGX_CONF_UNSET_UINT;
3628 clcf->open_file_cache = NGX_CONF_UNSET_PTR;
3629 clcf->open_file_cache_valid = NGX_CONF_UNSET;
3630 clcf->open_file_cache_min_uses = NGX_CONF_UNSET_UINT;
3631 clcf->open_file_cache_errors = NGX_CONF_UNSET;
3632 clcf->open_file_cache_events = NGX_CONF_UNSET;
3634 #if (NGX_HTTP_GZIP)
3635 clcf->gzip_vary = NGX_CONF_UNSET;
3636 clcf->gzip_http_version = NGX_CONF_UNSET_UINT;
3637 #if (NGX_PCRE)
3638 clcf->gzip_disable = NGX_CONF_UNSET_PTR;
3639 #endif
3640 clcf->gzip_disable_msie6 = 3;
3641 #if (NGX_HTTP_DEGRADATION)
3642 clcf->gzip_disable_degradation = 3;
3643 #endif
3644 #endif
3646 #if (NGX_HAVE_OPENAT)
3647 clcf->disable_symlinks = NGX_CONF_UNSET_UINT;
3648 clcf->disable_symlinks_from = NGX_CONF_UNSET_PTR;
3649 #endif
3651 return clcf;
3655 static ngx_str_t ngx_http_core_text_html_type = ngx_string("text/html");
3656 static ngx_str_t ngx_http_core_image_gif_type = ngx_string("image/gif");
3657 static ngx_str_t ngx_http_core_image_jpeg_type = ngx_string("image/jpeg");
3659 static ngx_hash_key_t ngx_http_core_default_types[] = {
3660 { ngx_string("html"), 0, &ngx_http_core_text_html_type },
3661 { ngx_string("gif"), 0, &ngx_http_core_image_gif_type },
3662 { ngx_string("jpg"), 0, &ngx_http_core_image_jpeg_type },
3663 { ngx_null_string, 0, NULL }
3667 static char *
3668 ngx_http_core_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
3670 ngx_http_core_loc_conf_t *prev = parent;
3671 ngx_http_core_loc_conf_t *conf = child;
3673 ngx_uint_t i;
3674 ngx_hash_key_t *type;
3675 ngx_hash_init_t types_hash;
3677 if (conf->root.data == NULL) {
3679 conf->alias = prev->alias;
3680 conf->root = prev->root;
3681 conf->root_lengths = prev->root_lengths;
3682 conf->root_values = prev->root_values;
3684 if (prev->root.data == NULL) {
3685 ngx_str_set(&conf->root, "html");
3687 if (ngx_conf_full_name(cf->cycle, &conf->root, 0) != NGX_OK) {
3688 return NGX_CONF_ERROR;
3693 if (conf->post_action.data == NULL) {
3694 conf->post_action = prev->post_action;
3697 ngx_conf_merge_uint_value(conf->types_hash_max_size,
3698 prev->types_hash_max_size, 1024);
3700 ngx_conf_merge_uint_value(conf->types_hash_bucket_size,
3701 prev->types_hash_bucket_size,
3702 ngx_cacheline_size);
3704 conf->types_hash_bucket_size = ngx_align(conf->types_hash_bucket_size,
3705 ngx_cacheline_size);
3708 * the special handling of the "types" directive in the "http" section
3709 * to inherit the http's conf->types_hash to all servers
3712 if (prev->types && prev->types_hash.buckets == NULL) {
3714 types_hash.hash = &prev->types_hash;
3715 types_hash.key = ngx_hash_key_lc;
3716 types_hash.max_size = conf->types_hash_max_size;
3717 types_hash.bucket_size = conf->types_hash_bucket_size;
3718 types_hash.name = "types_hash";
3719 types_hash.pool = cf->pool;
3720 types_hash.temp_pool = NULL;
3722 if (ngx_hash_init(&types_hash, prev->types->elts, prev->types->nelts)
3723 != NGX_OK)
3725 return NGX_CONF_ERROR;
3729 if (conf->types == NULL) {
3730 conf->types = prev->types;
3731 conf->types_hash = prev->types_hash;
3734 if (conf->types == NULL) {
3735 conf->types = ngx_array_create(cf->pool, 3, sizeof(ngx_hash_key_t));
3736 if (conf->types == NULL) {
3737 return NGX_CONF_ERROR;
3740 for (i = 0; ngx_http_core_default_types[i].key.len; i++) {
3741 type = ngx_array_push(conf->types);
3742 if (type == NULL) {
3743 return NGX_CONF_ERROR;
3746 type->key = ngx_http_core_default_types[i].key;
3747 type->key_hash =
3748 ngx_hash_key_lc(ngx_http_core_default_types[i].key.data,
3749 ngx_http_core_default_types[i].key.len);
3750 type->value = ngx_http_core_default_types[i].value;
3754 if (conf->types_hash.buckets == NULL) {
3756 types_hash.hash = &conf->types_hash;
3757 types_hash.key = ngx_hash_key_lc;
3758 types_hash.max_size = conf->types_hash_max_size;
3759 types_hash.bucket_size = conf->types_hash_bucket_size;
3760 types_hash.name = "types_hash";
3761 types_hash.pool = cf->pool;
3762 types_hash.temp_pool = NULL;
3764 if (ngx_hash_init(&types_hash, conf->types->elts, conf->types->nelts)
3765 != NGX_OK)
3767 return NGX_CONF_ERROR;
3771 if (conf->error_log == NULL) {
3772 if (prev->error_log) {
3773 conf->error_log = prev->error_log;
3774 } else {
3775 conf->error_log = &cf->cycle->new_log;
3779 if (conf->error_pages == NULL && prev->error_pages) {
3780 conf->error_pages = prev->error_pages;
3783 ngx_conf_merge_str_value(conf->default_type,
3784 prev->default_type, "text/plain");
3786 ngx_conf_merge_off_value(conf->client_max_body_size,
3787 prev->client_max_body_size, 1 * 1024 * 1024);
3788 ngx_conf_merge_size_value(conf->client_body_buffer_size,
3789 prev->client_body_buffer_size,
3790 (size_t) 2 * ngx_pagesize);
3791 ngx_conf_merge_msec_value(conf->client_body_timeout,
3792 prev->client_body_timeout, 60000);
3794 ngx_conf_merge_bitmask_value(conf->keepalive_disable,
3795 prev->keepalive_disable,
3796 (NGX_CONF_BITMASK_SET
3797 |NGX_HTTP_KEEPALIVE_DISABLE_MSIE6));
3798 ngx_conf_merge_uint_value(conf->satisfy, prev->satisfy,
3799 NGX_HTTP_SATISFY_ALL);
3800 ngx_conf_merge_uint_value(conf->if_modified_since, prev->if_modified_since,
3801 NGX_HTTP_IMS_EXACT);
3802 ngx_conf_merge_uint_value(conf->max_ranges, prev->max_ranges,
3803 NGX_MAX_INT32_VALUE);
3804 ngx_conf_merge_uint_value(conf->client_body_in_file_only,
3805 prev->client_body_in_file_only,
3806 NGX_HTTP_REQUEST_BODY_FILE_OFF);
3807 ngx_conf_merge_value(conf->client_body_in_single_buffer,
3808 prev->client_body_in_single_buffer, 0);
3809 ngx_conf_merge_value(conf->internal, prev->internal, 0);
3810 ngx_conf_merge_value(conf->sendfile, prev->sendfile, 0);
3811 ngx_conf_merge_size_value(conf->sendfile_max_chunk,
3812 prev->sendfile_max_chunk, 0);
3813 #if (NGX_HAVE_FILE_AIO)
3814 ngx_conf_merge_value(conf->aio, prev->aio, NGX_HTTP_AIO_OFF);
3815 #endif
3816 ngx_conf_merge_size_value(conf->read_ahead, prev->read_ahead, 0);
3817 ngx_conf_merge_off_value(conf->directio, prev->directio,
3818 NGX_OPEN_FILE_DIRECTIO_OFF);
3819 ngx_conf_merge_off_value(conf->directio_alignment, prev->directio_alignment,
3820 512);
3821 ngx_conf_merge_value(conf->tcp_nopush, prev->tcp_nopush, 0);
3822 ngx_conf_merge_value(conf->tcp_nodelay, prev->tcp_nodelay, 1);
3824 ngx_conf_merge_msec_value(conf->send_timeout, prev->send_timeout, 60000);
3825 ngx_conf_merge_size_value(conf->send_lowat, prev->send_lowat, 0);
3826 ngx_conf_merge_size_value(conf->postpone_output, prev->postpone_output,
3827 1460);
3828 ngx_conf_merge_size_value(conf->limit_rate, prev->limit_rate, 0);
3829 ngx_conf_merge_size_value(conf->limit_rate_after, prev->limit_rate_after,
3831 ngx_conf_merge_msec_value(conf->keepalive_timeout,
3832 prev->keepalive_timeout, 75000);
3833 ngx_conf_merge_sec_value(conf->keepalive_header,
3834 prev->keepalive_header, 0);
3835 ngx_conf_merge_uint_value(conf->keepalive_requests,
3836 prev->keepalive_requests, 100);
3837 ngx_conf_merge_uint_value(conf->lingering_close,
3838 prev->lingering_close, NGX_HTTP_LINGERING_ON);
3839 ngx_conf_merge_msec_value(conf->lingering_time,
3840 prev->lingering_time, 30000);
3841 ngx_conf_merge_msec_value(conf->lingering_timeout,
3842 prev->lingering_timeout, 5000);
3843 ngx_conf_merge_msec_value(conf->resolver_timeout,
3844 prev->resolver_timeout, 30000);
3846 if (conf->resolver == NULL) {
3848 if (prev->resolver == NULL) {
3851 * create dummy resolver in http {} context
3852 * to inherit it in all servers
3855 prev->resolver = ngx_resolver_create(cf, NULL, 0);
3856 if (prev->resolver == NULL) {
3857 return NGX_CONF_ERROR;
3861 conf->resolver = prev->resolver;
3864 if (ngx_conf_merge_path_value(cf, &conf->client_body_temp_path,
3865 prev->client_body_temp_path,
3866 &ngx_http_client_temp_path)
3867 != NGX_OK)
3869 return NGX_CONF_ERROR;
3872 ngx_conf_merge_value(conf->reset_timedout_connection,
3873 prev->reset_timedout_connection, 0);
3874 ngx_conf_merge_value(conf->server_name_in_redirect,
3875 prev->server_name_in_redirect, 0);
3876 ngx_conf_merge_value(conf->port_in_redirect, prev->port_in_redirect, 1);
3877 ngx_conf_merge_value(conf->msie_padding, prev->msie_padding, 1);
3878 ngx_conf_merge_value(conf->msie_refresh, prev->msie_refresh, 0);
3879 ngx_conf_merge_value(conf->log_not_found, prev->log_not_found, 1);
3880 ngx_conf_merge_value(conf->log_subrequest, prev->log_subrequest, 0);
3881 ngx_conf_merge_value(conf->recursive_error_pages,
3882 prev->recursive_error_pages, 0);
3883 ngx_conf_merge_value(conf->server_tokens, prev->server_tokens, 1);
3884 ngx_conf_merge_value(conf->chunked_transfer_encoding,
3885 prev->chunked_transfer_encoding, 1);
3886 ngx_conf_merge_value(conf->etag, prev->etag, 1);
3888 ngx_conf_merge_ptr_value(conf->open_file_cache,
3889 prev->open_file_cache, NULL);
3891 ngx_conf_merge_sec_value(conf->open_file_cache_valid,
3892 prev->open_file_cache_valid, 60);
3894 ngx_conf_merge_uint_value(conf->open_file_cache_min_uses,
3895 prev->open_file_cache_min_uses, 1);
3897 ngx_conf_merge_sec_value(conf->open_file_cache_errors,
3898 prev->open_file_cache_errors, 0);
3900 ngx_conf_merge_sec_value(conf->open_file_cache_events,
3901 prev->open_file_cache_events, 0);
3902 #if (NGX_HTTP_GZIP)
3904 ngx_conf_merge_value(conf->gzip_vary, prev->gzip_vary, 0);
3905 ngx_conf_merge_uint_value(conf->gzip_http_version, prev->gzip_http_version,
3906 NGX_HTTP_VERSION_11);
3907 ngx_conf_merge_bitmask_value(conf->gzip_proxied, prev->gzip_proxied,
3908 (NGX_CONF_BITMASK_SET|NGX_HTTP_GZIP_PROXIED_OFF));
3910 #if (NGX_PCRE)
3911 ngx_conf_merge_ptr_value(conf->gzip_disable, prev->gzip_disable, NULL);
3912 #endif
3914 if (conf->gzip_disable_msie6 == 3) {
3915 conf->gzip_disable_msie6 =
3916 (prev->gzip_disable_msie6 == 3) ? 0 : prev->gzip_disable_msie6;
3919 #if (NGX_HTTP_DEGRADATION)
3921 if (conf->gzip_disable_degradation == 3) {
3922 conf->gzip_disable_degradation =
3923 (prev->gzip_disable_degradation == 3) ?
3924 0 : prev->gzip_disable_degradation;
3927 #endif
3928 #endif
3930 #if (NGX_HAVE_OPENAT)
3931 ngx_conf_merge_uint_value(conf->disable_symlinks, prev->disable_symlinks,
3932 NGX_DISABLE_SYMLINKS_OFF);
3933 ngx_conf_merge_ptr_value(conf->disable_symlinks_from,
3934 prev->disable_symlinks_from, NULL);
3935 #endif
3937 return NGX_CONF_OK;
3941 static char *
3942 ngx_http_core_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
3944 ngx_http_core_srv_conf_t *cscf = conf;
3946 ngx_str_t *value, size;
3947 ngx_url_t u;
3948 ngx_uint_t n;
3949 ngx_http_listen_opt_t lsopt;
3951 cscf->listen = 1;
3953 value = cf->args->elts;
3955 ngx_memzero(&u, sizeof(ngx_url_t));
3957 u.url = value[1];
3958 u.listen = 1;
3959 u.default_port = 80;
3961 if (ngx_parse_url(cf->pool, &u) != NGX_OK) {
3962 if (u.err) {
3963 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3964 "%s in \"%V\" of the \"listen\" directive",
3965 u.err, &u.url);
3968 return NGX_CONF_ERROR;
3971 ngx_memzero(&lsopt, sizeof(ngx_http_listen_opt_t));
3973 ngx_memcpy(&lsopt.u.sockaddr, u.sockaddr, u.socklen);
3975 lsopt.socklen = u.socklen;
3976 lsopt.backlog = NGX_LISTEN_BACKLOG;
3977 lsopt.rcvbuf = -1;
3978 lsopt.sndbuf = -1;
3979 #if (NGX_HAVE_SETFIB)
3980 lsopt.setfib = -1;
3981 #endif
3982 lsopt.wildcard = u.wildcard;
3983 #if (NGX_HAVE_INET6 && defined IPV6_V6ONLY)
3984 lsopt.ipv6only = 1;
3985 #endif
3987 (void) ngx_sock_ntop(&lsopt.u.sockaddr, lsopt.addr,
3988 NGX_SOCKADDR_STRLEN, 1);
3990 for (n = 2; n < cf->args->nelts; n++) {
3992 if (ngx_strcmp(value[n].data, "default_server") == 0
3993 || ngx_strcmp(value[n].data, "default") == 0)
3995 lsopt.default_server = 1;
3996 continue;
3999 if (ngx_strcmp(value[n].data, "bind") == 0) {
4000 lsopt.set = 1;
4001 lsopt.bind = 1;
4002 continue;
4005 #if (NGX_HAVE_SETFIB)
4006 if (ngx_strncmp(value[n].data, "setfib=", 7) == 0) {
4007 lsopt.setfib = ngx_atoi(value[n].data + 7, value[n].len - 7);
4009 if (lsopt.setfib == NGX_ERROR) {
4010 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4011 "invalid setfib \"%V\"", &value[n]);
4012 return NGX_CONF_ERROR;
4015 continue;
4017 #endif
4018 if (ngx_strncmp(value[n].data, "backlog=", 8) == 0) {
4019 lsopt.backlog = ngx_atoi(value[n].data + 8, value[n].len - 8);
4020 lsopt.set = 1;
4021 lsopt.bind = 1;
4023 if (lsopt.backlog == NGX_ERROR || lsopt.backlog == 0) {
4024 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4025 "invalid backlog \"%V\"", &value[n]);
4026 return NGX_CONF_ERROR;
4029 continue;
4032 if (ngx_strncmp(value[n].data, "rcvbuf=", 7) == 0) {
4033 size.len = value[n].len - 7;
4034 size.data = value[n].data + 7;
4036 lsopt.rcvbuf = ngx_parse_size(&size);
4037 lsopt.set = 1;
4038 lsopt.bind = 1;
4040 if (lsopt.rcvbuf == NGX_ERROR) {
4041 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4042 "invalid rcvbuf \"%V\"", &value[n]);
4043 return NGX_CONF_ERROR;
4046 continue;
4049 if (ngx_strncmp(value[n].data, "sndbuf=", 7) == 0) {
4050 size.len = value[n].len - 7;
4051 size.data = value[n].data + 7;
4053 lsopt.sndbuf = ngx_parse_size(&size);
4054 lsopt.set = 1;
4055 lsopt.bind = 1;
4057 if (lsopt.sndbuf == NGX_ERROR) {
4058 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4059 "invalid sndbuf \"%V\"", &value[n]);
4060 return NGX_CONF_ERROR;
4063 continue;
4066 if (ngx_strncmp(value[n].data, "accept_filter=", 14) == 0) {
4067 #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
4068 lsopt.accept_filter = (char *) &value[n].data[14];
4069 lsopt.set = 1;
4070 lsopt.bind = 1;
4071 #else
4072 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4073 "accept filters \"%V\" are not supported "
4074 "on this platform, ignored",
4075 &value[n]);
4076 #endif
4077 continue;
4080 if (ngx_strcmp(value[n].data, "deferred") == 0) {
4081 #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)
4082 lsopt.deferred_accept = 1;
4083 lsopt.set = 1;
4084 lsopt.bind = 1;
4085 #else
4086 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4087 "the deferred accept is not supported "
4088 "on this platform, ignored");
4089 #endif
4090 continue;
4093 if (ngx_strncmp(value[n].data, "ipv6only=o", 10) == 0) {
4094 #if (NGX_HAVE_INET6 && defined IPV6_V6ONLY)
4095 struct sockaddr *sa;
4097 sa = &lsopt.u.sockaddr;
4099 if (sa->sa_family == AF_INET6) {
4101 if (ngx_strcmp(&value[n].data[10], "n") == 0) {
4102 lsopt.ipv6only = 1;
4104 } else if (ngx_strcmp(&value[n].data[10], "ff") == 0) {
4105 lsopt.ipv6only = 0;
4107 } else {
4108 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4109 "invalid ipv6only flags \"%s\"",
4110 &value[n].data[9]);
4111 return NGX_CONF_ERROR;
4114 lsopt.set = 1;
4115 lsopt.bind = 1;
4117 } else {
4118 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4119 "ipv6only is not supported "
4120 "on addr \"%s\", ignored", lsopt.addr);
4123 continue;
4124 #else
4125 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4126 "ipv6only is not supported "
4127 "on this platform");
4128 return NGX_CONF_ERROR;
4129 #endif
4132 if (ngx_strcmp(value[n].data, "ssl") == 0) {
4133 #if (NGX_HTTP_SSL)
4134 lsopt.ssl = 1;
4135 continue;
4136 #else
4137 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4138 "the \"ssl\" parameter requires "
4139 "ngx_http_ssl_module");
4140 return NGX_CONF_ERROR;
4141 #endif
4144 if (ngx_strcmp(value[n].data, "spdy") == 0) {
4145 #if (NGX_HTTP_SPDY)
4146 lsopt.spdy = 1;
4147 continue;
4148 #else
4149 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4150 "the \"spdy\" parameter requires "
4151 "ngx_http_spdy_module");
4152 return NGX_CONF_ERROR;
4153 #endif
4156 if (ngx_strncmp(value[n].data, "so_keepalive=", 13) == 0) {
4158 if (ngx_strcmp(&value[n].data[13], "on") == 0) {
4159 lsopt.so_keepalive = 1;
4161 } else if (ngx_strcmp(&value[n].data[13], "off") == 0) {
4162 lsopt.so_keepalive = 2;
4164 } else {
4166 #if (NGX_HAVE_KEEPALIVE_TUNABLE)
4167 u_char *p, *end;
4168 ngx_str_t s;
4170 end = value[n].data + value[n].len;
4171 s.data = value[n].data + 13;
4173 p = ngx_strlchr(s.data, end, ':');
4174 if (p == NULL) {
4175 p = end;
4178 if (p > s.data) {
4179 s.len = p - s.data;
4181 lsopt.tcp_keepidle = ngx_parse_time(&s, 1);
4182 if (lsopt.tcp_keepidle == (time_t) NGX_ERROR) {
4183 goto invalid_so_keepalive;
4187 s.data = (p < end) ? (p + 1) : end;
4189 p = ngx_strlchr(s.data, end, ':');
4190 if (p == NULL) {
4191 p = end;
4194 if (p > s.data) {
4195 s.len = p - s.data;
4197 lsopt.tcp_keepintvl = ngx_parse_time(&s, 1);
4198 if (lsopt.tcp_keepintvl == (time_t) NGX_ERROR) {
4199 goto invalid_so_keepalive;
4203 s.data = (p < end) ? (p + 1) : end;
4205 if (s.data < end) {
4206 s.len = end - s.data;
4208 lsopt.tcp_keepcnt = ngx_atoi(s.data, s.len);
4209 if (lsopt.tcp_keepcnt == NGX_ERROR) {
4210 goto invalid_so_keepalive;
4214 if (lsopt.tcp_keepidle == 0 && lsopt.tcp_keepintvl == 0
4215 && lsopt.tcp_keepcnt == 0)
4217 goto invalid_so_keepalive;
4220 lsopt.so_keepalive = 1;
4222 #else
4224 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4225 "the \"so_keepalive\" parameter accepts "
4226 "only \"on\" or \"off\" on this platform");
4227 return NGX_CONF_ERROR;
4229 #endif
4232 lsopt.set = 1;
4233 lsopt.bind = 1;
4235 continue;
4237 #if (NGX_HAVE_KEEPALIVE_TUNABLE)
4238 invalid_so_keepalive:
4240 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4241 "invalid so_keepalive value: \"%s\"",
4242 &value[n].data[13]);
4243 return NGX_CONF_ERROR;
4244 #endif
4247 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4248 "invalid parameter \"%V\"", &value[n]);
4249 return NGX_CONF_ERROR;
4252 if (ngx_http_add_listen(cf, cscf, &lsopt) == NGX_OK) {
4253 return NGX_CONF_OK;
4256 return NGX_CONF_ERROR;
4260 static char *
4261 ngx_http_core_server_name(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
4263 ngx_http_core_srv_conf_t *cscf = conf;
4265 u_char ch;
4266 ngx_str_t *value;
4267 ngx_uint_t i;
4268 ngx_http_server_name_t *sn;
4270 value = cf->args->elts;
4272 for (i = 1; i < cf->args->nelts; i++) {
4274 ch = value[i].data[0];
4276 if ((ch == '*' && (value[i].len < 3 || value[i].data[1] != '.'))
4277 || (ch == '.' && value[i].len < 2))
4279 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4280 "server name \"%V\" is invalid", &value[i]);
4281 return NGX_CONF_ERROR;
4284 if (ngx_strchr(value[i].data, '/')) {
4285 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
4286 "server name \"%V\" has suspicious symbols",
4287 &value[i]);
4290 sn = ngx_array_push(&cscf->server_names);
4291 if (sn == NULL) {
4292 return NGX_CONF_ERROR;
4295 #if (NGX_PCRE)
4296 sn->regex = NULL;
4297 #endif
4298 sn->server = cscf;
4300 if (ngx_strcasecmp(value[i].data, (u_char *) "$hostname") == 0) {
4301 sn->name = cf->cycle->hostname;
4303 } else {
4304 sn->name = value[i];
4307 if (value[i].data[0] != '~') {
4308 ngx_strlow(sn->name.data, sn->name.data, sn->name.len);
4309 continue;
4312 #if (NGX_PCRE)
4314 u_char *p;
4315 ngx_regex_compile_t rc;
4316 u_char errstr[NGX_MAX_CONF_ERRSTR];
4318 if (value[i].len == 1) {
4319 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4320 "empty regex in server name \"%V\"", &value[i]);
4321 return NGX_CONF_ERROR;
4324 value[i].len--;
4325 value[i].data++;
4327 ngx_memzero(&rc, sizeof(ngx_regex_compile_t));
4329 rc.pattern = value[i];
4330 rc.err.len = NGX_MAX_CONF_ERRSTR;
4331 rc.err.data = errstr;
4333 for (p = value[i].data; p < value[i].data + value[i].len; p++) {
4334 if (*p >= 'A' && *p <= 'Z') {
4335 rc.options = NGX_REGEX_CASELESS;
4336 break;
4340 sn->regex = ngx_http_regex_compile(cf, &rc);
4341 if (sn->regex == NULL) {
4342 return NGX_CONF_ERROR;
4345 sn->name = value[i];
4346 cscf->captures = (rc.captures > 0);
4348 #else
4349 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4350 "using regex \"%V\" "
4351 "requires PCRE library", &value[i]);
4353 return NGX_CONF_ERROR;
4354 #endif
4357 return NGX_CONF_OK;
4361 static char *
4362 ngx_http_core_root(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
4364 ngx_http_core_loc_conf_t *clcf = conf;
4366 ngx_str_t *value;
4367 ngx_int_t alias;
4368 ngx_uint_t n;
4369 ngx_http_script_compile_t sc;
4371 alias = (cmd->name.len == sizeof("alias") - 1) ? 1 : 0;
4373 if (clcf->root.data) {
4375 if ((clcf->alias != 0) == alias) {
4376 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4377 "\"%V\" directive is duplicate",
4378 &cmd->name);
4379 } else {
4380 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4381 "\"%V\" directive is duplicate, "
4382 "\"%s\" directive was specified earlier",
4383 &cmd->name, clcf->alias ? "alias" : "root");
4386 return NGX_CONF_ERROR;
4389 if (clcf->named && alias) {
4390 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4391 "the \"alias\" directive cannot be used "
4392 "inside the named location");
4394 return NGX_CONF_ERROR;
4397 value = cf->args->elts;
4399 if (ngx_strstr(value[1].data, "$document_root")
4400 || ngx_strstr(value[1].data, "${document_root}"))
4402 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4403 "the $document_root variable cannot be used "
4404 "in the \"%V\" directive",
4405 &cmd->name);
4407 return NGX_CONF_ERROR;
4410 if (ngx_strstr(value[1].data, "$realpath_root")
4411 || ngx_strstr(value[1].data, "${realpath_root}"))
4413 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4414 "the $realpath_root variable cannot be used "
4415 "in the \"%V\" directive",
4416 &cmd->name);
4418 return NGX_CONF_ERROR;
4421 clcf->alias = alias ? clcf->name.len : 0;
4422 clcf->root = value[1];
4424 if (!alias && clcf->root.data[clcf->root.len - 1] == '/') {
4425 clcf->root.len--;
4428 if (clcf->root.data[0] != '$') {
4429 if (ngx_conf_full_name(cf->cycle, &clcf->root, 0) != NGX_OK) {
4430 return NGX_CONF_ERROR;
4434 n = ngx_http_script_variables_count(&clcf->root);
4436 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
4437 sc.variables = n;
4439 #if (NGX_PCRE)
4440 if (alias && clcf->regex) {
4441 n = 1;
4443 #endif
4445 if (n) {
4446 sc.cf = cf;
4447 sc.source = &clcf->root;
4448 sc.lengths = &clcf->root_lengths;
4449 sc.values = &clcf->root_values;
4450 sc.complete_lengths = 1;
4451 sc.complete_values = 1;
4453 if (ngx_http_script_compile(&sc) != NGX_OK) {
4454 return NGX_CONF_ERROR;
4458 return NGX_CONF_OK;
4462 static ngx_http_method_name_t ngx_methods_names[] = {
4463 { (u_char *) "GET", (uint32_t) ~NGX_HTTP_GET },
4464 { (u_char *) "HEAD", (uint32_t) ~NGX_HTTP_HEAD },
4465 { (u_char *) "POST", (uint32_t) ~NGX_HTTP_POST },
4466 { (u_char *) "PUT", (uint32_t) ~NGX_HTTP_PUT },
4467 { (u_char *) "DELETE", (uint32_t) ~NGX_HTTP_DELETE },
4468 { (u_char *) "MKCOL", (uint32_t) ~NGX_HTTP_MKCOL },
4469 { (u_char *) "COPY", (uint32_t) ~NGX_HTTP_COPY },
4470 { (u_char *) "MOVE", (uint32_t) ~NGX_HTTP_MOVE },
4471 { (u_char *) "OPTIONS", (uint32_t) ~NGX_HTTP_OPTIONS },
4472 { (u_char *) "PROPFIND" , (uint32_t) ~NGX_HTTP_PROPFIND },
4473 { (u_char *) "PROPPATCH", (uint32_t) ~NGX_HTTP_PROPPATCH },
4474 { (u_char *) "LOCK", (uint32_t) ~NGX_HTTP_LOCK },
4475 { (u_char *) "UNLOCK", (uint32_t) ~NGX_HTTP_UNLOCK },
4476 { (u_char *) "PATCH", (uint32_t) ~NGX_HTTP_PATCH },
4477 { NULL, 0 }
4481 static char *
4482 ngx_http_core_limit_except(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
4484 ngx_http_core_loc_conf_t *pclcf = conf;
4486 char *rv;
4487 void *mconf;
4488 ngx_str_t *value;
4489 ngx_uint_t i;
4490 ngx_conf_t save;
4491 ngx_http_module_t *module;
4492 ngx_http_conf_ctx_t *ctx, *pctx;
4493 ngx_http_method_name_t *name;
4494 ngx_http_core_loc_conf_t *clcf;
4496 if (pclcf->limit_except) {
4497 return "duplicate";
4500 pclcf->limit_except = 0xffffffff;
4502 value = cf->args->elts;
4504 for (i = 1; i < cf->args->nelts; i++) {
4505 for (name = ngx_methods_names; name->name; name++) {
4507 if (ngx_strcasecmp(value[i].data, name->name) == 0) {
4508 pclcf->limit_except &= name->method;
4509 goto next;
4513 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4514 "invalid method \"%V\"", &value[i]);
4515 return NGX_CONF_ERROR;
4517 next:
4518 continue;
4521 if (!(pclcf->limit_except & NGX_HTTP_GET)) {
4522 pclcf->limit_except &= (uint32_t) ~NGX_HTTP_HEAD;
4525 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
4526 if (ctx == NULL) {
4527 return NGX_CONF_ERROR;
4530 pctx = cf->ctx;
4531 ctx->main_conf = pctx->main_conf;
4532 ctx->srv_conf = pctx->srv_conf;
4534 ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
4535 if (ctx->loc_conf == NULL) {
4536 return NGX_CONF_ERROR;
4539 for (i = 0; ngx_modules[i]; i++) {
4540 if (ngx_modules[i]->type != NGX_HTTP_MODULE) {
4541 continue;
4544 module = ngx_modules[i]->ctx;
4546 if (module->create_loc_conf) {
4548 mconf = module->create_loc_conf(cf);
4549 if (mconf == NULL) {
4550 return NGX_CONF_ERROR;
4553 ctx->loc_conf[ngx_modules[i]->ctx_index] = mconf;
4558 clcf = ctx->loc_conf[ngx_http_core_module.ctx_index];
4559 pclcf->limit_except_loc_conf = ctx->loc_conf;
4560 clcf->loc_conf = ctx->loc_conf;
4561 clcf->name = pclcf->name;
4562 clcf->noname = 1;
4563 clcf->lmt_excpt = 1;
4565 if (ngx_http_add_location(cf, &pclcf->locations, clcf) != NGX_OK) {
4566 return NGX_CONF_ERROR;
4569 save = *cf;
4570 cf->ctx = ctx;
4571 cf->cmd_type = NGX_HTTP_LMT_CONF;
4573 rv = ngx_conf_parse(cf, NULL);
4575 *cf = save;
4577 return rv;
4581 static char *
4582 ngx_http_core_directio(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
4584 ngx_http_core_loc_conf_t *clcf = conf;
4586 ngx_str_t *value;
4588 if (clcf->directio != NGX_CONF_UNSET) {
4589 return "is duplicate";
4592 value = cf->args->elts;
4594 if (ngx_strcmp(value[1].data, "off") == 0) {
4595 clcf->directio = NGX_OPEN_FILE_DIRECTIO_OFF;
4596 return NGX_CONF_OK;
4599 clcf->directio = ngx_parse_offset(&value[1]);
4600 if (clcf->directio == (off_t) NGX_ERROR) {
4601 return "invalid value";
4604 return NGX_CONF_OK;
4608 static char *
4609 ngx_http_core_error_page(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
4611 ngx_http_core_loc_conf_t *clcf = conf;
4613 u_char *p;
4614 ngx_int_t overwrite;
4615 ngx_str_t *value, uri, args;
4616 ngx_uint_t i, n;
4617 ngx_http_err_page_t *err;
4618 ngx_http_complex_value_t cv;
4619 ngx_http_compile_complex_value_t ccv;
4621 if (clcf->error_pages == NULL) {
4622 clcf->error_pages = ngx_array_create(cf->pool, 4,
4623 sizeof(ngx_http_err_page_t));
4624 if (clcf->error_pages == NULL) {
4625 return NGX_CONF_ERROR;
4629 value = cf->args->elts;
4631 i = cf->args->nelts - 2;
4633 if (value[i].data[0] == '=') {
4634 if (i == 1) {
4635 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4636 "invalid value \"%V\"", &value[i]);
4637 return NGX_CONF_ERROR;
4640 if (value[i].len > 1) {
4641 overwrite = ngx_atoi(&value[i].data[1], value[i].len - 1);
4643 if (overwrite == NGX_ERROR) {
4644 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4645 "invalid value \"%V\"", &value[i]);
4646 return NGX_CONF_ERROR;
4649 } else {
4650 overwrite = 0;
4653 n = 2;
4655 } else {
4656 overwrite = -1;
4657 n = 1;
4660 uri = value[cf->args->nelts - 1];
4662 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
4664 ccv.cf = cf;
4665 ccv.value = &uri;
4666 ccv.complex_value = &cv;
4668 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
4669 return NGX_CONF_ERROR;
4672 ngx_str_null(&args);
4674 if (cv.lengths == NULL && uri.len && uri.data[0] == '/') {
4675 p = (u_char *) ngx_strchr(uri.data, '?');
4677 if (p) {
4678 cv.value.len = p - uri.data;
4679 cv.value.data = uri.data;
4680 p++;
4681 args.len = (uri.data + uri.len) - p;
4682 args.data = p;
4686 for (i = 1; i < cf->args->nelts - n; i++) {
4687 err = ngx_array_push(clcf->error_pages);
4688 if (err == NULL) {
4689 return NGX_CONF_ERROR;
4692 err->status = ngx_atoi(value[i].data, value[i].len);
4694 if (err->status == NGX_ERROR || err->status == 499) {
4695 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4696 "invalid value \"%V\"", &value[i]);
4697 return NGX_CONF_ERROR;
4700 if (err->status < 300 || err->status > 599) {
4701 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4702 "value \"%V\" must be between 300 and 599",
4703 &value[i]);
4704 return NGX_CONF_ERROR;
4707 err->overwrite = overwrite;
4709 if (overwrite == -1) {
4710 switch (err->status) {
4711 case NGX_HTTP_TO_HTTPS:
4712 case NGX_HTTPS_CERT_ERROR:
4713 case NGX_HTTPS_NO_CERT:
4714 err->overwrite = NGX_HTTP_BAD_REQUEST;
4715 default:
4716 break;
4720 err->value = cv;
4721 err->args = args;
4724 return NGX_CONF_OK;
4728 static char *
4729 ngx_http_core_try_files(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
4731 ngx_http_core_loc_conf_t *clcf = conf;
4733 ngx_str_t *value;
4734 ngx_int_t code;
4735 ngx_uint_t i, n;
4736 ngx_http_try_file_t *tf;
4737 ngx_http_script_compile_t sc;
4738 ngx_http_core_main_conf_t *cmcf;
4740 if (clcf->try_files) {
4741 return "is duplicate";
4744 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
4746 cmcf->try_files = 1;
4748 tf = ngx_pcalloc(cf->pool, cf->args->nelts * sizeof(ngx_http_try_file_t));
4749 if (tf == NULL) {
4750 return NGX_CONF_ERROR;
4753 clcf->try_files = tf;
4755 value = cf->args->elts;
4757 for (i = 0; i < cf->args->nelts - 1; i++) {
4759 tf[i].name = value[i + 1];
4761 if (tf[i].name.data[tf[i].name.len - 1] == '/') {
4762 tf[i].test_dir = 1;
4763 tf[i].name.len--;
4764 tf[i].name.data[tf[i].name.len] = '\0';
4767 n = ngx_http_script_variables_count(&tf[i].name);
4769 if (n) {
4770 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
4772 sc.cf = cf;
4773 sc.source = &tf[i].name;
4774 sc.lengths = &tf[i].lengths;
4775 sc.values = &tf[i].values;
4776 sc.variables = n;
4777 sc.complete_lengths = 1;
4778 sc.complete_values = 1;
4780 if (ngx_http_script_compile(&sc) != NGX_OK) {
4781 return NGX_CONF_ERROR;
4784 } else {
4785 /* add trailing '\0' to length */
4786 tf[i].name.len++;
4790 if (tf[i - 1].name.data[0] == '=') {
4792 code = ngx_atoi(tf[i - 1].name.data + 1, tf[i - 1].name.len - 2);
4794 if (code == NGX_ERROR || code > 999) {
4795 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4796 "invalid code \"%*s\"",
4797 tf[i - 1].name.len - 1, tf[i - 1].name.data);
4798 return NGX_CONF_ERROR;
4801 tf[i].code = code;
4804 return NGX_CONF_OK;
4808 static char *
4809 ngx_http_core_open_file_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
4811 ngx_http_core_loc_conf_t *clcf = conf;
4813 time_t inactive;
4814 ngx_str_t *value, s;
4815 ngx_int_t max;
4816 ngx_uint_t i;
4818 if (clcf->open_file_cache != NGX_CONF_UNSET_PTR) {
4819 return "is duplicate";
4822 value = cf->args->elts;
4824 max = 0;
4825 inactive = 60;
4827 for (i = 1; i < cf->args->nelts; i++) {
4829 if (ngx_strncmp(value[i].data, "max=", 4) == 0) {
4831 max = ngx_atoi(value[i].data + 4, value[i].len - 4);
4832 if (max <= 0) {
4833 goto failed;
4836 continue;
4839 if (ngx_strncmp(value[i].data, "inactive=", 9) == 0) {
4841 s.len = value[i].len - 9;
4842 s.data = value[i].data + 9;
4844 inactive = ngx_parse_time(&s, 1);
4845 if (inactive == (time_t) NGX_ERROR) {
4846 goto failed;
4849 continue;
4852 if (ngx_strcmp(value[i].data, "off") == 0) {
4854 clcf->open_file_cache = NULL;
4856 continue;
4859 failed:
4861 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4862 "invalid \"open_file_cache\" parameter \"%V\"",
4863 &value[i]);
4864 return NGX_CONF_ERROR;
4867 if (clcf->open_file_cache == NULL) {
4868 return NGX_CONF_OK;
4871 if (max == 0) {
4872 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4873 "\"open_file_cache\" must have the \"max\" parameter");
4874 return NGX_CONF_ERROR;
4877 clcf->open_file_cache = ngx_open_file_cache_init(cf->pool, max, inactive);
4878 if (clcf->open_file_cache) {
4879 return NGX_CONF_OK;
4882 return NGX_CONF_ERROR;
4886 static char *
4887 ngx_http_core_error_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
4889 ngx_http_core_loc_conf_t *clcf = conf;
4891 ngx_str_t *value, name;
4893 if (clcf->error_log) {
4894 return "is duplicate";
4897 value = cf->args->elts;
4899 if (ngx_strcmp(value[1].data, "stderr") == 0) {
4900 ngx_str_null(&name);
4902 } else {
4903 name = value[1];
4906 clcf->error_log = ngx_log_create(cf->cycle, &name);
4907 if (clcf->error_log == NULL) {
4908 return NGX_CONF_ERROR;
4911 if (cf->args->nelts == 2) {
4912 clcf->error_log->log_level = NGX_LOG_ERR;
4913 return NGX_CONF_OK;
4916 return ngx_log_set_levels(cf, clcf->error_log);
4920 static char *
4921 ngx_http_core_keepalive(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
4923 ngx_http_core_loc_conf_t *clcf = conf;
4925 ngx_str_t *value;
4927 if (clcf->keepalive_timeout != NGX_CONF_UNSET_MSEC) {
4928 return "is duplicate";
4931 value = cf->args->elts;
4933 clcf->keepalive_timeout = ngx_parse_time(&value[1], 0);
4935 if (clcf->keepalive_timeout == (ngx_msec_t) NGX_ERROR) {
4936 return "invalid value";
4939 if (cf->args->nelts == 2) {
4940 return NGX_CONF_OK;
4943 clcf->keepalive_header = ngx_parse_time(&value[2], 1);
4945 if (clcf->keepalive_header == (time_t) NGX_ERROR) {
4946 return "invalid value";
4949 return NGX_CONF_OK;
4953 static char *
4954 ngx_http_core_internal(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
4956 ngx_http_core_loc_conf_t *clcf = conf;
4958 if (clcf->internal != NGX_CONF_UNSET) {
4959 return "is duplicate";
4962 clcf->internal = 1;
4964 return NGX_CONF_OK;
4968 static char *
4969 ngx_http_core_resolver(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
4971 ngx_http_core_loc_conf_t *clcf = conf;
4973 ngx_str_t *value;
4975 if (clcf->resolver) {
4976 return "is duplicate";
4979 value = cf->args->elts;
4981 clcf->resolver = ngx_resolver_create(cf, &value[1], cf->args->nelts - 1);
4982 if (clcf->resolver == NULL) {
4983 return NGX_CONF_ERROR;
4986 return NGX_CONF_OK;
4990 #if (NGX_HTTP_GZIP)
4992 static char *
4993 ngx_http_gzip_disable(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
4995 ngx_http_core_loc_conf_t *clcf = conf;
4997 #if (NGX_PCRE)
4999 ngx_str_t *value;
5000 ngx_uint_t i;
5001 ngx_regex_elt_t *re;
5002 ngx_regex_compile_t rc;
5003 u_char errstr[NGX_MAX_CONF_ERRSTR];
5005 if (clcf->gzip_disable == NGX_CONF_UNSET_PTR) {
5006 clcf->gzip_disable = ngx_array_create(cf->pool, 2,
5007 sizeof(ngx_regex_elt_t));
5008 if (clcf->gzip_disable == NULL) {
5009 return NGX_CONF_ERROR;
5013 value = cf->args->elts;
5015 ngx_memzero(&rc, sizeof(ngx_regex_compile_t));
5017 rc.pool = cf->pool;
5018 rc.err.len = NGX_MAX_CONF_ERRSTR;
5019 rc.err.data = errstr;
5021 for (i = 1; i < cf->args->nelts; i++) {
5023 if (ngx_strcmp(value[i].data, "msie6") == 0) {
5024 clcf->gzip_disable_msie6 = 1;
5025 continue;
5028 #if (NGX_HTTP_DEGRADATION)
5030 if (ngx_strcmp(value[i].data, "degradation") == 0) {
5031 clcf->gzip_disable_degradation = 1;
5032 continue;
5035 #endif
5037 re = ngx_array_push(clcf->gzip_disable);
5038 if (re == NULL) {
5039 return NGX_CONF_ERROR;
5042 rc.pattern = value[i];
5043 rc.options = NGX_REGEX_CASELESS;
5045 if (ngx_regex_compile(&rc) != NGX_OK) {
5046 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "%V", &rc.err);
5047 return NGX_CONF_ERROR;
5050 re->regex = rc.regex;
5051 re->name = value[i].data;
5054 return NGX_CONF_OK;
5056 #else
5057 ngx_str_t *value;
5058 ngx_uint_t i;
5060 value = cf->args->elts;
5062 for (i = 1; i < cf->args->nelts; i++) {
5063 if (ngx_strcmp(value[i].data, "msie6") == 0) {
5064 clcf->gzip_disable_msie6 = 1;
5065 continue;
5068 #if (NGX_HTTP_DEGRADATION)
5070 if (ngx_strcmp(value[i].data, "degradation") == 0) {
5071 clcf->gzip_disable_degradation = 1;
5072 continue;
5075 #endif
5077 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
5078 "without PCRE library \"gzip_disable\" supports "
5079 "builtin \"msie6\" and \"degradation\" mask only");
5081 return NGX_CONF_ERROR;
5084 return NGX_CONF_OK;
5086 #endif
5089 #endif
5092 #if (NGX_HAVE_OPENAT)
5094 static char *
5095 ngx_http_disable_symlinks(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
5097 ngx_http_core_loc_conf_t *clcf = conf;
5099 ngx_str_t *value;
5100 ngx_uint_t i;
5101 ngx_http_compile_complex_value_t ccv;
5103 if (clcf->disable_symlinks != NGX_CONF_UNSET_UINT) {
5104 return "is duplicate";
5107 value = cf->args->elts;
5109 for (i = 1; i < cf->args->nelts; i++) {
5111 if (ngx_strcmp(value[i].data, "off") == 0) {
5112 clcf->disable_symlinks = NGX_DISABLE_SYMLINKS_OFF;
5113 continue;
5116 if (ngx_strcmp(value[i].data, "if_not_owner") == 0) {
5117 clcf->disable_symlinks = NGX_DISABLE_SYMLINKS_NOTOWNER;
5118 continue;
5121 if (ngx_strcmp(value[i].data, "on") == 0) {
5122 clcf->disable_symlinks = NGX_DISABLE_SYMLINKS_ON;
5123 continue;
5126 if (ngx_strncmp(value[i].data, "from=", 5) == 0) {
5127 value[i].len -= 5;
5128 value[i].data += 5;
5130 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
5132 ccv.cf = cf;
5133 ccv.value = &value[i];
5134 ccv.complex_value = ngx_palloc(cf->pool,
5135 sizeof(ngx_http_complex_value_t));
5136 if (ccv.complex_value == NULL) {
5137 return NGX_CONF_ERROR;
5140 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
5141 return NGX_CONF_ERROR;
5144 clcf->disable_symlinks_from = ccv.complex_value;
5146 continue;
5149 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
5150 "invalid parameter \"%V\"", &value[i]);
5151 return NGX_CONF_ERROR;
5154 if (clcf->disable_symlinks == NGX_CONF_UNSET_UINT) {
5155 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
5156 "\"%V\" must have \"off\", \"on\" "
5157 "or \"if_not_owner\" parameter",
5158 &cmd->name);
5159 return NGX_CONF_ERROR;
5162 if (cf->args->nelts == 2) {
5163 clcf->disable_symlinks_from = NULL;
5164 return NGX_CONF_OK;
5167 if (clcf->disable_symlinks_from == NGX_CONF_UNSET_PTR) {
5168 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
5169 "duplicate parameters \"%V %V\"",
5170 &value[1], &value[2]);
5171 return NGX_CONF_ERROR;
5174 if (clcf->disable_symlinks == NGX_DISABLE_SYMLINKS_OFF) {
5175 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
5176 "\"from=\" cannot be used with \"off\" parameter");
5177 return NGX_CONF_ERROR;
5180 return NGX_CONF_OK;
5183 #endif
5186 static char *
5187 ngx_http_core_lowat_check(ngx_conf_t *cf, void *post, void *data)
5189 #if (NGX_FREEBSD)
5190 ssize_t *np = data;
5192 if ((u_long) *np >= ngx_freebsd_net_inet_tcp_sendspace) {
5193 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
5194 "\"send_lowat\" must be less than %d "
5195 "(sysctl net.inet.tcp.sendspace)",
5196 ngx_freebsd_net_inet_tcp_sendspace);
5198 return NGX_CONF_ERROR;
5201 #elif !(NGX_HAVE_SO_SNDLOWAT)
5202 ssize_t *np = data;
5204 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
5205 "\"send_lowat\" is not supported, ignored");
5207 *np = 0;
5209 #endif
5211 return NGX_CONF_OK;
5215 static char *
5216 ngx_http_core_pool_size(ngx_conf_t *cf, void *post, void *data)
5218 size_t *sp = data;
5220 if (*sp < NGX_MIN_POOL_SIZE) {
5221 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
5222 "the pool size must be no less than %uz",
5223 NGX_MIN_POOL_SIZE);
5224 return NGX_CONF_ERROR;
5227 if (*sp % NGX_POOL_ALIGNMENT) {
5228 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
5229 "the pool size must be a multiple of %uz",
5230 NGX_POOL_ALIGNMENT);
5231 return NGX_CONF_ERROR;
5234 return NGX_CONF_OK;