Merge branch 'android-app'
[strongswan.git] / src / libfast / request.c
1 /*
2 * Copyright (C) 2007 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #define _GNU_SOURCE
17
18 #include "request.h"
19
20 #include <library.h>
21 #include <debug.h>
22 #include <stdlib.h>
23 #include <pthread.h>
24 #include <string.h>
25 #include <ClearSilver/ClearSilver.h>
26
27 #include <threading/thread.h>
28 #include <threading/thread_value.h>
29
30 typedef struct private_request_t private_request_t;
31
32 /**
33 * private data of the task manager
34 */
35 struct private_request_t {
36
37 /**
38 * public functions
39 */
40 request_t public;
41
42 /**
43 * FastCGI request object
44 */
45 FCGX_Request req;
46
47 /**
48 * length of the req.envp array
49 */
50 int req_env_len;
51
52 /**
53 * ClearSilver CGI Kit context
54 */
55 CGI *cgi;
56
57 /**
58 * ClearSilver HDF dataset for this request
59 */
60 HDF *hdf;
61
62 /**
63 * close the session?
64 */
65 bool closed;
66
67 /**
68 * reference count
69 */
70 refcount_t ref;
71 };
72
73 /**
74 * ClearSilver cgiwrap is not threadsave, so we use a private
75 * context for each thread.
76 */
77 static thread_value_t *thread_this;
78
79 /**
80 * control variable for pthread_once
81 */
82 pthread_once_t once = PTHREAD_ONCE_INIT;
83
84 /**
85 * fcgiwrap read callback
86 */
87 static int read_cb(void *null, char *buf, int size)
88 {
89 private_request_t *this = (private_request_t*)thread_this->get(thread_this);
90
91 return FCGX_GetStr(buf, size, this->req.in);
92 }
93
94 /**
95 * fcgiwrap writef callback
96 */
97 static int writef_cb(void *null, const char *format, va_list args)
98 {
99 private_request_t *this = (private_request_t*)thread_this->get(thread_this);
100
101 FCGX_VFPrintF(this->req.out, format, args);
102 return 0;
103 }
104 /**
105 * fcgiwrap write callback
106 */
107 static int write_cb(void *null, const char *buf, int size)
108 {
109 private_request_t *this = (private_request_t*)thread_this->get(thread_this);
110
111 return FCGX_PutStr(buf, size, this->req.out);
112 }
113
114 /**
115 * fcgiwrap getenv callback
116 */
117 static char *getenv_cb(void *null, const char *key)
118 {
119 char *value;
120 private_request_t *this = (private_request_t*)thread_this->get(thread_this);
121
122 value = FCGX_GetParam(key, this->req.envp);
123 return strdupnull(value);
124 }
125
126 /**
127 * fcgiwrap getenv callback
128 */
129 static int putenv_cb(void *null, const char *key, const char *value)
130 {
131 /* not supported */
132 return 1;
133 }
134
135 /**
136 * fcgiwrap iterenv callback
137 */
138 static int iterenv_cb(void *null, int num, char **key, char **value)
139 {
140 *key = NULL;
141 *value = NULL;
142 private_request_t *this = (private_request_t*)thread_this->get(thread_this);
143 if (num < this->req_env_len)
144 {
145 char *eq;
146
147 eq = strchr(this->req.envp[num], '=');
148 if (eq)
149 {
150 *key = strndup(this->req.envp[num], eq - this->req.envp[num]);
151 *value = strdup(eq + 1);
152 }
153 if (*key == NULL || *value == NULL)
154 {
155 free(*key);
156 free(*value);
157 return 1;
158 }
159 }
160 return 0;
161 }
162
163 METHOD(request_t, get_cookie, char*,
164 private_request_t *this, char *name)
165 {
166 return hdf_get_valuef(this->hdf, "Cookie.%s", name);
167 }
168
169 METHOD(request_t, get_path, char*,
170 private_request_t *this)
171 {
172 char * path = FCGX_GetParam("PATH_INFO", this->req.envp);
173 return path ? path : "";
174 }
175
176 METHOD(request_t, get_host, char*,
177 private_request_t *this)
178 {
179 char *addr = FCGX_GetParam("REMOTE_ADDR", this->req.envp);
180 return addr ? addr : "";
181 }
182
183 METHOD(request_t, get_user_agent, char*,
184 private_request_t *this)
185 {
186 char *agent = FCGX_GetParam("HTTP_USER_AGENT", this->req.envp);
187 return agent ? agent : "";
188 }
189
190 METHOD(request_t, get_query_data, char*,
191 private_request_t *this, char *name)
192 {
193 return hdf_get_valuef(this->hdf, "Query.%s", name);
194 }
195
196 METHOD(request_t, get_env_var, char*,
197 private_request_t *this, char *name)
198 {
199 return FCGX_GetParam(name, this->req.envp);
200 }
201
202 METHOD(request_t, read_data, int,
203 private_request_t *this, char *buf, int len)
204 {
205 return FCGX_GetStr(buf, len, this->req.in);
206 }
207
208 METHOD(request_t, get_base, char*,
209 private_request_t *this)
210 {
211 return FCGX_GetParam("SCRIPT_NAME", this->req.envp);
212 }
213
214 METHOD(request_t, add_cookie, void,
215 private_request_t *this, char *name, char *value)
216 {
217 thread_this->set(thread_this, this);
218 cgi_cookie_set(this->cgi, name, value, NULL, NULL, NULL, 0, 0);
219 }
220
221 METHOD(request_t, redirect, void,
222 private_request_t *this, char *fmt, ...)
223 {
224 va_list args;
225
226 FCGX_FPrintF(this->req.out, "Status: 303 See Other\n");
227 FCGX_FPrintF(this->req.out, "Location: %s%s", get_base(this),
228 *fmt == '/' ? "" : "/");
229 va_start(args, fmt);
230 FCGX_VFPrintF(this->req.out, fmt, args);
231 va_end(args);
232 FCGX_FPrintF(this->req.out, "\n\n");
233 }
234
235 METHOD(request_t, get_referer, char*,
236 private_request_t *this)
237 {
238 return FCGX_GetParam("HTTP_REFERER", this->req.envp);
239 }
240
241 METHOD(request_t, to_referer, void,
242 private_request_t *this)
243 {
244 char *referer;
245
246 referer = get_referer(this);
247 if (referer)
248 {
249 FCGX_FPrintF(this->req.out, "Status: 303 See Other\n");
250 FCGX_FPrintF(this->req.out, "Location: %s\n\n", referer);
251 }
252 else
253 {
254 redirect(this, "/");
255 }
256 }
257
258 METHOD(request_t, session_closed, bool,
259 private_request_t *this)
260 {
261 return this->closed;
262 }
263
264 METHOD(request_t, close_session, void,
265 private_request_t *this)
266 {
267 this->closed = TRUE;
268 }
269
270 METHOD(request_t, serve, void,
271 private_request_t *this, char *headers, chunk_t chunk)
272 {
273 FCGX_FPrintF(this->req.out, "%s\n\n", headers);
274
275 FCGX_PutStr(chunk.ptr, chunk.len, this->req.out);
276 }
277
278 METHOD(request_t, render, void,
279 private_request_t *this, char *template)
280 {
281 NEOERR* err;
282
283 thread_this->set(thread_this, this);
284 err = cgi_display(this->cgi, template);
285 if (err)
286 {
287 cgi_neo_error(this->cgi, err);
288 nerr_log_error(err);
289 }
290 }
291
292 METHOD(request_t, streamf, int,
293 private_request_t *this, char *format, ...)
294 {
295 va_list args;
296 int written;
297
298 va_start(args, format);
299 written = FCGX_VFPrintF(this->req.out, format, args);
300 va_end(args);
301 if (written >= 0 &&
302 FCGX_FFlush(this->req.out) == -1)
303 {
304 return -1;
305 }
306 return written;
307 }
308
309 METHOD(request_t, set, void,
310 private_request_t *this, char *key, char *value)
311 {
312 hdf_set_value(this->hdf, key, value);
313 }
314
315 METHOD(request_t, setf, void,
316 private_request_t *this, char *format, ...)
317 {
318 va_list args;
319
320 va_start(args, format);
321 hdf_set_valuevf(this->hdf, format, args);
322 va_end(args);
323 }
324
325 METHOD(request_t, get_ref, request_t*,
326 private_request_t *this)
327 {
328 ref_get(&this->ref);
329 return &this->public;
330 }
331
332 METHOD(request_t, destroy, void,
333 private_request_t *this)
334 {
335 if (ref_put(&this->ref))
336 {
337 thread_this->set(thread_this, this);
338 cgi_destroy(&this->cgi);
339 FCGX_Finish_r(&this->req);
340 free(this);
341 }
342 }
343
344 /**
345 * This initialization method is guaranteed to run only once
346 * for all threads.
347 */
348 static void init(void)
349 {
350 cgiwrap_init_emu(NULL, read_cb, writef_cb, write_cb,
351 getenv_cb, putenv_cb, iterenv_cb);
352 thread_this = thread_value_create(NULL);
353 }
354
355 /*
356 * see header file
357 */
358 request_t *request_create(int fd, bool debug)
359 {
360 NEOERR* err;
361 private_request_t *this;
362 bool failed = FALSE;
363
364 INIT(this,
365 .public = {
366 .get_path = _get_path,
367 .get_base = _get_base,
368 .get_host = _get_host,
369 .get_user_agent = _get_user_agent,
370 .add_cookie = _add_cookie,
371 .get_cookie = _get_cookie,
372 .get_query_data = _get_query_data,
373 .get_env_var = _get_env_var,
374 .read_data = _read_data,
375 .session_closed = _session_closed,
376 .close_session = _close_session,
377 .redirect = _redirect,
378 .get_referer = _get_referer,
379 .to_referer = _to_referer,
380 .render = _render,
381 .streamf = _streamf,
382 .serve = _serve,
383 .set = _set,
384 .setf = _setf,
385 .get_ref = _get_ref,
386 .destroy = _destroy,
387 },
388 .ref = 1,
389 );
390
391 thread_cleanup_push(free, this);
392 if (FCGX_InitRequest(&this->req, fd, 0) != 0 ||
393 FCGX_Accept_r(&this->req) != 0)
394 {
395 failed = TRUE;
396 }
397 thread_cleanup_pop(failed);
398 if (failed)
399 {
400 return NULL;
401 }
402
403 pthread_once(&once, init);
404 thread_this->set(thread_this, this);
405
406 while (this->req.envp[this->req_env_len] != NULL)
407 {
408 this->req_env_len++;
409 }
410
411 err = hdf_init(&this->hdf);
412 if (!err)
413 {
414 hdf_set_value(this->hdf, "base", get_base(this));
415 hdf_set_value(this->hdf, "Config.NoCache", "true");
416 if (!debug)
417 {
418 hdf_set_value(this->hdf, "Config.TimeFooter", "0");
419 hdf_set_value(this->hdf, "Config.CompressionEnabled", "1");
420 hdf_set_value(this->hdf, "Config.WhiteSpaceStrip", "2");
421 }
422
423 err = cgi_init(&this->cgi, this->hdf);
424 if (!err)
425 {
426 err = cgi_parse(this->cgi);
427 if (!err)
428 {
429 return &this->public;
430 }
431 cgi_destroy(&this->cgi);
432 }
433 }
434 nerr_log_error(err);
435 FCGX_Finish_r(&this->req);
436 free(this);
437 return NULL;
438 }
439