Menu

[r952]: / framework / branches / refactoring / session_pool.cpp  Maximize  Restore  History

Download this file

257 lines (231 with data), 6.1 kB

  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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#define CPPCMS_SOURCE
#include "session_pool.h"
#include "service.h"
#include "thread_pool.h"
#include "aio_timer.h"
#include "session_cookies.h"
#include "session_sid.h"
#include "session_dual.h"
#include "hmac_encryptor.h"
#include "json.h"
#include "cppcms_error.h"
#include "config.h"
#ifdef CPPCMS_USE_EXTERNAL_BOOST
# include <boost/bind.hpp>
# include <boost/shared_ptr.hpp>
# include <boost/enable_shared_from_this.hpp>
#else // Internal Boost
# include <cppcms_boost/bind.hpp>
# include <cppcms_boost/shared_ptr.hpp>
# include <cppcms_boost/enable_shared_from_this.hpp>
namespace boost = cppcms_boost;
#endif
#ifndef HAVE_GCRYPT
#include "aes_encryptor.h"
#endif
#ifdef CPPCMS_WIN_NATIVE
#include "session_win32_file_storage.h"
#else
#include "session_posix_file_storage.h"
#endif
namespace cppcms {
struct session_pool::data
{
};
using namespace cppcms::sessions;
template<typename Encryptor>
struct session_pool::enc_factory : public encryptor_factory {
enc_factory(std::string const &key) : key_(key) {}
virtual std::auto_ptr<cppcms::sessions::encryptor> get()
{
std::auto_ptr<cppcms::sessions::encryptor> tmp(new Encryptor(key_));
return tmp;
}
private:
std::string key_;
};
struct session_pool::sid_factory : public session_api_factory
{
sid_factory(session_pool *pool) : pool_(pool) {}
bool requires_gc() {
if(pool_->storage_.get())
return pool_->storage_->requires_gc();
else
return false;
}
void gc()
{
if(pool_->storage_.get())
pool_->storage_->gc_job();
}
intrusive_ptr<session_api> get() {
if(pool_->storage_.get())
return new session_sid(pool_->storage_->get());
else
return 0;
}
private:
session_pool *pool_;
};
struct session_pool::cookies_factory : public session_api_factory
{
cookies_factory(session_pool *pool) : pool_(pool) {}
bool requires_gc() {
return false;
}
void gc() {}
intrusive_ptr<session_api> get() {
if(pool_->encryptor_.get())
return new session_cookies(pool_->encryptor_->get());
else
return 0;
}
private:
session_pool *pool_;
};
struct session_pool::dual_factory : public session_api_factory
{
dual_factory(unsigned limit,session_pool *pool) : limit_(limit), pool_(pool) {}
bool requires_gc() {
if(pool_->storage_.get())
return pool_->storage_->requires_gc();
else
return false;
}
void gc()
{
if(pool_->storage_.get())
pool_->storage_->gc_job();
}
intrusive_ptr<session_api> get() {
if(pool_->storage_.get() && pool_->encryptor_.get())
return new session_dual(pool_->encryptor_->get(),pool_->storage_->get(),limit_);
else
return 0;
}
private:
unsigned limit_;
session_pool *pool_;
};
class session_pool::gc_job : public boost::enable_shared_from_this<gc_job> {
public:
gc_job(service *ser,int freq,session_pool *pool) :
timer_(new aio::timer(*ser)),
service_(ser),
freq_(freq),
pool_(pool)
{
}
void async_run() const
{
service_->thread_pool().post(boost::bind(&gc_job::gc,shared_from_this()));
}
private:
void gc() const
{
pool_->backend_->gc();
timer_->expires_from_now(freq_);
timer_->async_wait(boost::bind(&gc_job::async_run,shared_from_this()));
}
boost::shared_ptr<aio::timer> timer_;
service *service_;
int freq_;
session_pool *pool_;
};
session_pool::session_pool(service &srv) :
service_(&srv)
{
std::string location=srv.settings().get("session.location","none");
if(location == "client" || location=="both") {
std::string enc=srv.settings().get("session.cookies_encryptor","");
std::auto_ptr<cppcms::sessions::encryptor_factory> factory;
if(enc=="hmac") {
std::string key = srv.settings().get<std::string>("session.cookies_key");
factory.reset(new enc_factory<cppcms::sessions::impl::hmac_cipher>(key));
}
#ifdef HAVE_GCRYPT
else if(enc=="aes") {
std::string key = srv.settings().get<std::string>("session.cookies_key");
factory.reset(new enc_factory<cppcms::sessions::impl::aes_cipher>(key));
}
#endif
else if(enc.empty())
; // Nothing to do
else
throw cppcms_error("Unknown encryptor: "+enc);
encryptor(factory);
}
if(location == "server" || location == "both" ) {
std::string storage=srv.settings().get("session.backend","");
if(storage == "files") {
std::auto_ptr<sessions::session_storage_factory> tmp;
std::string dir = srv.settings().get("session.files.dir","");
#ifdef CPPCMS_WIN_NATIVE
tmp.reset(new session_file_storage_factory(dir));
#else
bool sharing = srv.settings().get("session.files.shared",true);
int threads = srv.threads_no();
int procs = srv.procs_no();
if(procs == 0) procs=1;
tmp.reset(new session_file_storage_factory(dir,threads*procs,procs,sharing));
#endif
}
else if(storage.empty())
; /// Nothing to do
else
throw cppcms_error("Unknown server side storage:"+storage);
}
if(location == "server") {
std::auto_ptr<session_api_factory> f(new sid_factory(this));
backend(f);
}
else if(location == "client") {
std::auto_ptr<session_api_factory> f(new cookies_factory(this));
backend(f);
}
else if(location == "both") {
unsigned limit=srv.settings().get("session.client_size_limit",2048);
std::auto_ptr<session_api_factory> f(new dual_factory(limit,this));
backend(f);
}
else if(location == "none")
;
else
throw cppcms_error("Unknown location");
service_->after_fork(boost::bind(&session_pool::after_fork,this));
}
void session_pool::after_fork()
{
if(backend_.get() && backend_->requires_gc()) {
if(service_->process_id()!=1)
return;
int frequency = service_->settings().get("sessions.gc",0);
if(frequency > 0) {
boost::shared_ptr<gc_job> job(new gc_job(service_,frequency,this));
job->async_run();
}
}
}
session_pool::~session_pool()
{
}
intrusive_ptr<session_api> session_pool::get()
{
if(backend_.get())
return backend_->get();
else
return 0;
}
void session_pool::backend(std::auto_ptr<session_api_factory> b)
{
backend_=b;
}
void session_pool::encryptor(std::auto_ptr<sessions::encryptor_factory> e)
{
encryptor_=e;
}
void session_pool::storage(std::auto_ptr<sessions::session_storage_factory> s)
{
storage_=s;
}
} /// cppcms
MongoDB Logo MongoDB