Show contents of the CP payload in message_t stringification
[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 value ? strdup(value) : NULL;
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 /**
164 * Implementation of request_t.get_cookie.
165 */
166 static char* get_cookie(private_request_t *this, char *name)
167 {
168 return hdf_get_valuef(this->hdf, "Cookie.%s", name);
169 }
170
171 /**
172 * Implementation of request_t.get_path.
173 */
174 static char* get_path(private_request_t *this)
175 {
176 char * path = FCGX_GetParam("PATH_INFO", this->req.envp);
177 return path ? path : "";
178 }
179
180 /**
181 * Implementation of request_t.get_host.
182 */
183 static char* get_host(private_request_t *this)
184 {
185 char *addr = FCGX_GetParam("REMOTE_ADDR", this->req.envp);
186 return addr ? addr : "";
187 }
188
189 /**
190 * Implementation of request_t.get_user_agent.
191 */
192 static char* get_user_agent(private_request_t *this)
193 {
194 char *agent = FCGX_GetParam("HTTP_USER_AGENT", this->req.envp);
195 return agent ? agent : "";
196 }
197
198 /**
199 * Implementation of request_t.get_post_data.
200 */
201 static char* get_query_data(private_request_t *this, char *name)
202 {
203 return hdf_get_valuef(this->hdf, "Query.%s", name);
204 }
205
206 /**
207 * Implementation of request_t.read_data.
208 */
209 static int read_data(private_request_t *this, char *buf, int len)
210 {
211 return FCGX_GetStr(buf, len, this->req.in);
212 }
213
214 /**
215 * Implementation of request_t.get_base.
216 */
217 static char* get_base(private_request_t *this)
218 {
219 return FCGX_GetParam("SCRIPT_NAME", this->req.envp);
220 }
221
222 /**
223 * Implementation of request_t.add_cookie.
224 */
225 static void add_cookie(private_request_t *this, char *name, char *value)
226 {
227 thread_this->set(thread_this, this);
228 cgi_cookie_set (this->cgi, name, value, get_base(this), NULL, NULL, 0, 0);
229 }
230
231 /**
232 * Implementation of request_t.redirect.
233 */
234 static void redirect(private_request_t *this, char *fmt, ...)
235 {
236 va_list args;
237
238 FCGX_FPrintF(this->req.out, "Status: 303 See Other\n");
239 FCGX_FPrintF(this->req.out, "Location: %s%s", get_base(this),
240 *fmt == '/' ? "" : "/");
241 va_start(args, fmt);
242 FCGX_VFPrintF(this->req.out, fmt, args);
243 va_end(args);
244 FCGX_FPrintF(this->req.out, "\n\n");
245 }
246
247 /**
248 * Implementation of request_t.get_referer.
249 */
250 static char* get_referer(private_request_t *this)
251 {
252 return FCGX_GetParam("HTTP_REFERER", this->req.envp);
253 }
254
255 /**
256 * Implementation of request_t.to_referer.
257 */
258 static void to_referer(private_request_t *this)
259 {
260 char *referer;
261
262 referer = get_referer(this);
263 if (referer)
264 {
265 FCGX_FPrintF(this->req.out, "Status: 303 See Other\n");
266 FCGX_FPrintF(this->req.out, "Location: %s\n\n", referer);
267 }
268 else
269 {
270 redirect(this, "/");
271 }
272 }
273
274 /**
275 * Implementation of request_t.session_closed.
276 */
277 static bool session_closed(private_request_t *this)
278 {
279 return this->closed;
280 }
281
282 /**
283 * Implementation of request_t.close_session.
284 */
285 static void close_session(private_request_t *this)
286 {
287 this->closed = TRUE;
288 }
289
290 /**
291 * Implementation of request_t.serve.
292 */
293 static void serve(private_request_t *this, char *headers, chunk_t chunk)
294 {
295 FCGX_FPrintF(this->req.out, "%s\n\n", headers);
296
297 FCGX_PutStr(chunk.ptr, chunk.len, this->req.out);
298 }
299
300 /**
301 * Implementation of request_t.render.
302 */
303 static void render(private_request_t *this, char *template)
304 {
305 NEOERR* err;
306
307 thread_this->set(thread_this, this);
308 err = cgi_display(this->cgi, template);
309 if (err)
310 {
311 cgi_neo_error(this->cgi, err);
312 nerr_log_error(err);
313 }
314 return;
315 }
316
317 /**
318 * Implementation of request_t.streamf.
319 */
320 static int streamf(private_request_t *this, char *format, ...)
321 {
322 va_list args;
323 int written;
324
325 va_start(args, format);
326 written = FCGX_VFPrintF(this->req.out, format, args);
327 va_end(args);
328 if (written >= 0 &&
329 FCGX_FFlush(this->req.out) == -1)
330 {
331 return -1;
332 }
333 return written;
334 }
335
336 /**
337 * Implementation of request_t.set.
338 */
339 static void set(private_request_t *this, char *key, char *value)
340 {
341 hdf_set_value(this->hdf, key, value);
342 }
343
344 /**
345 * Implementation of request_t.setf.
346 */
347 static void setf(private_request_t *this, char *format, ...)
348 {
349 va_list args;
350
351 va_start(args, format);
352 hdf_set_valuevf(this->hdf, format, args);
353 va_end(args);
354 }
355
356 /**
357 * Implementation of request_t.get_ref.
358 */
359 static request_t* get_ref(private_request_t *this)
360 {
361 ref_get(&this->ref);
362 return &this->public;
363 }
364
365 /**
366 * Implementation of request_t.destroy
367 */
368 static void destroy(private_request_t *this)
369 {
370 if (ref_put(&this->ref))
371 {
372 thread_this->set(thread_this, this);
373 cgi_destroy(&this->cgi);
374 FCGX_Finish_r(&this->req);
375 free(this);
376 }
377 }
378
379 /**
380 * This initialization method is guaranteed to run only once
381 * for all threads.
382 */
383 static void init(void)
384 {
385 cgiwrap_init_emu(NULL, read_cb, writef_cb, write_cb,
386 getenv_cb, putenv_cb, iterenv_cb);
387 thread_this = thread_value_create(NULL);
388 }
389
390 /*
391 * see header file
392 */
393 request_t *request_create(int fd, bool debug)
394 {
395 NEOERR* err;
396 private_request_t *this = malloc_thing(private_request_t);
397 bool failed = FALSE;
398
399 thread_cleanup_push(free, this);
400 if (FCGX_InitRequest(&this->req, fd, 0) != 0 ||
401 FCGX_Accept_r(&this->req) != 0)
402 {
403 failed = TRUE;
404 }
405 thread_cleanup_pop(failed);
406 if (failed)
407 {
408 return NULL;
409 }
410
411 this->public.get_path = (char*(*)(request_t*))get_path;
412 this->public.get_base = (char*(*)(request_t*))get_base;
413 this->public.get_host = (char*(*)(request_t*))get_host;
414 this->public.get_user_agent = (char*(*)(request_t*))get_user_agent;
415 this->public.add_cookie = (void(*)(request_t*, char *name, char *value))add_cookie;
416 this->public.get_cookie = (char*(*)(request_t*,char*))get_cookie;
417 this->public.get_query_data = (char*(*)(request_t*, char *name))get_query_data;
418 this->public.read_data = (int(*)(request_t*, char*, int))read_data;
419 this->public.session_closed = (bool(*)(request_t*))session_closed;
420 this->public.close_session = (void(*)(request_t*))close_session;
421 this->public.redirect = (void(*)(request_t*, char *fmt,...))redirect;
422 this->public.get_referer = (char*(*)(request_t*))get_referer;
423 this->public.to_referer = (void(*)(request_t*))to_referer;
424 this->public.render = (void(*)(request_t*,char*))render;
425 this->public.streamf = (int(*)(request_t*, char *format, ...))streamf;
426 this->public.serve = (void(*)(request_t*,char*,chunk_t))serve;
427 this->public.set = (void(*)(request_t*, char *, char*))set;
428 this->public.setf = (void(*)(request_t*, char *format, ...))setf;
429 this->public.get_ref = (request_t*(*)(request_t*))get_ref;
430 this->public.destroy = (void(*)(request_t*))destroy;
431
432 pthread_once(&once, init);
433 thread_this->set(thread_this, this);
434
435 this->ref = 1;
436 this->closed = FALSE;
437 this->req_env_len = 0;
438 while (this->req.envp[this->req_env_len] != NULL)
439 {
440 this->req_env_len++;
441 }
442
443 err = hdf_init(&this->hdf);
444 if (!err)
445 {
446 hdf_set_value(this->hdf, "base", get_base(this));
447 hdf_set_value(this->hdf, "Config.NoCache", "true");
448 if (!debug)
449 {
450 hdf_set_value(this->hdf, "Config.TimeFooter", "0");
451 hdf_set_value(this->hdf, "Config.CompressionEnabled", "1");
452 hdf_set_value(this->hdf, "Config.WhiteSpaceStrip", "2");
453 }
454
455 err = cgi_init(&this->cgi, this->hdf);
456 if (!err)
457 {
458 err = cgi_parse(this->cgi);
459 if (!err)
460 {
461 return &this->public;
462 }
463 cgi_destroy(&this->cgi);
464 }
465 }
466 nerr_log_error(err);
467 FCGX_Finish_r(&this->req);
468 free(this);
469 return NULL;
470 }
471