1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
/**
* @file session.c
*
* @brief Implementation of session_t.
*
*/
/*
* Copyright (C) 2007 Martin Willi
* Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#define _GNU_SOURCE
#include "session.h"
#include <string.h>
#include <fcgiapp.h>
#include <stdio.h>
#include <utils/linked_list.h>
#include <utils/randomizer.h>
typedef struct private_session_t private_session_t;
/**
* private data of the task manager
*/
struct private_session_t {
/**
* public functions
*/
session_t public;
/**
* session ID
*/
char *sid;
/**
* list of controller instances controller_t
*/
linked_list_t *controllers;
/**
* user defined session context
*/
context_t *context;
};
/**
* Implementation of session_t.load_controller.
*/
static void add_controller(private_session_t *this, controller_t *controller)
{
this->controllers->insert_last(this->controllers, controller);
}
/**
* Create a session ID and a cookie
*/
static void create_sid(private_session_t *this, response_t *response)
{
char buf[16];
chunk_t chunk = chunk_from_buf(buf);
randomizer_t *randomizer = randomizer_create();
randomizer->get_pseudo_random_bytes(randomizer, sizeof(buf), buf);
asprintf(&this->sid, "%#B", &chunk);
response->add_cookie(response, "SID", this->sid);
randomizer->destroy(randomizer);
}
/**
* Implementation of session_t.process.
*/
static void process(private_session_t *this,
request_t *request, response_t *response)
{
char *pos, *path, *controller, *action;
iterator_t *iterator;
bool handled = FALSE;
controller_t *current;
if (this->sid == NULL)
{
create_sid(this, response);
}
path = request->get_path(request);
if (*path == '/') path++;
pos = strchr(path, '/');
if (pos == NULL)
{
controller = strdup(path);
action = strdup("");
}
else
{
controller = strndup(path, pos - path);
path = pos + 1;
pos = strchr(path, '/');
if (pos == NULL)
{
action = strdup(path);
}
else
{
action = strndup(path, pos - path);
}
}
iterator = this->controllers->create_iterator(this->controllers, TRUE);
while (iterator->iterate(iterator, (void**)¤t))
{
if (streq(current->get_name(current), controller))
{
current->handle(current, request, response, action, NULL, NULL, NULL, NULL);
handled = TRUE;
break;
}
}
iterator->destroy(iterator);
free(controller);
free(action);
if (!handled)
{
if (this->controllers->get_first(this->controllers,
(void**)¤t) == SUCCESS)
{
response->redirect(response, current->get_name(current));
}
else
{
response->printf(response, "No controllers loaded!\n");
}
}
}
/**
* Implementation of session_t.get_sid.
*/
static char* get_sid(private_session_t *this)
{
return this->sid;
}
/**
* Implementation of session_t.destroy
*/
static void destroy(private_session_t *this)
{
this->controllers->destroy_offset(this->controllers, offsetof(controller_t, destroy));
if (this->context) this->context->destroy(this->context);
free(this->sid);
free(this);
}
/*
* see header file
*/
session_t *session_create(context_t *context)
{
private_session_t *this = malloc_thing(private_session_t);
this->public.add_controller = (void(*)(session_t*, controller_t*))add_controller;
this->public.process = (void(*)(session_t*, request_t*,response_t*))process;
this->public.get_sid = (char*(*)(session_t*))get_sid;
this->public.destroy = (void(*)(session_t*))destroy;
this->sid = NULL;
this->controllers = linked_list_create();
this->context = context;
return &this->public;
}
|