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