comparison src/http/modules/ngx_http_fastcgi_module.c.orig @ 164:b922c231a392 NGINX_0_3_29

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