Skip to content

Commit

Permalink
rewritten asynchronous architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasMadsen committed Oct 29, 2011
1 parent f99469c commit d21c718
Showing 1 changed file with 78 additions and 77 deletions.
155 changes: 78 additions & 77 deletions lib/http-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,82 +11,83 @@

(function (global, module, undefined) {

//ECMA 5 strict mode will be used
"use strict";

//Require http and https module
var httpModule = require(process.argv[2] === "http" ? "http" : "https"),

//Require path module
path = require("path"),

//Require file system module
fs = require("fs"),

//Require http-proxy-server module
proxy = require("http-proxy"),

//Require internal plugin
internal = require("./internal"),

//Parse configuration object
config = JSON.parse(process.argv[3]),

//SSL options placeholder
ssl = undefined,

//Create a vhost proxy object
vhost = {};

//Listen for new vhost data
internal.vhost.on("push", function (data) {

data.forEach(function (info) {

vhost[info.vhost] = new proxy.HttpProxy({
target: {
port : info.port,
host : info.host
}
});
//Proxy also www.*
vhost["www." + info.vhost] = vhost[info.vhost];
});
});

//Create a ssl object if this cluster is made for https
if (process.argv[2] === "https") {
ssl = {
key: fs.readFileSync(path.join(config.ssl, './key.pem'), 'utf8'),
cert: fs.readFileSync(path.join(config.ssl, './cert.pem'), 'utf8')
};
}

//Proxy HTTP(S) requests
exports = module.exports = httpModule.createServer(function (req, res) {

console.log("got request");

var host = req.headers.host;
host = (host.indexOf(":") === -1 ? host : host.substr(0, host.indexOf(":")));

//Store the host header
req.headers['x-forwarded-host'] = req.headers.host;

//If virtual host is missing destroy the connection
if (vhost[host] === undefined) {
res.connection.destroy();
}
//Proxy the main request
else {
vhost[host].proxyRequest(req, res);
}
});
//ECMA 5 strict mode will be used
"use strict";

//Require http and https module
var httpModule = require(process.argv[2] === "http" ? "http" : "https"),

//Require native modules
path = require("path"),
fs = require("fs"),

//Require http-proxy-server module
proxy = require("http-proxy"),

//Require internal plugin
internal = require("./internal"),

//Parse configuration object
config = JSON.parse(process.argv[3]),

//SSL options placeholder
ssl = undefined,

//Create a vhost proxy object
vhost = {};

//Listen for new vhost data
internal.vhost.on("push", function (data, configurationDone) {

data.forEach(function (info) {

vhost[info.vhost] = new proxy.HttpProxy({
target: {
port : info.port,
host : info.host
}
});
//Proxy also www.*
vhost["www." + info.vhost] = vhost[info.vhost];
});

//Run configurationDone since there are nothing more to do
configurationDone();
});

//Create a ssl object if this cluster is made for https
if (process.argv[2] === "https") {
ssl = {
key: fs.readFileSync(path.join(config.ssl, './key.pem'), 'utf8'),
cert: fs.readFileSync(path.join(config.ssl, './cert.pem'), 'utf8')
};
}

//Proxy HTTP(S) requests
exports = module.exports = httpModule.createServer(function (req, res) {

console.log("got request");

var host = req.headers.host;
host = (host.indexOf(":") === -1 ? host : host.substr(0, host.indexOf(":")));

//Store the host header
req.headers['x-forwarded-host'] = req.headers.host;

//If virtual host is missing destroy the connection
if (vhost[host] === undefined) {
res.connection.destroy();
}
//Proxy the main request
else {
vhost[host].proxyRequest(req, res);
}
});

exports.on('upgrade', function (req, socket, head) {
//Proxy HTTP Upgrade requests
vhost[req.header.host].proxyWebSocketRequest(req, socket, head);
});
exports.on('upgrade', function (req, socket, head) {
//Proxy HTTP Upgrade requests
vhost[req.header.host].proxyWebSocketRequest(req, socket, head);
});
})(global, module);

0 comments on commit d21c718

Please sign in to comment.