Mercurial > hg > nginx-quic
annotate src/http/modules/ngx_http_fastcgi_module.c @ 990:bc232bbdddf9
undo r979
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Mon, 08 Jan 2007 18:20:03 +0000 |
parents | a0310ac2814f |
children | 5a86fcc480c6 |
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_str_t index; | |
17 | |
18 ngx_array_t *flushes; | |
19 ngx_array_t *params_len; | |
20 ngx_array_t *params; | |
21 ngx_array_t *params_source; | |
479 | 22 } ngx_http_fastcgi_loc_conf_t; |
23 | |
24 | |
25 typedef enum { | |
26 ngx_http_fastcgi_st_version = 0, | |
27 ngx_http_fastcgi_st_type, | |
28 ngx_http_fastcgi_st_request_id_hi, | |
29 ngx_http_fastcgi_st_request_id_lo, | |
30 ngx_http_fastcgi_st_content_length_hi, | |
31 ngx_http_fastcgi_st_content_length_lo, | |
32 ngx_http_fastcgi_st_padding_length, | |
33 ngx_http_fastcgi_st_reserved, | |
34 ngx_http_fastcgi_st_data, | |
515 | 35 ngx_http_fastcgi_st_padding |
479 | 36 } ngx_http_fastcgi_state_e; |
37 | |
38 | |
39 typedef struct { | |
651 | 40 ngx_http_fastcgi_state_e state; |
41 u_char *pos; | |
42 u_char *last; | |
43 ngx_uint_t type; | |
44 size_t length; | |
45 size_t padding; | |
46 | |
47 ngx_uint_t fastcgi_stdout; | |
479 | 48 } ngx_http_fastcgi_ctx_t; |
49 | |
50 | |
51 #define NGX_HTTP_FASTCGI_RESPONDER 1 | |
52 | |
53 #define NGX_HTTP_FASTCGI_BEGIN_REQUEST 1 | |
54 #define NGX_HTTP_FASTCGI_ABORT_REQUEST 2 | |
55 #define NGX_HTTP_FASTCGI_END_REQUEST 3 | |
56 #define NGX_HTTP_FASTCGI_PARAMS 4 | |
57 #define NGX_HTTP_FASTCGI_STDIN 5 | |
58 #define NGX_HTTP_FASTCGI_STDOUT 6 | |
59 #define NGX_HTTP_FASTCGI_STDERR 7 | |
60 #define NGX_HTTP_FASTCGI_DATA 8 | |
61 | |
62 | |
63 typedef struct { | |
64 u_char version; | |
65 u_char type; | |
66 u_char request_id_hi; | |
67 u_char request_id_lo; | |
68 u_char content_length_hi; | |
69 u_char content_length_lo; | |
70 u_char padding_length; | |
71 u_char reserved; | |
72 } ngx_http_fastcgi_header_t; | |
73 | |
74 | |
75 typedef struct { | |
76 u_char role_hi; | |
77 u_char role_lo; | |
78 u_char flags; | |
79 u_char reserved[5]; | |
80 } ngx_http_fastcgi_begin_request_t; | |
81 | |
82 | |
509 | 83 typedef struct { |
84 u_char version; | |
85 u_char type; | |
86 u_char request_id_hi; | |
87 u_char request_id_lo; | |
88 } ngx_http_fastcgi_header_small_t; | |
89 | |
90 | |
91 typedef struct { | |
92 ngx_http_fastcgi_header_t h0; | |
93 ngx_http_fastcgi_begin_request_t br; | |
94 ngx_http_fastcgi_header_small_t h1; | |
95 } ngx_http_fastcgi_request_start_t; | |
96 | |
97 | |
479 | 98 static ngx_int_t ngx_http_fastcgi_create_request(ngx_http_request_t *r); |
99 static ngx_int_t ngx_http_fastcgi_reinit_request(ngx_http_request_t *r); | |
100 static ngx_int_t ngx_http_fastcgi_process_header(ngx_http_request_t *r); | |
101 static ngx_int_t ngx_http_fastcgi_input_filter(ngx_event_pipe_t *p, | |
487 | 102 ngx_buf_t *buf); |
479 | 103 static ngx_int_t ngx_http_fastcgi_process_record(ngx_http_request_t *r, |
487 | 104 ngx_http_fastcgi_ctx_t *f); |
479 | 105 static void ngx_http_fastcgi_abort_request(ngx_http_request_t *r); |
106 static void ngx_http_fastcgi_finalize_request(ngx_http_request_t *r, | |
487 | 107 ngx_int_t rc); |
479 | 108 |
509 | 109 static ngx_int_t ngx_http_fastcgi_add_variables(ngx_conf_t *cf); |
110 static void *ngx_http_fastcgi_create_loc_conf(ngx_conf_t *cf); | |
111 static char *ngx_http_fastcgi_merge_loc_conf(ngx_conf_t *cf, | |
112 void *parent, void *child); | |
573 | 113 static ngx_int_t ngx_http_fastcgi_script_name_variable(ngx_http_request_t *r, |
114 ngx_http_variable_value_t *v, uintptr_t data); | |
509 | 115 |
479 | 116 static char *ngx_http_fastcgi_pass(ngx_conf_t *cf, ngx_command_t *cmd, |
487 | 117 void *conf); |
479 | 118 static char *ngx_http_fastcgi_lowat_check(ngx_conf_t *cf, void *post, |
487 | 119 void *data); |
479 | 120 |
884 | 121 static char *ngx_http_fastcgi_upstream_max_fails_unsupported(ngx_conf_t *cf, |
122 ngx_command_t *cmd, void *conf); | |
123 static char *ngx_http_fastcgi_upstream_fail_timeout_unsupported(ngx_conf_t *cf, | |
124 ngx_command_t *cmd, void *conf); | |
125 | |
479 | 126 |
509 | 127 static ngx_http_fastcgi_request_start_t ngx_http_fastcgi_request_start = { |
128 { 1, /* version */ | |
129 NGX_HTTP_FASTCGI_BEGIN_REQUEST, /* type */ | |
130 0, /* request_id_hi */ | |
131 1, /* request_id_lo */ | |
132 0, /* content_length_hi */ | |
133 sizeof(ngx_http_fastcgi_begin_request_t), /* content_length_lo */ | |
134 0, /* padding_length */ | |
135 0 }, /* reserved */ | |
136 | |
137 { 0, /* role_hi */ | |
138 NGX_HTTP_FASTCGI_RESPONDER, /* role_lo */ | |
139 0, /* NGX_HTTP_FASTCGI_KEEP_CONN */ /* flags */ | |
140 { 0, 0, 0, 0, 0 } }, /* reserved[5] */ | |
141 | |
142 { 1, /* version */ | |
143 NGX_HTTP_FASTCGI_PARAMS, /* type */ | |
144 0, /* request_id_hi */ | |
145 1 }, /* request_id_lo */ | |
146 | |
147 }; | |
148 | |
149 | |
150 static ngx_str_t ngx_http_fastcgi_script_name = | |
151 ngx_string("fastcgi_script_name"); | |
479 | 152 |
153 | |
509 | 154 static ngx_conf_post_t ngx_http_fastcgi_lowat_post = |
155 { ngx_http_fastcgi_lowat_check }; | |
479 | 156 |
581 | 157 static ngx_conf_deprecated_t ngx_conf_deprecated_fastcgi_header_buffer_size = { |
158 ngx_conf_deprecated, "fastcgi_header_buffer_size", "fastcgi_buffer_size" | |
159 }; | |
160 | |
657 | 161 static ngx_conf_deprecated_t ngx_conf_deprecated_fastcgi_redirect_errors = { |
162 ngx_conf_deprecated, "fastcgi_redirect_errors", "fastcgi_intercept_errors" | |
163 }; | |
164 | |
479 | 165 |
166 static ngx_conf_bitmask_t ngx_http_fastcgi_next_upstream_masks[] = { | |
167 { ngx_string("error"), NGX_HTTP_UPSTREAM_FT_ERROR }, | |
168 { ngx_string("timeout"), NGX_HTTP_UPSTREAM_FT_TIMEOUT }, | |
169 { ngx_string("invalid_header"), NGX_HTTP_UPSTREAM_FT_INVALID_HEADER }, | |
170 { ngx_string("http_500"), NGX_HTTP_UPSTREAM_FT_HTTP_500 }, | |
623 | 171 { ngx_string("http_503"), NGX_HTTP_UPSTREAM_FT_HTTP_503 }, |
479 | 172 { ngx_string("http_404"), NGX_HTTP_UPSTREAM_FT_HTTP_404 }, |
665 | 173 { ngx_string("off"), NGX_HTTP_UPSTREAM_FT_OFF }, |
479 | 174 { ngx_null_string, 0 } |
175 }; | |
176 | |
177 | |
178 static ngx_command_t ngx_http_fastcgi_commands[] = { | |
179 | |
180 { ngx_string("fastcgi_pass"), | |
629 | 181 NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1, |
479 | 182 ngx_http_fastcgi_pass, |
183 NGX_HTTP_LOC_CONF_OFFSET, | |
184 0, | |
185 NULL }, | |
186 | |
187 { ngx_string("fastcgi_index"), | |
188 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
189 ngx_conf_set_str_slot, | |
190 NGX_HTTP_LOC_CONF_OFFSET, | |
191 offsetof(ngx_http_fastcgi_loc_conf_t, index), | |
192 NULL }, | |
193 | |
629 | 194 { ngx_string("fastcgi_ignore_client_abort"), |
195 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
196 ngx_conf_set_flag_slot, | |
197 NGX_HTTP_LOC_CONF_OFFSET, | |
198 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.ignore_client_abort), | |
199 NULL }, | |
200 | |
479 | 201 { ngx_string("fastcgi_connect_timeout"), |
202 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
203 ngx_conf_set_msec_slot, | |
204 NGX_HTTP_LOC_CONF_OFFSET, | |
205 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.connect_timeout), | |
206 NULL }, | |
207 | |
208 { ngx_string("fastcgi_send_timeout"), | |
209 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
210 ngx_conf_set_msec_slot, | |
211 NGX_HTTP_LOC_CONF_OFFSET, | |
212 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.send_timeout), | |
213 NULL }, | |
214 | |
215 { ngx_string("fastcgi_send_lowat"), | |
216 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
217 ngx_conf_set_size_slot, | |
218 NGX_HTTP_LOC_CONF_OFFSET, | |
219 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.send_lowat), | |
220 &ngx_http_fastcgi_lowat_post }, | |
221 | |
581 | 222 { ngx_string("fastcgi_buffer_size"), |
223 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
224 ngx_conf_set_size_slot, | |
225 NGX_HTTP_LOC_CONF_OFFSET, | |
226 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.buffer_size), | |
227 NULL }, | |
228 | |
479 | 229 { ngx_string("fastcgi_header_buffer_size"), |
230 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
231 ngx_conf_set_size_slot, | |
232 NGX_HTTP_LOC_CONF_OFFSET, | |
581 | 233 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.buffer_size), |
234 &ngx_conf_deprecated_fastcgi_header_buffer_size }, | |
479 | 235 |
509 | 236 { ngx_string("fastcgi_pass_request_headers"), |
237 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
238 ngx_conf_set_flag_slot, | |
239 NGX_HTTP_LOC_CONF_OFFSET, | |
240 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.pass_request_headers), | |
241 NULL }, | |
242 | |
243 { ngx_string("fastcgi_pass_request_body"), | |
244 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
245 ngx_conf_set_flag_slot, | |
246 NGX_HTTP_LOC_CONF_OFFSET, | |
247 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.pass_request_body), | |
248 NULL }, | |
249 | |
657 | 250 { ngx_string("fastcgi_intercept_errors"), |
251 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
252 ngx_conf_set_flag_slot, | |
253 NGX_HTTP_LOC_CONF_OFFSET, | |
254 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.intercept_errors), | |
255 NULL }, | |
256 | |
487 | 257 { ngx_string("fastcgi_redirect_errors"), |
258 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
259 ngx_conf_set_flag_slot, | |
260 NGX_HTTP_LOC_CONF_OFFSET, | |
657 | 261 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.intercept_errors), |
262 &ngx_conf_deprecated_fastcgi_redirect_errors }, | |
487 | 263 |
479 | 264 { ngx_string("fastcgi_read_timeout"), |
265 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
266 ngx_conf_set_msec_slot, | |
267 NGX_HTTP_LOC_CONF_OFFSET, | |
268 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.read_timeout), | |
269 NULL }, | |
270 | |
271 { ngx_string("fastcgi_buffers"), | |
272 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2, | |
273 ngx_conf_set_bufs_slot, | |
274 NGX_HTTP_LOC_CONF_OFFSET, | |
275 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.bufs), | |
276 NULL }, | |
277 | |
278 { ngx_string("fastcgi_busy_buffers_size"), | |
279 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
280 ngx_conf_set_size_slot, | |
281 NGX_HTTP_LOC_CONF_OFFSET, | |
529 | 282 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.busy_buffers_size_conf), |
479 | 283 NULL }, |
284 | |
285 { ngx_string("fastcgi_temp_path"), | |
286 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1234, | |
287 ngx_conf_set_path_slot, | |
288 NGX_HTTP_LOC_CONF_OFFSET, | |
289 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.temp_path), | |
290 (void *) ngx_garbage_collector_temp_handler }, | |
291 | |
292 { ngx_string("fastcgi_max_temp_file_size"), | |
293 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
294 ngx_conf_set_size_slot, | |
295 NGX_HTTP_LOC_CONF_OFFSET, | |
529 | 296 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.max_temp_file_size_conf), |
479 | 297 NULL }, |
298 | |
299 { ngx_string("fastcgi_temp_file_write_size"), | |
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, | |
529 | 303 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.temp_file_write_size_conf), |
479 | 304 NULL }, |
305 | |
306 { ngx_string("fastcgi_next_upstream"), | |
547 | 307 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE, |
479 | 308 ngx_conf_set_bitmask_slot, |
309 NGX_HTTP_LOC_CONF_OFFSET, | |
310 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.next_upstream), | |
311 &ngx_http_fastcgi_next_upstream_masks }, | |
312 | |
561 | 313 { ngx_string("fastcgi_upstream_max_fails"), |
314 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
884 | 315 ngx_http_fastcgi_upstream_max_fails_unsupported, |
316 0, | |
317 0, | |
561 | 318 NULL }, |
319 | |
320 { ngx_string("fastcgi_upstream_fail_timeout"), | |
321 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
884 | 322 ngx_http_fastcgi_upstream_fail_timeout_unsupported, |
323 0, | |
324 0, | |
561 | 325 NULL }, |
326 | |
509 | 327 { ngx_string("fastcgi_param"), |
328 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2, | |
649 | 329 ngx_conf_set_keyval_slot, |
485 | 330 NGX_HTTP_LOC_CONF_OFFSET, |
509 | 331 offsetof(ngx_http_fastcgi_loc_conf_t, params_source), |
485 | 332 NULL }, |
333 | |
649 | 334 { ngx_string("fastcgi_pass_header"), |
335 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
336 ngx_conf_set_str_array_slot, | |
337 NGX_HTTP_LOC_CONF_OFFSET, | |
338 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.pass_headers), | |
339 NULL }, | |
340 | |
341 { ngx_string("fastcgi_hide_header"), | |
342 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, | |
343 ngx_conf_set_str_array_slot, | |
344 NGX_HTTP_LOC_CONF_OFFSET, | |
345 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.hide_headers), | |
346 NULL }, | |
347 | |
479 | 348 ngx_null_command |
349 }; | |
350 | |
351 | |
667 | 352 static ngx_http_module_t ngx_http_fastcgi_module_ctx = { |
509 | 353 ngx_http_fastcgi_add_variables, /* preconfiguration */ |
354 NULL, /* postconfiguration */ | |
479 | 355 |
356 NULL, /* create main configuration */ | |
357 NULL, /* init main configuration */ | |
358 | |
359 NULL, /* create server configuration */ | |
360 NULL, /* merge server configuration */ | |
361 | |
362 ngx_http_fastcgi_create_loc_conf, /* create location configuration */ | |
363 ngx_http_fastcgi_merge_loc_conf /* merge location configuration */ | |
364 }; | |
365 | |
366 | |
367 ngx_module_t ngx_http_fastcgi_module = { | |
509 | 368 NGX_MODULE_V1, |
479 | 369 &ngx_http_fastcgi_module_ctx, /* module context */ |
370 ngx_http_fastcgi_commands, /* module directives */ | |
371 NGX_HTTP_MODULE, /* module type */ | |
541 | 372 NULL, /* init master */ |
479 | 373 NULL, /* init module */ |
541 | 374 NULL, /* init process */ |
375 NULL, /* init thread */ | |
376 NULL, /* exit thread */ | |
377 NULL, /* exit process */ | |
378 NULL, /* exit master */ | |
379 NGX_MODULE_V1_PADDING | |
479 | 380 }; |
381 | |
382 | |
649 | 383 static ngx_str_t ngx_http_fastcgi_hide_headers[] = { |
384 ngx_string("Status"), | |
385 ngx_string("X-Accel-Expires"), | |
386 ngx_string("X-Accel-Redirect"), | |
387 ngx_string("X-Accel-Limit-Rate"), | |
388 ngx_string("X-Accel-Buffer"), | |
389 ngx_null_string | |
390 }; | |
391 | |
392 | |
487 | 393 static ngx_int_t |
394 ngx_http_fastcgi_handler(ngx_http_request_t *r) | |
479 | 395 { |
396 ngx_int_t rc; | |
397 ngx_http_upstream_t *u; | |
398 ngx_http_fastcgi_loc_conf_t *flcf; | |
399 | |
777
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
751
diff
changeset
|
400 if (r->subrequest_in_memory) { |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
751
diff
changeset
|
401 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
751
diff
changeset
|
402 "ngx_http_fastcgi_module does not support " |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
751
diff
changeset
|
403 "subrequest in memeory"); |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
751
diff
changeset
|
404 return NGX_HTTP_INTERNAL_SERVER_ERROR; |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
751
diff
changeset
|
405 } |
4ab852b691f5
<!--#include virtual=... set=... -->
Igor Sysoev <igor@sysoev.ru>
parents:
751
diff
changeset
|
406 |
479 | 407 flcf = ngx_http_get_module_loc_conf(r, ngx_http_fastcgi_module); |
408 | |
501 | 409 u = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_t)); |
410 if (u == NULL) { | |
479 | 411 return NGX_HTTP_INTERNAL_SERVER_ERROR; |
412 } | |
413 | |
414 u->peer.log = r->connection->log; | |
415 u->peer.log_error = NGX_ERROR_ERR; | |
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 |
1550 conf->upstream.pass_request_headers = NGX_CONF_UNSET; | |
1551 conf->upstream.pass_request_body = NGX_CONF_UNSET; | |
1552 | |
657 | 1553 conf->upstream.intercept_errors = NGX_CONF_UNSET; |
479 | 1554 |
1555 /* "fastcgi_cyclic_temp_file" is disabled */ | |
1556 conf->upstream.cyclic_temp_file = 0; | |
1557 | |
1558 return conf; | |
1559 } | |
1560 | |
1561 | |
487 | 1562 static char * |
1563 ngx_http_fastcgi_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) | |
479 | 1564 { |
1565 ngx_http_fastcgi_loc_conf_t *prev = parent; | |
1566 ngx_http_fastcgi_loc_conf_t *conf = child; | |
1567 | |
509 | 1568 u_char *p; |
1569 size_t size; | |
1570 uintptr_t *code; | |
649 | 1571 ngx_str_t *header; |
1572 ngx_uint_t i, j; | |
1573 ngx_array_t hide_headers; | |
1574 ngx_keyval_t *src; | |
1575 ngx_hash_key_t *hk; | |
1576 ngx_hash_init_t hash; | |
509 | 1577 ngx_http_script_compile_t sc; |
1578 ngx_http_script_copy_code_t *copy; | |
479 | 1579 |
581 | 1580 ngx_conf_merge_value(conf->upstream.buffering, |
1581 prev->upstream.buffering, 1); | |
1582 | |
629 | 1583 ngx_conf_merge_value(conf->upstream.ignore_client_abort, |
1584 prev->upstream.ignore_client_abort, 0); | |
1585 | |
479 | 1586 ngx_conf_merge_msec_value(conf->upstream.connect_timeout, |
1587 prev->upstream.connect_timeout, 60000); | |
507 | 1588 |
479 | 1589 ngx_conf_merge_msec_value(conf->upstream.send_timeout, |
1590 prev->upstream.send_timeout, 60000); | |
1591 | |
1592 ngx_conf_merge_msec_value(conf->upstream.read_timeout, | |
1593 prev->upstream.read_timeout, 60000); | |
1594 | |
507 | 1595 ngx_conf_merge_size_value(conf->upstream.send_lowat, |
1596 prev->upstream.send_lowat, 0); | |
479 | 1597 |
581 | 1598 ngx_conf_merge_size_value(conf->upstream.buffer_size, |
1599 prev->upstream.buffer_size, | |
479 | 1600 (size_t) ngx_pagesize); |
1601 | |
507 | 1602 |
479 | 1603 ngx_conf_merge_bufs_value(conf->upstream.bufs, prev->upstream.bufs, |
1604 8, ngx_pagesize); | |
1605 | |
1606 if (conf->upstream.bufs.num < 2) { | |
1607 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
1608 "there must be at least 2 \"fastcgi_buffers\""); | |
1609 return NGX_CONF_ERROR; | |
1610 } | |
1611 | |
1612 | |
581 | 1613 size = conf->upstream.buffer_size; |
479 | 1614 if (size < conf->upstream.bufs.size) { |
1615 size = conf->upstream.bufs.size; | |
1616 } | |
1617 | |
1618 | |
529 | 1619 ngx_conf_merge_size_value(conf->upstream.busy_buffers_size_conf, |
1620 prev->upstream.busy_buffers_size_conf, | |
479 | 1621 NGX_CONF_UNSET_SIZE); |
1622 | |
529 | 1623 if (conf->upstream.busy_buffers_size_conf == NGX_CONF_UNSET_SIZE) { |
479 | 1624 conf->upstream.busy_buffers_size = 2 * size; |
529 | 1625 } else { |
1626 conf->upstream.busy_buffers_size = | |
1627 conf->upstream.busy_buffers_size_conf; | |
1628 } | |
479 | 1629 |
529 | 1630 if (conf->upstream.busy_buffers_size < size) { |
479 | 1631 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
1632 "\"fastcgi_busy_buffers_size\" must be equal or bigger than " | |
1633 "maximum of the value of \"fastcgi_header_buffer_size\" and " | |
1634 "one of the \"fastcgi_buffers\""); | |
1635 | |
1636 return NGX_CONF_ERROR; | |
529 | 1637 } |
479 | 1638 |
529 | 1639 if (conf->upstream.busy_buffers_size |
1640 > (conf->upstream.bufs.num - 1) * conf->upstream.bufs.size) | |
479 | 1641 { |
1642 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
1643 "\"fastcgi_busy_buffers_size\" must be less than " | |
1644 "the size of all \"fastcgi_buffers\" minus one buffer"); | |
1645 | |
1646 return NGX_CONF_ERROR; | |
1647 } | |
1648 | |
1649 | |
529 | 1650 ngx_conf_merge_size_value(conf->upstream.temp_file_write_size_conf, |
1651 prev->upstream.temp_file_write_size_conf, | |
479 | 1652 NGX_CONF_UNSET_SIZE); |
1653 | |
529 | 1654 if (conf->upstream.temp_file_write_size_conf == NGX_CONF_UNSET_SIZE) { |
479 | 1655 conf->upstream.temp_file_write_size = 2 * size; |
529 | 1656 } else { |
1657 conf->upstream.temp_file_write_size = | |
1658 conf->upstream.temp_file_write_size_conf; | |
1659 } | |
479 | 1660 |
529 | 1661 if (conf->upstream.temp_file_write_size < size) { |
479 | 1662 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
1663 "\"fastcgi_temp_file_write_size\" must be equal or bigger than " | |
1664 "maximum of the value of \"fastcgi_header_buffer_size\" and " | |
1665 "one of the \"fastcgi_buffers\""); | |
1666 | |
1667 return NGX_CONF_ERROR; | |
1668 } | |
1669 | |
1670 | |
529 | 1671 ngx_conf_merge_size_value(conf->upstream.max_temp_file_size_conf, |
1672 prev->upstream.max_temp_file_size_conf, | |
479 | 1673 NGX_CONF_UNSET_SIZE); |
1674 | |
529 | 1675 if (conf->upstream.max_temp_file_size_conf == NGX_CONF_UNSET_SIZE) { |
479 | 1676 conf->upstream.max_temp_file_size = 1024 * 1024 * 1024; |
529 | 1677 } else { |
1678 conf->upstream.max_temp_file_size = | |
1679 conf->upstream.max_temp_file_size_conf; | |
1680 } | |
479 | 1681 |
529 | 1682 if (conf->upstream.max_temp_file_size != 0 |
1683 && conf->upstream.max_temp_file_size < size) | |
479 | 1684 { |
1685 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
1686 "\"fastcgi_max_temp_file_size\" must be equal to zero to disable " | |
1687 "the temporary files usage or must be equal or bigger than " | |
1688 "maximum of the value of \"fastcgi_header_buffer_size\" and " | |
1689 "one of the \"fastcgi_buffers\""); | |
1690 | |
1691 return NGX_CONF_ERROR; | |
1692 } | |
1693 | |
1694 | |
1695 ngx_conf_merge_bitmask_value(conf->upstream.next_upstream, | |
487 | 1696 prev->upstream.next_upstream, |
1697 (NGX_CONF_BITMASK_SET | |
1698 |NGX_HTTP_UPSTREAM_FT_ERROR | |
1699 |NGX_HTTP_UPSTREAM_FT_TIMEOUT)); | |
479 | 1700 |
665 | 1701 if (conf->upstream.next_upstream & NGX_HTTP_UPSTREAM_FT_OFF) { |
1702 conf->upstream.next_upstream = NGX_CONF_BITMASK_SET | |
1703 |NGX_HTTP_UPSTREAM_FT_OFF; | |
1704 } | |
1705 | |
479 | 1706 ngx_conf_merge_path_value(conf->upstream.temp_path, |
1707 prev->upstream.temp_path, | |
1708 NGX_HTTP_FASTCGI_TEMP_PATH, 1, 2, 0, | |
1709 ngx_garbage_collector_temp_handler, cf); | |
1710 | |
509 | 1711 ngx_conf_merge_value(conf->upstream.pass_request_headers, |
1712 prev->upstream.pass_request_headers, 1); | |
1713 ngx_conf_merge_value(conf->upstream.pass_request_body, | |
1714 prev->upstream.pass_request_body, 1); | |
1715 | |
657 | 1716 ngx_conf_merge_value(conf->upstream.intercept_errors, |
1717 prev->upstream.intercept_errors, 0); | |
509 | 1718 |
507 | 1719 |
479 | 1720 ngx_conf_merge_str_value(conf->index, prev->index, ""); |
1721 | |
649 | 1722 if (conf->upstream.hide_headers == NULL |
1723 && conf->upstream.pass_headers == NULL) | |
1724 { | |
1725 conf->upstream.hide_headers = prev->upstream.hide_headers; | |
1726 conf->upstream.pass_headers = prev->upstream.pass_headers; | |
1727 conf->upstream.hide_headers_hash = prev->upstream.hide_headers_hash; | |
1728 | |
1729 if (conf->upstream.hide_headers_hash.buckets) { | |
1730 goto peers; | |
1731 } | |
1732 | |
1733 } else { | |
1734 if (conf->upstream.hide_headers == NULL) { | |
1735 conf->upstream.hide_headers = prev->upstream.hide_headers; | |
1736 } | |
1737 | |
1738 if (conf->upstream.pass_headers == NULL) { | |
1739 conf->upstream.pass_headers = prev->upstream.pass_headers; | |
1740 } | |
1741 } | |
1742 | |
1743 if (ngx_array_init(&hide_headers, cf->temp_pool, 4, sizeof(ngx_hash_key_t)) | |
1744 != NGX_OK) | |
1745 { | |
1746 return NGX_CONF_ERROR; | |
1747 } | |
1748 | |
1749 for (header = ngx_http_fastcgi_hide_headers; header->len; header++) { | |
1750 hk = ngx_array_push(&hide_headers); | |
1751 if (hk == NULL) { | |
1752 return NGX_CONF_ERROR; | |
1753 } | |
1754 | |
1755 hk->key = *header; | |
1756 hk->key_hash = ngx_hash_key_lc(header->data, header->len); | |
1757 hk->value = (void *) 1; | |
1758 } | |
1759 | |
1760 if (conf->upstream.hide_headers) { | |
1761 | |
1762 header = conf->upstream.hide_headers->elts; | |
1763 | |
1764 for (i = 0; i < conf->upstream.hide_headers->nelts; i++) { | |
1765 | |
1766 hk = hide_headers.elts; | |
1767 | |
1768 for (j = 0; j < hide_headers.nelts; j++) { | |
1769 if (ngx_strcasecmp(header[i].data, hk[j].key.data) == 0) { | |
1770 goto exist; | |
1771 } | |
1772 } | |
1773 | |
1774 hk = ngx_array_push(&hide_headers); | |
1775 if (hk == NULL) { | |
1776 return NGX_CONF_ERROR; | |
1777 } | |
1778 | |
1779 hk->key = header[i]; | |
1780 hk->key_hash = ngx_hash_key_lc(header[i].data, header[i].len); | |
1781 hk->value = (void *) 1; | |
1782 | |
1783 exist: | |
1784 | |
1785 continue; | |
1786 } | |
1787 } | |
1788 | |
1789 if (conf->upstream.pass_headers) { | |
1790 | |
1791 hk = hide_headers.elts; | |
1792 header = conf->upstream.pass_headers->elts; | |
1793 | |
1794 for (i = 0; i < conf->upstream.pass_headers->nelts; i++) { | |
1795 | |
1796 for (j = 0; j < hide_headers.nelts; j++) { | |
1797 | |
1798 if (hk[j].key.data == NULL) { | |
1799 continue; | |
1800 } | |
1801 | |
1802 if (ngx_strcasecmp(header[i].data, hk[j].key.data) == 0) { | |
1803 hk[j].key.data = NULL; | |
1804 break; | |
1805 } | |
1806 } | |
1807 } | |
1808 } | |
1809 | |
1810 hash.hash = &conf->upstream.hide_headers_hash; | |
1811 hash.key = ngx_hash_key_lc; | |
1812 hash.max_size = 512; | |
751
bae59a740c40
align hash bucket size to cache line
Igor Sysoev <igor@sysoev.ru>
parents:
750
diff
changeset
|
1813 hash.bucket_size = ngx_align(64, ngx_cacheline_size); |
649 | 1814 hash.name = "fastcgi_hide_headers_hash"; |
1815 hash.pool = cf->pool; | |
1816 hash.temp_pool = NULL; | |
1817 | |
1818 if (ngx_hash_init(&hash, hide_headers.elts, hide_headers.nelts) != NGX_OK) { | |
1819 return NGX_CONF_ERROR; | |
1820 } | |
1821 | |
1822 peers: | |
1823 | |
884 | 1824 if (conf->upstream.upstream == NULL) { |
1825 conf->upstream.upstream = prev->upstream.upstream; | |
629 | 1826 conf->upstream.schema = prev->upstream.schema; |
507 | 1827 } |
1828 | |
509 | 1829 if (conf->params_source == NULL) { |
573 | 1830 conf->flushes = prev->flushes; |
509 | 1831 conf->params_len = prev->params_len; |
1832 conf->params = prev->params; | |
573 | 1833 conf->params_source = prev->params_source; |
509 | 1834 |
1835 if (conf->params_source == NULL) { | |
1836 return NGX_CONF_OK; | |
1837 } | |
1838 } | |
1839 | |
1840 conf->params_len = ngx_array_create(cf->pool, 64, 1); | |
1841 if (conf->params_len == NULL) { | |
1842 return NGX_CONF_ERROR; | |
1843 } | |
573 | 1844 |
509 | 1845 conf->params = ngx_array_create(cf->pool, 512, 1); |
1846 if (conf->params == NULL) { | |
1847 return NGX_CONF_ERROR; | |
1848 } | |
1849 | |
1850 src = conf->params_source->elts; | |
1851 for (i = 0; i < conf->params_source->nelts; i++) { | |
1852 | |
1853 if (ngx_http_script_variables_count(&src[i].value) == 0) { | |
1854 copy = ngx_array_push_n(conf->params_len, | |
1855 sizeof(ngx_http_script_copy_code_t)); | |
1856 if (copy == NULL) { | |
1857 return NGX_CONF_ERROR; | |
1858 } | |
1859 | |
1860 copy->code = (ngx_http_script_code_pt) | |
1861 ngx_http_script_copy_len_code; | |
1862 copy->len = src[i].key.len; | |
1863 | |
1864 | |
1865 copy = ngx_array_push_n(conf->params_len, | |
1866 sizeof(ngx_http_script_copy_code_t)); | |
1867 if (copy == NULL) { | |
1868 return NGX_CONF_ERROR; | |
1869 } | |
1870 | |
1871 copy->code = (ngx_http_script_code_pt) | |
1872 ngx_http_script_copy_len_code; | |
1873 copy->len = src[i].value.len; | |
1874 | |
1875 | |
1876 size = (sizeof(ngx_http_script_copy_code_t) | |
1877 + src[i].key.len + src[i].value.len | |
1878 + sizeof(uintptr_t) - 1) | |
1879 & ~(sizeof(uintptr_t) - 1); | |
1880 | |
1881 copy = ngx_array_push_n(conf->params, size); | |
1882 if (copy == NULL) { | |
1883 return NGX_CONF_ERROR; | |
1884 } | |
577 | 1885 |
509 | 1886 copy->code = ngx_http_script_copy_code; |
1887 copy->len = src[i].key.len + src[i].value.len; | |
1888 | |
1889 p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t); | |
1890 | |
1891 p = ngx_cpymem(p, src[i].key.data, src[i].key.len); | |
1892 ngx_memcpy(p, src[i].value.data, src[i].value.len); | |
1893 | |
1894 } else { | |
1895 copy = ngx_array_push_n(conf->params_len, | |
1896 sizeof(ngx_http_script_copy_code_t)); | |
1897 if (copy == NULL) { | |
1898 return NGX_CONF_ERROR; | |
1899 } | |
1900 | |
1901 copy->code = (ngx_http_script_code_pt) | |
1902 ngx_http_script_copy_len_code; | |
1903 copy->len = src[i].key.len; | |
1904 | |
1905 | |
1906 size = (sizeof(ngx_http_script_copy_code_t) | |
1907 + src[i].key.len + sizeof(uintptr_t) - 1) | |
1908 & ~(sizeof(uintptr_t) - 1); | |
1909 | |
1910 copy = ngx_array_push_n(conf->params, size); | |
1911 if (copy == NULL) { | |
1912 return NGX_CONF_ERROR; | |
1913 } | |
577 | 1914 |
509 | 1915 copy->code = ngx_http_script_copy_code; |
1916 copy->len = src[i].key.len; | |
1917 | |
1918 p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t); | |
1919 ngx_memcpy(p, src[i].key.data, src[i].key.len); | |
1920 | |
1921 | |
1922 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t)); | |
1923 | |
1924 sc.cf = cf; | |
1925 sc.source = &src[i].value; | |
573 | 1926 sc.flushes = &conf->flushes; |
509 | 1927 sc.lengths = &conf->params_len; |
1928 sc.values = &conf->params; | |
1929 | |
1930 if (ngx_http_script_compile(&sc) != NGX_OK) { | |
1931 return NGX_CONF_ERROR; | |
1932 } | |
1933 } | |
1934 | |
1935 code = ngx_array_push_n(conf->params_len, sizeof(uintptr_t)); | |
1936 if (code == NULL) { | |
1937 return NGX_CONF_ERROR; | |
1938 } | |
1939 | |
1940 *code = (uintptr_t) NULL; | |
1941 | |
1942 | |
1943 code = ngx_array_push_n(conf->params, sizeof(uintptr_t)); | |
1944 if (code == NULL) { | |
1945 return NGX_CONF_ERROR; | |
1946 } | |
1947 | |
1948 *code = (uintptr_t) NULL; | |
1949 } | |
1950 | |
1951 code = ngx_array_push_n(conf->params_len, sizeof(uintptr_t)); | |
1952 if (code == NULL) { | |
1953 return NGX_CONF_ERROR; | |
1954 } | |
1955 | |
1956 *code = (uintptr_t) NULL; | |
1957 | |
479 | 1958 return NGX_CONF_OK; |
1959 } | |
509 | 1960 |
1961 | |
573 | 1962 static ngx_int_t |
1963 ngx_http_fastcgi_script_name_variable(ngx_http_request_t *r, | |
1964 ngx_http_variable_value_t *v, uintptr_t data) | |
509 | 1965 { |
1966 u_char *p; | |
1967 ngx_http_fastcgi_loc_conf_t *flcf; | |
1968 | |
577 | 1969 v->valid = 1; |
573 | 1970 v->no_cachable = 0; |
1971 v->not_found = 0; | |
509 | 1972 |
1973 flcf = ngx_http_get_module_loc_conf(r, ngx_http_fastcgi_module); | |
1974 | |
1975 if (r->uri.data[r->uri.len - 1] != '/') { | |
573 | 1976 v->len = r->uri.len; |
1977 v->data = r->uri.data; | |
1978 return NGX_OK; | |
509 | 1979 } |
1980 | |
573 | 1981 v->len = r->uri.len + flcf->index.len; |
509 | 1982 |
573 | 1983 v->data = ngx_palloc(r->pool, v->len); |
1984 if (v->data == NULL) { | |
1985 return NGX_ERROR; | |
509 | 1986 } |
1987 | |
573 | 1988 p = ngx_copy(v->data, r->uri.data, r->uri.len); |
509 | 1989 ngx_memcpy(p, flcf->index.data, flcf->index.len); |
1990 | |
573 | 1991 return NGX_OK; |
509 | 1992 } |
1993 | |
1994 | |
1995 static char * | |
1996 ngx_http_fastcgi_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
1997 { | |
1998 ngx_http_fastcgi_loc_conf_t *lcf = conf; | |
1999 | |
651 | 2000 ngx_url_t u; |
509 | 2001 ngx_str_t *value; |
2002 ngx_http_core_loc_conf_t *clcf; | |
2003 | |
555 | 2004 if (lcf->upstream.schema.len) { |
2005 return "is duplicate"; | |
2006 } | |
2007 | |
509 | 2008 value = cf->args->elts; |
2009 | |
651 | 2010 ngx_memzero(&u, sizeof(ngx_url_t)); |
2011 | |
2012 u.url = value[1]; | |
884 | 2013 u.no_resolve = 1; |
2014 | |
2015 lcf->upstream.upstream = ngx_http_upstream_add(cf, &u, 0); | |
2016 if (lcf->upstream.upstream == NULL) { | |
509 | 2017 return NGX_CONF_ERROR; |
2018 } | |
2019 | |
2020 lcf->upstream.schema.len = sizeof("fastcgi://") - 1; | |
2021 lcf->upstream.schema.data = (u_char *) "fastcgi://"; | |
2022 | |
2023 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); | |
2024 | |
2025 clcf->handler = ngx_http_fastcgi_handler; | |
2026 | |
573 | 2027 lcf->upstream.location = clcf->name; |
509 | 2028 |
2029 if (clcf->name.data[clcf->name.len - 1] == '/') { | |
2030 clcf->auto_redirect = 1; | |
2031 } | |
2032 | |
2033 return NGX_CONF_OK; | |
2034 } | |
2035 | |
2036 | |
2037 static char * | |
2038 ngx_http_fastcgi_lowat_check(ngx_conf_t *cf, void *post, void *data) | |
2039 { | |
2040 #if (NGX_FREEBSD) | |
2041 ssize_t *np = data; | |
2042 | |
673 | 2043 if ((u_long) *np >= ngx_freebsd_net_inet_tcp_sendspace) { |
509 | 2044 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
2045 "\"fastcgi_send_lowat\" must be less than %d " | |
2046 "(sysctl net.inet.tcp.sendspace)", | |
2047 ngx_freebsd_net_inet_tcp_sendspace); | |
2048 | |
2049 return NGX_CONF_ERROR; | |
2050 } | |
2051 | |
2052 #elif !(NGX_HAVE_SO_SNDLOWAT) | |
2053 ssize_t *np = data; | |
2054 | |
2055 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, | |
2056 "\"fastcgi_send_lowat\" is not supported, ignored"); | |
2057 | |
2058 *np = 0; | |
2059 | |
2060 #endif | |
2061 | |
2062 return NGX_CONF_OK; | |
2063 } | |
884 | 2064 |
2065 | |
2066 static char * | |
2067 ngx_http_fastcgi_upstream_max_fails_unsupported(ngx_conf_t *cf, | |
2068 ngx_command_t *cmd, void *conf) | |
2069 { | |
2070 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
2071 "\"fastcgi_upstream_max_fails\" is not supported, " | |
2072 "use the \"max_fails\" parameter of the \"server\" directive ", | |
2073 "inside the \"upstream\" block"); | |
2074 | |
2075 return NGX_CONF_ERROR; | |
2076 } | |
2077 | |
2078 | |
2079 static char * | |
2080 ngx_http_fastcgi_upstream_fail_timeout_unsupported(ngx_conf_t *cf, | |
2081 ngx_command_t *cmd, void *conf) | |
2082 { | |
2083 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
2084 "\"fastcgi_upstream_fail_timeout\" is not supported, " | |
2085 "use the \"fail_timeout\" parameter of the \"server\" directive ", | |
2086 "inside the \"upstream\" block"); | |
2087 | |
2088 return NGX_CONF_ERROR; | |
2089 } |