Permalink
Browse files

Add the dynamic server.

  • Loading branch information...
1 parent 3e32d79 commit d072fb7bc4c048dae28c40a616c223be79cf4c2f @JacksonTian committed Nov 17, 2011
Showing with 170 additions and 3 deletions.
  1. +2 −2 app.js
  2. +2 −1 config.js
  3. +29 −0 cookie.js
  4. +88 −0 server.js
  5. +49 −0 session.js
View
4 app.js
@@ -1,4 +1,4 @@
-var PORT = 8000;
+var PORT = process.argv[2] || 8000;
var http = require("http");
var url = require("url");
var fs = require("fs");
@@ -91,4 +91,4 @@ var server = http.createServer(function(request, response) {
});
server.listen(PORT);
-console.log("Server runing at port: " + PORT + ".");
+console.log("Server running at port: " + PORT + ".");
View
@@ -7,4 +7,5 @@ exports.Compress = {
};
exports.Welcome = {
file: "index.html"
-};
+};
+exports.Timeout = 20 * 60 * 1000;
View
@@ -0,0 +1,29 @@
+exports.parse = function (cookies) {
+ var map = {};
+ var pairs = cookies.split(";");
+ pairs.forEach(function (pair) {
+ var kv = pair.split("=");
+ map[kv[0]] = kv[1] || "";
+ });
+ return map;
+};
+exports.stringify = function (cookie) {
+ var buffer = [cookie.key, "=", cookie.value];
+ if (cookie.expires) {
+ buffer.push("; expires=", (new Date(cookie.expires)).toUTCString());
+ }
+ if (cookie.path) {
+ buffer.push("; path=", cookie.path);
+ }
+ if (cookie.domain) {
+ buffer.push("; domain=", cookie.domain);
+ }
+ if (cookie.secure) {
+ buffer.push("; secure");
+ }
+ if (cookie.httpOnly) {
+ buffer.push("; httponly");
+ }
+
+ return buffer.join("");
+};
View
@@ -0,0 +1,88 @@
+var http = require("http");
+var url = require("url");
+var qs = require("querystring");
+var cookie = require("./cookie");
+var session = require("./session");
+var config = require("./config");
+
+var sessionManager = new session.SessionManager(config.Timeout);
+
+var server = http.createServer(function (request, response) {
+
+ if (request.url == "/favicon.ico") {
+ response.writeHead(404, "Not Found");
+ response.end();
+ return;
+ }
+ var handle = function (session) {
+ response.setHeader("Content-Type", "text/plain");
+ response.writeHead(200, "Ok");
+ if (!session.get("username")) {
+ session.set("username", request.get("username"));
+ }
+ response.write("Hi, " + session.get("username") + "\n\r");
+ response.end();
+ };
+
+ var _cookieMap;
+ request.cookie = function (key) {
+ if (!_cookieMap) {
+ _cookieMap = cookie.parse(request.headers.cookie || "");
+ }
+ return _cookieMap[key];
+ };
+ var _setCookieMap = {};
+ response.setCookie = function (cookieObj) {
+ _setCookieMap[cookieObj.key] = cookie.stringify(cookieObj);
+ var returnVal = [];
+ for(var key in _setCookieMap) {
+ returnVal.push(_setCookieMap[key]);
+ }
+
+ response.setHeader("Set-Cookie", returnVal.join(", "));
+ };
+
+ var sessionId = request.cookie(session.SESSIONID_KEY);
+ console.log(_cookieMap);
+ var curSession;
+ if (sessionId && (curSession = sessionManager.get(sessionId))) {
+ if (sessionManager.isTimeout(curSession)) {
+ sessionManager.remove(sessionId);
+ curSession = sessionManager.renew(response);
+ } else {
+ curSession.updateTime();
+ }
+ } else {
+ curSession = sessionManager.renew(response);
+ }
+
+ var _urlMap;
+ request.get = function (key) {
+ if (!_urlMap) {
+ _urlMap = url.parse(request.url, true);
+ }
+ return _urlMap.query[key];
+ };
+ if (request.method === "POST") {
+ var _postData = "",
+ _postMap = "";
+
+ request.on('data', function (chunk) {
+ _postData += chunk;
+ })
+ .on("end", function () {
+ request.postData = _postData;
+ request.post = function (key) {
+ if (!_postMap) {
+ _postMap = qs.parse(_postData);
+ }
+ return _postMap[key];
+ };
+ handle(curSession);
+ });
+ } else {
+ handle(curSession);
+ }
+});
+
+server.listen(8000);
View
@@ -0,0 +1,49 @@
+var Session = function (sessionId) {
+ this.sessionId = sessionId;
+ this._map = {};
+};
+Session.prototype.set = function (name, value) {
+ this._map[name] = value;
+};
+Session.prototype.get = function (name) {
+ return this._map[name];
+};
+Session.prototype.remove = function (key) {
+ delete this._map[key];
+};
+Session.prototype.removeAll = function () {
+ delete this._map;
+ this._map = {};
+};
+Session.prototype.updateTime = function () {
+ this._updateTime = new Date().getTime();
+};
+
+var SESSIONID_KEY = exports.SESSIONID_KEY = "session_id";
+
+var SessionManager = function (timeout) {
+ this.timeout = timeout;
+ this._sessions = {};
+};
+SessionManager.prototype.renew = function (response) {
+ var that = this;
+ var sessionId = [new Date().getTime(), Math.round(Math.random() * 1000)].join("");
+ var session = new Session(sessionId);
+ session.updateTime();
+ this._sessions[sessionId] = session;
+ var clientTimeout = 30 * 24 * 60 * 60 * 1000;
+ var cookie = {key: SESSIONID_KEY, value: sessionId, path: "/", expires: new Date().getTime() + clientTimeout};
+ response.setCookie(cookie);
+ return session;
+};
+SessionManager.prototype.get = function (sessionId) {
+ return this._sessions[sessionId];
+};
+SessionManager.prototype.remove = function (sessionId) {
+ delete this._sessions[sessionId];
+};
+SessionManager.prototype.isTimeout = function (session) {
+ return (session._updateTime + this.timeout) < new Date().getTime();
+};
+exports.Session = Session;
+exports.SessionManager = SessionManager;

0 comments on commit d072fb7

Please sign in to comment.