comparison src/http/modules/ngx_http_fastcgi_module.c @ 50:72eb30262aac NGINX_0_1_25

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