diff --git a/node/server.js b/node/server.js index 461e82693..7b82f255e 100644 --- a/node/server.js +++ b/node/server.js @@ -1,6 +1,6 @@ /** - * 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 this module, Socket.IO messages are passed + * 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 this module, Socket.IO messages are passed * to MessageHandler and minfied requests are passed to minified. */ @@ -51,12 +51,12 @@ try version = version.substring(0, 7); console.log("Your Etherpad Lite git version is " + version); } -catch(e) +catch(e) { - console.warn("Can't get git version for server header\n" + e.message) + console.warn("Can't get git version for server header\n" + e.message); } -console.log("Report bugs at https://github.com/Pita/etherpad-lite/issues") +console.log("Report bugs at https://github.com/Pita/etherpad-lite/issues"); var serverName = "Etherpad-Lite " + version + " (http://j.mp/ep-lite)"; @@ -77,7 +77,7 @@ async.waterfall([ { //create server var app = express.createServer(); - + //load modules that needs a initalized db readOnlyManager = require("./db/ReadOnlyManager"); exporthtml = require("./utils/ExportHtml"); @@ -87,43 +87,46 @@ async.waterfall([ padManager = require('./db/PadManager'); securityManager = require('./db/SecurityManager'); socketIORouter = require("./handler/SocketIORouter"); - - //install logging + + //install logging var httpLogger = log4js.getLogger("http"); - app.configure(function() + app.configure(function() { // Activate http basic auth if it has been defined in settings.json - if(settings.httpAuth != null) app.use(basic_auth); + if(settings.httpAuth) { + 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")) + 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.error(function(err, req, res, next){ res.send(500); console.error(err.stack ? err.stack : err.toString()); gracefulShutdown(); }); - + //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 }); }); - + //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); @@ -133,14 +136,15 @@ async.waterfall([ next(); } }); - + //checks for padAccess function hasPadAccess(req, res, callback) { + console.log(req.params); 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") { @@ -163,7 +167,7 @@ async.waterfall([ return; } } - + res.header('WWW-Authenticate', 'Basic realm="Protected Area"'); if (req.headers.authorization) { setTimeout(function () { @@ -173,16 +177,16 @@ async.waterfall([ res.send('Authentication required', 401); } } - + //serve read only pad app.get('/ro/:id', function(req, res) - { + { res.header("Server", serverName); - + var html; var padId; var pad; - + async.series([ //translate the read only pad to a padId function(callback) @@ -190,12 +194,12 @@ async.waterfall([ 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; - + + //we need that to tell hasPadAcess about the pad + req.params.pad = padId; + callback(); }); }, @@ -203,12 +207,12 @@ async.waterfall([ function(callback) { //return if the there is no padId - if(padId == null) + if(!padId) { callback("notfound"); return; } - + hasPadAccess(req, res, function() { //render the html document @@ -225,29 +229,29 @@ async.waterfall([ //throw any unexpected error if(err && err != "notfound") ERR(err); - + if(err == "notfound") res.send('404 - Not Found', 404); else res.send(html); }); }); - + //serve pad.html under /p app.get('/p/:pad', function(req, res, next) - { + { //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); return; } - + res.header("Server", serverName); var filePath = path.normalize(__dirname + "/../static/pad.html"); res.sendfile(filePath, { maxAge: exports.maxAge }); }); - + //serve timeslider.html under /p/$padname/timeslider app.get('/p/:pad/timeslider', function(req, res, next) { @@ -257,12 +261,12 @@ async.waterfall([ res.send('Such a padname is forbidden', 404); return; } - + res.header("Server", serverName); var filePath = path.normalize(__dirname + "/../static/timeslider.html"); res.sendfile(filePath, { maxAge: exports.maxAge }); }); - + //serve timeslider.html under /p/$padname/timeslider app.get('/p/:pad/export/:type', function(req, res, next) { @@ -272,7 +276,7 @@ async.waterfall([ res.send('Such a padname is forbidden', 404); return; } - + 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) @@ -280,24 +284,23 @@ async.waterfall([ 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) + if(!settings.abiword && ["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); }); }); - + //handle import requests app.post('/p/:pad/import', function(req, res, next) { @@ -307,84 +310,84 @@ async.waterfall([ res.send('Such a padname is forbidden', 404); return; } - + //if abiword is disabled, skip handling this request - if(settings.abiword == null) + if(!settings.abiword) { next(); - return; + return; } - + res.header("Server", serverName); - + hasPadAccess(req, res, function() { importHandler.doImport(req, res, req.params.pad); }); }); - + 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); - } - + }; + //This is a api GET call, collect all post informations and pass it to the apiHandler app.get('/api/1/:func', function(req, res) { - apiCaller(req, res, req.query) + apiCaller(req, res, req.query); }); //This is a api POST call, collect all post informations and pass it to the apiHandler app.post('/api/1/:func', function(req, res) { - new formidable.IncomingForm().parse(req, function(err, fields, files) + new formidable.IncomingForm().parse(req, function(err, fields, files) { - apiCaller(req, res, fields) + apiCaller(req, res, fields); }); }); - + //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) - { + new formidable.IncomingForm().parse(req, function(err, fields, files) + { console.log("DIAGNOSTIC-INFO: " + fields.diagnosticInfo); res.end("OK"); }); }); - + //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) - { + new formidable.IncomingForm().parse(req, function(err, fields, files) + { console.error("CLIENT SIDE JAVASCRIPT ERROR: " + fields.errorInfo); res.end("OK"); }); }); - + //serve index.html under / app.get('/', function(req, res) { @@ -392,7 +395,7 @@ async.waterfall([ var filePath = path.normalize(__dirname + "/../static/index.html"); res.sendfile(filePath, { maxAge: exports.maxAge }); }); - + //serve robots.txt app.get('/robots.txt', function(req, res) { @@ -400,7 +403,7 @@ async.waterfall([ var filePath = path.normalize(__dirname + "/../static/robots.txt"); res.sendfile(filePath, { maxAge: exports.maxAge }); }); - + //serve favicon.ico app.get('/favicon.ico', function(req, res) { @@ -416,7 +419,7 @@ async.waterfall([ } }); }); - + //let the server listen app.listen(settings.port, settings.ip); console.log("Server is listening at " + settings.ip + ":" + settings.port); @@ -432,13 +435,13 @@ async.waterfall([ { 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(); @@ -446,14 +449,14 @@ async.waterfall([ 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) @@ -462,22 +465,22 @@ async.waterfall([ //https://github.com/joyent/node/issues/1553 process.on('SIGINT', gracefulShutdown); } - + 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); @@ -489,21 +492,21 @@ async.waterfall([ 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); - - callback(null); + + callback(null); } ]);