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