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