Identd server tweaks
[KiwiIRC.git] / server / httphandler.js
1 var url = require('url'),
2 node_static = require ('node-static');
3
4
5
6 var HttpHandler = function (config) {
7 var public_html = config.public_html || 'client/';
8 this.file_server = new node_static.Server(public_html);
9 };
10
11 module.exports.HttpHandler = HttpHandler;
12
13
14
15 HttpHandler.prototype.serve = function (request, response) {
16 // The incoming requests base path (ie. /kiwiclient)
17 var base_path = global.config.http_base_path || '/kiwi',
18 base_path_regex;
19
20 // Trim of any trailing slashes
21 if (base_path.substr(base_path.length - 1) === '/') {
22 base_path = base_path.substr(0, base_path.length - 1);
23 }
24
25 // Build the regex to match the base_path
26 base_path_regex = base_path.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&');
27
28 // Any asset request to head into the asset dir
29 request.url = request.url.replace(base_path + '/assets/', '/assets/');
30
31 // Any requests for /client to load the index file
32 if (request.url.match(new RegExp('^' + base_path_regex + '([/$]|$)', 'i'))) {
33 request.url = '/';
34 }
35
36
37 this.file_server.serve(request, response, function (err) {
38 if (err) {
39 response.writeHead(err.status, err.headers);
40 response.end();
41 }
42 });
43 };