Menu

[r591]: / framework / branches / sessions / session_sid.cpp  Maximize  Restore  History

Download this file

146 lines (126 with data), 2.8 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
#include "session_sid.h"
#include "md5.h"
#include "session_storage.h"
#include "session_interface.h"
#include <fstream>
#include "cppcms_error.h"
#include "archive.h"
#include "worker_thread.h"
using namespace std;
namespace cppcms {
namespace details {
sid_generator::sid_generator()
{
hashed.session_counter=0;
ifstream urandom("/dev/urandom");
if(!urandom.good() || urandom.get(hashed.uid,16).fail()) {
throw cppcms_error("Failed to read /dev/urandom");
}
}
std::string sid_generator::operator()()
{
hashed.session_counter++;
gettimeofday(&hashed.tv,NULL);
md5_byte_t md5[16];
char res[33];
md5_state_t st;
md5_init(&st);
md5_append(&st,(md5_byte_t*)&hashed,sizeof(hashed));
md5_finish(&st,md5);
for(int i=0;i<16;i++) {
snprintf(res+i*2,3,"%02x",md5[i]);
}
return std::string(res);
}
} // namespace details
namespace {
struct cached_data : public serializable {
time_t timeout;
std::string data;
virtual void load(archive &a)
{
a>>timeout>>data;
}
virtual void save(archive &a) const
{
a<<timeout<<data;
}
};
}
bool session_sid::valid_sid(std::string const &id)
{
if(id.size()!=32)
return false;
for(int i=0;i<32;i++) {
char c=id[i];
bool is_low_x_digit=('0'<=c && c<='9') || ('a'<=c && c<='f');
if(!is_low_x_digit)
return false;
}
return true;
}
string session_sid::key(std::string sid)
{
return "cppcms_session_"+sid;
}
void session_sid::save(session_interface *session,std::string const &data,time_t timeout)
{
string id=session->get_session_cookie();
if(!valid_sid(id)) {
id=sid(); // if id not valid create new one
}
else if(cache){
session->get_worker().cache.rise(key(id));
}
storage->save(id,timeout,data);
session->set_session_cookie(id); // Renew cookie or set new one
if(cache) {
cached_data cdata;
cdata.timeout=timeout;
cdata.data=data;
session->get_worker().cache.store_data(
key(id),
cdata,
set<string>(),
timeout - time(NULL));
}
}
bool session_sid::load(session_interface *session,std::string &data,time_t &timeout)
{
string id=session->get_session_cookie();
if(!valid_sid(id))
return false;
if(cache){
cached_data cdata;
if(session->get_worker().cache.fetch_data(key(id),cdata,false)) {
data.swap(cdata.data);
timeout=cdata.timeout;
return true;
}
}
if(!storage->load(id,&timeout,data)) {
return false;
}
if(!cache)
return true;
cached_data cdata;
cdata.timeout=timeout;
cdata.data=data;
session->get_worker().cache.store_data(
key(id),
cdata,
set<string>(),
timeout-time(NULL)
);
return true;
}
void session_sid::clear(session_interface *session)
{
string id=session->get_session_cookie();
if(valid_sid(id)) {
storage->remove(id);
if(cache)
session->get_worker().cache.rise(key(id));
}
}
} // namespace cppcms
MongoDB Logo MongoDB