Mercurial > hg > nginx-quic
annotate src/http/modules/ngx_http_fastcgi_module.c @ 788:ff16489f1eac
downgrade gravity of getsockname() errors
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Wed, 18 Oct 2006 14:27:16 +0000 |
parents | 4ab852b691f5 |
children | 4d68c486fcb0 |
rev | line source |
---|---|
479 | 1 |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4 */ | |
5 | |
6 | |
7 #include <ngx_config.h> | |
8 #include <ngx_core.h> | |
9 #include <ngx_http.h> | |
10 #include <nginx.h> | |
11 | |
12 | |
13 typedef struct { | |
651 | 14 ngx_http_upstream_conf_t upstream; |
15 | |
16 ngx_http_upstream_srv_conf_t *upstream_peers; | |
17 ngx_peers_t *peers0; | |
18 | |
19 ngx_str_t index; | |
20 | |
21 ngx_array_t *flushes; | |
22 ngx_array_t *params_len; | |
23 ngx_array_t *params; | |
24 ngx_array_t *params_source; | |
479 | 25 } ngx_http_fastcgi_loc_conf_t; |
26 | |
27 | |
28 typedef enum { | |
29 ngx_http_fastcgi_st_version = 0, | |
30 ngx_http_fastcgi_st_type, | |
31 ngx_http_fastcgi_st_request_id_hi, | |
32 ngx_http_fastcgi_st_request_id_lo, | |
33 ngx_http_fastcgi_st_content_length_hi, | |
34 ngx_http_fastcgi_st_content_length_lo, | |
35 ngx_http_fastcgi_st_padding_length, | |
36 ngx_http_fastcgi_st_reserved, | |
37 ngx_http_fastcgi_st_data, | |
515 | 38 ngx_http_fastcgi_st_padding |
479 | 39 } ngx_http_fastcgi_state_e; |
40 | |
41 | |
42 typedef struct { | |
651 | 43 ngx_http_fastcgi_state_e state; |
44 u_char *pos; | |
45 u_char *last; | |
46 ngx_uint_t type; | |
47 size_t length; | |
48 size_t padding; | |
49 | |
50 ngx_uint_t fastcgi_stdout; | |
479 | 51 } ngx_http_fastcgi_ctx_t; |
52 | |
53 | |
54 #define NGX_HTTP_FASTCGI_RESPONDER 1 | |
55 | |
56 #define NGX_HTTP_FASTCGI_BEGIN_REQUEST 1 | |
57 #define NGX_HTTP_FASTCGI_ABORT_REQUEST 2 | |
58 #define NGX_HTTP_FASTCGI_END_REQUEST 3 | |
59 #define NGX_HTTP_FASTCGI_PARAMS 4 | |
60 #define NGX_HTTP_FASTCGI_STDIN 5 | |
61 #define NGX_HTTP_FASTCGI_STDOUT 6 | |
62 #define NGX_HTTP_FASTCGI_STDERR 7 | |
63 #define NGX_HTTP_FASTCGI_DATA 8 | |
64 | |
65 | |
66 typedef struct { | |
67 u_char version; | |
68 u_char type; | |
69 u_char request_id_hi; | |
70 u_char request_id_lo; | |
71 u_char content_length_hi; | |
72 u_char content_length_lo; | |
73 u_char padding_length; | |
74 u_char reserved; | |
75 } ngx_http_fastcgi_header_t; | |
76 | |
77 | |
78 typedef struct { | |
79 u_char role_hi; | |
80 u_char role_lo; | |
81 u_char flags; | |
82 u_char reserved[5]; | |
83 } ngx_http_fastcgi_begin_request_t; | |
84 | |
85 | |
509 | 86 typedef struct { |
87 u_char version; | |
88 u_char type; | |
89 u_char request_id_hi; | |
90 u_char request_id_lo; | |
91 } ngx_http_fastcgi_header_small_t; | |
92 | |
93 | |
94 typedef struct { | |
95 ngx_http_fastcgi_header_t h0; | |
96 ngx_http_fastcgi_begin_request_t br; | |
97 ngx_http_fastcgi_header_small_t h1; | |
98 } ngx_http_fastcgi_request_start_t; | |
99 | |
100 | |
479 | 101 static ngx_int_t ngx_http_fastcgi_create_request(ngx_http_request_t *r); |
102 static ngx_int_t ngx_http_fastcgi_reinit_request(ngx_http_request_t *r); | |
103 static ngx_int_t ngx_http_fastcgi_process_header(ngx_http_request_t *r); | |
104 static ngx_int_t ngx_http_fastcgi_input_filter(ngx_event_pipe_t *p, | |
487 | 105 ngx_buf_t *buf); |
479 | 106 static ngx_int_t ngx_http_fastcgi_process_record(ngx_http_request_t *r, |
487 | 107 ngx_http_fastcgi_ctx_t *f); |
479 | 108 static void ngx_http_fastcgi_abort_request(ngx_http_request_t *r); |
109 static void ngx_http_fastcgi_finalize_request(ngx_http_request_t *r, | |
487 | 110 ngx_int_t rc); |
479 | 111 |
509 | 112 static ngx_int_t ngx_http_fastcgi_add_variables(ngx_conf_t *cf); |
113 static void *ngx_http_fastcgi_create_loc_conf(ngx_conf_t *cf); | |
114 static char *ngx_http_fastcgi_merge_loc_conf(ngx_conf_t *cf, | |
115 void *parent, void *child); | |
573 | 116 static ngx_int_t ngx_http_fastcgi_script_name_variable(ngx_http_request_t *r, |
117 ngx_http_variable_value_t *v, uintptr_t data); | |
509 | 118 |
479 | 119 static char *ngx_http_fastcgi_pass(ngx_conf_t *cf, ngx_command_t *cmd, |
487 | 120 void *conf); |
479 | 121 static char *ngx_http_fastcgi_lowat_check(ngx_conf_t *cf, void *post, |
487 | 122 void *data); |
479 | 123 |
124 | |
509 | 125 static ngx_http_fastcgi_request_start_t ngx_http_fastcgi_request_start = { |
126 { 1, /* version */ | |
127 NGX_HTTP_FASTCGI_BEGIN_REQUEST, /* type */ | |
128 0, /* request_id_hi */ | |
129 1, /* request_id_lo */ | |
130 0, /* content_length_hi */ | |
131 sizeof(ngx_http_fastcgi_begin_request_t), /* content_length_lo */ | |
132 0, /* padding_length */ | |
133 0 }, /* reserved */ | |
134 | |
135 { 0, /* role_hi */ | |
136 NGX_HTTP_FASTCGI_RESPONDER, /* role_lo */ | |
137 0, /* NGX_HTTP_FASTCGI_KEEP_CONN */ /* flags */ | |
138 { 0, 0, 0, 0, 0 } }, /* reserved[5] */ | |
139 | |
140 { 1, /* version */ | |
141 NGX_HTTP_FASTCGI_PARAMS, /* type */ | |
142 0, /* request_id_hi */ | |
143 1 }, /* request_id_lo */ | |
144 | |
145 }; | |
146 | |
147 | |
148 static ngx_str_t ngx_http_fastcgi_script_name = | |
149 ngx_string("fastcgi_script_name"); | |
479 | 150 |
151 | |
509 | 152 static ngx_conf_post_t ngx_http_fastcgi_lowat_post = |
153 { ngx_http_fastcgi_lowat_check }; | |
479 | 154 |
581 | 155 static ngx_conf_deprecated_t ngx_conf_deprecated_fastcgi_header_buffer_size = { |
156 ngx_conf_deprecated, "fastcgi_header_buffer_size", "fastcgi_buffer_size" | |
157 }; | |
158 | |
657 | 159 static ngx_conf_deprecated_t ngx_conf_deprecated_fastcgi_redirect_errors = { |
160 ngx_conf_deprecated, "fastcgi_redirect_errors", "fastcgi_intercept_errors" | |
161 }; | |
162 | |
479 | 163 |
164 static ngx_conf_bitmask_t ngx_http_fastcgi_next_upstream_masks[] = { | |
165 { ngx_string("error"), NGX_HTTP_UPSTREAM_FT_ERROR }, | |
166 { ngx_string("timeout"), NGX_HTTP_UPSTREAM_FT_TIMEOUT }, | |
167 { ngx_string("invalid_header"), NGX_HTTP_UPSTREAM_FT_INVALID_HEADER }, | |
168 { ngx_string("http_500"), NGX_HTTP_UPSTREAM_FT_HTTP_500 }, | |
623 | 169 { ngx_string("http_503"), NGX_HTTP_UPSTREAM_FT_HTTP_503 }, |
479 | 170 { ngx_string("http_404"), NGX_HTTP_UPSTREAM_FT_HTTP_404 }, |
665 | 171 { ngx_string("off"), NGX_HTTP_UPSTREAM_FT_OFF }, |
479 | 172 { ngx_null_string, 0 } |
173 }; | |
174 | |
175 | |
176 static ngx_command_t ngx_http_fastcgi_commands[] = { | |
177 | |
178 { ngx_string("fastcgi_pass"), | |
629 | 179 NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1, |
479 | 180 ngx_http_fastcgi_pass, |
181 NGX_HTTP_LOC_CONF_OFFSET, | |
182 0, | |
183 NULL }, | |
184 | |
185 { ngx_string("fastcgi_index"), | |
186 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
187 ngx_conf_set_str_slot, | |
188 NGX_HTTP_LOC_CONF_OFFSET, | |
189 offsetof(ngx_http_fastcgi_loc_conf_t, index), | |
190 NULL }, | |
191 | |
629 | 192 { ngx_string("fastcgi_ignore_client_abort"), |
193 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
194 ngx_conf_set_flag_slot, | |
195 NGX_HTTP_LOC_CONF_OFFSET, | |
196 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.ignore_client_abort), | |
197 NULL }, | |
198 | |
479 | 199 { ngx_string("fastcgi_connect_timeout"), |
200 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
201 ngx_conf_set_msec_slot, | |
202 NGX_HTTP_LOC_CONF_OFFSET, | |
203 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.connect_timeout), | |
204 NULL }, | |
205 | |
206 { ngx_string("fastcgi_send_timeout"), | |
207 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
208 ngx_conf_set_msec_slot, | |
209 NGX_HTTP_LOC_CONF_OFFSET, | |
210 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.send_timeout), | |
211 NULL }, | |
212 | |
213 { ngx_string("fastcgi_send_lowat"), | |
214 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
215 ngx_conf_set_size_slot, | |
216 NGX_HTTP_LOC_CONF_OFFSET, | |
217 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.send_lowat), | |
218 &ngx_http_fastcgi_lowat_post }, | |
219 | |
581 | 220 { ngx_string("fastcgi_buffer_size"), |
221 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
222 ngx_conf_set_size_slot, | |
223 NGX_HTTP_LOC_CONF_OFFSET, | |
224 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.buffer_size), | |
225 NULL }, | |
226 | |
479 | 227 { ngx_string("fastcgi_header_buffer_size"), |
228 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
229 ngx_conf_set_size_slot, | |
230 NGX_HTTP_LOC_CONF_OFFSET, | |
581 | 231 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.buffer_size), |
232 &ngx_conf_deprecated_fastcgi_header_buffer_size }, | |
479 | 233 |
509 | 234 { ngx_string("fastcgi_pass_request_headers"), |
235 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
236 ngx_conf_set_flag_slot, | |
237 NGX_HTTP_LOC_CONF_OFFSET, | |
238 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.pass_request_headers), | |
239 NULL }, | |
240 | |
241 { ngx_string("fastcgi_pass_request_body"), | |
242 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
243 ngx_conf_set_flag_slot, | |
244 NGX_HTTP_LOC_CONF_OFFSET, | |
245 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.pass_request_body), | |
246 NULL }, | |
247 | |
657 | 248 { ngx_string("fastcgi_intercept_errors"), |
249 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
250 ngx_conf_set_flag_slot, | |
251 NGX_HTTP_LOC_CONF_OFFSET, | |
252 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.intercept_errors), | |
253 NULL }, | |
254 | |
487 | 255 { ngx_string("fastcgi_redirect_errors"), |
256 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
257 ngx_conf_set_flag_slot, | |
258 NGX_HTTP_LOC_CONF_OFFSET, | |
657 | 259 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.intercept_errors), |
260 &ngx_conf_deprecated_fastcgi_redirect_errors }, | |
487 | 261 |
479 | 262 { ngx_string("fastcgi_read_timeout"), |
263 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
264 ngx_conf_set_msec_slot, | |
265 NGX_HTTP_LOC_CONF_OFFSET, | |
266 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.read_timeout), | |
267 NULL }, | |
268 | |
269 { ngx_string("fastcgi_buffers"), | |
270 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2, | |
271 ngx_conf_set_bufs_slot, | |
272 NGX_HTTP_LOC_CONF_OFFSET, | |
273 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.bufs), | |
274 NULL }, | |
275 | |
276 { ngx_string("fastcgi_busy_buffers_size"), | |
277 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
278 ngx_conf_set_size_slot, | |
279 NGX_HTTP_LOC_CONF_OFFSET, | |
529 | 280 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.busy_buffers_size_conf), |
479 | 281 NULL }, |
282 | |
283 { ngx_string("fastcgi_temp_path"), | |
284 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1234, | |
285 ngx_conf_set_path_slot, | |
286 NGX_HTTP_LOC_CONF_OFFSET, | |
287 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.temp_path), | |
288 (void *) ngx_garbage_collector_temp_handler }, | |
289 | |
290 { ngx_string("fastcgi_max_temp_file_size"), | |
291 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
292 ngx_conf_set_size_slot, | |
293 NGX_HTTP_LOC_CONF_OFFSET, | |
529 | 294 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.max_temp_file_size_conf), |
479 | 295 NULL }, |
296 | |
297 { ngx_string("fastcgi_temp_file_write_size"), | |
298 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
299 ngx_conf_set_size_slot, | |
300 NGX_HTTP_LOC_CONF_OFFSET, | |
529 | 301 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.temp_file_write_size_conf), |
479 | 302 NULL }, |
303 | |
304 { ngx_string("fastcgi_next_upstream"), | |
547 | 305 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE, |
479 | 306 ngx_conf_set_bitmask_slot, |
307 NGX_HTTP_LOC_CONF_OFFSET, | |
308 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.next_upstream), | |
309 &ngx_http_fastcgi_next_upstream_masks }, | |
310 | |
561 | 311 { ngx_string("fastcgi_upstream_max_fails"), |
312 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
313 ngx_conf_set_num_slot, | |
314 NGX_HTTP_LOC_CONF_OFFSET, | |
315 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.max_fails), | |
316 NULL }, | |
317 | |
318 { ngx_string("fastcgi_upstream_fail_timeout"), | |
319 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
320 ngx_conf_set_sec_slot, | |
321 NGX_HTTP_LOC_CONF_OFFSET, | |
322 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.fail_timeout), | |
323 NULL }, | |
324 | |
509 | 325 { ngx_string("fastcgi_param"), |
326 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2, | |
649 | 327 ngx_conf_set_keyval_slot, |
485 | 328 NGX_HTTP_LOC_CONF_OFFSET, |
509 | 329 offsetof(ngx_http_fastcgi_loc_conf_t, params_source), |
485 | 330 NULL }, |
331 | |
649 | 332 { ngx_string("fastcgi_pass_header"), |
333 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
334 ngx_conf_set_str_array_slot, | |
335 NGX_HTTP_LOC_CONF_OFFSET, | |
336 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.pass_headers), | |
337 NULL }, | |
338 | |
339 { ngx_string("fastcgi_hide_header"), | |
340 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
341 ngx_conf_set_str_array_slot, | |
342 NGX_HTTP_LOC_CONF_OFFSET, | |
343 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.hide_headers), | |
344 NULL }, | |
345 | |
479 | 346 ngx_null_command |
347 }; | |
348 | |
349 | |
667 | 350 static ngx_http_module_t ngx_http_fastcgi_module_ctx = { |
509 | 351 ngx_http_fastcgi_add_variables, /* preconfiguration */ |
352 NULL, /* postconfiguration */ | |
479 | 353 |
354 NULL, /* create main configuration */ | |
355 NULL, /* init main configuration */ | |
356 | |
357 NULL, /* create server configuration */ | |
358 NULL, /* merge server configuration */ | |
359 | |
360 ngx_http_fastcgi_create_loc_conf, /* create location configuration */ | |
361 ngx_http_fastcgi_merge_loc_conf /* merge location configuration */ | |
362 }; | |
363 | |
364 | |
365 ngx_module_t ngx_http_fastcgi_module = { | |
509 | 366 NGX_MODULE_V1, |
479 | 367 &ngx_http_fastcgi_module_ctx, /* module context */ |
368 ngx_http_fastcgi_commands, /* module directives */ | |
369 NGX_HTTP_MODULE, /* module type */ | |
541 | 370 NULL, /* init master */ |
479 | 371 NULL, /* init module */ |
541 | 372 NULL, /* init process */ |
373 NULL, /* init thread */ | |
374 NULL, /* exit thread */ | |
375 NULL, /* exit process */ | |
376 NULL, /* exit master */ | |
377 NGX_MODULE_V1_PADDING | |
479 | 378 }; |
379 | |
380 | |
649 | 381 static ngx_str_t ngx_http_fastcgi_hide_headers[] = { |
382 ngx_string("Status"), | |
383 ngx_string("X-Accel-Expires"), | |
384 ngx_string("X-Accel-Redirect"), | |
385 ngx_string("X-Accel-Limit-Rate"), | |
386 ngx_string("X-Accel-Buffer"), | |
387 ngx_null_string | |
388 }; | |
389 | |
390 | |
487 | 391 static ngx_int_t |
392 ngx_http_fastcgi_handler(ngx_http_request_t *r) | |
479 | 393 { |
394 ngx_int_t rc; | |
395 ngx_http_upstream_t *u; | |
396 ngx_http_fastcgi_loc_conf_t *flcf; | |
397 | |
777
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
751
diff
changeset
|
398 if (r->subrequest_in_memory) { |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
751
diff
changeset
|
399 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
751
diff
changeset
|
400 "ngx_http_fastcgi_module does not support " |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
751
diff
changeset
|
401 "subrequest in memeory"); |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
751
diff
changeset
|
402 return NGX_HTTP_INTERNAL_SERVER_ERROR; |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
751
diff
changeset
|
403 } |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
751
diff
changeset
|
404 |
479 | 405 flcf = ngx_http_get_module_loc_conf(r, ngx_http_fastcgi_module); |
406 | |
501 | 407 u = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_t)); |
408 if (u == NULL) { | |
479 | 409 return NGX_HTTP_INTERNAL_SERVER_ERROR; |
410 } | |
411 | |
412 u->peer.log = r->connection->log; | |
413 u->peer.log_error = NGX_ERROR_ERR; | |
651 | 414 u->peer.peers = flcf->upstream_peers->peers; |
415 u->peer.tries = flcf->upstream_peers->peers->number; | |
479 | 416 #if (NGX_THREADS) |
417 u->peer.lock = &r->connection->lock; | |
418 #endif | |
419 | |
420 u->output.tag = (ngx_buf_tag_t) &ngx_http_fastcgi_module; | |
421 | |
422 u->conf = &flcf->upstream; | |
423 | |
424 u->create_request = ngx_http_fastcgi_create_request; | |
425 u->reinit_request = ngx_http_fastcgi_reinit_request; | |
426 u->process_header = ngx_http_fastcgi_process_header; | |
427 u->abort_request = ngx_http_fastcgi_abort_request; | |
428 u->finalize_request = ngx_http_fastcgi_finalize_request; | |
429 | |
649 | 430 u->buffering = 1; |
431 | |
581 | 432 u->pipe = ngx_pcalloc(r->pool, sizeof(ngx_event_pipe_t)); |
433 if (u->pipe == NULL) { | |
434 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
435 } | |
436 | |
437 u->pipe->input_filter = ngx_http_fastcgi_input_filter; | |
438 u->pipe->input_ctx = r; | |
479 | 439 |
440 r->upstream = u; | |
441 | |
442 rc = ngx_http_read_client_request_body(r, ngx_http_upstream_init); | |
443 | |
444 if (rc >= NGX_HTTP_SPECIAL_RESPONSE) { | |
445 return rc; | |
446 } | |
447 | |
448 return NGX_DONE; | |
449 } | |
450 | |
451 | |
487 | 452 static ngx_int_t |
453 ngx_http_fastcgi_create_request(ngx_http_request_t *r) | |
479 | 454 { |
509 | 455 off_t file_pos; |
456 u_char ch, *pos; | |
457 size_t size, len, key_len, val_len, padding; | |
458 ngx_uint_t i, n, next; | |
459 ngx_buf_t *b; | |
460 ngx_chain_t *cl, *body; | |
461 ngx_list_part_t *part; | |
462 ngx_table_elt_t *header; | |
463 ngx_http_script_code_pt code; | |
464 ngx_http_script_engine_t e, le; | |
465 ngx_http_fastcgi_header_t *h; | |
466 ngx_http_fastcgi_loc_conf_t *flcf; | |
467 ngx_http_script_len_code_pt lcode; | |
479 | 468 |
509 | 469 len = 0; |
485 | 470 |
479 | 471 flcf = ngx_http_get_module_loc_conf(r, ngx_http_fastcgi_module); |
472 | |
509 | 473 if (flcf->params_len) { |
474 ngx_memzero(&le, sizeof(ngx_http_script_engine_t)); | |
479 | 475 |
573 | 476 ngx_http_script_flush_no_cachable_variables(r, flcf->flushes); |
477 le.flushed = 1; | |
478 | |
509 | 479 le.ip = flcf->params_len->elts; |
480 le.request = r; | |
479 | 481 |
509 | 482 while (*(uintptr_t *) le.ip) { |
487 | 483 |
509 | 484 lcode = *(ngx_http_script_len_code_pt *) le.ip; |
485 key_len = lcode(&le); | |
479 | 486 |
509 | 487 for (val_len = 0; *(uintptr_t *) le.ip; val_len += lcode(&le)) { |
488 lcode = *(ngx_http_script_len_code_pt *) le.ip; | |
489 } | |
490 le.ip += sizeof(uintptr_t); | |
479 | 491 |
537 | 492 len += 1 + key_len + ((val_len > 127) ? 4 : 1) + val_len; |
485 | 493 } |
494 } | |
495 | |
509 | 496 if (flcf->upstream.pass_request_headers) { |
485 | 497 |
509 | 498 part = &r->headers_in.headers.part; |
499 header = part->elts; | |
500 | |
501 for (i = 0; /* void */; i++) { | |
479 | 502 |
509 | 503 if (i >= part->nelts) { |
504 if (part->next == NULL) { | |
505 break; | |
506 } | |
479 | 507 |
509 | 508 part = part->next; |
509 header = part->elts; | |
510 i = 0; | |
479 | 511 } |
512 | |
509 | 513 len += ((sizeof("HTTP_") - 1 + header[i].key.len > 127) ? 4 : 1) |
514 + ((header[i].value.len > 127) ? 4 : 1) | |
515 + sizeof("HTTP_") - 1 + header[i].key.len + header[i].value.len; | |
479 | 516 } |
517 } | |
518 | |
519 | |
520 if (len > 65535) { | |
521 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, | |
522 "fastcgi: the request record is too big"); | |
523 return NGX_ERROR; | |
524 } | |
525 | |
526 | |
527 padding = 8 - len % 8; | |
528 padding = (padding == 8) ? 0 : padding; | |
529 | |
530 | |
531 size = sizeof(ngx_http_fastcgi_header_t) | |
532 + sizeof(ngx_http_fastcgi_begin_request_t) | |
533 | |
534 + sizeof(ngx_http_fastcgi_header_t) /* NGX_HTTP_FASTCGI_PARAMS */ | |
535 + len + padding | |
536 + sizeof(ngx_http_fastcgi_header_t) /* NGX_HTTP_FASTCGI_PARAMS */ | |
537 | |
538 + sizeof(ngx_http_fastcgi_header_t); /* NGX_HTTP_FASTCGI_STDIN */ | |
539 | |
540 | |
501 | 541 b = ngx_create_temp_buf(r->pool, size); |
542 if (b == NULL) { | |
479 | 543 return NGX_ERROR; |
544 } | |
545 | |
501 | 546 cl = ngx_alloc_chain_link(r->pool); |
547 if (cl == NULL) { | |
479 | 548 return NGX_ERROR; |
549 } | |
550 | |
551 cl->buf = b; | |
552 | |
509 | 553 ngx_memcpy(b->pos, &ngx_http_fastcgi_request_start, |
554 sizeof(ngx_http_fastcgi_request_start_t)); | |
479 | 555 |
556 h = (ngx_http_fastcgi_header_t *) | |
557 (b->pos + sizeof(ngx_http_fastcgi_header_t) | |
558 + sizeof(ngx_http_fastcgi_begin_request_t)); | |
559 | |
560 h->content_length_hi = (u_char) ((len >> 8) & 0xff); | |
561 h->content_length_lo = (u_char) (len & 0xff); | |
562 h->padding_length = (u_char) padding; | |
563 h->reserved = 0; | |
564 | |
565 b->last = b->pos + sizeof(ngx_http_fastcgi_header_t) | |
566 + sizeof(ngx_http_fastcgi_begin_request_t) | |
567 + sizeof(ngx_http_fastcgi_header_t); | |
568 | |
569 | |
509 | 570 if (flcf->params_len) { |
571 ngx_memzero(&e, sizeof(ngx_http_script_engine_t)); | |
479 | 572 |
509 | 573 e.ip = flcf->params->elts; |
574 e.pos = b->last; | |
575 e.request = r; | |
573 | 576 e.flushed = 1; |
479 | 577 |
509 | 578 le.ip = flcf->params_len->elts; |
479 | 579 |
509 | 580 while (*(uintptr_t *) le.ip) { |
479 | 581 |
509 | 582 lcode = *(ngx_http_script_len_code_pt *) le.ip; |
583 key_len = (u_char) lcode(&le); | |
479 | 584 |
509 | 585 for (val_len = 0; *(uintptr_t *) le.ip; val_len += lcode(&le)) { |
586 lcode = *(ngx_http_script_len_code_pt *) le.ip; | |
587 } | |
588 le.ip += sizeof(uintptr_t); | |
479 | 589 |
537 | 590 *e.pos++ = (u_char) key_len; |
479 | 591 |
537 | 592 if (val_len > 127) { |
593 *e.pos++ = (u_char) (((val_len >> 24) & 0x7f) | 0x80); | |
594 *e.pos++ = (u_char) ((val_len >> 16) & 0xff); | |
595 *e.pos++ = (u_char) ((val_len >> 8) & 0xff); | |
596 *e.pos++ = (u_char) (val_len & 0xff); | |
479 | 597 |
537 | 598 } else { |
599 *e.pos++ = (u_char) val_len; | |
509 | 600 } |
479 | 601 |
509 | 602 while (*(uintptr_t *) e.ip) { |
603 code = *(ngx_http_script_code_pt *) e.ip; | |
604 code((ngx_http_script_engine_t *) &e); | |
605 } | |
606 e.ip += sizeof(uintptr_t); | |
479 | 607 } |
608 | |
509 | 609 b->last = e.pos; |
487 | 610 } |
611 | |
612 | |
509 | 613 if (flcf->upstream.pass_request_headers) { |
479 | 614 |
509 | 615 part = &r->headers_in.headers.part; |
616 header = part->elts; | |
479 | 617 |
509 | 618 for (i = 0; /* void */; i++) { |
491 | 619 |
509 | 620 if (i >= part->nelts) { |
621 if (part->next == NULL) { | |
577 | 622 break; |
509 | 623 } |
577 | 624 |
509 | 625 part = part->next; |
626 header = part->elts; | |
627 i = 0; | |
628 } | |
479 | 629 |
509 | 630 len = sizeof("HTTP_") - 1 + header[i].key.len; |
631 if (len > 127) { | |
632 *b->last++ = (u_char) (((len >> 24) & 0x7f) | 0x80); | |
633 *b->last++ = (u_char) ((len >> 16) & 0xff); | |
634 *b->last++ = (u_char) ((len >> 8) & 0xff); | |
635 *b->last++ = (u_char) (len & 0xff); | |
577 | 636 |
509 | 637 } else { |
638 *b->last++ = (u_char) len; | |
487 | 639 } |
640 | |
509 | 641 len = header[i].value.len; |
642 if (len > 127) { | |
643 *b->last++ = (u_char) (((len >> 24) & 0x7f) | 0x80); | |
644 *b->last++ = (u_char) ((len >> 16) & 0xff); | |
645 *b->last++ = (u_char) ((len >> 8) & 0xff); | |
646 *b->last++ = (u_char) (len & 0xff); | |
487 | 647 |
509 | 648 } else { |
649 *b->last++ = (u_char) len; | |
479 | 650 } |
651 | |
509 | 652 b->last = ngx_cpymem(b->last, "HTTP_", sizeof("HTTP_") - 1); |
479 | 653 |
509 | 654 for (n = 0; n < header[i].key.len; n++) { |
655 ch = header[i].key.data[n]; | |
479 | 656 |
509 | 657 if (ch >= 'a' && ch <= 'z') { |
658 ch &= ~0x20; | |
479 | 659 |
509 | 660 } else if (ch == '-') { |
661 ch = '_'; | |
662 } | |
479 | 663 |
509 | 664 *b->last++ = ch; |
479 | 665 } |
666 | |
573 | 667 b->last = ngx_copy(b->last, header[i].value.data, |
668 header[i].value.len); | |
479 | 669 } |
670 } | |
671 | |
672 | |
673 if (padding) { | |
674 ngx_memzero(b->last, padding); | |
675 b->last += padding; | |
676 } | |
677 | |
678 | |
679 h = (ngx_http_fastcgi_header_t *) b->last; | |
680 b->last += sizeof(ngx_http_fastcgi_header_t); | |
681 | |
682 h->version = 1; | |
683 h->type = NGX_HTTP_FASTCGI_PARAMS; | |
684 h->request_id_hi = 0; | |
685 h->request_id_lo = 1; | |
686 h->content_length_hi = 0; | |
687 h->content_length_lo = 0; | |
688 h->padding_length = 0; | |
689 h->reserved = 0; | |
690 | |
691 h = (ngx_http_fastcgi_header_t *) b->last; | |
692 b->last += sizeof(ngx_http_fastcgi_header_t); | |
693 | |
509 | 694 if (flcf->upstream.pass_request_body) { |
695 body = r->upstream->request_bufs; | |
696 r->upstream->request_bufs = cl; | |
479 | 697 |
698 #if (NGX_SUPPRESS_WARN) | |
509 | 699 file_pos = 0; |
700 pos = NULL; | |
479 | 701 #endif |
702 | |
509 | 703 while (body) { |
479 | 704 |
509 | 705 if (body->buf->in_file) { |
706 file_pos = body->buf->file_pos; | |
479 | 707 |
509 | 708 } else { |
709 pos = body->buf->pos; | |
479 | 710 } |
711 | |
509 | 712 next = 0; |
479 | 713 |
509 | 714 do { |
715 b = ngx_alloc_buf(r->pool); | |
716 if (b == NULL) { | |
717 return NGX_ERROR; | |
479 | 718 } |
719 | |
509 | 720 ngx_memcpy(b, body->buf, sizeof(ngx_buf_t)); |
721 | |
722 if (body->buf->in_file) { | |
723 b->file_pos = file_pos; | |
724 file_pos += 32 * 1024; | |
479 | 725 |
509 | 726 if (file_pos > body->buf->file_last) { |
727 file_pos = body->buf->file_last; | |
728 next = 1; | |
729 } | |
730 | |
731 b->file_last = file_pos; | |
732 len = (ngx_uint_t) (file_pos - b->file_pos); | |
733 | |
734 } else { | |
735 b->pos = pos; | |
736 pos += 32 * 1024; | |
479 | 737 |
509 | 738 if (pos > body->buf->last) { |
739 pos = body->buf->last; | |
740 next = 1; | |
741 } | |
742 | |
743 b->last = pos; | |
744 len = (ngx_uint_t) (pos - b->pos); | |
745 } | |
479 | 746 |
509 | 747 padding = 8 - len % 8; |
748 padding = (padding == 8) ? 0 : padding; | |
479 | 749 |
509 | 750 h->version = 1; |
751 h->type = NGX_HTTP_FASTCGI_STDIN; | |
752 h->request_id_hi = 0; | |
753 h->request_id_lo = 1; | |
754 h->content_length_hi = (u_char) ((len >> 8) & 0xff); | |
755 h->content_length_lo = (u_char) (len & 0xff); | |
756 h->padding_length = (u_char) padding; | |
757 h->reserved = 0; | |
479 | 758 |
509 | 759 cl->next = ngx_alloc_chain_link(r->pool); |
760 if (cl->next == NULL) { | |
761 return NGX_ERROR; | |
762 } | |
479 | 763 |
509 | 764 cl = cl->next; |
765 cl->buf = b; | |
766 | |
767 b = ngx_create_temp_buf(r->pool, | |
768 sizeof(ngx_http_fastcgi_header_t) | |
769 + padding); | |
770 if (b == NULL) { | |
771 return NGX_ERROR; | |
772 } | |
479 | 773 |
509 | 774 if (padding) { |
775 ngx_memzero(b->last, padding); | |
776 b->last += padding; | |
777 } | |
778 | |
779 h = (ngx_http_fastcgi_header_t *) b->last; | |
780 b->last += sizeof(ngx_http_fastcgi_header_t); | |
479 | 781 |
509 | 782 cl->next = ngx_alloc_chain_link(r->pool); |
783 if (cl->next == NULL) { | |
784 return NGX_ERROR; | |
785 } | |
786 | |
787 cl = cl->next; | |
788 cl->buf = b; | |
479 | 789 |
509 | 790 } while (!next); |
479 | 791 |
509 | 792 body = body->next; |
793 } | |
479 | 794 |
509 | 795 } else { |
796 r->upstream->request_bufs = cl; | |
479 | 797 } |
798 | |
799 h->version = 1; | |
800 h->type = NGX_HTTP_FASTCGI_STDIN; | |
801 h->request_id_hi = 0; | |
802 h->request_id_lo = 1; | |
803 h->content_length_hi = 0; | |
804 h->content_length_lo = 0; | |
805 h->padding_length = 0; | |
806 h->reserved = 0; | |
807 | |
808 cl->next = NULL; | |
809 | |
810 return NGX_OK; | |
811 } | |
812 | |
813 | |
487 | 814 static ngx_int_t |
815 ngx_http_fastcgi_reinit_request(ngx_http_request_t *r) | |
479 | 816 { |
817 ngx_http_fastcgi_ctx_t *f; | |
818 | |
819 f = ngx_http_get_module_ctx(r, ngx_http_fastcgi_module); | |
820 | |
821 if (f == NULL) { | |
822 return NGX_OK; | |
823 } | |
824 | |
825 f->state = ngx_http_fastcgi_st_version; | |
615 | 826 f->fastcgi_stdout = 0; |
479 | 827 |
828 return NGX_OK; | |
829 } | |
830 | |
831 | |
507 | 832 static ngx_int_t |
833 ngx_http_fastcgi_process_header(ngx_http_request_t *r) | |
479 | 834 { |
509 | 835 u_char *start, *last; |
836 ngx_str_t *status_line, line; | |
837 ngx_int_t rc, status; | |
649 | 838 ngx_uint_t i; |
509 | 839 ngx_table_elt_t *h; |
840 ngx_http_upstream_t *u; | |
841 ngx_http_fastcgi_ctx_t *f; | |
842 ngx_http_upstream_header_t *hh; | |
843 ngx_http_upstream_main_conf_t *umcf; | |
479 | 844 |
845 f = ngx_http_get_module_ctx(r, ngx_http_fastcgi_module); | |
846 | |
509 | 847 umcf = ngx_http_get_module_main_conf(r, ngx_http_upstream_module); |
848 | |
479 | 849 if (f == NULL) { |
501 | 850 f = ngx_pcalloc(r->pool, sizeof(ngx_http_fastcgi_ctx_t)); |
851 if (f == NULL) { | |
479 | 852 return NGX_HTTP_INTERNAL_SERVER_ERROR; |
853 } | |
854 | |
855 ngx_http_set_ctx(r, f, ngx_http_fastcgi_module); | |
856 } | |
857 | |
858 u = r->upstream; | |
859 | |
860 for ( ;; ) { | |
861 | |
862 if (f->state < ngx_http_fastcgi_st_data) { | |
863 | |
581 | 864 f->pos = u->buffer.pos; |
865 f->last = u->buffer.last; | |
479 | 866 |
867 rc = ngx_http_fastcgi_process_record(r, f); | |
868 | |
581 | 869 u->buffer.pos = f->pos; |
870 u->buffer.last = f->last; | |
479 | 871 |
872 if (rc == NGX_AGAIN) { | |
873 return NGX_AGAIN; | |
874 } | |
875 | |
876 if (rc == NGX_ERROR) { | |
877 return NGX_HTTP_UPSTREAM_INVALID_HEADER; | |
878 } | |
879 | |
491 | 880 if (f->type != NGX_HTTP_FASTCGI_STDOUT |
881 && f->type != NGX_HTTP_FASTCGI_STDERR) | |
882 { | |
479 | 883 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
884 "upstream sent unexpected FastCGI record: %d", | |
885 f->type); | |
886 | |
887 return NGX_HTTP_UPSTREAM_INVALID_HEADER; | |
888 } | |
889 | |
491 | 890 if (f->type == NGX_HTTP_FASTCGI_STDOUT && f->length == 0) { |
479 | 891 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
892 "upstream closed prematurely FastCGI stdout"); | |
893 | |
894 return NGX_HTTP_UPSTREAM_INVALID_HEADER; | |
895 } | |
896 } | |
897 | |
898 if (f->state == ngx_http_fastcgi_st_padding) { | |
899 | |
581 | 900 if (u->buffer.pos + f->padding < u->buffer.last) { |
479 | 901 f->state = ngx_http_fastcgi_st_version; |
581 | 902 u->buffer.pos += f->padding; |
479 | 903 |
904 continue; | |
905 } | |
906 | |
581 | 907 if (u->buffer.pos + f->padding == u->buffer.last) { |
479 | 908 f->state = ngx_http_fastcgi_st_version; |
581 | 909 u->buffer.pos = u->buffer.last; |
479 | 910 |
911 return NGX_AGAIN; | |
912 } | |
913 | |
581 | 914 f->padding -= u->buffer.last - u->buffer.pos; |
915 u->buffer.pos = u->buffer.last; | |
479 | 916 |
917 return NGX_AGAIN; | |
918 } | |
919 | |
491 | 920 |
479 | 921 /* f->state == ngx_http_fastcgi_st_data */ |
922 | |
491 | 923 if (f->type == NGX_HTTP_FASTCGI_STDERR) { |
924 | |
925 if (f->length) { | |
581 | 926 line.data = u->buffer.pos; |
491 | 927 |
581 | 928 if (u->buffer.pos + f->length <= u->buffer.last) { |
491 | 929 line.len = f->length; |
581 | 930 u->buffer.pos += f->length; |
491 | 931 f->length = 0; |
932 f->state = ngx_http_fastcgi_st_padding; | |
933 | |
577 | 934 } else { |
581 | 935 line.len = u->buffer.last - u->buffer.pos; |
936 f->length -= u->buffer.last - u->buffer.pos; | |
937 u->buffer.pos = u->buffer.last; | |
491 | 938 } |
939 | |
940 while (line.data[line.len - 1] == LF | |
941 || line.data[line.len - 1] == CR | |
942 || line.data[line.len - 1] == '.' | |
943 || line.data[line.len - 1] == ' ') | |
944 { | |
945 line.len--; | |
946 } | |
947 | |
948 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
949 "FastCGI sent in stderr: \"%V\"", &line); | |
950 | |
581 | 951 if (u->buffer.pos == u->buffer.last) { |
615 | 952 |
953 if (!f->fastcgi_stdout) { | |
954 | |
955 /* | |
956 * the special handling the large number | |
957 * of the PHP warnings to not allocate memory | |
958 */ | |
959 | |
960 u->buffer.pos = u->buffer.start; | |
961 u->buffer.last = u->buffer.start; | |
962 } | |
963 | |
491 | 964 return NGX_AGAIN; |
965 } | |
966 | |
967 } else { | |
968 f->state = ngx_http_fastcgi_st_version; | |
969 } | |
970 | |
971 continue; | |
972 } | |
973 | |
974 | |
975 /* f->type == NGX_HTTP_FASTCGI_STDOUT */ | |
976 | |
615 | 977 f->fastcgi_stdout = 1; |
978 | |
581 | 979 start = u->buffer.pos; |
479 | 980 |
581 | 981 if (u->buffer.pos + f->length < u->buffer.last) { |
479 | 982 |
983 /* | |
581 | 984 * set u->buffer.last to the end of the FastCGI record data |
479 | 985 * for ngx_http_parse_header_line() |
986 */ | |
987 | |
581 | 988 last = u->buffer.last; |
989 u->buffer.last = u->buffer.pos + f->length; | |
479 | 990 |
991 } else { | |
992 last = NULL; | |
993 } | |
994 | |
995 for ( ;; ) { | |
996 | |
581 | 997 rc = ngx_http_parse_header_line(r, &u->buffer); |
479 | 998 |
999 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
1000 "http fastcgi parser: %d", rc); | |
1001 | |
1002 if (rc == NGX_AGAIN) { | |
1003 break; | |
1004 } | |
1005 | |
1006 if (rc == NGX_OK) { | |
1007 | |
1008 /* a header line has been parsed successfully */ | |
1009 | |
509 | 1010 h = ngx_list_push(&u->headers_in.headers); |
501 | 1011 if (h == NULL) { |
479 | 1012 return NGX_HTTP_INTERNAL_SERVER_ERROR; |
1013 } | |
1014 | |
509 | 1015 h->hash = r->header_hash; |
1016 | |
479 | 1017 h->key.len = r->header_name_end - r->header_name_start; |
1018 h->value.len = r->header_end - r->header_start; | |
1019 | |
1020 h->key.data = ngx_palloc(r->pool, | |
649 | 1021 h->key.len + 1 + h->value.len + 1 + h->key.len); |
479 | 1022 if (h->key.data == NULL) { |
1023 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
1024 } | |
1025 | |
1026 h->value.data = h->key.data + h->key.len + 1; | |
649 | 1027 h->lowcase_key = h->key.data + h->key.len + 1 |
1028 + h->value.len + 1; | |
479 | 1029 |
1030 ngx_cpystrn(h->key.data, r->header_name_start, h->key.len + 1); | |
1031 ngx_cpystrn(h->value.data, r->header_start, h->value.len + 1); | |
1032 | |
649 | 1033 if (h->key.len == r->lowcase_index) { |
1034 ngx_memcpy(h->lowcase_key, r->lowcase_header, h->key.len); | |
1035 | |
1036 } else { | |
1037 for (i = 0; i < h->key.len; i++) { | |
1038 h->lowcase_key[i] = ngx_tolower(h->lowcase_key[i]); | |
479 | 1039 } |
1040 } | |
1041 | |
649 | 1042 hh = ngx_hash_find(&umcf->headers_in_hash, h->hash, |
1043 h->lowcase_key, h->key.len); | |
1044 | |
1045 if (hh && hh->handler(r, h, hh->offset) != NGX_OK) { | |
1046 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
1047 } | |
1048 | |
479 | 1049 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
1050 "http fastcgi header: \"%V: %V\"", | |
1051 &h->key, &h->value); | |
1052 | |
615 | 1053 if (u->buffer.pos < u->buffer.last) { |
1054 continue; | |
1055 } | |
1056 | |
1057 /* the end of the FastCGI record */ | |
1058 | |
1059 break; | |
479 | 1060 } |
1061 | |
1062 if (rc == NGX_HTTP_PARSE_HEADER_DONE) { | |
1063 | |
1064 /* a whole header has been parsed successfully */ | |
1065 | |
1066 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
1067 "http fastcgi header done"); | |
1068 | |
509 | 1069 if (u->headers_in.status) { |
1070 status_line = &u->headers_in.status->value; | |
479 | 1071 |
1072 status = ngx_atoi(status_line->data, 3); | |
1073 | |
1074 if (status == NGX_ERROR) { | |
1075 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
1076 } | |
1077 | |
529 | 1078 u->headers_in.status_n = status; |
1079 u->headers_in.status_line = *status_line; | |
479 | 1080 |
1081 } else { | |
529 | 1082 u->headers_in.status_n = 200; |
1083 u->headers_in.status_line.len = sizeof("200 OK") - 1; | |
1084 u->headers_in.status_line.data = (u_char *) "200 OK"; | |
479 | 1085 } |
1086 | |
529 | 1087 u->state->status = u->headers_in.status_n; |
479 | 1088 #if 0 |
1089 if (u->cachable) { | |
1090 u->cachable = ngx_http_upstream_is_cachable(r); | |
1091 } | |
1092 #endif | |
1093 | |
1094 break; | |
1095 } | |
1096 | |
1097 /* there was error while a header line parsing */ | |
1098 | |
1099 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
619 | 1100 "upstream sent invalid header"); |
479 | 1101 |
1102 return NGX_HTTP_UPSTREAM_INVALID_HEADER; | |
1103 | |
1104 } | |
1105 | |
1106 if (last) { | |
581 | 1107 u->buffer.last = last; |
479 | 1108 } |
1109 | |
581 | 1110 f->length -= u->buffer.pos - start; |
479 | 1111 |
1112 if (f->length == 0) { | |
1113 if (f->padding) { | |
1114 f->state = ngx_http_fastcgi_st_padding; | |
1115 } else { | |
1116 f->state = ngx_http_fastcgi_st_version; | |
1117 } | |
1118 } | |
1119 | |
615 | 1120 if (rc == NGX_HTTP_PARSE_HEADER_DONE) { |
1121 return NGX_OK; | |
1122 } | |
1123 | |
1124 if (u->buffer.pos == u->buffer.last) { | |
1125 return NGX_AGAIN; | |
1126 } | |
1127 | |
1128 if (rc == NGX_AGAIN) { | |
1129 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, | |
1130 "upstream split a header line in FastCGI records"); | |
1131 | |
1132 return NGX_HTTP_UPSTREAM_INVALID_HEADER; | |
1133 } | |
479 | 1134 } |
1135 } | |
1136 | |
1137 | |
487 | 1138 static ngx_int_t |
1139 ngx_http_fastcgi_input_filter(ngx_event_pipe_t *p, ngx_buf_t *buf) | |
479 | 1140 { |
1141 ngx_int_t rc; | |
483 | 1142 ngx_buf_t *b, **prev; |
479 | 1143 ngx_str_t line; |
1144 ngx_chain_t *cl; | |
1145 ngx_http_request_t *r; | |
1146 ngx_http_fastcgi_ctx_t *f; | |
1147 | |
1148 if (buf->pos == buf->last) { | |
1149 return NGX_OK; | |
1150 } | |
1151 | |
1152 r = p->input_ctx; | |
1153 f = ngx_http_get_module_ctx(r, ngx_http_fastcgi_module); | |
1154 | |
1155 b = NULL; | |
483 | 1156 prev = &buf->shadow; |
479 | 1157 |
1158 f->pos = buf->pos; | |
1159 f->last = buf->last; | |
1160 | |
1161 for ( ;; ) { | |
1162 if (f->state < ngx_http_fastcgi_st_data) { | |
1163 | |
1164 rc = ngx_http_fastcgi_process_record(r, f); | |
1165 | |
1166 if (rc == NGX_AGAIN) { | |
1167 break; | |
1168 } | |
1169 | |
1170 if (rc == NGX_ERROR) { | |
1171 return NGX_ERROR; | |
1172 } | |
1173 | |
1174 if (f->type == NGX_HTTP_FASTCGI_STDOUT && f->length == 0) { | |
1175 f->state = ngx_http_fastcgi_st_version; | |
1176 p->upstream_done = 1; | |
1177 | |
1178 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, p->log, 0, | |
1179 "http fastcgi closed stdout"); | |
1180 | |
1181 continue; | |
1182 } | |
1183 | |
1184 if (f->type == NGX_HTTP_FASTCGI_END_REQUEST) { | |
1185 f->state = ngx_http_fastcgi_st_version; | |
1186 p->upstream_done = 1; | |
1187 | |
1188 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, p->log, 0, | |
1189 "http fastcgi sent end request"); | |
1190 | |
1191 break; | |
1192 } | |
1193 } | |
1194 | |
1195 | |
1196 if (f->state == ngx_http_fastcgi_st_padding) { | |
1197 | |
1198 if (f->pos + f->padding < f->last) { | |
1199 f->state = ngx_http_fastcgi_st_version; | |
1200 f->pos += f->padding; | |
1201 | |
1202 continue; | |
1203 } | |
1204 | |
1205 if (f->pos + f->padding == f->last) { | |
1206 f->state = ngx_http_fastcgi_st_version; | |
1207 | |
1208 break; | |
1209 } | |
1210 | |
1211 f->padding -= f->last - f->pos; | |
1212 | |
1213 break; | |
1214 } | |
1215 | |
1216 | |
1217 /* f->state == ngx_http_fastcgi_st_data */ | |
1218 | |
1219 if (f->type == NGX_HTTP_FASTCGI_STDERR) { | |
1220 | |
1221 if (f->length) { | |
639 | 1222 |
1223 if (f->pos == f->last) { | |
1224 break; | |
1225 } | |
1226 | |
479 | 1227 line.data = f->pos; |
1228 | |
1229 if (f->pos + f->length <= f->last) { | |
1230 line.len = f->length; | |
1231 f->pos += f->length; | |
491 | 1232 f->length = 0; |
1233 f->state = ngx_http_fastcgi_st_padding; | |
479 | 1234 |
577 | 1235 } else { |
479 | 1236 line.len = f->last - f->pos; |
1237 f->length -= f->last - f->pos; | |
1238 f->pos = f->last; | |
1239 } | |
1240 | |
491 | 1241 while (line.data[line.len - 1] == LF |
1242 || line.data[line.len - 1] == CR | |
1243 || line.data[line.len - 1] == '.' | |
1244 || line.data[line.len - 1] == ' ') | |
1245 { | |
1246 line.len--; | |
1247 } | |
479 | 1248 |
1249 ngx_log_error(NGX_LOG_ERR, p->log, 0, | |
491 | 1250 "FastCGI sent in stderr: \"%V\"", &line); |
479 | 1251 |
1252 if (f->pos == f->last) { | |
1253 break; | |
1254 } | |
491 | 1255 |
1256 } else { | |
1257 f->state = ngx_http_fastcgi_st_version; | |
479 | 1258 } |
1259 | |
1260 continue; | |
1261 } | |
1262 | |
1263 | |
1264 /* f->type == NGX_HTTP_FASTCGI_STDOUT */ | |
1265 | |
639 | 1266 if (f->pos == f->last) { |
1267 break; | |
1268 } | |
1269 | |
479 | 1270 if (p->free) { |
1271 b = p->free->buf; | |
1272 p->free = p->free->next; | |
1273 | |
1274 } else { | |
501 | 1275 b = ngx_alloc_buf(p->pool); |
1276 if (b == NULL) { | |
479 | 1277 return NGX_ERROR; |
1278 } | |
1279 } | |
1280 | |
1281 ngx_memzero(b, sizeof(ngx_buf_t)); | |
1282 | |
1283 b->pos = f->pos; | |
483 | 1284 b->start = buf->start; |
1285 b->end = buf->end; | |
479 | 1286 b->tag = p->tag; |
1287 b->temporary = 1; | |
1288 b->recycled = 1; | |
483 | 1289 |
1290 *prev = b; | |
1291 prev = &b->shadow; | |
479 | 1292 |
501 | 1293 cl = ngx_alloc_chain_link(p->pool); |
1294 if (cl == NULL) { | |
479 | 1295 return NGX_ERROR; |
1296 } | |
1297 | |
1298 cl->buf = b; | |
1299 cl->next = NULL; | |
1300 | |
501 | 1301 if (p->in) { |
1302 *p->last_in = cl; | |
1303 } else { | |
1304 p->in = cl; | |
1305 } | |
1306 p->last_in = &cl->next; | |
1307 | |
1308 | |
483 | 1309 /* STUB */ b->num = buf->num; |
1310 | |
479 | 1311 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0, "input buf #%d", b->num); |
1312 | |
1313 | |
1314 if (f->pos + f->length < f->last) { | |
1315 | |
1316 if (f->padding) { | |
1317 f->state = ngx_http_fastcgi_st_padding; | |
1318 } else { | |
1319 f->state = ngx_http_fastcgi_st_version; | |
1320 } | |
1321 | |
1322 f->pos += f->length; | |
1323 b->last = f->pos; | |
1324 | |
1325 continue; | |
1326 } | |
1327 | |
1328 if (f->pos + f->length == f->last) { | |
1329 | |
1330 if (f->padding) { | |
1331 f->state = ngx_http_fastcgi_st_padding; | |
1332 } else { | |
1333 f->state = ngx_http_fastcgi_st_version; | |
1334 } | |
1335 | |
1336 b->last = f->last; | |
1337 | |
1338 break; | |
1339 } | |
1340 | |
1341 f->length -= f->last - f->pos; | |
1342 | |
1343 b->last = f->last; | |
1344 | |
1345 break; | |
1346 | |
1347 } | |
1348 | |
1349 if (b) { | |
483 | 1350 b->shadow = buf; |
479 | 1351 b->last_shadow = 1; |
483 | 1352 |
1353 return NGX_OK; | |
1354 } | |
1355 | |
1356 /* there is no data record in the buf, add it to free chain */ | |
1357 | |
1358 if (ngx_event_pipe_add_free_buf(p, buf) != NGX_OK) { | |
1359 return NGX_ERROR; | |
479 | 1360 } |
1361 | |
1362 return NGX_OK; | |
1363 } | |
1364 | |
1365 | |
487 | 1366 static ngx_int_t |
1367 ngx_http_fastcgi_process_record(ngx_http_request_t *r, | |
1368 ngx_http_fastcgi_ctx_t *f) | |
479 | 1369 { |
1370 u_char ch, *p; | |
1371 ngx_http_fastcgi_state_e state; | |
1372 | |
1373 state = f->state; | |
1374 | |
1375 for (p = f->pos; p < f->last; p++) { | |
1376 | |
1377 ch = *p; | |
1378 | |
1379 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
1380 "http fastcgi record byte: %02Xd", ch); | |
1381 | |
1382 switch (state) { | |
1383 | |
1384 case ngx_http_fastcgi_st_version: | |
1385 if (ch != 1) { | |
1386 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
1387 "upstream sent unsupported FastCGI " | |
1388 "protocol version: %d", ch); | |
1389 return NGX_ERROR; | |
1390 } | |
1391 state = ngx_http_fastcgi_st_type; | |
1392 break; | |
1393 | |
1394 case ngx_http_fastcgi_st_type: | |
1395 switch (ch) { | |
1396 case NGX_HTTP_FASTCGI_STDOUT: | |
1397 case NGX_HTTP_FASTCGI_STDERR: | |
1398 case NGX_HTTP_FASTCGI_END_REQUEST: | |
1399 f->type = (ngx_uint_t) ch; | |
1400 break; | |
1401 default: | |
1402 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
1403 "upstream sent invalid FastCGI " | |
1404 "record type: %d", ch); | |
1405 return NGX_ERROR; | |
1406 | |
1407 } | |
1408 state = ngx_http_fastcgi_st_request_id_hi; | |
1409 break; | |
1410 | |
1411 /* we support the single request per connection */ | |
1412 | |
1413 case ngx_http_fastcgi_st_request_id_hi: | |
1414 if (ch != 0) { | |
1415 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
1416 "upstream sent unexpected FastCGI " | |
1417 "request id high byte: %d", ch); | |
1418 return NGX_ERROR; | |
1419 } | |
1420 state = ngx_http_fastcgi_st_request_id_lo; | |
1421 break; | |
1422 | |
1423 case ngx_http_fastcgi_st_request_id_lo: | |
1424 if (ch != 1) { | |
1425 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, | |
1426 "upstream sent unexpected FastCGI " | |
1427 "request id low byte: %d", ch); | |
1428 return NGX_ERROR; | |
1429 } | |
1430 state = ngx_http_fastcgi_st_content_length_hi; | |
1431 break; | |
1432 | |
1433 case ngx_http_fastcgi_st_content_length_hi: | |
1434 f->length = ch << 8; | |
1435 state = ngx_http_fastcgi_st_content_length_lo; | |
1436 break; | |
1437 | |
1438 case ngx_http_fastcgi_st_content_length_lo: | |
1439 f->length |= (size_t) ch; | |
1440 state = ngx_http_fastcgi_st_padding_length; | |
1441 break; | |
1442 | |
1443 case ngx_http_fastcgi_st_padding_length: | |
1444 f->padding = (size_t) ch; | |
1445 state = ngx_http_fastcgi_st_reserved; | |
1446 break; | |
1447 | |
1448 case ngx_http_fastcgi_st_reserved: | |
1449 state = ngx_http_fastcgi_st_data; | |
1450 | |
1451 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
1452 "http fastcgi record length: %z", f->length); | |
1453 | |
1454 f->pos = p + 1; | |
1455 f->state = state; | |
1456 | |
1457 return NGX_OK; | |
1458 | |
1459 /* suppress warning */ | |
1460 case ngx_http_fastcgi_st_data: | |
1461 case ngx_http_fastcgi_st_padding: | |
1462 break; | |
1463 } | |
1464 } | |
1465 | |
1466 f->state = state; | |
1467 | |
1468 return NGX_AGAIN; | |
1469 } | |
1470 | |
1471 | |
487 | 1472 static void |
1473 ngx_http_fastcgi_abort_request(ngx_http_request_t *r) | |
479 | 1474 { |
1475 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
1476 "abort http fastcgi request"); | |
1477 | |
1478 return; | |
1479 } | |
1480 | |
1481 | |
487 | 1482 static void |
1483 ngx_http_fastcgi_finalize_request(ngx_http_request_t *r, ngx_int_t rc) | |
479 | 1484 { |
1485 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
1486 "finalize http fastcgi request"); | |
1487 | |
1488 return; | |
1489 } | |
1490 | |
1491 | |
509 | 1492 static ngx_int_t |
1493 ngx_http_fastcgi_add_variables(ngx_conf_t *cf) | |
479 | 1494 { |
509 | 1495 ngx_http_variable_t *var; |
479 | 1496 |
583 | 1497 var = ngx_http_add_variable(cf, &ngx_http_fastcgi_script_name, |
655 | 1498 NGX_HTTP_VAR_NOHASH|NGX_HTTP_VAR_NOCACHABLE); |
509 | 1499 if (var == NULL) { |
1500 return NGX_ERROR; | |
479 | 1501 } |
1502 | |
637 | 1503 var->get_handler = ngx_http_fastcgi_script_name_variable; |
485 | 1504 |
509 | 1505 return NGX_OK; |
479 | 1506 } |
1507 | |
1508 | |
487 | 1509 static void * |
1510 ngx_http_fastcgi_create_loc_conf(ngx_conf_t *cf) | |
479 | 1511 { |
1512 ngx_http_fastcgi_loc_conf_t *conf; | |
1513 | |
501 | 1514 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_fastcgi_loc_conf_t)); |
1515 if (conf == NULL) { | |
479 | 1516 return NGX_CONF_ERROR; |
1517 } | |
1518 | |
1519 /* | |
1520 * set by ngx_pcalloc(): | |
1521 * | |
1522 * conf->upstream.bufs.num = 0; | |
1523 * conf->upstream.next_upstream = 0; | |
1524 * conf->upstream.temp_path = NULL; | |
649 | 1525 * conf->upstream.hide_headers_hash = { NULL, 0 }; |
1526 * conf->upstream.hide_headers = NULL; | |
1527 * conf->upstream.pass_headers = NULL; | |
509 | 1528 * conf->upstream.schema = { 0, NULL }; |
1529 * conf->upstream.uri = { 0, NULL }; | |
1530 * conf->upstream.location = NULL; | |
1531 * | |
479 | 1532 * conf->index.len = 0; |
1533 * conf->index.data = NULL; | |
1534 */ | |
1535 | |
581 | 1536 conf->upstream.buffering = NGX_CONF_UNSET; |
629 | 1537 conf->upstream.ignore_client_abort = NGX_CONF_UNSET; |
581 | 1538 |
479 | 1539 conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC; |
1540 conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC; | |
507 | 1541 conf->upstream.read_timeout = NGX_CONF_UNSET_MSEC; |
1542 | |
479 | 1543 conf->upstream.send_lowat = NGX_CONF_UNSET_SIZE; |
581 | 1544 conf->upstream.buffer_size = NGX_CONF_UNSET_SIZE; |
529 | 1545 |
1546 conf->upstream.busy_buffers_size_conf = NGX_CONF_UNSET_SIZE; | |
577 | 1547 conf->upstream.max_temp_file_size_conf = NGX_CONF_UNSET_SIZE; |
529 | 1548 conf->upstream.temp_file_write_size_conf = NGX_CONF_UNSET_SIZE; |
509 | 1549 |
561 | 1550 conf->upstream.max_fails = NGX_CONF_UNSET_UINT; |
1551 conf->upstream.fail_timeout = NGX_CONF_UNSET; | |
1552 | |
509 | 1553 conf->upstream.pass_request_headers = NGX_CONF_UNSET; |
1554 conf->upstream.pass_request_body = NGX_CONF_UNSET; | |
1555 | |
657 | 1556 conf->upstream.intercept_errors = NGX_CONF_UNSET; |
479 | 1557 |
1558 /* "fastcgi_cyclic_temp_file" is disabled */ | |
1559 conf->upstream.cyclic_temp_file = 0; | |
1560 | |
1561 return conf; | |
1562 } | |
1563 | |
1564 | |
487 | 1565 static char * |
1566 ngx_http_fastcgi_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) | |
479 | 1567 { |
1568 ngx_http_fastcgi_loc_conf_t *prev = parent; | |
1569 ngx_http_fastcgi_loc_conf_t *conf = child; | |
1570 | |
509 | 1571 u_char *p; |
1572 size_t size; | |
1573 uintptr_t *code; | |
649 | 1574 ngx_str_t *header; |
1575 ngx_uint_t i, j; | |
663 | 1576 ngx_peer_t *peer; |
649 | 1577 ngx_array_t hide_headers; |
1578 ngx_keyval_t *src; | |
1579 ngx_hash_key_t *hk; | |
1580 ngx_hash_init_t hash; | |
509 | 1581 ngx_http_script_compile_t sc; |
1582 ngx_http_script_copy_code_t *copy; | |
479 | 1583 |
581 | 1584 ngx_conf_merge_value(conf->upstream.buffering, |
1585 prev->upstream.buffering, 1); | |
1586 | |
629 | 1587 ngx_conf_merge_value(conf->upstream.ignore_client_abort, |
1588 prev->upstream.ignore_client_abort, 0); | |
1589 | |
479 | 1590 ngx_conf_merge_msec_value(conf->upstream.connect_timeout, |
1591 prev->upstream.connect_timeout, 60000); | |
507 | 1592 |
479 | 1593 ngx_conf_merge_msec_value(conf->upstream.send_timeout, |
1594 prev->upstream.send_timeout, 60000); | |
1595 | |
1596 ngx_conf_merge_msec_value(conf->upstream.read_timeout, | |
1597 prev->upstream.read_timeout, 60000); | |
1598 | |
507 | 1599 ngx_conf_merge_size_value(conf->upstream.send_lowat, |
1600 prev->upstream.send_lowat, 0); | |
479 | 1601 |
581 | 1602 ngx_conf_merge_size_value(conf->upstream.buffer_size, |
1603 prev->upstream.buffer_size, | |
479 | 1604 (size_t) ngx_pagesize); |
1605 | |
507 | 1606 |
479 | 1607 ngx_conf_merge_bufs_value(conf->upstream.bufs, prev->upstream.bufs, |
1608 8, ngx_pagesize); | |
1609 | |
1610 if (conf->upstream.bufs.num < 2) { | |
1611 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
1612 "there must be at least 2 \"fastcgi_buffers\""); | |
1613 return NGX_CONF_ERROR; | |
1614 } | |
1615 | |
1616 | |
581 | 1617 size = conf->upstream.buffer_size; |
479 | 1618 if (size < conf->upstream.bufs.size) { |
1619 size = conf->upstream.bufs.size; | |
1620 } | |
1621 | |
1622 | |
529 | 1623 ngx_conf_merge_size_value(conf->upstream.busy_buffers_size_conf, |
1624 prev->upstream.busy_buffers_size_conf, | |
479 | 1625 NGX_CONF_UNSET_SIZE); |
1626 | |
529 | 1627 if (conf->upstream.busy_buffers_size_conf == NGX_CONF_UNSET_SIZE) { |
479 | 1628 conf->upstream.busy_buffers_size = 2 * size; |
529 | 1629 } else { |
1630 conf->upstream.busy_buffers_size = | |
1631 conf->upstream.busy_buffers_size_conf; | |
1632 } | |
479 | 1633 |
529 | 1634 if (conf->upstream.busy_buffers_size < size) { |
479 | 1635 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
1636 "\"fastcgi_busy_buffers_size\" must be equal or bigger than " | |
1637 "maximum of the value of \"fastcgi_header_buffer_size\" and " | |
1638 "one of the \"fastcgi_buffers\""); | |
1639 | |
1640 return NGX_CONF_ERROR; | |
529 | 1641 } |
479 | 1642 |
529 | 1643 if (conf->upstream.busy_buffers_size |
1644 > (conf->upstream.bufs.num - 1) * conf->upstream.bufs.size) | |
479 | 1645 { |
1646 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
1647 "\"fastcgi_busy_buffers_size\" must be less than " | |
1648 "the size of all \"fastcgi_buffers\" minus one buffer"); | |
1649 | |
1650 return NGX_CONF_ERROR; | |
1651 } | |
1652 | |
1653 | |
529 | 1654 ngx_conf_merge_size_value(conf->upstream.temp_file_write_size_conf, |
1655 prev->upstream.temp_file_write_size_conf, | |
479 | 1656 NGX_CONF_UNSET_SIZE); |
1657 | |
529 | 1658 if (conf->upstream.temp_file_write_size_conf == NGX_CONF_UNSET_SIZE) { |
479 | 1659 conf->upstream.temp_file_write_size = 2 * size; |
529 | 1660 } else { |
1661 conf->upstream.temp_file_write_size = | |
1662 conf->upstream.temp_file_write_size_conf; | |
1663 } | |
479 | 1664 |
529 | 1665 if (conf->upstream.temp_file_write_size < size) { |
479 | 1666 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
1667 "\"fastcgi_temp_file_write_size\" must be equal or bigger than " | |
1668 "maximum of the value of \"fastcgi_header_buffer_size\" and " | |
1669 "one of the \"fastcgi_buffers\""); | |
1670 | |
1671 return NGX_CONF_ERROR; | |
1672 } | |
1673 | |
1674 | |
529 | 1675 ngx_conf_merge_size_value(conf->upstream.max_temp_file_size_conf, |
1676 prev->upstream.max_temp_file_size_conf, | |
479 | 1677 NGX_CONF_UNSET_SIZE); |
1678 | |
529 | 1679 if (conf->upstream.max_temp_file_size_conf == NGX_CONF_UNSET_SIZE) { |
479 | 1680 conf->upstream.max_temp_file_size = 1024 * 1024 * 1024; |
529 | 1681 } else { |
1682 conf->upstream.max_temp_file_size = | |
1683 conf->upstream.max_temp_file_size_conf; | |
1684 } | |
479 | 1685 |
529 | 1686 if (conf->upstream.max_temp_file_size != 0 |
1687 && conf->upstream.max_temp_file_size < size) | |
479 | 1688 { |
1689 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
1690 "\"fastcgi_max_temp_file_size\" must be equal to zero to disable " | |
1691 "the temporary files usage or must be equal or bigger than " | |
1692 "maximum of the value of \"fastcgi_header_buffer_size\" and " | |
1693 "one of the \"fastcgi_buffers\""); | |
1694 | |
1695 return NGX_CONF_ERROR; | |
1696 } | |
1697 | |
1698 | |
1699 ngx_conf_merge_bitmask_value(conf->upstream.next_upstream, | |
487 | 1700 prev->upstream.next_upstream, |
1701 (NGX_CONF_BITMASK_SET | |
1702 |NGX_HTTP_UPSTREAM_FT_ERROR | |
1703 |NGX_HTTP_UPSTREAM_FT_TIMEOUT)); | |
479 | 1704 |
665 | 1705 if (conf->upstream.next_upstream & NGX_HTTP_UPSTREAM_FT_OFF) { |
1706 conf->upstream.next_upstream = NGX_CONF_BITMASK_SET | |
1707 |NGX_HTTP_UPSTREAM_FT_OFF; | |
1708 } | |
1709 | |
663 | 1710 ngx_conf_merge_uint_value(conf->upstream.max_fails, |
561 | 1711 prev->upstream.max_fails, 1); |
1712 | |
1713 ngx_conf_merge_sec_value(conf->upstream.fail_timeout, | |
1714 prev->upstream.fail_timeout, 10); | |
1715 | |
663 | 1716 if (conf->upstream_peers) { |
1717 peer = conf->upstream_peers->peers->peer; | |
651 | 1718 for (i = 0; i < conf->upstream_peers->peers->number; i++) { |
663 | 1719 ngx_conf_init_uint_value(peer[i].weight, 1); |
1720 peer[i].current_weight = peer[i].weight; | |
1721 ngx_conf_init_uint_value(peer[i].max_fails, | |
1722 conf->upstream.max_fails); | |
1723 ngx_conf_init_value(peer[i].fail_timeout, | |
1724 conf->upstream.fail_timeout); | |
561 | 1725 } |
663 | 1726 |
561 | 1727 } |
1728 | |
479 | 1729 ngx_conf_merge_path_value(conf->upstream.temp_path, |
1730 prev->upstream.temp_path, | |
1731 NGX_HTTP_FASTCGI_TEMP_PATH, 1, 2, 0, | |
1732 ngx_garbage_collector_temp_handler, cf); | |
1733 | |
509 | 1734 ngx_conf_merge_value(conf->upstream.pass_request_headers, |
1735 prev->upstream.pass_request_headers, 1); | |
1736 ngx_conf_merge_value(conf->upstream.pass_request_body, | |
1737 prev->upstream.pass_request_body, 1); | |
1738 | |
657 | 1739 ngx_conf_merge_value(conf->upstream.intercept_errors, |
1740 prev->upstream.intercept_errors, 0); | |
509 | 1741 |
507 | 1742 |
479 | 1743 ngx_conf_merge_str_value(conf->index, prev->index, ""); |
1744 | |
649 | 1745 if (conf->upstream.hide_headers == NULL |
1746 && conf->upstream.pass_headers == NULL) | |
1747 { | |
1748 conf->upstream.hide_headers = prev->upstream.hide_headers; | |
1749 conf->upstream.pass_headers = prev->upstream.pass_headers; | |
1750 conf->upstream.hide_headers_hash = prev->upstream.hide_headers_hash; | |
1751 | |
1752 if (conf->upstream.hide_headers_hash.buckets) { | |
1753 goto peers; | |
1754 } | |
1755 | |
1756 } else { | |
1757 if (conf->upstream.hide_headers == NULL) { | |
1758 conf->upstream.hide_headers = prev->upstream.hide_headers; | |
1759 } | |
1760 | |
1761 if (conf->upstream.pass_headers == NULL) { | |
1762 conf->upstream.pass_headers = prev->upstream.pass_headers; | |
1763 } | |
1764 } | |
1765 | |
1766 if (ngx_array_init(&hide_headers, cf->temp_pool, 4, sizeof(ngx_hash_key_t)) | |
1767 != NGX_OK) | |
1768 { | |
1769 return NGX_CONF_ERROR; | |
1770 } | |
1771 | |
1772 for (header = ngx_http_fastcgi_hide_headers; header->len; header++) { | |
1773 hk = ngx_array_push(&hide_headers); | |
1774 if (hk == NULL) { | |
1775 return NGX_CONF_ERROR; | |
1776 } | |
1777 | |
1778 hk->key = *header; | |
1779 hk->key_hash = ngx_hash_key_lc(header->data, header->len); | |
1780 hk->value = (void *) 1; | |
1781 } | |
1782 | |
1783 if (conf->upstream.hide_headers) { | |
1784 | |
1785 header = conf->upstream.hide_headers->elts; | |
1786 | |
1787 for (i = 0; i < conf->upstream.hide_headers->nelts; i++) { | |
1788 | |
1789 hk = hide_headers.elts; | |
1790 | |
1791 for (j = 0; j < hide_headers.nelts; j++) { | |
1792 if (ngx_strcasecmp(header[i].data, hk[j].key.data) == 0) { | |
1793 goto exist; | |
1794 } | |
1795 } | |
1796 | |
1797 hk = ngx_array_push(&hide_headers); | |
1798 if (hk == NULL) { | |
1799 return NGX_CONF_ERROR; | |
1800 } | |
1801 | |
1802 hk->key = header[i]; | |
1803 hk->key_hash = ngx_hash_key_lc(header[i].data, header[i].len); | |
1804 hk->value = (void *) 1; | |
1805 | |
1806 exist: | |
1807 | |
1808 continue; | |
1809 } | |
1810 } | |
1811 | |
1812 if (conf->upstream.pass_headers) { | |
1813 | |
1814 hk = hide_headers.elts; | |
1815 header = conf->upstream.pass_headers->elts; | |
1816 | |
1817 for (i = 0; i < conf->upstream.pass_headers->nelts; i++) { | |
1818 | |
1819 for (j = 0; j < hide_headers.nelts; j++) { | |
1820 | |
1821 if (hk[j].key.data == NULL) { | |
1822 continue; | |
1823 } | |
1824 | |
1825 if (ngx_strcasecmp(header[i].data, hk[j].key.data) == 0) { | |
1826 hk[j].key.data = NULL; | |
1827 break; | |
1828 } | |
1829 } | |
1830 } | |
1831 } | |
1832 | |
1833 hash.hash = &conf->upstream.hide_headers_hash; | |
1834 hash.key = ngx_hash_key_lc; | |
1835 hash.max_size = 512; | |
751
bae59a740c40
align hash bucket size to cache line
Igor Sysoev <igor@sysoev.ru>
parents:
750
diff
changeset
|
1836 hash.bucket_size = ngx_align(64, ngx_cacheline_size); |
649 | 1837 hash.name = "fastcgi_hide_headers_hash"; |
1838 hash.pool = cf->pool; | |
1839 hash.temp_pool = NULL; | |
1840 | |
1841 if (ngx_hash_init(&hash, hide_headers.elts, hide_headers.nelts) != NGX_OK) { | |
1842 return NGX_CONF_ERROR; | |
1843 } | |
1844 | |
1845 peers: | |
1846 | |
651 | 1847 if (conf->upstream_peers == NULL) { |
1848 conf->upstream_peers = prev->upstream_peers; | |
629 | 1849 conf->upstream.schema = prev->upstream.schema; |
507 | 1850 } |
1851 | |
509 | 1852 if (conf->params_source == NULL) { |
573 | 1853 conf->flushes = prev->flushes; |
509 | 1854 conf->params_len = prev->params_len; |
1855 conf->params = prev->params; | |
573 | 1856 conf->params_source = prev->params_source; |
509 | 1857 |
1858 if (conf->params_source == NULL) { | |
1859 return NGX_CONF_OK; | |
1860 } | |
1861 } | |
1862 | |
1863 conf->params_len = ngx_array_create(cf->pool, 64, 1); | |
1864 if (conf->params_len == NULL) { | |
1865 return NGX_CONF_ERROR; | |
1866 } | |
573 | 1867 |
509 | 1868 conf->params = ngx_array_create(cf->pool, 512, 1); |
1869 if (conf->params == NULL) { | |
1870 return NGX_CONF_ERROR; | |
1871 } | |
1872 | |
1873 src = conf->params_source->elts; | |
1874 for (i = 0; i < conf->params_source->nelts; i++) { | |
1875 | |
1876 if (ngx_http_script_variables_count(&src[i].value) == 0) { | |
1877 copy = ngx_array_push_n(conf->params_len, | |
1878 sizeof(ngx_http_script_copy_code_t)); | |
1879 if (copy == NULL) { | |
1880 return NGX_CONF_ERROR; | |
1881 } | |
1882 | |
1883 copy->code = (ngx_http_script_code_pt) | |
1884 ngx_http_script_copy_len_code; | |
1885 copy->len = src[i].key.len; | |
1886 | |
1887 | |
1888 copy = ngx_array_push_n(conf->params_len, | |
1889 sizeof(ngx_http_script_copy_code_t)); | |
1890 if (copy == NULL) { | |
1891 return NGX_CONF_ERROR; | |
1892 } | |
1893 | |
1894 copy->code = (ngx_http_script_code_pt) | |
1895 ngx_http_script_copy_len_code; | |
1896 copy->len = src[i].value.len; | |
1897 | |
1898 | |
1899 size = (sizeof(ngx_http_script_copy_code_t) | |
1900 + src[i].key.len + src[i].value.len | |
1901 + sizeof(uintptr_t) - 1) | |
1902 & ~(sizeof(uintptr_t) - 1); | |
1903 | |
1904 copy = ngx_array_push_n(conf->params, size); | |
1905 if (copy == NULL) { | |
1906 return NGX_CONF_ERROR; | |
1907 } | |
577 | 1908 |
509 | 1909 copy->code = ngx_http_script_copy_code; |
1910 copy->len = src[i].key.len + src[i].value.len; | |
1911 | |
1912 p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t); | |
1913 | |
1914 p = ngx_cpymem(p, src[i].key.data, src[i].key.len); | |
1915 ngx_memcpy(p, src[i].value.data, src[i].value.len); | |
1916 | |
1917 } else { | |
1918 copy = ngx_array_push_n(conf->params_len, | |
1919 sizeof(ngx_http_script_copy_code_t)); | |
1920 if (copy == NULL) { | |
1921 return NGX_CONF_ERROR; | |
1922 } | |
1923 | |
1924 copy->code = (ngx_http_script_code_pt) | |
1925 ngx_http_script_copy_len_code; | |
1926 copy->len = src[i].key.len; | |
1927 | |
1928 | |
1929 size = (sizeof(ngx_http_script_copy_code_t) | |
1930 + src[i].key.len + sizeof(uintptr_t) - 1) | |
1931 & ~(sizeof(uintptr_t) - 1); | |
1932 | |
1933 copy = ngx_array_push_n(conf->params, size); | |
1934 if (copy == NULL) { | |
1935 return NGX_CONF_ERROR; | |
1936 } | |
577 | 1937 |
509 | 1938 copy->code = ngx_http_script_copy_code; |
1939 copy->len = src[i].key.len; | |
1940 | |
1941 p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t); | |
1942 ngx_memcpy(p, src[i].key.data, src[i].key.len); | |
1943 | |
1944 | |
1945 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t)); | |
1946 | |
1947 sc.cf = cf; | |
1948 sc.source = &src[i].value; | |
573 | 1949 sc.flushes = &conf->flushes; |
509 | 1950 sc.lengths = &conf->params_len; |
1951 sc.values = &conf->params; | |
1952 | |
1953 if (ngx_http_script_compile(&sc) != NGX_OK) { | |
1954 return NGX_CONF_ERROR; | |
1955 } | |
1956 } | |
1957 | |
1958 code = ngx_array_push_n(conf->params_len, sizeof(uintptr_t)); | |
1959 if (code == NULL) { | |
1960 return NGX_CONF_ERROR; | |
1961 } | |
1962 | |
1963 *code = (uintptr_t) NULL; | |
1964 | |
1965 | |
1966 code = ngx_array_push_n(conf->params, sizeof(uintptr_t)); | |
1967 if (code == NULL) { | |
1968 return NGX_CONF_ERROR; | |
1969 } | |
1970 | |
1971 *code = (uintptr_t) NULL; | |
1972 } | |
1973 | |
1974 code = ngx_array_push_n(conf->params_len, sizeof(uintptr_t)); | |
1975 if (code == NULL) { | |
1976 return NGX_CONF_ERROR; | |
1977 } | |
1978 | |
1979 *code = (uintptr_t) NULL; | |
1980 | |
479 | 1981 return NGX_CONF_OK; |
1982 } | |
509 | 1983 |
1984 | |
573 | 1985 static ngx_int_t |
1986 ngx_http_fastcgi_script_name_variable(ngx_http_request_t *r, | |
1987 ngx_http_variable_value_t *v, uintptr_t data) | |
509 | 1988 { |
1989 u_char *p; | |
1990 ngx_http_fastcgi_loc_conf_t *flcf; | |
1991 | |
577 | 1992 v->valid = 1; |
573 | 1993 v->no_cachable = 0; |
1994 v->not_found = 0; | |
509 | 1995 |
1996 flcf = ngx_http_get_module_loc_conf(r, ngx_http_fastcgi_module); | |
1997 | |
1998 if (r->uri.data[r->uri.len - 1] != '/') { | |
573 | 1999 v->len = r->uri.len; |
2000 v->data = r->uri.data; | |
2001 return NGX_OK; | |
509 | 2002 } |
2003 | |
573 | 2004 v->len = r->uri.len + flcf->index.len; |
509 | 2005 |
573 | 2006 v->data = ngx_palloc(r->pool, v->len); |
2007 if (v->data == NULL) { | |
2008 return NGX_ERROR; | |
509 | 2009 } |
2010 | |
573 | 2011 p = ngx_copy(v->data, r->uri.data, r->uri.len); |
509 | 2012 ngx_memcpy(p, flcf->index.data, flcf->index.len); |
2013 | |
573 | 2014 return NGX_OK; |
509 | 2015 } |
2016 | |
2017 | |
2018 static char * | |
2019 ngx_http_fastcgi_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
2020 { | |
2021 ngx_http_fastcgi_loc_conf_t *lcf = conf; | |
2022 | |
651 | 2023 ngx_url_t u; |
509 | 2024 ngx_str_t *value; |
2025 ngx_http_core_loc_conf_t *clcf; | |
2026 | |
555 | 2027 if (lcf->upstream.schema.len) { |
2028 return "is duplicate"; | |
2029 } | |
2030 | |
509 | 2031 value = cf->args->elts; |
2032 | |
651 | 2033 ngx_memzero(&u, sizeof(ngx_url_t)); |
2034 | |
2035 u.url = value[1]; | |
2036 u.upstream = 1; | |
2037 | |
2038 lcf->upstream_peers = ngx_http_upstream_add(cf, &u); | |
2039 if (lcf->upstream_peers == NULL) { | |
509 | 2040 return NGX_CONF_ERROR; |
2041 } | |
2042 | |
2043 lcf->upstream.schema.len = sizeof("fastcgi://") - 1; | |
2044 lcf->upstream.schema.data = (u_char *) "fastcgi://"; | |
2045 | |
2046 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); | |
2047 | |
2048 clcf->handler = ngx_http_fastcgi_handler; | |
2049 | |
573 | 2050 lcf->upstream.location = clcf->name; |
509 | 2051 |
2052 if (clcf->name.data[clcf->name.len - 1] == '/') { | |
2053 clcf->auto_redirect = 1; | |
2054 } | |
2055 | |
2056 return NGX_CONF_OK; | |
2057 } | |
2058 | |
2059 | |
2060 static char * | |
2061 ngx_http_fastcgi_lowat_check(ngx_conf_t *cf, void *post, void *data) | |
2062 { | |
2063 #if (NGX_FREEBSD) | |
2064 ssize_t *np = data; | |
2065 | |
673 | 2066 if ((u_long) *np >= ngx_freebsd_net_inet_tcp_sendspace) { |
509 | 2067 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
2068 "\"fastcgi_send_lowat\" must be less than %d " | |
2069 "(sysctl net.inet.tcp.sendspace)", | |
2070 ngx_freebsd_net_inet_tcp_sendspace); | |
2071 | |
2072 return NGX_CONF_ERROR; | |
2073 } | |
2074 | |
2075 #elif !(NGX_HAVE_SO_SNDLOWAT) | |
2076 ssize_t *np = data; | |
2077 | |
2078 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, | |
2079 "\"fastcgi_send_lowat\" is not supported, ignored"); | |
2080 | |
2081 *np = 0; | |
2082 | |
2083 #endif | |
2084 | |
2085 return NGX_CONF_OK; | |
2086 } |