-
Notifications
You must be signed in to change notification settings - Fork 17
/
http_state.js
49 lines (44 loc) · 1.29 KB
/
http_state.js
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
var http = require('http');
var util = require('./util');
var idgen = require('./idgen');
(function() {
var sessions = {};
process.mixin(http.IncomingMessage.prototype, {
get_cookie: function(name) {
var cookies = this.headers.cookie && this.headers.cookie.split(";");
while(cookie = (cookies && cookies.shift())) {
var parts = cookie.split("=");
if(parts[0].trim() === name) return parts[1];
}
},
get_or_create_session: function(req, res, options) {
var session_id = req.get_cookie("session_id");
if(!session_id) {
session_id = idgen.generate_id(22);
res.set_cookie("session_id", session_id);
}
sessions[session_id] = (sessions[session_id] || {
session: {session_id: session_id},
touch: function() {
this.expiration = (+ new Date) + (options.duration || 30*60*1000);
return this;
}
}).touch();
return sessions[session_id].session;
}
});
process.mixin(http.ServerResponse.prototype, {
set_cookie: function(name, value) {
this.cookies = this.cookies || [];
this.cookies.push(name + "=" + value + "; path=/;");
}
});
function cleanup_sessions() {
for(session_id in sessions) {
if((+ new Date) > sessions[session_id].expiration) {
delete sessions[session_id];
}
}
}
setInterval(cleanup_sessions, 1000);
})();