blob: 62e172b62f07d4f2792207e237b482e77f3ba38f [file] [log] [blame]
Igor Sysoev4e9393a2003-01-09 05:36:00 +00001
Igor Sysoevd90282d2004-09-28 08:34:51 +00002/*
Igor Sysoevff8da912004-09-29 16:00:49 +00003 * Copyright (C) Igor Sysoev
Igor Sysoevd90282d2004-09-28 08:34:51 +00004 */
5
6
Igor Sysoev4e9393a2003-01-09 05:36:00 +00007#include <ngx_config.h>
Igor Sysoev4e9393a2003-01-09 05:36:00 +00008#include <ngx_core.h>
Igor Sysoevb3e73d82003-10-10 15:10:50 +00009#include <ngx_event.h>
Igor Sysoev6253ca12003-05-27 12:18:54 +000010#include <ngx_http.h>
Igor Sysoeva9830112003-05-19 16:39:14 +000011#include <nginx.h>
Igor Sysoev4e9393a2003-01-09 05:36:00 +000012
Igor Sysoev1b735832004-11-11 14:07:14 +000013
Igor Sysoev94e32ce2006-04-07 14:08:04 +000014typedef struct {
15 char *name;
16 uint32_t method;
17} ngx_http_method_name_t;
18
19
Igor Sysoev74a5ddb2004-07-18 19:11:20 +000020#define NGX_HTTP_LOCATION_EXACT 1
21#define NGX_HTTP_LOCATION_AUTO_REDIRECT 2
Igor Sysoevd43bee82004-11-20 19:52:20 +000022#define NGX_HTTP_LOCATION_NOREGEX 3
23#define NGX_HTTP_LOCATION_REGEX 4
Igor Sysoev74a5ddb2004-07-18 19:11:20 +000024
Igor Sysoev6253ca12003-05-27 12:18:54 +000025
Igor Sysoev805d9db2005-02-03 19:33:37 +000026static void ngx_http_core_run_phases(ngx_http_request_t *r);
27static ngx_int_t ngx_http_core_find_location(ngx_http_request_t *r,
Igor Sysoevaa828612005-02-09 14:31:07 +000028 ngx_array_t *locations, size_t len);
Igor Sysoev4e9393a2003-01-09 05:36:00 +000029
Igor Sysoev899b44e2005-05-12 14:58:06 +000030static ngx_int_t ngx_http_core_preconfiguration(ngx_conf_t *cf);
Igor Sysoev890fc962003-07-20 21:15:59 +000031static void *ngx_http_core_create_main_conf(ngx_conf_t *cf);
32static char *ngx_http_core_init_main_conf(ngx_conf_t *cf, void *conf);
33static void *ngx_http_core_create_srv_conf(ngx_conf_t *cf);
34static char *ngx_http_core_merge_srv_conf(ngx_conf_t *cf,
Igor Sysoevaa828612005-02-09 14:31:07 +000035 void *parent, void *child);
Igor Sysoev890fc962003-07-20 21:15:59 +000036static void *ngx_http_core_create_loc_conf(ngx_conf_t *cf);
37static char *ngx_http_core_merge_loc_conf(ngx_conf_t *cf,
Igor Sysoevaa828612005-02-09 14:31:07 +000038 void *parent, void *child);
Igor Sysoevdc479b42003-03-20 16:09:44 +000039
Igor Sysoev805d9db2005-02-03 19:33:37 +000040static char *ngx_http_core_server(ngx_conf_t *cf, ngx_command_t *cmd,
Igor Sysoevaa828612005-02-09 14:31:07 +000041 void *dummy);
Igor Sysoev805d9db2005-02-03 19:33:37 +000042static char *ngx_http_core_location(ngx_conf_t *cf, ngx_command_t *cmd,
Igor Sysoevaa828612005-02-09 14:31:07 +000043 void *dummy);
Igor Sysoev4d656dc2005-03-22 16:02:46 +000044static int ngx_libc_cdecl ngx_http_core_cmp_locations(const void *first,
45 const void *second);
Igor Sysoev805d9db2005-02-03 19:33:37 +000046
47static char *ngx_http_core_types(ngx_conf_t *cf, ngx_command_t *cmd,
Igor Sysoevaa828612005-02-09 14:31:07 +000048 void *conf);
Igor Sysoev805d9db2005-02-03 19:33:37 +000049static char *ngx_http_core_type(ngx_conf_t *cf, ngx_command_t *dummy,
Igor Sysoevaa828612005-02-09 14:31:07 +000050 void *conf);
Igor Sysoev4e9393a2003-01-09 05:36:00 +000051
Igor Sysoev805d9db2005-02-03 19:33:37 +000052static char *ngx_http_core_listen(ngx_conf_t *cf, ngx_command_t *cmd,
Igor Sysoevaa828612005-02-09 14:31:07 +000053 void *conf);
Igor Sysoev805d9db2005-02-03 19:33:37 +000054static char *ngx_http_core_server_name(ngx_conf_t *cf, ngx_command_t *cmd,
Igor Sysoevaa828612005-02-09 14:31:07 +000055 void *conf);
Igor Sysoev805d9db2005-02-03 19:33:37 +000056static char *ngx_http_core_root(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
Igor Sysoev94e32ce2006-04-07 14:08:04 +000057static char *ngx_http_core_limit_except(ngx_conf_t *cf, ngx_command_t *cmd,
58 void *conf);
Igor Sysoev805d9db2005-02-03 19:33:37 +000059static char *ngx_http_core_error_page(ngx_conf_t *cf, ngx_command_t *cmd,
Igor Sysoevaa828612005-02-09 14:31:07 +000060 void *conf);
Igor Sysoev805d9db2005-02-03 19:33:37 +000061static char *ngx_http_core_error_log(ngx_conf_t *cf, ngx_command_t *cmd,
Igor Sysoevaa828612005-02-09 14:31:07 +000062 void *conf);
Igor Sysoev805d9db2005-02-03 19:33:37 +000063static char *ngx_http_core_keepalive(ngx_conf_t *cf, ngx_command_t *cmd,
Igor Sysoevaa828612005-02-09 14:31:07 +000064 void *conf);
Igor Sysoev899b44e2005-05-12 14:58:06 +000065static char *ngx_http_core_internal(ngx_conf_t *cf, ngx_command_t *cmd,
66 void *conf);
Igor Sysoevb5faed22003-10-29 08:30:44 +000067
Igor Sysoev805d9db2005-02-03 19:33:37 +000068static char *ngx_http_core_lowat_check(ngx_conf_t *cf, void *post, void *data);
69
70static ngx_conf_post_t ngx_http_core_lowat_post =
71 { ngx_http_core_lowat_check };
Igor Sysoevb5faed22003-10-29 08:30:44 +000072
Igor Sysoev8290d282006-02-03 12:58:48 +000073static ngx_conf_deprecated_t ngx_conf_deprecated_optimize_host_names = {
74 ngx_conf_deprecated, "optimize_host_names", "optimize_server_names"
75};
76
Igor Sysoev4e9393a2003-01-09 05:36:00 +000077
78static ngx_command_t ngx_http_core_commands[] = {
79
Igor Sysoevffe71442006-02-08 15:33:12 +000080 { ngx_string("variables_hash_max_size"),
81 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
82 ngx_conf_set_num_slot,
83 NGX_HTTP_MAIN_CONF_OFFSET,
84 offsetof(ngx_http_core_main_conf_t, variables_hash_max_size),
85 NULL },
86
87 { ngx_string("variables_hash_bucket_size"),
88 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
89 ngx_conf_set_num_slot,
90 NGX_HTTP_MAIN_CONF_OFFSET,
91 offsetof(ngx_http_core_main_conf_t, variables_hash_bucket_size),
92 NULL },
93
Igor Sysoev305a9d82005-12-26 17:07:48 +000094 { ngx_string("server_names_hash_max_size"),
Igor Sysoevb1dfe472004-12-21 12:30:30 +000095 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
96 ngx_conf_set_num_slot,
97 NGX_HTTP_MAIN_CONF_OFFSET,
Igor Sysoev305a9d82005-12-26 17:07:48 +000098 offsetof(ngx_http_core_main_conf_t, server_names_hash_max_size),
Igor Sysoevb1dfe472004-12-21 12:30:30 +000099 NULL },
100
Igor Sysoev305a9d82005-12-26 17:07:48 +0000101 { ngx_string("server_names_hash_bucket_size"),
Igor Sysoevb1dfe472004-12-21 12:30:30 +0000102 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
103 ngx_conf_set_num_slot,
104 NGX_HTTP_MAIN_CONF_OFFSET,
Igor Sysoev305a9d82005-12-26 17:07:48 +0000105 offsetof(ngx_http_core_main_conf_t, server_names_hash_bucket_size),
Igor Sysoevb1dfe472004-12-21 12:30:30 +0000106 NULL },
107
Igor Sysoev865c1502003-11-30 20:03:18 +0000108 { ngx_string("server"),
109 NGX_HTTP_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
Igor Sysoev805d9db2005-02-03 19:33:37 +0000110 ngx_http_core_server,
Igor Sysoev865c1502003-11-30 20:03:18 +0000111 0,
112 0,
113 NULL },
Igor Sysoev4e9393a2003-01-09 05:36:00 +0000114
Igor Sysoev865c1502003-11-30 20:03:18 +0000115 { ngx_string("connection_pool_size"),
116 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
117 ngx_conf_set_size_slot,
118 NGX_HTTP_SRV_CONF_OFFSET,
119 offsetof(ngx_http_core_srv_conf_t, connection_pool_size),
120 NULL },
Igor Sysoev6a644c62003-03-04 06:33:48 +0000121
Igor Sysoev865c1502003-11-30 20:03:18 +0000122 { ngx_string("request_pool_size"),
123 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
124 ngx_conf_set_size_slot,
125 NGX_HTTP_SRV_CONF_OFFSET,
126 offsetof(ngx_http_core_srv_conf_t, request_pool_size),
127 NULL },
Igor Sysoev187fcd82003-05-23 11:53:01 +0000128
Igor Sysoev865c1502003-11-30 20:03:18 +0000129 { ngx_string("client_header_timeout"),
130 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
131 ngx_conf_set_msec_slot,
132 NGX_HTTP_SRV_CONF_OFFSET,
133 offsetof(ngx_http_core_srv_conf_t, client_header_timeout),
134 NULL },
Igor Sysoev6a644c62003-03-04 06:33:48 +0000135
Igor Sysoev865c1502003-11-30 20:03:18 +0000136 { ngx_string("client_header_buffer_size"),
137 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
138 ngx_conf_set_size_slot,
139 NGX_HTTP_SRV_CONF_OFFSET,
140 offsetof(ngx_http_core_srv_conf_t, client_header_buffer_size),
141 NULL },
Igor Sysoev6a644c62003-03-04 06:33:48 +0000142
Igor Sysoevf7abd722004-09-23 06:32:00 +0000143 { ngx_string("large_client_header_buffers"),
Igor Sysoev85080d02004-09-22 16:18:21 +0000144 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE2,
145 ngx_conf_set_bufs_slot,
146 NGX_HTTP_SRV_CONF_OFFSET,
Igor Sysoevf7abd722004-09-23 06:32:00 +0000147 offsetof(ngx_http_core_srv_conf_t, large_client_header_buffers),
Igor Sysoev865c1502003-11-30 20:03:18 +0000148 NULL },
Igor Sysoev6a644c62003-03-04 06:33:48 +0000149
Igor Sysoev8290d282006-02-03 12:58:48 +0000150 { ngx_string("optimize_server_names"),
151 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_FLAG,
152 ngx_conf_set_flag_slot,
153 NGX_HTTP_SRV_CONF_OFFSET,
154 offsetof(ngx_http_core_srv_conf_t, optimize_server_names),
155 NULL },
156
Igor Sysoev34303462006-01-24 16:08:27 +0000157 { ngx_string("optimize_host_names"),
158 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_FLAG,
159 ngx_conf_set_flag_slot,
160 NGX_HTTP_SRV_CONF_OFFSET,
Igor Sysoev8290d282006-02-03 12:58:48 +0000161 offsetof(ngx_http_core_srv_conf_t, optimize_server_names),
162 &ngx_conf_deprecated_optimize_host_names },
Igor Sysoev34303462006-01-24 16:08:27 +0000163
Igor Sysoev3362b8d2005-05-14 18:42:03 +0000164 { ngx_string("ignore_invalid_headers"),
165 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_FLAG,
166 ngx_conf_set_flag_slot,
167 NGX_HTTP_SRV_CONF_OFFSET,
168 offsetof(ngx_http_core_srv_conf_t, ignore_invalid_headers),
169 NULL },
170
Igor Sysoev865c1502003-11-30 20:03:18 +0000171 { ngx_string("location"),
Igor Sysoev74a5ddb2004-07-18 19:11:20 +0000172 NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE12,
Igor Sysoev805d9db2005-02-03 19:33:37 +0000173 ngx_http_core_location,
Igor Sysoev865c1502003-11-30 20:03:18 +0000174 NGX_HTTP_SRV_CONF_OFFSET,
175 0,
176 NULL },
Igor Sysoev4e9393a2003-01-09 05:36:00 +0000177
Igor Sysoev865c1502003-11-30 20:03:18 +0000178 { ngx_string("listen"),
Igor Sysoevb145b062005-06-15 18:33:41 +0000179 NGX_HTTP_SRV_CONF|NGX_CONF_1MORE,
Igor Sysoev805d9db2005-02-03 19:33:37 +0000180 ngx_http_core_listen,
Igor Sysoev865c1502003-11-30 20:03:18 +0000181 NGX_HTTP_SRV_CONF_OFFSET,
182 0,
183 NULL },
Igor Sysoeva19a85e2003-01-28 15:56:37 +0000184
Igor Sysoev865c1502003-11-30 20:03:18 +0000185 { ngx_string("server_name"),
186 NGX_HTTP_SRV_CONF|NGX_CONF_1MORE,
Igor Sysoev805d9db2005-02-03 19:33:37 +0000187 ngx_http_core_server_name,
Igor Sysoev865c1502003-11-30 20:03:18 +0000188 NGX_HTTP_SRV_CONF_OFFSET,
189 0,
190 NULL },
Igor Sysoev13933252003-05-29 13:02:09 +0000191
Igor Sysoev24025022005-12-16 15:07:08 +0000192 { ngx_string("types_hash_max_size"),
193 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
194 ngx_conf_set_num_slot,
195 NGX_HTTP_LOC_CONF_OFFSET,
196 offsetof(ngx_http_core_loc_conf_t, types_hash_max_size),
197 NULL },
198
199 { ngx_string("types_hash_bucket_size"),
200 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
201 ngx_conf_set_num_slot,
202 NGX_HTTP_LOC_CONF_OFFSET,
203 offsetof(ngx_http_core_loc_conf_t, types_hash_bucket_size),
204 NULL },
205
Igor Sysoev865c1502003-11-30 20:03:18 +0000206 { ngx_string("types"),
207 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF
208 |NGX_CONF_BLOCK|NGX_CONF_NOARGS,
Igor Sysoev805d9db2005-02-03 19:33:37 +0000209 ngx_http_core_types,
Igor Sysoev865c1502003-11-30 20:03:18 +0000210 NGX_HTTP_LOC_CONF_OFFSET,
211 0,
212 NULL },
Igor Sysoev79a80482003-05-14 17:13:13 +0000213
Igor Sysoev865c1502003-11-30 20:03:18 +0000214 { ngx_string("default_type"),
215 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
216 ngx_conf_set_str_slot,
217 NGX_HTTP_LOC_CONF_OFFSET,
218 offsetof(ngx_http_core_loc_conf_t, default_type),
219 NULL },
Igor Sysoev6253ca12003-05-27 12:18:54 +0000220
Igor Sysoev865c1502003-11-30 20:03:18 +0000221 { ngx_string("root"),
Igor Sysoev899b44e2005-05-12 14:58:06 +0000222 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
223 |NGX_CONF_TAKE1,
Igor Sysoev805d9db2005-02-03 19:33:37 +0000224 ngx_http_core_root,
Igor Sysoev865c1502003-11-30 20:03:18 +0000225 NGX_HTTP_LOC_CONF_OFFSET,
Igor Sysoev10a543a2004-03-16 07:10:12 +0000226 0,
227 NULL },
228
229 { ngx_string("alias"),
230 NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
Igor Sysoev805d9db2005-02-03 19:33:37 +0000231 ngx_http_core_root,
Igor Sysoev10a543a2004-03-16 07:10:12 +0000232 NGX_HTTP_LOC_CONF_OFFSET,
233 0,
Igor Sysoev865c1502003-11-30 20:03:18 +0000234 NULL },
Igor Sysoev4e9393a2003-01-09 05:36:00 +0000235
Igor Sysoev94e32ce2006-04-07 14:08:04 +0000236 { ngx_string("limit_except"),
237 NGX_HTTP_LOC_CONF|NGX_CONF_BLOCK|NGX_CONF_1MORE,
238 ngx_http_core_limit_except,
239 NGX_HTTP_LOC_CONF_OFFSET,
240 0,
241 NULL },
242
Igor Sysoevae02c192004-03-19 05:25:53 +0000243 { ngx_string("client_max_body_size"),
244 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
245 ngx_conf_set_size_slot,
246 NGX_HTTP_LOC_CONF_OFFSET,
247 offsetof(ngx_http_core_loc_conf_t, client_max_body_size),
248 NULL },
249
Igor Sysoevdbb27762004-04-01 16:20:53 +0000250 { ngx_string("client_body_buffer_size"),
251 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
252 ngx_conf_set_size_slot,
253 NGX_HTTP_LOC_CONF_OFFSET,
254 offsetof(ngx_http_core_loc_conf_t, client_body_buffer_size),
255 NULL },
256
Igor Sysoev865c1502003-11-30 20:03:18 +0000257 { ngx_string("client_body_timeout"),
258 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
259 ngx_conf_set_msec_slot,
260 NGX_HTTP_LOC_CONF_OFFSET,
261 offsetof(ngx_http_core_loc_conf_t, client_body_timeout),
262 NULL },
Igor Sysoev2b0c76c2003-10-27 21:01:00 +0000263
Igor Sysoev02025fd2005-01-18 13:03:58 +0000264 { ngx_string("client_body_temp_path"),
265 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1234,
266 ngx_conf_set_path_slot,
267 NGX_HTTP_LOC_CONF_OFFSET,
268 offsetof(ngx_http_core_loc_conf_t, client_body_temp_path),
269 (void *) ngx_garbage_collector_temp_handler },
270
Igor Sysoev865c1502003-11-30 20:03:18 +0000271 { ngx_string("sendfile"),
272 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
273 ngx_conf_set_flag_slot,
274 NGX_HTTP_LOC_CONF_OFFSET,
275 offsetof(ngx_http_core_loc_conf_t, sendfile),
276 NULL },
Igor Sysoev5bf3d252003-10-22 07:05:29 +0000277
Igor Sysoev3c3ca172004-01-05 20:55:48 +0000278 { ngx_string("tcp_nopush"),
279 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
280 ngx_conf_set_flag_slot,
281 NGX_HTTP_LOC_CONF_OFFSET,
282 offsetof(ngx_http_core_loc_conf_t, tcp_nopush),
283 NULL },
284
Igor Sysoev924bd792004-10-11 15:07:03 +0000285 { ngx_string("tcp_nodelay"),
286 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
287 ngx_conf_set_flag_slot,
288 NGX_HTTP_LOC_CONF_OFFSET,
289 offsetof(ngx_http_core_loc_conf_t, tcp_nodelay),
290 NULL },
291
Igor Sysoev865c1502003-11-30 20:03:18 +0000292 { ngx_string("send_timeout"),
293 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
294 ngx_conf_set_msec_slot,
295 NGX_HTTP_LOC_CONF_OFFSET,
296 offsetof(ngx_http_core_loc_conf_t, send_timeout),
297 NULL },
Igor Sysoev4e9393a2003-01-09 05:36:00 +0000298
Igor Sysoev865c1502003-11-30 20:03:18 +0000299 { ngx_string("send_lowat"),
300 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
301 ngx_conf_set_size_slot,
302 NGX_HTTP_LOC_CONF_OFFSET,
303 offsetof(ngx_http_core_loc_conf_t, send_lowat),
Igor Sysoev805d9db2005-02-03 19:33:37 +0000304 &ngx_http_core_lowat_post },
Igor Sysoevb5faed22003-10-29 08:30:44 +0000305
Igor Sysoev7823cc32004-07-14 16:01:42 +0000306 { ngx_string("postpone_output"),
307 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
308 ngx_conf_set_size_slot,
309 NGX_HTTP_LOC_CONF_OFFSET,
310 offsetof(ngx_http_core_loc_conf_t, postpone_output),
311 NULL },
312
313 { ngx_string("limit_rate"),
Igor Sysoev805d9db2005-02-03 19:33:37 +0000314 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
315 |NGX_CONF_TAKE1,
Igor Sysoev7823cc32004-07-14 16:01:42 +0000316 ngx_conf_set_size_slot,
317 NGX_HTTP_LOC_CONF_OFFSET,
318 offsetof(ngx_http_core_loc_conf_t, limit_rate),
319 NULL },
320
Igor Sysoev865c1502003-11-30 20:03:18 +0000321 { ngx_string("keepalive_timeout"),
Igor Sysoev307c3ad2004-09-17 16:07:35 +0000322 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
Igor Sysoev805d9db2005-02-03 19:33:37 +0000323 ngx_http_core_keepalive,
Igor Sysoev865c1502003-11-30 20:03:18 +0000324 NGX_HTTP_LOC_CONF_OFFSET,
Igor Sysoev307c3ad2004-09-17 16:07:35 +0000325 0,
Igor Sysoev865c1502003-11-30 20:03:18 +0000326 NULL },
Igor Sysoevfa73aac2003-05-21 13:28:21 +0000327
Igor Sysoev31eb8c02005-09-23 11:02:22 +0000328 { ngx_string("satisfy_any"),
329 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
330 ngx_conf_set_flag_slot,
331 NGX_HTTP_LOC_CONF_OFFSET,
332 offsetof(ngx_http_core_loc_conf_t, satisfy_any),
333 NULL },
334
Igor Sysoev899b44e2005-05-12 14:58:06 +0000335 { ngx_string("internal"),
336 NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,
337 ngx_http_core_internal,
338 NGX_HTTP_LOC_CONF_OFFSET,
339 0,
340 NULL },
341
Igor Sysoev865c1502003-11-30 20:03:18 +0000342 { ngx_string("lingering_time"),
343 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
344 ngx_conf_set_msec_slot,
345 NGX_HTTP_LOC_CONF_OFFSET,
346 offsetof(ngx_http_core_loc_conf_t, lingering_time),
347 NULL },
Igor Sysoevb7387572003-03-11 20:38:13 +0000348
Igor Sysoev865c1502003-11-30 20:03:18 +0000349 { ngx_string("lingering_timeout"),
350 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
351 ngx_conf_set_msec_slot,
352 NGX_HTTP_LOC_CONF_OFFSET,
353 offsetof(ngx_http_core_loc_conf_t, lingering_timeout),
354 NULL },
Igor Sysoevb7387572003-03-11 20:38:13 +0000355
Igor Sysoev0ab91b92004-06-06 19:49:18 +0000356 { ngx_string("reset_timedout_connection"),
357 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
358 ngx_conf_set_flag_slot,
359 NGX_HTTP_LOC_CONF_OFFSET,
360 offsetof(ngx_http_core_loc_conf_t, reset_timedout_connection),
361 NULL },
362
Igor Sysoev7b190b42005-06-07 15:56:31 +0000363 { ngx_string("port_in_redirect"),
364 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
365 ngx_conf_set_flag_slot,
366 NGX_HTTP_LOC_CONF_OFFSET,
367 offsetof(ngx_http_core_loc_conf_t, port_in_redirect),
368 NULL },
369
Igor Sysoev865c1502003-11-30 20:03:18 +0000370 { ngx_string("msie_padding"),
371 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
372 ngx_conf_set_flag_slot,
373 NGX_HTTP_LOC_CONF_OFFSET,
374 offsetof(ngx_http_core_loc_conf_t, msie_padding),
375 NULL },
Igor Sysoev12b4b002003-10-24 06:53:41 +0000376
Igor Sysoev5192b362005-07-08 14:34:20 +0000377 { ngx_string("log_not_found"),
378 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
379 ngx_conf_set_flag_slot,
380 NGX_HTTP_LOC_CONF_OFFSET,
381 offsetof(ngx_http_core_loc_conf_t, log_not_found),
382 NULL },
383
Igor Sysoev865c1502003-11-30 20:03:18 +0000384 { ngx_string("error_page"),
Igor Sysoev899b44e2005-05-12 14:58:06 +0000385 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
386 |NGX_CONF_2MORE,
Igor Sysoev805d9db2005-02-03 19:33:37 +0000387 ngx_http_core_error_page,
Igor Sysoev865c1502003-11-30 20:03:18 +0000388 NGX_HTTP_LOC_CONF_OFFSET,
389 0,
390 NULL },
Igor Sysoev74e95c22003-11-09 20:03:38 +0000391
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000392 { ngx_string("post_action"),
393 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
394 |NGX_CONF_TAKE1,
395 ngx_conf_set_str_slot,
396 NGX_HTTP_LOC_CONF_OFFSET,
397 offsetof(ngx_http_core_loc_conf_t, post_action),
398 NULL },
399
Igor Sysoev865c1502003-11-30 20:03:18 +0000400 { ngx_string("error_log"),
Igor Sysoev03420a62004-01-20 20:40:08 +0000401 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
Igor Sysoev805d9db2005-02-03 19:33:37 +0000402 ngx_http_core_error_log,
Igor Sysoev865c1502003-11-30 20:03:18 +0000403 NGX_HTTP_LOC_CONF_OFFSET,
404 0,
405 NULL },
Igor Sysoev890fc962003-07-20 21:15:59 +0000406
Igor Sysoev67f88e92004-03-12 16:57:08 +0000407#if (NGX_HTTP_CACHE)
408
Igor Sysoev865c1502003-11-30 20:03:18 +0000409 { ngx_string("open_file_cache"),
410 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE4,
411 ngx_http_set_cache_slot,
412 NGX_HTTP_LOC_CONF_OFFSET,
413 offsetof(ngx_http_core_loc_conf_t, open_files),
414 NULL },
415
Igor Sysoev67f88e92004-03-12 16:57:08 +0000416#endif
417
Igor Sysoev865c1502003-11-30 20:03:18 +0000418 ngx_null_command
Igor Sysoev4e9393a2003-01-09 05:36:00 +0000419};
420
421
422ngx_http_module_t ngx_http_core_module_ctx = {
Igor Sysoev899b44e2005-05-12 14:58:06 +0000423 ngx_http_core_preconfiguration, /* preconfiguration */
Igor Sysoev09c684b2005-11-09 17:25:55 +0000424 NULL, /* postconfiguration */
Igor Sysoev78329332003-11-10 17:17:31 +0000425
Igor Sysoeva9830112003-05-19 16:39:14 +0000426 ngx_http_core_create_main_conf, /* create main configuration */
427 ngx_http_core_init_main_conf, /* init main configuration */
Igor Sysoevdc479b42003-03-20 16:09:44 +0000428
Igor Sysoeva9830112003-05-19 16:39:14 +0000429 ngx_http_core_create_srv_conf, /* create server configuration */
430 ngx_http_core_merge_srv_conf, /* merge server configuration */
431
432 ngx_http_core_create_loc_conf, /* create location configuration */
433 ngx_http_core_merge_loc_conf /* merge location configuration */
Igor Sysoev4e9393a2003-01-09 05:36:00 +0000434};
435
436
437ngx_module_t ngx_http_core_module = {
Igor Sysoev899b44e2005-05-12 14:58:06 +0000438 NGX_MODULE_V1,
Igor Sysoev4e9393a2003-01-09 05:36:00 +0000439 &ngx_http_core_module_ctx, /* module context */
440 ngx_http_core_commands, /* module directives */
Igor Sysoev6253ca12003-05-27 12:18:54 +0000441 NGX_HTTP_MODULE, /* module type */
Igor Sysoeve5733802005-09-08 14:36:09 +0000442 NULL, /* init master */
Igor Sysoev899b44e2005-05-12 14:58:06 +0000443 NULL, /* init module */
Igor Sysoeve5733802005-09-08 14:36:09 +0000444 NULL, /* init process */
445 NULL, /* init thread */
446 NULL, /* exit thread */
447 NULL, /* exit process */
448 NULL, /* exit master */
449 NGX_MODULE_V1_PADDING
Igor Sysoev4e9393a2003-01-09 05:36:00 +0000450};
451
452
Igor Sysoevaa828612005-02-09 14:31:07 +0000453void
454ngx_http_handler(ngx_http_request_t *r)
Igor Sysoev6ddfbf02003-05-15 15:42:53 +0000455{
Igor Sysoeve5a222c2005-01-25 12:27:35 +0000456 r->connection->log->action = NULL;
Igor Sysoev6ddfbf02003-05-15 15:42:53 +0000457
458 r->connection->unexpected_eof = 0;
459
Igor Sysoev31eb8c02005-09-23 11:02:22 +0000460 if (r->err_ctx == NULL) {
461 switch (r->headers_in.connection_type) {
462 case 0:
463 if (r->http_version > NGX_HTTP_VERSION_10) {
464 r->keepalive = 1;
465 } else {
466 r->keepalive = 0;
467 }
468 break;
469
470 case NGX_HTTP_CONNECTION_CLOSE:
471 r->keepalive = 0;
472 break;
473
474 case NGX_HTTP_CONNECTION_KEEP_ALIVE:
Igor Sysoev419f9ac2003-10-21 16:49:56 +0000475 r->keepalive = 1;
Igor Sysoev31eb8c02005-09-23 11:02:22 +0000476 break;
477 }
478
479 if (r->keepalive && r->headers_in.msie && r->method == NGX_HTTP_POST) {
480
481 /*
482 * MSIE may wait for some time if the response for the POST request
483 * is sent over the keepalive connection
484 */
485
Igor Sysoev9760a132003-10-21 07:47:21 +0000486 r->keepalive = 0;
487 }
Igor Sysoev419f9ac2003-10-21 16:49:56 +0000488
Igor Sysoev31eb8c02005-09-23 11:02:22 +0000489 if (r->headers_in.content_length_n > 0) {
490 r->lingering_close = 1;
Igor Sysoev419f9ac2003-10-21 16:49:56 +0000491
Igor Sysoev31eb8c02005-09-23 11:02:22 +0000492 } else {
493 r->lingering_close = 0;
494 }
Igor Sysoev9760a132003-10-21 07:47:21 +0000495 }
496
Igor Sysoev899b44e2005-05-12 14:58:06 +0000497 r->write_event_handler = ngx_http_core_run_phases;
Igor Sysoev6ddfbf02003-05-15 15:42:53 +0000498
Igor Sysoevc04deca2005-03-28 14:43:02 +0000499 r->valid_unparsed_uri = 1;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000500 r->valid_location = 1;
Igor Sysoev805d9db2005-02-03 19:33:37 +0000501 r->uri_changed = 1;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000502 r->uri_changes = NGX_HTTP_MAX_REWRITE_CYCLES + 1;
Igor Sysoevc04deca2005-03-28 14:43:02 +0000503
Igor Sysoevdf3254a2006-01-11 15:26:57 +0000504 r->phase = (r == r->main) ? NGX_HTTP_POST_READ_PHASE:
Igor Sysoev09c684b2005-11-09 17:25:55 +0000505 NGX_HTTP_SERVER_REWRITE_PHASE;
Igor Sysoev1ebfead2005-02-16 13:40:36 +0000506 r->phase_handler = 0;
Igor Sysoev805d9db2005-02-03 19:33:37 +0000507
508 ngx_http_core_run_phases(r);
Igor Sysoevb3e73d82003-10-10 15:10:50 +0000509}
Igor Sysoeve2a31542003-04-08 15:40:10 +0000510
511
Igor Sysoevaa828612005-02-09 14:31:07 +0000512static void
Igor Sysoevaa828612005-02-09 14:31:07 +0000513ngx_http_core_run_phases(ngx_http_request_t *r)
Igor Sysoevb3e73d82003-10-10 15:10:50 +0000514{
Igor Sysoev865c1502003-11-30 20:03:18 +0000515 ngx_int_t rc;
Igor Sysoevb3e73d82003-10-10 15:10:50 +0000516 ngx_http_handler_pt *h;
Igor Sysoev865c1502003-11-30 20:03:18 +0000517 ngx_http_core_loc_conf_t *clcf;
Igor Sysoevb3e73d82003-10-10 15:10:50 +0000518 ngx_http_core_main_conf_t *cmcf;
519
Igor Sysoev899b44e2005-05-12 14:58:06 +0000520 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
521 "http phase handler");
522
Igor Sysoevb3e73d82003-10-10 15:10:50 +0000523 cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
524
Igor Sysoevc31a9bb2005-11-26 10:11:11 +0000525 for (/* void */; r->phase < NGX_HTTP_LOG_PHASE; r->phase++) {
Igor Sysoevb3e73d82003-10-10 15:10:50 +0000526
Igor Sysoev1ebfead2005-02-16 13:40:36 +0000527 if (r->phase == NGX_HTTP_REWRITE_PHASE + 1 && r->uri_changed) {
Igor Sysoev805d9db2005-02-03 19:33:37 +0000528
529 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
530 "uri changes: %d", r->uri_changes);
531
532 /*
533 * gcc before 3.3 compiles the broken code for
534 * if (r->uri_changes-- == 0)
535 * if the r->uri_changes is defined as
536 * unsigned uri_changes:4
537 */
538
539 r->uri_changes--;
540
541 if (r->uri_changes == 0) {
542 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
543 "rewrite cycle");
544 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
545 return;
546 }
547
Igor Sysoev1ebfead2005-02-16 13:40:36 +0000548 r->phase = NGX_HTTP_FIND_CONFIG_PHASE;
Igor Sysoev805d9db2005-02-03 19:33:37 +0000549 }
550
Igor Sysoevdf3254a2006-01-11 15:26:57 +0000551 if (r->phase == NGX_HTTP_ACCESS_PHASE && r != r->main) {
Igor Sysoev31eb8c02005-09-23 11:02:22 +0000552 continue;
553 }
554
Igor Sysoev865c1502003-11-30 20:03:18 +0000555 if (r->phase == NGX_HTTP_CONTENT_PHASE && r->content_handler) {
Igor Sysoev899b44e2005-05-12 14:58:06 +0000556 r->write_event_handler = ngx_http_request_empty_handler;
Igor Sysoev805d9db2005-02-03 19:33:37 +0000557 ngx_http_finalize_request(r, r->content_handler(r));
Igor Sysoev865c1502003-11-30 20:03:18 +0000558 return;
559 }
560
Igor Sysoevb3e73d82003-10-10 15:10:50 +0000561 h = cmcf->phases[r->phase].handlers.elts;
562 for (r->phase_handler = cmcf->phases[r->phase].handlers.nelts - 1;
563 r->phase_handler >= 0;
564 r->phase_handler--)
565 {
566 rc = h[r->phase_handler](r);
567
Igor Sysoev9760a132003-10-21 07:47:21 +0000568 if (rc == NGX_DONE) {
Igor Sysoev865c1502003-11-30 20:03:18 +0000569
570 /*
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000571 * we should never use r here because
Igor Sysoev31eb8c02005-09-23 11:02:22 +0000572 * it may point to already freed data
Igor Sysoev865c1502003-11-30 20:03:18 +0000573 */
574
Igor Sysoev9760a132003-10-21 07:47:21 +0000575 return;
576 }
577
Igor Sysoevb3e73d82003-10-10 15:10:50 +0000578 if (rc == NGX_DECLINED) {
579 continue;
580 }
581
Igor Sysoev31eb8c02005-09-23 11:02:22 +0000582 if (r->phase == NGX_HTTP_ACCESS_PHASE) {
583 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
584
585 if (clcf->satisfy_any) {
586
587 if (rc == NGX_OK) {
588 r->access_code = 0;
589
590 if (r->headers_out.www_authenticate) {
591 r->headers_out.www_authenticate->hash = 0;
592 }
593
594 break;
595 }
596
597 if (rc == NGX_HTTP_FORBIDDEN || rc == NGX_HTTP_UNAUTHORIZED)
598 {
599 r->access_code = rc;
600
601 continue;
602 }
603 }
604 }
605
Igor Sysoev3362b8d2005-05-14 18:42:03 +0000606 if (rc >= NGX_HTTP_SPECIAL_RESPONSE
607 || rc == NGX_HTTP_NO_CONTENT
608 || rc == NGX_ERROR)
609 {
Igor Sysoevb3e73d82003-10-10 15:10:50 +0000610 ngx_http_finalize_request(r, rc);
611 return;
612 }
613
Igor Sysoeve3466a42003-12-15 13:57:13 +0000614 if (r->phase == NGX_HTTP_CONTENT_PHASE) {
615 ngx_http_finalize_request(r, rc);
616 return;
617 }
618
619 if (rc == NGX_AGAIN) {
Igor Sysoev865c1502003-11-30 20:03:18 +0000620 return;
621 }
622
Igor Sysoevb3e73d82003-10-10 15:10:50 +0000623 if (rc == NGX_OK && cmcf->phases[r->phase].type == NGX_OK) {
624 break;
625 }
Igor Sysoev31eb8c02005-09-23 11:02:22 +0000626
627 }
628
629 if (r->phase == NGX_HTTP_ACCESS_PHASE && r->access_code) {
630 ngx_http_finalize_request(r, r->access_code);
631 return;
Igor Sysoevb3e73d82003-10-10 15:10:50 +0000632 }
Igor Sysoevb3e73d82003-10-10 15:10:50 +0000633 }
634
Igor Sysoev805d9db2005-02-03 19:33:37 +0000635 /* no content handler was found */
Igor Sysoev865c1502003-11-30 20:03:18 +0000636
Igor Sysoev1ebfead2005-02-16 13:40:36 +0000637 if (r->uri.data[r->uri.len - 1] == '/' && !r->zero_in_uri) {
Igor Sysoev865c1502003-11-30 20:03:18 +0000638
639 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
640
Igor Sysoev865c1502003-11-30 20:03:18 +0000641 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
Igor Sysoev805d9db2005-02-03 19:33:37 +0000642 "directory index of \"%V%V\" is forbidden",
643 &clcf->root, &r->uri);
Igor Sysoev865c1502003-11-30 20:03:18 +0000644
645 ngx_http_finalize_request(r, NGX_HTTP_FORBIDDEN);
Igor Sysoevb3e73d82003-10-10 15:10:50 +0000646 return;
647 }
648
Igor Sysoev865c1502003-11-30 20:03:18 +0000649 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "no handler found");
650
651 ngx_http_finalize_request(r, NGX_HTTP_NOT_FOUND);
Igor Sysoevb3e73d82003-10-10 15:10:50 +0000652}
653
654
Igor Sysoevaa828612005-02-09 14:31:07 +0000655ngx_int_t
656ngx_http_find_location_config(ngx_http_request_t *r)
Igor Sysoevb3e73d82003-10-10 15:10:50 +0000657{
Igor Sysoev74a5ddb2004-07-18 19:11:20 +0000658 ngx_int_t rc;
659 ngx_http_core_loc_conf_t *clcf;
660 ngx_http_core_srv_conf_t *cscf;
Igor Sysoeve2a31542003-04-08 15:40:10 +0000661
Igor Sysoev1ebfead2005-02-16 13:40:36 +0000662 r->content_handler = NULL;
663 r->uri_changed = 0;
664
Igor Sysoev6253ca12003-05-27 12:18:54 +0000665 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
Igor Sysoeve2a31542003-04-08 15:40:10 +0000666
Igor Sysoev805d9db2005-02-03 19:33:37 +0000667 rc = ngx_http_core_find_location(r, &cscf->locations, 0);
Igor Sysoeva8fa0a62003-11-25 20:44:56 +0000668
Igor Sysoev74a5ddb2004-07-18 19:11:20 +0000669 if (rc == NGX_HTTP_INTERNAL_SERVER_ERROR) {
670 return rc;
Igor Sysoeva8fa0a62003-11-25 20:44:56 +0000671 }
672
Igor Sysoev419f9ac2003-10-21 16:49:56 +0000673 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
674
Igor Sysoev899b44e2005-05-12 14:58:06 +0000675 if (!r->internal && clcf->internal) {
676 return NGX_HTTP_NOT_FOUND;
677 }
678
Igor Sysoeva2573672005-10-05 14:46:21 +0000679 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
680 "using configuration \"%s%V\"",
681 (clcf->noname ? "*" : (clcf->exact_match ? "=" : "")),
682 &clcf->name);
683
Igor Sysoevb85fd592005-08-23 15:36:54 +0000684 ngx_http_update_location_config(r);
Igor Sysoev89690bf2004-03-23 06:01:52 +0000685
686 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
Igor Sysoev1b735832004-11-11 14:07:14 +0000687 "http cl:%z max:%uz",
688 r->headers_in.content_length_n, clcf->client_max_body_size);
Igor Sysoev89690bf2004-03-23 06:01:52 +0000689
690 if (r->headers_in.content_length_n != -1
691 && clcf->client_max_body_size
692 && clcf->client_max_body_size < (size_t) r->headers_in.content_length_n)
693 {
694 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
Igor Sysoev1b735832004-11-11 14:07:14 +0000695 "client intented to send too large body: %z bytes",
Igor Sysoev89690bf2004-03-23 06:01:52 +0000696 r->headers_in.content_length_n);
697
698 return NGX_HTTP_REQUEST_ENTITY_TOO_LARGE;
699 }
700
701
Igor Sysoev74a5ddb2004-07-18 19:11:20 +0000702 if (rc == NGX_HTTP_LOCATION_AUTO_REDIRECT) {
Igor Sysoevaab4d8c2004-09-06 18:45:00 +0000703 r->headers_out.location = ngx_list_push(&r->headers_out.headers);
704 if (r->headers_out.location == NULL) {
Igor Sysoev1b138ed2003-11-18 21:34:08 +0000705 return NGX_HTTP_INTERNAL_SERVER_ERROR;
706 }
707
Igor Sysoev899b44e2005-05-12 14:58:06 +0000708 /*
709 * we do not need to set the r->headers_out.location->hash and
710 * r->headers_out.location->key fields
711 */
712
Igor Sysoev74a5ddb2004-07-18 19:11:20 +0000713 r->headers_out.location->value = clcf->name;
Igor Sysoev1b138ed2003-11-18 21:34:08 +0000714
715 return NGX_HTTP_MOVED_PERMANENTLY;
716 }
717
Igor Sysoevb85fd592005-08-23 15:36:54 +0000718 return NGX_OK;
719}
720
721
722void
723ngx_http_update_location_config(ngx_http_request_t *r)
724{
725 ngx_http_core_loc_conf_t *clcf;
726
727 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
728
Igor Sysoev94e32ce2006-04-07 14:08:04 +0000729 if (r->method & clcf->limit_except) {
730 r->loc_conf = clcf->limit_except_loc_conf;
731 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
732 }
733
Igor Sysoevb85fd592005-08-23 15:36:54 +0000734 r->connection->log->file = clcf->err_log->file;
735 if (!(r->connection->log->log_level & NGX_LOG_DEBUG_CONNECTION)) {
736 r->connection->log->log_level = clcf->err_log->log_level;
737 }
738
739 if ((ngx_io.flags & NGX_IO_SENDFILE) && clcf->sendfile) {
740 r->connection->sendfile = 1;
741
742 } else {
743 r->connection->sendfile = 0;
744 }
745
746 if (r->keepalive && clcf->keepalive_timeout == 0) {
747 r->keepalive = 0;
748 }
749
750 if (!clcf->tcp_nopush) {
751 /* disable TCP_NOPUSH/TCP_CORK use */
752 r->connection->tcp_nopush = NGX_TCP_NOPUSH_DISABLED;
753 }
754
Igor Sysoevc2807ec2006-02-16 15:26:46 +0000755 if (r->limit_rate == 0) {
756 r->limit_rate = clcf->limit_rate;
757 }
Igor Sysoev5192b362005-07-08 14:34:20 +0000758
Igor Sysoev6253ca12003-05-27 12:18:54 +0000759 if (clcf->handler) {
Igor Sysoevb3e73d82003-10-10 15:10:50 +0000760 r->content_handler = clcf->handler;
Igor Sysoev4e9393a2003-01-09 05:36:00 +0000761 }
Igor Sysoev4e9393a2003-01-09 05:36:00 +0000762}
763
764
Igor Sysoevaa828612005-02-09 14:31:07 +0000765static ngx_int_t
766ngx_http_core_find_location(ngx_http_request_t *r,
767 ngx_array_t *locations, size_t len)
Igor Sysoev74a5ddb2004-07-18 19:11:20 +0000768{
769 ngx_int_t n, rc;
Igor Sysoevd43bee82004-11-20 19:52:20 +0000770 ngx_uint_t i, found, noregex;
Igor Sysoev74a5ddb2004-07-18 19:11:20 +0000771 ngx_http_core_loc_conf_t *clcf, **clcfp;
772
Igor Sysoeva2573672005-10-05 14:46:21 +0000773 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
774 "find location for \"%V\"", &r->uri);
Igor Sysoev74a5ddb2004-07-18 19:11:20 +0000775
776 found = 0;
Igor Sysoevd43bee82004-11-20 19:52:20 +0000777 noregex = 0;
Igor Sysoev74a5ddb2004-07-18 19:11:20 +0000778
779 clcfp = locations->elts;
780 for (i = 0; i < locations->nelts; i++) {
781
Igor Sysoevc0edbcc2004-10-21 15:34:38 +0000782#if (NGX_PCRE)
Igor Sysoev74a5ddb2004-07-18 19:11:20 +0000783 if (clcfp[i]->regex) {
784 break;
785 }
786#endif
787
Igor Sysoev805d9db2005-02-03 19:33:37 +0000788 if (clcfp[i]->noname) {
789 break;
790 }
791
Igor Sysoev74a5ddb2004-07-18 19:11:20 +0000792 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
Igor Sysoev1b735832004-11-11 14:07:14 +0000793 "find location: %s\"%V\"",
794 clcfp[i]->exact_match ? "= " : "", &clcfp[i]->name);
Igor Sysoev74a5ddb2004-07-18 19:11:20 +0000795
796 if (clcfp[i]->auto_redirect
797 && r->uri.len == clcfp[i]->name.len - 1
798 && ngx_strncmp(r->uri.data, clcfp[i]->name.data,
799 clcfp[i]->name.len - 1) == 0)
800 {
801 /* the locations are lexicographically sorted */
802
803 r->loc_conf = clcfp[i]->loc_conf;
804
805 return NGX_HTTP_LOCATION_AUTO_REDIRECT;
806 }
807
808 if (r->uri.len < clcfp[i]->name.len) {
809 continue;
810 }
811
812 n = ngx_strncmp(r->uri.data, clcfp[i]->name.data, clcfp[i]->name.len);
813
814 if (n < 0) {
815 /* the locations are lexicographically sorted */
816 break;
817 }
818
819 if (n == 0) {
Igor Sysoev805d9db2005-02-03 19:33:37 +0000820 if (clcfp[i]->exact_match) {
821
822 if (r->uri.len == clcfp[i]->name.len) {
823 r->loc_conf = clcfp[i]->loc_conf;
824 return NGX_HTTP_LOCATION_EXACT;
825 }
826
827 continue;
Igor Sysoevea521232004-07-26 16:21:18 +0000828 }
829
Igor Sysoev71057632004-08-30 19:24:51 +0000830 if (len > clcfp[i]->name.len) {
Igor Sysoev74a5ddb2004-07-18 19:11:20 +0000831 /* the previous match is longer */
832 break;
833 }
834
835 r->loc_conf = clcfp[i]->loc_conf;
Igor Sysoevd43bee82004-11-20 19:52:20 +0000836 noregex = clcfp[i]->noregex;
Igor Sysoev74a5ddb2004-07-18 19:11:20 +0000837 found = 1;
838 }
839 }
840
841 if (found) {
842 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
843
844 if (clcf->locations.nelts) {
Igor Sysoev805d9db2005-02-03 19:33:37 +0000845 rc = ngx_http_core_find_location(r, &clcf->locations, len);
Igor Sysoev74a5ddb2004-07-18 19:11:20 +0000846
847 if (rc != NGX_OK) {
848 return rc;
849 }
850 }
851 }
852
Igor Sysoevc0edbcc2004-10-21 15:34:38 +0000853#if (NGX_PCRE)
Igor Sysoev74a5ddb2004-07-18 19:11:20 +0000854
Igor Sysoevd43bee82004-11-20 19:52:20 +0000855 if (noregex) {
856 return NGX_HTTP_LOCATION_NOREGEX;
857 }
858
Igor Sysoev74a5ddb2004-07-18 19:11:20 +0000859 /* regex matches */
860
861 for (/* void */; i < locations->nelts; i++) {
862
863 if (!clcfp[i]->regex) {
864 continue;
865 }
866
Igor Sysoev805d9db2005-02-03 19:33:37 +0000867 if (clcfp[i]->noname) {
868 break;
869 }
870
Igor Sysoev74a5ddb2004-07-18 19:11:20 +0000871 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
Igor Sysoev1b735832004-11-11 14:07:14 +0000872 "find location: ~ \"%V\"", &clcfp[i]->name);
Igor Sysoev74a5ddb2004-07-18 19:11:20 +0000873
874 n = ngx_regex_exec(clcfp[i]->regex, &r->uri, NULL, 0);
875
Igor Sysoev3259e852005-01-19 13:10:56 +0000876 if (n == NGX_REGEX_NO_MATCHED) {
Igor Sysoev74a5ddb2004-07-18 19:11:20 +0000877 continue;
878 }
879
880 if (n < 0) {
881 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
882 ngx_regex_exec_n
Igor Sysoev1b735832004-11-11 14:07:14 +0000883 " failed: %d on \"%V\" using \"%V\"",
884 n, &r->uri, &clcfp[i]->name);
Igor Sysoev74a5ddb2004-07-18 19:11:20 +0000885 return NGX_HTTP_INTERNAL_SERVER_ERROR;
886 }
887
888 /* match */
889
890 r->loc_conf = clcfp[i]->loc_conf;
891
892 return NGX_HTTP_LOCATION_REGEX;
893 }
894
Igor Sysoevc0edbcc2004-10-21 15:34:38 +0000895#endif /* NGX_PCRE */
Igor Sysoev74a5ddb2004-07-18 19:11:20 +0000896
897 return NGX_OK;
898}
899
900
Igor Sysoevaa828612005-02-09 14:31:07 +0000901ngx_int_t
902ngx_http_set_content_type(ngx_http_request_t *r)
Igor Sysoev865c1502003-11-30 20:03:18 +0000903{
Igor Sysoev805d9db2005-02-03 19:33:37 +0000904 u_char c, *p, *exten;
Igor Sysoev24025022005-12-16 15:07:08 +0000905 ngx_str_t *type;
Igor Sysoev10a543a2004-03-16 07:10:12 +0000906 ngx_uint_t i;
Igor Sysoev865c1502003-11-30 20:03:18 +0000907 ngx_http_core_loc_conf_t *clcf;
908
Igor Sysoev865c1502003-11-30 20:03:18 +0000909 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
910
911 if (r->exten.len) {
Igor Sysoev805d9db2005-02-03 19:33:37 +0000912
913 if (!r->low_case_exten) {
914 for (i = 0; i < r->exten.len; i++) {
915 c = r->exten.data[i];
916 if (c >= 'A' && c <= 'Z') {
917 break;
918 }
919 }
920
921 if (i < r->exten.len) {
Igor Sysoevc1571722005-03-19 12:38:37 +0000922 p = ngx_palloc(r->pool, r->exten.len);
923 if (p == NULL) {
Igor Sysoev805d9db2005-02-03 19:33:37 +0000924 return NGX_HTTP_INTERNAL_SERVER_ERROR;
925 }
926
927 exten = p;
928
929 for (i = 0; i < r->exten.len; i++) {
930 c = r->exten.data[i];
Igor Sysoev24025022005-12-16 15:07:08 +0000931 *p++ = ngx_tolower(c);
Igor Sysoev805d9db2005-02-03 19:33:37 +0000932 }
933
934 r->exten.data = exten;
935 }
936
937 r->low_case_exten = 1;
938 }
939
Igor Sysoev24025022005-12-16 15:07:08 +0000940 type = ngx_hash_find(&clcf->types_hash,
941 ngx_hash_key(r->exten.data, r->exten.len),
942 r->exten.data, r->exten.len);
Igor Sysoev865c1502003-11-30 20:03:18 +0000943
Igor Sysoev24025022005-12-16 15:07:08 +0000944 if (type) {
945 r->headers_out.content_type = *type;
946 return NGX_OK;
Igor Sysoev865c1502003-11-30 20:03:18 +0000947 }
948 }
949
Igor Sysoev24025022005-12-16 15:07:08 +0000950 r->headers_out.content_type = clcf->default_type;
Igor Sysoev865c1502003-11-30 20:03:18 +0000951
952 return NGX_OK;
953}
954
955
Igor Sysoevaa828612005-02-09 14:31:07 +0000956ngx_int_t
Igor Sysoevaa828612005-02-09 14:31:07 +0000957ngx_http_set_exten(ngx_http_request_t *r)
Igor Sysoev3b30a902003-12-25 20:26:58 +0000958{
959 ngx_int_t i;
960
961 r->exten.len = 0;
962 r->exten.data = NULL;
963
964 for (i = r->uri.len - 1; i > 1; i--) {
965 if (r->uri.data[i] == '.' && r->uri.data[i - 1] != '/') {
966 r->exten.len = r->uri.len - i - 1;
967
968 if (r->exten.len > 0) {
Igor Sysoevc1571722005-03-19 12:38:37 +0000969 r->exten.data = ngx_palloc(r->pool, r->exten.len + 1);
970 if (r->exten.data == NULL) {
Igor Sysoev3b30a902003-12-25 20:26:58 +0000971 return NGX_ERROR;
972 }
973
974 ngx_cpystrn(r->exten.data, &r->uri.data[i + 1],
975 r->exten.len + 1);
976 }
977
978 break;
979
980 } else if (r->uri.data[i] == '/') {
981 break;
982 }
983 }
984
Igor Sysoev805d9db2005-02-03 19:33:37 +0000985 r->low_case_exten = 0;
986
Igor Sysoev3b30a902003-12-25 20:26:58 +0000987 return NGX_OK;
988}
989
990
Igor Sysoevaa828612005-02-09 14:31:07 +0000991ngx_int_t
Igor Sysoev208eed22005-10-07 13:30:52 +0000992ngx_http_send_header(ngx_http_request_t *r)
993{
994 if (r->err_status) {
995 r->headers_out.status = r->err_status;
996 r->headers_out.status_line.len = 0;
997 }
998
999 return ngx_http_top_header_filter(r);
1000}
1001
1002
1003ngx_int_t
1004ngx_http_output_filter(ngx_http_request_t *r, ngx_chain_t *in)
1005{
1006 ngx_int_t rc;
1007
Igor Sysoevd3283ff2005-12-05 13:18:09 +00001008 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1009 "http output filter \"%V?%V\"", &r->uri, &r->args);
Igor Sysoev208eed22005-10-07 13:30:52 +00001010
1011 rc = ngx_http_top_body_filter(r, in);
1012
1013 if (rc == NGX_ERROR) {
1014 /* NGX_ERROR may be returned by any filter */
Igor Sysoevd3283ff2005-12-05 13:18:09 +00001015 r->connection->error = 1;
Igor Sysoev208eed22005-10-07 13:30:52 +00001016 }
1017
1018 return rc;
1019}
1020
1021
1022u_char *
1023ngx_http_map_uri_to_path(ngx_http_request_t *r, ngx_str_t *path,
1024 size_t reserved)
1025{
Igor Sysoev8fea8852006-03-15 09:53:04 +00001026 u_char *last;
1027 size_t alias;
1028 ngx_http_core_loc_conf_t *clcf;
Igor Sysoev208eed22005-10-07 13:30:52 +00001029
1030 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
1031
1032 alias = clcf->alias ? clcf->name.len : 0;
1033
Igor Sysoev34303462006-01-24 16:08:27 +00001034 if (alias && !r->valid_location) {
1035 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
1036 "\"alias\" could not be used in location \"%V\" "
1037 "where URI was rewritten", &clcf->name);
1038 return NULL;
1039 }
1040
Igor Sysoev8fea8852006-03-15 09:53:04 +00001041 reserved += r->uri.len - alias + 1;
1042
Igor Sysoev3ca233e2005-12-28 14:23:52 +00001043 if (clcf->root_lengths == NULL) {
Igor Sysoev208eed22005-10-07 13:30:52 +00001044
Igor Sysoev3ca233e2005-12-28 14:23:52 +00001045 r->root_length = clcf->root.len;
1046
Igor Sysoev8fea8852006-03-15 09:53:04 +00001047 path->len = clcf->root.len + reserved;
Igor Sysoev3ca233e2005-12-28 14:23:52 +00001048
1049 path->data = ngx_palloc(r->pool, path->len);
1050 if (path->data == NULL) {
1051 return NULL;
1052 }
1053
1054 last = ngx_copy(path->data, clcf->root.data, clcf->root.len);
Igor Sysoev3ca233e2005-12-28 14:23:52 +00001055
Igor Sysoev8fea8852006-03-15 09:53:04 +00001056 } else {
1057 last = ngx_http_script_run(r, path, clcf->root_lengths->elts, reserved,
1058 clcf->root_values->elts);
1059 if (last == NULL) {
1060 return NULL;
1061 }
1062
1063 r->root_length = path->len - reserved;
Igor Sysoev3ca233e2005-12-28 14:23:52 +00001064 }
1065
Igor Sysoev8fea8852006-03-15 09:53:04 +00001066 last = ngx_cpystrn(last, r->uri.data + alias, r->uri.len - alias + 1);
Igor Sysoev208eed22005-10-07 13:30:52 +00001067
1068 return last;
1069}
1070
1071
1072ngx_int_t
Igor Sysoevceb99292005-09-06 16:09:32 +00001073ngx_http_auth_basic_user(ngx_http_request_t *r)
1074{
1075 ngx_str_t auth, encoded;
1076 ngx_uint_t len;
1077
1078 if (r->headers_in.user.len == 0 && r->headers_in.user.data != NULL) {
1079 return NGX_DECLINED;
1080 }
1081
1082 if (r->headers_in.authorization == NULL) {
1083 r->headers_in.user.data = (u_char *) "";
1084 return NGX_DECLINED;
1085 }
1086
1087 encoded = r->headers_in.authorization->value;
1088
1089 if (encoded.len < sizeof("Basic ") - 1
1090 || ngx_strncasecmp(encoded.data, "Basic ", sizeof("Basic ") - 1) != 0)
1091 {
1092 r->headers_in.user.data = (u_char *) "";
1093 return NGX_DECLINED;
1094 }
1095
1096 encoded.len -= sizeof("Basic ") - 1;
1097 encoded.data += sizeof("Basic ") - 1;
1098
1099 while (encoded.len && encoded.data[0] == ' ') {
1100 encoded.len--;
1101 encoded.data++;
1102 }
1103
1104 if (encoded.len == 0) {
1105 r->headers_in.user.data = (u_char *) "";
1106 return NGX_DECLINED;
1107 }
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001108
Igor Sysoevceb99292005-09-06 16:09:32 +00001109 auth.len = ngx_base64_decoded_length(encoded.len);
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001110 auth.data = ngx_palloc(r->pool, auth.len + 1);
Igor Sysoevceb99292005-09-06 16:09:32 +00001111 if (auth.data == NULL) {
1112 return NGX_ERROR;
1113 }
1114
1115 if (ngx_decode_base64(&auth, &encoded) != NGX_OK) {
1116 r->headers_in.user.data = (u_char *) "";
1117 return NGX_DECLINED;
1118 }
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001119
Igor Sysoevceb99292005-09-06 16:09:32 +00001120 auth.data[auth.len] = '\0';
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001121
1122 for (len = 0; len < auth.len; len++) {
Igor Sysoevceb99292005-09-06 16:09:32 +00001123 if (auth.data[len] == ':') {
1124 break;
1125 }
1126 }
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001127
Igor Sysoev09c684b2005-11-09 17:25:55 +00001128 if (len == 0 || len == auth.len) {
Igor Sysoevceb99292005-09-06 16:09:32 +00001129 r->headers_in.user.data = (u_char *) "";
1130 return NGX_DECLINED;
1131 }
1132
1133 r->headers_in.user.len = len;
1134 r->headers_in.user.data = auth.data;
1135 r->headers_in.passwd.len = auth.len - len - 1;
1136 r->headers_in.passwd.data = &auth.data[len + 1];
1137
1138 return NGX_OK;
1139}
1140
1141
1142ngx_int_t
Igor Sysoev899b44e2005-05-12 14:58:06 +00001143ngx_http_subrequest(ngx_http_request_t *r,
Igor Sysoev09c684b2005-11-09 17:25:55 +00001144 ngx_str_t *uri, ngx_str_t *args, ngx_uint_t flags)
Igor Sysoev899b44e2005-05-12 14:58:06 +00001145{
Igor Sysoevd3283ff2005-12-05 13:18:09 +00001146 ngx_connection_t *c;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001147 ngx_http_request_t *sr;
1148 ngx_http_core_srv_conf_t *cscf;
1149 ngx_http_postponed_request_t *pr, *p;
1150
1151 sr = ngx_pcalloc(r->pool, sizeof(ngx_http_request_t));
1152 if (sr == NULL) {
1153 return NGX_HTTP_INTERNAL_SERVER_ERROR;
1154 }
1155
1156 sr->signature = NGX_HTTP_MODULE;
Igor Sysoevd3283ff2005-12-05 13:18:09 +00001157
1158 c = r->connection;
1159 sr->connection = c;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001160
1161 sr->ctx = ngx_pcalloc(r->pool, sizeof(void *) * ngx_http_max_module);
1162 if (sr->ctx == NULL) {
1163 return NGX_HTTP_INTERNAL_SERVER_ERROR;
1164 }
1165
1166 if (ngx_list_init(&sr->headers_out.headers, r->pool, 20,
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001167 sizeof(ngx_table_elt_t))
1168 == NGX_ERROR)
Igor Sysoev899b44e2005-05-12 14:58:06 +00001169 {
1170 return NGX_HTTP_INTERNAL_SERVER_ERROR;
1171 }
1172
1173 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
1174 sr->main_conf = cscf->ctx->main_conf;
1175 sr->srv_conf = cscf->ctx->srv_conf;
1176 sr->loc_conf = cscf->ctx->loc_conf;
1177
1178 sr->pool = r->pool;
1179
1180 sr->headers_in = r->headers_in;
1181
1182 sr->start_time = ngx_time();
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001183
1184 ngx_http_clear_content_length(sr);
1185 ngx_http_clear_accept_ranges(sr);
1186 ngx_http_clear_last_modified(sr);
Igor Sysoev899b44e2005-05-12 14:58:06 +00001187
1188 sr->request_body = r->request_body;
1189
1190 sr->method = NGX_HTTP_GET;
1191 sr->http_version = r->http_version;
1192 sr->http_major = r->http_minor;
1193
1194 sr->request_line = r->request_line;
1195 sr->uri = *uri;
Igor Sysoev09c684b2005-11-09 17:25:55 +00001196
Igor Sysoev899b44e2005-05-12 14:58:06 +00001197 if (args) {
1198 sr->args = *args;
1199 }
Igor Sysoev09c684b2005-11-09 17:25:55 +00001200
Igor Sysoevd3283ff2005-12-05 13:18:09 +00001201 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
1202 "http subrequest \"%V?%V\"", uri, &sr->args);
1203
Igor Sysoev09c684b2005-11-09 17:25:55 +00001204 if (flags & NGX_HTTP_ZERO_IN_URI) {
1205 sr->zero_in_uri = 1;
1206 }
1207
Igor Sysoev899b44e2005-05-12 14:58:06 +00001208 sr->unparsed_uri = r->unparsed_uri;
1209 sr->method_name = r->method_name;
1210 sr->http_protocol = r->http_protocol;
1211
1212 if (ngx_http_set_exten(sr) != NGX_OK) {
1213 return NGX_HTTP_INTERNAL_SERVER_ERROR;
1214 }
1215
Igor Sysoevf6e1fe32005-10-04 10:38:53 +00001216 sr->main = r->main;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001217 sr->parent = r;
1218 sr->read_event_handler = ngx_http_request_empty_handler;
1219 sr->write_event_handler = ngx_http_request_empty_handler;
1220
Igor Sysoevd3283ff2005-12-05 13:18:09 +00001221 if (c->data == r) {
1222 c->data = sr;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001223 }
1224
1225 sr->in_addr = r->in_addr;
1226 sr->port = r->port;
1227 sr->port_text = r->port_text;
1228 sr->server_name = r->server_name;
1229
1230 sr->variables = r->variables;
1231
1232 sr->log_handler = r->log_handler;
1233
1234 pr = ngx_palloc(r->pool, sizeof(ngx_http_postponed_request_t));
1235 if (pr == NULL) {
1236 return NGX_HTTP_INTERNAL_SERVER_ERROR;
1237 }
1238
1239 pr->request = sr;
1240 pr->out = NULL;
1241 pr->next = NULL;
1242
1243 if (r->postponed) {
1244 for (p = r->postponed; p->next; p = p->next) { /* void */ }
1245 p->next = pr;
1246
1247 } else {
1248 r->postponed = pr;
1249 }
1250
1251 sr->internal = 1;
Igor Sysoevdf3254a2006-01-11 15:26:57 +00001252 sr->fast_subrequest = 1;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001253
Igor Sysoev31eb8c02005-09-23 11:02:22 +00001254 sr->discard_body = r->discard_body;
Igor Sysoevd52477f2005-05-16 13:53:20 +00001255 sr->main_filter_need_in_memory = r->main_filter_need_in_memory;
1256
Igor Sysoev899b44e2005-05-12 14:58:06 +00001257 ngx_http_handler(sr);
1258
Igor Sysoevd3283ff2005-12-05 13:18:09 +00001259 if (!c->destroyed) {
Igor Sysoevdf3254a2006-01-11 15:26:57 +00001260 sr->fast_subrequest = 0;
1261
Igor Sysoevd3283ff2005-12-05 13:18:09 +00001262 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
1263 "http subrequest done \"%V?%V\"", uri, &sr->args);
1264 }
Igor Sysoev899b44e2005-05-12 14:58:06 +00001265
1266 return NGX_OK;
1267}
1268
1269
1270ngx_int_t
Igor Sysoevaa828612005-02-09 14:31:07 +00001271ngx_http_internal_redirect(ngx_http_request_t *r,
1272 ngx_str_t *uri, ngx_str_t *args)
Igor Sysoev4e9393a2003-01-09 05:36:00 +00001273{
Igor Sysoevaa828612005-02-09 14:31:07 +00001274 ngx_http_core_srv_conf_t *cscf;
1275
Igor Sysoev1b735832004-11-11 14:07:14 +00001276 r->uri = *uri;
Igor Sysoev13933252003-05-29 13:02:09 +00001277
1278 if (args) {
Igor Sysoev1b735832004-11-11 14:07:14 +00001279 r->args = *args;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001280
1281 } else {
1282 r->args.len = 0;
1283 r->args.data = NULL;
Igor Sysoev13933252003-05-29 13:02:09 +00001284 }
1285
Igor Sysoevd3283ff2005-12-05 13:18:09 +00001286 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1287 "internal redirect: \"%V?%V\"", uri, &r->args);
1288
Igor Sysoev3b30a902003-12-25 20:26:58 +00001289 if (ngx_http_set_exten(r) != NGX_OK) {
1290 return NGX_HTTP_INTERNAL_SERVER_ERROR;
Igor Sysoev13933252003-05-29 13:02:09 +00001291 }
Igor Sysoev4e9393a2003-01-09 05:36:00 +00001292
Igor Sysoev27c30f92003-11-11 18:13:43 +00001293 if (r->err_ctx) {
Igor Sysoevd59a0472003-11-10 21:09:22 +00001294
Igor Sysoevaa828612005-02-09 14:31:07 +00001295 /* allocate the new module's contexts */
Igor Sysoev27c30f92003-11-11 18:13:43 +00001296
Igor Sysoevd59a0472003-11-10 21:09:22 +00001297 r->ctx = ngx_pcalloc(r->pool, sizeof(void *) * ngx_http_max_module);
1298 if (r->ctx == NULL) {
1299 return NGX_HTTP_INTERNAL_SERVER_ERROR;
1300 }
1301
1302 } else {
Igor Sysoev27c30f92003-11-11 18:13:43 +00001303
1304 /* clear the modules contexts */
1305
Igor Sysoevd59a0472003-11-10 21:09:22 +00001306 ngx_memzero(r->ctx, sizeof(void *) * ngx_http_max_module);
1307 }
Igor Sysoev0a280a32003-10-12 16:49:16 +00001308
Igor Sysoevaa828612005-02-09 14:31:07 +00001309 cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
1310 r->loc_conf = cscf->ctx->loc_conf;
1311
Igor Sysoevb85fd592005-08-23 15:36:54 +00001312 ngx_http_update_location_config(r);
1313
Igor Sysoev899b44e2005-05-12 14:58:06 +00001314 r->internal = 1;
1315
Igor Sysoev79a80482003-05-14 17:13:13 +00001316 ngx_http_handler(r);
Igor Sysoev13933252003-05-29 13:02:09 +00001317
Igor Sysoev9760a132003-10-21 07:47:21 +00001318 return NGX_DONE;
Igor Sysoev4e9393a2003-01-09 05:36:00 +00001319}
1320
1321
Igor Sysoev9ac946b2005-10-24 15:09:41 +00001322ngx_http_cleanup_t *
1323ngx_http_cleanup_add(ngx_http_request_t *r, size_t size)
Igor Sysoev0a280a32003-10-12 16:49:16 +00001324{
Igor Sysoev9ac946b2005-10-24 15:09:41 +00001325 ngx_http_cleanup_t *cln;
Igor Sysoev0a280a32003-10-12 16:49:16 +00001326
Igor Sysoev9ac946b2005-10-24 15:09:41 +00001327 r = r->main;
1328
1329 cln = ngx_palloc(r->pool, sizeof(ngx_http_cleanup_t));
1330 if (cln == NULL) {
1331 return NULL;
Igor Sysoev0a280a32003-10-12 16:49:16 +00001332 }
1333
Igor Sysoev9ac946b2005-10-24 15:09:41 +00001334 if (size) {
1335 cln->data = ngx_palloc(r->pool, size);
1336 if (cln->data == NULL) {
1337 return NULL;
1338 }
Igor Sysoev0a280a32003-10-12 16:49:16 +00001339
Igor Sysoev9ac946b2005-10-24 15:09:41 +00001340 } else {
1341 cln->data = NULL;
1342 }
1343
1344 cln->handler = NULL;
1345 cln->next = r->cleanup;
1346
1347 r->cleanup = cln;
1348
1349 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1350 "http cleanup add: %p", cln);
1351
1352 return cln;
1353}
Igor Sysoev0a280a32003-10-12 16:49:16 +00001354
1355
Igor Sysoevaa828612005-02-09 14:31:07 +00001356static char *
1357ngx_http_core_server(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy)
Igor Sysoev4e9393a2003-01-09 05:36:00 +00001358{
Igor Sysoevaa828612005-02-09 14:31:07 +00001359 char *rv;
1360 void *mconf;
Igor Sysoev7b190b42005-06-07 15:56:31 +00001361 ngx_uint_t m;
1362 ngx_conf_t pcf;
Igor Sysoevaa828612005-02-09 14:31:07 +00001363 ngx_http_module_t *module;
1364 ngx_http_conf_ctx_t *ctx, *http_ctx;
1365 ngx_http_core_srv_conf_t *cscf, **cscfp;
1366 ngx_http_core_main_conf_t *cmcf;
Igor Sysoev4e9393a2003-01-09 05:36:00 +00001367
Igor Sysoevc1571722005-03-19 12:38:37 +00001368 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
1369 if (ctx == NULL) {
Igor Sysoevaa828612005-02-09 14:31:07 +00001370 return NGX_CONF_ERROR;
1371 }
Igor Sysoev4e9393a2003-01-09 05:36:00 +00001372
Igor Sysoev74a5ddb2004-07-18 19:11:20 +00001373 http_ctx = cf->ctx;
1374 ctx->main_conf = http_ctx->main_conf;
Igor Sysoevbb4ec5c2003-05-16 15:27:48 +00001375
Igor Sysoeva9830112003-05-19 16:39:14 +00001376 /* the server{}'s srv_conf */
Igor Sysoevbb4ec5c2003-05-16 15:27:48 +00001377
Igor Sysoevaa828612005-02-09 14:31:07 +00001378 ctx->srv_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
1379 if (ctx->srv_conf == NULL) {
1380 return NGX_CONF_ERROR;
1381 }
Igor Sysoev4e9393a2003-01-09 05:36:00 +00001382
Igor Sysoeva9830112003-05-19 16:39:14 +00001383 /* the server{}'s loc_conf */
Igor Sysoevbb4ec5c2003-05-16 15:27:48 +00001384
Igor Sysoevaa828612005-02-09 14:31:07 +00001385 ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
1386 if (ctx->loc_conf == NULL) {
1387 return NGX_CONF_ERROR;
1388 }
Igor Sysoev4e9393a2003-01-09 05:36:00 +00001389
Igor Sysoev1c13c662003-05-20 15:37:55 +00001390 for (m = 0; ngx_modules[m]; m++) {
Igor Sysoev6253ca12003-05-27 12:18:54 +00001391 if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
Igor Sysoev4e9393a2003-01-09 05:36:00 +00001392 continue;
1393 }
1394
Igor Sysoev6253ca12003-05-27 12:18:54 +00001395 module = ngx_modules[m]->ctx;
Igor Sysoev4e9393a2003-01-09 05:36:00 +00001396
1397 if (module->create_srv_conf) {
Igor Sysoevc1571722005-03-19 12:38:37 +00001398 mconf = module->create_srv_conf(cf);
1399 if (mconf == NULL) {
Igor Sysoevaa828612005-02-09 14:31:07 +00001400 return NGX_CONF_ERROR;
1401 }
1402
1403 ctx->srv_conf[ngx_modules[m]->ctx_index] = mconf;
Igor Sysoev4e9393a2003-01-09 05:36:00 +00001404 }
1405
1406 if (module->create_loc_conf) {
Igor Sysoevc1571722005-03-19 12:38:37 +00001407 mconf = module->create_loc_conf(cf);
1408 if (mconf == NULL) {
Igor Sysoevaa828612005-02-09 14:31:07 +00001409 return NGX_CONF_ERROR;
1410 }
1411
1412 ctx->loc_conf[ngx_modules[m]->ctx_index] = mconf;
Igor Sysoev4e9393a2003-01-09 05:36:00 +00001413 }
1414 }
1415
Igor Sysoevaa828612005-02-09 14:31:07 +00001416
1417 /* the server configuration context */
Igor Sysoeva9830112003-05-19 16:39:14 +00001418
Igor Sysoev6253ca12003-05-27 12:18:54 +00001419 cscf = ctx->srv_conf[ngx_http_core_module.ctx_index];
Igor Sysoeva9830112003-05-19 16:39:14 +00001420 cscf->ctx = ctx;
1421
Igor Sysoevaa828612005-02-09 14:31:07 +00001422
Igor Sysoev6253ca12003-05-27 12:18:54 +00001423 cmcf = ctx->main_conf[ngx_http_core_module.ctx_index];
Igor Sysoevaa828612005-02-09 14:31:07 +00001424
Igor Sysoevc1571722005-03-19 12:38:37 +00001425 cscfp = ngx_array_push(&cmcf->servers);
1426 if (cscfp == NULL) {
Igor Sysoevaa828612005-02-09 14:31:07 +00001427 return NGX_CONF_ERROR;
1428 }
1429
Igor Sysoeva9830112003-05-19 16:39:14 +00001430 *cscfp = cscf;
1431
Igor Sysoevaa828612005-02-09 14:31:07 +00001432
Igor Sysoeva9830112003-05-19 16:39:14 +00001433 /* parse inside server{} */
1434
Igor Sysoev7b190b42005-06-07 15:56:31 +00001435 pcf = *cf;
Igor Sysoev4e9393a2003-01-09 05:36:00 +00001436 cf->ctx = ctx;
Igor Sysoev79a80482003-05-14 17:13:13 +00001437 cf->cmd_type = NGX_HTTP_SRV_CONF;
Igor Sysoev805d9db2005-02-03 19:33:37 +00001438
Igor Sysoev4e9393a2003-01-09 05:36:00 +00001439 rv = ngx_conf_parse(cf, NULL);
Igor Sysoev805d9db2005-02-03 19:33:37 +00001440
Igor Sysoev7b190b42005-06-07 15:56:31 +00001441 *cf = pcf;
Igor Sysoev4e9393a2003-01-09 05:36:00 +00001442
Igor Sysoev13933252003-05-29 13:02:09 +00001443 if (rv != NGX_CONF_OK) {
1444 return rv;
1445 }
1446
Igor Sysoevd9d0ca12003-11-21 06:30:49 +00001447 ngx_qsort(cscf->locations.elts, (size_t) cscf->locations.nelts,
Igor Sysoev805d9db2005-02-03 19:33:37 +00001448 sizeof(ngx_http_core_loc_conf_t *), ngx_http_core_cmp_locations);
Igor Sysoev13933252003-05-29 13:02:09 +00001449
Igor Sysoeva9830112003-05-19 16:39:14 +00001450 return rv;
Igor Sysoev4e9393a2003-01-09 05:36:00 +00001451}
1452
1453
Igor Sysoevaa828612005-02-09 14:31:07 +00001454static char *
1455ngx_http_core_location(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy)
Igor Sysoev4e9393a2003-01-09 05:36:00 +00001456{
Igor Sysoev4e9393a2003-01-09 05:36:00 +00001457 char *rv;
Igor Sysoeva8fa0a62003-11-25 20:44:56 +00001458 ngx_int_t m;
Igor Sysoevfc5a10a2004-03-09 19:47:07 +00001459 ngx_str_t *value;
Igor Sysoev805d9db2005-02-03 19:33:37 +00001460 ngx_conf_t save;
Igor Sysoev4e9393a2003-01-09 05:36:00 +00001461 ngx_http_module_t *module;
Igor Sysoev74a5ddb2004-07-18 19:11:20 +00001462 ngx_http_conf_ctx_t *ctx, *pctx;
Igor Sysoeva9830112003-05-19 16:39:14 +00001463 ngx_http_core_srv_conf_t *cscf;
Igor Sysoev74a5ddb2004-07-18 19:11:20 +00001464 ngx_http_core_loc_conf_t *clcf, *pclcf, **clcfp;
Igor Sysoevc0edbcc2004-10-21 15:34:38 +00001465#if (NGX_PCRE)
Igor Sysoevfc5a10a2004-03-09 19:47:07 +00001466 ngx_str_t err;
Igor Sysoev10a543a2004-03-16 07:10:12 +00001467 u_char errstr[NGX_MAX_CONF_ERRSTR];
Igor Sysoevfc5a10a2004-03-09 19:47:07 +00001468#endif
Igor Sysoev4e9393a2003-01-09 05:36:00 +00001469
Igor Sysoevc1571722005-03-19 12:38:37 +00001470 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
1471 if (ctx == NULL) {
Igor Sysoeva8fa0a62003-11-25 20:44:56 +00001472 return NGX_CONF_ERROR;
1473 }
Igor Sysoev4e9393a2003-01-09 05:36:00 +00001474
Igor Sysoev74a5ddb2004-07-18 19:11:20 +00001475 pctx = cf->ctx;
1476 ctx->main_conf = pctx->main_conf;
1477 ctx->srv_conf = pctx->srv_conf;
Igor Sysoev4e9393a2003-01-09 05:36:00 +00001478
Igor Sysoeva8fa0a62003-11-25 20:44:56 +00001479 ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
1480 if (ctx->loc_conf == NULL) {
1481 return NGX_CONF_ERROR;
1482 }
Igor Sysoev4e9393a2003-01-09 05:36:00 +00001483
Igor Sysoev6253ca12003-05-27 12:18:54 +00001484 for (m = 0; ngx_modules[m]; m++) {
1485 if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
Igor Sysoev4e9393a2003-01-09 05:36:00 +00001486 continue;
1487 }
1488
Igor Sysoev6253ca12003-05-27 12:18:54 +00001489 module = ngx_modules[m]->ctx;
Igor Sysoev4e9393a2003-01-09 05:36:00 +00001490
1491 if (module->create_loc_conf) {
Igor Sysoeva8fa0a62003-11-25 20:44:56 +00001492 ctx->loc_conf[ngx_modules[m]->ctx_index] =
1493 module->create_loc_conf(cf);
1494 if (ctx->loc_conf[ngx_modules[m]->ctx_index] == NULL) {
1495 return NGX_CONF_ERROR;
1496 }
Igor Sysoev4e9393a2003-01-09 05:36:00 +00001497 }
1498 }
1499
Igor Sysoev6253ca12003-05-27 12:18:54 +00001500 clcf = ctx->loc_conf[ngx_http_core_module.ctx_index];
Igor Sysoeva9830112003-05-19 16:39:14 +00001501 clcf->loc_conf = ctx->loc_conf;
Igor Sysoev4e9393a2003-01-09 05:36:00 +00001502
Igor Sysoev3b30a902003-12-25 20:26:58 +00001503 value = cf->args->elts;
Igor Sysoeva8fa0a62003-11-25 20:44:56 +00001504
1505 if (cf->args->nelts == 3) {
1506 if (value[1].len == 1 && value[1].data[0] == '=') {
Igor Sysoev2a3f4902004-11-11 20:58:09 +00001507 clcf->name = value[2];
Igor Sysoeva8fa0a62003-11-25 20:44:56 +00001508 clcf->exact_match = 1;
1509
Igor Sysoevd43bee82004-11-20 19:52:20 +00001510 } else if (value[1].len == 2
1511 && value[1].data[0] == '^'
1512 && value[1].data[1] == '~')
1513 {
1514 clcf->name = value[2];
1515 clcf->noregex = 1;
1516
Igor Sysoeva8fa0a62003-11-25 20:44:56 +00001517 } else if ((value[1].len == 1 && value[1].data[0] == '~')
1518 || (value[1].len == 2
1519 && value[1].data[0] == '~'
1520 && value[1].data[1] == '*'))
1521 {
Igor Sysoevc0edbcc2004-10-21 15:34:38 +00001522#if (NGX_PCRE)
Igor Sysoeva8fa0a62003-11-25 20:44:56 +00001523 err.len = NGX_MAX_CONF_ERRSTR;
1524 err.data = errstr;
1525
1526 clcf->regex = ngx_regex_compile(&value[2],
1527 value[1].len == 2 ? NGX_REGEX_CASELESS: 0,
1528 cf->pool, &err);
1529
1530 if (clcf->regex == NULL) {
1531 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "%s", err.data);
1532 return NGX_CONF_ERROR;
1533 }
1534
Igor Sysoev3b30a902003-12-25 20:26:58 +00001535 clcf->name = value[2];
Igor Sysoevdc867cd2003-12-14 20:10:27 +00001536#else
1537 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
Igor Sysoev1b735832004-11-11 14:07:14 +00001538 "the using of the regex \"%V\" "
1539 "requires PCRE library", &value[2]);
Igor Sysoevdc867cd2003-12-14 20:10:27 +00001540 return NGX_CONF_ERROR;
1541#endif
Igor Sysoeva8fa0a62003-11-25 20:44:56 +00001542
1543 } else {
1544 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
Igor Sysoev1b735832004-11-11 14:07:14 +00001545 "invalid location modifier \"%V\"", &value[1]);
Igor Sysoeva8fa0a62003-11-25 20:44:56 +00001546 return NGX_CONF_ERROR;
1547 }
1548
1549 } else {
Igor Sysoev2a3f4902004-11-11 20:58:09 +00001550 clcf->name = value[1];
Igor Sysoeva8fa0a62003-11-25 20:44:56 +00001551 }
1552
Igor Sysoev74a5ddb2004-07-18 19:11:20 +00001553 pclcf = pctx->loc_conf[ngx_http_core_module.ctx_index];
1554
1555 if (pclcf->name.len == 0) {
1556 cscf = ctx->srv_conf[ngx_http_core_module.ctx_index];
Igor Sysoevc1571722005-03-19 12:38:37 +00001557
1558 clcfp = ngx_array_push(&cscf->locations);
1559 if (clcfp == NULL) {
Igor Sysoev74a5ddb2004-07-18 19:11:20 +00001560 return NGX_CONF_ERROR;
1561 }
1562
1563 } else {
Igor Sysoev805d9db2005-02-03 19:33:37 +00001564#if 0
Igor Sysoev74a5ddb2004-07-18 19:11:20 +00001565 clcf->prev_location = pclcf;
Igor Sysoev805d9db2005-02-03 19:33:37 +00001566#endif
Igor Sysoev74a5ddb2004-07-18 19:11:20 +00001567
1568 if (pclcf->exact_match) {
1569 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
Igor Sysoev1b735832004-11-11 14:07:14 +00001570 "location \"%V\" could not be inside "
1571 "the exact location \"%V\"",
1572 &clcf->name, &pclcf->name);
Igor Sysoev74a5ddb2004-07-18 19:11:20 +00001573 return NGX_CONF_ERROR;
1574 }
1575
Igor Sysoevc0edbcc2004-10-21 15:34:38 +00001576#if (NGX_PCRE)
Igor Sysoev74a5ddb2004-07-18 19:11:20 +00001577 if (clcf->regex == NULL
1578 && ngx_strncmp(clcf->name.data, pclcf->name.data, pclcf->name.len)
1579 != 0)
Igor Sysoevea521232004-07-26 16:21:18 +00001580#else
1581 if (ngx_strncmp(clcf->name.data, pclcf->name.data, pclcf->name.len)
1582 != 0)
1583#endif
Igor Sysoev74a5ddb2004-07-18 19:11:20 +00001584 {
1585 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
Igor Sysoev1b735832004-11-11 14:07:14 +00001586 "location \"%V\" is outside location \"%V\"",
1587 &clcf->name, &pclcf->name);
Igor Sysoev74a5ddb2004-07-18 19:11:20 +00001588 return NGX_CONF_ERROR;
1589 }
1590
1591 if (pclcf->locations.elts == NULL) {
Igor Sysoevc1571722005-03-19 12:38:37 +00001592 if (ngx_array_init(&pclcf->locations, cf->pool, 4, sizeof(void *))
1593 != NGX_OK)
1594 {
1595 return NGX_CONF_ERROR;
1596 }
Igor Sysoev74a5ddb2004-07-18 19:11:20 +00001597 }
1598
Igor Sysoevc1571722005-03-19 12:38:37 +00001599 clcfp = ngx_array_push(&pclcf->locations);
1600 if (clcfp == NULL) {
Igor Sysoev74a5ddb2004-07-18 19:11:20 +00001601 return NGX_CONF_ERROR;
1602 }
Igor Sysoeva8fa0a62003-11-25 20:44:56 +00001603 }
Igor Sysoev74a5ddb2004-07-18 19:11:20 +00001604
Igor Sysoeva9830112003-05-19 16:39:14 +00001605 *clcfp = clcf;
Igor Sysoev4e9393a2003-01-09 05:36:00 +00001606
Igor Sysoev805d9db2005-02-03 19:33:37 +00001607 save = *cf;
Igor Sysoev4e9393a2003-01-09 05:36:00 +00001608 cf->ctx = ctx;
Igor Sysoev79a80482003-05-14 17:13:13 +00001609 cf->cmd_type = NGX_HTTP_LOC_CONF;
Igor Sysoev805d9db2005-02-03 19:33:37 +00001610
Igor Sysoev4e9393a2003-01-09 05:36:00 +00001611 rv = ngx_conf_parse(cf, NULL);
Igor Sysoev805d9db2005-02-03 19:33:37 +00001612
1613 *cf = save;
1614
1615 if (rv != NGX_CONF_OK) {
1616 return rv;
1617 }
1618
1619 ngx_qsort(clcf->locations.elts, (size_t) clcf->locations.nelts,
1620 sizeof(ngx_http_core_loc_conf_t *), ngx_http_core_cmp_locations);
Igor Sysoev79a80482003-05-14 17:13:13 +00001621
1622 return rv;
1623}
1624
1625
Igor Sysoev4d656dc2005-03-22 16:02:46 +00001626static int ngx_libc_cdecl
Igor Sysoevaa828612005-02-09 14:31:07 +00001627ngx_http_core_cmp_locations(const void *one, const void *two)
Igor Sysoev805d9db2005-02-03 19:33:37 +00001628{
1629 ngx_int_t rc;
1630 ngx_http_core_loc_conf_t *first, *second;
1631
1632 first = *(ngx_http_core_loc_conf_t **) one;
1633 second = *(ngx_http_core_loc_conf_t **) two;
1634
1635 if (first->noname && !second->noname) {
1636 /* shift no named locations to the end */
1637 return 1;
1638 }
1639
1640 if (!first->noname && second->noname) {
1641 /* shift no named locations to the end */
1642 return -1;
1643 }
1644
1645 if (first->noname || second->noname) {
1646 /* do not sort no named locations */
1647 return 0;
1648 }
1649
1650#if (NGX_PCRE)
1651
1652 if (first->regex && !second->regex) {
1653 /* shift the regex matches to the end */
1654 return 1;
1655 }
1656
1657 if (!first->regex && second->regex) {
1658 /* shift the regex matches to the end */
1659 return -1;
1660 }
1661
1662 if (first->regex || second->regex) {
1663 /* do not sort the regex matches */
1664 return 0;
1665 }
1666
1667#endif
1668
1669 rc = ngx_strcmp(first->name.data, second->name.data);
1670
1671 if (rc == 0 && second->exact_match) {
1672 /* an exact match must be before the same inclusive one */
1673 return 1;
1674 }
1675
1676 return (int) rc;
1677}
1678
1679
Igor Sysoevaa828612005-02-09 14:31:07 +00001680static char *
1681ngx_http_core_types(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
Igor Sysoev79a80482003-05-14 17:13:13 +00001682{
Igor Sysoevaa3436c2003-05-30 14:27:59 +00001683 char *rv;
Igor Sysoev805d9db2005-02-03 19:33:37 +00001684 ngx_conf_t save;
Igor Sysoevaa3436c2003-05-30 14:27:59 +00001685
Igor Sysoev805d9db2005-02-03 19:33:37 +00001686 save = *cf;
1687 cf->handler = ngx_http_core_type;
Igor Sysoevaa3436c2003-05-30 14:27:59 +00001688 cf->handler_conf = conf;
Igor Sysoev805d9db2005-02-03 19:33:37 +00001689
Igor Sysoevaa3436c2003-05-30 14:27:59 +00001690 rv = ngx_conf_parse(cf, NULL);
Igor Sysoev805d9db2005-02-03 19:33:37 +00001691
1692 *cf = save;
Igor Sysoevaa3436c2003-05-30 14:27:59 +00001693
1694 return rv;
1695}
1696
1697
Igor Sysoevaa828612005-02-09 14:31:07 +00001698static char *
1699ngx_http_core_type(ngx_conf_t *cf, ngx_command_t *dummy, void *conf)
Igor Sysoevaa3436c2003-05-30 14:27:59 +00001700{
1701 ngx_http_core_loc_conf_t *lcf = conf;
Igor Sysoev79a80482003-05-14 17:13:13 +00001702
Igor Sysoev24025022005-12-16 15:07:08 +00001703 ngx_str_t *value, *content_type, *old;
1704 ngx_uint_t i, n;
1705 ngx_hash_key_t *type;
Igor Sysoev79a80482003-05-14 17:13:13 +00001706
1707 if (lcf->types == NULL) {
Igor Sysoev24025022005-12-16 15:07:08 +00001708 lcf->types = ngx_array_create(cf->pool, 64, sizeof(ngx_hash_key_t));
Igor Sysoevdc3b2a72004-09-14 19:39:54 +00001709 if (lcf->types == NULL) {
1710 return NGX_CONF_ERROR;
1711 }
Igor Sysoev24025022005-12-16 15:07:08 +00001712 }
Igor Sysoev79a80482003-05-14 17:13:13 +00001713
Igor Sysoev24025022005-12-16 15:07:08 +00001714 content_type = ngx_palloc(cf->pool, sizeof(ngx_str_t));
1715 if (content_type == NULL) {
1716 return NGX_CONF_ERROR;
Igor Sysoev79a80482003-05-14 17:13:13 +00001717 }
1718
Igor Sysoev805d9db2005-02-03 19:33:37 +00001719 value = cf->args->elts;
Igor Sysoev24025022005-12-16 15:07:08 +00001720 *content_type = value[0];
Igor Sysoev79a80482003-05-14 17:13:13 +00001721
1722 for (i = 1; i < cf->args->nelts; i++) {
Igor Sysoev79a80482003-05-14 17:13:13 +00001723
Igor Sysoev24025022005-12-16 15:07:08 +00001724 for (n = 0; n < value[i].len; n++) {
1725 value[i].data[n] = ngx_tolower(value[i].data[n]);
1726 }
1727
1728 type = lcf->types->elts;
1729 for (n = 0; n < lcf->types->nelts; n++) {
1730 if (ngx_strcmp(value[i].data, type[n].key.data) == 0) {
1731 old = type[n].value;
1732 type[n].value = content_type;
1733
1734 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
1735 "duplicate extention \"%V\", "
1736 "content type: \"%V\", "
1737 "old content type: \"%V\"",
1738 &value[i], content_type, old);
1739 continue;
1740 }
1741 }
1742
1743
1744 type = ngx_array_push(lcf->types);
Igor Sysoevc1571722005-03-19 12:38:37 +00001745 if (type == NULL) {
Igor Sysoevdc3b2a72004-09-14 19:39:54 +00001746 return NGX_CONF_ERROR;
1747 }
1748
Igor Sysoev24025022005-12-16 15:07:08 +00001749 type->key = value[i];
1750 type->key_hash = ngx_hash_key(value[i].data, value[i].len);
1751 type->value = content_type;
Igor Sysoev79a80482003-05-14 17:13:13 +00001752 }
1753
1754 return NGX_CONF_OK;
1755}
1756
1757
Igor Sysoev899b44e2005-05-12 14:58:06 +00001758static ngx_int_t
1759ngx_http_core_preconfiguration(ngx_conf_t *cf)
1760{
1761 return ngx_http_variables_add_core_vars(cf);
1762}
1763
1764
Igor Sysoevaa828612005-02-09 14:31:07 +00001765static void *
1766ngx_http_core_create_main_conf(ngx_conf_t *cf)
Igor Sysoev4e9393a2003-01-09 05:36:00 +00001767{
Igor Sysoev805d9db2005-02-03 19:33:37 +00001768 ngx_http_core_main_conf_t *cmcf;
Igor Sysoev4e9393a2003-01-09 05:36:00 +00001769
Igor Sysoevc1571722005-03-19 12:38:37 +00001770 cmcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_core_main_conf_t));
1771 if (cmcf == NULL) {
Igor Sysoev805d9db2005-02-03 19:33:37 +00001772 return NGX_CONF_ERROR;
1773 }
Igor Sysoev4e9393a2003-01-09 05:36:00 +00001774
Igor Sysoev8184d1b2005-03-04 14:06:57 +00001775 if (ngx_array_init(&cmcf->servers, cf->pool, 4,
Igor Sysoev9ac946b2005-10-24 15:09:41 +00001776 sizeof(ngx_http_core_srv_conf_t *))
1777 == NGX_ERROR)
Igor Sysoev805d9db2005-02-03 19:33:37 +00001778 {
1779 return NGX_CONF_ERROR;
1780 }
Igor Sysoev4e9393a2003-01-09 05:36:00 +00001781
Igor Sysoev305a9d82005-12-26 17:07:48 +00001782 cmcf->server_names_hash_max_size = NGX_CONF_UNSET_UINT;
1783 cmcf->server_names_hash_bucket_size = NGX_CONF_UNSET_UINT;
Igor Sysoevb1dfe472004-12-21 12:30:30 +00001784
Igor Sysoevffe71442006-02-08 15:33:12 +00001785 cmcf->variables_hash_max_size = NGX_CONF_UNSET_UINT;
1786 cmcf->variables_hash_bucket_size = NGX_CONF_UNSET_UINT;
1787
Igor Sysoeva9830112003-05-19 16:39:14 +00001788 return cmcf;
Igor Sysoev4e9393a2003-01-09 05:36:00 +00001789}
1790
1791
Igor Sysoevaa828612005-02-09 14:31:07 +00001792static char *
1793ngx_http_core_init_main_conf(ngx_conf_t *cf, void *conf)
Igor Sysoev4e9393a2003-01-09 05:36:00 +00001794{
Igor Sysoev6253ca12003-05-27 12:18:54 +00001795 ngx_http_core_main_conf_t *cmcf = conf;
Igor Sysoeva9830112003-05-19 16:39:14 +00001796
Igor Sysoev305a9d82005-12-26 17:07:48 +00001797 if (cmcf->server_names_hash_max_size == NGX_CONF_UNSET_UINT) {
1798 cmcf->server_names_hash_max_size = 512;
Igor Sysoevb1dfe472004-12-21 12:30:30 +00001799 }
1800
Igor Sysoev305a9d82005-12-26 17:07:48 +00001801 if (cmcf->server_names_hash_bucket_size == NGX_CONF_UNSET_UINT) {
1802 cmcf->server_names_hash_bucket_size = ngx_cacheline_size;
Igor Sysoevb1dfe472004-12-21 12:30:30 +00001803 }
Igor Sysoeva9830112003-05-19 16:39:14 +00001804
Igor Sysoev305a9d82005-12-26 17:07:48 +00001805 cmcf->server_names_hash_bucket_size =
1806 ngx_align(cmcf->server_names_hash_bucket_size, ngx_cacheline_size);
1807
Igor Sysoevffe71442006-02-08 15:33:12 +00001808
1809 if (cmcf->variables_hash_max_size == NGX_CONF_UNSET_UINT) {
1810 cmcf->variables_hash_max_size = 512;
1811 }
1812
1813 if (cmcf->variables_hash_bucket_size == NGX_CONF_UNSET_UINT) {
1814 cmcf->variables_hash_bucket_size = 64;
1815 }
1816
1817 cmcf->variables_hash_bucket_size =
1818 ngx_align(cmcf->variables_hash_bucket_size, ngx_cacheline_size);
1819
Igor Sysoeva9830112003-05-19 16:39:14 +00001820 return NGX_CONF_OK;
1821}
1822
1823
Igor Sysoevaa828612005-02-09 14:31:07 +00001824static void *
1825ngx_http_core_create_srv_conf(ngx_conf_t *cf)
Igor Sysoeva9830112003-05-19 16:39:14 +00001826{
1827 ngx_http_core_srv_conf_t *cscf;
1828
Igor Sysoevc1571722005-03-19 12:38:37 +00001829 cscf = ngx_pcalloc(cf->pool, sizeof(ngx_http_core_srv_conf_t));
1830 if (cscf == NULL) {
Igor Sysoevaa828612005-02-09 14:31:07 +00001831 return NGX_CONF_ERROR;
1832 }
1833
Igor Sysoev85080d02004-09-22 16:18:21 +00001834 /*
Igor Sysoev02025fd2005-01-18 13:03:58 +00001835 * set by ngx_pcalloc():
Igor Sysoevaa828612005-02-09 14:31:07 +00001836 *
Igor Sysoev02025fd2005-01-18 13:03:58 +00001837 * conf->client_large_buffers.num = 0;
1838 */
Igor Sysoev85080d02004-09-22 16:18:21 +00001839
Igor Sysoev8184d1b2005-03-04 14:06:57 +00001840 if (ngx_array_init(&cscf->locations, cf->pool, 4, sizeof(void *))
Igor Sysoev305a9d82005-12-26 17:07:48 +00001841 == NGX_ERROR)
Igor Sysoevaa828612005-02-09 14:31:07 +00001842 {
1843 return NGX_CONF_ERROR;
1844 }
1845
Igor Sysoev8184d1b2005-03-04 14:06:57 +00001846 if (ngx_array_init(&cscf->listen, cf->pool, 4, sizeof(ngx_http_listen_t))
Igor Sysoev305a9d82005-12-26 17:07:48 +00001847 == NGX_ERROR)
Igor Sysoevaa828612005-02-09 14:31:07 +00001848 {
1849 return NGX_CONF_ERROR;
1850 }
1851
Igor Sysoev8184d1b2005-03-04 14:06:57 +00001852 if (ngx_array_init(&cscf->server_names, cf->pool, 4,
Igor Sysoev305a9d82005-12-26 17:07:48 +00001853 sizeof(ngx_http_server_name_t))
1854 == NGX_ERROR)
Igor Sysoevaa828612005-02-09 14:31:07 +00001855 {
1856 return NGX_CONF_ERROR;
1857 }
Igor Sysoeva9830112003-05-19 16:39:14 +00001858
Igor Sysoev10a543a2004-03-16 07:10:12 +00001859 cscf->connection_pool_size = NGX_CONF_UNSET_SIZE;
Igor Sysoev10a543a2004-03-16 07:10:12 +00001860 cscf->request_pool_size = NGX_CONF_UNSET_SIZE;
1861 cscf->client_header_timeout = NGX_CONF_UNSET_MSEC;
1862 cscf->client_header_buffer_size = NGX_CONF_UNSET_SIZE;
Igor Sysoev8290d282006-02-03 12:58:48 +00001863 cscf->optimize_server_names = NGX_CONF_UNSET;
Igor Sysoev3362b8d2005-05-14 18:42:03 +00001864 cscf->ignore_invalid_headers = NGX_CONF_UNSET;
Igor Sysoev187fcd82003-05-23 11:53:01 +00001865
Igor Sysoeva9830112003-05-19 16:39:14 +00001866 return cscf;
1867}
1868
1869
Igor Sysoevaa828612005-02-09 14:31:07 +00001870static char *
1871ngx_http_core_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
Igor Sysoeva9830112003-05-19 16:39:14 +00001872{
Igor Sysoevaa3436c2003-05-30 14:27:59 +00001873 ngx_http_core_srv_conf_t *prev = parent;
1874 ngx_http_core_srv_conf_t *conf = child;
Igor Sysoev4e9393a2003-01-09 05:36:00 +00001875
Igor Sysoev305a9d82005-12-26 17:07:48 +00001876 ngx_http_listen_t *ls;
1877 ngx_http_server_name_t *sn;
Igor Sysoev4e9393a2003-01-09 05:36:00 +00001878
Igor Sysoev7578ec92003-06-02 15:24:30 +00001879 /* TODO: it does not merge, it inits only */
Igor Sysoeva9830112003-05-19 16:39:14 +00001880
1881 if (conf->listen.nelts == 0) {
Igor Sysoevc1571722005-03-19 12:38:37 +00001882 ls = ngx_array_push(&conf->listen);
1883 if (ls == NULL) {
Igor Sysoevaa828612005-02-09 14:31:07 +00001884 return NGX_CONF_ERROR;
1885 }
1886
Igor Sysoevb145b062005-06-15 18:33:41 +00001887 ngx_memzero(ls, sizeof(ngx_http_listen_t));
1888
Igor Sysoevaa828612005-02-09 14:31:07 +00001889 ls->addr = INADDR_ANY;
Igor Sysoev1b735832004-11-11 14:07:14 +00001890#if (NGX_WIN32)
Igor Sysoevaa828612005-02-09 14:31:07 +00001891 ls->port = 80;
Igor Sysoev1c104622003-06-03 15:42:58 +00001892#else
Igor Sysoev7578ec92003-06-02 15:24:30 +00001893 /* STUB: getuid() should be cached */
Igor Sysoevaa828612005-02-09 14:31:07 +00001894 ls->port = (getuid() == 0) ? 80 : 8000;
Igor Sysoev1c104622003-06-03 15:42:58 +00001895#endif
Igor Sysoevaa828612005-02-09 14:31:07 +00001896 ls->family = AF_INET;
Igor Sysoev9ac946b2005-10-24 15:09:41 +00001897
1898 ls->conf.backlog = -1;
1899 ls->conf.rcvbuf = -1;
1900 ls->conf.sndbuf = -1;
Igor Sysoev4e9393a2003-01-09 05:36:00 +00001901 }
1902
Igor Sysoev9e580192006-02-01 18:22:15 +00001903 if (conf->server_name.data == NULL) {
1904 conf->server_name.data = ngx_palloc(cf->pool, NGX_MAXHOSTNAMELEN);
1905 if (conf->server_name.data == NULL) {
Igor Sysoevaa828612005-02-09 14:31:07 +00001906 return NGX_CONF_ERROR;
1907 }
Igor Sysoev13933252003-05-29 13:02:09 +00001908
Igor Sysoev9e580192006-02-01 18:22:15 +00001909 if (gethostname((char *) conf->server_name.data, NGX_MAXHOSTNAMELEN)
1910 == -1)
1911 {
Igor Sysoev8e1fbe62003-07-18 14:44:05 +00001912 ngx_conf_log_error(NGX_LOG_EMERG, cf, ngx_errno,
1913 "gethostname() failed");
1914 return NGX_CONF_ERROR;
Igor Sysoevad22e012003-01-15 07:02:27 +00001915 }
Igor Sysoev13933252003-05-29 13:02:09 +00001916
Igor Sysoev9e580192006-02-01 18:22:15 +00001917 conf->server_name.len = ngx_strlen(conf->server_name.data);
1918
1919 sn = ngx_array_push(&conf->server_names);
1920 if (sn == NULL) {
1921 return NGX_CONF_ERROR;
1922 }
1923
1924 sn->name.len = conf->server_name.len;
1925 sn->name.data = conf->server_name.data;
Igor Sysoevaa828612005-02-09 14:31:07 +00001926 sn->core_srv_conf = conf;
Igor Sysoevad22e012003-01-15 07:02:27 +00001927 }
1928
Igor Sysoev239baac2003-06-11 15:28:34 +00001929 ngx_conf_merge_size_value(conf->connection_pool_size,
Igor Sysoev8035fd22004-10-01 15:53:53 +00001930 prev->connection_pool_size, 256);
Igor Sysoev187fcd82003-05-23 11:53:01 +00001931 ngx_conf_merge_size_value(conf->request_pool_size,
Igor Sysoev8035fd22004-10-01 15:53:53 +00001932 prev->request_pool_size, 4096);
Igor Sysoev187fcd82003-05-23 11:53:01 +00001933 ngx_conf_merge_msec_value(conf->client_header_timeout,
1934 prev->client_header_timeout, 60000);
1935 ngx_conf_merge_size_value(conf->client_header_buffer_size,
1936 prev->client_header_buffer_size, 1024);
Igor Sysoevf7abd722004-09-23 06:32:00 +00001937 ngx_conf_merge_bufs_value(conf->large_client_header_buffers,
1938 prev->large_client_header_buffers,
1939 4, ngx_pagesize);
1940
1941 if (conf->large_client_header_buffers.size < conf->connection_pool_size) {
1942 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1943 "the \"large_client_header_buffers\" size must be "
1944 "equal to or bigger than \"connection_pool_size\"");
1945 return NGX_CONF_ERROR;
1946 }
1947
Igor Sysoev8290d282006-02-03 12:58:48 +00001948 ngx_conf_merge_value(conf->optimize_server_names,
1949 prev->optimize_server_names, 1);
Igor Sysoev34303462006-01-24 16:08:27 +00001950
Igor Sysoev3362b8d2005-05-14 18:42:03 +00001951 ngx_conf_merge_value(conf->ignore_invalid_headers,
1952 prev->ignore_invalid_headers, 1);
1953
Igor Sysoev4e9393a2003-01-09 05:36:00 +00001954 return NGX_CONF_OK;
1955}
1956
1957
Igor Sysoevaa828612005-02-09 14:31:07 +00001958static void *
1959ngx_http_core_create_loc_conf(ngx_conf_t *cf)
Igor Sysoev4e9393a2003-01-09 05:36:00 +00001960{
Igor Sysoev805d9db2005-02-03 19:33:37 +00001961 ngx_http_core_loc_conf_t *lcf;
Igor Sysoev4e9393a2003-01-09 05:36:00 +00001962
Igor Sysoevc1571722005-03-19 12:38:37 +00001963 lcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_core_loc_conf_t));
1964 if (lcf == NULL) {
Igor Sysoevaa828612005-02-09 14:31:07 +00001965 return NGX_CONF_ERROR;
1966 }
Igor Sysoev4e9393a2003-01-09 05:36:00 +00001967
Igor Sysoev02025fd2005-01-18 13:03:58 +00001968 /*
1969 * set by ngx_pcalloc():
1970 *
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001971 * lcf->root = { 0, NULL };
Igor Sysoev94e32ce2006-04-07 14:08:04 +00001972 * lcf->limit_except = 0;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001973 * lcf->post_action = { 0, NULL };
Igor Sysoev02025fd2005-01-18 13:03:58 +00001974 * lcf->types = NULL;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001975 * lcf->default_type = { 0, NULL };
Igor Sysoev02025fd2005-01-18 13:03:58 +00001976 * lcf->err_log = NULL;
1977 * lcf->error_pages = NULL;
1978 * lcf->client_body_path = NULL;
1979 * lcf->regex = NULL;
1980 * lcf->exact_match = 0;
1981 * lcf->auto_redirect = 0;
1982 * lcf->alias = 0;
1983 */
Igor Sysoev4e9393a2003-01-09 05:36:00 +00001984
Igor Sysoevae02c192004-03-19 05:25:53 +00001985 lcf->client_max_body_size = NGX_CONF_UNSET_SIZE;
Igor Sysoevdbb27762004-04-01 16:20:53 +00001986 lcf->client_body_buffer_size = NGX_CONF_UNSET_SIZE;
Igor Sysoev10a543a2004-03-16 07:10:12 +00001987 lcf->client_body_timeout = NGX_CONF_UNSET_MSEC;
Igor Sysoev31eb8c02005-09-23 11:02:22 +00001988 lcf->satisfy_any = NGX_CONF_UNSET;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001989 lcf->internal = NGX_CONF_UNSET;
Igor Sysoev5bf3d252003-10-22 07:05:29 +00001990 lcf->sendfile = NGX_CONF_UNSET;
Igor Sysoev3c3ca172004-01-05 20:55:48 +00001991 lcf->tcp_nopush = NGX_CONF_UNSET;
Igor Sysoev924bd792004-10-11 15:07:03 +00001992 lcf->tcp_nodelay = NGX_CONF_UNSET;
Igor Sysoev10a543a2004-03-16 07:10:12 +00001993 lcf->send_timeout = NGX_CONF_UNSET_MSEC;
1994 lcf->send_lowat = NGX_CONF_UNSET_SIZE;
Igor Sysoev7823cc32004-07-14 16:01:42 +00001995 lcf->postpone_output = NGX_CONF_UNSET_SIZE;
1996 lcf->limit_rate = NGX_CONF_UNSET_SIZE;
Igor Sysoev10a543a2004-03-16 07:10:12 +00001997 lcf->keepalive_timeout = NGX_CONF_UNSET_MSEC;
Igor Sysoev307c3ad2004-09-17 16:07:35 +00001998 lcf->keepalive_header = NGX_CONF_UNSET;
Igor Sysoev10a543a2004-03-16 07:10:12 +00001999 lcf->lingering_time = NGX_CONF_UNSET_MSEC;
2000 lcf->lingering_timeout = NGX_CONF_UNSET_MSEC;
Igor Sysoev0ab91b92004-06-06 19:49:18 +00002001 lcf->reset_timedout_connection = NGX_CONF_UNSET;
Igor Sysoev7b190b42005-06-07 15:56:31 +00002002 lcf->port_in_redirect = NGX_CONF_UNSET;
Igor Sysoev12b4b002003-10-24 06:53:41 +00002003 lcf->msie_padding = NGX_CONF_UNSET;
Igor Sysoev5192b362005-07-08 14:34:20 +00002004 lcf->log_not_found = NGX_CONF_UNSET;
Igor Sysoev24025022005-12-16 15:07:08 +00002005 lcf->types_hash_max_size = NGX_CONF_UNSET_UINT;
2006 lcf->types_hash_bucket_size = NGX_CONF_UNSET_UINT;
Igor Sysoev12b4b002003-10-24 06:53:41 +00002007
Igor Sysoev4e9393a2003-01-09 05:36:00 +00002008 return lcf;
2009}
Igor Sysoeva19a85e2003-01-28 15:56:37 +00002010
Igor Sysoev79a80482003-05-14 17:13:13 +00002011
Igor Sysoev24025022005-12-16 15:07:08 +00002012static ngx_str_t ngx_http_core_text_html_type = ngx_string("text/html");
2013static ngx_str_t ngx_http_core_image_gif_type = ngx_string("image/gif");
2014static ngx_str_t ngx_http_core_image_jpeg_type = ngx_string("image/jpeg");
2015
2016static ngx_hash_key_t ngx_http_core_default_types[] = {
2017 { ngx_string("html"), 0, &ngx_http_core_text_html_type },
2018 { ngx_string("gif"), 0, &ngx_http_core_image_gif_type },
2019 { ngx_string("jpg"), 0, &ngx_http_core_image_jpeg_type },
2020 { ngx_null_string, 0, NULL }
Igor Sysoev79a80482003-05-14 17:13:13 +00002021};
2022
2023
Igor Sysoevaa828612005-02-09 14:31:07 +00002024static char *
Igor Sysoev24025022005-12-16 15:07:08 +00002025ngx_http_core_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
Igor Sysoeve2a31542003-04-08 15:40:10 +00002026{
Igor Sysoevaa3436c2003-05-30 14:27:59 +00002027 ngx_http_core_loc_conf_t *prev = parent;
2028 ngx_http_core_loc_conf_t *conf = child;
Igor Sysoev79a80482003-05-14 17:13:13 +00002029
Igor Sysoevaa828612005-02-09 14:31:07 +00002030 ngx_uint_t i;
Igor Sysoev24025022005-12-16 15:07:08 +00002031 ngx_hash_key_t *type;
2032 ngx_hash_init_t types_hash;
Igor Sysoev79a80482003-05-14 17:13:13 +00002033
Igor Sysoev34303462006-01-24 16:08:27 +00002034 if (conf->root.data == NULL) {
Igor Sysoev79a80482003-05-14 17:13:13 +00002035
Igor Sysoev455a7fc2006-03-21 08:20:41 +00002036 conf->alias = prev->alias;
Igor Sysoev34303462006-01-24 16:08:27 +00002037 conf->root = prev->root;
2038 conf->root_lengths = prev->root_lengths;
2039 conf->root_values = prev->root_values;
2040
2041 if (prev->root.data == NULL) {
2042 conf->root.len = sizeof("html") - 1;
2043 conf->root.data = (u_char *) "html";
2044
2045 if (ngx_conf_full_name(cf->cycle, &conf->root) == NGX_ERROR) {
2046 return NGX_CONF_ERROR;
2047 }
2048 }
Igor Sysoev6d2a14a2004-09-27 16:03:21 +00002049 }
2050
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00002051 if (conf->post_action.data == NULL) {
2052 conf->post_action = prev->post_action;
2053 }
2054
Igor Sysoev24025022005-12-16 15:07:08 +00002055 ngx_conf_merge_unsigned_value(conf->types_hash_max_size,
Igor Sysoev3ca233e2005-12-28 14:23:52 +00002056 prev->types_hash_max_size, 1024);
Igor Sysoev79a80482003-05-14 17:13:13 +00002057
Igor Sysoev24025022005-12-16 15:07:08 +00002058 ngx_conf_merge_unsigned_value(conf->types_hash_bucket_size,
2059 prev->types_hash_bucket_size,
2060 ngx_cacheline_size);
2061
2062 conf->types_hash_bucket_size = ngx_align(conf->types_hash_bucket_size,
2063 ngx_cacheline_size);
2064
2065 /*
2066 * the special handling the "types" directive in the "http" section
2067 * to inherit the http's conf->types_hash to all servers
2068 */
2069
2070 if (prev->types && prev->types_hash.buckets == NULL) {
2071
2072 types_hash.hash = &prev->types_hash;
2073 types_hash.key = ngx_hash_key_lc;
2074 types_hash.max_size = conf->types_hash_max_size;
2075 types_hash.bucket_size = conf->types_hash_bucket_size;
Igor Sysoev3ca233e2005-12-28 14:23:52 +00002076 types_hash.name = "types_hash";
Igor Sysoev24025022005-12-16 15:07:08 +00002077 types_hash.pool = cf->pool;
2078 types_hash.temp_pool = NULL;
2079
2080 if (ngx_hash_init(&types_hash, prev->types->elts, prev->types->nelts)
2081 != NGX_OK)
2082 {
2083 return NGX_CONF_ERROR;
2084 }
2085 }
2086
2087 if (conf->types == NULL) {
2088 conf->types = prev->types;
2089 conf->types_hash = prev->types_hash;
2090 }
2091
2092 if (conf->types == NULL) {
2093 conf->types = ngx_array_create(cf->pool, 4, sizeof(ngx_hash_key_t));
2094 if (conf->types == NULL) {
2095 return NGX_CONF_ERROR;
2096 }
2097
2098 for (i = 0; ngx_http_core_default_types[i].key.len; i++) {
2099 type = ngx_array_push(conf->types);
2100 if (type == NULL) {
Igor Sysoevaa828612005-02-09 14:31:07 +00002101 return NGX_CONF_ERROR;
Igor Sysoev79a80482003-05-14 17:13:13 +00002102 }
2103
Igor Sysoev24025022005-12-16 15:07:08 +00002104 type->key = ngx_http_core_default_types[i].key;
2105 type->key_hash =
2106 ngx_hash_key_lc(ngx_http_core_default_types[i].key.data,
2107 ngx_http_core_default_types[i].key.len);
2108 type->value = ngx_http_core_default_types[i].value;
2109 }
2110 }
Igor Sysoev79a80482003-05-14 17:13:13 +00002111
Igor Sysoev24025022005-12-16 15:07:08 +00002112 if (conf->types_hash.buckets == NULL) {
Igor Sysoevaa828612005-02-09 14:31:07 +00002113
Igor Sysoev24025022005-12-16 15:07:08 +00002114 types_hash.hash = &conf->types_hash;
2115 types_hash.key = ngx_hash_key_lc;
2116 types_hash.max_size = conf->types_hash_max_size;
2117 types_hash.bucket_size = conf->types_hash_bucket_size;
Igor Sysoev3ca233e2005-12-28 14:23:52 +00002118 types_hash.name = "mime_types_hash";
Igor Sysoev24025022005-12-16 15:07:08 +00002119 types_hash.pool = cf->pool;
2120 types_hash.temp_pool = NULL;
Igor Sysoevaa828612005-02-09 14:31:07 +00002121
Igor Sysoev24025022005-12-16 15:07:08 +00002122 if (ngx_hash_init(&types_hash, conf->types->elts, conf->types->nelts)
2123 != NGX_OK)
2124 {
2125 return NGX_CONF_ERROR;
Igor Sysoev79a80482003-05-14 17:13:13 +00002126 }
2127 }
2128
Igor Sysoev890fc962003-07-20 21:15:59 +00002129 if (conf->err_log == NULL) {
2130 if (prev->err_log) {
2131 conf->err_log = prev->err_log;
2132 } else {
Igor Sysoev630ad0c2004-04-16 05:14:16 +00002133 conf->err_log = cf->cycle->new_log;
Igor Sysoev890fc962003-07-20 21:15:59 +00002134 }
2135 }
2136
Igor Sysoev74e95c22003-11-09 20:03:38 +00002137 if (conf->error_pages == NULL && prev->error_pages) {
2138 conf->error_pages = prev->error_pages;
2139 }
2140
Igor Sysoev6253ca12003-05-27 12:18:54 +00002141 ngx_conf_merge_str_value(conf->default_type,
Igor Sysoevaa828612005-02-09 14:31:07 +00002142 prev->default_type, "text/plain");
Igor Sysoev6253ca12003-05-27 12:18:54 +00002143
Igor Sysoevae02c192004-03-19 05:25:53 +00002144 ngx_conf_merge_size_value(conf->client_max_body_size,
Igor Sysoev8035fd22004-10-01 15:53:53 +00002145 prev->client_max_body_size, 1 * 1024 * 1024);
Igor Sysoevdbb27762004-04-01 16:20:53 +00002146 ngx_conf_merge_size_value(conf->client_body_buffer_size,
Igor Sysoev8035fd22004-10-01 15:53:53 +00002147 prev->client_body_buffer_size,
2148 (size_t) 2 * ngx_pagesize);
Igor Sysoev2b0c76c2003-10-27 21:01:00 +00002149 ngx_conf_merge_msec_value(conf->client_body_timeout,
Igor Sysoev7af6b162004-02-09 07:46:43 +00002150 prev->client_body_timeout, 60000);
Igor Sysoev899b44e2005-05-12 14:58:06 +00002151
Igor Sysoev31eb8c02005-09-23 11:02:22 +00002152 ngx_conf_merge_value(conf->satisfy_any, prev->satisfy_any, 0);
Igor Sysoev899b44e2005-05-12 14:58:06 +00002153 ngx_conf_merge_value(conf->internal, prev->internal, 0);
Igor Sysoev5bf3d252003-10-22 07:05:29 +00002154 ngx_conf_merge_value(conf->sendfile, prev->sendfile, 0);
Igor Sysoev3c3ca172004-01-05 20:55:48 +00002155 ngx_conf_merge_value(conf->tcp_nopush, prev->tcp_nopush, 0);
Igor Sysoev924bd792004-10-11 15:07:03 +00002156 ngx_conf_merge_value(conf->tcp_nodelay, prev->tcp_nodelay, 0);
Igor Sysoev899b44e2005-05-12 14:58:06 +00002157
Igor Sysoev7af6b162004-02-09 07:46:43 +00002158 ngx_conf_merge_msec_value(conf->send_timeout, prev->send_timeout, 60000);
Igor Sysoevb5faed22003-10-29 08:30:44 +00002159 ngx_conf_merge_size_value(conf->send_lowat, prev->send_lowat, 0);
Igor Sysoev7823cc32004-07-14 16:01:42 +00002160 ngx_conf_merge_size_value(conf->postpone_output, prev->postpone_output,
2161 1460);
2162 ngx_conf_merge_size_value(conf->limit_rate, prev->limit_rate, 0);
Igor Sysoev187fcd82003-05-23 11:53:01 +00002163 ngx_conf_merge_msec_value(conf->keepalive_timeout,
Igor Sysoev307c3ad2004-09-17 16:07:35 +00002164 prev->keepalive_timeout, 75000);
2165 ngx_conf_merge_sec_value(conf->keepalive_header,
2166 prev->keepalive_header, 0);
Igor Sysoev187fcd82003-05-23 11:53:01 +00002167 ngx_conf_merge_msec_value(conf->lingering_time,
2168 prev->lingering_time, 30000);
2169 ngx_conf_merge_msec_value(conf->lingering_timeout,
2170 prev->lingering_timeout, 5000);
Igor Sysoev79a80482003-05-14 17:13:13 +00002171
Igor Sysoev02025fd2005-01-18 13:03:58 +00002172 ngx_conf_merge_path_value(conf->client_body_temp_path,
2173 prev->client_body_temp_path,
2174 NGX_HTTP_CLIENT_TEMP_PATH, 0, 0, 0,
2175 ngx_garbage_collector_temp_handler, cf);
2176
Igor Sysoev0ab91b92004-06-06 19:49:18 +00002177 ngx_conf_merge_value(conf->reset_timedout_connection,
Igor Sysoevaa828612005-02-09 14:31:07 +00002178 prev->reset_timedout_connection, 0);
Igor Sysoev7b190b42005-06-07 15:56:31 +00002179 ngx_conf_merge_value(conf->port_in_redirect, prev->port_in_redirect, 1);
Igor Sysoev12b4b002003-10-24 06:53:41 +00002180 ngx_conf_merge_value(conf->msie_padding, prev->msie_padding, 1);
Igor Sysoev5192b362005-07-08 14:34:20 +00002181 ngx_conf_merge_value(conf->log_not_found, prev->log_not_found, 1);
Igor Sysoev12b4b002003-10-24 06:53:41 +00002182
Igor Sysoev865c1502003-11-30 20:03:18 +00002183 if (conf->open_files == NULL) {
2184 conf->open_files = prev->open_files;
2185 }
2186
Igor Sysoeve2a31542003-04-08 15:40:10 +00002187 return NGX_CONF_OK;
2188}
2189
Igor Sysoev79a80482003-05-14 17:13:13 +00002190
Igor Sysoevb145b062005-06-15 18:33:41 +00002191/* AF_INET only */
2192
Igor Sysoevaa828612005-02-09 14:31:07 +00002193static char *
2194ngx_http_core_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
Igor Sysoeva19a85e2003-01-28 15:56:37 +00002195{
Igor Sysoevaa3436c2003-05-30 14:27:59 +00002196 ngx_http_core_srv_conf_t *scf = conf;
Igor Sysoeva9830112003-05-19 16:39:14 +00002197
Igor Sysoevb145b062005-06-15 18:33:41 +00002198 char *err;
Igor Sysoevc2068d02005-10-19 12:33:58 +00002199 ngx_str_t *value, size;
Igor Sysoevb145b062005-06-15 18:33:41 +00002200 ngx_uint_t n;
2201 struct hostent *h;
2202 ngx_http_listen_t *ls;
2203 ngx_inet_upstream_t inet_upstream;
Igor Sysoeva19a85e2003-01-28 15:56:37 +00002204
Igor Sysoeve2ff3ea2004-09-14 15:55:24 +00002205 /*
2206 * TODO: check duplicate 'listen' directives,
2207 * add resolved name to server names ???
2208 */
Igor Sysoev6ddfbf02003-05-15 15:42:53 +00002209
Igor Sysoevb145b062005-06-15 18:33:41 +00002210 value = cf->args->elts;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00002211
Igor Sysoevb145b062005-06-15 18:33:41 +00002212 ngx_memzero(&inet_upstream, sizeof(ngx_inet_upstream_t));
2213
2214 inet_upstream.url = value[1];
2215 inet_upstream.port_only = 1;
2216
2217 err = ngx_inet_parse_host_port(&inet_upstream);
2218
2219 if (err) {
2220 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2221 "%s in \"%V\" of the \"listen\" directive",
2222 err, &inet_upstream.url);
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00002223 return NGX_CONF_ERROR;
Igor Sysoevb145b062005-06-15 18:33:41 +00002224 }
2225
Igor Sysoevc1571722005-03-19 12:38:37 +00002226 ls = ngx_array_push(&scf->listen);
2227 if (ls == NULL) {
Igor Sysoeve2ff3ea2004-09-14 15:55:24 +00002228 return NGX_CONF_ERROR;
2229 }
Igor Sysoeva19a85e2003-01-28 15:56:37 +00002230
Igor Sysoevb145b062005-06-15 18:33:41 +00002231 ngx_memzero(ls, sizeof(ngx_http_listen_t));
Igor Sysoeva19a85e2003-01-28 15:56:37 +00002232
2233 ls->family = AF_INET;
Igor Sysoevb145b062005-06-15 18:33:41 +00002234 ls->port = (in_port_t) (inet_upstream.default_port ?
Igor Sysoevffe71442006-02-08 15:33:12 +00002235 80 : inet_upstream.port);
Igor Sysoevfe5cb6b2003-01-29 07:25:51 +00002236 ls->file_name = cf->conf_file->file.name;
Igor Sysoeva19a85e2003-01-28 15:56:37 +00002237 ls->line = cf->conf_file->line;
Igor Sysoevb145b062005-06-15 18:33:41 +00002238 ls->conf.backlog = -1;
Igor Sysoevc2068d02005-10-19 12:33:58 +00002239 ls->conf.rcvbuf = -1;
2240 ls->conf.sndbuf = -1;
Igor Sysoeva19a85e2003-01-28 15:56:37 +00002241
Igor Sysoev13c68742006-03-10 12:51:52 +00002242 if (inet_upstream.host.len == 1 && inet_upstream.host.data[0] == '*') {
2243 inet_upstream.host.len = 0;
2244 }
2245
Igor Sysoevb145b062005-06-15 18:33:41 +00002246 if (inet_upstream.host.len) {
2247 inet_upstream.host.data[inet_upstream.host.len] = '\0';
Igor Sysoeva19a85e2003-01-28 15:56:37 +00002248
Igor Sysoevb145b062005-06-15 18:33:41 +00002249 ls->addr = inet_addr((const char *) inet_upstream.host.data);
2250
2251 if (ls->addr == INADDR_NONE) {
2252 h = gethostbyname((const char *) inet_upstream.host.data);
2253
2254 if (h == NULL || h->h_addr_list[0] == NULL) {
2255 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2256 "can not resolve host \"%s\" "
2257 "in the \"listen\" directive",
2258 inet_upstream.host.data);
2259 return NGX_CONF_ERROR;
2260 }
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00002261
Igor Sysoevb145b062005-06-15 18:33:41 +00002262 ls->addr = *(in_addr_t *)(h->h_addr_list[0]);
Igor Sysoev6ddfbf02003-05-15 15:42:53 +00002263 }
Igor Sysoeva19a85e2003-01-28 15:56:37 +00002264
Igor Sysoev236e0452004-09-23 16:39:34 +00002265 } else {
Igor Sysoevb145b062005-06-15 18:33:41 +00002266 ls->addr = INADDR_ANY;
Igor Sysoev236e0452004-09-23 16:39:34 +00002267 }
Igor Sysoevf1602632004-09-21 19:55:00 +00002268
Igor Sysoev94e32ce2006-04-07 14:08:04 +00002269 n = ngx_inet_ntop(AF_INET, &ls->addr, ls->conf.addr, INET_ADDRSTRLEN + 6);
2270 ngx_sprintf(&ls->conf.addr[n], ":%ui", ls->port);
2271
Igor Sysoevb145b062005-06-15 18:33:41 +00002272 if (cf->args->nelts == 2) {
2273 return NGX_CONF_OK;
2274 }
Igor Sysoevaa828612005-02-09 14:31:07 +00002275
Igor Sysoevb145b062005-06-15 18:33:41 +00002276 if (ngx_strcmp(value[2].data, "default") == 0) {
2277 ls->conf.default_server = 1;
2278 n = 3;
Igor Sysoev24025022005-12-16 15:07:08 +00002279
Igor Sysoevb145b062005-06-15 18:33:41 +00002280 } else {
2281 n = 2;
2282 }
Igor Sysoev13933252003-05-29 13:02:09 +00002283
Igor Sysoevb145b062005-06-15 18:33:41 +00002284 for ( /* void */ ; n < cf->args->nelts; n++) {
2285
2286 if (ls->conf.default_server == 0) {
Igor Sysoev8e1fbe62003-07-18 14:44:05 +00002287 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
Igor Sysoevb145b062005-06-15 18:33:41 +00002288 "\"%V\" parameter can be specified for "
2289 "the default \"listen\" directive only",
2290 &value[n]);
Igor Sysoev8e1fbe62003-07-18 14:44:05 +00002291 return NGX_CONF_ERROR;
Igor Sysoev13933252003-05-29 13:02:09 +00002292 }
2293
Igor Sysoevb145b062005-06-15 18:33:41 +00002294 if (ngx_strcmp(value[n].data, "bind") == 0) {
2295 ls->conf.bind = 1;
2296 continue;
2297 }
2298
Igor Sysoevc2068d02005-10-19 12:33:58 +00002299 if (ngx_strncmp(value[n].data, "backlog=", 8) == 0) {
2300 ls->conf.backlog = ngx_atoi(value[n].data + 8, value[n].len - 8);
Igor Sysoevb145b062005-06-15 18:33:41 +00002301 ls->conf.bind = 1;
2302
2303 if (ls->conf.backlog == NGX_ERROR || ls->conf.backlog == 0) {
2304 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2305 "invalid backlog \"%V\"", &value[n]);
2306 return NGX_CONF_ERROR;
2307 }
2308
2309 continue;
2310 }
2311
Igor Sysoevc2068d02005-10-19 12:33:58 +00002312 if (ngx_strncmp(value[n].data, "rcvbuf=", 7) == 0) {
2313 size.len = value[n].len - 7;
2314 size.data = value[n].data + 7;
2315
2316 ls->conf.rcvbuf = ngx_parse_size(&size);
2317 ls->conf.bind = 1;
2318
2319 if (ls->conf.rcvbuf == NGX_ERROR) {
2320 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2321 "invalid rcvbuf \"%V\"", &value[n]);
2322 return NGX_CONF_ERROR;
2323 }
2324
2325 continue;
2326 }
2327
2328 if (ngx_strncmp(value[n].data, "sndbuf=", 7) == 0) {
2329 size.len = value[n].len - 7;
2330 size.data = value[n].data + 7;
2331
2332 ls->conf.sndbuf = ngx_parse_size(&size);
2333 ls->conf.bind = 1;
2334
2335 if (ls->conf.sndbuf == NGX_ERROR) {
2336 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2337 "invalid sndbuf \"%V\"", &value[n]);
2338 return NGX_CONF_ERROR;
2339 }
2340
2341 continue;
2342 }
2343
2344 if (ngx_strncmp(value[n].data, "accept_filter=", 14) == 0) {
Igor Sysoevb145b062005-06-15 18:33:41 +00002345#if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
Igor Sysoevc2068d02005-10-19 12:33:58 +00002346 ls->conf.accept_filter = (char *) &value[n].data[14];
Igor Sysoevb145b062005-06-15 18:33:41 +00002347 ls->conf.bind = 1;
2348#else
2349 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2350 "accept filters \"%V\" are not supported "
2351 "on this platform, ignored",
2352 &value[n]);
2353#endif
2354 continue;
2355 }
2356
2357 if (ngx_strcmp(value[n].data, "deferred") == 0) {
2358#if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)
2359 ls->conf.deferred_accept = 1;
2360 ls->conf.bind = 1;
2361#else
2362 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2363 "the deferred accept is not supported "
2364 "on this platform, ignored");
2365#endif
2366 continue;
2367 }
2368
2369 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2370 "the invalid \"%V\" parameter", &value[n]);
2371 return NGX_CONF_ERROR;
Igor Sysoev13933252003-05-29 13:02:09 +00002372 }
2373
2374 return NGX_CONF_OK;
2375}
2376
2377
Igor Sysoevaa828612005-02-09 14:31:07 +00002378static char *
2379ngx_http_core_server_name(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
Igor Sysoev13933252003-05-29 13:02:09 +00002380{
Igor Sysoev305a9d82005-12-26 17:07:48 +00002381 ngx_http_core_srv_conf_t *cscf = conf;
Igor Sysoev13933252003-05-29 13:02:09 +00002382
Igor Sysoev305a9d82005-12-26 17:07:48 +00002383 u_char ch;
2384 ngx_str_t *value, name;
2385 ngx_uint_t i;
2386 ngx_http_server_name_t *sn;
Igor Sysoevb1af9bb2004-06-25 14:42:03 +00002387
Igor Sysoevaa3436c2003-05-30 14:27:59 +00002388 value = cf->args->elts;
Igor Sysoev13933252003-05-29 13:02:09 +00002389
Igor Sysoev305a9d82005-12-26 17:07:48 +00002390 ch = value[1].data[0];
2391
2392 if (cscf->server_name.data == NULL && value[1].len) {
2393 if (ch == '*') {
Igor Sysoev8e1fbe62003-07-18 14:44:05 +00002394 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
Igor Sysoev305a9d82005-12-26 17:07:48 +00002395 "first server name \"%V\" must not be wildcard",
2396 &value[1]);
Igor Sysoev8e1fbe62003-07-18 14:44:05 +00002397 return NGX_CONF_ERROR;
Igor Sysoevaa3436c2003-05-30 14:27:59 +00002398 }
Igor Sysoev13933252003-05-29 13:02:09 +00002399
Igor Sysoev305a9d82005-12-26 17:07:48 +00002400 name = value[1];
2401
2402 if (ch == '.') {
2403 name.len--;
2404 name.data++;
2405 }
2406
2407 cscf->server_name.len = name.len;
2408 cscf->server_name.data = ngx_pstrdup(cf->pool, &name);
2409 if (cscf->server_name.data == NULL) {
2410 return NGX_CONF_ERROR;
2411 }
2412 }
2413
2414 for (i = 1; i < cf->args->nelts; i++) {
2415
2416 ch = value[i].data[0];
2417
2418 if (value[i].len == 0
2419 || (ch == '*' && (value[i].len < 3 || value[i].data[1] != '.'))
2420 || (ch == '.' && value[i].len < 2))
2421 {
2422 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2423 "server name \"%V\" is invalid", &value[i]);
2424 return NGX_CONF_ERROR;
2425 }
2426
2427 sn = ngx_array_push(&cscf->server_names);
Igor Sysoevc1571722005-03-19 12:38:37 +00002428 if (sn == NULL) {
Igor Sysoevc0edbcc2004-10-21 15:34:38 +00002429 return NGX_CONF_ERROR;
2430 }
Igor Sysoevaa3436c2003-05-30 14:27:59 +00002431
2432 sn->name.len = value[i].len;
2433 sn->name.data = value[i].data;
Igor Sysoev305a9d82005-12-26 17:07:48 +00002434 sn->core_srv_conf = cscf;
Igor Sysoevaa3436c2003-05-30 14:27:59 +00002435 }
Igor Sysoev13933252003-05-29 13:02:09 +00002436
Igor Sysoeva19a85e2003-01-28 15:56:37 +00002437 return NGX_CONF_OK;
2438}
Igor Sysoev9d639522003-07-07 06:11:50 +00002439
2440
Igor Sysoevaa828612005-02-09 14:31:07 +00002441static char *
2442ngx_http_core_root(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
Igor Sysoev10a543a2004-03-16 07:10:12 +00002443{
2444 ngx_http_core_loc_conf_t *lcf = conf;
2445
Igor Sysoev3ca233e2005-12-28 14:23:52 +00002446 ngx_str_t *value;
2447 ngx_uint_t alias, n;
2448 ngx_http_script_compile_t sc;
Igor Sysoev10a543a2004-03-16 07:10:12 +00002449
2450 alias = (cmd->name.len == sizeof("alias") - 1) ? 1 : 0;
2451
2452 if (lcf->root.data) {
Igor Sysoeva741f8d2004-03-30 20:31:58 +00002453
2454 /* the (ngx_uint_t) cast is required by gcc 2.7.2.3 */
2455
2456 if ((ngx_uint_t) lcf->alias == alias) {
Igor Sysoev10a543a2004-03-16 07:10:12 +00002457 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
Igor Sysoev1b735832004-11-11 14:07:14 +00002458 "\"%V\" directive is duplicate",
2459 &cmd->name);
Igor Sysoev10a543a2004-03-16 07:10:12 +00002460 } else {
2461 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
Igor Sysoev1b735832004-11-11 14:07:14 +00002462 "\"%V\" directive is duplicate, "
Igor Sysoev10a543a2004-03-16 07:10:12 +00002463 "\"%s\" directive is specified before",
Igor Sysoev1b735832004-11-11 14:07:14 +00002464 &cmd->name, lcf->alias ? "alias" : "root");
Igor Sysoev10a543a2004-03-16 07:10:12 +00002465 }
2466
2467 return NGX_CONF_ERROR;
2468 }
2469
2470 value = cf->args->elts;
2471
2472 lcf->alias = alias;
2473 lcf->root = value[1];
2474
Igor Sysoev71057632004-08-30 19:24:51 +00002475 if (!alias && lcf->root.data[lcf->root.len - 1] == '/') {
2476 lcf->root.len--;
2477 }
2478
Igor Sysoev34303462006-01-24 16:08:27 +00002479 if (ngx_conf_full_name(cf->cycle, &lcf->root) == NGX_ERROR) {
2480 return NGX_CONF_ERROR;
2481 }
2482
2483 n = ngx_http_script_variables_count(&lcf->root);
Igor Sysoev3ca233e2005-12-28 14:23:52 +00002484
2485 if (n == 0) {
2486 return NGX_CONF_OK;
2487 }
2488
2489 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
2490
2491 sc.cf = cf;
Igor Sysoev34303462006-01-24 16:08:27 +00002492 sc.source = &lcf->root;
Igor Sysoev3ca233e2005-12-28 14:23:52 +00002493 sc.lengths = &lcf->root_lengths;
2494 sc.values = &lcf->root_values;
2495 sc.variables = n;
2496 sc.complete_lengths = 1;
2497 sc.complete_values = 1;
2498
2499 if (ngx_http_script_compile(&sc) != NGX_OK) {
2500 return NGX_CONF_ERROR;
2501 }
2502
Igor Sysoev10a543a2004-03-16 07:10:12 +00002503 return NGX_CONF_OK;
2504}
2505
2506
Igor Sysoev94e32ce2006-04-07 14:08:04 +00002507static ngx_http_method_name_t ngx_methods_names[] = {
2508 { "GET", (uint32_t) ~NGX_HTTP_GET },
2509 { "HEAD", (uint32_t) ~NGX_HTTP_HEAD },
2510 { NULL, 0 }
2511};
2512
2513
2514static char *
2515ngx_http_core_limit_except(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
2516{
2517 ngx_http_core_loc_conf_t *clcf = conf;
2518
2519 char *rv;
2520 void *mconf;
2521 ngx_str_t *value;
2522 ngx_uint_t i;
2523 ngx_conf_t save;
2524 ngx_http_module_t *module;
2525 ngx_http_conf_ctx_t *ctx, *pctx;
2526 ngx_http_method_name_t *name;
2527 ngx_http_core_loc_conf_t *lcf, **clcfp;
2528
2529 if (clcf->limit_except) {
2530 return "duplicate";
2531 }
2532
2533 clcf->limit_except = 0xffffffff;
2534
2535 value = cf->args->elts;
2536
2537 for (i = 1; i < cf->args->nelts; i++) {
2538 for (name = ngx_methods_names; name->name; name++) {
2539
2540 if (ngx_strcasecmp(value[i].data, name->name) == 0) {
2541 clcf->limit_except &= name->method;
2542 goto next;
2543 }
2544 }
2545
2546 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2547 "invalid method \"%V\"", &value[i]);
2548 return NGX_CONF_ERROR;
2549
2550 next:
2551 continue;
2552 }
2553
2554 if (!(clcf->limit_except & NGX_HTTP_GET)) {
2555 clcf->limit_except &= (uint32_t) ~NGX_HTTP_HEAD;
2556 }
2557
2558 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
2559 if (ctx == NULL) {
2560 return NGX_CONF_ERROR;
2561 }
2562
2563 pctx = cf->ctx;
2564 ctx->main_conf = pctx->main_conf;
2565 ctx->srv_conf = pctx->srv_conf;
2566
2567 ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
2568 if (ctx->loc_conf == NULL) {
2569 return NGX_CONF_ERROR;
2570 }
2571
2572 for (i = 0; ngx_modules[i]; i++) {
2573 if (ngx_modules[i]->type != NGX_HTTP_MODULE) {
2574 continue;
2575 }
2576
2577 module = ngx_modules[i]->ctx;
2578
2579 if (module->create_loc_conf) {
2580
2581 mconf = module->create_loc_conf(cf);
2582 if (mconf == NULL) {
2583 return NGX_CONF_ERROR;
2584 }
2585
2586 ctx->loc_conf[ngx_modules[i]->ctx_index] = mconf;
2587 }
2588 }
2589
2590
2591 lcf = ctx->loc_conf[ngx_http_core_module.ctx_index];
2592 clcf->limit_except_loc_conf = ctx->loc_conf;
2593 lcf->loc_conf = ctx->loc_conf;
2594 lcf->name = clcf->name;
2595 lcf->noname = 1;
2596
2597 if (clcf->locations.elts == NULL) {
2598 if (ngx_array_init(&clcf->locations, cf->pool, 4, sizeof(void *))
2599 == NGX_ERROR)
2600 {
2601 return NGX_CONF_ERROR;
2602 }
2603 }
2604
2605 clcfp = ngx_array_push(&clcf->locations);
2606 if (clcfp == NULL) {
2607 return NGX_CONF_ERROR;
2608 }
2609
2610 *clcfp = lcf;
2611
2612
2613 save = *cf;
2614 cf->ctx = ctx;
2615 cf->cmd_type = NGX_HTTP_LMT_CONF;
2616
2617 rv = ngx_conf_parse(cf, NULL);
2618
2619 *cf = save;
2620
2621 return rv;
2622}
2623
2624
Igor Sysoevaa828612005-02-09 14:31:07 +00002625static char *
2626ngx_http_core_error_page(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
Igor Sysoev74e95c22003-11-09 20:03:38 +00002627{
2628 ngx_http_core_loc_conf_t *lcf = conf;
2629
Igor Sysoevaa828612005-02-09 14:31:07 +00002630 ngx_int_t overwrite;
Igor Sysoev732a2712004-04-21 18:54:33 +00002631 ngx_uint_t i, n;
Igor Sysoev74e95c22003-11-09 20:03:38 +00002632 ngx_str_t *value;
2633 ngx_http_err_page_t *err;
2634
2635 if (lcf->error_pages == NULL) {
Igor Sysoevaa828612005-02-09 14:31:07 +00002636 lcf->error_pages = ngx_array_create(cf->pool, 4,
Igor Sysoev74e95c22003-11-09 20:03:38 +00002637 sizeof(ngx_http_err_page_t));
2638 if (lcf->error_pages == NULL) {
2639 return NGX_CONF_ERROR;
2640 }
2641 }
2642
2643 value = cf->args->elts;
2644
Igor Sysoev732a2712004-04-21 18:54:33 +00002645 i = cf->args->nelts - 2;
2646
2647 if (value[i].data[0] == '=') {
2648 if (i == 1) {
2649 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
Igor Sysoev1b735832004-11-11 14:07:14 +00002650 "invalid value \"%V\"", &value[i]);
Igor Sysoev732a2712004-04-21 18:54:33 +00002651 return NGX_CONF_ERROR;
2652 }
2653
Igor Sysoeva2573672005-10-05 14:46:21 +00002654 if (value[i].len > 1) {
2655 overwrite = ngx_atoi(&value[i].data[1], value[i].len - 1);
Igor Sysoev732a2712004-04-21 18:54:33 +00002656
Igor Sysoeva2573672005-10-05 14:46:21 +00002657 if (overwrite == NGX_ERROR) {
2658 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2659 "invalid value \"%V\"", &value[i]);
2660 return NGX_CONF_ERROR;
2661 }
2662
2663 } else {
2664 overwrite = 0;
Igor Sysoev732a2712004-04-21 18:54:33 +00002665 }
2666
2667 n = 2;
2668
2669 } else {
Igor Sysoeva2573672005-10-05 14:46:21 +00002670 overwrite = -1;
Igor Sysoev732a2712004-04-21 18:54:33 +00002671 n = 1;
2672 }
2673
2674 for (i = 1; i < cf->args->nelts - n; i++) {
Igor Sysoevc1571722005-03-19 12:38:37 +00002675 err = ngx_array_push(lcf->error_pages);
2676 if (err == NULL) {
Igor Sysoev732a2712004-04-21 18:54:33 +00002677 return NGX_CONF_ERROR;
2678 }
2679
Igor Sysoev3f4685f2004-04-25 20:13:21 +00002680 err->status = ngx_atoi(value[i].data, value[i].len);
Igor Sysoevaa828612005-02-09 14:31:07 +00002681
Igor Sysoev3f4685f2004-04-25 20:13:21 +00002682 if (err->status == NGX_ERROR) {
Igor Sysoev74e95c22003-11-09 20:03:38 +00002683 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
Igor Sysoev1b735832004-11-11 14:07:14 +00002684 "invalid value \"%V\"", &value[i]);
Igor Sysoev74e95c22003-11-09 20:03:38 +00002685 return NGX_CONF_ERROR;
2686 }
2687
Igor Sysoev3f4685f2004-04-25 20:13:21 +00002688 if (err->status < 400 || err->status > 599) {
Igor Sysoev74e95c22003-11-09 20:03:38 +00002689 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
Igor Sysoev1b735832004-11-11 14:07:14 +00002690 "value \"%V\" must be between 400 and 599",
2691 &value[i]);
Igor Sysoev74e95c22003-11-09 20:03:38 +00002692 return NGX_CONF_ERROR;
2693 }
2694
Igor Sysoeva2573672005-10-05 14:46:21 +00002695 err->overwrite = (overwrite >= 0) ? overwrite : err->status;
2696
Igor Sysoev74e95c22003-11-09 20:03:38 +00002697 err->uri = value[cf->args->nelts - 1];
2698 }
2699
2700 return NGX_CONF_OK;
2701}
2702
2703
Igor Sysoevaa828612005-02-09 14:31:07 +00002704static char *
Igor Sysoev899b44e2005-05-12 14:58:06 +00002705ngx_http_core_error_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
2706{
2707 ngx_http_core_loc_conf_t *lcf = conf;
2708
2709 lcf->err_log = ngx_log_create_errlog(cf->cycle, cf->args);
2710 if (lcf->err_log == NULL) {
2711 return NGX_CONF_ERROR;
2712 }
2713
2714 return ngx_set_error_log_levels(cf, lcf->err_log);
2715}
2716
2717
2718static char *
Igor Sysoevaa828612005-02-09 14:31:07 +00002719ngx_http_core_keepalive(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
Igor Sysoev307c3ad2004-09-17 16:07:35 +00002720{
2721 ngx_http_core_loc_conf_t *lcf = conf;
2722
2723 ngx_str_t *value;
2724
2725 if (lcf->keepalive_timeout != NGX_CONF_UNSET_MSEC) {
2726 return "is duplicate";
2727 }
2728
2729 value = cf->args->elts;
2730
2731 lcf->keepalive_timeout = ngx_parse_time(&value[1], 0);
Igor Sysoevaa828612005-02-09 14:31:07 +00002732
Igor Sysoev307c3ad2004-09-17 16:07:35 +00002733 if (lcf->keepalive_timeout == (ngx_msec_t) NGX_ERROR) {
2734 return "invalid value";
2735 }
2736
2737 if (lcf->keepalive_timeout == (ngx_msec_t) NGX_PARSE_LARGE_TIME) {
2738 return "value must be less than 597 hours";
2739 }
2740
2741 if (cf->args->nelts == 2) {
2742 return NGX_CONF_OK;
2743 }
2744
2745 lcf->keepalive_header = ngx_parse_time(&value[2], 1);
Igor Sysoevaa828612005-02-09 14:31:07 +00002746
Igor Sysoev307c3ad2004-09-17 16:07:35 +00002747 if (lcf->keepalive_header == NGX_ERROR) {
2748 return "invalid value";
2749 }
2750
2751 if (lcf->keepalive_header == NGX_PARSE_LARGE_TIME) {
2752 return "value must be less than 68 years";
2753 }
2754
2755 return NGX_CONF_OK;
2756}
2757
2758
Igor Sysoevaa828612005-02-09 14:31:07 +00002759static char *
Igor Sysoev899b44e2005-05-12 14:58:06 +00002760ngx_http_core_internal(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
Igor Sysoev9d639522003-07-07 06:11:50 +00002761{
2762 ngx_http_core_loc_conf_t *lcf = conf;
2763
Igor Sysoev899b44e2005-05-12 14:58:06 +00002764 if (lcf->internal != NGX_CONF_UNSET) {
2765 return "is duplicate";
Igor Sysoev03420a62004-01-20 20:40:08 +00002766 }
Igor Sysoev9d639522003-07-07 06:11:50 +00002767
Igor Sysoev899b44e2005-05-12 14:58:06 +00002768 lcf->internal = 1;
2769
2770 return NGX_CONF_OK;
Igor Sysoev9d639522003-07-07 06:11:50 +00002771}
Igor Sysoevb5faed22003-10-29 08:30:44 +00002772
2773
Igor Sysoevaa828612005-02-09 14:31:07 +00002774static char *
2775ngx_http_core_lowat_check(ngx_conf_t *cf, void *post, void *data)
Igor Sysoevb5faed22003-10-29 08:30:44 +00002776{
Igor Sysoevc0edbcc2004-10-21 15:34:38 +00002777#if (NGX_FREEBSD)
Igor Sysoev42b12b32004-12-02 18:40:46 +00002778 ssize_t *np = data;
Igor Sysoev924bd792004-10-11 15:07:03 +00002779
Igor Sysoevb5faed22003-10-29 08:30:44 +00002780 if (*np >= ngx_freebsd_net_inet_tcp_sendspace) {
2781 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2782 "\"send_lowat\" must be less than %d "
2783 "(sysctl net.inet.tcp.sendspace)",
2784 ngx_freebsd_net_inet_tcp_sendspace);
2785
2786 return NGX_CONF_ERROR;
2787 }
2788
Igor Sysoevf6906042004-11-25 16:17:31 +00002789#elif !(NGX_HAVE_SO_SNDLOWAT)
Igor Sysoev42b12b32004-12-02 18:40:46 +00002790 ssize_t *np = data;
Igor Sysoevb5faed22003-10-29 08:30:44 +00002791
2792 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
2793 "\"send_lowat\" is not supported, ignored");
2794
Igor Sysoev924bd792004-10-11 15:07:03 +00002795 *np = 0;
2796
Igor Sysoevb5faed22003-10-29 08:30:44 +00002797#endif
2798
2799 return NGX_CONF_OK;
2800}