Skip to content

Commit

Permalink
Cleaned up and reorganised code.
Browse files Browse the repository at this point in the history
Looking much better thanks to Logan's advice.
  • Loading branch information
tgetgood committed Feb 7, 2012
1 parent 5b76a84 commit 8c5a5fe
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 48 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -33,7 +33,7 @@ a working proxy.
``` js ``` js
var solrProxy = require('PATH/solrProxy'); var solrProxy = require('PATH/solrProxy');


solrProxy.createProxy(proxyPort, proxyOptions, queryFilter); solrProxy.createProxy(proxyOptions, queryFilter).listen(4321);
``` ```


proxyOptions is an object which defines how the proxy behaves --- including proxyOptions is an object which defines how the proxy behaves --- including
Expand All @@ -58,7 +58,7 @@ just the parsed query, and the handler to which the query was sent.


See solrQueryFilter.js for a more complete example. See solrQueryFilter.js for a more complete example.


### scriptInjector.js ## scriptInjector.js


A proxy that allows you to inject arbitrary javascript into a webpage by A proxy that allows you to inject arbitrary javascript into a webpage by
passing it through a proxy. Most useful when combined with the following passing it through a proxy. Most useful when combined with the following
Expand Down
24 changes: 15 additions & 9 deletions solrProxy.js
Expand Up @@ -8,8 +8,8 @@
// Info: // Info:
// ============================================================================ // ============================================================================
// Created: 09/01/2012 // Created: 09/01/2012
// Last modified: 19/01/2012 // Last modified: 07/02/2012
// Version: 0.0.? // Version: 0.0.2
// Author: Thomas Getgood <thomas@evolvingweb.ca> // Author: Thomas Getgood <thomas@evolvingweb.ca>
// Available: https://github.com/evolvingweb/node-proxies.git // Available: https://github.com/evolvingweb/node-proxies.git
// //
Expand All @@ -36,9 +36,9 @@ var httpProxy = require('http-proxy'),
url = require('url'), url = require('url'),
qs = require('querystring'); qs = require('querystring');


exports.createProxy = function(proxyPort, proxyOptions, verifyCallback) { exports.createProxy = function(proxyOptions, verifyCallback) {


httpProxy.createServer(function(req, res, proxy) { return httpProxy.createServer(function(req, res, proxy) {


/* /*
* Generic access denied. * Generic access denied.
Expand Down Expand Up @@ -72,6 +72,8 @@ exports.createProxy = function(proxyPort, proxyOptions, verifyCallback) {
var data = ''; var data = '';


// Server logic // Server logic
// The POST handling is a bit of a mess and not really necessary. It may be
// removed in the future.
// ========================================================================== // ==========================================================================


if (req.method === 'OPTIONS') { if (req.method === 'OPTIONS') {
Expand All @@ -87,15 +89,21 @@ exports.createProxy = function(proxyPort, proxyOptions, verifyCallback) {
else if (req.method === 'POST') { else if (req.method === 'POST') {
// TODO: forbid if req.headers['content-length'] is too big; // TODO: forbid if req.headers['content-length'] is too big;
// What's too big? // What's too big?
req.setEncoding('utf8');
req.on('data', function(chunk) { req.on('data', function(chunk) {
data += chunk; data += chunk;
}); });
req.on('end', function() { req.on('end', function() {
// N.B. This will need to be changed depending on how data is going to
// be formatted in POST requests. As it is, it assumes a URL encoding.
// JSON would be more sensible.

//TODO: Why does queryString.parse fail to parse this?
var args = url.parse('?' + data, true).query; var args = url.parse('?' + data, true).query;
query = url.parse('?' + args.query, true).query; query = url.parse('?' + args.query, true).query;

console.log(query);
// TODO: Once we get the handler business figured out, fix this. handler = args.handler;
vetQuery(query, undefined); vetQuery(query, handler);
}); });
} }
else if (req.method === 'GET') { else if (req.method === 'GET') {
Expand All @@ -108,7 +116,5 @@ exports.createProxy = function(proxyPort, proxyOptions, verifyCallback) {
else { else {
forbid(); forbid();
} }
}).listen(proxyPort, function () {
console.log("Proxy ready.");
}); });
}; };
9 changes: 6 additions & 3 deletions solrProxyExample.js
Expand Up @@ -6,14 +6,17 @@
// Proxies Solr running on port 8080 (default Tomcat port) through port 8008 on // Proxies Solr running on port 8080 (default Tomcat port) through port 8008 on
// the same host. // the same host.


var solrProxy = require('./solrProxy.js'); var solrProxy = require('./solrProxy');
var qf = require('./solrQueryFilter.js'); var queryFunction = require('./solrQueryFilter');


// These specify where the proxy routes to. // These specify where the proxy routes to.
var proxyOptions = { var proxyOptions = {
host: 'localhost', host: 'localhost',
port: 8080 port: 8080
}; };


solrProxy.createProxy(8008, proxyOptions, qf.verify); solrProxy.createProxy(proxyOptions, queryFunction).
listen(8008, function() {
console.log("Proxy ready.");
});


68 changes: 34 additions & 34 deletions solrQueryFilter.js
Expand Up @@ -35,46 +35,46 @@
// probably better off using separate cores and keeping one public and one // probably better off using separate cores and keeping one public and one
// entirely private though). // entirely private though).
// //
//
// Note on solrURL vs. proxyURL
// ============================================================================
//
// This filter requires a handler to be explicitely defined and at present, if
// ajax-solr is configured with the proxyUrl option, then no handler
// information is passed. Thus this filter will deny all requests. You could
// assume '/solr/select' if you so desire in your own filter to bypass this
// problem.


var self = { /*
filters: { * Special processing for certain filters.
qt: function() {return false;} */
}, var filters = {
handlers: { qt: function() {return false;}
'/solr/select': function(req, query) { };
for (var param in query) {
if (!query.hasOwnProperty(param)) { /*
continue; * Define callbacks for the handlers we want to deal with (those left out get
} * rejected automatically below).
*/
var handlers = {
'/solr/select': function(req, query) {
for (var param in query) {
if (!query.hasOwnProperty(param)) {
continue;
}


var q = param.split('.'); var q = param.split('.');


if (self.filters[q[0]] !== undefined && typeof(self.filters[q[0]]) === 'function') { if (typeof(filters[q[0]]) === 'function') {
if (!self.filters[q[0]](req, query[param], q.slice(1))) { if (!filters[q[0]](req, query[param], q.slice(1))) {
return false; return false;
}
} }
} }
return true;
}
},
verify: function(req, query, handler) {
if (self.handlers[handler] !== undefined && typeof(self.handlers[handler]) === 'function') {
return self.handlers[handler](req, query);
}
else {
return false;
} }
return true;
}
};

/*
* Query filter function.
*/
module.exports = function(req, query, handler) {
if (typeof(handlers[handler]) === 'function') {
return handlers[handler](req, query);
}
else {
return false;
} }
}; };


exports.verify = self.verify;

0 comments on commit 8c5a5fe

Please sign in to comment.