3
* Copyright (C) Igor Sysoev
7
#include <ngx_config.h>
13
ngx_http_upstream_conf_t upstream;
18
ngx_array_t *params_len;
20
ngx_array_t *params_source;
21
ngx_array_t *catch_stderr;
23
ngx_array_t *fastcgi_lengths;
24
ngx_array_t *fastcgi_values;
26
ngx_hash_t headers_hash;
27
ngx_uint_t header_params;
30
ngx_http_complex_value_t cache_key;
34
ngx_regex_t *split_regex;
37
} ngx_http_fastcgi_loc_conf_t;
41
ngx_http_fastcgi_st_version = 0,
42
ngx_http_fastcgi_st_type,
43
ngx_http_fastcgi_st_request_id_hi,
44
ngx_http_fastcgi_st_request_id_lo,
45
ngx_http_fastcgi_st_content_length_hi,
46
ngx_http_fastcgi_st_content_length_lo,
47
ngx_http_fastcgi_st_padding_length,
48
ngx_http_fastcgi_st_reserved,
49
ngx_http_fastcgi_st_data,
50
ngx_http_fastcgi_st_padding
51
} ngx_http_fastcgi_state_e;
57
} ngx_http_fastcgi_split_part_t;
61
ngx_http_fastcgi_state_e state;
68
unsigned fastcgi_stdout:1;
69
unsigned large_stderr:1;
71
ngx_array_t *split_parts;
73
ngx_str_t script_name;
75
} ngx_http_fastcgi_ctx_t;
78
#define NGX_HTTP_FASTCGI_RESPONDER 1
80
#define NGX_HTTP_FASTCGI_BEGIN_REQUEST 1
81
#define NGX_HTTP_FASTCGI_ABORT_REQUEST 2
82
#define NGX_HTTP_FASTCGI_END_REQUEST 3
83
#define NGX_HTTP_FASTCGI_PARAMS 4
84
#define NGX_HTTP_FASTCGI_STDIN 5
85
#define NGX_HTTP_FASTCGI_STDOUT 6
86
#define NGX_HTTP_FASTCGI_STDERR 7
87
#define NGX_HTTP_FASTCGI_DATA 8
95
u_char content_length_hi;
96
u_char content_length_lo;
97
u_char padding_length;
99
} ngx_http_fastcgi_header_t;
107
} ngx_http_fastcgi_begin_request_t;
113
u_char request_id_hi;
114
u_char request_id_lo;
115
} ngx_http_fastcgi_header_small_t;
119
ngx_http_fastcgi_header_t h0;
120
ngx_http_fastcgi_begin_request_t br;
121
ngx_http_fastcgi_header_small_t h1;
122
} ngx_http_fastcgi_request_start_t;
125
static ngx_int_t ngx_http_fastcgi_eval(ngx_http_request_t *r,
126
ngx_http_fastcgi_loc_conf_t *flcf);
128
static ngx_int_t ngx_http_fastcgi_create_key(ngx_http_request_t *r);
130
static ngx_int_t ngx_http_fastcgi_create_request(ngx_http_request_t *r);
131
static ngx_int_t ngx_http_fastcgi_reinit_request(ngx_http_request_t *r);
132
static ngx_int_t ngx_http_fastcgi_process_header(ngx_http_request_t *r);
133
static ngx_int_t ngx_http_fastcgi_input_filter(ngx_event_pipe_t *p,
135
static ngx_int_t ngx_http_fastcgi_process_record(ngx_http_request_t *r,
136
ngx_http_fastcgi_ctx_t *f);
137
static void ngx_http_fastcgi_abort_request(ngx_http_request_t *r);
138
static void ngx_http_fastcgi_finalize_request(ngx_http_request_t *r,
141
static ngx_int_t ngx_http_fastcgi_add_variables(ngx_conf_t *cf);
142
static void *ngx_http_fastcgi_create_loc_conf(ngx_conf_t *cf);
143
static char *ngx_http_fastcgi_merge_loc_conf(ngx_conf_t *cf,
144
void *parent, void *child);
145
static ngx_int_t ngx_http_fastcgi_script_name_variable(ngx_http_request_t *r,
146
ngx_http_variable_value_t *v, uintptr_t data);
147
static ngx_int_t ngx_http_fastcgi_path_info_variable(ngx_http_request_t *r,
148
ngx_http_variable_value_t *v, uintptr_t data);
149
static ngx_http_fastcgi_ctx_t *ngx_http_fastcgi_split(ngx_http_request_t *r,
150
ngx_http_fastcgi_loc_conf_t *flcf);
152
static char *ngx_http_fastcgi_pass(ngx_conf_t *cf, ngx_command_t *cmd,
154
static char *ngx_http_fastcgi_split_path_info(ngx_conf_t *cf,
155
ngx_command_t *cmd, void *conf);
156
static char *ngx_http_fastcgi_store(ngx_conf_t *cf, ngx_command_t *cmd,
159
static char *ngx_http_fastcgi_cache(ngx_conf_t *cf, ngx_command_t *cmd,
161
static char *ngx_http_fastcgi_cache_key(ngx_conf_t *cf, ngx_command_t *cmd,
165
static char *ngx_http_fastcgi_lowat_check(ngx_conf_t *cf, void *post,
169
static ngx_conf_post_t ngx_http_fastcgi_lowat_post =
170
{ ngx_http_fastcgi_lowat_check };
173
static ngx_conf_bitmask_t ngx_http_fastcgi_next_upstream_masks[] = {
174
{ ngx_string("error"), NGX_HTTP_UPSTREAM_FT_ERROR },
175
{ ngx_string("timeout"), NGX_HTTP_UPSTREAM_FT_TIMEOUT },
176
{ ngx_string("invalid_header"), NGX_HTTP_UPSTREAM_FT_INVALID_HEADER },
177
{ ngx_string("http_500"), NGX_HTTP_UPSTREAM_FT_HTTP_500 },
178
{ ngx_string("http_503"), NGX_HTTP_UPSTREAM_FT_HTTP_503 },
179
{ ngx_string("http_404"), NGX_HTTP_UPSTREAM_FT_HTTP_404 },
180
{ ngx_string("updating"), NGX_HTTP_UPSTREAM_FT_UPDATING },
181
{ ngx_string("off"), NGX_HTTP_UPSTREAM_FT_OFF },
182
{ ngx_null_string, 0 }
186
ngx_module_t ngx_http_fastcgi_module;
189
static ngx_command_t ngx_http_fastcgi_commands[] = {
191
{ ngx_string("fastcgi_pass"),
192
NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
193
ngx_http_fastcgi_pass,
194
NGX_HTTP_LOC_CONF_OFFSET,
198
{ ngx_string("fastcgi_index"),
199
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
200
ngx_conf_set_str_slot,
201
NGX_HTTP_LOC_CONF_OFFSET,
202
offsetof(ngx_http_fastcgi_loc_conf_t, index),
205
{ ngx_string("fastcgi_split_path_info"),
206
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
207
ngx_http_fastcgi_split_path_info,
208
NGX_HTTP_LOC_CONF_OFFSET,
212
{ ngx_string("fastcgi_store"),
213
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
214
ngx_http_fastcgi_store,
215
NGX_HTTP_LOC_CONF_OFFSET,
219
{ ngx_string("fastcgi_store_access"),
220
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE123,
221
ngx_conf_set_access_slot,
222
NGX_HTTP_LOC_CONF_OFFSET,
223
offsetof(ngx_http_fastcgi_loc_conf_t, upstream.store_access),
226
{ ngx_string("fastcgi_ignore_client_abort"),
227
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
228
ngx_conf_set_flag_slot,
229
NGX_HTTP_LOC_CONF_OFFSET,
230
offsetof(ngx_http_fastcgi_loc_conf_t, upstream.ignore_client_abort),
233
{ ngx_string("fastcgi_bind"),
234
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
235
ngx_http_upstream_bind_set_slot,
236
NGX_HTTP_LOC_CONF_OFFSET,
237
offsetof(ngx_http_fastcgi_loc_conf_t, upstream.local),
240
{ ngx_string("fastcgi_connect_timeout"),
241
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
242
ngx_conf_set_msec_slot,
243
NGX_HTTP_LOC_CONF_OFFSET,
244
offsetof(ngx_http_fastcgi_loc_conf_t, upstream.connect_timeout),
247
{ ngx_string("fastcgi_send_timeout"),
248
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
249
ngx_conf_set_msec_slot,
250
NGX_HTTP_LOC_CONF_OFFSET,
251
offsetof(ngx_http_fastcgi_loc_conf_t, upstream.send_timeout),
254
{ ngx_string("fastcgi_send_lowat"),
255
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
256
ngx_conf_set_size_slot,
257
NGX_HTTP_LOC_CONF_OFFSET,
258
offsetof(ngx_http_fastcgi_loc_conf_t, upstream.send_lowat),
259
&ngx_http_fastcgi_lowat_post },
261
{ ngx_string("fastcgi_buffer_size"),
262
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
263
ngx_conf_set_size_slot,
264
NGX_HTTP_LOC_CONF_OFFSET,
265
offsetof(ngx_http_fastcgi_loc_conf_t, upstream.buffer_size),
268
{ ngx_string("fastcgi_pass_request_headers"),
269
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
270
ngx_conf_set_flag_slot,
271
NGX_HTTP_LOC_CONF_OFFSET,
272
offsetof(ngx_http_fastcgi_loc_conf_t, upstream.pass_request_headers),
275
{ ngx_string("fastcgi_pass_request_body"),
276
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
277
ngx_conf_set_flag_slot,
278
NGX_HTTP_LOC_CONF_OFFSET,
279
offsetof(ngx_http_fastcgi_loc_conf_t, upstream.pass_request_body),
282
{ ngx_string("fastcgi_intercept_errors"),
283
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
284
ngx_conf_set_flag_slot,
285
NGX_HTTP_LOC_CONF_OFFSET,
286
offsetof(ngx_http_fastcgi_loc_conf_t, upstream.intercept_errors),
289
{ ngx_string("fastcgi_read_timeout"),
290
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
291
ngx_conf_set_msec_slot,
292
NGX_HTTP_LOC_CONF_OFFSET,
293
offsetof(ngx_http_fastcgi_loc_conf_t, upstream.read_timeout),
296
{ ngx_string("fastcgi_buffers"),
297
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
298
ngx_conf_set_bufs_slot,
299
NGX_HTTP_LOC_CONF_OFFSET,
300
offsetof(ngx_http_fastcgi_loc_conf_t, upstream.bufs),
303
{ ngx_string("fastcgi_busy_buffers_size"),
304
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
305
ngx_conf_set_size_slot,
306
NGX_HTTP_LOC_CONF_OFFSET,
307
offsetof(ngx_http_fastcgi_loc_conf_t, upstream.busy_buffers_size_conf),
312
{ ngx_string("fastcgi_cache"),
313
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
314
ngx_http_fastcgi_cache,
315
NGX_HTTP_LOC_CONF_OFFSET,
319
{ ngx_string("fastcgi_cache_key"),
320
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
321
ngx_http_fastcgi_cache_key,
322
NGX_HTTP_LOC_CONF_OFFSET,
326
{ ngx_string("fastcgi_cache_path"),
327
NGX_HTTP_MAIN_CONF|NGX_CONF_2MORE,
328
ngx_http_file_cache_set_slot,
331
&ngx_http_fastcgi_module },
333
{ ngx_string("fastcgi_cache_bypass"),
334
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
335
ngx_http_set_predicate_slot,
336
NGX_HTTP_LOC_CONF_OFFSET,
337
offsetof(ngx_http_fastcgi_loc_conf_t, upstream.cache_bypass),
340
{ ngx_string("fastcgi_no_cache"),
341
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
342
ngx_http_set_predicate_slot,
343
NGX_HTTP_LOC_CONF_OFFSET,
344
offsetof(ngx_http_fastcgi_loc_conf_t, upstream.no_cache),
347
{ ngx_string("fastcgi_cache_valid"),
348
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
349
ngx_http_file_cache_valid_set_slot,
350
NGX_HTTP_LOC_CONF_OFFSET,
351
offsetof(ngx_http_fastcgi_loc_conf_t, upstream.cache_valid),
354
{ ngx_string("fastcgi_cache_min_uses"),
355
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
356
ngx_conf_set_num_slot,
357
NGX_HTTP_LOC_CONF_OFFSET,
358
offsetof(ngx_http_fastcgi_loc_conf_t, upstream.cache_min_uses),
361
{ ngx_string("fastcgi_cache_use_stale"),
362
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
363
ngx_conf_set_bitmask_slot,
364
NGX_HTTP_LOC_CONF_OFFSET,
365
offsetof(ngx_http_fastcgi_loc_conf_t, upstream.cache_use_stale),
366
&ngx_http_fastcgi_next_upstream_masks },
368
{ ngx_string("fastcgi_cache_methods"),
369
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
370
ngx_conf_set_bitmask_slot,
371
NGX_HTTP_LOC_CONF_OFFSET,
372
offsetof(ngx_http_fastcgi_loc_conf_t, upstream.cache_methods),
373
&ngx_http_upstream_cache_method_mask },
377
{ ngx_string("fastcgi_temp_path"),
378
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1234,
379
ngx_conf_set_path_slot,
380
NGX_HTTP_LOC_CONF_OFFSET,
381
offsetof(ngx_http_fastcgi_loc_conf_t, upstream.temp_path),
384
{ ngx_string("fastcgi_max_temp_file_size"),
385
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
386
ngx_conf_set_size_slot,
387
NGX_HTTP_LOC_CONF_OFFSET,
388
offsetof(ngx_http_fastcgi_loc_conf_t, upstream.max_temp_file_size_conf),
391
{ ngx_string("fastcgi_temp_file_write_size"),
392
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
393
ngx_conf_set_size_slot,
394
NGX_HTTP_LOC_CONF_OFFSET,
395
offsetof(ngx_http_fastcgi_loc_conf_t, upstream.temp_file_write_size_conf),
398
{ ngx_string("fastcgi_next_upstream"),
399
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
400
ngx_conf_set_bitmask_slot,
401
NGX_HTTP_LOC_CONF_OFFSET,
402
offsetof(ngx_http_fastcgi_loc_conf_t, upstream.next_upstream),
403
&ngx_http_fastcgi_next_upstream_masks },
405
{ ngx_string("fastcgi_param"),
406
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
407
ngx_conf_set_keyval_slot,
408
NGX_HTTP_LOC_CONF_OFFSET,
409
offsetof(ngx_http_fastcgi_loc_conf_t, params_source),
412
{ ngx_string("fastcgi_pass_header"),
413
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
414
ngx_conf_set_str_array_slot,
415
NGX_HTTP_LOC_CONF_OFFSET,
416
offsetof(ngx_http_fastcgi_loc_conf_t, upstream.pass_headers),
419
{ ngx_string("fastcgi_hide_header"),
420
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
421
ngx_conf_set_str_array_slot,
422
NGX_HTTP_LOC_CONF_OFFSET,
423
offsetof(ngx_http_fastcgi_loc_conf_t, upstream.hide_headers),
426
{ ngx_string("fastcgi_ignore_headers"),
427
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
428
ngx_conf_set_bitmask_slot,
429
NGX_HTTP_LOC_CONF_OFFSET,
430
offsetof(ngx_http_fastcgi_loc_conf_t, upstream.ignore_headers),
431
&ngx_http_upstream_ignore_headers_masks },
433
{ ngx_string("fastcgi_catch_stderr"),
434
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
435
ngx_conf_set_str_array_slot,
436
NGX_HTTP_LOC_CONF_OFFSET,
437
offsetof(ngx_http_fastcgi_loc_conf_t, catch_stderr),
444
static ngx_http_module_t ngx_http_fastcgi_module_ctx = {
445
ngx_http_fastcgi_add_variables, /* preconfiguration */
446
NULL, /* postconfiguration */
448
NULL, /* create main configuration */
449
NULL, /* init main configuration */
451
NULL, /* create server configuration */
452
NULL, /* merge server configuration */
454
ngx_http_fastcgi_create_loc_conf, /* create location configuration */
455
ngx_http_fastcgi_merge_loc_conf /* merge location configuration */
459
ngx_module_t ngx_http_fastcgi_module = {
461
&ngx_http_fastcgi_module_ctx, /* module context */
462
ngx_http_fastcgi_commands, /* module directives */
463
NGX_HTTP_MODULE, /* module type */
464
NULL, /* init master */
465
NULL, /* init module */
466
NULL, /* init process */
467
NULL, /* init thread */
468
NULL, /* exit thread */
469
NULL, /* exit process */
470
NULL, /* exit master */
471
NGX_MODULE_V1_PADDING
475
static ngx_http_fastcgi_request_start_t ngx_http_fastcgi_request_start = {
477
NGX_HTTP_FASTCGI_BEGIN_REQUEST, /* type */
478
0, /* request_id_hi */
479
1, /* request_id_lo */
480
0, /* content_length_hi */
481
sizeof(ngx_http_fastcgi_begin_request_t), /* content_length_lo */
482
0, /* padding_length */
486
NGX_HTTP_FASTCGI_RESPONDER, /* role_lo */
487
0, /* NGX_HTTP_FASTCGI_KEEP_CONN */ /* flags */
488
{ 0, 0, 0, 0, 0 } }, /* reserved[5] */
491
NGX_HTTP_FASTCGI_PARAMS, /* type */
492
0, /* request_id_hi */
493
1 }, /* request_id_lo */
498
static ngx_http_variable_t ngx_http_fastcgi_vars[] = {
500
{ ngx_string("fastcgi_script_name"), NULL,
501
ngx_http_fastcgi_script_name_variable, 0,
502
NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
504
{ ngx_string("fastcgi_path_info"), NULL,
505
ngx_http_fastcgi_path_info_variable, 0,
506
NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
508
{ ngx_null_string, NULL, NULL, 0, 0, 0 }
512
static ngx_str_t ngx_http_fastcgi_hide_headers[] = {
513
ngx_string("Status"),
514
ngx_string("X-Accel-Expires"),
515
ngx_string("X-Accel-Redirect"),
516
ngx_string("X-Accel-Limit-Rate"),
517
ngx_string("X-Accel-Buffering"),
518
ngx_string("X-Accel-Charset"),
525
static ngx_keyval_t ngx_http_fastcgi_cache_headers[] = {
526
{ ngx_string("HTTP_IF_MODIFIED_SINCE"), ngx_string("") },
527
{ ngx_string("HTTP_IF_UNMODIFIED_SINCE"), ngx_string("") },
528
{ ngx_string("HTTP_IF_NONE_MATCH"), ngx_string("") },
529
{ ngx_string("HTTP_IF_MATCH"), ngx_string("") },
530
{ ngx_string("HTTP_RANGE"), ngx_string("") },
531
{ ngx_string("HTTP_IF_RANGE"), ngx_string("") },
532
{ ngx_null_string, ngx_null_string }
538
static ngx_path_init_t ngx_http_fastcgi_temp_path = {
539
ngx_string(NGX_HTTP_FASTCGI_TEMP_PATH), { 1, 2, 0 }
544
ngx_http_fastcgi_handler(ngx_http_request_t *r)
547
ngx_http_upstream_t *u;
548
ngx_http_fastcgi_ctx_t *f;
549
ngx_http_fastcgi_loc_conf_t *flcf;
551
if (r->subrequest_in_memory) {
552
ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
553
"ngx_http_fastcgi_module does not support "
554
"subrequest in memory");
555
return NGX_HTTP_INTERNAL_SERVER_ERROR;
558
if (ngx_http_upstream_create(r) != NGX_OK) {
559
return NGX_HTTP_INTERNAL_SERVER_ERROR;
562
f = ngx_pcalloc(r->pool, sizeof(ngx_http_fastcgi_ctx_t));
564
return NGX_HTTP_INTERNAL_SERVER_ERROR;
567
ngx_http_set_ctx(r, f, ngx_http_fastcgi_module);
569
flcf = ngx_http_get_module_loc_conf(r, ngx_http_fastcgi_module);
571
if (flcf->fastcgi_lengths) {
572
if (ngx_http_fastcgi_eval(r, flcf) != NGX_OK) {
573
return NGX_HTTP_INTERNAL_SERVER_ERROR;
579
ngx_str_set(&u->schema, "fastcgi://");
580
u->output.tag = (ngx_buf_tag_t) &ngx_http_fastcgi_module;
582
u->conf = &flcf->upstream;
585
u->create_key = ngx_http_fastcgi_create_key;
587
u->create_request = ngx_http_fastcgi_create_request;
588
u->reinit_request = ngx_http_fastcgi_reinit_request;
589
u->process_header = ngx_http_fastcgi_process_header;
590
u->abort_request = ngx_http_fastcgi_abort_request;
591
u->finalize_request = ngx_http_fastcgi_finalize_request;
595
u->pipe = ngx_pcalloc(r->pool, sizeof(ngx_event_pipe_t));
596
if (u->pipe == NULL) {
597
return NGX_HTTP_INTERNAL_SERVER_ERROR;
600
u->pipe->input_filter = ngx_http_fastcgi_input_filter;
601
u->pipe->input_ctx = r;
603
rc = ngx_http_read_client_request_body(r, ngx_http_upstream_init);
605
if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
614
ngx_http_fastcgi_eval(ngx_http_request_t *r, ngx_http_fastcgi_loc_conf_t *flcf)
617
ngx_http_upstream_t *u;
619
ngx_memzero(&url, sizeof(ngx_url_t));
621
if (ngx_http_script_run(r, &url.url, flcf->fastcgi_lengths->elts, 0,
622
flcf->fastcgi_values->elts)
630
if (ngx_parse_url(r->pool, &url) != NGX_OK) {
632
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
633
"%s in upstream \"%V\"", url.err, &url.url);
640
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
641
"no port in upstream \"%V\"", &url.url);
647
u->resolved = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_resolved_t));
648
if (u->resolved == NULL) {
652
if (url.addrs && url.addrs[0].sockaddr) {
653
u->resolved->sockaddr = url.addrs[0].sockaddr;
654
u->resolved->socklen = url.addrs[0].socklen;
655
u->resolved->naddrs = 1;
656
u->resolved->host = url.addrs[0].name;
659
u->resolved->host = url.host;
660
u->resolved->port = url.port;
670
ngx_http_fastcgi_create_key(ngx_http_request_t *r)
673
ngx_http_fastcgi_loc_conf_t *flcf;
675
key = ngx_array_push(&r->cache->keys);
680
flcf = ngx_http_get_module_loc_conf(r, ngx_http_fastcgi_module);
682
if (ngx_http_complex_value(r, &flcf->cache_key, key) != NGX_OK) {
693
ngx_http_fastcgi_create_request(ngx_http_request_t *r)
696
u_char ch, *pos, *lowcase_key;
697
size_t size, len, key_len, val_len, padding,
699
ngx_uint_t i, n, next, hash, header_params;
701
ngx_chain_t *cl, *body;
702
ngx_list_part_t *part;
703
ngx_table_elt_t *header, **ignored;
704
ngx_http_script_code_pt code;
705
ngx_http_script_engine_t e, le;
706
ngx_http_fastcgi_header_t *h;
707
ngx_http_fastcgi_loc_conf_t *flcf;
708
ngx_http_script_len_code_pt lcode;
714
flcf = ngx_http_get_module_loc_conf(r, ngx_http_fastcgi_module);
716
if (flcf->params_len) {
717
ngx_memzero(&le, sizeof(ngx_http_script_engine_t));
719
ngx_http_script_flush_no_cacheable_variables(r, flcf->flushes);
722
le.ip = flcf->params_len->elts;
725
while (*(uintptr_t *) le.ip) {
727
lcode = *(ngx_http_script_len_code_pt *) le.ip;
728
key_len = lcode(&le);
730
for (val_len = 0; *(uintptr_t *) le.ip; val_len += lcode(&le)) {
731
lcode = *(ngx_http_script_len_code_pt *) le.ip;
733
le.ip += sizeof(uintptr_t);
735
len += 1 + key_len + ((val_len > 127) ? 4 : 1) + val_len;
739
if (flcf->upstream.pass_request_headers) {
744
if (flcf->header_params) {
745
ignored = ngx_palloc(r->pool, flcf->header_params * sizeof(void *));
746
if (ignored == NULL) {
751
part = &r->headers_in.headers.part;
754
for (i = 0; /* void */; i++) {
756
if (i >= part->nelts) {
757
if (part->next == NULL) {
766
if (flcf->header_params) {
767
if (allocated < header[i].key.len) {
768
allocated = header[i].key.len + 16;
769
lowcase_key = ngx_pnalloc(r->pool, allocated);
770
if (lowcase_key == NULL) {
777
for (n = 0; n < header[i].key.len; n++) {
778
ch = header[i].key.data[n];
780
if (ch >= 'A' && ch <= 'Z') {
783
} else if (ch == '-') {
787
hash = ngx_hash(hash, ch);
791
if (ngx_hash_find(&flcf->headers_hash, hash, lowcase_key, n)) {
792
ignored[header_params++] = &header[i];
796
n += sizeof("HTTP_") - 1;
799
n = sizeof("HTTP_") - 1 + header[i].key.len;
802
len += ((n > 127) ? 4 : 1) + ((header[i].value.len > 127) ? 4 : 1)
803
+ n + header[i].value.len;
809
ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
810
"fastcgi request record is too big: %uz", len);
815
padding = 8 - len % 8;
816
padding = (padding == 8) ? 0 : padding;
819
size = sizeof(ngx_http_fastcgi_header_t)
820
+ sizeof(ngx_http_fastcgi_begin_request_t)
822
+ sizeof(ngx_http_fastcgi_header_t) /* NGX_HTTP_FASTCGI_PARAMS */
824
+ sizeof(ngx_http_fastcgi_header_t) /* NGX_HTTP_FASTCGI_PARAMS */
826
+ sizeof(ngx_http_fastcgi_header_t); /* NGX_HTTP_FASTCGI_STDIN */
829
b = ngx_create_temp_buf(r->pool, size);
834
cl = ngx_alloc_chain_link(r->pool);
841
ngx_memcpy(b->pos, &ngx_http_fastcgi_request_start,
842
sizeof(ngx_http_fastcgi_request_start_t));
844
h = (ngx_http_fastcgi_header_t *)
845
(b->pos + sizeof(ngx_http_fastcgi_header_t)
846
+ sizeof(ngx_http_fastcgi_begin_request_t));
848
h->content_length_hi = (u_char) ((len >> 8) & 0xff);
849
h->content_length_lo = (u_char) (len & 0xff);
850
h->padding_length = (u_char) padding;
853
b->last = b->pos + sizeof(ngx_http_fastcgi_header_t)
854
+ sizeof(ngx_http_fastcgi_begin_request_t)
855
+ sizeof(ngx_http_fastcgi_header_t);
858
if (flcf->params_len) {
859
ngx_memzero(&e, sizeof(ngx_http_script_engine_t));
861
e.ip = flcf->params->elts;
866
le.ip = flcf->params_len->elts;
868
while (*(uintptr_t *) le.ip) {
870
lcode = *(ngx_http_script_len_code_pt *) le.ip;
871
key_len = (u_char) lcode(&le);
873
for (val_len = 0; *(uintptr_t *) le.ip; val_len += lcode(&le)) {
874
lcode = *(ngx_http_script_len_code_pt *) le.ip;
876
le.ip += sizeof(uintptr_t);
878
*e.pos++ = (u_char) key_len;
881
*e.pos++ = (u_char) (((val_len >> 24) & 0x7f) | 0x80);
882
*e.pos++ = (u_char) ((val_len >> 16) & 0xff);
883
*e.pos++ = (u_char) ((val_len >> 8) & 0xff);
884
*e.pos++ = (u_char) (val_len & 0xff);
887
*e.pos++ = (u_char) val_len;
890
while (*(uintptr_t *) e.ip) {
891
code = *(ngx_http_script_code_pt *) e.ip;
892
code((ngx_http_script_engine_t *) &e);
894
e.ip += sizeof(uintptr_t);
896
ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
897
"fastcgi param: \"%*s: %*s\"",
898
key_len, e.pos - (key_len + val_len),
899
val_len, e.pos - val_len);
906
if (flcf->upstream.pass_request_headers) {
908
part = &r->headers_in.headers.part;
911
for (i = 0; /* void */; i++) {
913
if (i >= part->nelts) {
914
if (part->next == NULL) {
923
for (n = 0; n < header_params; n++) {
924
if (&header[i] == ignored[n]) {
929
key_len = sizeof("HTTP_") - 1 + header[i].key.len;
931
*b->last++ = (u_char) (((key_len >> 24) & 0x7f) | 0x80);
932
*b->last++ = (u_char) ((key_len >> 16) & 0xff);
933
*b->last++ = (u_char) ((key_len >> 8) & 0xff);
934
*b->last++ = (u_char) (key_len & 0xff);
937
*b->last++ = (u_char) key_len;
940
val_len = header[i].value.len;
942
*b->last++ = (u_char) (((val_len >> 24) & 0x7f) | 0x80);
943
*b->last++ = (u_char) ((val_len >> 16) & 0xff);
944
*b->last++ = (u_char) ((val_len >> 8) & 0xff);
945
*b->last++ = (u_char) (val_len & 0xff);
948
*b->last++ = (u_char) val_len;
951
b->last = ngx_cpymem(b->last, "HTTP_", sizeof("HTTP_") - 1);
953
for (n = 0; n < header[i].key.len; n++) {
954
ch = header[i].key.data[n];
956
if (ch >= 'a' && ch <= 'z') {
959
} else if (ch == '-') {
966
b->last = ngx_copy(b->last, header[i].value.data, val_len);
968
ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
969
"fastcgi param: \"%*s: %*s\"",
970
key_len, b->last - (key_len + val_len),
971
val_len, b->last - val_len);
980
ngx_memzero(b->last, padding);
985
h = (ngx_http_fastcgi_header_t *) b->last;
986
b->last += sizeof(ngx_http_fastcgi_header_t);
989
h->type = NGX_HTTP_FASTCGI_PARAMS;
990
h->request_id_hi = 0;
991
h->request_id_lo = 1;
992
h->content_length_hi = 0;
993
h->content_length_lo = 0;
994
h->padding_length = 0;
997
h = (ngx_http_fastcgi_header_t *) b->last;
998
b->last += sizeof(ngx_http_fastcgi_header_t);
1000
if (flcf->upstream.pass_request_body) {
1001
body = r->upstream->request_bufs;
1002
r->upstream->request_bufs = cl;
1004
#if (NGX_SUPPRESS_WARN)
1011
if (body->buf->in_file) {
1012
file_pos = body->buf->file_pos;
1015
pos = body->buf->pos;
1021
b = ngx_alloc_buf(r->pool);
1026
ngx_memcpy(b, body->buf, sizeof(ngx_buf_t));
1028
if (body->buf->in_file) {
1029
b->file_pos = file_pos;
1030
file_pos += 32 * 1024;
1032
if (file_pos >= body->buf->file_last) {
1033
file_pos = body->buf->file_last;
1037
b->file_last = file_pos;
1038
len = (ngx_uint_t) (file_pos - b->file_pos);
1044
if (pos >= body->buf->last) {
1045
pos = body->buf->last;
1050
len = (ngx_uint_t) (pos - b->pos);
1053
padding = 8 - len % 8;
1054
padding = (padding == 8) ? 0 : padding;
1057
h->type = NGX_HTTP_FASTCGI_STDIN;
1058
h->request_id_hi = 0;
1059
h->request_id_lo = 1;
1060
h->content_length_hi = (u_char) ((len >> 8) & 0xff);
1061
h->content_length_lo = (u_char) (len & 0xff);
1062
h->padding_length = (u_char) padding;
1065
cl->next = ngx_alloc_chain_link(r->pool);
1066
if (cl->next == NULL) {
1073
b = ngx_create_temp_buf(r->pool,
1074
sizeof(ngx_http_fastcgi_header_t)
1081
ngx_memzero(b->last, padding);
1085
h = (ngx_http_fastcgi_header_t *) b->last;
1086
b->last += sizeof(ngx_http_fastcgi_header_t);
1088
cl->next = ngx_alloc_chain_link(r->pool);
1089
if (cl->next == NULL) {
1102
r->upstream->request_bufs = cl;
1106
h->type = NGX_HTTP_FASTCGI_STDIN;
1107
h->request_id_hi = 0;
1108
h->request_id_lo = 1;
1109
h->content_length_hi = 0;
1110
h->content_length_lo = 0;
1111
h->padding_length = 0;
1121
ngx_http_fastcgi_reinit_request(ngx_http_request_t *r)
1123
ngx_http_fastcgi_ctx_t *f;
1125
f = ngx_http_get_module_ctx(r, ngx_http_fastcgi_module);
1131
f->state = ngx_http_fastcgi_st_version;
1132
f->fastcgi_stdout = 0;
1133
f->large_stderr = 0;
1140
ngx_http_fastcgi_process_header(ngx_http_request_t *r)
1142
u_char *p, *msg, *start, *last,
1143
*part_start, *part_end;
1145
ngx_str_t *status_line, *pattern;
1146
ngx_int_t rc, status;
1150
ngx_http_upstream_t *u;
1151
ngx_http_fastcgi_ctx_t *f;
1152
ngx_http_upstream_header_t *hh;
1153
ngx_http_fastcgi_loc_conf_t *flcf;
1154
ngx_http_fastcgi_split_part_t *part;
1155
ngx_http_upstream_main_conf_t *umcf;
1157
f = ngx_http_get_module_ctx(r, ngx_http_fastcgi_module);
1159
umcf = ngx_http_get_module_main_conf(r, ngx_http_upstream_module);
1165
if (f->state < ngx_http_fastcgi_st_data) {
1167
f->pos = u->buffer.pos;
1168
f->last = u->buffer.last;
1170
rc = ngx_http_fastcgi_process_record(r, f);
1172
u->buffer.pos = f->pos;
1173
u->buffer.last = f->last;
1175
if (rc == NGX_AGAIN) {
1179
if (rc == NGX_ERROR) {
1180
return NGX_HTTP_UPSTREAM_INVALID_HEADER;
1183
if (f->type != NGX_HTTP_FASTCGI_STDOUT
1184
&& f->type != NGX_HTTP_FASTCGI_STDERR)
1186
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1187
"upstream sent unexpected FastCGI record: %d",
1190
return NGX_HTTP_UPSTREAM_INVALID_HEADER;
1193
if (f->type == NGX_HTTP_FASTCGI_STDOUT && f->length == 0) {
1194
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1195
"upstream closed prematurely FastCGI stdout");
1197
return NGX_HTTP_UPSTREAM_INVALID_HEADER;
1201
if (f->state == ngx_http_fastcgi_st_padding) {
1203
if (u->buffer.pos + f->padding < u->buffer.last) {
1204
f->state = ngx_http_fastcgi_st_version;
1205
u->buffer.pos += f->padding;
1210
if (u->buffer.pos + f->padding == u->buffer.last) {
1211
f->state = ngx_http_fastcgi_st_version;
1212
u->buffer.pos = u->buffer.last;
1217
f->padding -= u->buffer.last - u->buffer.pos;
1218
u->buffer.pos = u->buffer.last;
1224
/* f->state == ngx_http_fastcgi_st_data */
1226
if (f->type == NGX_HTTP_FASTCGI_STDERR) {
1229
msg = u->buffer.pos;
1231
if (u->buffer.pos + f->length <= u->buffer.last) {
1232
u->buffer.pos += f->length;
1234
f->state = ngx_http_fastcgi_st_padding;
1237
f->length -= u->buffer.last - u->buffer.pos;
1238
u->buffer.pos = u->buffer.last;
1241
for (p = u->buffer.pos - 1; msg < p; p--) {
1242
if (*p != LF && *p != CR && *p != '.' && *p != ' ') {
1249
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1250
"FastCGI sent in stderr: \"%*s\"", p - msg, msg);
1252
flcf = ngx_http_get_module_loc_conf(r, ngx_http_fastcgi_module);
1254
if (flcf->catch_stderr) {
1255
pattern = flcf->catch_stderr->elts;
1257
for (i = 0; i < flcf->catch_stderr->nelts; i++) {
1258
if (ngx_strnstr(msg, (char *) pattern[i].data,
1262
return NGX_HTTP_UPSTREAM_INVALID_HEADER;
1267
if (u->buffer.pos == u->buffer.last) {
1269
if (!f->fastcgi_stdout) {
1272
* the special handling the large number
1273
* of the PHP warnings to not allocate memory
1276
#if (NGX_HTTP_CACHE)
1278
u->buffer.pos = u->buffer.start
1279
+ r->cache->header_start;
1281
u->buffer.pos = u->buffer.start;
1285
u->buffer.last = u->buffer.pos;
1286
f->large_stderr = 1;
1293
f->state = ngx_http_fastcgi_st_version;
1300
/* f->type == NGX_HTTP_FASTCGI_STDOUT */
1302
#if (NGX_HTTP_CACHE)
1304
if (f->large_stderr && r->cache) {
1307
ngx_http_fastcgi_header_t *fh;
1309
start = u->buffer.start + r->cache->header_start;
1311
len = u->buffer.pos - start - 2 * sizeof(ngx_http_fastcgi_header_t);
1314
* A tail of large stderr output before HTTP header is placed
1315
* in a cache file without a FastCGI record header.
1316
* To workaround it we put a dummy FastCGI record header at the
1317
* start of the stderr output or update r->cache_header_start,
1318
* if there is no enough place for the record header.
1322
fh = (ngx_http_fastcgi_header_t *) start;
1324
fh->type = NGX_HTTP_FASTCGI_STDERR;
1325
fh->request_id_hi = 0;
1326
fh->request_id_lo = 1;
1327
fh->content_length_hi = (u_char) ((len >> 8) & 0xff);
1328
fh->content_length_lo = (u_char) (len & 0xff);
1329
fh->padding_length = 0;
1333
r->cache->header_start += u->buffer.pos - start
1334
- sizeof(ngx_http_fastcgi_header_t);
1337
f->large_stderr = 0;
1342
f->fastcgi_stdout = 1;
1344
start = u->buffer.pos;
1346
if (u->buffer.pos + f->length < u->buffer.last) {
1349
* set u->buffer.last to the end of the FastCGI record data
1350
* for ngx_http_parse_header_line()
1353
last = u->buffer.last;
1354
u->buffer.last = u->buffer.pos + f->length;
1362
part_start = u->buffer.pos;
1363
part_end = u->buffer.last;
1365
rc = ngx_http_parse_header_line(r, &u->buffer, 1);
1367
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1368
"http fastcgi parser: %d", rc);
1370
if (rc == NGX_AGAIN) {
1376
/* a header line has been parsed successfully */
1378
h = ngx_list_push(&u->headers_in.headers);
1383
if (f->split_parts && f->split_parts->nelts) {
1385
part = f->split_parts->elts;
1386
size = u->buffer.pos - part_start;
1388
for (i = 0; i < f->split_parts->nelts; i++) {
1389
size += part[i].end - part[i].start;
1392
p = ngx_pnalloc(r->pool, size);
1399
for (i = 0; i < f->split_parts->nelts; i++) {
1400
p = ngx_cpymem(p, part[i].start,
1401
part[i].end - part[i].start);
1404
p = ngx_cpymem(p, part_start, u->buffer.pos - part_start);
1408
f->split_parts->nelts = 0;
1410
rc = ngx_http_parse_header_line(r, &buf, 1);
1412
h->key.len = r->header_name_end - r->header_name_start;
1413
h->key.data = r->header_name_start;
1414
h->key.data[h->key.len] = '\0';
1416
h->value.len = r->header_end - r->header_start;
1417
h->value.data = r->header_start;
1418
h->value.data[h->value.len] = '\0';
1420
h->lowcase_key = ngx_pnalloc(r->pool, h->key.len);
1421
if (h->lowcase_key == NULL) {
1427
h->key.len = r->header_name_end - r->header_name_start;
1428
h->value.len = r->header_end - r->header_start;
1430
h->key.data = ngx_pnalloc(r->pool,
1431
h->key.len + 1 + h->value.len + 1
1433
if (h->key.data == NULL) {
1437
h->value.data = h->key.data + h->key.len + 1;
1438
h->lowcase_key = h->key.data + h->key.len + 1
1441
ngx_cpystrn(h->key.data, r->header_name_start,
1443
ngx_cpystrn(h->value.data, r->header_start,
1447
h->hash = r->header_hash;
1449
if (h->key.len == r->lowcase_index) {
1450
ngx_memcpy(h->lowcase_key, r->lowcase_header, h->key.len);
1453
ngx_strlow(h->lowcase_key, h->key.data, h->key.len);
1456
hh = ngx_hash_find(&umcf->headers_in_hash, h->hash,
1457
h->lowcase_key, h->key.len);
1459
if (hh && hh->handler(r, h, hh->offset) != NGX_OK) {
1463
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1464
"http fastcgi header: \"%V: %V\"",
1465
&h->key, &h->value);
1467
if (u->buffer.pos < u->buffer.last) {
1471
/* the end of the FastCGI record */
1476
if (rc == NGX_HTTP_PARSE_HEADER_DONE) {
1478
/* a whole header has been parsed successfully */
1480
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1481
"http fastcgi header done");
1483
if (u->headers_in.status) {
1484
status_line = &u->headers_in.status->value;
1486
status = ngx_atoi(status_line->data, 3);
1488
if (status == NGX_ERROR) {
1489
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1490
"upstream sent invalid status \"%V\"",
1492
return NGX_HTTP_UPSTREAM_INVALID_HEADER;
1495
u->headers_in.status_n = status;
1496
u->headers_in.status_line = *status_line;
1498
} else if (u->headers_in.location) {
1499
u->headers_in.status_n = 302;
1500
ngx_str_set(&u->headers_in.status_line,
1501
"302 Moved Temporarily");
1504
u->headers_in.status_n = 200;
1505
ngx_str_set(&u->headers_in.status_line, "200 OK");
1509
u->state->status = u->headers_in.status_n;
1515
/* there was error while a header line parsing */
1517
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1518
"upstream sent invalid header");
1520
return NGX_HTTP_UPSTREAM_INVALID_HEADER;
1524
u->buffer.last = last;
1527
f->length -= u->buffer.pos - start;
1529
if (f->length == 0) {
1531
f->state = ngx_http_fastcgi_st_padding;
1533
f->state = ngx_http_fastcgi_st_version;
1537
if (rc == NGX_HTTP_PARSE_HEADER_DONE) {
1545
/* rc == NGX_AGAIN */
1547
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1548
"upstream split a header line in FastCGI records");
1550
if (f->split_parts == NULL) {
1551
f->split_parts = ngx_array_create(r->pool, 1,
1552
sizeof(ngx_http_fastcgi_split_part_t));
1553
if (f->split_parts == NULL) {
1558
part = ngx_array_push(f->split_parts);
1560
part->start = part_start;
1561
part->end = part_end;
1563
if (u->buffer.pos < u->buffer.last) {
1573
ngx_http_fastcgi_input_filter(ngx_event_pipe_t *p, ngx_buf_t *buf)
1577
ngx_buf_t *b, **prev;
1579
ngx_http_request_t *r;
1580
ngx_http_fastcgi_ctx_t *f;
1582
if (buf->pos == buf->last) {
1587
f = ngx_http_get_module_ctx(r, ngx_http_fastcgi_module);
1590
prev = &buf->shadow;
1593
f->last = buf->last;
1596
if (f->state < ngx_http_fastcgi_st_data) {
1598
rc = ngx_http_fastcgi_process_record(r, f);
1600
if (rc == NGX_AGAIN) {
1604
if (rc == NGX_ERROR) {
1608
if (f->type == NGX_HTTP_FASTCGI_STDOUT && f->length == 0) {
1609
f->state = ngx_http_fastcgi_st_version;
1610
p->upstream_done = 1;
1612
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, p->log, 0,
1613
"http fastcgi closed stdout");
1618
if (f->type == NGX_HTTP_FASTCGI_END_REQUEST) {
1619
f->state = ngx_http_fastcgi_st_version;
1620
p->upstream_done = 1;
1622
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, p->log, 0,
1623
"http fastcgi sent end request");
1630
if (f->state == ngx_http_fastcgi_st_padding) {
1632
if (f->pos + f->padding < f->last) {
1633
f->state = ngx_http_fastcgi_st_version;
1634
f->pos += f->padding;
1639
if (f->pos + f->padding == f->last) {
1640
f->state = ngx_http_fastcgi_st_version;
1645
f->padding -= f->last - f->pos;
1651
/* f->state == ngx_http_fastcgi_st_data */
1653
if (f->type == NGX_HTTP_FASTCGI_STDERR) {
1657
if (f->pos == f->last) {
1663
if (f->pos + f->length <= f->last) {
1664
f->pos += f->length;
1666
f->state = ngx_http_fastcgi_st_padding;
1669
f->length -= f->last - f->pos;
1673
for (m = f->pos - 1; msg < m; m--) {
1674
if (*m != LF && *m != CR && *m != '.' && *m != ' ') {
1679
ngx_log_error(NGX_LOG_ERR, p->log, 0,
1680
"FastCGI sent in stderr: \"%*s\"",
1683
if (f->pos == f->last) {
1688
f->state = ngx_http_fastcgi_st_version;
1695
/* f->type == NGX_HTTP_FASTCGI_STDOUT */
1697
if (f->pos == f->last) {
1703
p->free = p->free->next;
1706
b = ngx_alloc_buf(p->pool);
1712
ngx_memzero(b, sizeof(ngx_buf_t));
1715
b->start = buf->start;
1724
cl = ngx_alloc_chain_link(p->pool);
1737
p->last_in = &cl->next;
1740
/* STUB */ b->num = buf->num;
1742
ngx_log_debug2(NGX_LOG_DEBUG_EVENT, p->log, 0,
1743
"input buf #%d %p", b->num, b->pos);
1745
if (f->pos + f->length < f->last) {
1748
f->state = ngx_http_fastcgi_st_padding;
1750
f->state = ngx_http_fastcgi_st_version;
1753
f->pos += f->length;
1759
if (f->pos + f->length == f->last) {
1762
f->state = ngx_http_fastcgi_st_padding;
1764
f->state = ngx_http_fastcgi_st_version;
1772
f->length -= f->last - f->pos;
1784
ngx_log_debug2(NGX_LOG_DEBUG_EVENT, p->log, 0,
1785
"input buf %p %z", b->pos, b->last - b->pos);
1790
/* there is no data record in the buf, add it to free chain */
1792
if (ngx_event_pipe_add_free_buf(p, buf) != NGX_OK) {
1801
ngx_http_fastcgi_process_record(ngx_http_request_t *r,
1802
ngx_http_fastcgi_ctx_t *f)
1805
ngx_http_fastcgi_state_e state;
1809
for (p = f->pos; p < f->last; p++) {
1813
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1814
"http fastcgi record byte: %02Xd", ch);
1818
case ngx_http_fastcgi_st_version:
1820
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1821
"upstream sent unsupported FastCGI "
1822
"protocol version: %d", ch);
1825
state = ngx_http_fastcgi_st_type;
1828
case ngx_http_fastcgi_st_type:
1830
case NGX_HTTP_FASTCGI_STDOUT:
1831
case NGX_HTTP_FASTCGI_STDERR:
1832
case NGX_HTTP_FASTCGI_END_REQUEST:
1833
f->type = (ngx_uint_t) ch;
1836
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1837
"upstream sent invalid FastCGI "
1838
"record type: %d", ch);
1842
state = ngx_http_fastcgi_st_request_id_hi;
1845
/* we support the single request per connection */
1847
case ngx_http_fastcgi_st_request_id_hi:
1849
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1850
"upstream sent unexpected FastCGI "
1851
"request id high byte: %d", ch);
1854
state = ngx_http_fastcgi_st_request_id_lo;
1857
case ngx_http_fastcgi_st_request_id_lo:
1859
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1860
"upstream sent unexpected FastCGI "
1861
"request id low byte: %d", ch);
1864
state = ngx_http_fastcgi_st_content_length_hi;
1867
case ngx_http_fastcgi_st_content_length_hi:
1868
f->length = ch << 8;
1869
state = ngx_http_fastcgi_st_content_length_lo;
1872
case ngx_http_fastcgi_st_content_length_lo:
1873
f->length |= (size_t) ch;
1874
state = ngx_http_fastcgi_st_padding_length;
1877
case ngx_http_fastcgi_st_padding_length:
1878
f->padding = (size_t) ch;
1879
state = ngx_http_fastcgi_st_reserved;
1882
case ngx_http_fastcgi_st_reserved:
1883
state = ngx_http_fastcgi_st_data;
1885
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1886
"http fastcgi record length: %z", f->length);
1893
/* suppress warning */
1894
case ngx_http_fastcgi_st_data:
1895
case ngx_http_fastcgi_st_padding:
1907
ngx_http_fastcgi_abort_request(ngx_http_request_t *r)
1909
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1910
"abort http fastcgi request");
1917
ngx_http_fastcgi_finalize_request(ngx_http_request_t *r, ngx_int_t rc)
1919
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1920
"finalize http fastcgi request");
1927
ngx_http_fastcgi_add_variables(ngx_conf_t *cf)
1929
ngx_http_variable_t *var, *v;
1931
for (v = ngx_http_fastcgi_vars; v->name.len; v++) {
1932
var = ngx_http_add_variable(cf, &v->name, v->flags);
1937
var->get_handler = v->get_handler;
1938
var->data = v->data;
1946
ngx_http_fastcgi_create_loc_conf(ngx_conf_t *cf)
1948
ngx_http_fastcgi_loc_conf_t *conf;
1950
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_fastcgi_loc_conf_t));
1956
* set by ngx_pcalloc():
1958
* conf->upstream.bufs.num = 0;
1959
* conf->upstream.ignore_headers = 0;
1960
* conf->upstream.next_upstream = 0;
1961
* conf->upstream.cache_use_stale = 0;
1962
* conf->upstream.cache_methods = 0;
1963
* conf->upstream.temp_path = NULL;
1964
* conf->upstream.hide_headers_hash = { NULL, 0 };
1965
* conf->upstream.uri = { 0, NULL };
1966
* conf->upstream.location = NULL;
1967
* conf->upstream.store_lengths = NULL;
1968
* conf->upstream.store_values = NULL;
1970
* conf->index.len = { 0, NULL };
1973
conf->upstream.store = NGX_CONF_UNSET;
1974
conf->upstream.store_access = NGX_CONF_UNSET_UINT;
1975
conf->upstream.buffering = NGX_CONF_UNSET;
1976
conf->upstream.ignore_client_abort = NGX_CONF_UNSET;
1978
conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC;
1979
conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC;
1980
conf->upstream.read_timeout = NGX_CONF_UNSET_MSEC;
1982
conf->upstream.send_lowat = NGX_CONF_UNSET_SIZE;
1983
conf->upstream.buffer_size = NGX_CONF_UNSET_SIZE;
1985
conf->upstream.busy_buffers_size_conf = NGX_CONF_UNSET_SIZE;
1986
conf->upstream.max_temp_file_size_conf = NGX_CONF_UNSET_SIZE;
1987
conf->upstream.temp_file_write_size_conf = NGX_CONF_UNSET_SIZE;
1989
conf->upstream.pass_request_headers = NGX_CONF_UNSET;
1990
conf->upstream.pass_request_body = NGX_CONF_UNSET;
1992
#if (NGX_HTTP_CACHE)
1993
conf->upstream.cache = NGX_CONF_UNSET_PTR;
1994
conf->upstream.cache_min_uses = NGX_CONF_UNSET_UINT;
1995
conf->upstream.cache_bypass = NGX_CONF_UNSET_PTR;
1996
conf->upstream.no_cache = NGX_CONF_UNSET_PTR;
1997
conf->upstream.cache_valid = NGX_CONF_UNSET_PTR;
2000
conf->upstream.hide_headers = NGX_CONF_UNSET_PTR;
2001
conf->upstream.pass_headers = NGX_CONF_UNSET_PTR;
2003
conf->upstream.intercept_errors = NGX_CONF_UNSET;
2005
/* "fastcgi_cyclic_temp_file" is disabled */
2006
conf->upstream.cyclic_temp_file = 0;
2008
conf->catch_stderr = NGX_CONF_UNSET_PTR;
2015
ngx_http_fastcgi_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
2017
ngx_http_fastcgi_loc_conf_t *prev = parent;
2018
ngx_http_fastcgi_loc_conf_t *conf = child;
2024
ngx_array_t headers_names;
2027
ngx_hash_init_t hash;
2028
ngx_http_core_loc_conf_t *clcf;
2029
ngx_http_script_compile_t sc;
2030
ngx_http_script_copy_code_t *copy;
2032
if (conf->upstream.store != 0) {
2033
ngx_conf_merge_value(conf->upstream.store,
2034
prev->upstream.store, 0);
2036
if (conf->upstream.store_lengths == NULL) {
2037
conf->upstream.store_lengths = prev->upstream.store_lengths;
2038
conf->upstream.store_values = prev->upstream.store_values;
2042
ngx_conf_merge_uint_value(conf->upstream.store_access,
2043
prev->upstream.store_access, 0600);
2045
ngx_conf_merge_value(conf->upstream.buffering,
2046
prev->upstream.buffering, 1);
2048
ngx_conf_merge_value(conf->upstream.ignore_client_abort,
2049
prev->upstream.ignore_client_abort, 0);
2051
ngx_conf_merge_msec_value(conf->upstream.connect_timeout,
2052
prev->upstream.connect_timeout, 60000);
2054
ngx_conf_merge_msec_value(conf->upstream.send_timeout,
2055
prev->upstream.send_timeout, 60000);
2057
ngx_conf_merge_msec_value(conf->upstream.read_timeout,
2058
prev->upstream.read_timeout, 60000);
2060
ngx_conf_merge_size_value(conf->upstream.send_lowat,
2061
prev->upstream.send_lowat, 0);
2063
ngx_conf_merge_size_value(conf->upstream.buffer_size,
2064
prev->upstream.buffer_size,
2065
(size_t) ngx_pagesize);
2068
ngx_conf_merge_bufs_value(conf->upstream.bufs, prev->upstream.bufs,
2071
if (conf->upstream.bufs.num < 2) {
2072
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2073
"there must be at least 2 \"fastcgi_buffers\"");
2074
return NGX_CONF_ERROR;
2078
size = conf->upstream.buffer_size;
2079
if (size < conf->upstream.bufs.size) {
2080
size = conf->upstream.bufs.size;
2084
ngx_conf_merge_size_value(conf->upstream.busy_buffers_size_conf,
2085
prev->upstream.busy_buffers_size_conf,
2086
NGX_CONF_UNSET_SIZE);
2088
if (conf->upstream.busy_buffers_size_conf == NGX_CONF_UNSET_SIZE) {
2089
conf->upstream.busy_buffers_size = 2 * size;
2091
conf->upstream.busy_buffers_size =
2092
conf->upstream.busy_buffers_size_conf;
2095
if (conf->upstream.busy_buffers_size < size) {
2096
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2097
"\"fastcgi_busy_buffers_size\" must be equal or bigger than "
2098
"maximum of the value of \"fastcgi_buffer_size\" and "
2099
"one of the \"fastcgi_buffers\"");
2101
return NGX_CONF_ERROR;
2104
if (conf->upstream.busy_buffers_size
2105
> (conf->upstream.bufs.num - 1) * conf->upstream.bufs.size)
2107
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2108
"\"fastcgi_busy_buffers_size\" must be less than "
2109
"the size of all \"fastcgi_buffers\" minus one buffer");
2111
return NGX_CONF_ERROR;
2115
ngx_conf_merge_size_value(conf->upstream.temp_file_write_size_conf,
2116
prev->upstream.temp_file_write_size_conf,
2117
NGX_CONF_UNSET_SIZE);
2119
if (conf->upstream.temp_file_write_size_conf == NGX_CONF_UNSET_SIZE) {
2120
conf->upstream.temp_file_write_size = 2 * size;
2122
conf->upstream.temp_file_write_size =
2123
conf->upstream.temp_file_write_size_conf;
2126
if (conf->upstream.temp_file_write_size < size) {
2127
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2128
"\"fastcgi_temp_file_write_size\" must be equal or bigger than "
2129
"maximum of the value of \"fastcgi_buffer_size\" and "
2130
"one of the \"fastcgi_buffers\"");
2132
return NGX_CONF_ERROR;
2136
ngx_conf_merge_size_value(conf->upstream.max_temp_file_size_conf,
2137
prev->upstream.max_temp_file_size_conf,
2138
NGX_CONF_UNSET_SIZE);
2140
if (conf->upstream.max_temp_file_size_conf == NGX_CONF_UNSET_SIZE) {
2141
conf->upstream.max_temp_file_size = 1024 * 1024 * 1024;
2143
conf->upstream.max_temp_file_size =
2144
conf->upstream.max_temp_file_size_conf;
2147
if (conf->upstream.max_temp_file_size != 0
2148
&& conf->upstream.max_temp_file_size < size)
2150
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2151
"\"fastcgi_max_temp_file_size\" must be equal to zero to disable "
2152
"the temporary files usage or must be equal or bigger than "
2153
"maximum of the value of \"fastcgi_buffer_size\" and "
2154
"one of the \"fastcgi_buffers\"");
2156
return NGX_CONF_ERROR;
2160
ngx_conf_merge_bitmask_value(conf->upstream.ignore_headers,
2161
prev->upstream.ignore_headers,
2162
NGX_CONF_BITMASK_SET);
2165
ngx_conf_merge_bitmask_value(conf->upstream.next_upstream,
2166
prev->upstream.next_upstream,
2167
(NGX_CONF_BITMASK_SET
2168
|NGX_HTTP_UPSTREAM_FT_ERROR
2169
|NGX_HTTP_UPSTREAM_FT_TIMEOUT));
2171
if (conf->upstream.next_upstream & NGX_HTTP_UPSTREAM_FT_OFF) {
2172
conf->upstream.next_upstream = NGX_CONF_BITMASK_SET
2173
|NGX_HTTP_UPSTREAM_FT_OFF;
2176
if (ngx_conf_merge_path_value(cf, &conf->upstream.temp_path,
2177
prev->upstream.temp_path,
2178
&ngx_http_fastcgi_temp_path)
2181
return NGX_CONF_ERROR;
2184
#if (NGX_HTTP_CACHE)
2186
ngx_conf_merge_ptr_value(conf->upstream.cache,
2187
prev->upstream.cache, NULL);
2189
if (conf->upstream.cache && conf->upstream.cache->data == NULL) {
2190
ngx_shm_zone_t *shm_zone;
2192
shm_zone = conf->upstream.cache;
2194
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2195
"\"fastcgi_cache\" zone \"%V\" is unknown",
2196
&shm_zone->shm.name);
2198
return NGX_CONF_ERROR;
2201
ngx_conf_merge_uint_value(conf->upstream.cache_min_uses,
2202
prev->upstream.cache_min_uses, 1);
2204
ngx_conf_merge_bitmask_value(conf->upstream.cache_use_stale,
2205
prev->upstream.cache_use_stale,
2206
(NGX_CONF_BITMASK_SET
2207
|NGX_HTTP_UPSTREAM_FT_OFF));
2209
if (conf->upstream.cache_use_stale & NGX_HTTP_UPSTREAM_FT_OFF) {
2210
conf->upstream.cache_use_stale = NGX_CONF_BITMASK_SET
2211
|NGX_HTTP_UPSTREAM_FT_OFF;
2214
if (conf->upstream.cache_methods == 0) {
2215
conf->upstream.cache_methods = prev->upstream.cache_methods;
2218
conf->upstream.cache_methods |= NGX_HTTP_GET|NGX_HTTP_HEAD;
2220
ngx_conf_merge_ptr_value(conf->upstream.cache_bypass,
2221
prev->upstream.cache_bypass, NULL);
2223
ngx_conf_merge_ptr_value(conf->upstream.no_cache,
2224
prev->upstream.no_cache, NULL);
2226
if (conf->upstream.no_cache && conf->upstream.cache_bypass == NULL) {
2227
ngx_log_error(NGX_LOG_WARN, cf->log, 0,
2228
"\"fastcgi_no_cache\" functionality has been changed in 0.8.46, "
2229
"now it should be used together with \"fastcgi_cache_bypass\"");
2232
ngx_conf_merge_ptr_value(conf->upstream.cache_valid,
2233
prev->upstream.cache_valid, NULL);
2235
if (conf->cache_key.value.data == NULL) {
2236
conf->cache_key = prev->cache_key;
2241
ngx_conf_merge_value(conf->upstream.pass_request_headers,
2242
prev->upstream.pass_request_headers, 1);
2243
ngx_conf_merge_value(conf->upstream.pass_request_body,
2244
prev->upstream.pass_request_body, 1);
2246
ngx_conf_merge_value(conf->upstream.intercept_errors,
2247
prev->upstream.intercept_errors, 0);
2249
ngx_conf_merge_ptr_value(conf->catch_stderr, prev->catch_stderr, NULL);
2252
ngx_conf_merge_str_value(conf->index, prev->index, "");
2254
hash.max_size = 512;
2255
hash.bucket_size = ngx_align(64, ngx_cacheline_size);
2256
hash.name = "fastcgi_hide_headers_hash";
2258
if (ngx_http_upstream_hide_headers_hash(cf, &conf->upstream,
2259
&prev->upstream, ngx_http_fastcgi_hide_headers, &hash)
2262
return NGX_CONF_ERROR;
2265
if (conf->upstream.upstream == NULL) {
2266
conf->upstream.upstream = prev->upstream.upstream;
2269
if (conf->fastcgi_lengths == NULL) {
2270
conf->fastcgi_lengths = prev->fastcgi_lengths;
2271
conf->fastcgi_values = prev->fastcgi_values;
2274
if (conf->upstream.upstream || conf->fastcgi_lengths) {
2275
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
2276
if (clcf->handler == NULL && clcf->lmt_excpt) {
2277
clcf->handler = ngx_http_fastcgi_handler;
2282
if (conf->split_regex == NULL) {
2283
conf->split_regex = prev->split_regex;
2284
conf->split_name = prev->split_name;
2288
if (conf->params_source == NULL) {
2289
conf->flushes = prev->flushes;
2290
conf->params_len = prev->params_len;
2291
conf->params = prev->params;
2292
conf->params_source = prev->params_source;
2293
conf->headers_hash = prev->headers_hash;
2295
#if (NGX_HTTP_CACHE)
2297
if (conf->params_source == NULL) {
2299
if ((conf->upstream.cache == NULL)
2300
== (prev->upstream.cache == NULL))
2305
/* 6 is a number of ngx_http_fastcgi_cache_headers entries */
2306
conf->params_source = ngx_array_create(cf->pool, 6,
2307
sizeof(ngx_keyval_t));
2308
if (conf->params_source == NULL) {
2309
return NGX_CONF_ERROR;
2314
if (conf->params_source == NULL) {
2321
conf->params_len = ngx_array_create(cf->pool, 64, 1);
2322
if (conf->params_len == NULL) {
2323
return NGX_CONF_ERROR;
2326
conf->params = ngx_array_create(cf->pool, 512, 1);
2327
if (conf->params == NULL) {
2328
return NGX_CONF_ERROR;
2331
if (ngx_array_init(&headers_names, cf->temp_pool, 4, sizeof(ngx_hash_key_t))
2334
return NGX_CONF_ERROR;
2337
src = conf->params_source->elts;
2339
#if (NGX_HTTP_CACHE)
2341
if (conf->upstream.cache) {
2342
ngx_keyval_t *h, *s;
2344
for (h = ngx_http_fastcgi_cache_headers; h->key.len; h++) {
2346
for (i = 0; i < conf->params_source->nelts; i++) {
2347
if (ngx_strcasecmp(h->key.data, src[i].key.data) == 0) {
2352
s = ngx_array_push(conf->params_source);
2354
return NGX_CONF_ERROR;
2359
src = conf->params_source->elts;
2369
for (i = 0; i < conf->params_source->nelts; i++) {
2371
if (src[i].key.len > sizeof("HTTP_") - 1
2372
&& ngx_strncmp(src[i].key.data, "HTTP_", sizeof("HTTP_") - 1) == 0)
2374
hk = ngx_array_push(&headers_names);
2376
return NGX_CONF_ERROR;
2379
hk->key.len = src[i].key.len - 5;
2380
hk->key.data = src[i].key.data + 5;
2381
hk->key_hash = ngx_hash_key_lc(hk->key.data, hk->key.len);
2382
hk->value = (void *) 1;
2384
if (src[i].value.len == 0) {
2389
copy = ngx_array_push_n(conf->params_len,
2390
sizeof(ngx_http_script_copy_code_t));
2392
return NGX_CONF_ERROR;
2395
copy->code = (ngx_http_script_code_pt) ngx_http_script_copy_len_code;
2396
copy->len = src[i].key.len;
2399
size = (sizeof(ngx_http_script_copy_code_t)
2400
+ src[i].key.len + sizeof(uintptr_t) - 1)
2401
& ~(sizeof(uintptr_t) - 1);
2403
copy = ngx_array_push_n(conf->params, size);
2405
return NGX_CONF_ERROR;
2408
copy->code = ngx_http_script_copy_code;
2409
copy->len = src[i].key.len;
2411
p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t);
2412
ngx_memcpy(p, src[i].key.data, src[i].key.len);
2415
ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
2418
sc.source = &src[i].value;
2419
sc.flushes = &conf->flushes;
2420
sc.lengths = &conf->params_len;
2421
sc.values = &conf->params;
2423
if (ngx_http_script_compile(&sc) != NGX_OK) {
2424
return NGX_CONF_ERROR;
2427
code = ngx_array_push_n(conf->params_len, sizeof(uintptr_t));
2429
return NGX_CONF_ERROR;
2432
*code = (uintptr_t) NULL;
2435
code = ngx_array_push_n(conf->params, sizeof(uintptr_t));
2437
return NGX_CONF_ERROR;
2440
*code = (uintptr_t) NULL;
2443
code = ngx_array_push_n(conf->params_len, sizeof(uintptr_t));
2445
return NGX_CONF_ERROR;
2448
*code = (uintptr_t) NULL;
2451
conf->header_params = headers_names.nelts;
2453
hash.hash = &conf->headers_hash;
2454
hash.key = ngx_hash_key_lc;
2455
hash.max_size = 512;
2456
hash.bucket_size = 64;
2457
hash.name = "fastcgi_params_hash";
2458
hash.pool = cf->pool;
2459
hash.temp_pool = NULL;
2461
if (ngx_hash_init(&hash, headers_names.elts, headers_names.nelts) != NGX_OK)
2463
return NGX_CONF_ERROR;
2471
ngx_http_fastcgi_script_name_variable(ngx_http_request_t *r,
2472
ngx_http_variable_value_t *v, uintptr_t data)
2475
ngx_http_fastcgi_ctx_t *f;
2476
ngx_http_fastcgi_loc_conf_t *flcf;
2478
flcf = ngx_http_get_module_loc_conf(r, ngx_http_fastcgi_module);
2480
f = ngx_http_fastcgi_split(r, flcf);
2486
if (f->script_name.len == 0
2487
|| f->script_name.data[f->script_name.len - 1] != '/')
2489
v->len = f->script_name.len;
2491
v->no_cacheable = 0;
2493
v->data = f->script_name.data;
2498
v->len = f->script_name.len + flcf->index.len;
2500
v->data = ngx_pnalloc(r->pool, v->len);
2501
if (v->data == NULL) {
2505
p = ngx_copy(v->data, f->script_name.data, f->script_name.len);
2506
ngx_memcpy(p, flcf->index.data, flcf->index.len);
2513
ngx_http_fastcgi_path_info_variable(ngx_http_request_t *r,
2514
ngx_http_variable_value_t *v, uintptr_t data)
2516
ngx_http_fastcgi_ctx_t *f;
2517
ngx_http_fastcgi_loc_conf_t *flcf;
2519
flcf = ngx_http_get_module_loc_conf(r, ngx_http_fastcgi_module);
2521
f = ngx_http_fastcgi_split(r, flcf);
2527
v->len = f->path_info.len;
2529
v->no_cacheable = 0;
2531
v->data = f->path_info.data;
2537
static ngx_http_fastcgi_ctx_t *
2538
ngx_http_fastcgi_split(ngx_http_request_t *r, ngx_http_fastcgi_loc_conf_t *flcf)
2540
ngx_http_fastcgi_ctx_t *f;
2543
int captures[(1 + 2) * 3];
2545
f = ngx_http_get_module_ctx(r, ngx_http_fastcgi_module);
2548
f = ngx_pcalloc(r->pool, sizeof(ngx_http_fastcgi_ctx_t));
2553
ngx_http_set_ctx(r, f, ngx_http_fastcgi_module);
2556
if (f->script_name.len) {
2560
if (flcf->split_regex == NULL) {
2561
f->script_name = r->uri;
2565
n = ngx_regex_exec(flcf->split_regex, &r->uri, captures, (1 + 2) * 3);
2567
if (n >= 0) { /* match */
2568
f->script_name.len = captures[3] - captures[2];
2569
f->script_name.data = r->uri.data + captures[2];
2571
f->path_info.len = captures[5] - captures[4];
2572
f->path_info.data = r->uri.data + captures[4];
2577
if (n == NGX_REGEX_NO_MATCHED) {
2578
f->script_name = r->uri;
2582
ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
2583
ngx_regex_exec_n " failed: %i on \"%V\" using \"%V\"",
2584
n, &r->uri, &flcf->split_name);
2589
f = ngx_http_get_module_ctx(r, ngx_http_fastcgi_module);
2592
f = ngx_pcalloc(r->pool, sizeof(ngx_http_fastcgi_ctx_t));
2597
ngx_http_set_ctx(r, f, ngx_http_fastcgi_module);
2600
f->script_name = r->uri;
2609
ngx_http_fastcgi_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
2611
ngx_http_fastcgi_loc_conf_t *flcf = conf;
2614
ngx_str_t *value, *url;
2616
ngx_http_core_loc_conf_t *clcf;
2617
ngx_http_script_compile_t sc;
2619
if (flcf->upstream.upstream || flcf->fastcgi_lengths) {
2620
return "is duplicate";
2623
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
2625
clcf->handler = ngx_http_fastcgi_handler;
2627
if (clcf->name.data[clcf->name.len - 1] == '/') {
2628
clcf->auto_redirect = 1;
2631
value = cf->args->elts;
2635
n = ngx_http_script_variables_count(url);
2639
ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
2643
sc.lengths = &flcf->fastcgi_lengths;
2644
sc.values = &flcf->fastcgi_values;
2646
sc.complete_lengths = 1;
2647
sc.complete_values = 1;
2649
if (ngx_http_script_compile(&sc) != NGX_OK) {
2650
return NGX_CONF_ERROR;
2656
ngx_memzero(&u, sizeof(ngx_url_t));
2661
flcf->upstream.upstream = ngx_http_upstream_add(cf, &u, 0);
2662
if (flcf->upstream.upstream == NULL) {
2663
return NGX_CONF_ERROR;
2671
ngx_http_fastcgi_split_path_info(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
2674
ngx_http_fastcgi_loc_conf_t *flcf = conf;
2677
ngx_regex_compile_t rc;
2678
u_char errstr[NGX_MAX_CONF_ERRSTR];
2680
value = cf->args->elts;
2682
flcf->split_name = value[1];
2684
ngx_memzero(&rc, sizeof(ngx_regex_compile_t));
2686
rc.pattern = value[1];
2688
rc.err.len = NGX_MAX_CONF_ERRSTR;
2689
rc.err.data = errstr;
2691
if (ngx_regex_compile(&rc) != NGX_OK) {
2692
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "%V", &rc.err);
2693
return NGX_CONF_ERROR;
2696
if (rc.captures != 2) {
2697
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2698
"pattern \"%V\" must have 2 captures", &value[1]);
2699
return NGX_CONF_ERROR;
2702
flcf->split_regex = rc.regex;
2708
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2709
"\"%V\" requires PCRE library", &cmd->name);
2710
return NGX_CONF_ERROR;
2717
ngx_http_fastcgi_store(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
2719
ngx_http_fastcgi_loc_conf_t *flcf = conf;
2722
ngx_http_script_compile_t sc;
2724
if (flcf->upstream.store != NGX_CONF_UNSET
2725
|| flcf->upstream.store_lengths)
2727
return "is duplicate";
2730
value = cf->args->elts;
2732
if (ngx_strcmp(value[1].data, "off") == 0) {
2733
flcf->upstream.store = 0;
2737
#if (NGX_HTTP_CACHE)
2739
if (flcf->upstream.cache != NGX_CONF_UNSET_PTR
2740
&& flcf->upstream.cache != NULL)
2742
return "is incompatible with \"fastcgi_cache\"";
2747
if (ngx_strcmp(value[1].data, "on") == 0) {
2748
flcf->upstream.store = 1;
2752
/* include the terminating '\0' into script */
2755
ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
2758
sc.source = &value[1];
2759
sc.lengths = &flcf->upstream.store_lengths;
2760
sc.values = &flcf->upstream.store_values;
2761
sc.variables = ngx_http_script_variables_count(&value[1]);
2762
sc.complete_lengths = 1;
2763
sc.complete_values = 1;
2765
if (ngx_http_script_compile(&sc) != NGX_OK) {
2766
return NGX_CONF_ERROR;
2773
#if (NGX_HTTP_CACHE)
2776
ngx_http_fastcgi_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
2778
ngx_http_fastcgi_loc_conf_t *flcf = conf;
2782
value = cf->args->elts;
2784
if (flcf->upstream.cache != NGX_CONF_UNSET_PTR) {
2785
return "is duplicate";
2788
if (ngx_strcmp(value[1].data, "off") == 0) {
2789
flcf->upstream.cache = NULL;
2793
if (flcf->upstream.store > 0 || flcf->upstream.store_lengths) {
2794
return "is incompatible with \"fastcgi_store\"";
2797
flcf->upstream.cache = ngx_shared_memory_add(cf, &value[1], 0,
2798
&ngx_http_fastcgi_module);
2799
if (flcf->upstream.cache == NULL) {
2800
return NGX_CONF_ERROR;
2808
ngx_http_fastcgi_cache_key(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
2810
ngx_http_fastcgi_loc_conf_t *flcf = conf;
2813
ngx_http_compile_complex_value_t ccv;
2815
value = cf->args->elts;
2817
if (flcf->cache_key.value.len) {
2818
return "is duplicate";
2821
ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
2824
ccv.value = &value[1];
2825
ccv.complex_value = &flcf->cache_key;
2827
if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
2828
return NGX_CONF_ERROR;
2838
ngx_http_fastcgi_lowat_check(ngx_conf_t *cf, void *post, void *data)
2843
if ((u_long) *np >= ngx_freebsd_net_inet_tcp_sendspace) {
2844
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2845
"\"fastcgi_send_lowat\" must be less than %d "
2846
"(sysctl net.inet.tcp.sendspace)",
2847
ngx_freebsd_net_inet_tcp_sendspace);
2849
return NGX_CONF_ERROR;
2852
#elif !(NGX_HAVE_SO_SNDLOWAT)
2855
ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
2856
"\"fastcgi_send_lowat\" is not supported, ignored");