4 * @brief Implementation of session_t.
9 * Copyright (C) 2007 Martin Willi
10 * Hochschule fuer Technik Rapperswil
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
31 #include <utils/linked_list.h>
32 #include <utils/randomizer.h>
34 typedef struct private_session_t private_session_t
;
37 * private data of the task manager
39 struct private_session_t
{
52 * list of controller instances controller_t
54 linked_list_t
*controllers
;
57 * user defined session context
63 * Implementation of session_t.load_controller.
65 static void add_controller(private_session_t
*this, controller_t
*controller
)
67 this->controllers
->insert_last(this->controllers
, controller
);
71 * Create a session ID and a cookie
73 static void create_sid(private_session_t
*this, request_t
*request
)
76 chunk_t chunk
= chunk_from_buf(buf
);
77 randomizer_t
*randomizer
= randomizer_create();
79 randomizer
->get_pseudo_random_bytes(randomizer
, sizeof(buf
), buf
);
80 this->sid
= chunk_to_hex(chunk
, FALSE
);
81 request
->add_cookie(request
, "SID", this->sid
);
82 randomizer
->destroy(randomizer
);
86 * Implementation of session_t.process.
88 static void process(private_session_t
*this, request_t
*request
)
90 char *pos
, *start
, *param
[6] = {NULL
, NULL
, NULL
, NULL
, NULL
, NULL
};
93 controller_t
*current
;
96 if (this->sid
== NULL
)
98 create_sid(this, request
);
101 start
= request
->get_path(request
);
104 if (*start
== '/') start
++;
105 while ((pos
= strchr(start
, '/')) != NULL
&& i
< 5)
107 param
[i
++] = strndup(start
, pos
- start
);
110 param
[i
] = strdup(start
);
111 iterator
= this->controllers
->create_iterator(this->controllers
, TRUE
);
112 while (iterator
->iterate(iterator
, (void**)¤t
))
114 if (streq(current
->get_name(current
), param
[0]))
116 current
->handle(current
, request
, param
[1], param
[2], param
[3],
122 iterator
->destroy(iterator
);
123 for (i
= 0; i
< 6; i
++)
130 if (this->controllers
->get_first(this->controllers
,
131 (void**)¤t
) == SUCCESS
)
133 request
->redirect(request
, current
->get_name(current
));
139 * Implementation of session_t.get_sid.
141 static char* get_sid(private_session_t
*this)
147 * Implementation of session_t.destroy
149 static void destroy(private_session_t
*this)
151 this->controllers
->destroy_offset(this->controllers
, offsetof(controller_t
, destroy
));
152 if (this->context
) this->context
->destroy(this->context
);
160 session_t
*session_create(context_t
*context
)
162 private_session_t
*this = malloc_thing(private_session_t
);
164 this->public.add_controller
= (void(*)(session_t
*, controller_t
*))add_controller
;
165 this->public.process
= (void(*)(session_t
*,request_t
*))process
;
166 this->public.get_sid
= (char*(*)(session_t
*))get_sid
;
167 this->public.destroy
= (void(*)(session_t
*))destroy
;
170 this->controllers
= linked_list_create();
171 this->context
= context
;
173 return &this->public;