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