From: Darren Date: Thu, 4 Sep 2014 12:14:41 +0000 (+0100) Subject: Cleaner HTTP routing; Support empty http_base_pase configs X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=ba317e8717bf4857bf633c0e38bba3d82a2fb130;p=KiwiIRC.git Cleaner HTTP routing; Support empty http_base_pase configs --- diff --git a/client/build.js b/client/build.js index 3f1b887..baf3461 100644 --- a/client/build.js +++ b/client/build.js @@ -223,12 +223,18 @@ fs.readdir(__dirname + '/src/translations', function (err, translation_files) { * Build the index.html file */ var build_time = new Date().getTime(); +var base_path = config.get().http_base_path || ''; + +// Trim off any trailing slashes +if (base_path.substr(base_path.length - 1) === '/') { + base_path = base_path.substr(0, base_path.length - 1); +} var index_src = fs.readFileSync(__dirname + '/src/index.html.tmpl', FILE_ENCODING) - .replace(new RegExp('<%base_path%>', 'g'), config.get().http_base_path || '') + .replace(new RegExp('<%base_path%>', 'g'), base_path) .replace(new RegExp('<%build_version%>', 'g'), package_json.version) .replace(new RegExp('<%build_time%>', 'g'), build_time); - + fs.writeFile(__dirname + '/index.html', index_src, { encoding: FILE_ENCODING }, function (err) { if (!err) { console.log('Built index.html'); diff --git a/server/httphandler.js b/server/httphandler.js index c3fc067..c55dc57 100644 --- a/server/httphandler.js +++ b/server/httphandler.js @@ -22,21 +22,43 @@ module.exports.HttpHandler = HttpHandler; HttpHandler.prototype.serve = function (request, response) { // The incoming requests base path (ie. /kiwiclient) - var base_path = global.config.http_base_path || '', - whitelisted_folders = ['assets', 'src']; + var base_path, base_check, + whitelisted_folders = ['/assets', '/src'], + is_whitelisted_folder = false; - // Trim off any trailing slashes + // Trim off any trailing slashes from the base_path + base_path = global.config.http_base_path || ''; if (base_path.substr(base_path.length - 1) === '/') { base_path = base_path.substr(0, base_path.length - 1); } + // Normalise the URL to compare against the base_path + base_check = request.url; + if (base_check.substr(base_check.length - 1) !== '/') { + base_check += '/'; + } + + // Normalise the URL we use by removing the base path + if (base_check.indexOf(base_path + '/') === 0) { + request.url = request.url.replace(base_path, ''); + + } else if (base_check !== '/') { + // We don't handle requests outside of the base path and not /, so just 404 + response.writeHead(404); + response.write('Not Found'); + response.end(); + return; + } + // Map any whitelisted folders to the local directories whitelisted_folders.forEach(function(folder) { - request.url = request.url.replace(base_path + '/' + folder + '/', '/' + folder + '/'); + if (request.url.indexOf(folder) === 0) { + is_whitelisted_folder = true; + } }); - // Any requests for /base_path/* to load the index file - if (request.url.toLowerCase().indexOf(base_path.toLowerCase()) === 0) { + // Any requests not for whitelisted assets returns the index page + if (!is_whitelisted_folder) { request.url = '/index.html'; } diff --git a/server/weblistener.js b/server/weblistener.js index 475f017..8680cc8 100644 --- a/server/weblistener.js +++ b/server/weblistener.js @@ -85,7 +85,14 @@ var WebListener = module.exports = function (web_config) { }); hs.on('request', function(req, res){ - var transport_url = (global.config.http_base_path || '') + '/transport'; + var base_path = (global.config.http_base_path || ''), + transport_url; + + // Trim off any trailing slashes + if (base_path.substr(base_path.length - 1) === '/') { + base_path = base_path.substr(0, base_path.length - 1); + } + transport_url = base_path + '/transport'; Stats.incr('http.request');