typo
[strongswan.git] / src / libfast / session.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 * $Id$
16 */
17
18 #define _GNU_SOURCE
19
20 #include "session.h"
21
22 #include <string.h>
23 #include <fcgiapp.h>
24 #include <stdio.h>
25
26 #include <utils/linked_list.h>
27
28 typedef struct private_session_t private_session_t;
29
30 /**
31 * private data of the task manager
32 */
33 struct private_session_t {
34
35 /**
36 * public functions
37 */
38 session_t public;
39
40 /**
41 * session ID
42 */
43 char *sid;
44
45 /**
46 * list of controller instances controller_t
47 */
48 linked_list_t *controllers;
49
50 /**
51 * list of filter instances filter_t
52 */
53 linked_list_t *filters;
54
55 /**
56 * user defined session context
57 */
58 context_t *context;
59 };
60
61 /**
62 * Implementation of session_t.add_controller.
63 */
64 static void add_controller(private_session_t *this, controller_t *controller)
65 {
66 this->controllers->insert_last(this->controllers, controller);
67 }
68
69 /**
70 * Implementation of session_t.add_filter.
71 */
72 static void add_filter(private_session_t *this, filter_t *filter)
73 {
74 this->filters->insert_last(this->filters, filter);
75 }
76
77 /**
78 * Create a session ID and a cookie
79 */
80 static void create_sid(private_session_t *this, request_t *request)
81 {
82 char buf[16];
83 chunk_t chunk = chunk_from_buf(buf);
84 rng_t *rng;
85
86 rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
87 if (rng)
88 {
89 rng->get_bytes(rng, sizeof(buf), buf);
90 this->sid = chunk_to_hex(chunk, FALSE);
91 request->add_cookie(request, "SID", this->sid);
92 rng->destroy(rng);
93 }
94 }
95
96 /**
97 * run all registered filters
98 */
99 static bool run_filter(private_session_t *this, request_t *request,
100 controller_t *controller)
101 {
102 iterator_t *iterator;
103 filter_t *filter;
104
105 iterator = this->filters->create_iterator(this->filters, TRUE);
106 while (iterator->iterate(iterator, (void**)&filter))
107 {
108 if (!filter->run(filter, request, controller))
109 {
110 iterator->destroy(iterator);
111 return FALSE;
112 }
113 }
114 iterator->destroy(iterator);
115 return TRUE;
116 }
117
118 /**
119 * Implementation of session_t.process.
120 */
121 static void process(private_session_t *this, request_t *request)
122 {
123 char *pos, *start, *param[6] = {NULL, NULL, NULL, NULL, NULL, NULL};
124 iterator_t *iterator;
125 bool handled = FALSE;
126 controller_t *current;
127 int i = 0;
128
129 if (this->sid == NULL)
130 {
131 create_sid(this, request);
132 }
133
134 start = request->get_path(request);
135 if (start)
136 {
137 if (*start == '/') start++;
138 while ((pos = strchr(start, '/')) != NULL && i < 5)
139 {
140 param[i++] = strndup(start, pos - start);
141 start = pos + 1;
142 }
143 param[i] = strdup(start);
144 iterator = this->controllers->create_iterator(this->controllers, TRUE);
145 while (iterator->iterate(iterator, (void**)&current))
146 {
147 if (streq(current->get_name(current), param[0]))
148 {
149 if (run_filter(this, request, current))
150 {
151 current->handle(current, request, param[1], param[2],
152 param[3], param[4], param[5]);
153 handled = TRUE;
154 }
155 break;
156 }
157 }
158 iterator->destroy(iterator);
159 for (i = 0; i < 6; i++)
160 {
161 free(param[i]);
162 }
163 }
164 if (!handled)
165 {
166 if (this->controllers->get_first(this->controllers,
167 (void**)&current) == SUCCESS)
168 {
169 request->redirect(request, current->get_name(current));
170 }
171 }
172 }
173
174 /**
175 * Implementation of session_t.get_sid.
176 */
177 static char* get_sid(private_session_t *this)
178 {
179 return this->sid;
180 }
181
182 /**
183 * Implementation of session_t.destroy
184 */
185 static void destroy(private_session_t *this)
186 {
187 this->controllers->destroy_offset(this->controllers, offsetof(controller_t, destroy));
188 this->filters->destroy_offset(this->filters, offsetof(filter_t, destroy));
189 if (this->context) this->context->destroy(this->context);
190 free(this->sid);
191 free(this);
192 }
193
194 /*
195 * see header file
196 */
197 session_t *session_create(context_t *context)
198 {
199 private_session_t *this = malloc_thing(private_session_t);
200
201 this->public.add_controller = (void(*)(session_t*, controller_t*))add_controller;
202 this->public.add_filter = (void(*)(session_t*, filter_t*))add_filter;
203 this->public.process = (void(*)(session_t*,request_t*))process;
204 this->public.get_sid = (char*(*)(session_t*))get_sid;
205 this->public.destroy = (void(*)(session_t*))destroy;
206
207 this->sid = NULL;
208 this->controllers = linked_list_create();
209 this->filters = linked_list_create();
210 this->context = context;
211
212 return &this->public;
213 }
214