Permalink
Browse files

server: optionally enforce vhost

  • Loading branch information...
1 parent 019cf30 commit 68366316bd93f3a46d29f9791159f20a3478e817 @astro astro committed Feb 17, 2011
Showing with 15 additions and 0 deletions.
  1. +15 −0 server.js
View
@@ -6,6 +6,16 @@ var sys = require('sys');
var MAX_FILE_SIZE = 100 * 1024 * 1024;
+function enforceVhost(vhost) {
+ return function(req, res, next) {
+ if (!req.headers['host'] || req.headers['host'] === vhost)
+ return next();
+
+ res.writeHead(302, { 'Location': 'http://' + vhost + '/' });
+ res.end();
+ };
+}
+
function app(app) {
var HTML_HEADERS = { 'Content-Type': 'text/html; charset=UTF-8' };
app.get('/', function(req, res) {
@@ -100,8 +110,13 @@ function fileDownload(req, res, next) {
next();
}
+var optionalVhostEnforce = process.env.VHOST ?
+ enforceVhost(process.env.VHOST) :
+ function(req, res, next) { return next; };
+
connect.createServer(
connect.logger(),
+ optionalVhostEnforce,
connect.router(app),
fileDownload,
connect.staticProvider(__dirname + '/public'),

0 comments on commit 6836631

Please sign in to comment.