* Build the index.html file\r
*/\r
var build_time = new Date().getTime();\r
+var base_path = config.get().http_base_path || '';\r
+\r
+// Trim off any trailing slashes\r
+if (base_path.substr(base_path.length - 1) === '/') {\r
+ base_path = base_path.substr(0, base_path.length - 1);\r
+}\r
\r
var index_src = fs.readFileSync(__dirname + '/src/index.html.tmpl', FILE_ENCODING)\r
- .replace(new RegExp('<%base_path%>', 'g'), config.get().http_base_path || '')\r
+ .replace(new RegExp('<%base_path%>', 'g'), base_path)\r
.replace(new RegExp('<%build_version%>', 'g'), package_json.version)\r
.replace(new RegExp('<%build_time%>', 'g'), build_time);\r
- \r
+\r
fs.writeFile(__dirname + '/index.html', index_src, { encoding: FILE_ENCODING }, function (err) {\r
if (!err) {\r
console.log('Built index.html');\r
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';
}
});
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');