Skip to content

Instantly share code, notes, and snippets.

@mrajagopal
Created October 28, 2014 22:43
Show Gist options
  • Save mrajagopal/777cbdf25df93facc254 to your computer and use it in GitHub Desktop.
Save mrajagopal/777cbdf25df93facc254 to your computer and use it in GitHub Desktop.
//********************************************************************************************************************************
// Description: HTTP redirection in a forward-proxy context. A forward-proxy is useful for filter connections
// particularly outbound to the internet for clients sitting behind a firewall for example.
// In this example below, any user HTTP request from clients results in a redirection
// to the specified site (in this case a bland example.com site). More sophistication can be added
// like a login page for authenticating outbound/internet access and other IT policy enforcement.
"use strict";
var fpm = require('lrs/forwardProxyModule');
var redirectHost = 'www.example.com';
var redisLib = require("redis");
var redisClient = redisLib.createClient();
// Handler for client requests - this pulls out the host field value
function onClientReq(servReq, servResp, next) {
// extract the full URL request from the header
var requestedHost = servReq.headers.host || '';
var query = url.parse(servReq.url)
// Test if the URL request matches the redirection.
// Forward the request along on such a match thereby preventing
// a redirect loop.
if(requestedHost == redirectHost) {
next();
}
else {
servResp.setHeader('Content-Type', 'text/html');
servResp.writeHead(301, {'Location' : 'http://' + redirectHost, 'Server' : 'LineRate'});
servResp.end();
next();
}
}
var onExists = function(fp) {
fp.on('request', onClientReq);
};
redisClient.set("string key", "string val", redisLib.print);
redisClient.hset("hash key", "hashtest 1", "some value", redisLib.print);
redisClient.hset(["hash key", "hashtest 2", "some other value"], redisLib.print);
fpm.on('exist', 'fp1', onExists);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment