Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 790 lines (623 sloc) 19.795 kb
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2
3 /* Cherokee
4 *
5 * Authors:
6 * Alvaro Lopez Ortega <alvaro@alobbs.com>
7 *
2c39092 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@2660 5dc97367-97f1...
alobbs authored
8 * Copyright (C) 2001-2009 Alvaro Lopez Ortega
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU General Public
12 * License as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
2c39092 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@2660 5dc97367-97f1...
alobbs authored
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 * 02110-1301, USA.
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
23 */
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
24
25 #include "common-internal.h"
a18b345 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1708 5dc97367-97f1...
alobbs authored
26
27 #include <fcntl.h>
28
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
29 #include "handler_fcgi.h"
30 #include "header.h"
31 #include "connection-protected.h"
32 #include "util.h"
33 #include "thread.h"
7b0e48a @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@387 5dc97367-97f1-...
alobbs authored
34 #include "source_interpreter.h"
a18b345 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1708 5dc97367-97f1...
alobbs authored
35 #include "bogotime.h"
4d387f4 @alobbs This patch changes substantially the way in which errors and warnings
alobbs authored
36 #include "error_log.h"
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
37
38 #include "fastcgi.h"
39
40 #define POST_PACKAGE_LEN 32600
41 #define ENTRIES "fcgi,handler"
42
43
44 #define set_env(cgi,key,val,len) \
45 set_env_pair (cgi, key, sizeof(key)-1, val, len)
46
47
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
48 static void set_env_pair (cherokee_handler_cgi_base_t *cgi_base,
49 const char *key, int key_len,
1e47304 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@2701 5dc97367-97f1...
alobbs authored
50 const char *val, int val_len);
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
51
b69d2e0 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@476 5dc97367-97f1-...
alobbs authored
52 /* Plug-in initialization
53 */
0d3a69e @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@2867 5dc97367-97f1...
alobbs authored
54 CGI_LIB_INIT (fcgi, http_all_methods);
b69d2e0 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@476 5dc97367-97f1-...
alobbs authored
55
56
57 /* Methods implementation
58 */
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
59 static ret_t
60 process_package (cherokee_handler_fcgi_t *hdl, cherokee_buffer_t *inbuf, cherokee_buffer_t *outbuf)
61 {
97945bf @alobbs Fixes bug #676. The server was crashing whenever PHP (via fastcgi)
alobbs authored
62 FCGI_Header *header;
63 FCGI_EndRequestBody *ending;
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
64
65 cuint_t len;
66 char *data;
67 cint_t return_val;
68 cuint_t type;
69 cuint_t id;
70 cuint_t padding;
4a68bff git-svn-id: svn://cherokee-project.com/cherokee/trunk@2404 5dc97367-97f1...
taher authored
71
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
72 /* Is there enough information?
73 */
74 if (inbuf->len < sizeof(FCGI_Header))
75 return ret_ok;
76
77 /* At least there is a header
78 */
79 header = (FCGI_Header *)inbuf->buf;
80
81 if (header->version != 1) {
82 cherokee_buffer_print_debug (inbuf, -1);
165f0ad @alobbs Merges the 'newerrors' branch (r3613:HEAD). It implements a new
alobbs authored
83 LOG_ERROR_S (CHEROKEE_ERROR_HANDLER_FCGI_VERSION);
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
84 return ret_error;
85 }
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
86
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
87 if (header->type != FCGI_STDERR &&
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
88 header->type != FCGI_STDOUT &&
81679d0 handler_*.c, space / code style cleanups
ADF authored
89 header->type != FCGI_END_REQUEST) {
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
90 cherokee_buffer_print_debug (inbuf, -1);
165f0ad @alobbs Merges the 'newerrors' branch (r3613:HEAD). It implements a new
alobbs authored
91 LOG_ERROR_S (CHEROKEE_ERROR_HANDLER_FCGI_PARSING);
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
92 return ret_error;
93 }
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
94
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
95 /* Read the header
96 */
97 type = header->type;
98 padding = header->paddingLength;
99 id = (header->requestIdB0 | (header->requestIdB1 << 8));
100 len = (header->contentLengthB0 | (header->contentLengthB1 << 8));
101 data = inbuf->buf + FCGI_HEADER_LEN;
102
b13f4bf @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@446 5dc97367-97f1-...
alobbs authored
103 /* printf ("have %d, hdr=%d exp_len=%d pad=%d\n", inbuf->len, FCGI_HEADER_LEN, len, padding); */
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
104
105 /* Is the package complete?
106 */
107 if (len + padding > inbuf->len - FCGI_HEADER_LEN) {
b13f4bf @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@446 5dc97367-97f1-...
alobbs authored
108 /* printf ("Incomplete: %d < %d\n", len + padding, inbuf->len - FCGI_HEADER_LEN); */
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
109 return ret_ok;
110 }
111
112 /* It has received the full package content
113 */
114 switch (type) {
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
115 case FCGI_STDERR:
b13f4bf @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@446 5dc97367-97f1-...
alobbs authored
116 /* printf ("READ:STDERR (%d): %s", len, data?data:""); */
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
117
97945bf @alobbs Fixes bug #676. The server was crashing whenever PHP (via fastcgi)
alobbs authored
118 LOG_ERROR (CHEROKEE_ERROR_HANDLER_FCGI_STDERR, data);
119
120 /* Debug mode */
121 if (SOURCE_INT(hdl->src_ref)->debug) {
4a68bff git-svn-id: svn://cherokee-project.com/cherokee/trunk@2404 5dc97367-97f1...
taher authored
122 PRINT_MSG ("%.*s\n", len, data);
123 }
124
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
125 break;
126
127 case FCGI_STDOUT:
8b87f31 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3199 5dc97367-97f1...
alobbs authored
128 /* printf ("READ:STDOUT eof=%d: %d", HDL_CGI_BASE(hdl)->got_eof, len); */
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
129 cherokee_buffer_add (outbuf, data, len);
130 break;
131
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
132 case FCGI_END_REQUEST:
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
133 ending = (FCGI_EndRequestBody *)data;
134
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
135 return_val = ((ending->appStatusB0) |
136 (ending->appStatusB0 << 8) |
137 (ending->appStatusB0 << 16) |
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
138 (ending->appStatusB0 << 24));
139
3509796 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@282 5dc97367-97f1-...
alobbs authored
140 HDL_CGI_BASE(hdl)->got_eof = true;
b13f4bf @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@446 5dc97367-97f1-...
alobbs authored
141 /* printf ("READ:END"); */
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
142 break;
143
144 default:
145 SHOULDNT_HAPPEN;
146 }
147
148 cherokee_buffer_move_to_begin (inbuf, len + FCGI_HEADER_LEN + padding);
4a68bff git-svn-id: svn://cherokee-project.com/cherokee/trunk@2404 5dc97367-97f1...
taher authored
149 /* printf ("- FCGI left %d\n", inbuf->len); */
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
150 return ret_eagain;
151 }
152
153
154 static ret_t
155 process_buffer (cherokee_handler_fcgi_t *hdl, cherokee_buffer_t *inbuf, cherokee_buffer_t *outbuf)
156 {
157 ret_t ret;
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
158
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
159 do {
160 ret = process_package (hdl, inbuf, outbuf);
161 } while (ret == ret_eagain);
162
163 if (ret == ret_ok) {
164 if (cherokee_buffer_is_empty (outbuf))
3509796 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@282 5dc97367-97f1-...
alobbs authored
165 return (HDL_CGI_BASE(hdl)->got_eof) ? ret_eof : ret_eagain;
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
166 }
167
168 return ret;
169 }
170
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
171 static ret_t
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
172 read_from_fcgi (cherokee_handler_cgi_base_t *cgi, cherokee_buffer_t *buffer)
173 {
174 ret_t ret;
175 size_t read = 0;
3509796 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@282 5dc97367-97f1-...
alobbs authored
176 cherokee_handler_fcgi_t *fcgi = HDL_FCGI(cgi);
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
177
0932395 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@656 5dc97367-97f1-...
alobbs authored
178 ret = cherokee_socket_bufread (&fcgi->socket, &fcgi->write_buffer, DEFAULT_READ_SIZE, &read);
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
179
180 switch (ret) {
181 case ret_eagain:
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
182 ret = cherokee_thread_deactive_to_polling (HANDLER_THREAD(cgi), HANDLER_CONN(cgi),
24f5c27 @alobbs Check thread_deactive_to_polling() returned value.
alobbs authored
183 fcgi->socket.socket, FDPOLL_MODE_READ,
184 false);
185 if (unlikely (ret != ret_ok)) {
186 cgi->got_eof = true;
187 return ret_error;
188 }
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
189 return ret_eagain;
190
191 case ret_ok:
192 ret = process_buffer (fcgi, &fcgi->write_buffer, buffer);
d5e94a6 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1175 5dc97367-97f1...
alobbs authored
193 TRACE (ENTRIES, "%d bytes read, buffer.len %d\n", read, buffer->len);
24f5c27 @alobbs Check thread_deactive_to_polling() returned value.
alobbs authored
194
195 if ((ret == ret_ok) && cgi->got_eof && (buffer->len > 0)) {
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
196 return ret_eof_have_data;
24f5c27 @alobbs Check thread_deactive_to_polling() returned value.
alobbs authored
197 }
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
198 return ret;
199
200 case ret_eof:
201 case ret_error:
202 cgi->got_eof = true;
203 return ret;
204
205 default:
206 RET_UNKNOWN(ret);
207 }
208
209 SHOULDNT_HAPPEN;
2e0ea0e @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@430 5dc97367-97f1-...
alobbs authored
210 return ret_error;
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
211 }
212
213
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
214 static ret_t
be04810 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@283 5dc97367-97f1-...
alobbs authored
215 props_free (cherokee_handler_fcgi_props_t *props)
216 {
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
217 if (props->balancer != NULL)
7b0e48a @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@387 5dc97367-97f1-...
alobbs authored
218 cherokee_balancer_free (props->balancer);
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
219
623deda @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@294 5dc97367-97f1-...
alobbs authored
220 return cherokee_handler_cgi_base_props_free (PROP_CGI_BASE(props));
be04810 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@283 5dc97367-97f1-...
alobbs authored
221 }
222
223
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
224 ret_t
225 cherokee_handler_fcgi_configure (cherokee_config_node_t *conf,
8b87f31 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3199 5dc97367-97f1...
alobbs authored
226 cherokee_server_t *srv,
227 cherokee_module_props_t **_props)
b6c69ad @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@269 5dc97367-97f1-...
alobbs authored
228 {
3509796 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@282 5dc97367-97f1-...
alobbs authored
229 ret_t ret;
ee8023f @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@384 5dc97367-97f1-...
alobbs authored
230 cherokee_list_t *i;
3509796 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@282 5dc97367-97f1-...
alobbs authored
231 cherokee_handler_fcgi_props_t *props;
b6c69ad @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@269 5dc97367-97f1-...
alobbs authored
232
3509796 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@282 5dc97367-97f1-...
alobbs authored
233 /* Instance a new property object
234 */
235 if (*_props == NULL) {
236 CHEROKEE_NEW_STRUCT (n, handler_fcgi_props);
be04810 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@283 5dc97367-97f1-...
alobbs authored
237
b69d2e0 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@476 5dc97367-97f1-...
alobbs authored
238 cherokee_handler_cgi_base_props_init_base (PROP_CGI_BASE(n),
239 MODULE_PROPS_FREE(props_free));
240
241 INIT_LIST_HEAD (&n->server_list);
7b0e48a @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@387 5dc97367-97f1-...
alobbs authored
242 n->balancer = NULL;
243
244 *_props = MODULE_PROPS(n);
3509796 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@282 5dc97367-97f1-...
alobbs authored
245 }
246
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
247 props = PROP_FCGI(*_props);
3509796 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@282 5dc97367-97f1-...
alobbs authored
248
249 /* Parse the configuration tree
250 */
b6c69ad @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@269 5dc97367-97f1-...
alobbs authored
251 cherokee_config_node_foreach (i, conf) {
252 cherokee_config_node_t *subconf = CONFIG_NODE(i);
253
7b0e48a @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@387 5dc97367-97f1-...
alobbs authored
254 if (equal_buf_str (&subconf->key, "balancer")) {
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
255 ret = cherokee_balancer_instance (&subconf->val, subconf, srv, &props->balancer);
b6c69ad @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@269 5dc97367-97f1-...
alobbs authored
256 if (ret != ret_ok) return ret;
257 }
258 }
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
259
7b0e48a @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@387 5dc97367-97f1-...
alobbs authored
260 /* Init base class
261 */
262 ret = cherokee_handler_cgi_base_configure (conf, srv, _props);
263 if (ret != ret_ok) return ret;
264
265 /* Final checks
266 */
267 if (props->balancer == NULL) {
165f0ad @alobbs Merges the 'newerrors' branch (r3613:HEAD). It implements a new
alobbs authored
268 LOG_CRITICAL_S (CHEROKEE_ERROR_HANDLER_FCGI_BALANCER);
7b0e48a @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@387 5dc97367-97f1-...
alobbs authored
269 return ret_error;
270 }
271
272 return ret_ok;
b6c69ad @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@269 5dc97367-97f1-...
alobbs authored
273 }
274
275
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
276 ret_t
7b0e48a @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@387 5dc97367-97f1-...
alobbs authored
277 cherokee_handler_fcgi_new (cherokee_handler_t **hdl, void *cnt, cherokee_module_props_t *props)
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
278 {
279 CHEROKEE_NEW_STRUCT (n, handler_fcgi);
280
281 /* Init the base class
282 */
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
283 cherokee_handler_cgi_base_init (HDL_CGI_BASE(n), cnt, PLUGIN_INFO_HANDLER_PTR(fcgi),
b69d2e0 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@476 5dc97367-97f1-...
alobbs authored
284 HANDLER_PROPS(props), set_env_pair, read_from_fcgi);
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
285
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
286 /* Virtual methods
287 */
288 MODULE(n)->init = (handler_func_init_t) cherokee_handler_fcgi_init;
423d6fa @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@370 5dc97367-97f1-...
alobbs authored
289 MODULE(n)->free = (module_func_free_t) cherokee_handler_fcgi_free;
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
290
291 /* Virtual methods: implemented by handler_cgi_base
292 */
293 HANDLER(n)->step = (handler_func_step_t) cherokee_handler_cgi_base_step;
294 HANDLER(n)->add_headers = (handler_func_add_headers_t) cherokee_handler_cgi_base_add_headers;
295
296 /* Properties
297 */
298 n->post_phase = fcgi_post_init;
299 n->post_len = 0;
a18b345 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1708 5dc97367-97f1...
alobbs authored
300 n->src_ref = NULL;
ba212a4 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@467 5dc97367-97f1-...
alobbs authored
301
a18b345 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1708 5dc97367-97f1...
alobbs authored
302 cherokee_socket_init (&n->socket);
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
303 cherokee_buffer_init (&n->write_buffer);
304 cherokee_buffer_ensure_size (&n->write_buffer, 512);
305
306 /* Return the object
307 */
308 *hdl = HANDLER(n);
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
309 return ret_ok;
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
310 }
311
312
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
313 ret_t
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
314 cherokee_handler_fcgi_free (cherokee_handler_fcgi_t *hdl)
315 {
483759d @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@466 5dc97367-97f1-...
alobbs authored
316 TRACE (ENTRIES, "fcgi handler free: %p\n", hdl);
317
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
318 cherokee_socket_close (&hdl->socket);
319 cherokee_socket_mrproper (&hdl->socket);
320
321 cherokee_buffer_mrproper (&hdl->write_buffer);
322
3509796 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@282 5dc97367-97f1-...
alobbs authored
323 return cherokee_handler_cgi_base_free (HDL_CGI_BASE(hdl));
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
324 }
325
326
327 static void
328 fcgi_build_header (FCGI_Header *hdr, cuchar_t type, cushort_t request_id, cuint_t content_length, cuchar_t padding)
329 {
81679d0 handler_*.c, space / code style cleanups
ADF authored
330 hdr->version = FCGI_VERSION_1;
331 hdr->type = type;
332 hdr->requestIdB0 = (cuchar_t) request_id;
333 hdr->requestIdB1 = (cuchar_t) (request_id >> 8) & 0xff;
334 hdr->contentLengthB0 = (cuchar_t) (content_length % 256);
335 hdr->contentLengthB1 = (cuchar_t) (content_length / 256);
336 hdr->paddingLength = padding;
337 hdr->reserved = 0;
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
338 }
339
340 static void
341 fcgi_build_request_body (FCGI_BeginRequestRecord *request)
342 {
81679d0 handler_*.c, space / code style cleanups
ADF authored
343 request->body.roleB0 = FCGI_RESPONDER;
344 request->body.roleB1 = 0;
345 request->body.flags = 0;
346 request->body.reserved[0] = 0;
347 request->body.reserved[1] = 0;
348 request->body.reserved[2] = 0;
349 request->body.reserved[3] = 0;
350 request->body.reserved[4] = 0;
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
351 }
352
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
353 static void
354 set_env_pair (cherokee_handler_cgi_base_t *cgi_base,
355 const char *key, int key_len,
1e47304 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@2701 5dc97367-97f1...
alobbs authored
356 const char *val, int val_len)
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
357 {
81679d0 handler_*.c, space / code style cleanups
ADF authored
358 int len;
359 FCGI_BeginRequestRecord request;
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
360 cherokee_handler_fcgi_t *hdl = HDL_FCGI(cgi_base);
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
361 cherokee_buffer_t *buf = &hdl->write_buffer;
362
0a136b0 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3233 5dc97367-97f1...
alobbs authored
363 #ifdef TRACE_ENABLED
364 cherokee_buffer_t *tmp = &HANDLER_THREAD(cgi_base)->tmp_buf2;
365
366 cherokee_buffer_clean (tmp);
367 cherokee_buffer_add (tmp, key, key_len);
368 cherokee_buffer_add_str (tmp, " = \"");
369 cherokee_buffer_add (tmp, val, val_len);
370 cherokee_buffer_add_str (tmp, "\"\n");
371
372 TRACE (ENTRIES, "%s", tmp->buf);
373 #endif
374
81679d0 handler_*.c, space / code style cleanups
ADF authored
375 len = key_len + val_len;
376 len += key_len > 127 ? 4 : 1;
377 len += val_len > 127 ? 4 : 1;
378
379 fcgi_build_header (&request.header, FCGI_PARAMS, 1, len, 0);
380
381 cherokee_buffer_ensure_size (buf, buf->len + sizeof(FCGI_Header) + key_len + val_len);
382 cherokee_buffer_add (buf, (void *)&request.header, sizeof(FCGI_Header));
383
384 if (key_len <= 127) {
385 buf->buf[buf->len++] = key_len;
386 } else {
387 buf->buf[buf->len++] = ((key_len >> 24) & 0xff) | 0x80;
388 buf->buf[buf->len++] = (key_len >> 16) & 0xff;
389 buf->buf[buf->len++] = (key_len >> 8) & 0xff;
390 buf->buf[buf->len++] = (key_len >> 0) & 0xff;
391 }
392
393 if (val_len <= 127) {
394 buf->buf[buf->len++] = val_len;
395 } else {
396 buf->buf[buf->len++] = ((val_len >> 24) & 0xff) | 0x80;
397 buf->buf[buf->len++] = (val_len >> 16) & 0xff;
398 buf->buf[buf->len++] = (val_len >> 8) & 0xff;
399 buf->buf[buf->len++] = (val_len >> 0) & 0xff;
400 }
401
402 cherokee_buffer_add (buf, key, key_len);
403 cherokee_buffer_add (buf, val, val_len);
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
404 }
405
406
407 static ret_t
408 add_extra_fcgi_env (cherokee_handler_fcgi_t *hdl, cuint_t *last_header_offset)
409 {
8cdea13 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@2969 5dc97367-97f1...
alobbs authored
410 off_t post_len = 0;
3509796 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@282 5dc97367-97f1-...
alobbs authored
411 cherokee_handler_cgi_base_t *cgi_base = HDL_CGI_BASE(hdl);
81679d0 handler_*.c, space / code style cleanups
ADF authored
412 cherokee_buffer_t buffer = CHEROKEE_BUF_INIT;
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
413 cherokee_connection_t *conn = HANDLER_CONN(hdl);
414
415 /* CONTENT_LENGTH
416 */
8cdea13 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@2969 5dc97367-97f1...
alobbs authored
417 if (http_method_with_input (conn->header.method)) {
418 cherokee_post_get_len (&conn->post, &post_len);
419
420 cherokee_buffer_add_ullong10 (&buffer, post_len);
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
421 set_env (cgi_base, "CONTENT_LENGTH", buffer.buf, buffer.len);
8cdea13 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@2969 5dc97367-97f1...
alobbs authored
422 }
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
423
424 /* Add PATH_TRANSLATED only it there is pathinfo
425 */
426 #if 0
427 if (! cherokee_buffer_is_empty (&conn->pathinfo)) {
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
428 cherokee_buffer_add_buffer (&buffer, &conn->local_directory);
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
429 cherokee_buffer_add_buffer (&buffer, &conn->pathinfo);
430
431 set_env (cgi_base, "PATH_TRANSLATED", buffer.buf, buffer.len);
432 TRACE (ENTRIES, "PATH_TRANSLATED '%s'\n", cgi_base->executable.buf);
433 }
434 #endif
435
436 /* The last one
437 */
438 *last_header_offset = hdl->write_buffer.len;
439
6b3a530 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@2959 5dc97367-97f1...
alobbs authored
440 set_env (cgi_base, "SCRIPT_FILENAME",
441 cgi_base->executable.buf,
442 cgi_base->executable.len);
443
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
444 TRACE (ENTRIES, "SCRIPT_FILENAME '%s'\n", cgi_base->executable.buf);
445
446 cherokee_buffer_mrproper (&buffer);
447 return ret_ok;
448 }
449
450
451 static void
452 fixup_padding (cherokee_buffer_t *buf, cuint_t last_header_offset)
453 {
81679d0 handler_*.c, space / code style cleanups
ADF authored
454 cuint_t rest;
455 cuint_t pad;
456 static char padding[8] = {0, 0, 0, 0, 0, 0, 0, 0};
457 FCGI_Header *last_header;
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
458
81679d0 handler_*.c, space / code style cleanups
ADF authored
459 if (buf->len <= 0)
460 return;
461 last_header = (FCGI_Header *) (buf->buf + last_header_offset);
462 rest = buf->len % 8;
463 pad = 8 - rest;
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
464
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
465 if (rest == 0)
81679d0 handler_*.c, space / code style cleanups
ADF authored
466 return;
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
467
81679d0 handler_*.c, space / code style cleanups
ADF authored
468 last_header->paddingLength = pad;
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
469
81679d0 handler_*.c, space / code style cleanups
ADF authored
470 cherokee_buffer_ensure_size (buf, buf->len + pad);
471 cherokee_buffer_add (buf, padding, pad);
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
472 }
473
474 static void
475 add_empty_packet (cherokee_handler_fcgi_t *hdl, cuint_t type)
476 {
81679d0 handler_*.c, space / code style cleanups
ADF authored
477 FCGI_BeginRequestRecord request;
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
478
81679d0 handler_*.c, space / code style cleanups
ADF authored
479 fcgi_build_header (&request.header, type, 1, 0, 0);
480 cherokee_buffer_add (&hdl->write_buffer, (void *)&request.header, sizeof(FCGI_Header));
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
481
81679d0 handler_*.c, space / code style cleanups
ADF authored
482 TRACE (ENTRIES, "empty packet type=%d, len=%d\n", type, hdl->write_buffer.len);
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
483 }
484
485
486 static ret_t
487 build_header (cherokee_handler_fcgi_t *hdl, cherokee_buffer_t *buffer)
488 {
489 FCGI_BeginRequestRecord request;
81679d0 handler_*.c, space / code style cleanups
ADF authored
490 cuint_t last_header_offset;
2e0ea0e @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@430 5dc97367-97f1-...
alobbs authored
491 cherokee_connection_t *conn = HANDLER_CONN(hdl);
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
492
493 cherokee_buffer_clean (buffer);
494
495 /* FCGI_BEGIN_REQUEST
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
496 */
81679d0 handler_*.c, space / code style cleanups
ADF authored
497 fcgi_build_header (&request.header, FCGI_BEGIN_REQUEST, 1, sizeof(request.body), 0);
498 fcgi_build_request_body (&request);
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
499
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
500 cherokee_buffer_add (buffer, (void *)&request, sizeof(FCGI_BeginRequestRecord));
81679d0 handler_*.c, space / code style cleanups
ADF authored
501 TRACE (ENTRIES, "Added FCGI_BEGIN_REQUEST, len=%d\n", buffer->len);
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
502
503 /* Add enviroment variables
504 */
3509796 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@282 5dc97367-97f1-...
alobbs authored
505 cherokee_handler_cgi_base_build_envp (HDL_CGI_BASE(hdl), conn);
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
506
507 add_extra_fcgi_env (hdl, &last_header_offset);
81679d0 handler_*.c, space / code style cleanups
ADF authored
508 fixup_padding (buffer, last_header_offset);
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
509
81679d0 handler_*.c, space / code style cleanups
ADF authored
510 /* There are no more parameters
511 */
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
512 add_empty_packet (hdl, FCGI_PARAMS);
513
81679d0 handler_*.c, space / code style cleanups
ADF authored
514 TRACE (ENTRIES, "Added FCGI_PARAMS, len=%d\n", buffer->len);
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
515 return ret_ok;
516 }
517
518
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
519 static ret_t
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
520 connect_to_server (cherokee_handler_fcgi_t *hdl)
521 {
3509796 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@282 5dc97367-97f1-...
alobbs authored
522 ret_t ret;
5695e23 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1849 5dc97367-97f1...
alobbs authored
523 cherokee_connection_t *conn = HANDLER_CONN(hdl);
524 cherokee_handler_fcgi_props_t *props = HANDLER_FCGI_PROPS(hdl);
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
525
a18b345 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1708 5dc97367-97f1...
alobbs authored
526 /* Get a reference to the target host
527 */
528 if (hdl->src_ref == NULL) {
529 ret = cherokee_balancer_dispatch (props->balancer, conn, &hdl->src_ref);
530 if (ret != ret_ok)
531 return ret;
532 }
7b0e48a @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@387 5dc97367-97f1-...
alobbs authored
533
a18b345 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1708 5dc97367-97f1...
alobbs authored
534 /* Try to connect
535 */
14efdd8 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@2572 5dc97367-97f1...
alobbs authored
536 if (hdl->src_ref->type == source_host) {
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
537 ret = cherokee_source_connect_polling (hdl->src_ref, &hdl->socket, conn);
14efdd8 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@2572 5dc97367-97f1...
alobbs authored
538 if ((ret == ret_deny) || (ret == ret_error))
539 {
540 cherokee_balancer_report_fail (props->balancer, conn, hdl->src_ref);
541 }
542 } else {
543 ret = cherokee_source_interpreter_connect_polling (SOURCE_INT(hdl->src_ref),
83990a0 @alobbs A fragment of the spawning mechanism has been rewritten. This new
alobbs authored
544 &hdl->socket, conn);
14efdd8 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@2572 5dc97367-97f1...
alobbs authored
545 }
45ead25 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@428 5dc97367-97f1-...
alobbs authored
546
14efdd8 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@2572 5dc97367-97f1...
alobbs authored
547 return ret;
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
548 }
549
550
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
551 static ret_t
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
552 do_send (cherokee_handler_fcgi_t *hdl, cherokee_buffer_t *buffer)
553 {
2e0ea0e @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@430 5dc97367-97f1-...
alobbs authored
554 ret_t ret;
b0dd10b @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1398 5dc97367-97f1...
alobbs authored
555 size_t written = 0;
2e0ea0e @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@430 5dc97367-97f1-...
alobbs authored
556 cherokee_connection_t *conn = HANDLER_CONN(hdl);
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
557
0932395 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@656 5dc97367-97f1-...
alobbs authored
558 ret = cherokee_socket_bufwrite (&hdl->socket, buffer, &written);
ba212a4 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@467 5dc97367-97f1-...
alobbs authored
559 switch (ret) {
560 case ret_ok:
3cbd17a @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@475 5dc97367-97f1-...
alobbs authored
561 break;
ba212a4 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@467 5dc97367-97f1-...
alobbs authored
562 case ret_eagain:
b69d2e0 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@476 5dc97367-97f1-...
alobbs authored
563 if (written > 0)
564 break;
ba212a4 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@467 5dc97367-97f1-...
alobbs authored
565 return ret_eagain;
566 default:
2e0ea0e @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@430 5dc97367-97f1-...
alobbs authored
567 conn->error_code = http_bad_gateway;
ba212a4 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@467 5dc97367-97f1-...
alobbs authored
568 return ret_error;
2e0ea0e @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@430 5dc97367-97f1-...
alobbs authored
569 }
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
570
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
571 cherokee_buffer_move_to_begin (buffer, written);
572
573 TRACE (ENTRIES, "sent remaining=%d\n", buffer->len);
574
575 if (! cherokee_buffer_is_empty (buffer))
576 return ret_eagain;
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
577
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
578 return ret_ok;
579 }
580
581
582 static ret_t
f11857f @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@600 5dc97367-97f1-...
alobbs authored
583 send_no_post (cherokee_handler_fcgi_t *hdl, cherokee_buffer_t *buf)
584 {
585 switch (hdl->post_phase) {
586 case fcgi_post_init:
587 add_empty_packet (hdl, FCGI_STDIN);
588 hdl->post_phase = fcgi_post_write;
589
590 case fcgi_post_write:
591 return do_send (hdl, buf);
592
593 default:
594 SHOULDNT_HAPPEN;
595 }
596 return ret_error;
597 }
598
599
600 static ret_t
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
601 send_post (cherokee_handler_fcgi_t *hdl, cherokee_buffer_t *buf)
602 {
603 ret_t ret;
604 cherokee_connection_t *conn = HANDLER_CONN(hdl);
605 static FCGI_Header empty_header = {0,0,0,0,0,0,0,0};
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
606
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
607 switch (hdl->post_phase) {
608 case fcgi_post_init:
609 TRACE (ENTRIES, "Post %s\n", "init");
610
611 /* Init the POST storing object
612 */
613 cherokee_post_walk_reset (&conn->post);
614 cherokee_post_get_len (&conn->post, &hdl->post_len);
615
616 if (hdl->post_len <= 0)
617 return ret_ok;
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
618
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
619 hdl->post_phase = fcgi_post_read;
620
621 case fcgi_post_read:
622 TRACE (ENTRIES, "Post %s\n", "read");
623
624 /* Add space for the header, it'll filled out later on..
625 */
626 if (cherokee_buffer_is_empty (buf)) {
0f3a68f @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@625 5dc97367-97f1-...
alobbs authored
627 cherokee_buffer_add (buf, (const char *)&empty_header, sizeof (FCGI_Header));
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
628 }
629
630 /* Take a chunck of post
631 */
632 ret = cherokee_post_walk_read (&conn->post, buf, POST_PACKAGE_LEN);
633 switch (ret) {
634 case ret_ok:
635 case ret_eagain:
636 break;
637 case ret_error:
638 return ret;
639 default:
640 RET_UNKNOWN(ret);
641 return ret_error;
642 }
643
644 TRACE (ENTRIES, "Post buffer.len %d\n", buf->len);
645
646 /* Complete the header
647 */
648 if (buf->len > sizeof(FCGI_Header)) {
649 fcgi_build_header ((FCGI_Header *)buf->buf, FCGI_STDIN, 1,
650 buf->len - sizeof(FCGI_Header), 0);
651 }
652
653 /* Close STDIN if it was the last chunck
654 */
655 ret = cherokee_post_walk_finished (&conn->post);
656 if (ret == ret_ok) {
657 add_empty_packet (hdl, FCGI_STDIN);
658 }
659
660 hdl->post_phase = fcgi_post_write;
661
662 case fcgi_post_write:
663 TRACE (ENTRIES, "Post write, buf.len=%d (header len %d)\n", buf->len, sizeof(FCGI_Header));
664
665 if (! cherokee_buffer_is_empty (buf)) {
666 ret = do_send (hdl, buf);
667 switch (ret) {
668 case ret_ok:
669 break;
670 case ret_eagain:
671 return ret_eagain;
672 case ret_eof:
673 case ret_error:
674 return ret_error;
675 default:
676 RET_UNKNOWN(ret);
677 return ret_error;
678 }
679 }
680
681 if (! cherokee_buffer_is_empty (buf))
682 return ret_eagain;
683
684 ret = cherokee_post_walk_finished (&conn->post);
685 switch (ret) {
686 case ret_ok:
687 break;
688 case ret_error:
689 return ret_error;
690 case ret_eagain:
691 hdl->post_phase = fcgi_post_read;
692 return ret_eagain;
693 default:
694 RET_UNKNOWN(ret);
695 return ret_error;
696 }
697
698 TRACE (ENTRIES, "Post %s\n", "finished");
699 return ret_ok;
700
701 default:
702 SHOULDNT_HAPPEN;
703 }
704
705 return ret_error;
706 }
707
708
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
709 ret_t
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
710 cherokee_handler_fcgi_init (cherokee_handler_fcgi_t *hdl)
711 {
b830ce0 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3264 5dc97367-97f1...
alobbs authored
712 ret_t ret;
713 cherokee_connection_t *conn = HANDLER_CONN(hdl);
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
714 cherokee_handler_cgi_base_props_t *props = HANDLER_CGI_BASE_PROPS(hdl);
715
3509796 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@282 5dc97367-97f1-...
alobbs authored
716 switch (HDL_CGI_BASE(hdl)->init_phase) {
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
717 case hcgi_phase_build_headers:
a18b345 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1708 5dc97367-97f1...
alobbs authored
718 TRACE (ENTRIES, "Init: %s\n", "begins");
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
719
720 /* Prepare Post
721 */
722 if (! cherokee_post_is_empty (&conn->post)) {
723 cherokee_post_walk_reset (&conn->post);
724 cherokee_post_get_len (&conn->post, &hdl->post_len);
725 }
726
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
727 /* Extracts PATH_INFO and filename from request uri
728 */
b830ce0 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3264 5dc97367-97f1...
alobbs authored
729 ret = cherokee_handler_cgi_base_extract_path (HDL_CGI_BASE(hdl), props->check_file);
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
730 if (unlikely (ret < ret_ok)) return ret;
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
731
732 /* Build the headers
733 */
734 ret = build_header (hdl, &hdl->write_buffer);
735 if (unlikely (ret != ret_ok)) return ret;
736
a18b345 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1708 5dc97367-97f1...
alobbs authored
737 HDL_CGI_BASE(hdl)->init_phase = hcgi_phase_connect;
738
739 case hcgi_phase_connect:
740 TRACE (ENTRIES, "Init: %s\n", "connect");
741
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
742 /* Connect
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
743 */
744 ret = connect_to_server (hdl);
16f53b0 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1658 5dc97367-97f1...
alobbs authored
745 switch (ret) {
746 case ret_ok:
747 break;
a18b345 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1708 5dc97367-97f1...
alobbs authored
748 case ret_eagain:
749 return ret_eagain;
16f53b0 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1658 5dc97367-97f1...
alobbs authored
750 case ret_deny:
751 conn->error_code = http_gateway_timeout;
752 return ret_error;
753 default:
2e0ea0e @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@430 5dc97367-97f1-...
alobbs authored
754 conn->error_code = http_service_unavailable;
16f53b0 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1658 5dc97367-97f1...
alobbs authored
755 return ret_error;
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
756 }
757
3509796 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@282 5dc97367-97f1-...
alobbs authored
758 HDL_CGI_BASE(hdl)->init_phase = hcgi_phase_send_headers;
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
759
760 case hcgi_phase_send_headers:
a18b345 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@1708 5dc97367-97f1...
alobbs authored
761 TRACE (ENTRIES, "Init: %s\n", "send_headers");
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
762
763 /* Send the header
764 */
765 ret = do_send (hdl, &hdl->write_buffer);
81679d0 handler_*.c, space / code style cleanups
ADF authored
766 if (ret != ret_ok)
767 return ret;
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
768
3509796 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@282 5dc97367-97f1-...
alobbs authored
769 HDL_CGI_BASE(hdl)->init_phase = hcgi_phase_send_post;
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
770
771 case hcgi_phase_send_post:
772 /* Send the Post
773 */
774 if (hdl->post_len > 0) {
775 return send_post (hdl, &hdl->write_buffer);
f11857f @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@600 5dc97367-97f1-...
alobbs authored
776 } else {
777 ret = send_no_post (hdl, &hdl->write_buffer);
81679d0 handler_*.c, space / code style cleanups
ADF authored
778 if (ret != ret_ok)
779 return ret;
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
780 }
781 break;
782 }
783
784 TRACE (ENTRIES, "Init %s\n", "finishes");
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
785
ae9d971 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@243 5dc97367-97f1-...
alobbs authored
786 cherokee_buffer_clean (&hdl->write_buffer);
787 return ret_ok;
788 }
789
Something went wrong with that request. Please try again.