source: nginx/src/http/ngx_http_core_module.c@ 6006:942283a53c28

Last change on this file since 6006:942283a53c28 was 6006:942283a53c28, checked in by Ruslan Ermilov <ru@…>, 5 years ago

The "aio" directive parser made smarter.

It now prints meaningful warnings on all platforms.

No functional changes.

File size: 141.8 KB
Line 
1
2/*
3 * Copyright (C) Igor Sysoev
4 * Copyright (C) Nginx, Inc.
5 */
6
7
8#include <ngx_config.h>
9#include <ngx_core.h>
10#include <ngx_http.h>
11
12
13typedef struct {
14 u_char *name;
15 uint32_t method;
16} ngx_http_method_name_t;
17
18
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
22
23
24static ngx_int_t ngx_http_core_find_location(ngx_http_request_t *r);
25static ngx_int_t ngx_http_core_find_static_location(ngx_http_request_t *r,
26 ngx_http_location_tree_node_t *node);
27
28static ngx_int_t ngx_http_core_preconfiguration(ngx_conf_t *cf);
29static void *ngx_http_core_create_main_conf(ngx_conf_t *cf);
30static char *ngx_http_core_init_main_conf(ngx_conf_t *cf, void *conf);
31static void *ngx_http_core_create_srv_conf(ngx_conf_t *cf);
32static char *ngx_http_core_merge_srv_conf(ngx_conf_t *cf,
33 void *parent, void *child);
34static void *ngx_http_core_create_loc_conf(ngx_conf_t *cf);
35static char *ngx_http_core_merge_loc_conf(ngx_conf_t *cf,
36 void *parent, void *child);
37
38static char *ngx_http_core_server(ngx_conf_t *cf, ngx_command_t *cmd,
39 void *dummy);
40static char *ngx_http_core_location(ngx_conf_t *cf, ngx_command_t *cmd,
41 void *dummy);
42static 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);
44
45static char *ngx_http_core_types(ngx_conf_t *cf, ngx_command_t *cmd,
46 void *conf);
47static char *ngx_http_core_type(ngx_conf_t *cf, ngx_command_t *dummy,
48 void *conf);
49
50static char *ngx_http_core_listen(ngx_conf_t *cf, ngx_command_t *cmd,
51 void *conf);
52static char *ngx_http_core_server_name(ngx_conf_t *cf, ngx_command_t *cmd,
53 void *conf);
54static char *ngx_http_core_root(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
55static char *ngx_http_core_limit_except(ngx_conf_t *cf, ngx_command_t *cmd,
56 void *conf);
57static char *ngx_http_core_set_aio(ngx_conf_t *cf, ngx_command_t *cmd,
58 void *conf);
59static char *ngx_http_core_directio(ngx_conf_t *cf, ngx_command_t *cmd,
60 void *conf);
61static char *ngx_http_core_error_page(ngx_conf_t *cf, ngx_command_t *cmd,
62 void *conf);
63static char *ngx_http_core_try_files(ngx_conf_t *cf, ngx_command_t *cmd,
64 void *conf);
65static char *ngx_http_core_open_file_cache(ngx_conf_t *cf, ngx_command_t *cmd,
66 void *conf);
67static char *ngx_http_core_error_log(ngx_conf_t *cf, ngx_command_t *cmd,
68 void *conf);
69static char *ngx_http_core_keepalive(ngx_conf_t *cf, ngx_command_t *cmd,
70 void *conf);
71static char *ngx_http_core_internal(ngx_conf_t *cf, ngx_command_t *cmd,
72 void *conf);
73static char *ngx_http_core_resolver(ngx_conf_t *cf, ngx_command_t *cmd,
74 void *conf);
75#if (NGX_HTTP_GZIP)
76static ngx_int_t ngx_http_gzip_accept_encoding(ngx_str_t *ae);
77static ngx_uint_t ngx_http_gzip_quantity(u_char *p, u_char *last);
78static char *ngx_http_gzip_disable(ngx_conf_t *cf, ngx_command_t *cmd,
79 void *conf);
80#endif
81static ngx_int_t ngx_http_get_forwarded_addr_internal(ngx_http_request_t *r,
82 ngx_addr_t *addr, u_char *xff, size_t xfflen, ngx_array_t *proxies,
83 int recursive);
84#if (NGX_HAVE_OPENAT)
85static char *ngx_http_disable_symlinks(ngx_conf_t *cf, ngx_command_t *cmd,
86 void *conf);
87#endif
88
89static char *ngx_http_core_lowat_check(ngx_conf_t *cf, void *post, void *data);
90static char *ngx_http_core_pool_size(ngx_conf_t *cf, void *post, void *data);
91
92static ngx_conf_post_t ngx_http_core_lowat_post =
93 { ngx_http_core_lowat_check };
94
95static ngx_conf_post_handler_pt ngx_http_core_pool_size_p =
96 ngx_http_core_pool_size;
97
98static ngx_conf_deprecated_t ngx_conf_deprecated_optimize_server_names = {
99 ngx_conf_deprecated, "optimize_server_names", "server_name_in_redirect"
100};
101
102static ngx_conf_deprecated_t ngx_conf_deprecated_open_file_cache_retest = {
103 ngx_conf_deprecated, "open_file_cache_retest", "open_file_cache_valid"
104};
105
106static ngx_conf_deprecated_t ngx_conf_deprecated_satisfy_any = {
107 ngx_conf_deprecated, "satisfy_any", "satisfy"
108};
109
110
111static ngx_conf_enum_t ngx_http_core_request_body_in_file[] = {
112 { ngx_string("off"), NGX_HTTP_REQUEST_BODY_FILE_OFF },
113 { ngx_string("on"), NGX_HTTP_REQUEST_BODY_FILE_ON },
114 { ngx_string("clean"), NGX_HTTP_REQUEST_BODY_FILE_CLEAN },
115 { ngx_null_string, 0 }
116};
117
118
119static ngx_conf_enum_t ngx_http_core_satisfy[] = {
120 { ngx_string("all"), NGX_HTTP_SATISFY_ALL },
121 { ngx_string("any"), NGX_HTTP_SATISFY_ANY },
122 { ngx_null_string, 0 }
123};
124
125
126static ngx_conf_enum_t ngx_http_core_lingering_close[] = {
127 { ngx_string("off"), NGX_HTTP_LINGERING_OFF },
128 { ngx_string("on"), NGX_HTTP_LINGERING_ON },
129 { ngx_string("always"), NGX_HTTP_LINGERING_ALWAYS },
130 { ngx_null_string, 0 }
131};
132
133
134static ngx_conf_enum_t ngx_http_core_if_modified_since[] = {
135 { ngx_string("off"), NGX_HTTP_IMS_OFF },
136 { ngx_string("exact"), NGX_HTTP_IMS_EXACT },
137 { ngx_string("before"), NGX_HTTP_IMS_BEFORE },
138 { ngx_null_string, 0 }
139};
140
141
142static ngx_conf_bitmask_t ngx_http_core_keepalive_disable[] = {
143 { ngx_string("none"), NGX_HTTP_KEEPALIVE_DISABLE_NONE },
144 { ngx_string("msie6"), NGX_HTTP_KEEPALIVE_DISABLE_MSIE6 },
145 { ngx_string("safari"), NGX_HTTP_KEEPALIVE_DISABLE_SAFARI },
146 { ngx_null_string, 0 }
147};
148
149
150static ngx_path_init_t ngx_http_client_temp_path = {
151 ngx_string(NGX_HTTP_CLIENT_TEMP_PATH), { 0, 0, 0 }
152};
153
154
155#if (NGX_HTTP_GZIP)
156
157static ngx_conf_enum_t ngx_http_gzip_http_version[] = {
158 { ngx_string("1.0"), NGX_HTTP_VERSION_10 },
159 { ngx_string("1.1"), NGX_HTTP_VERSION_11 },
160 { ngx_null_string, 0 }
161};
162
163
164static ngx_conf_bitmask_t ngx_http_gzip_proxied_mask[] = {
165 { ngx_string("off"), NGX_HTTP_GZIP_PROXIED_OFF },
166 { ngx_string("expired"), NGX_HTTP_GZIP_PROXIED_EXPIRED },
167 { ngx_string("no-cache"), NGX_HTTP_GZIP_PROXIED_NO_CACHE },
168 { ngx_string("no-store"), NGX_HTTP_GZIP_PROXIED_NO_STORE },
169 { ngx_string("private"), NGX_HTTP_GZIP_PROXIED_PRIVATE },
170 { ngx_string("no_last_modified"), NGX_HTTP_GZIP_PROXIED_NO_LM },
171 { ngx_string("no_etag"), NGX_HTTP_GZIP_PROXIED_NO_ETAG },
172 { ngx_string("auth"), NGX_HTTP_GZIP_PROXIED_AUTH },
173 { ngx_string("any"), NGX_HTTP_GZIP_PROXIED_ANY },
174 { ngx_null_string, 0 }
175};
176
177
178static ngx_str_t ngx_http_gzip_no_cache = ngx_string("no-cache");
179static ngx_str_t ngx_http_gzip_no_store = ngx_string("no-store");
180static ngx_str_t ngx_http_gzip_private = ngx_string("private");
181
182#endif
183
184
185static ngx_command_t ngx_http_core_commands[] = {
186
187 { ngx_string("variables_hash_max_size"),
188 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
189 ngx_conf_set_num_slot,
190 NGX_HTTP_MAIN_CONF_OFFSET,
191 offsetof(ngx_http_core_main_conf_t, variables_hash_max_size),
192 NULL },
193
194 { ngx_string("variables_hash_bucket_size"),
195 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
196 ngx_conf_set_num_slot,
197 NGX_HTTP_MAIN_CONF_OFFSET,
198 offsetof(ngx_http_core_main_conf_t, variables_hash_bucket_size),
199 NULL },
200
201 { ngx_string("server_names_hash_max_size"),
202 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
203 ngx_conf_set_num_slot,
204 NGX_HTTP_MAIN_CONF_OFFSET,
205 offsetof(ngx_http_core_main_conf_t, server_names_hash_max_size),
206 NULL },
207
208 { ngx_string("server_names_hash_bucket_size"),
209 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
210 ngx_conf_set_num_slot,
211 NGX_HTTP_MAIN_CONF_OFFSET,
212 offsetof(ngx_http_core_main_conf_t, server_names_hash_bucket_size),
213 NULL },
214
215 { ngx_string("server"),
216 NGX_HTTP_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
217 ngx_http_core_server,
218 0,
219 0,
220 NULL },
221
222 { ngx_string("connection_pool_size"),
223 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
224 ngx_conf_set_size_slot,
225 NGX_HTTP_SRV_CONF_OFFSET,
226 offsetof(ngx_http_core_srv_conf_t, connection_pool_size),
227 &ngx_http_core_pool_size_p },
228
229 { ngx_string("request_pool_size"),
230 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
231 ngx_conf_set_size_slot,
232 NGX_HTTP_SRV_CONF_OFFSET,
233 offsetof(ngx_http_core_srv_conf_t, request_pool_size),
234 &ngx_http_core_pool_size_p },
235
236 { ngx_string("client_header_timeout"),
237 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
238 ngx_conf_set_msec_slot,
239 NGX_HTTP_SRV_CONF_OFFSET,
240 offsetof(ngx_http_core_srv_conf_t, client_header_timeout),
241 NULL },
242
243 { ngx_string("client_header_buffer_size"),
244 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
245 ngx_conf_set_size_slot,
246 NGX_HTTP_SRV_CONF_OFFSET,
247 offsetof(ngx_http_core_srv_conf_t, client_header_buffer_size),
248 NULL },
249
250 { ngx_string("large_client_header_buffers"),
251 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE2,
252 ngx_conf_set_bufs_slot,
253 NGX_HTTP_SRV_CONF_OFFSET,
254 offsetof(ngx_http_core_srv_conf_t, large_client_header_buffers),
255 NULL },
256
257 { ngx_string("optimize_server_names"),
258 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_FLAG,
259 ngx_conf_set_flag_slot,
260 NGX_HTTP_LOC_CONF_OFFSET,
261 offsetof(ngx_http_core_loc_conf_t, server_name_in_redirect),
262 &ngx_conf_deprecated_optimize_server_names },
263
264 { ngx_string("ignore_invalid_headers"),
265 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_FLAG,
266 ngx_conf_set_flag_slot,
267 NGX_HTTP_SRV_CONF_OFFSET,
268 offsetof(ngx_http_core_srv_conf_t, ignore_invalid_headers),
269 NULL },
270
271 { ngx_string("merge_slashes"),
272 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_FLAG,
273 ngx_conf_set_flag_slot,
274 NGX_HTTP_SRV_CONF_OFFSET,
275 offsetof(ngx_http_core_srv_conf_t, merge_slashes),
276 NULL },
277
278 { ngx_string("underscores_in_headers"),
279 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_FLAG,
280 ngx_conf_set_flag_slot,
281 NGX_HTTP_SRV_CONF_OFFSET,
282 offsetof(ngx_http_core_srv_conf_t, underscores_in_headers),
283 NULL },
284
285 { ngx_string("location"),
286 NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE12,
287 ngx_http_core_location,
288 NGX_HTTP_SRV_CONF_OFFSET,
289 0,
290 NULL },
291
292 { ngx_string("listen"),
293 NGX_HTTP_SRV_CONF|NGX_CONF_1MORE,
294 ngx_http_core_listen,
295 NGX_HTTP_SRV_CONF_OFFSET,
296 0,
297 NULL },
298
299 { ngx_string("server_name"),
300 NGX_HTTP_SRV_CONF|NGX_CONF_1MORE,
301 ngx_http_core_server_name,
302 NGX_HTTP_SRV_CONF_OFFSET,
303 0,
304 NULL },
305
306 { ngx_string("types_hash_max_size"),
307 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
308 ngx_conf_set_num_slot,
309 NGX_HTTP_LOC_CONF_OFFSET,
310 offsetof(ngx_http_core_loc_conf_t, types_hash_max_size),
311 NULL },
312
313 { ngx_string("types_hash_bucket_size"),
314 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
315 ngx_conf_set_num_slot,
316 NGX_HTTP_LOC_CONF_OFFSET,
317 offsetof(ngx_http_core_loc_conf_t, types_hash_bucket_size),
318 NULL },
319
320 { ngx_string("types"),
321 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF
322 |NGX_CONF_BLOCK|NGX_CONF_NOARGS,
323 ngx_http_core_types,
324 NGX_HTTP_LOC_CONF_OFFSET,
325 0,
326 NULL },
327
328 { ngx_string("default_type"),
329 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
330 ngx_conf_set_str_slot,
331 NGX_HTTP_LOC_CONF_OFFSET,
332 offsetof(ngx_http_core_loc_conf_t, default_type),
333 NULL },
334
335 { ngx_string("root"),
336 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
337 |NGX_CONF_TAKE1,
338 ngx_http_core_root,
339 NGX_HTTP_LOC_CONF_OFFSET,
340 0,
341 NULL },
342
343 { ngx_string("alias"),
344 NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
345 ngx_http_core_root,
346 NGX_HTTP_LOC_CONF_OFFSET,
347 0,
348 NULL },
349
350 { ngx_string("limit_except"),
351 NGX_HTTP_LOC_CONF|NGX_CONF_BLOCK|NGX_CONF_1MORE,
352 ngx_http_core_limit_except,
353 NGX_HTTP_LOC_CONF_OFFSET,
354 0,
355 NULL },
356
357 { ngx_string("client_max_body_size"),
358 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
359 ngx_conf_set_off_slot,
360 NGX_HTTP_LOC_CONF_OFFSET,
361 offsetof(ngx_http_core_loc_conf_t, client_max_body_size),
362 NULL },
363
364 { ngx_string("client_body_buffer_size"),
365 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
366 ngx_conf_set_size_slot,
367 NGX_HTTP_LOC_CONF_OFFSET,
368 offsetof(ngx_http_core_loc_conf_t, client_body_buffer_size),
369 NULL },
370
371 { ngx_string("client_body_timeout"),
372 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
373 ngx_conf_set_msec_slot,
374 NGX_HTTP_LOC_CONF_OFFSET,
375 offsetof(ngx_http_core_loc_conf_t, client_body_timeout),
376 NULL },
377
378 { ngx_string("client_body_temp_path"),
379 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1234,
380 ngx_conf_set_path_slot,
381 NGX_HTTP_LOC_CONF_OFFSET,
382 offsetof(ngx_http_core_loc_conf_t, client_body_temp_path),
383 NULL },
384
385 { ngx_string("client_body_in_file_only"),
386 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
387 ngx_conf_set_enum_slot,
388 NGX_HTTP_LOC_CONF_OFFSET,
389 offsetof(ngx_http_core_loc_conf_t, client_body_in_file_only),
390 &ngx_http_core_request_body_in_file },
391
392 { ngx_string("client_body_in_single_buffer"),
393 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
394 ngx_conf_set_flag_slot,
395 NGX_HTTP_LOC_CONF_OFFSET,
396 offsetof(ngx_http_core_loc_conf_t, client_body_in_single_buffer),
397 NULL },
398
399 { ngx_string("sendfile"),
400 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
401 |NGX_CONF_FLAG,
402 ngx_conf_set_flag_slot,
403 NGX_HTTP_LOC_CONF_OFFSET,
404 offsetof(ngx_http_core_loc_conf_t, sendfile),
405 NULL },
406
407 { ngx_string("sendfile_max_chunk"),
408 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
409 ngx_conf_set_size_slot,
410 NGX_HTTP_LOC_CONF_OFFSET,
411 offsetof(ngx_http_core_loc_conf_t, sendfile_max_chunk),
412 NULL },
413
414 { ngx_string("aio"),
415 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
416 ngx_http_core_set_aio,
417 NGX_HTTP_LOC_CONF_OFFSET,
418 0,
419 NULL },
420
421 { ngx_string("read_ahead"),
422 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
423 ngx_conf_set_size_slot,
424 NGX_HTTP_LOC_CONF_OFFSET,
425 offsetof(ngx_http_core_loc_conf_t, read_ahead),
426 NULL },
427
428 { ngx_string("directio"),
429 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
430 ngx_http_core_directio,
431 NGX_HTTP_LOC_CONF_OFFSET,
432 0,
433 NULL },
434
435 { ngx_string("directio_alignment"),
436 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
437 ngx_conf_set_off_slot,
438 NGX_HTTP_LOC_CONF_OFFSET,
439 offsetof(ngx_http_core_loc_conf_t, directio_alignment),
440 NULL },
441
442 { ngx_string("tcp_nopush"),
443 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
444 ngx_conf_set_flag_slot,
445 NGX_HTTP_LOC_CONF_OFFSET,
446 offsetof(ngx_http_core_loc_conf_t, tcp_nopush),
447 NULL },
448
449 { ngx_string("tcp_nodelay"),
450 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
451 ngx_conf_set_flag_slot,
452 NGX_HTTP_LOC_CONF_OFFSET,
453 offsetof(ngx_http_core_loc_conf_t, tcp_nodelay),
454 NULL },
455
456 { ngx_string("send_timeout"),
457 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
458 ngx_conf_set_msec_slot,
459 NGX_HTTP_LOC_CONF_OFFSET,
460 offsetof(ngx_http_core_loc_conf_t, send_timeout),
461 NULL },
462
463 { ngx_string("send_lowat"),
464 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
465 ngx_conf_set_size_slot,
466 NGX_HTTP_LOC_CONF_OFFSET,
467 offsetof(ngx_http_core_loc_conf_t, send_lowat),
468 &ngx_http_core_lowat_post },
469
470 { ngx_string("postpone_output"),
471 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
472 ngx_conf_set_size_slot,
473 NGX_HTTP_LOC_CONF_OFFSET,
474 offsetof(ngx_http_core_loc_conf_t, postpone_output),
475 NULL },
476
477 { ngx_string("limit_rate"),
478 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
479 |NGX_CONF_TAKE1,
480 ngx_conf_set_size_slot,
481 NGX_HTTP_LOC_CONF_OFFSET,
482 offsetof(ngx_http_core_loc_conf_t, limit_rate),
483 NULL },
484
485 { ngx_string("limit_rate_after"),
486 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
487 |NGX_CONF_TAKE1,
488 ngx_conf_set_size_slot,
489 NGX_HTTP_LOC_CONF_OFFSET,
490 offsetof(ngx_http_core_loc_conf_t, limit_rate_after),
491 NULL },
492
493 { ngx_string("keepalive_timeout"),
494 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
495 ngx_http_core_keepalive,
496 NGX_HTTP_LOC_CONF_OFFSET,
497 0,
498 NULL },
499
500 { ngx_string("keepalive_requests"),
501 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
502 ngx_conf_set_num_slot,
503 NGX_HTTP_LOC_CONF_OFFSET,
504 offsetof(ngx_http_core_loc_conf_t, keepalive_requests),
505 NULL },
506
507 { ngx_string("keepalive_disable"),
508 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
509 ngx_conf_set_bitmask_slot,
510 NGX_HTTP_LOC_CONF_OFFSET,
511 offsetof(ngx_http_core_loc_conf_t, keepalive_disable),
512 &ngx_http_core_keepalive_disable },
513
514 { ngx_string("satisfy"),
515 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
516 ngx_conf_set_enum_slot,
517 NGX_HTTP_LOC_CONF_OFFSET,
518 offsetof(ngx_http_core_loc_conf_t, satisfy),
519 &ngx_http_core_satisfy },
520
521 { ngx_string("satisfy_any"),
522 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
523 ngx_conf_set_flag_slot,
524 NGX_HTTP_LOC_CONF_OFFSET,
525 offsetof(ngx_http_core_loc_conf_t, satisfy),
526 &ngx_conf_deprecated_satisfy_any },
527
528 { ngx_string("internal"),
529 NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,
530 ngx_http_core_internal,
531 NGX_HTTP_LOC_CONF_OFFSET,
532 0,
533 NULL },
534
535 { ngx_string("lingering_close"),
536 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
537 ngx_conf_set_enum_slot,
538 NGX_HTTP_LOC_CONF_OFFSET,
539 offsetof(ngx_http_core_loc_conf_t, lingering_close),
540 &ngx_http_core_lingering_close },
541
542 { ngx_string("lingering_time"),
543 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
544 ngx_conf_set_msec_slot,
545 NGX_HTTP_LOC_CONF_OFFSET,
546 offsetof(ngx_http_core_loc_conf_t, lingering_time),
547 NULL },
548
549 { ngx_string("lingering_timeout"),
550 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
551 ngx_conf_set_msec_slot,
552 NGX_HTTP_LOC_CONF_OFFSET,
553 offsetof(ngx_http_core_loc_conf_t, lingering_timeout),
554 NULL },
555
556 { ngx_string("reset_timedout_connection"),
557 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
558 ngx_conf_set_flag_slot,
559 NGX_HTTP_LOC_CONF_OFFSET,
560 offsetof(ngx_http_core_loc_conf_t, reset_timedout_connection),
561 NULL },
562
563 { ngx_string("server_name_in_redirect"),
564 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
565 ngx_conf_set_flag_slot,
566 NGX_HTTP_LOC_CONF_OFFSET,
567 offsetof(ngx_http_core_loc_conf_t, server_name_in_redirect),
568 NULL },
569
570 { ngx_string("port_in_redirect"),
571 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
572 ngx_conf_set_flag_slot,
573 NGX_HTTP_LOC_CONF_OFFSET,
574 offsetof(ngx_http_core_loc_conf_t, port_in_redirect),
575 NULL },
576
577 { ngx_string("msie_padding"),
578 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
579 ngx_conf_set_flag_slot,
580 NGX_HTTP_LOC_CONF_OFFSET,
581 offsetof(ngx_http_core_loc_conf_t, msie_padding),
582 NULL },
583
584 { ngx_string("msie_refresh"),
585 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
586 ngx_conf_set_flag_slot,
587 NGX_HTTP_LOC_CONF_OFFSET,
588 offsetof(ngx_http_core_loc_conf_t, msie_refresh),
589 NULL },
590
591 { ngx_string("log_not_found"),
592 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
593 ngx_conf_set_flag_slot,
594 NGX_HTTP_LOC_CONF_OFFSET,
595 offsetof(ngx_http_core_loc_conf_t, log_not_found),
596 NULL },
597
598 { ngx_string("log_subrequest"),
599 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
600 ngx_conf_set_flag_slot,
601 NGX_HTTP_LOC_CONF_OFFSET,
602 offsetof(ngx_http_core_loc_conf_t, log_subrequest),
603 NULL },
604
605 { ngx_string("recursive_error_pages"),
606 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
607 ngx_conf_set_flag_slot,
608 NGX_HTTP_LOC_CONF_OFFSET,
609 offsetof(ngx_http_core_loc_conf_t, recursive_error_pages),
610 NULL },
611
612 { ngx_string("server_tokens"),
613 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
614 ngx_conf_set_flag_slot,
615 NGX_HTTP_LOC_CONF_OFFSET,
616 offsetof(ngx_http_core_loc_conf_t, server_tokens),
617 NULL },
618
619 { ngx_string("if_modified_since"),
620 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
621 ngx_conf_set_enum_slot,
622 NGX_HTTP_LOC_CONF_OFFSET,
623 offsetof(ngx_http_core_loc_conf_t, if_modified_since),
624 &ngx_http_core_if_modified_since },
625
626 { ngx_string("max_ranges"),
627 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
628 ngx_conf_set_num_slot,
629 NGX_HTTP_LOC_CONF_OFFSET,
630 offsetof(ngx_http_core_loc_conf_t, max_ranges),
631 NULL },
632
633 { ngx_string("chunked_transfer_encoding"),
634 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
635 ngx_conf_set_flag_slot,
636 NGX_HTTP_LOC_CONF_OFFSET,
637 offsetof(ngx_http_core_loc_conf_t, chunked_transfer_encoding),
638 NULL },
639
640 { ngx_string("etag"),
641 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
642 ngx_conf_set_flag_slot,
643 NGX_HTTP_LOC_CONF_OFFSET,
644 offsetof(ngx_http_core_loc_conf_t, etag),
645 NULL },
646
647 { ngx_string("error_page"),
648 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
649 |NGX_CONF_2MORE,
650 ngx_http_core_error_page,
651 NGX_HTTP_LOC_CONF_OFFSET,
652 0,
653 NULL },
654
655 { ngx_string("try_files"),
656 NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_2MORE,
657 ngx_http_core_try_files,
658 NGX_HTTP_LOC_CONF_OFFSET,
659 0,
660 NULL },
661
662 { ngx_string("post_action"),
663 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
664 |NGX_CONF_TAKE1,
665 ngx_conf_set_str_slot,
666 NGX_HTTP_LOC_CONF_OFFSET,
667 offsetof(ngx_http_core_loc_conf_t, post_action),
668 NULL },
669
670 { ngx_string("error_log"),
671 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
672 ngx_http_core_error_log,
673 NGX_HTTP_LOC_CONF_OFFSET,
674 0,
675 NULL },
676
677 { ngx_string("open_file_cache"),
678 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
679 ngx_http_core_open_file_cache,
680 NGX_HTTP_LOC_CONF_OFFSET,
681 offsetof(ngx_http_core_loc_conf_t, open_file_cache),
682 NULL },
683
684 { ngx_string("open_file_cache_valid"),
685 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
686 ngx_conf_set_sec_slot,
687 NGX_HTTP_LOC_CONF_OFFSET,
688 offsetof(ngx_http_core_loc_conf_t, open_file_cache_valid),
689 NULL },
690
691 { ngx_string("open_file_cache_retest"),
692 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
693 ngx_conf_set_sec_slot,
694 NGX_HTTP_LOC_CONF_OFFSET,
695 offsetof(ngx_http_core_loc_conf_t, open_file_cache_valid),
696 &ngx_conf_deprecated_open_file_cache_retest },
697
698 { ngx_string("open_file_cache_min_uses"),
699 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
700 ngx_conf_set_num_slot,
701 NGX_HTTP_LOC_CONF_OFFSET,
702 offsetof(ngx_http_core_loc_conf_t, open_file_cache_min_uses),
703 NULL },
704
705 { ngx_string("open_file_cache_errors"),
706 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
707 ngx_conf_set_flag_slot,
708 NGX_HTTP_LOC_CONF_OFFSET,
709 offsetof(ngx_http_core_loc_conf_t, open_file_cache_errors),
710 NULL },
711
712 { ngx_string("open_file_cache_events"),
713 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
714 ngx_conf_set_flag_slot,
715 NGX_HTTP_LOC_CONF_OFFSET,
716 offsetof(ngx_http_core_loc_conf_t, open_file_cache_events),
717 NULL },
718
719 { ngx_string("resolver"),
720 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
721 ngx_http_core_resolver,
722 NGX_HTTP_LOC_CONF_OFFSET,
723 0,
724 NULL },
725
726 { ngx_string("resolver_timeout"),
727 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
728 ngx_conf_set_msec_slot,
729 NGX_HTTP_LOC_CONF_OFFSET,
730 offsetof(ngx_http_core_loc_conf_t, resolver_timeout),
731 NULL },
732
733#if (NGX_HTTP_GZIP)
734
735 { ngx_string("gzip_vary"),
736 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
737 ngx_conf_set_flag_slot,
738 NGX_HTTP_LOC_CONF_OFFSET,
739 offsetof(ngx_http_core_loc_conf_t, gzip_vary),
740 NULL },
741
742 { ngx_string("gzip_http_version"),
743 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
744 ngx_conf_set_enum_slot,
745 NGX_HTTP_LOC_CONF_OFFSET,
746 offsetof(ngx_http_core_loc_conf_t, gzip_http_version),
747 &ngx_http_gzip_http_version },
748
749 { ngx_string("gzip_proxied"),
750 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
751 ngx_conf_set_bitmask_slot,
752 NGX_HTTP_LOC_CONF_OFFSET,
753 offsetof(ngx_http_core_loc_conf_t, gzip_proxied),
754 &ngx_http_gzip_proxied_mask },
755
756 { ngx_string("gzip_disable"),
757 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
758 ngx_http_gzip_disable,
759 NGX_HTTP_LOC_CONF_OFFSET,
760 0,
761 NULL },
762
763#endif
764
765#if (NGX_HAVE_OPENAT)
766
767 { ngx_string("disable_symlinks"),
768 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
769 ngx_http_disable_symlinks,
770 NGX_HTTP_LOC_CONF_OFFSET,
771 0,
772 NULL },
773
774#endif
775
776 ngx_null_command
777};
778
779
780static ngx_http_module_t ngx_http_core_module_ctx = {
781 ngx_http_core_preconfiguration, /* preconfiguration */
782 NULL, /* postconfiguration */
783
784 ngx_http_core_create_main_conf, /* create main configuration */
785 ngx_http_core_init_main_conf, /* init main configuration */
786
787 ngx_http_core_create_srv_conf, /* create server configuration */
788 ngx_http_core_merge_srv_conf, /* merge server configuration */
789
790 ngx_http_core_create_loc_conf, /* create location configuration */
791 ngx_http_core_merge_loc_conf /* merge location configuration */
792};
793
794
795ngx_module_t ngx_http_core_module = {
796 NGX_MODULE_V1,
797 &ngx_http_core_module_ctx, /* module context */
798 ngx_http_core_commands, /* module directives */
799 NGX_HTTP_MODULE, /* module type */
800 NULL, /* init master */
801 NULL, /* init module */
802 NULL, /* init process */
803 NULL, /* init thread */
804 NULL, /* exit thread */
805 NULL, /* exit process */
806 NULL, /* exit master */
807 NGX_MODULE_V1_PADDING
808};
809
810
811ngx_str_t ngx_http_core_get_method = { 3, (u_char *) "GET " };
812
813
814void
815ngx_http_handler(ngx_http_request_t *r)
816{
817 ngx_http_core_main_conf_t *cmcf;
818
819 r->connection->log->action = NULL;
820
821 r->connection->unexpected_eof = 0;
822
823 if (!r->internal) {
824 switch (r->headers_in.connection_type) {
825 case 0:
826 r->keepalive = (r->http_version > NGX_HTTP_VERSION_10);
827 break;
828
829 case NGX_HTTP_CONNECTION_CLOSE:
830 r->keepalive = 0;
831 break;
832
833 case NGX_HTTP_CONNECTION_KEEP_ALIVE:
834 r->keepalive = 1;
835 break;
836 }
837
838 r->lingering_close = (r->headers_in.content_length_n > 0
839 || r->headers_in.chunked);
840 r->phase_handler = 0;
841
842 } else {
843 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
844 r->phase_handler = cmcf->phase_engine.server_rewrite_index;
845 }
846
847 r->valid_location = 1;
848#if (NGX_HTTP_GZIP)
849 r->gzip_tested = 0;
850 r->gzip_ok = 0;
851 r->gzip_vary = 0;
852#endif
853
854 r->write_event_handler = ngx_http_core_run_phases;
855 ngx_http_core_run_phases(r);
856}
857
858
859void
860ngx_http_core_run_phases(ngx_http_request_t *r)
861{
862 ngx_int_t rc;
863 ngx_http_phase_handler_t *ph;
864 ngx_http_core_main_conf_t *cmcf;
865
866 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
867
868 ph = cmcf->phase_engine.handlers;
869
870 while (ph[r->phase_handler].checker) {
871
872 rc = ph[r->phase_handler].checker(r, &ph[r->phase_handler]);
873
874 if (rc == NGX_OK) {
875 return;
876 }
877 }
878}
879
880
881ngx_int_t
882ngx_http_core_generic_phase(ngx_http_request_t *r, ngx_http_phase_handler_t *ph)
883{
884 ngx_int_t rc;
885
886 /*
887 * generic phase checker,
888 * used by the post read and pre-access phases
889 */
890
891 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
892 "generic phase: %ui", r->phase_handler);
893
894 rc = ph->handler(r);
895
896 if (rc == NGX_OK) {
897 r->phase_handler = ph->next;
898 return NGX_AGAIN;
899 }
900
901 if (rc == NGX_DECLINED) {
902 r->phase_handler++;
903 return NGX_AGAIN;
904 }
905
906 if (rc == NGX_AGAIN || rc == NGX_DONE) {
907 return NGX_OK;
908 }
909
910 /* rc == NGX_ERROR || rc == NGX_HTTP_... */
911
912 ngx_http_finalize_request(r, rc);
913
914 return NGX_OK;
915}
916
917
918ngx_int_t
919ngx_http_core_rewrite_phase(ngx_http_request_t *r, ngx_http_phase_handler_t *ph)
920{
921 ngx_int_t rc;
922
923 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
924 "rewrite phase: %ui", r->phase_handler);
925
926 rc = ph->handler(r);
927
928 if (rc == NGX_DECLINED) {
929 r->phase_handler++;
930 return NGX_AGAIN;
931 }
932
933 if (rc == NGX_DONE) {
934 return NGX_OK;
935 }
936
937 /* NGX_OK, NGX_AGAIN, NGX_ERROR, NGX_HTTP_... */
938
939 ngx_http_finalize_request(r, rc);
940
941 return NGX_OK;
942}
943
944
945ngx_int_t
946ngx_http_core_find_config_phase(ngx_http_request_t *r,
947 ngx_http_phase_handler_t *ph)
948{
949 u_char *p;
950 size_t len;
951 ngx_int_t rc;
952 ngx_http_core_loc_conf_t *clcf;
953
954 r->content_handler = NULL;
955 r->uri_changed = 0;
956
957 rc = ngx_http_core_find_location(r);
958
959 if (rc == NGX_ERROR) {
960 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
961 return NGX_OK;
962 }
963
964 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
965
966 if (!r->internal && clcf->internal) {
967 ngx_http_finalize_request(r, NGX_HTTP_NOT_FOUND);
968 return NGX_OK;
969 }
970
971 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
972 "using configuration \"%s%V\"",
973 (clcf->noname ? "*" : (clcf->exact_match ? "=" : "")),
974 &clcf->name);
975
976 ngx_http_update_location_config(r);
977
978 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
979 "http cl:%O max:%O",
980 r->headers_in.content_length_n, clcf->client_max_body_size);
981
982 if (r->headers_in.content_length_n != -1
983 && !r->discard_body
984 && clcf->client_max_body_size
985 && clcf->client_max_body_size < r->headers_in.content_length_n)
986 {
987 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
988 "client intended to send too large body: %O bytes",
989 r->headers_in.content_length_n);
990
991 r->expect_tested = 1;
992 (void) ngx_http_discard_request_body(r);
993 ngx_http_finalize_request(r, NGX_HTTP_REQUEST_ENTITY_TOO_LARGE);
994 return NGX_OK;
995 }
996
997 if (rc == NGX_DONE) {
998 ngx_http_clear_location(r);
999
1000 r->headers_out.location = ngx_list_push(&r->headers_out.headers);
1001 if (r->headers_out.location == NULL) {
1002 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
1003 return NGX_OK;
1004 }
1005
1006 /*
1007 * we do not need to set the r->headers_out.location->hash and
1008 * r->headers_out.location->key fields
1009 */
1010
1011 if (r->args.len == 0) {
1012 r->headers_out.location->value = clcf->name;
1013
1014 } else {
1015 len = clcf->name.len + 1 + r->args.len;
1016 p = ngx_pnalloc(r->pool, len);
1017
1018 if (p == NULL) {
1019 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
1020 return NGX_OK;
1021 }
1022
1023 r->headers_out.location->value.len = len;
1024 r->headers_out.location->value.data = p;
1025
1026 p = ngx_cpymem(p, clcf->name.data, clcf->name.len);
1027 *p++ = '?';
1028 ngx_memcpy(p, r->args.data, r->args.len);
1029 }
1030
1031 ngx_http_finalize_request(r, NGX_HTTP_MOVED_PERMANENTLY);
1032 return NGX_OK;
1033 }
1034
1035 r->phase_handler++;
1036 return NGX_AGAIN;
1037}
1038
1039
1040ngx_int_t
1041ngx_http_core_post_rewrite_phase(ngx_http_request_t *r,
1042 ngx_http_phase_handler_t *ph)
1043{
1044 ngx_http_core_srv_conf_t *cscf;
1045
1046 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1047 "post rewrite phase: %ui", r->phase_handler);
1048
1049 if (!r->uri_changed) {
1050 r->phase_handler++;
1051 return NGX_AGAIN;
1052 }
1053
1054 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1055 "uri changes: %d", r->uri_changes);
1056
1057 /*
1058 * gcc before 3.3 compiles the broken code for
1059 * if (r->uri_changes-- == 0)
1060 * if the r->uri_changes is defined as
1061 * unsigned uri_changes:4
1062 */
1063
1064 r->uri_changes--;
1065
1066 if (r->uri_changes == 0) {
1067 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1068 "rewrite or internal redirection cycle "
1069 "while processing \"%V\"", &r->uri);
1070
1071 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
1072 return NGX_OK;
1073 }
1074
1075 r->phase_handler = ph->next;
1076
1077 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
1078 r->loc_conf = cscf->ctx->loc_conf;
1079
1080 return NGX_AGAIN;
1081}
1082
1083
1084ngx_int_t
1085ngx_http_core_access_phase(ngx_http_request_t *r, ngx_http_phase_handler_t *ph)
1086{
1087 ngx_int_t rc;
1088 ngx_http_core_loc_conf_t *clcf;
1089
1090 if (r != r->main) {
1091 r->phase_handler = ph->next;
1092 return NGX_AGAIN;
1093 }
1094
1095 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1096 "access phase: %ui", r->phase_handler);
1097
1098 rc = ph->handler(r);
1099
1100 if (rc == NGX_DECLINED) {
1101 r->phase_handler++;
1102 return NGX_AGAIN;
1103 }
1104
1105 if (rc == NGX_AGAIN || rc == NGX_DONE) {
1106 return NGX_OK;
1107 }
1108
1109 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1110
1111 if (clcf->satisfy == NGX_HTTP_SATISFY_ALL) {
1112
1113 if (rc == NGX_OK) {
1114 r->phase_handler++;
1115 return NGX_AGAIN;
1116 }
1117
1118 } else {
1119 if (rc == NGX_OK) {
1120 r->access_code = 0;
1121
1122 if (r->headers_out.www_authenticate) {
1123 r->headers_out.www_authenticate->hash = 0;
1124 }
1125
1126 r->phase_handler = ph->next;
1127 return NGX_AGAIN;
1128 }
1129
1130 if (rc == NGX_HTTP_FORBIDDEN || rc == NGX_HTTP_UNAUTHORIZED) {
1131 if (r->access_code != NGX_HTTP_UNAUTHORIZED) {
1132 r->access_code = rc;
1133 }
1134
1135 r->phase_handler++;
1136 return NGX_AGAIN;
1137 }
1138 }
1139
1140 /* rc == NGX_ERROR || rc == NGX_HTTP_... */
1141
1142 ngx_http_finalize_request(r, rc);
1143 return NGX_OK;
1144}
1145
1146
1147ngx_int_t
1148ngx_http_core_post_access_phase(ngx_http_request_t *r,
1149 ngx_http_phase_handler_t *ph)
1150{
1151 ngx_int_t access_code;
1152
1153 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1154 "post access phase: %ui", r->phase_handler);
1155
1156 access_code = r->access_code;
1157
1158 if (access_code) {
1159 if (access_code == NGX_HTTP_FORBIDDEN) {
1160 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1161 "access forbidden by rule");
1162 }
1163
1164 r->access_code = 0;
1165 ngx_http_finalize_request(r, access_code);
1166 return NGX_OK;
1167 }
1168
1169 r->phase_handler++;
1170 return NGX_AGAIN;
1171}
1172
1173
1174ngx_int_t
1175ngx_http_core_try_files_phase(ngx_http_request_t *r,
1176 ngx_http_phase_handler_t *ph)
1177{
1178 size_t len, root, alias, reserve, allocated;
1179 u_char *p, *name;
1180 ngx_str_t path, args;
1181 ngx_uint_t test_dir;
1182 ngx_http_try_file_t *tf;
1183 ngx_open_file_info_t of;
1184 ngx_http_script_code_pt code;
1185 ngx_http_script_engine_t e;
1186 ngx_http_core_loc_conf_t *clcf;
1187 ngx_http_script_len_code_pt lcode;
1188
1189 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1190 "try files phase: %ui", r->phase_handler);
1191
1192 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1193
1194 if (clcf->try_files == NULL) {
1195 r->phase_handler++;
1196 return NGX_AGAIN;
1197 }
1198
1199 allocated = 0;
1200 root = 0;
1201 name = NULL;
1202 /* suppress MSVC warning */
1203 path.data = NULL;
1204
1205 tf = clcf->try_files;
1206
1207 alias = clcf->alias;
1208
1209 for ( ;; ) {
1210
1211 if (tf->lengths) {
1212 ngx_memzero(&e, sizeof(ngx_http_script_engine_t));
1213
1214 e.ip = tf->lengths->elts;
1215 e.request = r;
1216
1217 /* 1 is for terminating '\0' as in static names */
1218 len = 1;
1219
1220 while (*(uintptr_t *) e.ip) {
1221 lcode = *(ngx_http_script_len_code_pt *) e.ip;
1222 len += lcode(&e);
1223 }
1224
1225 } else {
1226 len = tf->name.len;
1227 }
1228
1229 if (!alias) {
1230 reserve = len > r->uri.len ? len - r->uri.len : 0;
1231
1232 } else if (alias == NGX_MAX_SIZE_T_VALUE) {
1233 reserve = len;
1234
1235 } else {
1236 reserve = len > r->uri.len - alias ? len - (r->uri.len - alias) : 0;
1237 }
1238
1239 if (reserve > allocated || !allocated) {
1240
1241 /* 16 bytes are preallocation */
1242 allocated = reserve + 16;
1243
1244 if (ngx_http_map_uri_to_path(r, &path, &root, allocated) == NULL) {
1245 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
1246 return NGX_OK;
1247 }
1248
1249 name = path.data + root;
1250 }
1251
1252 if (tf->values == NULL) {
1253
1254 /* tf->name.len includes the terminating '\0' */
1255
1256 ngx_memcpy(name, tf->name.data, tf->name.len);
1257
1258 path.len = (name + tf->name.len - 1) - path.data;
1259
1260 } else {
1261 e.ip = tf->values->elts;
1262 e.pos = name;
1263 e.flushed = 1;
1264
1265 while (*(uintptr_t *) e.ip) {
1266 code = *(ngx_http_script_code_pt *) e.ip;
1267 code((ngx_http_script_engine_t *) &e);
1268 }
1269
1270 path.len = e.pos - path.data;
1271
1272 *e.pos = '\0';
1273
1274 if (alias && ngx_strncmp(name, clcf->name.data, alias) == 0) {
1275 ngx_memmove(name, name + alias, len - alias);
1276 path.len -= alias;
1277 }
1278 }
1279
1280 test_dir = tf->test_dir;
1281
1282 tf++;
1283
1284 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1285 "trying to use %s: \"%s\" \"%s\"",
1286 test_dir ? "dir" : "file", name, path.data);
1287
1288 if (tf->lengths == NULL && tf->name.len == 0) {
1289
1290 if (tf->code) {
1291 ngx_http_finalize_request(r, tf->code);
1292 return NGX_OK;
1293 }
1294
1295 path.len -= root;
1296 path.data += root;
1297
1298 if (path.data[0] == '@') {
1299 (void) ngx_http_named_location(r, &path);
1300
1301 } else {
1302 ngx_http_split_args(r, &path, &args);
1303
1304 (void) ngx_http_internal_redirect(r, &path, &args);
1305 }
1306
1307 ngx_http_finalize_request(r, NGX_DONE);
1308 return NGX_OK;
1309 }
1310
1311 ngx_memzero(&of, sizeof(ngx_open_file_info_t));
1312
1313 of.read_ahead = clcf->read_ahead;
1314 of.directio = clcf->directio;
1315 of.valid = clcf->open_file_cache_valid;
1316 of.min_uses = clcf->open_file_cache_min_uses;
1317 of.test_only = 1;
1318 of.errors = clcf->open_file_cache_errors;
1319 of.events = clcf->open_file_cache_events;
1320
1321 if (ngx_http_set_disable_symlinks(r, clcf, &path, &of) != NGX_OK) {
1322 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
1323 return NGX_OK;
1324 }
1325
1326 if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool)
1327 != NGX_OK)
1328 {
1329 if (of.err != NGX_ENOENT
1330 && of.err != NGX_ENOTDIR
1331 && of.err != NGX_ENAMETOOLONG)
1332 {
1333 ngx_log_error(NGX_LOG_CRIT, r->connection->log, of.err,
1334 "%s \"%s\" failed", of.failed, path.data);
1335 }
1336
1337 continue;
1338 }
1339
1340 if (of.is_dir != test_dir) {
1341 continue;
1342 }
1343
1344 path.len -= root;
1345 path.data += root;
1346
1347 if (!alias) {
1348 r->uri = path;
1349
1350 } else if (alias == NGX_MAX_SIZE_T_VALUE) {
1351 if (!test_dir) {
1352 r->uri = path;
1353 r->add_uri_to_alias = 1;
1354 }
1355
1356 } else {
1357 r->uri.len = alias + path.len;
1358 r->uri.data = ngx_pnalloc(r->pool, r->uri.len);
1359 if (r->uri.data == NULL) {
1360 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
1361 return NGX_OK;
1362 }
1363
1364 p = ngx_copy(r->uri.data, clcf->name.data, alias);
1365 ngx_memcpy(p, name, path.len);
1366 }
1367
1368 ngx_http_set_exten(r);
1369
1370 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1371 "try file uri: \"%V\"", &r->uri);
1372
1373 r->phase_handler++;
1374 return NGX_AGAIN;
1375 }
1376
1377 /* not reached */
1378}
1379
1380
1381ngx_int_t
1382ngx_http_core_content_phase(ngx_http_request_t *r,
1383 ngx_http_phase_handler_t *ph)
1384{
1385 size_t root;
1386 ngx_int_t rc;
1387 ngx_str_t path;
1388
1389 if (r->content_handler) {
1390 r->write_event_handler = ngx_http_request_empty_handler;
1391 ngx_http_finalize_request(r, r->content_handler(r));
1392 return NGX_OK;
1393 }
1394
1395 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1396 "content phase: %ui", r->phase_handler);
1397
1398 rc = ph->handler(r);
1399
1400 if (rc != NGX_DECLINED) {
1401 ngx_http_finalize_request(r, rc);
1402 return NGX_OK;
1403 }
1404
1405 /* rc == NGX_DECLINED */
1406
1407 ph++;
1408
1409 if (ph->checker) {
1410 r->phase_handler++;
1411 return NGX_AGAIN;
1412 }
1413
1414 /* no content handler was found */
1415
1416 if (r->uri.data[r->uri.len - 1] == '/') {
1417
1418 if (ngx_http_map_uri_to_path(r, &path, &root, 0) != NULL) {
1419 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1420 "directory index of \"%s\" is forbidden", path.data);
1421 }
1422
1423 ngx_http_finalize_request(r, NGX_HTTP_FORBIDDEN);
1424 return NGX_OK;
1425 }
1426
1427 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "no handler found");
1428
1429 ngx_http_finalize_request(r, NGX_HTTP_NOT_FOUND);
1430 return NGX_OK;
1431}
1432
1433
1434void
1435ngx_http_update_location_config(ngx_http_request_t *r)
1436{
1437 ngx_http_core_loc_conf_t *clcf;
1438
1439 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1440
1441 if (r->method & clcf->limit_except) {
1442 r->loc_conf = clcf->limit_except_loc_conf;
1443 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1444 }
1445
1446 if (r == r->main) {
1447 ngx_http_set_connection_log(r->connection, clcf->error_log);
1448 }
1449
1450 if ((ngx_io.flags & NGX_IO_SENDFILE) && clcf->sendfile) {
1451 r->connection->sendfile = 1;
1452
1453 } else {
1454 r->connection->sendfile = 0;
1455 }
1456
1457 if (clcf->client_body_in_file_only) {
1458 r->request_body_in_file_only = 1;
1459 r->request_body_in_persistent_file = 1;
1460 r->request_body_in_clean_file =
1461 clcf->client_body_in_file_only == NGX_HTTP_REQUEST_BODY_FILE_CLEAN;
1462 r->request_body_file_log_level = NGX_LOG_NOTICE;
1463
1464 } else {
1465 r->request_body_file_log_level = NGX_LOG_WARN;
1466 }
1467
1468 r->request_body_in_single_buf = clcf->client_body_in_single_buffer;
1469
1470 if (r->keepalive) {
1471 if (clcf->keepalive_timeout == 0) {
1472 r->keepalive = 0;
1473
1474 } else if (r->connection->requests >= clcf->keepalive_requests) {
1475 r->keepalive = 0;
1476
1477 } else if (r->headers_in.msie6
1478 && r->method == NGX_HTTP_POST
1479 && (clcf->keepalive_disable
1480 & NGX_HTTP_KEEPALIVE_DISABLE_MSIE6))
1481 {
1482 /*
1483 * MSIE may wait for some time if an response for
1484 * a POST request was sent over a keepalive connection
1485 */
1486 r->keepalive = 0;
1487
1488 } else if (r->headers_in.safari
1489 && (clcf->keepalive_disable
1490 & NGX_HTTP_KEEPALIVE_DISABLE_SAFARI))
1491 {
1492 /*
1493 * Safari may send a POST request to a closed keepalive
1494 * connection and may stall for some time, see
1495 * https://bugs.webkit.org/show_bug.cgi?id=5760
1496 */
1497 r->keepalive = 0;
1498 }
1499 }
1500
1501 if (!clcf->tcp_nopush) {
1502 /* disable TCP_NOPUSH/TCP_CORK use */
1503 r->connection->tcp_nopush = NGX_TCP_NOPUSH_DISABLED;
1504 }
1505
1506 if (r->limit_rate == 0) {
1507 r->limit_rate = clcf->limit_rate;
1508 }
1509
1510 if (clcf->handler) {
1511 r->content_handler = clcf->handler;
1512 }
1513}
1514
1515
1516/*
1517 * NGX_OK - exact or regex match
1518 * NGX_DONE - auto redirect
1519 * NGX_AGAIN - inclusive match
1520 * NGX_ERROR - regex error
1521 * NGX_DECLINED - no match
1522 */
1523
1524static ngx_int_t
1525ngx_http_core_find_location(ngx_http_request_t *r)
1526{
1527 ngx_int_t rc;
1528 ngx_http_core_loc_conf_t *pclcf;
1529#if (NGX_PCRE)
1530 ngx_int_t n;
1531 ngx_uint_t noregex;
1532 ngx_http_core_loc_conf_t *clcf, **clcfp;
1533
1534 noregex = 0;
1535#endif
1536
1537 pclcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1538
1539 rc = ngx_http_core_find_static_location(r, pclcf->static_locations);
1540
1541 if (rc == NGX_AGAIN) {
1542
1543#if (NGX_PCRE)
1544 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1545
1546 noregex = clcf->noregex;
1547#endif
1548
1549 /* look up nested locations */
1550
1551 rc = ngx_http_core_find_location(r);
1552 }
1553
1554 if (rc == NGX_OK || rc == NGX_DONE) {
1555 return rc;
1556 }
1557
1558 /* rc == NGX_DECLINED or rc == NGX_AGAIN in nested location */
1559
1560#if (NGX_PCRE)
1561
1562 if (noregex == 0 && pclcf->regex_locations) {
1563
1564 for (clcfp = pclcf->regex_locations; *clcfp; clcfp++) {
1565
1566 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1567 "test location: ~ \"%V\"", &(*clcfp)->name);
1568
1569 n = ngx_http_regex_exec(r, (*clcfp)->regex, &r->uri);
1570
1571 if (n == NGX_OK) {
1572 r->loc_conf = (*clcfp)->loc_conf;
1573
1574 /* look up nested locations */
1575
1576 rc = ngx_http_core_find_location(r);
1577
1578 return (rc == NGX_ERROR) ? rc : NGX_OK;
1579 }
1580
1581 if (n == NGX_DECLINED) {
1582 continue;
1583 }
1584
1585 return NGX_ERROR;
1586 }
1587 }
1588#endif
1589
1590 return rc;
1591}
1592
1593
1594/*
1595 * NGX_OK - exact match
1596 * NGX_DONE - auto redirect
1597 * NGX_AGAIN - inclusive match
1598 * NGX_DECLINED - no match
1599 */
1600
1601static ngx_int_t
1602ngx_http_core_find_static_location(ngx_http_request_t *r,
1603 ngx_http_location_tree_node_t *node)
1604{
1605 u_char *uri;
1606 size_t len, n;
1607 ngx_int_t rc, rv;
1608
1609 len = r->uri.len;
1610 uri = r->uri.data;
1611
1612 rv = NGX_DECLINED;
1613
1614 for ( ;; ) {
1615
1616 if (node == NULL) {
1617 return rv;
1618 }
1619
1620 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1621 "test location: \"%*s\"", node->len, node->name);
1622
1623 n = (len <= (size_t) node->len) ? len : node->len;
1624
1625 rc = ngx_filename_cmp(uri, node->name, n);
1626
1627 if (rc != 0) {
1628 node = (rc < 0) ? node->left : node->right;
1629
1630 continue;
1631 }
1632
1633 if (len > (size_t) node->len) {
1634
1635 if (node->inclusive) {
1636
1637 r->loc_conf = node->inclusive->loc_conf;
1638 rv = NGX_AGAIN;
1639
1640 node = node->tree;
1641 uri += n;
1642 len -= n;
1643
1644 continue;
1645 }
1646
1647 /* exact only */
1648
1649 node = node->right;
1650
1651 continue;
1652 }
1653
1654 if (len == (size_t) node->len) {
1655
1656 if (node->exact) {
1657 r->loc_conf = node->exact->loc_conf;
1658 return NGX_OK;
1659
1660 } else {
1661 r->loc_conf = node->inclusive->loc_conf;
1662 return NGX_AGAIN;
1663 }
1664 }
1665
1666 /* len < node->len */
1667
1668 if (len + 1 == (size_t) node->len && node->auto_redirect) {
1669
1670 r->loc_conf = (node->exact) ? node->exact->loc_conf:
1671 node->inclusive->loc_conf;
1672 rv = NGX_DONE;
1673 }
1674
1675 node = node->left;
1676 }
1677}
1678
1679
1680void *
1681ngx_http_test_content_type(ngx_http_request_t *r, ngx_hash_t *types_hash)
1682{
1683 u_char c, *lowcase;
1684 size_t len;
1685 ngx_uint_t i, hash;
1686
1687 if (types_hash->size == 0) {
1688 return (void *) 4;
1689 }
1690
1691 if (r->headers_out.content_type.len == 0) {
1692 return NULL;
1693 }
1694
1695 len = r->headers_out.content_type_len;
1696
1697 if (r->headers_out.content_type_lowcase == NULL) {
1698
1699 lowcase = ngx_pnalloc(r->pool, len);
1700 if (lowcase == NULL) {
1701 return NULL;
1702 }
1703
1704 r->headers_out.content_type_lowcase = lowcase;
1705
1706 hash = 0;
1707
1708 for (i = 0; i < len; i++) {
1709 c = ngx_tolower(r->headers_out.content_type.data[i]);
1710 hash = ngx_hash(hash, c);
1711 lowcase[i] = c;
1712 }
1713
1714 r->headers_out.content_type_hash = hash;
1715 }
1716
1717 return ngx_hash_find(types_hash, r->headers_out.content_type_hash,
1718 r->headers_out.content_type_lowcase, len);
1719}
1720
1721
1722ngx_int_t
1723ngx_http_set_content_type(ngx_http_request_t *r)
1724{
1725 u_char c, *exten;
1726 ngx_str_t *type;
1727 ngx_uint_t i, hash;
1728 ngx_http_core_loc_conf_t *clcf;
1729
1730 if (r->headers_out.content_type.len) {
1731 return NGX_OK;
1732 }
1733
1734 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1735
1736 if (r->exten.len) {
1737
1738 hash = 0;
1739
1740 for (i = 0; i < r->exten.len; i++) {
1741 c = r->exten.data[i];
1742
1743 if (c >= 'A' && c <= 'Z') {
1744
1745 exten = ngx_pnalloc(r->pool, r->exten.len);
1746 if (exten == NULL) {
1747 return NGX_ERROR;
1748 }
1749
1750 hash = ngx_hash_strlow(exten, r->exten.data, r->exten.len);
1751
1752 r->exten.data = exten;
1753
1754 break;
1755 }
1756
1757 hash = ngx_hash(hash, c);
1758 }
1759
1760 type = ngx_hash_find(&clcf->types_hash, hash,
1761 r->exten.data, r->exten.len);
1762
1763 if (type) {
1764 r->headers_out.content_type_len = type->len;
1765 r->headers_out.content_type = *type;
1766
1767 return NGX_OK;
1768 }
1769 }
1770
1771 r->headers_out.content_type_len = clcf->default_type.len;
1772 r->headers_out.content_type = clcf->default_type;
1773
1774 return NGX_OK;
1775}
1776
1777
1778void
1779ngx_http_set_exten(ngx_http_request_t *r)
1780{
1781 ngx_int_t i;
1782
1783 ngx_str_null(&r->exten);
1784
1785 for (i = r->uri.len - 1; i > 1; i--) {
1786 if (r->uri.data[i] == '.' && r->uri.data[i - 1] != '/') {
1787
1788 r->exten.len = r->uri.len - i - 1;
1789 r->exten.data = &r->uri.data[i + 1];
1790
1791 return;
1792
1793 } else if (r->uri.data[i] == '/') {
1794 return;
1795 }
1796 }
1797
1798 return;
1799}
1800
1801
1802ngx_int_t
1803ngx_http_set_etag(ngx_http_request_t *r)
1804{
1805 ngx_table_elt_t *etag;
1806 ngx_http_core_loc_conf_t *clcf;
1807
1808 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1809
1810 if (!clcf->etag) {
1811 return NGX_OK;
1812 }
1813
1814 etag = ngx_list_push(&r->headers_out.headers);
1815 if (etag == NULL) {
1816 return NGX_ERROR;
1817 }
1818
1819 etag->hash = 1;
1820 ngx_str_set(&etag->key, "ETag");
1821
1822 etag->value.data = ngx_pnalloc(r->pool, NGX_OFF_T_LEN + NGX_TIME_T_LEN + 3);
1823 if (etag->value.data == NULL) {
1824 etag->hash = 0;
1825 return NGX_ERROR;
1826 }
1827
1828 etag->value.len = ngx_sprintf(etag->value.data, "\"%xT-%xO\"",
1829 r->headers_out.last_modified_time,
1830 r->headers_out.content_length_n)
1831 - etag->value.data;
1832
1833 r->headers_out.etag = etag;
1834
1835 return NGX_OK;
1836}
1837
1838
1839void
1840ngx_http_weak_etag(ngx_http_request_t *r)
1841{
1842 size_t len;
1843 u_char *p;
1844 ngx_table_elt_t *etag;
1845
1846 etag = r->headers_out.etag;
1847
1848 if (etag == NULL) {
1849 return;
1850 }
1851
1852 if (etag->value.len > 2
1853 && etag->value.data[0] == 'W'
1854 && etag->value.data[1] == '/')
1855 {
1856 return;
1857 }
1858
1859 if (etag->value.len < 1 || etag->value.data[0] != '"') {
1860 r->headers_out.etag->hash = 0;
1861 r->headers_out.etag = NULL;
1862 return;
1863 }
1864
1865 p = ngx_pnalloc(r->pool, etag->value.len + 2);
1866 if (p == NULL) {
1867 r->headers_out.etag->hash = 0;
1868 r->headers_out.etag = NULL;
1869 return;
1870 }
1871
1872 len = ngx_sprintf(p, "W/%V", &etag->value) - p;
1873
1874 etag->value.data = p;
1875 etag->value.len = len;
1876}
1877
1878
1879ngx_int_t
1880ngx_http_send_response(ngx_http_request_t *r, ngx_uint_t status,
1881 ngx_str_t *ct, ngx_http_complex_value_t *cv)
1882{
1883 ngx_int_t rc;
1884 ngx_str_t val;
1885 ngx_buf_t *b;
1886 ngx_chain_t out;
1887
1888 if (ngx_http_discard_request_body(r) != NGX_OK) {
1889 return NGX_HTTP_INTERNAL_SERVER_ERROR;
1890 }
1891
1892 r->headers_out.status = status;
1893
1894 if (ngx_http_complex_value(r, cv, &val) != NGX_OK) {
1895 return NGX_HTTP_INTERNAL_SERVER_ERROR;
1896 }
1897
1898 if (status == NGX_HTTP_MOVED_PERMANENTLY
1899 || status == NGX_HTTP_MOVED_TEMPORARILY
1900 || status == NGX_HTTP_SEE_OTHER
1901 || status == NGX_HTTP_TEMPORARY_REDIRECT)
1902 {
1903 ngx_http_clear_location(r);
1904
1905 r->headers_out.location = ngx_list_push(&r->headers_out.headers);
1906 if (r->headers_out.location == NULL) {
1907 return NGX_HTTP_INTERNAL_SERVER_ERROR;
1908 }
1909
1910 r->headers_out.location->hash = 1;
1911 ngx_str_set(&r->headers_out.location->key, "Location");
1912 r->headers_out.location->value = val;
1913
1914 return status;
1915 }
1916
1917 r->headers_out.content_length_n = val.len;
1918
1919 if (ct) {
1920 r->headers_out.content_type_len = ct->len;
1921 r->headers_out.content_type = *ct;
1922
1923 } else {
1924 if (ngx_http_set_content_type(r) != NGX_OK) {
1925 return NGX_HTTP_INTERNAL_SERVER_ERROR;
1926 }
1927 }
1928
1929 if (r->method == NGX_HTTP_HEAD || (r != r->main && val.len == 0)) {
1930 return ngx_http_send_header(r);
1931 }
1932
1933 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
1934 if (b == NULL) {
1935 return NGX_HTTP_INTERNAL_SERVER_ERROR;
1936 }
1937
1938 b->pos = val.data;
1939 b->last = val.data + val.len;
1940 b->memory = val.len ? 1 : 0;
1941 b->last_buf = (r == r->main) ? 1 : 0;
1942 b->last_in_chain = 1;
1943
1944 out.buf = b;
1945 out.next = NULL;
1946
1947 rc = ngx_http_send_header(r);
1948
1949 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
1950 return rc;
1951 }
1952
1953 return ngx_http_output_filter(r, &out);
1954}
1955
1956
1957ngx_int_t
1958ngx_http_send_header(ngx_http_request_t *r)
1959{
1960 if (r->post_action) {
1961 return NGX_OK;
1962 }
1963
1964 if (r->header_sent) {
1965 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
1966 "header already sent");
1967 return NGX_ERROR;
1968 }
1969
1970 if (r->err_status) {
1971 r->headers_out.status = r->err_status;
1972 r->headers_out.status_line.len = 0;
1973 }
1974
1975 return ngx_http_top_header_filter(r);
1976}
1977
1978
1979ngx_int_t
1980ngx_http_output_filter(ngx_http_request_t *r, ngx_chain_t *in)
1981{
1982 ngx_int_t rc;
1983 ngx_connection_t *c;
1984
1985 c = r->connection;
1986
1987 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
1988 "http output filter \"%V?%V\"", &r->uri, &r->args);
1989
1990 rc = ngx_http_top_body_filter(r, in);
1991
1992 if (rc == NGX_ERROR) {
1993 /* NGX_ERROR may be returned by any filter */
1994 c->error = 1;
1995 }
1996
1997 return rc;
1998}
1999
2000
2001u_char *
2002ngx_http_map_uri_to_path(ngx_http_request_t *r, ngx_str_t *path,
2003 size_t *root_length, size_t reserved)
2004{
2005 u_char *last;
2006 size_t alias;
2007 ngx_http_core_loc_conf_t *clcf;
2008
2009 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
2010
2011 alias = clcf->alias;
2012
2013 if (alias && !r->valid_location) {
2014 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
2015 "\"alias\" cannot be used in location \"%V\" "
2016 "where URI was rewritten", &clcf->name);
2017 return NULL;
2018 }
2019
2020 if (clcf->root_lengths == NULL) {
2021
2022 *root_length = clcf->root.len;
2023
2024 path->len = clcf->root.len + reserved + r->uri.len - alias + 1;
2025
2026 path->data = ngx_pnalloc(r->pool, path->len);
2027 if (path->data == NULL) {
2028 return NULL;
2029 }
2030
2031 last = ngx_copy(path->data, clcf->root.data, clcf->root.len);
2032
2033 } else {
2034
2035 if (alias == NGX_MAX_SIZE_T_VALUE) {
2036 reserved += r->add_uri_to_alias ? r->uri.len + 1 : 1;
2037
2038 } else {
2039 reserved += r->uri.len - alias + 1;
2040 }
2041
2042 if (ngx_http_script_run(r, path, clcf->root_lengths->elts, reserved,
2043 clcf->root_values->elts)
2044 == NULL)
2045 {
2046 return NULL;
2047 }
2048
2049 if (ngx_get_full_name(r->pool, (ngx_str_t *) &ngx_cycle->prefix, path)
2050 != NGX_OK)
2051 {
2052 return NULL;
2053 }
2054
2055 *root_length = path->len - reserved;
2056 last = path->data + *root_length;
2057
2058 if (alias == NGX_MAX_SIZE_T_VALUE) {
2059 if (!r->add_uri_to_alias) {
2060 *last = '\0';
2061 return last;
2062 }
2063
2064 alias = 0;
2065 }
2066 }
2067
2068 last = ngx_cpystrn(last, r->uri.data + alias, r->uri.len - alias + 1);
2069
2070 return last;
2071}
2072
2073
2074ngx_int_t
2075ngx_http_auth_basic_user(ngx_http_request_t *r)
2076{
2077 ngx_str_t auth, encoded;
2078 ngx_uint_t len;
2079
2080 if (r->headers_in.user.len == 0 && r->headers_in.user.data != NULL) {
2081 return NGX_DECLINED;
2082 }
2083
2084 if (r->headers_in.authorization == NULL) {
2085 r->headers_in.user.data = (u_char *) "";
2086 return NGX_DECLINED;
2087 }
2088
2089 encoded = r->headers_in.authorization->value;
2090
2091 if (encoded.len < sizeof("Basic ") - 1
2092 || ngx_strncasecmp(encoded.data, (u_char *) "Basic ",
2093 sizeof("Basic ") - 1)
2094 != 0)
2095 {
2096 r->headers_in.user.data = (u_char *) "";
2097 return NGX_DECLINED;
2098 }
2099
2100 encoded.len -= sizeof("Basic ") - 1;
2101 encoded.data += sizeof("Basic ") - 1;
2102
2103 while (encoded.len && encoded.data[0] == ' ') {
2104 encoded.len--;
2105 encoded.data++;
2106 }
2107
2108 if (encoded.len == 0) {
2109 r->headers_in.user.data = (u_char *) "";
2110 return NGX_DECLINED;
2111 }
2112
2113 auth.len = ngx_base64_decoded_length(encoded.len);
2114 auth.data = ngx_pnalloc(r->pool, auth.len + 1);
2115 if (auth.data == NULL) {
2116 return NGX_ERROR;
2117 }
2118
2119 if (ngx_decode_base64(&auth, &encoded) != NGX_OK) {
2120 r->headers_in.user.data = (u_char *) "";
2121 return NGX_DECLINED;
2122 }
2123
2124 auth.data[auth.len] = '\0';
2125
2126 for (len = 0; len < auth.len; len++) {
2127 if (auth.data[len] == ':') {
2128 break;
2129 }
2130 }
2131
2132 if (len == 0 || len == auth.len) {
2133 r->headers_in.user.data = (u_char *) "";
2134 return NGX_DECLINED;
2135 }
2136
2137 r->headers_in.user.len = len;
2138 r->headers_in.user.data = auth.data;
2139 r->headers_in.passwd.len = auth.len - len - 1;
2140 r->headers_in.passwd.data = &auth.data[len + 1];
2141
2142 return NGX_OK;
2143}
2144
2145
2146#if (NGX_HTTP_GZIP)
2147
2148ngx_int_t
2149ngx_http_gzip_ok(ngx_http_request_t *r)
2150{
2151 time_t date, expires;
2152 ngx_uint_t p;
2153 ngx_array_t *cc;
2154 ngx_table_elt_t *e, *d, *ae;
2155 ngx_http_core_loc_conf_t *clcf;
2156
2157 r->gzip_tested = 1;
2158
2159 if (r != r->main) {
2160 return NGX_DECLINED;
2161 }
2162
2163#if (NGX_HTTP_SPDY)
2164 if (r->spdy_stream) {
2165 r->gzip_ok = 1;
2166 return NGX_OK;
2167 }
2168#endif
2169
2170 ae = r->headers_in.accept_encoding;
2171 if (ae == NULL) {
2172 return NGX_DECLINED;
2173 }
2174
2175 if (ae->value.len < sizeof("gzip") - 1) {
2176 return NGX_DECLINED;
2177 }
2178
2179 /*
2180 * test first for the most common case "gzip,...":
2181 * MSIE: "gzip, deflate"
2182 * Firefox: "gzip,deflate"
2183 * Chrome: "gzip,deflate,sdch"
2184 * Safari: "gzip, deflate"
2185 * Opera: "gzip, deflate"
2186 */
2187
2188 if (ngx_memcmp(ae->value.data, "gzip,", 5) != 0
2189 && ngx_http_gzip_accept_encoding(&ae->value) != NGX_OK)
2190 {
2191 return NGX_DECLINED;
2192 }
2193
2194 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
2195
2196 if (r->headers_in.msie6 && clcf->gzip_disable_msie6) {
2197 return NGX_DECLINED;
2198 }
2199
2200 if (r->http_version < clcf->gzip_http_version) {
2201 return NGX_DECLINED;
2202 }
2203
2204 if (r->headers_in.via == NULL) {
2205 goto ok;
2206 }
2207
2208 p = clcf->gzip_proxied;
2209
2210 if (p & NGX_HTTP_GZIP_PROXIED_OFF) {
2211 return NGX_DECLINED;
2212 }
2213
2214 if (p & NGX_HTTP_GZIP_PROXIED_ANY) {
2215 goto ok;
2216 }
2217
2218 if (r->headers_in.authorization && (p & NGX_HTTP_GZIP_PROXIED_AUTH)) {
2219 goto ok;
2220 }
2221
2222 e = r->headers_out.expires;
2223
2224 if (e) {
2225
2226 if (!(p & NGX_HTTP_GZIP_PROXIED_EXPIRED)) {
2227 return NGX_DECLINED;
2228 }
2229
2230 expires = ngx_http_parse_time(e->value.data, e->value.len);
2231 if (expires == NGX_ERROR) {
2232 return NGX_DECLINED;
2233 }
2234
2235 d = r->headers_out.date;
2236
2237 if (d) {
2238 date = ngx_http_parse_time(d->value.data, d->value.len);
2239 if (date == NGX_ERROR) {
2240 return NGX_DECLINED;
2241 }
2242
2243 } else {
2244 date = ngx_time();
2245 }
2246
2247 if (expires < date) {
2248 goto ok;
2249 }
2250
2251 return NGX_DECLINED;
2252 }
2253
2254 cc = &r->headers_out.cache_control;
2255
2256 if (cc->elts) {
2257
2258 if ((p & NGX_HTTP_GZIP_PROXIED_NO_CACHE)
2259 && ngx_http_parse_multi_header_lines(cc, &ngx_http_gzip_no_cache,
2260 NULL)
2261 >= 0)
2262 {
2263 goto ok;
2264 }
2265
2266 if ((p & NGX_HTTP_GZIP_PROXIED_NO_STORE)
2267 && ngx_http_parse_multi_header_lines(cc, &ngx_http_gzip_no_store,
2268 NULL)
2269 >= 0)
2270 {
2271 goto ok;
2272 }
2273
2274 if ((p & NGX_HTTP_GZIP_PROXIED_PRIVATE)
2275 && ngx_http_parse_multi_header_lines(cc, &ngx_http_gzip_private,
2276 NULL)
2277 >= 0)
2278 {
2279 goto ok;
2280 }
2281
2282 return NGX_DECLINED;
2283 }
2284
2285 if ((p & NGX_HTTP_GZIP_PROXIED_NO_LM) && r->headers_out.last_modified) {
2286 return NGX_DECLINED;
2287 }
2288
2289 if ((p & NGX_HTTP_GZIP_PROXIED_NO_ETAG) && r->headers_out.etag) {
2290 return NGX_DECLINED;
2291 }
2292
2293ok:
2294
2295#if (NGX_PCRE)
2296
2297 if (clcf->gzip_disable && r->headers_in.user_agent) {
2298
2299 if (ngx_regex_exec_array(clcf->gzip_disable,
2300 &r->headers_in.user_agent->value,
2301 r->connection->log)
2302 != NGX_DECLINED)
2303 {
2304 return NGX_DECLINED;
2305 }
2306 }
2307
2308#endif
2309
2310 r->gzip_ok = 1;
2311
2312 return NGX_OK;
2313}
2314
2315
2316/*
2317 * gzip is enabled for the following quantities:
2318 * "gzip; q=0.001" ... "gzip; q=1.000"
2319 * gzip is disabled for the following quantities:
2320 * "gzip; q=0" ... "gzip; q=0.000", and for any invalid cases
2321 */
2322
2323static ngx_int_t
2324ngx_http_gzip_accept_encoding(ngx_str_t *ae)
2325{
2326 u_char *p, *start, *last;
2327
2328 start = ae->data;
2329 last = start + ae->len;
2330
2331 for ( ;; ) {
2332 p = ngx_strcasestrn(start, "gzip", 4 - 1);
2333 if (p == NULL) {
2334 return NGX_DECLINED;
2335 }
2336
2337 if (p == start || (*(p - 1) == ',' || *(p - 1) == ' ')) {
2338 break;
2339 }
2340
2341 start = p + 4;
2342 }
2343
2344 p += 4;
2345
2346 while (p < last) {
2347 switch (*p++) {
2348 case ',':
2349 return NGX_OK;
2350 case ';':
2351 goto quantity;
2352 case ' ':
2353 continue;
2354 default:
2355 return NGX_DECLINED;
2356 }
2357 }
2358
2359 return NGX_OK;
2360
2361quantity:
2362
2363 while (p < last) {
2364 switch (*p++) {
2365 case 'q':
2366 case 'Q':
2367 goto equal;
2368 case ' ':
2369 continue;
2370 default:
2371 return NGX_DECLINED;
2372 }
2373 }
2374
2375 return NGX_OK;
2376
2377equal:
2378
2379 if (p + 2 > last || *p++ != '=') {
2380 return NGX_DECLINED;
2381 }
2382
2383 if (ngx_http_gzip_quantity(p, last) == 0) {
2384 return NGX_DECLINED;
2385 }
2386
2387 return NGX_OK;
2388}
2389
2390
2391static ngx_uint_t
2392ngx_http_gzip_quantity(u_char *p, u_char *last)
2393{
2394 u_char c;
2395 ngx_uint_t n, q;
2396
2397 c = *p++;
2398
2399 if (c != '0' && c != '1') {
2400 return 0;
2401 }
2402
2403 q = (c - '0') * 100;
2404
2405 if (p == last) {
2406 return q;
2407 }
2408
2409 c = *p++;
2410
2411 if (c == ',' || c == ' ') {
2412 return q;
2413 }
2414
2415 if (c != '.') {
2416 return 0;
2417 }
2418
2419 n = 0;
2420
2421 while (p < last) {
2422 c = *p++;
2423
2424 if (c == ',' || c == ' ') {
2425 break;
2426 }
2427
2428 if (c >= '0' && c <= '9') {
2429 q += c - '0';
2430 n++;
2431 continue;
2432 }
2433
2434 return 0;
2435 }
2436
2437 if (q > 100 || n > 3) {
2438 return 0;
2439 }
2440
2441 return q;
2442}
2443
2444#endif
2445
2446
2447ngx_int_t
2448ngx_http_subrequest(ngx_http_request_t *r,
2449 ngx_str_t *uri, ngx_str_t *args, ngx_http_request_t **psr,
2450 ngx_http_post_subrequest_t *ps, ngx_uint_t flags)
2451{
2452 ngx_time_t *tp;
2453 ngx_connection_t *c;
2454 ngx_http_request_t *sr;
2455 ngx_http_core_srv_conf_t *cscf;
2456 ngx_http_postponed_request_t *pr, *p;
2457
2458 r->main->subrequests--;
2459
2460 if (r->main->subrequests == 0) {
2461 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
2462 "subrequests cycle while processing \"%V\"", uri);
2463 r->main->subrequests = 1;
2464 return NGX_ERROR;
2465 }
2466
2467 sr = ngx_pcalloc(r->pool, sizeof(ngx_http_request_t));
2468 if (sr == NULL) {
2469 return NGX_ERROR;
2470 }
2471
2472 sr->signature = NGX_HTTP_MODULE;
2473
2474 c = r->connection;
2475 sr->connection = c;
2476
2477 sr->ctx = ngx_pcalloc(r->pool, sizeof(void *) * ngx_http_max_module);
2478 if (sr->ctx == NULL) {
2479 return NGX_ERROR;
2480 }
2481
2482 if (ngx_list_init(&sr->headers_out.headers, r->pool, 20,
2483 sizeof(ngx_table_elt_t))
2484 != NGX_OK)
2485 {
2486 return NGX_ERROR;
2487 }
2488
2489 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
2490 sr->main_conf = cscf->ctx->main_conf;
2491 sr->srv_conf = cscf->ctx->srv_conf;
2492 sr->loc_conf = cscf->ctx->loc_conf;
2493
2494 sr->pool = r->pool;
2495
2496 sr->headers_in = r->headers_in;
2497
2498 ngx_http_clear_content_length(sr);
2499 ngx_http_clear_accept_ranges(sr);
2500 ngx_http_clear_last_modified(sr);
2501
2502 sr->request_body = r->request_body;
2503
2504#if (NGX_HTTP_SPDY)
2505 sr->spdy_stream = r->spdy_stream;
2506#endif
2507
2508 sr->method = NGX_HTTP_GET;
2509 sr->http_version = r->http_version;
2510
2511 sr->request_line = r->request_line;
2512 sr->uri = *uri;
2513
2514 if (args) {
2515 sr->args = *args;
2516 }
2517
2518 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
2519 "http subrequest \"%V?%V\"", uri, &sr->args);
2520
2521 sr->subrequest_in_memory = (flags & NGX_HTTP_SUBREQUEST_IN_MEMORY) != 0;
2522 sr->waited = (flags & NGX_HTTP_SUBREQUEST_WAITED) != 0;
2523
2524 sr->unparsed_uri = r->unparsed_uri;
2525 sr->method_name = ngx_http_core_get_method;
2526 sr->http_protocol = r->http_protocol;
2527
2528 ngx_http_set_exten(sr);
2529
2530 sr->main = r->main;
2531 sr->parent = r;
2532 sr->post_subrequest = ps;
2533 sr->read_event_handler = ngx_http_request_empty_handler;
2534 sr->write_event_handler = ngx_http_handler;
2535
2536 if (c->data == r && r->postponed == NULL) {
2537 c->data = sr;
2538 }
2539
2540 sr->variables = r->variables;
2541
2542 sr->log_handler = r->log_handler;
2543
2544 pr = ngx_palloc(r->pool, sizeof(ngx_http_postponed_request_t));
2545 if (pr == NULL) {
2546 return NGX_ERROR;
2547 }
2548
2549 pr->request = sr;
2550 pr->out = NULL;
2551 pr->next = NULL;
2552
2553 if (r->postponed) {
2554 for (p = r->postponed; p->next; p = p->next) { /* void */ }
2555 p->next = pr;
2556
2557 } else {
2558 r->postponed = pr;
2559 }
2560
2561 sr->internal = 1;
2562
2563 sr->discard_body = r->discard_body;
2564 sr->expect_tested = 1;
2565 sr->main_filter_need_in_memory = r->main_filter_need_in_memory;
2566
2567 sr->uri_changes = NGX_HTTP_MAX_URI_CHANGES + 1;
2568
2569 tp = ngx_timeofday();
2570 sr->start_sec = tp->sec;
2571 sr->start_msec = tp->msec;
2572
2573 r->main->count++;
2574
2575 *psr = sr;
2576
2577 return ngx_http_post_request(sr, NULL);
2578}
2579
2580
2581ngx_int_t
2582ngx_http_internal_redirect(ngx_http_request_t *r,
2583 ngx_str_t *uri, ngx_str_t *args)
2584{
2585 ngx_http_core_srv_conf_t *cscf;
2586
2587 r->uri_changes--;
2588
2589 if (r->uri_changes == 0) {
2590 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
2591 "rewrite or internal redirection cycle "
2592 "while internally redirecting to \"%V\"", uri);
2593
2594 r->main->count++;
2595 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
2596 return NGX_DONE;
2597 }
2598
2599 r->uri = *uri;
2600
2601 if (args) {
2602 r->args = *args;
2603
2604 } else {
2605 ngx_str_null(&r->args);
2606 }
2607
2608 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2609 "internal redirect: \"%V?%V\"", uri, &r->args);
2610
2611 ngx_http_set_exten(r);
2612
2613 /* clear the modules contexts */
2614 ngx_memzero(r->ctx, sizeof(void *) * ngx_http_max_module);
2615
2616 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
2617 r->loc_conf = cscf->ctx->loc_conf;
2618
2619 ngx_http_update_location_config(r);
2620
2621#if (NGX_HTTP_CACHE)
2622 r->cache = NULL;
2623#endif
2624
2625 r->internal = 1;
2626 r->valid_unparsed_uri = 0;
2627 r->add_uri_to_alias = 0;
2628 r->main->count++;
2629
2630 ngx_http_handler(r);
2631
2632 return NGX_DONE;
2633}
2634
2635
2636ngx_int_t
2637ngx_http_named_location(ngx_http_request_t *r, ngx_str_t *name)
2638{
2639 ngx_http_core_srv_conf_t *cscf;
2640 ngx_http_core_loc_conf_t **clcfp;
2641 ngx_http_core_main_conf_t *cmcf;
2642
2643 r->main->count++;
2644 r->uri_changes--;
2645
2646 if (r->uri_changes == 0) {
2647 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
2648 "rewrite or internal redirection cycle "
2649 "while redirect to named location \"%V\"", name);
2650
2651 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
2652 return NGX_DONE;
2653 }
2654
2655 if (r->uri.len == 0) {
2656 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
2657 "empty URI in redirect to named location \"%V\"", name);
2658
2659 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
2660 return NGX_DONE;
2661 }
2662
2663 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
2664
2665 if (cscf->named_locations) {
2666
2667 for (clcfp = cscf->named_locations; *clcfp; clcfp++) {
2668
2669 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2670 "test location: \"%V\"", &(*clcfp)->name);
2671
2672 if (name->len != (*clcfp)->name.len
2673 || ngx_strncmp(name->data, (*clcfp)->name.data, name->len) != 0)
2674 {
2675 continue;
2676 }
2677
2678 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2679 "using location: %V \"%V?%V\"",
2680 name, &r->uri, &r->args);
2681
2682 r->internal = 1;
2683 r->content_handler = NULL;
2684 r->uri_changed = 0;
2685 r->loc_conf = (*clcfp)->loc_conf;
2686
2687 /* clear the modules contexts */
2688 ngx_memzero(r->ctx, sizeof(void *) * ngx_http_max_module);
2689
2690 ngx_http_update_location_config(r);
2691
2692 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
2693
2694 r->phase_handler = cmcf->phase_engine.location_rewrite_index;
2695
2696 r->write_event_handler = ngx_http_core_run_phases;
2697 ngx_http_core_run_phases(r);
2698
2699 return NGX_DONE;
2700 }
2701 }
2702
2703 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
2704 "could not find named location \"%V\"", name);
2705
2706 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
2707
2708 return NGX_DONE;
2709}
2710
2711
2712ngx_http_cleanup_t *
2713ngx_http_cleanup_add(ngx_http_request_t *r, size_t size)
2714{
2715 ngx_http_cleanup_t *cln;
2716
2717 r = r->main;
2718
2719 cln = ngx_palloc(r->pool, sizeof(ngx_http_cleanup_t));
2720 if (cln == NULL) {
2721 return NULL;
2722 }
2723
2724 if (size) {
2725 cln->data = ngx_palloc(r->pool, size);
2726 if (cln->data == NULL) {
2727 return NULL;
2728 }
2729
2730 } else {
2731 cln->data = NULL;
2732 }
2733
2734 cln->handler = NULL;
2735 cln->next = r->cleanup;
2736
2737 r->cleanup = cln;
2738
2739 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2740 "http cleanup add: %p", cln);
2741
2742 return cln;
2743}
2744
2745
2746ngx_int_t
2747ngx_http_set_disable_symlinks(ngx_http_request_t *r,
2748 ngx_http_core_loc_conf_t *clcf, ngx_str_t *path, ngx_open_file_info_t *of)
2749{
2750#if (NGX_HAVE_OPENAT)
2751 u_char *p;
2752 ngx_str_t from;
2753
2754 of->disable_symlinks = clcf->disable_symlinks;
2755
2756 if (clcf->disable_symlinks_from == NULL) {
2757 return NGX_OK;
2758 }
2759
2760 if (ngx_http_complex_value(r, clcf->disable_symlinks_from, &from)
2761 != NGX_OK)
2762 {
2763 return NGX_ERROR;
2764 }
2765
2766 if (from.len == 0
2767 || from.len > path->len
2768 || ngx_memcmp(path->data, from.data, from.len) != 0)
2769 {
2770 return NGX_OK;
2771 }
2772
2773 if (from.len == path->len) {
2774 of->disable_symlinks = NGX_DISABLE_SYMLINKS_OFF;
2775 return NGX_OK;
2776 }
2777
2778 p = path->data + from.len;
2779
2780 if (*p == '/') {
2781 of->disable_symlinks_from = from.len;
2782 return NGX_OK;
2783 }
2784
2785 p--;
2786
2787 if (*p == '/') {
2788 of->disable_symlinks_from = from.len - 1;
2789 }
2790#endif
2791
2792 return NGX_OK;
2793}
2794
2795
2796ngx_int_t
2797ngx_http_get_forwarded_addr(ngx_http_request_t *r, ngx_addr_t *addr,
2798 ngx_array_t *headers, ngx_str_t *value, ngx_array_t *proxies,
2799 int recursive)
2800{
2801 ngx_int_t rc;
2802 ngx_uint_t i, found;
2803 ngx_table_elt_t **h;
2804
2805 if (headers == NULL) {
2806 return ngx_http_get_forwarded_addr_internal(r, addr, value->data,
2807 value->len, proxies,
2808 recursive);
2809 }
2810
2811 i = headers->nelts;
2812 h = headers->elts;
2813
2814 rc = NGX_DECLINED;
2815
2816 found = 0;
2817
2818 while (i-- > 0) {
2819 rc = ngx_http_get_forwarded_addr_internal(r, addr, h[i]->value.data,
2820 h[i]->value.len, proxies,
2821 recursive);
2822
2823 if (!recursive) {
2824 break;
2825 }
2826
2827 if (rc == NGX_DECLINED && found) {
2828 rc = NGX_DONE;
2829 break;
2830 }
2831
2832 if (rc != NGX_OK) {
2833 break;
2834 }
2835
2836 found = 1;
2837 }
2838
2839 return rc;
2840}
2841
2842
2843static ngx_int_t
2844ngx_http_get_forwarded_addr_internal(ngx_http_request_t *r, ngx_addr_t *addr,
2845 u_char *xff, size_t xfflen, ngx_array_t *proxies, int recursive)
2846{
2847 u_char *p;
2848 in_addr_t inaddr;
2849 ngx_int_t rc;
2850 ngx_addr_t paddr;
2851 ngx_cidr_t *cidr;
2852 ngx_uint_t family, i;
2853#if (NGX_HAVE_INET6)
2854 ngx_uint_t n;
2855 struct in6_addr *inaddr6;
2856#endif
2857
2858#if (NGX_SUPPRESS_WARN)
2859 inaddr = 0;
2860#if (NGX_HAVE_INET6)
2861 inaddr6 = NULL;
2862#endif
2863#endif
2864
2865 family = addr->sockaddr->sa_family;
2866
2867 if (family == AF_INET) {
2868 inaddr = ((struct sockaddr_in *) addr->sockaddr)->sin_addr.s_addr;
2869 }
2870
2871#if (NGX_HAVE_INET6)
2872 else if (family == AF_INET6) {
2873 inaddr6 = &((struct sockaddr_in6 *) addr->sockaddr)->sin6_addr;
2874
2875 if (IN6_IS_ADDR_V4MAPPED(inaddr6)) {
2876 family = AF_INET;
2877
2878 p = inaddr6->s6_addr;
2879
2880 inaddr = p[12] << 24;
2881 inaddr += p[13] << 16;
2882 inaddr += p[14] << 8;
2883 inaddr += p[15];
2884
2885 inaddr = htonl(inaddr);
2886 }
2887 }
2888#endif
2889
2890 for (cidr = proxies->elts, i = 0; i < proxies->nelts; i++) {
2891 if (cidr[i].family != family) {
2892 goto next;
2893 }
2894
2895 switch (family) {
2896
2897#if (NGX_HAVE_INET6)
2898 case AF_INET6:
2899 for (n = 0; n < 16; n++) {
2900 if ((inaddr6->s6_addr[n] & cidr[i].u.in6.mask.s6_addr[n])
2901 != cidr[i].u.in6.addr.s6_addr[n])
2902 {
2903 goto next;
2904 }
2905 }
2906 break;
2907#endif
2908
2909#if (NGX_HAVE_UNIX_DOMAIN)
2910 case AF_UNIX:
2911 break;
2912#endif
2913
2914 default: /* AF_INET */
2915 if ((inaddr & cidr[i].u.in.mask) != cidr[i].u.in.addr) {
2916 goto next;
2917 }
2918 break;
2919 }
2920
2921 for (p = xff + xfflen - 1; p > xff; p--, xfflen--) {
2922 if (*p != ' ' && *p != ',') {
2923 break;
2924 }
2925 }
2926
2927 for ( /* void */ ; p > xff; p--) {
2928 if (*p == ' ' || *p == ',') {
2929 p++;
2930 break;
2931 }
2932 }
2933
2934 if (ngx_parse_addr(r->pool, &paddr, p, xfflen - (p - xff)) != NGX_OK) {
2935 return NGX_DECLINED;
2936 }
2937
2938 *addr = paddr;
2939
2940 if (recursive && p > xff) {
2941 rc = ngx_http_get_forwarded_addr_internal(r, addr, xff, p - 1 - xff,
2942 proxies, 1);
2943
2944 if (rc == NGX_DECLINED) {
2945 return NGX_DONE;
2946 }
2947
2948 /* rc == NGX_OK || rc == NGX_DONE */
2949 return rc;
2950 }
2951
2952 return NGX_OK;
2953
2954 next:
2955 continue;
2956 }
2957
2958 return NGX_DECLINED;
2959}
2960
2961
2962static char *
2963ngx_http_core_server(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy)
2964{
2965 char *rv;
2966 void *mconf;
2967 ngx_uint_t i;
2968 ngx_conf_t pcf;
2969 ngx_http_module_t *module;
2970 struct sockaddr_in *sin;
2971 ngx_http_conf_ctx_t *ctx, *http_ctx;
2972 ngx_http_listen_opt_t lsopt;
2973 ngx_http_core_srv_conf_t *cscf, **cscfp;
2974 ngx_http_core_main_conf_t *cmcf;
2975
2976 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
2977 if (ctx == NULL) {
2978 return NGX_CONF_ERROR;
2979 }
2980
2981 http_ctx = cf->ctx;
2982 ctx->main_conf = http_ctx->main_conf;
2983
2984 /* the server{}'s srv_conf */
2985
2986 ctx->srv_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
2987 if (ctx->srv_conf == NULL) {
2988 return NGX_CONF_ERROR;
2989 }
2990
2991 /* the server{}'s loc_conf */
2992
2993 ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
2994 if (ctx->loc_conf == NULL) {
2995 return NGX_CONF_ERROR;
2996 }
2997
2998 for (i = 0; ngx_modules[i]; i++) {
2999 if (ngx_modules[i]->type != NGX_HTTP_MODULE) {
3000 continue;
3001 }
3002
3003 module = ngx_modules[i]->ctx;
3004
3005 if (module->create_srv_conf) {
3006 mconf = module->create_srv_conf(cf);
3007 if (mconf == NULL) {
3008 return NGX_CONF_ERROR;
3009 }
3010
3011 ctx->srv_conf[ngx_modules[i]->ctx_index] = mconf;
3012 }
3013
3014 if (module->create_loc_conf) {
3015 mconf = module->create_loc_conf(cf);
3016 if (mconf == NULL) {
3017 return NGX_CONF_ERROR;
3018 }
3019
3020 ctx->loc_conf[ngx_modules[i]->ctx_index] = mconf;
3021 }
3022 }
3023
3024
3025 /* the server configuration context */
3026
3027 cscf = ctx->srv_conf[ngx_http_core_module.ctx_index];
3028 cscf->ctx = ctx;
3029
3030
3031 cmcf = ctx->main_conf[ngx_http_core_module.ctx_index];
3032
3033 cscfp = ngx_array_push(&cmcf->servers);
3034 if (cscfp == NULL) {
3035 return NGX_CONF_ERROR;
3036 }
3037
3038 *cscfp = cscf;
3039
3040
3041 /* parse inside server{} */
3042
3043 pcf = *cf;
3044 cf->ctx = ctx;
3045 cf->cmd_type = NGX_HTTP_SRV_CONF;
3046
3047 rv = ngx_conf_parse(cf, NULL);
3048
3049 *cf = pcf;
3050
3051 if (rv == NGX_CONF_OK && !cscf->listen) {
3052 ngx_memzero(&lsopt, sizeof(ngx_http_listen_opt_t));
3053
3054 sin = &lsopt.u.sockaddr_in;
3055
3056 sin->sin_family = AF_INET;
3057#if (NGX_WIN32)
3058 sin->sin_port = htons(80);
3059#else
3060 sin->sin_port = htons((getuid() == 0) ? 80 : 8000);
3061#endif
3062 sin->sin_addr.s_addr = INADDR_ANY;
3063
3064 lsopt.socklen = sizeof(struct sockaddr_in);
3065
3066 lsopt.backlog = NGX_LISTEN_BACKLOG;
3067 lsopt.rcvbuf = -1;
3068 lsopt.sndbuf = -1;
3069#if (NGX_HAVE_SETFIB)
3070 lsopt.setfib = -1;
3071#endif
3072#if (NGX_HAVE_TCP_FASTOPEN)
3073 lsopt.fastopen = -1;
3074#endif
3075 lsopt.wildcard = 1;
3076
3077 (void) ngx_sock_ntop(&lsopt.u.sockaddr, lsopt.socklen, lsopt.addr,
3078 NGX_SOCKADDR_STRLEN, 1);
3079
3080 if (ngx_http_add_listen(cf, cscf, &lsopt) != NGX_OK) {
3081 return NGX_CONF_ERROR;
3082 }
3083 }
3084
3085 return rv;
3086}
3087
3088
3089static char *
3090ngx_http_core_location(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy)
3091{
3092 char *rv;
3093 u_char *mod;
3094 size_t len;
3095 ngx_str_t *value, *name;
3096 ngx_uint_t i;
3097 ngx_conf_t save;
3098 ngx_http_module_t *module;
3099 ngx_http_conf_ctx_t *ctx, *pctx;
3100 ngx_http_core_loc_conf_t *clcf, *pclcf;
3101
3102 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
3103 if (ctx == NULL) {
3104 return NGX_CONF_ERROR;
3105 }
3106
3107 pctx = cf->ctx;
3108 ctx->main_conf = pctx->main_conf;
3109 ctx->srv_conf = pctx->srv_conf;
3110
3111 ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
3112 if (ctx->loc_conf == NULL) {
3113 return NGX_CONF_ERROR;
3114 }
3115
3116 for (i = 0; ngx_modules[i]; i++) {
3117 if (ngx_modules[i]->type != NGX_HTTP_MODULE) {
3118 continue;
3119 }
3120
3121 module = ngx_modules[i]->ctx;
3122
3123 if (module->create_loc_conf) {
3124 ctx->loc_conf[ngx_modules[i]->ctx_index] =
3125 module->create_loc_conf(cf);
3126 if (ctx->loc_conf[ngx_modules[i]->ctx_index] == NULL) {
3127 return NGX_CONF_ERROR;
3128 }
3129 }
3130 }
3131
3132 clcf = ctx->loc_conf[ngx_http_core_module.ctx_index];
3133 clcf->loc_conf = ctx->loc_conf;
3134
3135 value = cf->args->elts;
3136
3137 if (cf->args->nelts == 3) {
3138
3139 len = value[1].len;
3140 mod = value[1].data;
3141 name = &value[2];
3142
3143 if (len == 1 && mod[0] == '=') {
3144
3145 clcf->name = *name;
3146 clcf->exact_match = 1;
3147
3148 } else if (len == 2 && mod[0] == '^' && mod[1] == '~') {
3149
3150 clcf->name = *name;
3151 clcf->noregex = 1;
3152
3153 } else if (len == 1 && mod[0] == '~') {
3154
3155 if (ngx_http_core_regex_location(cf, clcf, name, 0) != NGX_OK) {
3156 return NGX_CONF_ERROR;
3157 }
3158
3159 } else if (len == 2 && mod[0] == '~' && mod[1] == '*') {
3160
3161 if (ngx_http_core_regex_location(cf, clcf, name, 1) != NGX_OK) {
3162 return NGX_CONF_ERROR;
3163 }
3164
3165 } else {
3166 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3167 "invalid location modifier \"%V\"", &value[1]);
3168 return NGX_CONF_ERROR;
3169 }
3170
3171 } else {
3172
3173 name = &value[1];
3174
3175 if (name->data[0] == '=') {
3176
3177 clcf->name.len = name->len - 1;
3178 clcf->name.data = name->data + 1;
3179 clcf->exact_match = 1;
3180
3181 } else if (name->data[0] == '^' && name->data[1] == '~') {
3182
3183 clcf->name.len = name->len - 2;
3184 clcf->name.data = name->data + 2;
3185 clcf->noregex = 1;
3186
3187 } else if (name->data[0] == '~') {
3188
3189 name->len--;
3190 name->data++;
3191
3192 if (name->data[0] == '*') {
3193
3194 name->len--;
3195 name->data++;
3196
3197 if (ngx_http_core_regex_location(cf, clcf, name, 1) != NGX_OK) {
3198 return NGX_CONF_ERROR;
3199 }
3200
3201 } else {
3202 if (ngx_http_core_regex_location(cf, clcf, name, 0) != NGX_OK) {
3203 return NGX_CONF_ERROR;
3204 }
3205 }
3206
3207 } else {
3208
3209 clcf->name = *name;
3210
3211 if (name->data[0] == '@') {
3212 clcf->named = 1;
3213 }
3214 }
3215 }
3216
3217 pclcf = pctx->loc_conf[ngx_http_core_module.ctx_index];
3218
3219 if (pclcf->name.len) {
3220
3221 /* nested location */
3222
3223#if 0
3224 clcf->prev_location = pclcf;
3225#endif
3226
3227 if (pclcf->exact_match) {
3228 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3229 "location \"%V\" cannot be inside "
3230 "the exact location \"%V\"",
3231 &clcf->name, &pclcf->name);
3232 return NGX_CONF_ERROR;
3233 }
3234
3235 if (pclcf->named) {
3236 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3237 "location \"%V\" cannot be inside "
3238 "the named location \"%V\"",
3239 &clcf->name, &pclcf->name);
3240 return NGX_CONF_ERROR;
3241 }
3242
3243 if (clcf->named) {
3244 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3245 "named location \"%V\" can be "
3246 "on the server level only",
3247 &clcf->name);
3248 return NGX_CONF_ERROR;
3249 }
3250
3251 len = pclcf->name.len;
3252
3253#if (NGX_PCRE)
3254 if (clcf->regex == NULL
3255 && ngx_filename_cmp(clcf->name.data, pclcf->name.data, len) != 0)
3256#else
3257 if (ngx_filename_cmp(clcf->name.data, pclcf->name.data, len) != 0)
3258#endif
3259 {
3260 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3261 "location \"%V\" is outside location \"%V\"",
3262 &clcf->name, &pclcf->name);
3263 return NGX_CONF_ERROR;
3264 }
3265 }
3266
3267 if (ngx_http_add_location(cf, &pclcf->locations, clcf) != NGX_OK) {
3268 return NGX_CONF_ERROR;
3269 }
3270
3271 save = *cf;
3272 cf->ctx = ctx;
3273 cf->cmd_type = NGX_HTTP_LOC_CONF;
3274
3275 rv = ngx_conf_parse(cf, NULL);
3276
3277 *cf = save;
3278
3279 return rv;
3280}
3281
3282
3283static ngx_int_t
3284ngx_http_core_regex_location(ngx_conf_t *cf, ngx_http_core_loc_conf_t *clcf,
3285 ngx_str_t *regex, ngx_uint_t caseless)
3286{
3287#if (NGX_PCRE)
3288 ngx_regex_compile_t rc;
3289 u_char errstr[NGX_MAX_CONF_ERRSTR];
3290
3291 ngx_memzero(&rc, sizeof(ngx_regex_compile_t));
3292
3293 rc.pattern = *regex;
3294 rc.err.len = NGX_MAX_CONF_ERRSTR;
3295 rc.err.data = errstr;
3296
3297#if (NGX_HAVE_CASELESS_FILESYSTEM)
3298 rc.options = NGX_REGEX_CASELESS;
3299#else
3300 rc.options = caseless ? NGX_REGEX_CASELESS : 0;
3301#endif
3302
3303 clcf->regex = ngx_http_regex_compile(cf, &rc);
3304 if (clcf->regex == NULL) {
3305 return NGX_ERROR;
3306 }
3307
3308 clcf->name = *regex;
3309
3310 return NGX_OK;
3311
3312#else
3313
3314 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3315 "using regex \"%V\" requires PCRE library",
3316 regex);
3317 return NGX_ERROR;
3318
3319#endif
3320}
3321
3322
3323static char *
3324ngx_http_core_types(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
3325{
3326 ngx_http_core_loc_conf_t *clcf = conf;
3327
3328 char *rv;
3329 ngx_conf_t save;
3330
3331 if (clcf->types == NULL) {
3332 clcf->types = ngx_array_create(cf->pool, 64, sizeof(ngx_hash_key_t));
3333 if (clcf->types == NULL) {
3334 return NGX_CONF_ERROR;
3335 }
3336 }
3337
3338 save = *cf;
3339 cf->handler = ngx_http_core_type;
3340 cf->handler_conf = conf;
3341
3342 rv = ngx_conf_parse(cf, NULL);
3343
3344 *cf = save;
3345
3346 return rv;
3347}
3348
3349
3350static char *
3351ngx_http_core_type(ngx_conf_t *cf, ngx_command_t *dummy, void *conf)
3352{
3353 ngx_http_core_loc_conf_t *clcf = conf;
3354
3355 ngx_str_t *value, *content_type, *old;
3356 ngx_uint_t i, n, hash;
3357 ngx_hash_key_t *type;
3358
3359 value = cf->args->elts;
3360
3361 if (ngx_strcmp(value[0].data, "include") == 0) {
3362 if (cf->args->nelts != 2) {
3363 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3364 "invalid number of arguments"
3365 " in \"include\" directive");
3366 return NGX_CONF_ERROR;
3367 }
3368
3369 return ngx_conf_include(cf, dummy, conf);
3370 }
3371
3372 content_type = ngx_palloc(cf->pool, sizeof(ngx_str_t));
3373 if (content_type == NULL) {
3374 return NGX_CONF_ERROR;
3375 }
3376
3377 *content_type = value[0];
3378
3379 for (i = 1; i < cf->args->nelts; i++) {
3380
3381 hash = ngx_hash_strlow(value[i].data, value[i].data, value[i].len);
3382
3383 type = clcf->types->elts;
3384 for (n = 0; n < clcf->types->nelts; n++) {
3385 if (ngx_strcmp(value[i].data, type[n].key.data) == 0) {
3386 old = type[n].value;
3387 type[n].value = content_type;
3388
3389 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
3390 "duplicate extension \"%V\", "
3391 "content type: \"%V\", "
3392 "previous content type: \"%V\"",
3393 &value[i], content_type, old);
3394 goto next;
3395 }
3396 }
3397
3398
3399 type = ngx_array_push(clcf->types);
3400 if (type == NULL) {
3401 return NGX_CONF_ERROR;
3402 }
3403
3404 type->key = value[i];
3405 type->key_hash = hash;
3406 type->value = content_type;
3407
3408 next:
3409 continue;
3410 }
3411
3412 return NGX_CONF_OK;
3413}
3414
3415
3416static ngx_int_t
3417ngx_http_core_preconfiguration(ngx_conf_t *cf)
3418{
3419 return ngx_http_variables_add_core_vars(cf);
3420}
3421
3422
3423static void *
3424ngx_http_core_create_main_conf(ngx_conf_t *cf)
3425{
3426 ngx_http_core_main_conf_t *cmcf;
3427
3428 cmcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_core_main_conf_t));
3429 if (cmcf == NULL) {
3430 return NULL;
3431 }
3432
3433 if (ngx_array_init(&cmcf->servers, cf->pool, 4,
3434 sizeof(ngx_http_core_srv_conf_t *))
3435 != NGX_OK)
3436 {
3437 return NULL;
3438 }
3439
3440 cmcf->server_names_hash_max_size = NGX_CONF_UNSET_UINT;
3441 cmcf->server_names_hash_bucket_size = NGX_CONF_UNSET_UINT;
3442
3443 cmcf->variables_hash_max_size = NGX_CONF_UNSET_UINT;
3444 cmcf->variables_hash_bucket_size = NGX_CONF_UNSET_UINT;
3445
3446 return cmcf;
3447}
3448
3449
3450static char *
3451ngx_http_core_init_main_conf(ngx_conf_t *cf, void *conf)
3452{
3453 ngx_http_core_main_conf_t *cmcf = conf;
3454
3455 ngx_conf_init_uint_value(cmcf->server_names_hash_max_size, 512);
3456 ngx_conf_init_uint_value(cmcf->server_names_hash_bucket_size,
3457 ngx_cacheline_size);
3458
3459 cmcf->server_names_hash_bucket_size =
3460 ngx_align(cmcf->server_names_hash_bucket_size, ngx_cacheline_size);
3461
3462
3463 ngx_conf_init_uint_value(cmcf->variables_hash_max_size, 1024);
3464 ngx_conf_init_uint_value(cmcf->variables_hash_bucket_size, 64);
3465
3466 cmcf->variables_hash_bucket_size =
3467 ngx_align(cmcf->variables_hash_bucket_size, ngx_cacheline_size);
3468
3469 if (cmcf->ncaptures) {
3470 cmcf->ncaptures = (cmcf->ncaptures + 1) * 3;
3471 }
3472
3473 return NGX_CONF_OK;
3474}
3475
3476
3477static void *
3478ngx_http_core_create_srv_conf(ngx_conf_t *cf)
3479{
3480 ngx_http_core_srv_conf_t *cscf;
3481
3482 cscf = ngx_pcalloc(cf->pool, sizeof(ngx_http_core_srv_conf_t));
3483 if (cscf == NULL) {
3484 return NULL;
3485 }
3486
3487 /*
3488 * set by ngx_pcalloc():
3489 *
3490 * conf->client_large_buffers.num = 0;
3491 */
3492
3493 if (ngx_array_init(&cscf->server_names, cf->temp_pool, 4,
3494 sizeof(ngx_http_server_name_t))
3495 != NGX_OK)
3496 {
3497 return NULL;
3498 }
3499
3500 cscf->connection_pool_size = NGX_CONF_UNSET_SIZE;
3501 cscf->request_pool_size = NGX_CONF_UNSET_SIZE;
3502 cscf->client_header_timeout = NGX_CONF_UNSET_MSEC;
3503 cscf->client_header_buffer_size = NGX_CONF_UNSET_SIZE;
3504 cscf->ignore_invalid_headers = NGX_CONF_UNSET;
3505 cscf->merge_slashes = NGX_CONF_UNSET;
3506 cscf->underscores_in_headers = NGX_CONF_UNSET;
3507
3508 return cscf;
3509}
3510
3511
3512static char *
3513ngx_http_core_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
3514{
3515 ngx_http_core_srv_conf_t *prev = parent;
3516 ngx_http_core_srv_conf_t *conf = child;
3517
3518 ngx_str_t name;
3519 ngx_http_server_name_t *sn;
3520
3521 /* TODO: it does not merge, it inits only */
3522
3523 ngx_conf_merge_size_value(conf->connection_pool_size,
3524 prev->connection_pool_size, 256);
3525 ngx_conf_merge_size_value(conf->request_pool_size,
3526 prev->request_pool_size, 4096);
3527 ngx_conf_merge_msec_value(conf->client_header_timeout,
3528 prev->client_header_timeout, 60000);
3529 ngx_conf_merge_size_value(conf->client_header_buffer_size,
3530 prev->client_header_buffer_size, 1024);
3531 ngx_conf_merge_bufs_value(conf->large_client_header_buffers,
3532 prev->large_client_header_buffers,
3533 4, 8192);
3534
3535 if (conf->large_client_header_buffers.size < conf->connection_pool_size) {
3536 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3537 "the \"large_client_header_buffers\" size must be "
3538 "equal to or greater than \"connection_pool_size\"");
3539 return NGX_CONF_ERROR;
3540 }
3541
3542 ngx_conf_merge_value(conf->ignore_invalid_headers,
3543 prev->ignore_invalid_headers, 1);
3544
3545 ngx_conf_merge_value(conf->merge_slashes, prev->merge_slashes, 1);
3546
3547 ngx_conf_merge_value(conf->underscores_in_headers,
3548 prev->underscores_in_headers, 0);
3549
3550 if (conf->server_names.nelts == 0) {
3551 /* the array has 4 empty preallocated elements, so push cannot fail */
3552 sn = ngx_array_push(&conf->server_names);
3553#if (NGX_PCRE)
3554 sn->regex = NULL;
3555#endif
3556 sn->server = conf;
3557 ngx_str_set(&sn->name, "");
3558 }
3559
3560 sn = conf->server_names.elts;
3561 name = sn[0].name;
3562
3563#if (NGX_PCRE)
3564 if (sn->regex) {
3565 name.len++;
3566 name.data--;
3567 } else
3568#endif
3569
3570 if (name.data[0] == '.') {
3571 name.len--;
3572 name.data++;
3573 }
3574
3575 conf->server_name.len = name.len;
3576 conf->server_name.data = ngx_pstrdup(cf->pool, &name);
3577 if (conf->server_name.data == NULL) {
3578 return NGX_CONF_ERROR;
3579 }
3580
3581 return NGX_CONF_OK;
3582}
3583
3584
3585static void *
3586ngx_http_core_create_loc_conf(ngx_conf_t *cf)
3587{
3588 ngx_http_core_loc_conf_t *clcf;
3589
3590 clcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_core_loc_conf_t));
3591 if (clcf == NULL) {
3592 return NULL;
3593 }
3594
3595 /*
3596 * set by ngx_pcalloc():
3597 *
3598 * clcf->root = { 0, NULL };
3599 * clcf->limit_except = 0;
3600 * clcf->post_action = { 0, NULL };
3601 * clcf->types = NULL;
3602 * clcf->default_type = { 0, NULL };
3603 * clcf->error_log = NULL;
3604 * clcf->error_pages = NULL;
3605 * clcf->try_files = NULL;
3606 * clcf->client_body_path = NULL;
3607 * clcf->regex = NULL;
3608 * clcf->exact_match = 0;
3609 * clcf->auto_redirect = 0;
3610 * clcf->alias = 0;
3611 * clcf->gzip_proxied = 0;
3612 * clcf->keepalive_disable = 0;
3613 */
3614
3615 clcf->client_max_body_size = NGX_CONF_UNSET;
3616 clcf->client_body_buffer_size = NGX_CONF_UNSET_SIZE;
3617 clcf->client_body_timeout = NGX_CONF_UNSET_MSEC;
3618 clcf->satisfy = NGX_CONF_UNSET_UINT;
3619 clcf->if_modified_since = NGX_CONF_UNSET_UINT;
3620 clcf->max_ranges = NGX_CONF_UNSET_UINT;
3621 clcf->client_body_in_file_only = NGX_CONF_UNSET_UINT;
3622 clcf->client_body_in_single_buffer = NGX_CONF_UNSET;
3623 clcf->internal = NGX_CONF_UNSET;
3624 clcf->sendfile = NGX_CONF_UNSET;
3625 clcf->sendfile_max_chunk = NGX_CONF_UNSET_SIZE;
3626 clcf->aio = NGX_CONF_UNSET;
3627 clcf->read_ahead = NGX_CONF_UNSET_SIZE;
3628 clcf->directio = NGX_CONF_UNSET;
3629 clcf->directio_alignment = NGX_CONF_UNSET;
3630 clcf->tcp_nopush = NGX_CONF_UNSET;
3631 clcf->tcp_nodelay = NGX_CONF_UNSET;
3632 clcf->send_timeout = NGX_CONF_UNSET_MSEC;
3633 clcf->send_lowat = NGX_CONF_UNSET_SIZE;
3634 clcf->postpone_output = NGX_CONF_UNSET_SIZE;
3635 clcf->limit_rate = NGX_CONF_UNSET_SIZE;
3636 clcf->limit_rate_after = NGX_CONF_UNSET_SIZE;
3637 clcf->keepalive_timeout = NGX_CONF_UNSET_MSEC;
3638 clcf->keepalive_header = NGX_CONF_UNSET;
3639 clcf->keepalive_requests = NGX_CONF_UNSET_UINT;
3640 clcf->lingering_close = NGX_CONF_UNSET_UINT;
3641 clcf->lingering_time = NGX_CONF_UNSET_MSEC;
3642 clcf->lingering_timeout = NGX_CONF_UNSET_MSEC;
3643 clcf->resolver_timeout = NGX_CONF_UNSET_MSEC;
3644 clcf->reset_timedout_connection = NGX_CONF_UNSET;
3645 clcf->server_name_in_redirect = NGX_CONF_UNSET;
3646 clcf->port_in_redirect = NGX_CONF_UNSET;
3647 clcf->msie_padding = NGX_CONF_UNSET;
3648 clcf->msie_refresh = NGX_CONF_UNSET;
3649 clcf->log_not_found = NGX_CONF_UNSET;
3650 clcf->log_subrequest = NGX_CONF_UNSET;
3651 clcf->recursive_error_pages = NGX_CONF_UNSET;
3652 clcf->server_tokens = NGX_CONF_UNSET;
3653 clcf->chunked_transfer_encoding = NGX_CONF_UNSET;
3654 clcf->etag = NGX_CONF_UNSET;
3655 clcf->types_hash_max_size = NGX_CONF_UNSET_UINT;
3656 clcf->types_hash_bucket_size = NGX_CONF_UNSET_UINT;
3657
3658 clcf->open_file_cache = NGX_CONF_UNSET_PTR;
3659 clcf->open_file_cache_valid = NGX_CONF_UNSET;
3660 clcf->open_file_cache_min_uses = NGX_CONF_UNSET_UINT;
3661 clcf->open_file_cache_errors = NGX_CONF_UNSET;
3662 clcf->open_file_cache_events = NGX_CONF_UNSET;
3663
3664#if (NGX_HTTP_GZIP)
3665 clcf->gzip_vary = NGX_CONF_UNSET;
3666 clcf->gzip_http_version = NGX_CONF_UNSET_UINT;
3667#if (NGX_PCRE)
3668 clcf->gzip_disable = NGX_CONF_UNSET_PTR;
3669#endif
3670 clcf->gzip_disable_msie6 = 3;
3671#if (NGX_HTTP_DEGRADATION)
3672 clcf->gzip_disable_degradation = 3;
3673#endif
3674#endif
3675
3676#if (NGX_HAVE_OPENAT)
3677 clcf->disable_symlinks = NGX_CONF_UNSET_UINT;
3678 clcf->disable_symlinks_from = NGX_CONF_UNSET_PTR;
3679#endif
3680
3681 return clcf;
3682}
3683
3684
3685static ngx_str_t ngx_http_core_text_html_type = ngx_string("text/html");
3686static ngx_str_t ngx_http_core_image_gif_type = ngx_string("image/gif");
3687static ngx_str_t ngx_http_core_image_jpeg_type = ngx_string("image/jpeg");
3688
3689static ngx_hash_key_t ngx_http_core_default_types[] = {
3690 { ngx_string("html"), 0, &ngx_http_core_text_html_type },
3691 { ngx_string("gif"), 0, &ngx_http_core_image_gif_type },
3692 { ngx_string("jpg"), 0, &ngx_http_core_image_jpeg_type },
3693 { ngx_null_string, 0, NULL }
3694};
3695
3696
3697static char *
3698ngx_http_core_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
3699{
3700 ngx_http_core_loc_conf_t *prev = parent;
3701 ngx_http_core_loc_conf_t *conf = child;
3702
3703 ngx_uint_t i;
3704 ngx_hash_key_t *type;
3705 ngx_hash_init_t types_hash;
3706
3707 if (conf->root.data == NULL) {
3708
3709 conf->alias = prev->alias;
3710 conf->root = prev->root;
3711 conf->root_lengths = prev->root_lengths;
3712 conf->root_values = prev->root_values;
3713
3714 if (prev->root.data == NULL) {
3715 ngx_str_set(&conf->root, "html");
3716
3717 if (ngx_conf_full_name(cf->cycle, &conf->root, 0) != NGX_OK) {
3718 return NGX_CONF_ERROR;
3719 }
3720 }
3721 }
3722
3723 if (conf->post_action.data == NULL) {
3724 conf->post_action = prev->post_action;
3725 }
3726
3727 ngx_conf_merge_uint_value(conf->types_hash_max_size,
3728 prev->types_hash_max_size, 1024);
3729
3730 ngx_conf_merge_uint_value(conf->types_hash_bucket_size,
3731 prev->types_hash_bucket_size, 64);
3732
3733 conf->types_hash_bucket_size = ngx_align(conf->types_hash_bucket_size,
3734 ngx_cacheline_size);
3735
3736 /*
3737 * the special handling of the "types" directive in the "http" section
3738 * to inherit the http's conf->types_hash to all servers
3739 */
3740
3741 if (prev->types && prev->types_hash.buckets == NULL) {
3742
3743 types_hash.hash = &prev->types_hash;
3744 types_hash.key = ngx_hash_key_lc;
3745 types_hash.max_size = conf->types_hash_max_size;
3746 types_hash.bucket_size = conf->types_hash_bucket_size;
3747 types_hash.name = "types_hash";
3748 types_hash.pool = cf->pool;
3749 types_hash.temp_pool = NULL;
3750
3751 if (ngx_hash_init(&types_hash, prev->types->elts, prev->types->nelts)
3752 != NGX_OK)
3753 {
3754 return NGX_CONF_ERROR;
3755 }
3756 }
3757
3758 if (conf->types == NULL) {
3759 conf->types = prev->types;
3760 conf->types_hash = prev->types_hash;
3761 }
3762
3763 if (conf->types == NULL) {
3764 conf->types = ngx_array_create(cf->pool, 3, sizeof(ngx_hash_key_t));
3765 if (conf->types == NULL) {
3766 return NGX_CONF_ERROR;
3767 }
3768
3769 for (i = 0; ngx_http_core_default_types[i].key.len; i++) {
3770 type = ngx_array_push(conf->types);
3771 if (type == NULL) {
3772 return NGX_CONF_ERROR;
3773 }
3774
3775 type->key = ngx_http_core_default_types[i].key;
3776 type->key_hash =
3777 ngx_hash_key_lc(ngx_http_core_default_types[i].key.data,
3778 ngx_http_core_default_types[i].key.len);
3779 type->value = ngx_http_core_default_types[i].value;
3780 }
3781 }
3782
3783 if (conf->types_hash.buckets == NULL) {
3784
3785 types_hash.hash = &conf->types_hash;
3786 types_hash.key = ngx_hash_key_lc;
3787 types_hash.max_size = conf->types_hash_max_size;
3788 types_hash.bucket_size = conf->types_hash_bucket_size;
3789 types_hash.name = "types_hash";
3790 types_hash.pool = cf->pool;
3791 types_hash.temp_pool = NULL;
3792
3793 if (ngx_hash_init(&types_hash, conf->types->elts, conf->types->nelts)
3794 != NGX_OK)
3795 {
3796 return NGX_CONF_ERROR;
3797 }
3798 }
3799
3800 if (conf->error_log == NULL) {
3801 if (prev->error_log) {
3802 conf->error_log = prev->error_log;
3803 } else {
3804 conf->error_log = &cf->cycle->new_log;
3805 }
3806 }
3807
3808 if (conf->error_pages == NULL && prev->error_pages) {
3809 conf->error_pages = prev->error_pages;
3810 }
3811
3812 ngx_conf_merge_str_value(conf->default_type,
3813 prev->default_type, "text/plain");
3814
3815 ngx_conf_merge_off_value(conf->client_max_body_size,
3816 prev->client_max_body_size, 1 * 1024 * 1024);
3817 ngx_conf_merge_size_value(conf->client_body_buffer_size,
3818 prev->client_body_buffer_size,
3819 (size_t) 2 * ngx_pagesize);
3820 ngx_conf_merge_msec_value(conf->client_body_timeout,
3821 prev->client_body_timeout, 60000);
3822
3823 ngx_conf_merge_bitmask_value(conf->keepalive_disable,
3824 prev->keepalive_disable,
3825 (NGX_CONF_BITMASK_SET
3826 |NGX_HTTP_KEEPALIVE_DISABLE_MSIE6));
3827 ngx_conf_merge_uint_value(conf->satisfy, prev->satisfy,
3828 NGX_HTTP_SATISFY_ALL);
3829 ngx_conf_merge_uint_value(conf->if_modified_since, prev->if_modified_since,
3830 NGX_HTTP_IMS_EXACT);
3831 ngx_conf_merge_uint_value(conf->max_ranges, prev->max_ranges,
3832 NGX_MAX_INT32_VALUE);
3833 ngx_conf_merge_uint_value(conf->client_body_in_file_only,
3834 prev->client_body_in_file_only,
3835 NGX_HTTP_REQUEST_BODY_FILE_OFF);
3836 ngx_conf_merge_value(conf->client_body_in_single_buffer,
3837 prev->client_body_in_single_buffer, 0);
3838 ngx_conf_merge_value(conf->internal, prev->internal, 0);
3839 ngx_conf_merge_value(conf->sendfile, prev->sendfile, 0);
3840 ngx_conf_merge_size_value(conf->sendfile_max_chunk,
3841 prev->sendfile_max_chunk, 0);
3842 ngx_conf_merge_value(conf->aio, prev->aio, NGX_HTTP_AIO_OFF);
3843 ngx_conf_merge_size_value(conf->read_ahead, prev->read_ahead, 0);
3844 ngx_conf_merge_off_value(conf->directio, prev->directio,
3845 NGX_OPEN_FILE_DIRECTIO_OFF);
3846 ngx_conf_merge_off_value(conf->directio_alignment, prev->directio_alignment,
3847 512);
3848 ngx_conf_merge_value(conf->tcp_nopush, prev->tcp_nopush, 0);
3849 ngx_conf_merge_value(conf->tcp_nodelay, prev->tcp_nodelay, 1);
3850
3851 ngx_conf_merge_msec_value(conf->send_timeout, prev->send_timeout, 60000);
3852 ngx_conf_merge_size_value(conf->send_lowat, prev->send_lowat, 0);
3853 ngx_conf_merge_size_value(conf->postpone_output, prev->postpone_output,
3854 1460);
3855 ngx_conf_merge_size_value(conf->limit_rate, prev->limit_rate, 0);
3856 ngx_conf_merge_size_value(conf->limit_rate_after, prev->limit_rate_after,
3857 0);
3858 ngx_conf_merge_msec_value(conf->keepalive_timeout,
3859 prev->keepalive_timeout, 75000);
3860 ngx_conf_merge_sec_value(conf->keepalive_header,
3861 prev->keepalive_header, 0);
3862 ngx_conf_merge_uint_value(conf->keepalive_requests,
3863 prev->keepalive_requests, 100);
3864 ngx_conf_merge_uint_value(conf->lingering_close,
3865 prev->lingering_close, NGX_HTTP_LINGERING_ON);
3866 ngx_conf_merge_msec_value(conf->lingering_time,
3867 prev->lingering_time, 30000);
3868 ngx_conf_merge_msec_value(conf->lingering_timeout,
3869 prev->lingering_timeout, 5000);
3870 ngx_conf_merge_msec_value(conf->resolver_timeout,
3871 prev->resolver_timeout, 30000);
3872
3873 if (conf->resolver == NULL) {
3874
3875 if (prev->resolver == NULL) {
3876
3877 /*
3878 * create dummy resolver in http {} context
3879 * to inherit it in all servers
3880 */
3881
3882 prev->resolver = ngx_resolver_create(cf, NULL, 0);
3883 if (prev->resolver == NULL) {
3884 return NGX_CONF_ERROR;
3885 }
3886 }
3887
3888 conf->resolver = prev->resolver;
3889 }
3890
3891 if (ngx_conf_merge_path_value(cf, &conf->client_body_temp_path,
3892 prev->client_body_temp_path,
3893 &ngx_http_client_temp_path)
3894 != NGX_OK)
3895 {
3896 return NGX_CONF_ERROR;
3897 }
3898
3899 ngx_conf_merge_value(conf->reset_timedout_connection,
3900 prev->reset_timedout_connection, 0);
3901 ngx_conf_merge_value(conf->server_name_in_redirect,
3902 prev->server_name_in_redirect, 0);
3903 ngx_conf_merge_value(conf->port_in_redirect, prev->port_in_redirect, 1);
3904 ngx_conf_merge_value(conf->msie_padding, prev->msie_padding, 1);
3905 ngx_conf_merge_value(conf->msie_refresh, prev->msie_refresh, 0);
3906 ngx_conf_merge_value(conf->log_not_found, prev->log_not_found, 1);
3907 ngx_conf_merge_value(conf->log_subrequest, prev->log_subrequest, 0);
3908 ngx_conf_merge_value(conf->recursive_error_pages,
3909 prev->recursive_error_pages, 0);
3910 ngx_conf_merge_value(conf->server_tokens, prev->server_tokens, 1);
3911 ngx_conf_merge_value(conf->chunked_transfer_encoding,
3912 prev->chunked_transfer_encoding, 1);
3913 ngx_conf_merge_value(conf->etag, prev->etag, 1);
3914
3915 ngx_conf_merge_ptr_value(conf->open_file_cache,
3916 prev->open_file_cache, NULL);
3917
3918 ngx_conf_merge_sec_value(conf->open_file_cache_valid,
3919 prev->open_file_cache_valid, 60);
3920
3921 ngx_conf_merge_uint_value(conf->open_file_cache_min_uses,
3922 prev->open_file_cache_min_uses, 1);
3923
3924 ngx_conf_merge_sec_value(conf->open_file_cache_errors,
3925 prev->open_file_cache_errors, 0);
3926
3927 ngx_conf_merge_sec_value(conf->open_file_cache_events,
3928 prev->open_file_cache_events, 0);
3929#if (NGX_HTTP_GZIP)
3930
3931 ngx_conf_merge_value(conf->gzip_vary, prev->gzip_vary, 0);
3932 ngx_conf_merge_uint_value(conf->gzip_http_version, prev->gzip_http_version,
3933 NGX_HTTP_VERSION_11);
3934 ngx_conf_merge_bitmask_value(conf->gzip_proxied, prev->gzip_proxied,
3935 (NGX_CONF_BITMASK_SET|NGX_HTTP_GZIP_PROXIED_OFF));
3936
3937#if (NGX_PCRE)
3938 ngx_conf_merge_ptr_value(conf->gzip_disable, prev->gzip_disable, NULL);
3939#endif
3940
3941 if (conf->gzip_disable_msie6 == 3) {
3942 conf->gzip_disable_msie6 =
3943 (prev->gzip_disable_msie6 == 3) ? 0 : prev->gzip_disable_msie6;
3944 }
3945
3946#if (NGX_HTTP_DEGRADATION)
3947
3948 if (conf->gzip_disable_degradation == 3) {
3949 conf->gzip_disable_degradation =
3950 (prev->gzip_disable_degradation == 3) ?
3951 0 : prev->gzip_disable_degradation;
3952 }
3953
3954#endif
3955#endif
3956
3957#if (NGX_HAVE_OPENAT)
3958 ngx_conf_merge_uint_value(conf->disable_symlinks, prev->disable_symlinks,
3959 NGX_DISABLE_SYMLINKS_OFF);
3960 ngx_conf_merge_ptr_value(conf->disable_symlinks_from,
3961 prev->disable_symlinks_from, NULL);
3962#endif
3963
3964 return NGX_CONF_OK;
3965}
3966
3967
3968static char *
3969ngx_http_core_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
3970{
3971 ngx_http_core_srv_conf_t *cscf = conf;
3972
3973 ngx_str_t *value, size;
3974 ngx_url_t u;
3975 ngx_uint_t n;
3976 ngx_http_listen_opt_t lsopt;
3977
3978 cscf->listen = 1;
3979
3980 value = cf->args->elts;
3981
3982 ngx_memzero(&u, sizeof(ngx_url_t));
3983
3984 u.url = value[1];
3985 u.listen = 1;
3986 u.default_port = 80;
3987
3988 if (ngx_parse_url(cf->pool, &u) != NGX_OK) {
3989 if (u.err) {
3990 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3991 "%s in \"%V\" of the \"listen\" directive",
3992 u.err, &u.url);
3993 }
3994
3995 return NGX_CONF_ERROR;
3996 }
3997
3998 ngx_memzero(&lsopt, sizeof(ngx_http_listen_opt_t));
3999
4000 ngx_memcpy(&lsopt.u.sockaddr, u.sockaddr, u.socklen);
4001
4002 lsopt.socklen = u.socklen;
4003 lsopt.backlog = NGX_LISTEN_BACKLOG;
4004 lsopt.rcvbuf = -1;
4005 lsopt.sndbuf = -1;
4006#if (NGX_HAVE_SETFIB)
4007 lsopt.setfib = -1;
4008#endif
4009#if (NGX_HAVE_TCP_FASTOPEN)
4010 lsopt.fastopen = -1;
4011#endif
4012 lsopt.wildcard = u.wildcard;
4013#if (NGX_HAVE_INET6 && defined IPV6_V6ONLY)
4014 lsopt.ipv6only = 1;
4015#endif
4016
4017 (void) ngx_sock_ntop(&lsopt.u.sockaddr, lsopt.socklen, lsopt.addr,
4018 NGX_SOCKADDR_STRLEN, 1);
4019
4020 for (n = 2; n < cf->args->nelts; n++) {
4021
4022 if (ngx_strcmp(value[n].data, "default_server") == 0
4023 || ngx_strcmp(value[n].data, "default") == 0)
4024 {
4025 lsopt.default_server = 1;
4026 continue;
4027 }
4028
4029 if (ngx_strcmp(value[n].data, "bind") == 0) {
4030 lsopt.set = 1;
4031 lsopt.bind = 1;
4032 continue;
4033 }
4034
4035#if (NGX_HAVE_SETFIB)
4036 if (ngx_strncmp(value[n].data, "setfib=", 7) == 0) {
4037 lsopt.setfib = ngx_atoi(value[n].data + 7, value[n].len - 7);
4038 lsopt.set = 1;
4039 lsopt.bind = 1;
4040
4041 if (lsopt.setfib == NGX_ERROR) {
4042 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4043 "invalid setfib \"%V\"", &value[n]);
4044 return NGX_CONF_ERROR;
4045 }
4046
4047 continue;
4048 }
4049#endif
4050
4051#if (NGX_HAVE_TCP_FASTOPEN)
4052 if (ngx_strncmp(value[n].data, "fastopen=", 9) == 0) {
4053 lsopt.fastopen = ngx_atoi(value[n].data + 9, value[n].len - 9);
4054 lsopt.set = 1;
4055 lsopt.bind = 1;
4056
4057 if (lsopt.fastopen == NGX_ERROR) {
4058 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4059 "invalid fastopen \"%V\"", &value[n]);
4060 return NGX_CONF_ERROR;
4061 }
4062
4063 continue;
4064 }
4065#endif
4066
4067 if (ngx_strncmp(value[n].data, "backlog=", 8) == 0) {
4068 lsopt.backlog = ngx_atoi(value[n].data + 8, value[n].len - 8);
4069 lsopt.set = 1;
4070 lsopt.bind = 1;
4071
4072 if (lsopt.backlog == NGX_ERROR || lsopt.backlog == 0) {
4073 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4074 "invalid backlog \"%V\"", &value[n]);
4075 return NGX_CONF_ERROR;
4076 }
4077
4078 continue;
4079 }
4080
4081 if (ngx_strncmp(value[n].data, "rcvbuf=", 7) == 0) {
4082 size.len = value[n].len - 7;
4083 size.data = value[n].data + 7;
4084
4085 lsopt.rcvbuf = ngx_parse_size(&size);
4086 lsopt.set = 1;
4087 lsopt.bind = 1;
4088
4089 if (lsopt.rcvbuf == NGX_ERROR) {
4090 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4091 "invalid rcvbuf \"%V\"", &value[n]);
4092 return NGX_CONF_ERROR;
4093 }
4094
4095 continue;
4096 }
4097
4098 if (ngx_strncmp(value[n].data, "sndbuf=", 7) == 0) {
4099 size.len = value[n].len - 7;
4100 size.data = value[n].data + 7;
4101
4102 lsopt.sndbuf = ngx_parse_size(&size);
4103 lsopt.set = 1;
4104 lsopt.bind = 1;
4105
4106 if (lsopt.sndbuf == NGX_ERROR) {
4107 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4108 "invalid sndbuf \"%V\"", &value[n]);
4109 return NGX_CONF_ERROR;
4110 }
4111
4112 continue;
4113 }
4114
4115 if (ngx_strncmp(value[n].data, "accept_filter=", 14) == 0) {
4116#if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
4117 lsopt.accept_filter = (char *) &value[n].data[14];
4118 lsopt.set = 1;
4119 lsopt.bind = 1;
4120#else
4121 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4122 "accept filters \"%V\" are not supported "
4123 "on this platform, ignored",
4124 &value[n]);
4125#endif
4126 continue;
4127 }
4128
4129 if (ngx_strcmp(value[n].data, "deferred") == 0) {
4130#if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)
4131 lsopt.deferred_accept = 1;
4132 lsopt.set = 1;
4133 lsopt.bind = 1;
4134#else
4135 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4136 "the deferred accept is not supported "
4137 "on this platform, ignored");
4138#endif
4139 continue;
4140 }
4141
4142 if (ngx_strncmp(value[n].data, "ipv6only=o", 10) == 0) {
4143#if (NGX_HAVE_INET6 && defined IPV6_V6ONLY)
4144 struct sockaddr *sa;
4145
4146 sa = &lsopt.u.sockaddr;
4147
4148 if (sa->sa_family == AF_INET6) {
4149
4150 if (ngx_strcmp(&value[n].data[10], "n") == 0) {
4151 lsopt.ipv6only = 1;
4152
4153 } else if (ngx_strcmp(&value[n].data[10], "ff") == 0) {
4154 lsopt.ipv6only = 0;
4155
4156 } else {
4157 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4158 "invalid ipv6only flags \"%s\"",
4159 &value[n].data[9]);
4160 return NGX_CONF_ERROR;
4161 }
4162
4163 lsopt.set = 1;
4164 lsopt.bind = 1;
4165
4166 } else {
4167 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4168 "ipv6only is not supported "
4169 "on addr \"%s\", ignored", lsopt.addr);
4170 }
4171
4172 continue;
4173#else
4174 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4175 "ipv6only is not supported "
4176 "on this platform");
4177 return NGX_CONF_ERROR;
4178#endif
4179 }
4180
4181 if (ngx_strcmp(value[n].data, "ssl") == 0) {
4182#if (NGX_HTTP_SSL)
4183 lsopt.ssl = 1;
4184 continue;
4185#else
4186 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4187 "the \"ssl\" parameter requires "
4188 "ngx_http_ssl_module");
4189 return NGX_CONF_ERROR;
4190#endif
4191 }
4192
4193 if (ngx_strcmp(value[n].data, "spdy") == 0) {
4194#if (NGX_HTTP_SPDY)
4195 lsopt.spdy = 1;
4196 continue;
4197#else
4198 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4199 "the \"spdy\" parameter requires "
4200 "ngx_http_spdy_module");
4201 return NGX_CONF_ERROR;
4202#endif
4203 }
4204
4205 if (ngx_strncmp(value[n].data, "so_keepalive=", 13) == 0) {
4206
4207 if (ngx_strcmp(&value[n].data[13], "on") == 0) {
4208 lsopt.so_keepalive = 1;
4209
4210 } else if (ngx_strcmp(&value[n].data[13], "off") == 0) {
4211 lsopt.so_keepalive = 2;
4212
4213 } else {
4214
4215#if (NGX_HAVE_KEEPALIVE_TUNABLE)
4216 u_char *p, *end;
4217 ngx_str_t s;
4218
4219 end = value[n].data + value[n].len;
4220 s.data = value[n].data + 13;
4221
4222 p = ngx_strlchr(s.data, end, ':');
4223 if (p == NULL) {
4224 p = end;
4225 }
4226
4227 if (p > s.data) {
4228 s.len = p - s.data;
4229
4230 lsopt.tcp_keepidle = ngx_parse_time(&s, 1);
4231 if (lsopt.tcp_keepidle == (time_t) NGX_ERROR) {
4232 goto invalid_so_keepalive;
4233 }
4234 }
4235
4236 s.data = (p < end) ? (p + 1) : end;
4237
4238 p = ngx_strlchr(s.data, end, ':');
4239 if (p == NULL) {
4240 p = end;
4241 }
4242
4243 if (p > s.data) {
4244 s.len = p - s.data;
4245
4246 lsopt.tcp_keepintvl = ngx_parse_time(&s, 1);
4247 if (lsopt.tcp_keepintvl == (time_t) NGX_ERROR) {
4248 goto invalid_so_keepalive;
4249 }
4250 }
4251
4252 s.data = (p < end) ? (p + 1) : end;
4253
4254 if (s.data < end) {
4255 s.len = end - s.data;
4256
4257 lsopt.tcp_keepcnt = ngx_atoi(s.data, s.len);
4258 if (lsopt.tcp_keepcnt == NGX_ERROR) {
4259 goto invalid_so_keepalive;
4260 }
4261 }
4262
4263 if (lsopt.tcp_keepidle == 0 && lsopt.tcp_keepintvl == 0
4264 && lsopt.tcp_keepcnt == 0)
4265 {
4266 goto invalid_so_keepalive;
4267 }
4268
4269 lsopt.so_keepalive = 1;
4270
4271#else
4272
4273 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4274 "the \"so_keepalive\" parameter accepts "
4275 "only \"on\" or \"off\" on this platform");
4276 return NGX_CONF_ERROR;
4277
4278#endif
4279 }
4280
4281 lsopt.set = 1;
4282 lsopt.bind = 1;
4283
4284 continue;
4285
4286#if (NGX_HAVE_KEEPALIVE_TUNABLE)
4287 invalid_so_keepalive:
4288
4289 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4290 "invalid so_keepalive value: \"%s\"",
4291 &value[n].data[13]);
4292 return NGX_CONF_ERROR;
4293#endif
4294 }
4295
4296 if (ngx_strcmp(value[n].data, "proxy_protocol") == 0) {
4297 lsopt.proxy_protocol = 1;
4298 continue;
4299 }
4300
4301 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4302 "invalid parameter \"%V\"", &value[n]);
4303 return NGX_CONF_ERROR;
4304 }
4305
4306 if (ngx_http_add_listen(cf, cscf, &lsopt) == NGX_OK) {
4307 return NGX_CONF_OK;
4308 }
4309
4310 return NGX_CONF_ERROR;
4311}
4312
4313
4314static char *
4315ngx_http_core_server_name(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
4316{
4317 ngx_http_core_srv_conf_t *cscf = conf;
4318
4319 u_char ch;
4320 ngx_str_t *value;
4321 ngx_uint_t i;
4322 ngx_http_server_name_t *sn;
4323
4324 value = cf->args->elts;
4325
4326 for (i = 1; i < cf->args->nelts; i++) {
4327
4328 ch = value[i].data[0];
4329
4330 if ((ch == '*' && (value[i].len < 3 || value[i].data[1] != '.'))
4331 || (ch == '.' && value[i].len < 2))
4332 {
4333 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4334 "server name \"%V\" is invalid", &value[i]);
4335 return NGX_CONF_ERROR;
4336 }
4337
4338 if (ngx_strchr(value[i].data, '/')) {
4339 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
4340 "server name \"%V\" has suspicious symbols",
4341 &value[i]);
4342 }
4343
4344 sn = ngx_array_push(&cscf->server_names);
4345 if (sn == NULL) {
4346 return NGX_CONF_ERROR;
4347 }
4348
4349#if (NGX_PCRE)
4350 sn->regex = NULL;
4351#endif
4352 sn->server = cscf;
4353
4354 if (ngx_strcasecmp(value[i].data, (u_char *) "$hostname") == 0) {
4355 sn->name = cf->cycle->hostname;
4356
4357 } else {
4358 sn->name = value[i];
4359 }
4360
4361 if (value[i].data[0] != '~') {
4362 ngx_strlow(sn->name.data, sn->name.data, sn->name.len);
4363 continue;
4364 }
4365
4366#if (NGX_PCRE)
4367 {
4368 u_char *p;
4369 ngx_regex_compile_t rc;
4370 u_char errstr[NGX_MAX_CONF_ERRSTR];
4371
4372 if (value[i].len == 1) {
4373 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4374 "empty regex in server name \"%V\"", &value[i]);
4375 return NGX_CONF_ERROR;
4376 }
4377
4378 value[i].len--;
4379 value[i].data++;
4380
4381 ngx_memzero(&rc, sizeof(ngx_regex_compile_t));
4382
4383 rc.pattern = value[i];
4384 rc.err.len = NGX_MAX_CONF_ERRSTR;
4385 rc.err.data = errstr;
4386
4387 for (p = value[i].data; p < value[i].data + value[i].len; p++) {
4388 if (*p >= 'A' && *p <= 'Z') {
4389 rc.options = NGX_REGEX_CASELESS;
4390 break;
4391 }
4392 }
4393
4394 sn->regex = ngx_http_regex_compile(cf, &rc);
4395 if (sn->regex == NULL) {
4396 return NGX_CONF_ERROR;
4397 }
4398
4399 sn->name = value[i];
4400 cscf->captures = (rc.captures > 0);
4401 }
4402#else
4403 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4404 "using regex \"%V\" "
4405 "requires PCRE library", &value[i]);
4406
4407 return NGX_CONF_ERROR;
4408#endif
4409 }
4410
4411 return NGX_CONF_OK;
4412}
4413
4414
4415static char *
4416ngx_http_core_root(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
4417{
4418 ngx_http_core_loc_conf_t *clcf = conf;
4419
4420 ngx_str_t *value;
4421 ngx_int_t alias;
4422 ngx_uint_t n;
4423 ngx_http_script_compile_t sc;
4424
4425 alias = (cmd->name.len == sizeof("alias") - 1) ? 1 : 0;
4426
4427 if (clcf->root.data) {
4428
4429 if ((clcf->alias != 0) == alias) {
4430 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4431 "\"%V\" directive is duplicate",
4432 &cmd->name);
4433 } else {
4434 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4435 "\"%V\" directive is duplicate, "
4436 "\"%s\" directive was specified earlier",
4437 &cmd->name, clcf->alias ? "alias" : "root");
4438 }
4439
4440 return NGX_CONF_ERROR;
4441 }
4442
4443 if (clcf->named && alias) {
4444 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4445 "the \"alias\" directive cannot be used "
4446 "inside the named location");
4447
4448 return NGX_CONF_ERROR;
4449 }
4450
4451 value = cf->args->elts;
4452
4453 if (ngx_strstr(value[1].data, "$document_root")
4454 || ngx_strstr(value[1].data, "${document_root}"))
4455 {
4456 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4457 "the $document_root variable cannot be used "
4458 "in the \"%V\" directive",
4459 &cmd->name);
4460
4461 return NGX_CONF_ERROR;
4462 }
4463
4464 if (ngx_strstr(value[1].data, "$realpath_root")
4465 || ngx_strstr(value[1].data, "${realpath_root}"))
4466 {
4467 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4468 "the $realpath_root variable cannot be used "
4469 "in the \"%V\" directive",
4470 &cmd->name);
4471
4472 return NGX_CONF_ERROR;
4473 }
4474
4475 clcf->alias = alias ? clcf->name.len : 0;
4476 clcf->root = value[1];
4477
4478 if (!alias && clcf->root.data[clcf->root.len - 1] == '/') {
4479 clcf->root.len--;
4480 }
4481
4482 if (clcf->root.data[0] != '$') {
4483 if (ngx_conf_full_name(cf->cycle, &clcf->root, 0) != NGX_OK) {
4484 return NGX_CONF_ERROR;
4485 }
4486 }
4487
4488 n = ngx_http_script_variables_count(&clcf->root);
4489
4490 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
4491 sc.variables = n;
4492
4493#if (NGX_PCRE)
4494 if (alias && clcf->regex) {
4495 clcf->alias = NGX_MAX_SIZE_T_VALUE;
4496 n = 1;
4497 }
4498#endif
4499
4500 if (n) {
4501 sc.cf = cf;
4502 sc.source = &clcf->root;
4503 sc.lengths = &clcf->root_lengths;
4504 sc.values = &clcf->root_values;
4505 sc.complete_lengths = 1;
4506 sc.complete_values = 1;
4507
4508 if (ngx_http_script_compile(&sc) != NGX_OK) {
4509 return NGX_CONF_ERROR;
4510 }
4511 }
4512
4513 return NGX_CONF_OK;
4514}
4515
4516
4517static ngx_http_method_name_t ngx_methods_names[] = {
4518 { (u_char *) "GET", (uint32_t) ~NGX_HTTP_GET },
4519 { (u_char *) "HEAD", (uint32_t) ~NGX_HTTP_HEAD },
4520 { (u_char *) "POST", (uint32_t) ~NGX_HTTP_POST },
4521 { (u_char *) "PUT", (uint32_t) ~NGX_HTTP_PUT },
4522 { (u_char *) "DELETE", (uint32_t) ~NGX_HTTP_DELETE },
4523 { (u_char *) "MKCOL", (uint32_t) ~NGX_HTTP_MKCOL },
4524 { (u_char *) "COPY", (uint32_t) ~NGX_HTTP_COPY },
4525 { (u_char *) "MOVE", (uint32_t) ~NGX_HTTP_MOVE },
4526 { (u_char *) "OPTIONS", (uint32_t) ~NGX_HTTP_OPTIONS },
4527 { (u_char *) "PROPFIND", (uint32_t) ~NGX_HTTP_PROPFIND },
4528 { (u_char *) "PROPPATCH", (uint32_t) ~NGX_HTTP_PROPPATCH },
4529 { (u_char *) "LOCK", (uint32_t) ~NGX_HTTP_LOCK },
4530 { (u_char *) "UNLOCK", (uint32_t) ~NGX_HTTP_UNLOCK },
4531 { (u_char *) "PATCH", (uint32_t) ~NGX_HTTP_PATCH },
4532 { NULL, 0 }
4533};
4534
4535
4536static char *
4537ngx_http_core_limit_except(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
4538{
4539 ngx_http_core_loc_conf_t *pclcf = conf;
4540
4541 char *rv;
4542 void *mconf;
4543 ngx_str_t *value;
4544 ngx_uint_t i;
4545 ngx_conf_t save;
4546 ngx_http_module_t *module;
4547 ngx_http_conf_ctx_t *ctx, *pctx;
4548 ngx_http_method_name_t *name;
4549 ngx_http_core_loc_conf_t *clcf;
4550
4551 if (pclcf->limit_except) {
4552 return "duplicate";
4553 }
4554
4555 pclcf->limit_except = 0xffffffff;
4556
4557 value = cf->args->elts;
4558
4559 for (i = 1; i < cf->args->nelts; i++) {
4560 for (name = ngx_methods_names; name->name; name++) {
4561
4562 if (ngx_strcasecmp(value[i].data, name->name) == 0) {
4563 pclcf->limit_except &= name->method;
4564 goto next;
4565 }
4566 }
4567
4568 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4569 "invalid method \"%V\"", &value[i]);
4570 return NGX_CONF_ERROR;
4571
4572 next:
4573 continue;
4574 }
4575
4576 if (!(pclcf->limit_except & NGX_HTTP_GET)) {
4577 pclcf->limit_except &= (uint32_t) ~NGX_HTTP_HEAD;
4578 }
4579
4580 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
4581 if (ctx == NULL) {
4582 return NGX_CONF_ERROR;
4583 }
4584
4585 pctx = cf->ctx;
4586 ctx->main_conf = pctx->main_conf;
4587 ctx->srv_conf = pctx->srv_conf;
4588
4589 ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
4590 if (ctx->loc_conf == NULL) {
4591 return NGX_CONF_ERROR;
4592 }
4593
4594 for (i = 0; ngx_modules[i]; i++) {
4595 if (ngx_modules[i]->type != NGX_HTTP_MODULE) {
4596 continue;
4597 }
4598
4599 module = ngx_modules[i]->ctx;
4600
4601 if (module->create_loc_conf) {
4602
4603 mconf = module->create_loc_conf(cf);
4604 if (mconf == NULL) {
4605 return NGX_CONF_ERROR;
4606 }
4607
4608 ctx->loc_conf[ngx_modules[i]->ctx_index] = mconf;
4609 }
4610 }
4611
4612
4613 clcf = ctx->loc_conf[ngx_http_core_module.ctx_index];
4614 pclcf->limit_except_loc_conf = ctx->loc_conf;
4615 clcf->loc_conf = ctx->loc_conf;
4616 clcf->name = pclcf->name;
4617 clcf->noname = 1;
4618 clcf->lmt_excpt = 1;
4619
4620 if (ngx_http_add_location(cf, &pclcf->locations, clcf) != NGX_OK) {
4621 return NGX_CONF_ERROR;
4622 }
4623
4624 save = *cf;
4625 cf->ctx = ctx;
4626 cf->cmd_type = NGX_HTTP_LMT_CONF;
4627
4628 rv = ngx_conf_parse(cf, NULL);
4629
4630 *cf = save;
4631
4632 return rv;
4633}
4634
4635
4636static char *
4637ngx_http_core_set_aio(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
4638{
4639 ngx_http_core_loc_conf_t *clcf = conf;
4640
4641 ngx_str_t *value;
4642
4643 if (clcf->aio != NGX_CONF_UNSET) {
4644 return "is duplicate";
4645 }
4646
4647 value = cf->args->elts;
4648
4649 if (ngx_strcmp(value[1].data, "off") == 0) {
4650 clcf->aio = NGX_HTTP_AIO_OFF;
4651 return NGX_CONF_OK;
4652 }
4653
4654 if (ngx_strcmp(value[1].data, "on") == 0) {
4655#if (NGX_HAVE_FILE_AIO)
4656 clcf->aio = NGX_HTTP_AIO_ON;
4657 return NGX_CONF_OK;
4658#else
4659 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4660 "\"aio on\" "
4661 "is unsupported on this platform");
4662 return NGX_CONF_ERROR;
4663#endif
4664 }
4665
4666#if (NGX_HAVE_AIO_SENDFILE)
4667
4668 if (ngx_strcmp(value[1].data, "sendfile") == 0) {
4669 clcf->aio = NGX_HTTP_AIO_ON;
4670
4671 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
4672 "the \"sendfile\" parameter of "
4673 "the \"aio\" directive is deprecated");
4674 return NGX_CONF_OK;
4675 }
4676
4677#endif
4678
4679 return "invalid value";
4680}
4681
4682
4683static char *
4684ngx_http_core_directio(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
4685{
4686 ngx_http_core_loc_conf_t *clcf = conf;
4687
4688 ngx_str_t *value;
4689
4690 if (clcf->directio != NGX_CONF_UNSET) {
4691 return "is duplicate";
4692 }
4693
4694 value = cf->args->elts;
4695
4696 if (ngx_strcmp(value[1].data, "off") == 0) {
4697 clcf->directio = NGX_OPEN_FILE_DIRECTIO_OFF;
4698 return NGX_CONF_OK;
4699 }
4700
4701 clcf->directio = ngx_parse_offset(&value[1]);
4702 if (clcf->directio == (off_t) NGX_ERROR) {
4703 return "invalid value";
4704 }
4705
4706 return NGX_CONF_OK;
4707}
4708
4709
4710static char *
4711ngx_http_core_error_page(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
4712{
4713 ngx_http_core_loc_conf_t *clcf = conf;
4714
4715 u_char *p;
4716 ngx_int_t overwrite;
4717 ngx_str_t *value, uri, args;
4718 ngx_uint_t i, n;
4719 ngx_http_err_page_t *err;
4720 ngx_http_complex_value_t cv;
4721 ngx_http_compile_complex_value_t ccv;
4722
4723 if (clcf->error_pages == NULL) {
4724 clcf->error_pages = ngx_array_create(cf->pool, 4,
4725 sizeof(ngx_http_err_page_t));
4726 if (clcf->error_pages == NULL) {
4727 return NGX_CONF_ERROR;
4728 }
4729 }
4730
4731 value = cf->args->elts;
4732
4733 i = cf->args->nelts - 2;
4734
4735 if (value[i].data[0] == '=') {
4736 if (i == 1) {
4737 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4738 "invalid value \"%V\"", &value[i]);
4739 return NGX_CONF_ERROR;
4740 }
4741
4742 if (value[i].len > 1) {
4743 overwrite = ngx_atoi(&value[i].data[1], value[i].len - 1);
4744
4745 if (overwrite == NGX_ERROR) {
4746 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4747 "invalid value \"%V\"", &value[i]);
4748 return NGX_CONF_ERROR;
4749 }
4750
4751 } else {
4752 overwrite = 0;
4753 }
4754
4755 n = 2;
4756
4757 } else {
4758 overwrite = -1;
4759 n = 1;
4760 }
4761
4762 uri = value[cf->args->nelts - 1];
4763
4764 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
4765
4766 ccv.cf = cf;
4767 ccv.value = &uri;
4768 ccv.complex_value = &cv;
4769
4770 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
4771 return NGX_CONF_ERROR;
4772 }
4773
4774 ngx_str_null(&args);
4775
4776 if (cv.lengths == NULL && uri.len && uri.data[0] == '/') {
4777 p = (u_char *) ngx_strchr(uri.data, '?');
4778
4779 if (p) {
4780 cv.value.len = p - uri.data;
4781 cv.value.data = uri.data;
4782 p++;
4783 args.len = (uri.data + uri.len) - p;
4784 args.data = p;
4785 }
4786 }
4787
4788 for (i = 1; i < cf->args->nelts - n; i++) {
4789 err = ngx_array_push(clcf->error_pages);
4790 if (err == NULL) {
4791 return NGX_CONF_ERROR;
4792 }
4793
4794 err->status = ngx_atoi(value[i].data, value[i].len);
4795
4796 if (err->status == NGX_ERROR || err->status == 499) {
4797 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4798 "invalid value \"%V\"", &value[i]);
4799 return NGX_CONF_ERROR;
4800 }
4801
4802 if (err->status < 300 || err->status > 599) {
4803 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4804 "value \"%V\" must be between 300 and 599",
4805 &value[i]);
4806 return NGX_CONF_ERROR;
4807 }
4808
4809 err->overwrite = overwrite;
4810
4811 if (overwrite == -1) {
4812 switch (err->status) {
4813 case NGX_HTTP_TO_HTTPS:
4814 case NGX_HTTPS_CERT_ERROR:
4815 case NGX_HTTPS_NO_CERT:
4816 err->overwrite = NGX_HTTP_BAD_REQUEST;
4817 default:
4818 break;
4819 }
4820 }
4821
4822 err->value = cv;
4823 err->args = args;
4824 }
4825
4826 return NGX_CONF_OK;
4827}
4828
4829
4830static char *
4831ngx_http_core_try_files(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
4832{
4833 ngx_http_core_loc_conf_t *clcf = conf;
4834
4835 ngx_str_t *value;
4836 ngx_int_t code;
4837 ngx_uint_t i, n;
4838 ngx_http_try_file_t *tf;
4839 ngx_http_script_compile_t sc;
4840 ngx_http_core_main_conf_t *cmcf;
4841
4842 if (clcf->try_files) {
4843 return "is duplicate";
4844 }
4845
4846 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
4847
4848 cmcf->try_files = 1;
4849
4850 tf = ngx_pcalloc(cf->pool, cf->args->nelts * sizeof(ngx_http_try_file_t));
4851 if (tf == NULL) {
4852 return NGX_CONF_ERROR;
4853 }
4854
4855 clcf->try_files = tf;
4856
4857 value = cf->args->elts;
4858
4859 for (i = 0; i < cf->args->nelts - 1; i++) {
4860
4861 tf[i].name = value[i + 1];
4862
4863 if (tf[i].name.len > 0
4864 && tf[i].name.data[tf[i].name.len - 1] == '/'
4865 && i + 2 < cf->args->nelts)
4866 {
4867 tf[i].test_dir = 1;
4868 tf[i].name.len--;
4869 tf[i].name.data[tf[i].name.len] = '\0';
4870 }
4871
4872 n = ngx_http_script_variables_count(&tf[i].name);
4873
4874 if (n) {
4875 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
4876
4877 sc.cf = cf;
4878 sc.source = &tf[i].name;
4879 sc.lengths = &tf[i].lengths;
4880 sc.values = &tf[i].values;
4881 sc.variables = n;
4882 sc.complete_lengths = 1;
4883 sc.complete_values = 1;
4884
4885 if (ngx_http_script_compile(&sc) != NGX_OK) {
4886 return NGX_CONF_ERROR;
4887 }
4888
4889 } else {
4890 /* add trailing '\0' to length */
4891 tf[i].name.len++;
4892 }
4893 }
4894
4895 if (tf[i - 1].name.data[0] == '=') {
4896
4897 code = ngx_atoi(tf[i - 1].name.data + 1, tf[i - 1].name.len - 2);
4898
4899 if (code == NGX_ERROR || code > 999) {
4900 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4901 "invalid code \"%*s\"",
4902 tf[i - 1].name.len - 1, tf[i - 1].name.data);
4903 return NGX_CONF_ERROR;
4904 }
4905
4906 tf[i].code = code;
4907 }
4908
4909 return NGX_CONF_OK;
4910}
4911
4912
4913static char *
4914ngx_http_core_open_file_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
4915{
4916 ngx_http_core_loc_conf_t *clcf = conf;
4917
4918 time_t inactive;
4919 ngx_str_t *value, s;
4920 ngx_int_t max;
4921 ngx_uint_t i;
4922
4923 if (clcf->open_file_cache != NGX_CONF_UNSET_PTR) {
4924 return "is duplicate";
4925 }
4926
4927 value = cf->args->elts;
4928
4929 max = 0;
4930 inactive = 60;
4931
4932 for (i = 1; i < cf->args->nelts; i++) {
4933
4934 if (ngx_strncmp(value[i].data, "max=", 4) == 0) {
4935
4936 max = ngx_atoi(value[i].data + 4, value[i].len - 4);
4937 if (max <= 0) {
4938 goto failed;
4939 }
4940
4941 continue;
4942 }
4943
4944 if (ngx_strncmp(value[i].data, "inactive=", 9) == 0) {
4945
4946 s.len = value[i].len - 9;
4947 s.data = value[i].data + 9;
4948
4949 inactive = ngx_parse_time(&s, 1);
4950 if (inactive == (time_t) NGX_ERROR) {
4951 goto failed;
4952 }
4953
4954 continue;
4955 }
4956
4957 if (ngx_strcmp(value[i].data, "off") == 0) {
4958
4959 clcf->open_file_cache = NULL;
4960
4961 continue;
4962 }
4963
4964 failed:
4965
4966 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4967 "invalid \"open_file_cache\" parameter \"%V\"",
4968 &value[i]);
4969 return NGX_CONF_ERROR;
4970 }
4971
4972 if (clcf->open_file_cache == NULL) {
4973 return NGX_CONF_OK;
4974 }
4975
4976 if (max == 0) {
4977 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
4978 "\"open_file_cache\" must have the \"max\" parameter");
4979 return NGX_CONF_ERROR;
4980 }
4981
4982 clcf->open_file_cache = ngx_open_file_cache_init(cf->pool, max, inactive);
4983 if (clcf->open_file_cache) {
4984 return NGX_CONF_OK;
4985 }
4986
4987 return NGX_CONF_ERROR;
4988}
4989
4990
4991static char *
4992ngx_http_core_error_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
4993{
4994 ngx_http_core_loc_conf_t *clcf = conf;
4995
4996 return ngx_log_set_log(cf, &clcf->error_log);
4997}
4998
4999
5000static char *
5001ngx_http_core_keepalive(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
5002{
5003 ngx_http_core_loc_conf_t *clcf = conf;
5004
5005 ngx_str_t *value;
5006
5007 if (clcf->keepalive_timeout != NGX_CONF_UNSET_MSEC) {
5008 return "is duplicate";
5009 }
5010
5011 value = cf->args->elts;
5012
5013 clcf->keepalive_timeout = ngx_parse_time(&value[1], 0);
5014
5015 if (clcf->keepalive_timeout == (ngx_msec_t) NGX_ERROR) {
5016 return "invalid value";
5017 }
5018
5019 if (cf->args->nelts == 2) {
5020 return NGX_CONF_OK;
5021 }
5022
5023 clcf->keepalive_header = ngx_parse_time(&value[2], 1);
5024
5025 if (clcf->keepalive_header == (time_t) NGX_ERROR) {
5026 return "invalid value";
5027 }
5028
5029 return NGX_CONF_OK;
5030}
5031
5032
5033static char *
5034ngx_http_core_internal(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
5035{
5036 ngx_http_core_loc_conf_t *clcf = conf;
5037
5038 if (clcf->internal != NGX_CONF_UNSET) {
5039 return "is duplicate";
5040 }
5041
5042 clcf->internal = 1;
5043
5044 return NGX_CONF_OK;
5045}
5046
5047
5048static char *
5049ngx_http_core_resolver(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
5050{
5051 ngx_http_core_loc_conf_t *clcf = conf;
5052
5053 ngx_str_t *value;
5054
5055 if (clcf->resolver) {
5056 return "is duplicate";
5057 }
5058
5059 value = cf->args->elts;
5060
5061 clcf->resolver = ngx_resolver_create(cf, &value[1], cf->args->nelts - 1);
5062 if (clcf->resolver == NULL) {
5063 return NGX_CONF_ERROR;
5064 }
5065
5066 return NGX_CONF_OK;
5067}
5068
5069
5070#if (NGX_HTTP_GZIP)
5071
5072static char *
5073ngx_http_gzip_disable(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
5074{
5075 ngx_http_core_loc_conf_t *clcf = conf;
5076
5077#if (NGX_PCRE)
5078
5079 ngx_str_t *value;
5080 ngx_uint_t i;
5081 ngx_regex_elt_t *re;
5082 ngx_regex_compile_t rc;
5083 u_char errstr[NGX_MAX_CONF_ERRSTR];
5084
5085 if (clcf->gzip_disable == NGX_CONF_UNSET_PTR) {
5086 clcf->gzip_disable = ngx_array_create(cf->pool, 2,
5087 sizeof(ngx_regex_elt_t));
5088 if (clcf->gzip_disable == NULL) {
5089 return NGX_CONF_ERROR;
5090 }
5091 }
5092
5093 value = cf->args->elts;
5094
5095 ngx_memzero(&rc, sizeof(ngx_regex_compile_t));
5096
5097 rc.pool = cf->pool;
5098 rc.err.len = NGX_MAX_CONF_ERRSTR;
5099 rc.err.data = errstr;
5100
5101 for (i = 1; i < cf->args->nelts; i++) {
5102
5103 if (ngx_strcmp(value[i].data, "msie6") == 0) {
5104 clcf->gzip_disable_msie6 = 1;
5105 continue;
5106 }
5107
5108#if (NGX_HTTP_DEGRADATION)
5109
5110 if (ngx_strcmp(value[i].data, "degradation") == 0) {
5111 clcf->gzip_disable_degradation = 1;
5112 continue;
5113 }
5114
5115#endif
5116
5117 re = ngx_array_push(clcf->gzip_disable);
5118 if (re == NULL) {
5119 return NGX_CONF_ERROR;
5120 }
5121
5122 rc.pattern = value[i];
5123 rc.options = NGX_REGEX_CASELESS;
5124
5125 if (ngx_regex_compile(&rc) != NGX_OK) {
5126 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "%V", &rc.err);
5127 return NGX_CONF_ERROR;
5128 }
5129
5130 re->regex = rc.regex;
5131 re->name = value[i].data;
5132 }
5133
5134 return NGX_CONF_OK;
5135
5136#else
5137 ngx_str_t *value;
5138 ngx_uint_t i;
5139
5140 value = cf->args->elts;
5141
5142 for (i = 1; i < cf->args->nelts; i++) {
5143 if (ngx_strcmp(value[i].data, "msie6") == 0) {
5144 clcf->gzip_disable_msie6 = 1;
5145 continue;
5146 }
5147
5148#if (NGX_HTTP_DEGRADATION)
5149
5150 if (ngx_strcmp(value[i].data, "degradation") == 0) {
5151 clcf->gzip_disable_degradation = 1;
5152 continue;
5153 }
5154
5155#endif
5156
5157 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
5158 "without PCRE library \"gzip_disable\" supports "
5159 "builtin \"msie6\" and \"degradation\" mask only");
5160
5161 return NGX_CONF_ERROR;
5162 }
5163
5164 return NGX_CONF_OK;
5165
5166#endif
5167}
5168
5169#endif
5170
5171
5172#if (NGX_HAVE_OPENAT)
5173
5174static char *
5175ngx_http_disable_symlinks(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
5176{
5177 ngx_http_core_loc_conf_t *clcf = conf;
5178
5179 ngx_str_t *value;
5180 ngx_uint_t i;
5181 ngx_http_compile_complex_value_t ccv;
5182
5183 if (clcf->disable_symlinks != NGX_CONF_UNSET_UINT) {
5184 return "is duplicate";
5185 }
5186
5187 value = cf->args->elts;
5188
5189 for (i = 1; i < cf->args->nelts; i++) {
5190
5191 if (ngx_strcmp(value[i].data, "off") == 0)