From fcb3b11c0e0f6d0c57b08d2ce96a2b12d5e51508 Mon Sep 17 00:00:00 2001 From: Montana Scott Rowe Date: Thu, 19 Jan 2012 09:42:56 -0600 Subject: [PATCH 01/23] factored out pad access as a combinator --- node/server.js | 67 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 63 insertions(+), 4 deletions(-) diff --git a/node/server.js b/node/server.js index 6a4f0cfe2..11b060eb5 100644 --- a/node/server.js +++ b/node/server.js @@ -66,12 +66,62 @@ exports.maxAge = 1000*60*60*6; //set loglevel log4js.setGlobalLogLevel(settings.loglevel); +function setupDb(callback){ + db.init(callback); +} + +function padAccessCombinator(securityManager, req, res, callback, errorback){ + function checkback(err, accessObj){ + if(err) return errorback(err); + if("grant" == accessObj.accessStatus) return callback(); + return res.send("403 - Can't touch this", 403); + } + //works great for one session + //but what if there are multiple? + //if(!("sessionIDs" in req.cookies)) + return securityManager.checkAccess( + req.params.pad, + req.cookies.sessionid, + req.cookies.token, + req.cookies.password, + checkback + ); + /*sessIds = JSON.parse(req.cookies.sessionIDs); + var tasks = []; + function createTask(sid){ + return function(cb){ + return securityManager.checkAccess( + req.params.pad, + sid, + req.cookies.token, + req.cookies.password, + cb//function(err, accessObj){return cb(err, accessObj);} + ); + } + } + for(var i = 0; i < sessIds.length; i++) + tasks[i] = createTasks(sessIds[i]); + return async.parallel( + tasks, + function(err, obs){ + if(err) return errorback(err); + for(var i = 0; i < obs.length; i++) + if("grant" == obs[i].accessStatus) return callback(null); + return res.send("none of those IDs worked", 403); + } + )*/ +} +function getStatic(req, res){ + res.header("Server", serverName); + var filePath = path.normalize( + __dirname + "/.." + + req.url.replace(/\.\./g, '').split("?")[0] + ); + res.sendfile(filePath, { maxAge: exports.maxAge }); +} async.waterfall([ //initalize the database - function (callback) - { - db.init(callback); - }, + setupDb, //initalize the http server function (callback) { @@ -137,6 +187,14 @@ async.waterfall([ //checks for padAccess function hasPadAccess(req, res, callback) { + return padAccessCombinator( + securityManager, req, res, + callback, + function errorback(err, accessObj){ + return (ERR(err, callback)); + } + ); +/* securityManager.checkAccess(req.params.pad, req.cookies.sessionid, req.cookies.token, req.cookies.password, function(err, accessObj) { if(ERR(err, callback)) return; @@ -152,6 +210,7 @@ async.waterfall([ res.send("403 - Can't touch this", 403); } }); +*/ } //checks for basic http auth From 08040e7444bda442604009d2c43beea91cac6562 Mon Sep 17 00:00:00 2001 From: Montana Scott Rowe Date: Thu, 19 Jan 2012 09:46:06 -0600 Subject: [PATCH 02/23] factored out static serve and pad name check combinators --- node/server.js | 70 ++++++++++++++++++++++++-------------------------- 1 file changed, 34 insertions(+), 36 deletions(-) diff --git a/node/server.js b/node/server.js index 11b060eb5..1129b1c7f 100644 --- a/node/server.js +++ b/node/server.js @@ -112,12 +112,33 @@ function padAccessCombinator(securityManager, req, res, callback, errorback){ )*/ } function getStatic(req, res){ - res.header("Server", serverName); - var filePath = path.normalize( - __dirname + "/.." + - req.url.replace(/\.\./g, '').split("?")[0] - ); - res.sendfile(filePath, { maxAge: exports.maxAge }); + res.header("Server", serverName); + var filePath = path.normalize( + __dirname + "/.." + + req.url.replace(/\.\./g, '').split("?")[0] + ); + res.sendfile(filePath, { maxAge: exports.maxAge }); +} +function getMinified(req, res, next) +{ + res.header("Server", serverName); + + var id = req.params.id; + + if(id == "pad.js" || id == "timeslider.js") + { + minify.minifyJS(req,res,id); + } + else + { + next(); + } +} +function checkPadName(padManager, req, res, callback){ + //ensure the padname is valid and the url doesn't end with a / + if(!padManager.isValidPadId(req.params.pad) || /\/$/.test(req.url)) + return res.send("Such a padname is forbidden", 404); + return callback(); } async.waterfall([ //initalize the database @@ -159,30 +180,10 @@ async.waterfall([ }); //serve static files - app.get('/static/*', function(req, res) - { - res.header("Server", serverName); - var filePath = path.normalize(__dirname + "/.." + - req.url.replace(/\.\./g, '').split("?")[0]); - res.sendfile(filePath, { maxAge: exports.maxAge }); - }); + app.get('/static/*', getStatic); //serve minified files - app.get('/minified/:id', function(req, res, next) - { - res.header("Server", serverName); - - var id = req.params.id; - - if(id == "pad.js" || id == "timeslider.js") - { - minify.minifyJS(req,res,id); - } - else - { - next(); - } - }); + app.get('/minified/:id', getMinified); //checks for padAccess function hasPadAccess(req, res, callback) @@ -294,13 +295,9 @@ async.waterfall([ //redirects browser to the pad's sanitized url if needed. otherwise, renders the html function goToPad(req, res, render) { - //ensure the padname is valid and the url doesn't end with a / - if(!padManager.isValidPadId(req.params.pad) || /\/$/.test(req.url)) - { - res.send('Such a padname is forbidden', 404); - } - else - { + return checkPadName( + padManager, req, res, + function callback(){ padManager.sanitizePadId(req.params.pad, function(padId) { //the pad id was sanitized, so we redirect to the sanitized version if(padId != req.params.pad) @@ -315,7 +312,8 @@ async.waterfall([ render(); } }); - } + } + ); } //serve pad.html under /p From 98f11bea6daf6eba3903898cac5164de5d9ba66d Mon Sep 17 00:00:00 2001 From: Montana Scott Rowe Date: Thu, 19 Jan 2012 10:11:51 -0600 Subject: [PATCH 03/23] factored out socket.io setup --- node/server.js | 80 ++++++++++++++++++++++++++------------------------ 1 file changed, 42 insertions(+), 38 deletions(-) diff --git a/node/server.js b/node/server.js index 1129b1c7f..b9622d9d4 100644 --- a/node/server.js +++ b/node/server.js @@ -140,6 +140,47 @@ function checkPadName(padManager, req, res, callback){ return res.send("Such a padname is forbidden", 404); return callback(); } +function setupIo(socketio, log4js, settings, socketIORouter, app){ + //init socket.io and redirect all requests to the MessageHandler + var io = socketio.listen(app); + + //this is only a workaround to ensure it works with all browers behind a proxy + //we should remove this when the new socket.io version is more stable + io.set('transports', ['xhr-polling']); + + var socketIOLogger = log4js.getLogger("socket.io"); + io.set('logger', { + debug: function (str) + { + socketIOLogger.debug.apply(socketIOLogger, arguments); + }, + info: function (str) + { + socketIOLogger.info.apply(socketIOLogger, arguments); + }, + warn: function (str) + { + socketIOLogger.warn.apply(socketIOLogger, arguments); + }, + error: function (str) + { + socketIOLogger.error.apply(socketIOLogger, arguments); + }, + }); + + //minify socket.io javascript + if(settings.minify) + io.enable('browser client minification'); + + var padMessageHandler = require("./handler/PadMessageHandler"); + var timesliderMessageHandler = require("./handler/TimesliderMessageHandler"); + + //Initalize the Socket.IO Router + socketIORouter.setSocketIO(io); + socketIORouter.addComponent("pad", padMessageHandler); + socketIORouter.addComponent("timeslider", timesliderMessageHandler); + return io; +} async.waterfall([ //initalize the database setupDb, @@ -529,44 +570,7 @@ async.waterfall([ process.on('uncaughtException', gracefulShutdown); - //init socket.io and redirect all requests to the MessageHandler - var io = socketio.listen(app); - - //this is only a workaround to ensure it works with all browers behind a proxy - //we should remove this when the new socket.io version is more stable - io.set('transports', ['xhr-polling']); - - var socketIOLogger = log4js.getLogger("socket.io"); - io.set('logger', { - debug: function (str) - { - socketIOLogger.debug.apply(socketIOLogger, arguments); - }, - info: function (str) - { - socketIOLogger.info.apply(socketIOLogger, arguments); - }, - warn: function (str) - { - socketIOLogger.warn.apply(socketIOLogger, arguments); - }, - error: function (str) - { - socketIOLogger.error.apply(socketIOLogger, arguments); - }, - }); - - //minify socket.io javascript - if(settings.minify) - io.enable('browser client minification'); - - var padMessageHandler = require("./handler/PadMessageHandler"); - var timesliderMessageHandler = require("./handler/TimesliderMessageHandler"); - - //Initalize the Socket.IO Router - socketIORouter.setSocketIO(io); - socketIORouter.addComponent("pad", padMessageHandler); - socketIORouter.addComponent("timeslider", timesliderMessageHandler); + var io = setupIo(socketio, log4js, settings, socketIORouter, app); callback(null); } From e1159f0ee0050f631439bb10f287f6f58cc5d77b Mon Sep 17 00:00:00 2001 From: Montana Scott Rowe Date: Thu, 19 Jan 2012 10:14:14 -0600 Subject: [PATCH 04/23] factored out shutdown setup --- node/server.js | 89 ++++++++++++++++++++++++++------------------------ 1 file changed, 46 insertions(+), 43 deletions(-) diff --git a/node/server.js b/node/server.js index b9622d9d4..1bb0ffd7f 100644 --- a/node/server.js +++ b/node/server.js @@ -181,6 +181,51 @@ function setupIo(socketio, log4js, settings, socketIORouter, app){ socketIORouter.addComponent("timeslider", timesliderMessageHandler); return io; } +function setupShutdown(db, app){ + var onShutdown = false; + var gracefulShutdown = function(err) + { + if(err && err.stack) + { + console.error(err.stack); + } + else if(err) + { + console.error(err); + } + + //ensure there is only one graceful shutdown running + if(onShutdown) return; + onShutdown = true; + + console.log("graceful shutdown..."); + + //stop the http server + app.close(); + + //do the db shutdown + db.db.doShutdown(function() + { + console.log("db sucessfully closed."); + + process.exit(0); + }); + + setTimeout(function(){ + process.exit(1); + }, 3000); + } + + //connect graceful shutdown with sigint and uncaughtexception + if(os.type().indexOf("Windows") == -1) + { + //sigint is so far not working on windows + //https://github.com/joyent/node/issues/1553 + process.on('SIGINT', gracefulShutdown); + } + + return process.on('uncaughtException', gracefulShutdown); +} async.waterfall([ //initalize the database setupDb, @@ -526,49 +571,7 @@ async.waterfall([ app.listen(settings.port, settings.ip); console.log("Server is listening at " + settings.ip + ":" + settings.port); - var onShutdown = false; - var gracefulShutdown = function(err) - { - if(err && err.stack) - { - console.error(err.stack); - } - else if(err) - { - console.error(err); - } - - //ensure there is only one graceful shutdown running - if(onShutdown) return; - onShutdown = true; - - console.log("graceful shutdown..."); - - //stop the http server - app.close(); - - //do the db shutdown - db.db.doShutdown(function() - { - console.log("db sucessfully closed."); - - process.exit(0); - }); - - setTimeout(function(){ - process.exit(1); - }, 3000); - } - - //connect graceful shutdown with sigint and uncaughtexception - if(os.type().indexOf("Windows") == -1) - { - //sigint is so far not working on windows - //https://github.com/joyent/node/issues/1553 - process.on('SIGINT', gracefulShutdown); - } - - process.on('uncaughtException', gracefulShutdown); + setupShutdown(db, app); var io = setupIo(socketio, log4js, settings, socketIORouter, app); From 7b96bb7f8ee337ed5d60065aea65bf29b28b52af Mon Sep 17 00:00:00 2001 From: Montana Scott Rowe Date: Thu, 19 Jan 2012 10:44:38 -0600 Subject: [PATCH 05/23] factored out static sendfile --- node/server.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/node/server.js b/node/server.js index 1bb0ffd7f..8e6b2e35c 100644 --- a/node/server.js +++ b/node/server.js @@ -226,6 +226,12 @@ function setupShutdown(db, app){ return process.on('uncaughtException', gracefulShutdown); } +function sendStatic(path, res, filename, callback){ + if("function" != typeof callback) callback = function(){}; + res.header("Server", serverName); + var filePath = path.normalize(__dirname + "/../static/" + filename); + return res.sendfile(filePath, { maxAge: exports.maxAge }, callback); +} async.waterfall([ //initalize the database setupDb, @@ -406,9 +412,12 @@ async.waterfall([ app.get('/p/:pad', function(req, res, next) { goToPad(req, res, function() { + return sendStatic(path, res, "pad.html"); +/* res.header("Server", serverName); var filePath = path.normalize(__dirname + "/../static/pad.html"); res.sendfile(filePath, { maxAge: exports.maxAge }); +*/ }); }); @@ -416,9 +425,12 @@ async.waterfall([ app.get('/p/:pad/timeslider', function(req, res, next) { goToPad(req, res, function() { + return sendStatic(path, res "timeslider.html"); +/* res.header("Server", serverName); var filePath = path.normalize(__dirname + "/../static/timeslider.html"); res.sendfile(filePath, { maxAge: exports.maxAge }); +*/ }); }); @@ -538,26 +550,36 @@ async.waterfall([ //serve index.html under / app.get('/', function(req, res) { + return sendStatic(path, res, "index.html"); +/* res.header("Server", serverName); var filePath = path.normalize(__dirname + "/../static/index.html"); res.sendfile(filePath, { maxAge: exports.maxAge }); +*/ }); //serve robots.txt app.get('/robots.txt', function(req, res) { + return sendStatic(path, res, "robots.txt"); +/* res.header("Server", serverName); var filePath = path.normalize(__dirname + "/../static/robots.txt"); res.sendfile(filePath, { maxAge: exports.maxAge }); +*/ }); //serve favicon.ico app.get('/favicon.ico', function(req, res) { + return sendStatic(path, res, "custom/favicon.ico", + function(err){ +/* res.header("Server", serverName); var filePath = path.normalize(__dirname + "/../static/custom/favicon.ico"); res.sendfile(filePath, { maxAge: exports.maxAge }, function(err) { +*/ //there is no custom favicon, send the default favicon if(err) { From c109af52e1f36c3a49051cee724d0350568e648d Mon Sep 17 00:00:00 2001 From: Montana Scott Rowe Date: Thu, 19 Jan 2012 11:55:59 -0600 Subject: [PATCH 06/23] for read-only pads, turned async series into waterfall to reduce state and factored out translation combinator --- node/server.js | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/node/server.js b/node/server.js index 8e6b2e35c..4f372693b 100644 --- a/node/server.js +++ b/node/server.js @@ -232,6 +232,17 @@ function sendStatic(path, res, filename, callback){ var filePath = path.normalize(__dirname + "/../static/" + filename); return res.sendfile(filePath, { maxAge: exports.maxAge }, callback); } +function translateRoCombinator(managers, req, ERR, callback){ + return function translateRo(callback){ + function padBack(err, padId){ + if(ERR(err, callback)) return; + //we need that to tell hasPadAccess about the pad + req.params.pad = padId; + callback(err, padId); + } + managers.ro.getPadID(req.params.id, padBack); + }; +} async.waterfall([ //initalize the database setupDb, @@ -284,7 +295,7 @@ async.waterfall([ securityManager, req, res, callback, function errorback(err, accessObj){ - return (ERR(err, callback)); + return ERR(err, callback); } ); /* @@ -331,28 +342,31 @@ async.waterfall([ { res.header("Server", serverName); - var html; - var padId; + //var html; + //var padId; var pad; - async.series([ + async.waterfall([ //translate the read only pad to a padId function(callback) { + return translateRoCombinater({ro: readOnlyManager}, req, ERR, callback); +/* readOnlyManager.getPadId(req.params.id, function(err, _padId) { if(ERR(err, callback)) return; - padId = _padId; + //padId = _padId; //we need that to tell hasPadAcess about the pad - req.params.pad = padId; + req.params.pad = _padId; - callback(); + callback(null, _padId); }); +*/ }, //render the html document - function(callback) + function(padId, callback) { //return if the there is no padId if(padId == null) @@ -367,10 +381,15 @@ async.waterfall([ exporthtml.getPadHTMLDocument(padId, null, false, function(err, _html) { if(ERR(err, callback)) return; - html = _html; - callback(); + //html = _html; + callback(null, _html); }); }); + }, + function(html, callback) + { + res.send(html); + return callback(null); } ], function(err) { @@ -380,8 +399,6 @@ async.waterfall([ if(err == "notfound") res.send('404 - Not Found', 404); - else - res.send(html); }); }); From b0a58b0b3e67a76b18646ff0797b1b14a9f6fdd4 Mon Sep 17 00:00:00 2001 From: Montana Scott Rowe Date: Thu, 19 Jan 2012 11:56:50 -0600 Subject: [PATCH 07/23] finished a step I'd accidentally left half-completed in the refactoring of a higher-order function --- node/server.js | 21 ++------------------- 1 file changed, 2 insertions(+), 19 deletions(-) diff --git a/node/server.js b/node/server.js index 4f372693b..5bc70729a 100644 --- a/node/server.js +++ b/node/server.js @@ -232,7 +232,7 @@ function sendStatic(path, res, filename, callback){ var filePath = path.normalize(__dirname + "/../static/" + filename); return res.sendfile(filePath, { maxAge: exports.maxAge }, callback); } -function translateRoCombinator(managers, req, ERR, callback){ +function translateRoCombinator(managers, req, ERR){ return function translateRo(callback){ function padBack(err, padId){ if(ERR(err, callback)) return; @@ -347,24 +347,7 @@ async.waterfall([ var pad; async.waterfall([ - //translate the read only pad to a padId - function(callback) - { - return translateRoCombinater({ro: readOnlyManager}, req, ERR, callback); -/* - readOnlyManager.getPadId(req.params.id, function(err, _padId) - { - if(ERR(err, callback)) return; - - //padId = _padId; - - //we need that to tell hasPadAcess about the pad - req.params.pad = _padId; - - callback(null, _padId); - }); -*/ - }, + translateRoCombinater({ro: readOnlyManager}, req, ERR), //render the html document function(padId, callback) { From c5c548db110dd94b2b18d58102937c5d3a0cae5d Mon Sep 17 00:00:00 2001 From: Montana Scott Rowe Date: Thu, 19 Jan 2012 12:52:52 -0600 Subject: [PATCH 08/23] refactored out readonly pad render combinators --- node/server.js | 48 +++++++++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/node/server.js b/node/server.js index 5bc70729a..0b8daf9a2 100644 --- a/node/server.js +++ b/node/server.js @@ -243,6 +243,32 @@ function translateRoCombinator(managers, req, ERR){ managers.ro.getPadID(req.params.id, padBack); }; } +function roAsyncOutCombinator(ERR, callback){ + return function(err, data){ + if(ERR(err, callback)) return; + callback(err, data); + } +} +function roAccessbackCombinator(exporthtml, padId, callback){ + return function(){ + exporthtml.getPadHTMLDocument(padId, null, false, callback); + } +} +function roRenderCombinator(padAccessp, req, res, exporthtml, ERR){ + return function(padId, callback){ + if(null == padId) + return callback("notfound"); + padAccessp( + req, res, + roAccessbackCombinator( + exporthtml, padId, + roAsyncOutCombinator( + ERR, callback + ) + ) + ); + } +} async.waterfall([ //initalize the database setupDb, @@ -348,27 +374,7 @@ async.waterfall([ async.waterfall([ translateRoCombinater({ro: readOnlyManager}, req, ERR), - //render the html document - function(padId, callback) - { - //return if the there is no padId - if(padId == null) - { - callback("notfound"); - return; - } - - hasPadAccess(req, res, function() - { - //render the html document - exporthtml.getPadHTMLDocument(padId, null, false, function(err, _html) - { - if(ERR(err, callback)) return; - //html = _html; - callback(null, _html); - }); - }); - }, + roRenderCombinator(hasPadAccess, req, res, exporthtml, ERR), function(html, callback) { res.send(html); From a4795eb88d41a5f726c2b4a4b1f8b80686747cf9 Mon Sep 17 00:00:00 2001 From: Montana Scott Rowe Date: Thu, 19 Jan 2012 13:17:42 -0600 Subject: [PATCH 09/23] factored out read only pad getter --- node/server.js | 66 ++++++++++++++++++++++++++------------------------ 1 file changed, 34 insertions(+), 32 deletions(-) diff --git a/node/server.js b/node/server.js index 0b8daf9a2..7c0429b07 100644 --- a/node/server.js +++ b/node/server.js @@ -238,27 +238,27 @@ function translateRoCombinator(managers, req, ERR){ if(ERR(err, callback)) return; //we need that to tell hasPadAccess about the pad req.params.pad = padId; - callback(err, padId); + return callback(err, padId); } - managers.ro.getPadID(req.params.id, padBack); + return managers.ro.getPadID(req.params.id, padBack); }; } function roAsyncOutCombinator(ERR, callback){ return function(err, data){ if(ERR(err, callback)) return; - callback(err, data); + return callback(err, data); } } function roAccessbackCombinator(exporthtml, padId, callback){ - return function(){ - exporthtml.getPadHTMLDocument(padId, null, false, callback); + return function accessBack(){ + return exporthtml.getPadHTMLDocument(padId, null, false, callback); } } function roRenderCombinator(padAccessp, req, res, exporthtml, ERR){ return function(padId, callback){ if(null == padId) return callback("notfound"); - padAccessp( + return padAccessp( req, res, roAccessbackCombinator( exporthtml, padId, @@ -269,6 +269,33 @@ function roRenderCombinator(padAccessp, req, res, exporthtml, ERR){ ); } } +function roBindSendBackCombinator(res){ + return function(html, callback){ + res.send(html); + return callback(null); + } +} +function roErrBackCombinator(ERR, res){ + return function(err){ + if(!err) return + if("notfound" == err) + return res.send("404 - Not Found", 404); + return ERR(err); + } +} +function getRoCombinator(serverName, managers, padAccessp, ERR, exporthtml){ + return function(req, res){ + res.header("Server", serverName); + return async.waterfall( + [ + translateRoCombinator(managers), + roRenderCombinator(padAccessp, req, res, exporthtml, ERR), + roBindSendBackCombinator(res) + ], + roErrBackCombinator(ERR, res)) + ) + } +} async.waterfall([ //initalize the database setupDb, @@ -364,32 +391,7 @@ async.waterfall([ } //serve read only pad - app.get('/ro/:id', function(req, res) - { - res.header("Server", serverName); - - //var html; - //var padId; - var pad; - - async.waterfall([ - translateRoCombinater({ro: readOnlyManager}, req, ERR), - roRenderCombinator(hasPadAccess, req, res, exporthtml, ERR), - function(html, callback) - { - res.send(html); - return callback(null); - } - ], function(err) - { - //throw any unexpected error - if(err && err != "notfound") - ERR(err); - - if(err == "notfound") - res.send('404 - Not Found', 404); - }); - }); + app.get('/ro/:id', getRoCombinator(serverName, {ro: readOnlyManager}, hasPadAccess, ERR, exporthtml)); //redirects browser to the pad's sanitized url if needed. otherwise, renders the html function goToPad(req, res, render) { From fdcf1d41530f3b80a9a7cfd1a30df38667fe7417 Mon Sep 17 00:00:00 2001 From: Montana Scott Rowe Date: Thu, 19 Jan 2012 13:36:50 -0600 Subject: [PATCH 10/23] factored out static serve on goToPad --- node/server.js | 52 +++++++++++++------------------------------------- 1 file changed, 13 insertions(+), 39 deletions(-) diff --git a/node/server.js b/node/server.js index 7c0429b07..1c0fa1c17 100644 --- a/node/server.js +++ b/node/server.js @@ -296,6 +296,16 @@ function getRoCombinator(serverName, managers, padAccessp, ERR, exporthtml){ ) } } +function sendStaticIfPad(goToPad, padManager, path, filename){ + return function staticSender(req, res, next){ + return goToPad( + req, res, + function goBack(){ + return sendStatic(path, res, filename); + } + ); + }; +} async.waterfall([ //initalize the database setupDb, @@ -351,23 +361,6 @@ async.waterfall([ return ERR(err, callback); } ); -/* - securityManager.checkAccess(req.params.pad, req.cookies.sessionid, req.cookies.token, req.cookies.password, function(err, accessObj) - { - if(ERR(err, callback)) return; - - //there is access, continue - if(accessObj.accessStatus == "grant") - { - callback(); - } - //no access - else - { - res.send("403 - Can't touch this", 403); - } - }); -*/ } //checks for basic http auth @@ -417,30 +410,11 @@ async.waterfall([ } //serve pad.html under /p - app.get('/p/:pad', function(req, res, next) - { - goToPad(req, res, function() { - return sendStatic(path, res, "pad.html"); -/* - res.header("Server", serverName); - var filePath = path.normalize(__dirname + "/../static/pad.html"); - res.sendfile(filePath, { maxAge: exports.maxAge }); -*/ - }); - }); + app.get('/p/:pad', sendStaticIfPad(goToPad, padManager, path, "pad.html")); //serve timeslider.html under /p/$padname/timeslider - app.get('/p/:pad/timeslider', function(req, res, next) - { - goToPad(req, res, function() { - return sendStatic(path, res "timeslider.html"); -/* - res.header("Server", serverName); - var filePath = path.normalize(__dirname + "/../static/timeslider.html"); - res.sendfile(filePath, { maxAge: exports.maxAge }); -*/ - }); - }); + app.get('/p/:pad/timeslider', sendStaticIfPad(goToPad, padManager, path, "timeslider.html")); + //serve timeslider.html under /p/$padname/timeslider app.get('/p/:pad/:rev?/export/:type', function(req, res, next) From b38d88e454b83aeef0705c25972faf14b57c4b34 Mon Sep 17 00:00:00 2001 From: Montana Scott Rowe Date: Thu, 19 Jan 2012 14:07:19 -0600 Subject: [PATCH 11/23] factored out pad export POST combinator in the least interesting way --- node/server.js | 59 ++++++++++++++++++++++++++------------------------ 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/node/server.js b/node/server.js index 1c0fa1c17..452149d3f 100644 --- a/node/server.js +++ b/node/server.js @@ -306,6 +306,35 @@ function sendStaticIfPad(goToPad, padManager, path, filename){ ); }; } +function getExportPadCombinator(goToPad, settings, hasPadAccess, exportHandler){ + return function(req, res, next){ + goToPad(req, res, function() { + var types = ["pdf", "doc", "txt", "html", "odt", "dokuwiki"]; + //send a 404 if we don't support this filetype + if(types.indexOf(req.params.type) == -1) + { + next(); + return; + } + + //if abiword is disabled, and this is a format we only support with abiword, output a message + if(settings.abiword == null && + ["odt", "pdf", "doc"].indexOf(req.params.type) !== -1) + { + res.send("Abiword is not enabled at this Etherpad Lite instance. Set the path to Abiword in settings.json to enable this feature"); + return; + } + + res.header("Access-Control-Allow-Origin", "*"); + res.header("Server", serverName); + + hasPadAccess(req, res, function() + { + exportHandler.doExport(req, res, req.params.pad, req.params.type); + }); + }); + } +} async.waterfall([ //initalize the database setupDb, @@ -417,34 +446,8 @@ async.waterfall([ //serve timeslider.html under /p/$padname/timeslider - app.get('/p/:pad/:rev?/export/:type', function(req, res, next) - { - goToPad(req, res, function() { - var types = ["pdf", "doc", "txt", "html", "odt", "dokuwiki"]; - //send a 404 if we don't support this filetype - if(types.indexOf(req.params.type) == -1) - { - next(); - return; - } - - //if abiword is disabled, and this is a format we only support with abiword, output a message - if(settings.abiword == null && - ["odt", "pdf", "doc"].indexOf(req.params.type) !== -1) - { - res.send("Abiword is not enabled at this Etherpad Lite instance. Set the path to Abiword in settings.json to enable this feature"); - return; - } - - res.header("Access-Control-Allow-Origin", "*"); - res.header("Server", serverName); - - hasPadAccess(req, res, function() - { - exportHandler.doExport(req, res, req.params.pad, req.params.type); - }); - }); - }); + //the above comment is wrong + app.get('/p/:pad/:rev?/export/:type', getExportPadCombinator(goToPad, settings, hasPadAccess, exportHandler)); //handle import requests app.post('/p/:pad/import', function(req, res, next) From e703fd1ba0bf191922a74214c6c102d8e613c3f2 Mon Sep 17 00:00:00 2001 From: Montana Scott Rowe Date: Thu, 19 Jan 2012 14:26:05 -0600 Subject: [PATCH 12/23] factored out pad import --- node/server.js | 48 ++++++++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/node/server.js b/node/server.js index 452149d3f..1c2415e60 100644 --- a/node/server.js +++ b/node/server.js @@ -306,9 +306,9 @@ function sendStaticIfPad(goToPad, padManager, path, filename){ ); }; } -function getExportPadCombinator(goToPad, settings, hasPadAccess, exportHandler){ - return function(req, res, next){ - goToPad(req, res, function() { +function getExportPadCombinator(goToPad, settings, hasPadAccess, exportHandler, serverName){ + return function getExportPad(req, res, next){ + goToPad(req, res, function padback() { var types = ["pdf", "doc", "txt", "html", "odt", "dokuwiki"]; //send a 404 if we don't support this filetype if(types.indexOf(req.params.type) == -1) @@ -335,6 +335,27 @@ function getExportPadCombinator(goToPad, settings, hasPadAccess, exportHandler){ }); } } + +function postImportPadCombinator(goToPad, settings, serverName, hasPadAccess, importHandler){ + return function postImportPad(req, res, next){ + goToPad(req, res, function padback() { + //if abiword is disabled, skip handling this request + if(settings.abiword == null) + { + next(); + return; + } + + res.header("Server", serverName); + + hasPadAccess(req, res, function() + { + importHandler.doImport(req, res, req.params.pad); + }); + }); + } +} + async.waterfall([ //initalize the database setupDb, @@ -447,27 +468,10 @@ async.waterfall([ //serve timeslider.html under /p/$padname/timeslider //the above comment is wrong - app.get('/p/:pad/:rev?/export/:type', getExportPadCombinator(goToPad, settings, hasPadAccess, exportHandler)); + app.get('/p/:pad/:rev?/export/:type', getExportPadCombinator(goToPad, settings, hasPadAccess, exportHandler, serverName)); //handle import requests - app.post('/p/:pad/import', function(req, res, next) - { - goToPad(req, res, function() { - //if abiword is disabled, skip handling this request - if(settings.abiword == null) - { - next(); - return; - } - - res.header("Server", serverName); - - hasPadAccess(req, res, function() - { - importHandler.doImport(req, res, req.params.pad); - }); - }); - }); + app.post('/p/:pad/import', postImportPadCombinator(goToPad, settings, serverName, hasPadAccess, importHandler)); var apiLogger = log4js.getLogger("API"); From b0d23cb952706c0c2af8d44b765cb34aca1f8df0 Mon Sep 17 00:00:00 2001 From: Montana Scott Rowe Date: Thu, 19 Jan 2012 14:42:40 -0600 Subject: [PATCH 13/23] factored out API caller and client feedback combinators --- node/server.js | 95 +++++++++++++++++++++----------------------------- 1 file changed, 39 insertions(+), 56 deletions(-) diff --git a/node/server.js b/node/server.js index 1c2415e60..bbcf005da 100644 --- a/node/server.js +++ b/node/server.js @@ -355,6 +355,42 @@ function postImportPadCombinator(goToPad, settings, serverName, hasPadAccess, im }); } } +function apiCallerCombinator(serverName, apiLogger, apiHandler){ + return function apiCaller(req, res, fields){ + res.header("Server", serverName); + res.header("Content-Type", "application/json; charset=utf-8"); + + apiLogger.info("REQUEST, " + req.params.func + ", " + JSON.stringify(fields)); + + //wrap the send function so we can log the response + res._send = res.send; + res.send = function(response) + { + response = JSON.stringify(response); + apiLogger.info("RESPONSE, " + req.params.func + ", " + response); + + //is this a jsonp call, if yes, add the function call + if(req.query.jsonp) + response = req.query.jsonp + "(" + response + ")"; + + res._send(response); + } + + //call the api handler + apiHandler.handle(req.params.func, fields, req, res); + } +} +function logOkPostCombinator(prefix, consoleFn, field){ + return function(req, res){ + new formidable.IncomingForm().parse( + req, + function(err, fields, files){ + console[consoleFn](prefix + ": " + fields[field]); + res.end("OK"); + } + ); + } +} async.waterfall([ //initalize the database @@ -476,30 +512,7 @@ async.waterfall([ var apiLogger = log4js.getLogger("API"); //This is for making an api call, collecting all post information and passing it to the apiHandler - var apiCaller = function(req, res, fields) - { - res.header("Server", serverName); - res.header("Content-Type", "application/json; charset=utf-8"); - - apiLogger.info("REQUEST, " + req.params.func + ", " + JSON.stringify(fields)); - - //wrap the send function so we can log the response - res._send = res.send; - res.send = function(response) - { - response = JSON.stringify(response); - apiLogger.info("RESPONSE, " + req.params.func + ", " + response); - - //is this a jsonp call, if yes, add the function call - if(req.query.jsonp) - response = req.query.jsonp + "(" + response + ")"; - - res._send(response); - } - - //call the api handler - apiHandler.handle(req.params.func, fields, req, res); - } + var apiCaller = apiCallerCombinator(serverName, apiLogger, apiHandler); //This is a api GET call, collect all post informations and pass it to the apiHandler app.get('/api/1/:func', function(req, res) @@ -517,45 +530,21 @@ async.waterfall([ }); //The Etherpad client side sends information about how a disconnect happen - app.post('/ep/pad/connection-diagnostic-info', function(req, res) - { - new formidable.IncomingForm().parse(req, function(err, fields, files) - { - console.log("DIAGNOSTIC-INFO: " + fields.diagnosticInfo); - res.end("OK"); - }); - }); + app.post('/ep/pad/connection-diagnostic-info', logOkPostCombinator("DIAGNOSTIC-INFO", "log", "diagnosticInfo")); //The Etherpad client side sends information about client side javscript errors - app.post('/jserror', function(req, res) - { - new formidable.IncomingForm().parse(req, function(err, fields, files) - { - console.error("CLIENT SIDE JAVASCRIPT ERROR: " + fields.errorInfo); - res.end("OK"); - }); - }); + app.post('/jserror', logOkPostCombinator("CLIENT SIDE JAVASCRIPT ERROR", "error", "errorInfo")); //serve index.html under / app.get('/', function(req, res) { return sendStatic(path, res, "index.html"); -/* - res.header("Server", serverName); - var filePath = path.normalize(__dirname + "/../static/index.html"); - res.sendfile(filePath, { maxAge: exports.maxAge }); -*/ }); //serve robots.txt app.get('/robots.txt', function(req, res) { return sendStatic(path, res, "robots.txt"); -/* - res.header("Server", serverName); - var filePath = path.normalize(__dirname + "/../static/robots.txt"); - res.sendfile(filePath, { maxAge: exports.maxAge }); -*/ }); //serve favicon.ico @@ -563,12 +552,6 @@ async.waterfall([ { return sendStatic(path, res, "custom/favicon.ico", function(err){ -/* - res.header("Server", serverName); - var filePath = path.normalize(__dirname + "/../static/custom/favicon.ico"); - res.sendfile(filePath, { maxAge: exports.maxAge }, function(err) - { -*/ //there is no custom favicon, send the default favicon if(err) { From 772925f2852941c113a7b86e1d9bde57e905f83a Mon Sep 17 00:00:00 2001 From: Montana Scott Rowe Date: Thu, 19 Jan 2012 16:29:04 -0600 Subject: [PATCH 14/23] pulled configure and basic_auth into global scope --- node/server.js | 66 +++++++++++++++++++++++++++----------------------- 1 file changed, 36 insertions(+), 30 deletions(-) diff --git a/node/server.js b/node/server.js index bbcf005da..442b6d3f2 100644 --- a/node/server.js +++ b/node/server.js @@ -392,6 +392,40 @@ function logOkPostCombinator(prefix, consoleFn, field){ } } +function configureCombinator(app, settings, basic_auth, log4js, httpLogger){ + return function configBack() + { + // Activate http basic auth if it has been defined in settings.json + if(settings.httpAuth != null) app.use(basic_auth); + + // If the log level specified in the config file is WARN or ERROR the application server never starts listening to requests as reported in issue #158. + // Not installing the log4js connect logger when the log level has a higher severity than INFO since it would not log at that level anyway. + if (!(settings.loglevel === "WARN" || settings.loglevel == "ERROR")) + app.use(log4js.connectLogger(httpLogger, { level: log4js.levels.INFO, format: ':status, :method :url'})); + app.use(express.cookieParser()); + }; +} + +//checks for basic http auth +function basic_auth (req, res, next) { + if (req.headers.authorization && req.headers.authorization.search('Basic ') === 0) { + // fetch login and password + if (new Buffer(req.headers.authorization.split(' ')[1], 'base64').toString() == settings.httpAuth) { + next(); + return; + } + } + + res.header('WWW-Authenticate', 'Basic realm="Protected Area"'); + if (req.headers.authorization) { + setTimeout(function () { + res.send('Authentication required', 401); + }, 1000); + } else { + res.send('Authentication required', 401); + } +} + async.waterfall([ //initalize the database setupDb, @@ -413,17 +447,7 @@ async.waterfall([ //install logging var httpLogger = log4js.getLogger("http"); - app.configure(function() - { - // Activate http basic auth if it has been defined in settings.json - if(settings.httpAuth != null) app.use(basic_auth); - - // If the log level specified in the config file is WARN or ERROR the application server never starts listening to requests as reported in issue #158. - // Not installing the log4js connect logger when the log level has a higher severity than INFO since it would not log at that level anyway. - if (!(settings.loglevel === "WARN" || settings.loglevel == "ERROR")) - app.use(log4js.connectLogger(httpLogger, { level: log4js.levels.INFO, format: ':status, :method :url'})); - app.use(express.cookieParser()); - }); + app.configure(configureCombinator(app, settings, basic_auth, log4js, httpLogger)); app.error(function(err, req, res, next){ res.send(500); @@ -449,25 +473,7 @@ async.waterfall([ ); } - //checks for basic http auth - function basic_auth (req, res, next) { - if (req.headers.authorization && req.headers.authorization.search('Basic ') === 0) { - // fetch login and password - if (new Buffer(req.headers.authorization.split(' ')[1], 'base64').toString() == settings.httpAuth) { - next(); - return; - } - } - - res.header('WWW-Authenticate', 'Basic realm="Protected Area"'); - if (req.headers.authorization) { - setTimeout(function () { - res.send('Authentication required', 401); - }, 1000); - } else { - res.send('Authentication required', 401); - } - } + //serve read only pad app.get('/ro/:id', getRoCombinator(serverName, {ro: readOnlyManager}, hasPadAccess, ERR, exporthtml)); From a45dd74b31decbac7792d9dc172723310d4f9b4c Mon Sep 17 00:00:00 2001 From: Montana Scott Rowe Date: Thu, 19 Jan 2012 16:48:24 -0600 Subject: [PATCH 15/23] factored out goToPad as a combinator --- node/server.js | 85 +++++++++++++++++++++++++++----------------------- 1 file changed, 46 insertions(+), 39 deletions(-) diff --git a/node/server.js b/node/server.js index 442b6d3f2..d0958e9c4 100644 --- a/node/server.js +++ b/node/server.js @@ -426,6 +426,30 @@ function basic_auth (req, res, next) { } } +function gotoPadCombinator(checkPadName, padManager){ + return function gotoPad(req, res, render){ + return checkPadName( + padManager, req, res, + function callback(){ + padManager.sanitizePadId(req.params.pad, function(padId) { + //the pad id was sanitized, so we redirect to the sanitized version + if(padId != req.params.pad) + { + var real_path = req.path.replace(/^\/p\/[^\/]+/, '/p/' + padId); + res.header('Location', real_path); + res.send('You should be redirected to ' + real_path + '', 302); + } + //the pad id was fine, so just render it + else + { + render(); + } + }); + } + ); + } +} + async.waterfall([ //initalize the database setupDb, @@ -447,20 +471,8 @@ async.waterfall([ //install logging var httpLogger = log4js.getLogger("http"); - app.configure(configureCombinator(app, settings, basic_auth, log4js, httpLogger)); - - app.error(function(err, req, res, next){ - res.send(500); - console.error(err.stack ? err.stack : err.toString()); - gracefulShutdown(); - }); - - //serve static files - app.get('/static/*', getStatic); - - //serve minified files - app.get('/minified/:id', getMinified); - + var apiLogger = log4js.getLogger("API"); + //checks for padAccess function hasPadAccess(req, res, callback) { @@ -472,35 +484,31 @@ async.waterfall([ } ); } + //redirects browser to the pad's sanitized url if needed. otherwise, renders the html + var goToPad = gotoPadCombinator(checkPadName, padManager); + + + app.configure(configureCombinator(app, settings, basic_auth, log4js, httpLogger)); + + app.error(function(err, req, res, next){ + res.send(500); + console.error(err.stack ? err.stack : err.toString()); + gracefulShutdown(); + }); + + + //serve static files + app.get('/static/*', getStatic); + + //serve minified files + app.get('/minified/:id', getMinified); //serve read only pad app.get('/ro/:id', getRoCombinator(serverName, {ro: readOnlyManager}, hasPadAccess, ERR, exporthtml)); - - //redirects browser to the pad's sanitized url if needed. otherwise, renders the html - function goToPad(req, res, render) { - return checkPadName( - padManager, req, res, - function callback(){ - padManager.sanitizePadId(req.params.pad, function(padId) { - //the pad id was sanitized, so we redirect to the sanitized version - if(padId != req.params.pad) - { - var real_path = req.path.replace(/^\/p\/[^\/]+/, '/p/' + padId); - res.header('Location', real_path); - res.send('You should be redirected to ' + real_path + '', 302); - } - //the pad id was fine, so just render it - else - { - render(); - } - }); - } - ); - } - + + //serve pad.html under /p app.get('/p/:pad', sendStaticIfPad(goToPad, padManager, path, "pad.html")); @@ -515,7 +523,6 @@ async.waterfall([ //handle import requests app.post('/p/:pad/import', postImportPadCombinator(goToPad, settings, serverName, hasPadAccess, importHandler)); - var apiLogger = log4js.getLogger("API"); //This is for making an api call, collecting all post information and passing it to the apiHandler var apiCaller = apiCallerCombinator(serverName, apiLogger, apiHandler); From 1b03e1991ac211e421b4c2e1113210b155ae2e5c Mon Sep 17 00:00:00 2001 From: Montana Scott Rowe Date: Thu, 19 Jan 2012 16:55:39 -0600 Subject: [PATCH 16/23] placed additionalSetup hook --- node/server.js | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/node/server.js b/node/server.js index d0958e9c4..e0e8da4d6 100644 --- a/node/server.js +++ b/node/server.js @@ -450,7 +450,8 @@ function gotoPadCombinator(checkPadName, padManager){ } } -async.waterfall([ +function init(additionalSetup){ + async.waterfall([ //initalize the database setupDb, //initalize the http server @@ -486,6 +487,9 @@ async.waterfall([ } //redirects browser to the pad's sanitized url if needed. otherwise, renders the html var goToPad = gotoPadCombinator(checkPadName, padManager); + //This is for making an api call, collecting all post information and passing it to the apiHandler + var apiCaller = apiCallerCombinator(serverName, apiLogger, apiHandler); + app.configure(configureCombinator(app, settings, basic_auth, log4js, httpLogger)); @@ -498,7 +502,7 @@ async.waterfall([ - + var gets = {}; //serve static files app.get('/static/*', getStatic); @@ -507,14 +511,12 @@ async.waterfall([ //serve read only pad app.get('/ro/:id', getRoCombinator(serverName, {ro: readOnlyManager}, hasPadAccess, ERR, exporthtml)); - //serve pad.html under /p app.get('/p/:pad', sendStaticIfPad(goToPad, padManager, path, "pad.html")); //serve timeslider.html under /p/$padname/timeslider app.get('/p/:pad/timeslider', sendStaticIfPad(goToPad, padManager, path, "timeslider.html")); - //serve timeslider.html under /p/$padname/timeslider //the above comment is wrong @@ -522,11 +524,7 @@ async.waterfall([ //handle import requests app.post('/p/:pad/import', postImportPadCombinator(goToPad, settings, serverName, hasPadAccess, importHandler)); - - - //This is for making an api call, collecting all post information and passing it to the apiHandler - var apiCaller = apiCallerCombinator(serverName, apiLogger, apiHandler); - + //This is a api GET call, collect all post informations and pass it to the apiHandler app.get('/api/1/:func', function(req, res) { @@ -574,6 +572,9 @@ async.waterfall([ }); }); + + additionalSetup(); + //let the server listen app.listen(settings.port, settings.ip); console.log("Server is listening at " + settings.ip + ":" + settings.port); @@ -584,4 +585,9 @@ async.waterfall([ callback(null); } -]); + ]); + +} + +this.init = init; +init(function(){}); \ No newline at end of file From c7d4a5cf3310976722b3c3aa68e3fa41c823290a Mon Sep 17 00:00:00 2001 From: Montana Scott Rowe Date: Thu, 19 Jan 2012 17:04:17 -0600 Subject: [PATCH 17/23] moved GET functions into an object --- node/server.js | 92 +++++++++++++++++++++++++++----------------------- 1 file changed, 50 insertions(+), 42 deletions(-) diff --git a/node/server.js b/node/server.js index e0e8da4d6..585972b35 100644 --- a/node/server.js +++ b/node/server.js @@ -504,33 +504,67 @@ function init(additionalSetup){ var gets = {}; //serve static files - app.get('/static/*', getStatic); - + gets['/static/*'] = getStatic; //serve minified files - app.get('/minified/:id', getMinified); - + gets['/minified/:id'] = getMinified; //serve read only pad - app.get('/ro/:id', getRoCombinator(serverName, {ro: readOnlyManager}, hasPadAccess, ERR, exporthtml)); - + gets['/ro/:id'] = getRoCombinator( + serverName, + {ro: readOnlyManager}, + hasPadAccess, + ERR, + exporthtml + ); //serve pad.html under /p - app.get('/p/:pad', sendStaticIfPad(goToPad, padManager, path, "pad.html")); - + gets['/p/:pad'] = sendStaticIfPad( + goToPad, + padManager, + path, + "pad.html" + ); //serve timeslider.html under /p/$padname/timeslider - app.get('/p/:pad/timeslider', sendStaticIfPad(goToPad, padManager, path, "timeslider.html")); + gets['/p/:pad/timeslider'] = sendStaticIfPad( + goToPad, padManager, path, "timeslider.html" + ); //serve timeslider.html under /p/$padname/timeslider //the above comment is wrong - app.get('/p/:pad/:rev?/export/:type', getExportPadCombinator(goToPad, settings, hasPadAccess, exportHandler, serverName)); - - //handle import requests - app.post('/p/:pad/import', postImportPadCombinator(goToPad, settings, serverName, hasPadAccess, importHandler)); - + gets['/p/:pad/:rev?/export/:type'] = getExportPadCombinator( + goToPad, settings, hasPadAccess, exportHandler, serverName + ); //This is a api GET call, collect all post informations and pass it to the apiHandler - app.get('/api/1/:func', function(req, res) + gets['/api/1/:func'] = function(req, res) { apiCaller(req, res, req.query) - }); + } + //serve index.html under / + gets['/'] = function(req, res) + { + return sendStatic(path, res, "index.html"); + } + //serve robots.txt + gets['/robots.txt'] = function(req, res) + { + return sendStatic(path, res, "robots.txt"); + } + //serve favicon.ico + gets['/favicon.ico'] = function(req, res) + { + return sendStatic(path, res, "custom/favicon.ico", + function(err){ + //there is no custom favicon, send the default favicon + if(err) + { + filePath = path.normalize(__dirname + "/../static/favicon.ico"); + res.sendfile(filePath, { maxAge: exports.maxAge }); + } + }); + }; + + for(var key in gets) app.get(key, gets[key]); + //handle import requests + app.post('/p/:pad/import', postImportPadCombinator(goToPad, settings, serverName, hasPadAccess, importHandler)); //This is a api POST call, collect all post informations and pass it to the apiHandler app.post('/api/1/:func', function(req, res) { @@ -546,32 +580,6 @@ function init(additionalSetup){ //The Etherpad client side sends information about client side javscript errors app.post('/jserror', logOkPostCombinator("CLIENT SIDE JAVASCRIPT ERROR", "error", "errorInfo")); - //serve index.html under / - app.get('/', function(req, res) - { - return sendStatic(path, res, "index.html"); - }); - - //serve robots.txt - app.get('/robots.txt', function(req, res) - { - return sendStatic(path, res, "robots.txt"); - }); - - //serve favicon.ico - app.get('/favicon.ico', function(req, res) - { - return sendStatic(path, res, "custom/favicon.ico", - function(err){ - //there is no custom favicon, send the default favicon - if(err) - { - filePath = path.normalize(__dirname + "/../static/favicon.ico"); - res.sendfile(filePath, { maxAge: exports.maxAge }); - } - }); - }); - additionalSetup(); From c16d3a0f67eaf5bd450eaca776e253425616af37 Mon Sep 17 00:00:00 2001 From: Montana Scott Rowe Date: Thu, 19 Jan 2012 17:20:26 -0600 Subject: [PATCH 18/23] moved GETs into an object literal --- node/server.js | 108 +++++++++++++++++++++++++++---------------------- 1 file changed, 60 insertions(+), 48 deletions(-) diff --git a/node/server.js b/node/server.js index 585972b35..953e5423e 100644 --- a/node/server.js +++ b/node/server.js @@ -502,66 +502,75 @@ function init(additionalSetup){ - var gets = {}; + var gets = { //serve static files - gets['/static/*'] = getStatic; + '/static/*': getStatic, + //serve minified files - gets['/minified/:id'] = getMinified; + '/minified/:id': getMinified, + //serve read only pad - gets['/ro/:id'] = getRoCombinator( - serverName, - {ro: readOnlyManager}, - hasPadAccess, - ERR, - exporthtml - ); + '/ro/:id': getRoCombinator( + serverName, + {ro: readOnlyManager}, + hasPadAccess, + ERR, + exporthtml + ), + //serve pad.html under /p - gets['/p/:pad'] = sendStaticIfPad( - goToPad, - padManager, - path, - "pad.html" - ); + '/p/:pad': sendStaticIfPad( + goToPad, + padManager, + path, + "pad.html" + ) + //serve timeslider.html under /p/$padname/timeslider - gets['/p/:pad/timeslider'] = sendStaticIfPad( - goToPad, padManager, path, "timeslider.html" - ); + '/p/:pad/timeslider': sendStaticIfPad( + goToPad, padManager, path, "timeslider.html" + ), //serve timeslider.html under /p/$padname/timeslider //the above comment is wrong - gets['/p/:pad/:rev?/export/:type'] = getExportPadCombinator( - goToPad, settings, hasPadAccess, exportHandler, serverName - ); + '/p/:pad/:rev?/export/:type': getExportPadCombinator( + goToPad, settings, hasPadAccess, exportHandler, serverName + ), + //This is a api GET call, collect all post informations and pass it to the apiHandler - gets['/api/1/:func'] = function(req, res) - { - apiCaller(req, res, req.query) - } + '/api/1/:func': function(req, res) + { + apiCaller(req, res, req.query) + }, + //serve index.html under / - gets['/'] = function(req, res) - { - return sendStatic(path, res, "index.html"); - } + '/': function(req, res) + { + return sendStatic(path, res, "index.html"); + }, + //serve robots.txt - gets['/robots.txt'] = function(req, res) - { - return sendStatic(path, res, "robots.txt"); - } + '/robots.txt': function(req, res) + { + return sendStatic(path, res, "robots.txt"); + }, + //serve favicon.ico - gets['/favicon.ico'] = function(req, res) - { - return sendStatic(path, res, "custom/favicon.ico", - function(err){ - //there is no custom favicon, send the default favicon - if(err) - { - filePath = path.normalize(__dirname + "/../static/favicon.ico"); - res.sendfile(filePath, { maxAge: exports.maxAge }); - } - }); + '/favicon.ico': function(req, res) + { + return sendStatic(path, res, "custom/favicon.ico", + function(err){ + //there is no custom favicon, send the default favicon + if(err) + { + filePath = path.normalize(__dirname + "/../static/favicon.ico"); + res.sendfile(filePath, { maxAge: exports.maxAge }); + } + }); + } }; - - for(var key in gets) app.get(key, gets[key]); + + var posts = {}; //handle import requests app.post('/p/:pad/import', postImportPadCombinator(goToPad, settings, serverName, hasPadAccess, importHandler)); @@ -581,7 +590,10 @@ function init(additionalSetup){ app.post('/jserror', logOkPostCombinator("CLIENT SIDE JAVASCRIPT ERROR", "error", "errorInfo")); - additionalSetup(); + additionalSetup(app, gets); + + + for(var key in gets) app.get(key, gets[key]); //let the server listen app.listen(settings.port, settings.ip); From 7e522eaf85c2dea7443fa75ad8b1b1b972d3aa11 Mon Sep 17 00:00:00 2001 From: Montana Scott Rowe Date: Thu, 19 Jan 2012 17:28:45 -0600 Subject: [PATCH 19/23] put POSTs into an object literal --- node/server.js | 54 +++++++++++++++++++++----------------------------- 1 file changed, 23 insertions(+), 31 deletions(-) diff --git a/node/server.js b/node/server.js index 953e5423e..aa6e52fa6 100644 --- a/node/server.js +++ b/node/server.js @@ -459,7 +459,8 @@ function init(additionalSetup){ { //create server var app = express.createServer(); - + + //load modules that needs a initalized db readOnlyManager = require("./db/ReadOnlyManager"); exporthtml = require("./utils/ExportHtml"); @@ -469,7 +470,17 @@ function init(additionalSetup){ padManager = require('./db/PadManager'); securityManager = require('./db/SecurityManager'); socketIORouter = require("./handler/SocketIORouter"); - + + var managers = { + ro: readOnlyManager, + security: securityManager + }; + var handlers = { + "export": exportHandler, + "import": importHandler, + api: apiHandler + }; + //install logging var httpLogger = log4js.getLogger("http"); var apiLogger = log4js.getLogger("API"); @@ -503,13 +514,8 @@ function init(additionalSetup){ var gets = { - //serve static files '/static/*': getStatic, - - //serve minified files '/minified/:id': getMinified, - - //serve read only pad '/ro/:id': getRoCombinator( serverName, {ro: readOnlyManager}, @@ -517,45 +523,29 @@ function init(additionalSetup){ ERR, exporthtml ), - - //serve pad.html under /p '/p/:pad': sendStaticIfPad( goToPad, padManager, path, "pad.html" ) - - //serve timeslider.html under /p/$padname/timeslider '/p/:pad/timeslider': sendStaticIfPad( goToPad, padManager, path, "timeslider.html" ), - - //serve timeslider.html under /p/$padname/timeslider - //the above comment is wrong '/p/:pad/:rev?/export/:type': getExportPadCombinator( goToPad, settings, hasPadAccess, exportHandler, serverName - ), - - //This is a api GET call, collect all post informations and pass it to the apiHandler '/api/1/:func': function(req, res) { apiCaller(req, res, req.query) }, - - //serve index.html under / '/': function(req, res) { return sendStatic(path, res, "index.html"); }, - - //serve robots.txt '/robots.txt': function(req, res) { return sendStatic(path, res, "robots.txt"); }, - - //serve favicon.ico '/favicon.ico': function(req, res) { return sendStatic(path, res, "custom/favicon.ico", @@ -570,30 +560,32 @@ function init(additionalSetup){ } }; - var posts = {}; + var posts = { //handle import requests - app.post('/p/:pad/import', postImportPadCombinator(goToPad, settings, serverName, hasPadAccess, importHandler)); + '/p/:pad/import': postImportPadCombinator(goToPad, settings, serverName, hasPadAccess, importHandler) //This is a api POST call, collect all post informations and pass it to the apiHandler - app.post('/api/1/:func', function(req, res) + '/api/1/:func': function(req, res) { new formidable.IncomingForm().parse(req, function(err, fields, files) { apiCaller(req, res, fields) }); - }); + }, //The Etherpad client side sends information about how a disconnect happen - app.post('/ep/pad/connection-diagnostic-info', logOkPostCombinator("DIAGNOSTIC-INFO", "log", "diagnosticInfo")); + '/ep/pad/connection-diagnostic-info': logOkPostCombinator("DIAGNOSTIC-INFO", "log", "diagnosticInfo"), //The Etherpad client side sends information about client side javscript errors - app.post('/jserror', logOkPostCombinator("CLIENT SIDE JAVASCRIPT ERROR", "error", "errorInfo")); + '/jserror': logOkPostCombinator("CLIENT SIDE JAVASCRIPT ERROR", "error", "errorInfo") + }; - additionalSetup(app, gets); + additionalSetup(app, gets, posts); for(var key in gets) app.get(key, gets[key]); + for(var key in posts) app.post(key, posts[key]); //let the server listen app.listen(settings.port, settings.ip); @@ -610,4 +602,4 @@ function init(additionalSetup){ } this.init = init; -init(function(){}); \ No newline at end of file +init(function(app, gets, posts){}); \ No newline at end of file From a7d9e6b9a39132c6fe8a628d77e7d47a1b42f7de Mon Sep 17 00:00:00 2001 From: Montana Scott Rowe Date: Thu, 19 Jan 2012 17:36:56 -0600 Subject: [PATCH 20/23] checked to make sure init is passed a function --- node/server.js | 44 ++++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/node/server.js b/node/server.js index aa6e52fa6..db8e72995 100644 --- a/node/server.js +++ b/node/server.js @@ -451,6 +451,8 @@ function gotoPadCombinator(checkPadName, padManager){ } function init(additionalSetup){ + if("function" != typeof additionalSetup) + additionalSetup = function(){}; async.waterfall([ //initalize the database setupDb, @@ -485,6 +487,7 @@ function init(additionalSetup){ var httpLogger = log4js.getLogger("http"); var apiLogger = log4js.getLogger("API"); + //checks for padAccess function hasPadAccess(req, res, callback) { @@ -561,27 +564,28 @@ function init(additionalSetup){ }; var posts = { - - //handle import requests - '/p/:pad/import': postImportPadCombinator(goToPad, settings, serverName, hasPadAccess, importHandler) - //This is a api POST call, collect all post informations and pass it to the apiHandler - '/api/1/:func': function(req, res) - { - new formidable.IncomingForm().parse(req, function(err, fields, files) - { - apiCaller(req, res, fields) - }); - }, - - //The Etherpad client side sends information about how a disconnect happen - '/ep/pad/connection-diagnostic-info': logOkPostCombinator("DIAGNOSTIC-INFO", "log", "diagnosticInfo"), - - //The Etherpad client side sends information about client side javscript errors - '/jserror': logOkPostCombinator("CLIENT SIDE JAVASCRIPT ERROR", "error", "errorInfo") + '/p/:pad/import': postImportPadCombinator( + goToPad, settings, serverName, hasPadAccess, importHandler + ), + '/api/1/:func': function(req, res){ + new formidable.IncomingForm().parse( + req, + function(err, fields, files) + { + apiCaller(req, res, fields) + }); + }, + '/ep/pad/connection-diagnostic-info': logOkPostCombinator( + "DIAGNOSTIC-INFO", "log", "diagnosticInfo" + ), + '/jserror': logOkPostCombinator( + "CLIENT SIDE JAVASCRIPT ERROR", "error", "errorInfo" + ) }; - + + - additionalSetup(app, gets, posts); + additionalSetup(app, gets, posts, managers, handlers, db); for(var key in gets) app.get(key, gets[key]); @@ -602,4 +606,4 @@ function init(additionalSetup){ } this.init = init; -init(function(app, gets, posts){}); \ No newline at end of file +init(function(app, gets, posts, managers, handlers, db){}); \ No newline at end of file From 2b8af11405149e4d3e3490d1f5d16068f95adaa0 Mon Sep 17 00:00:00 2001 From: Montana Scott Rowe Date: Thu, 19 Jan 2012 17:46:55 -0600 Subject: [PATCH 21/23] moved server into a module and actual invocation out of it --- bin/run.sh | 2 +- node/serve.js | 24 ++++++++++++++++++++++++ node/server.js | 4 ++-- 3 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 node/serve.js diff --git a/bin/run.sh b/bin/run.sh index a5245ff77..878e0a55c 100755 --- a/bin/run.sh +++ b/bin/run.sh @@ -26,4 +26,4 @@ bin/installDeps.sh || exit 1 #Move to the node folder and start echo "start..." cd "node" -node server.js +node serve.js diff --git a/node/serve.js b/node/serve.js new file mode 100644 index 000000000..12bbae66f --- /dev/null +++ b/node/serve.js @@ -0,0 +1,24 @@ +/** + * This module is started with bin/run.sh. It sets up a Express HTTP and a Socket.IO Server. + * Static file Requests are answered directly from the server module, Socket.IO messages are passed + * to MessageHandler and minfied requests are passed to minified. + */ + +/* + * derived from 2011 Peter 'Pita' Martischka (Primary Technology Ltd) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS-IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +require("./server").init(function(app, gets, posts, managers, handlers, db){}); \ No newline at end of file diff --git a/node/server.js b/node/server.js index db8e72995..4939075a7 100644 --- a/node/server.js +++ b/node/server.js @@ -1,5 +1,5 @@ /** - * This module is started with bin/run.sh. It sets up a Express HTTP and a Socket.IO Server. + * This module is included by serve.js, which bin/run.sh invokes. It sets up a Express HTTP and a Socket.IO Server. * Static file Requests are answered directly from this module, Socket.IO messages are passed * to MessageHandler and minfied requests are passed to minified. */ @@ -605,5 +605,5 @@ function init(additionalSetup){ } -this.init = init; + init(function(app, gets, posts, managers, handlers, db){}); \ No newline at end of file From 57908fcb16d7b0816603c33aed9b82b981af275f Mon Sep 17 00:00:00 2001 From: Montana Scott Rowe Date: Thu, 19 Jan 2012 17:51:26 -0600 Subject: [PATCH 22/23] forgot to modify other scripts --- start.bat | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/start.bat b/start.bat index cf6a60f18..0fe44b3f9 100644 --- a/start.bat +++ b/start.bat @@ -1,2 +1,2 @@ cd node -..\bin\node server.js +..\bin\node serve.js From 95d98f0cd0e78a8f5fc02b1c7804855237985d11 Mon Sep 17 00:00:00 2001 From: Montana Scott Rowe Date: Thu, 19 Jan 2012 17:51:44 -0600 Subject: [PATCH 23/23] forgot to edit other scripts --- bin/debugRun.sh | 2 +- node/README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/debugRun.sh b/bin/debugRun.sh index 01197a6b8..b963744d8 100755 --- a/bin/debugRun.sh +++ b/bin/debugRun.sh @@ -23,7 +23,7 @@ node-inspector & echo "If you are new to node-inspector, take a look at this video: http://youtu.be/AOnK3NVnxL8" cd "node" -node --debug server.js +node --debug serve.js #kill node-inspector before ending kill $! diff --git a/node/README.md b/node/README.md index 031810f6f..bc6ecb168 100644 --- a/node/README.md +++ b/node/README.md @@ -10,4 +10,4 @@ Module file names starts with a capital letter and uses camelCase # Where does it start? -server.js is started directly +serve.js invokes init in server.js