Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Cleaned up and reorganised code.

Looking much better thanks to Logan's advice.
  • Loading branch information...
commit 8c5a5fedcac585661626477c79a54b8b42605d91 1 parent 5b76a84
@tgetgood tgetgood authored
View
4 README.md
@@ -33,7 +33,7 @@ a working proxy.
``` js
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
@@ -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.
-### scriptInjector.js
+## scriptInjector.js
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
View
24 solrProxy.js
@@ -8,8 +8,8 @@
// Info:
// ============================================================================
// Created: 09/01/2012
-// Last modified: 19/01/2012
-// Version: 0.0.?
+// Last modified: 07/02/2012
+// Version: 0.0.2
// Author: Thomas Getgood <thomas@evolvingweb.ca>
// Available: https://github.com/evolvingweb/node-proxies.git
//
@@ -36,9 +36,9 @@ var httpProxy = require('http-proxy'),
url = require('url'),
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.
@@ -72,6 +72,8 @@ exports.createProxy = function(proxyPort, proxyOptions, verifyCallback) {
var data = '';
// 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') {
@@ -87,15 +89,21 @@ exports.createProxy = function(proxyPort, proxyOptions, verifyCallback) {
else if (req.method === 'POST') {
// TODO: forbid if req.headers['content-length'] is too big;
// What's too big?
+ req.setEncoding('utf8');
req.on('data', function(chunk) {
data += chunk;
});
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;
query = url.parse('?' + args.query, true).query;
-
- // TODO: Once we get the handler business figured out, fix this.
- vetQuery(query, undefined);
+ console.log(query);
+ handler = args.handler;
+ vetQuery(query, handler);
});
}
else if (req.method === 'GET') {
@@ -108,7 +116,5 @@ exports.createProxy = function(proxyPort, proxyOptions, verifyCallback) {
else {
forbid();
}
- }).listen(proxyPort, function () {
- console.log("Proxy ready.");
});
};
View
9 solrProxyExample.js
@@ -6,8 +6,8 @@
// Proxies Solr running on port 8080 (default Tomcat port) through port 8008 on
// the same host.
-var solrProxy = require('./solrProxy.js');
-var qf = require('./solrQueryFilter.js');
+var solrProxy = require('./solrProxy');
+var queryFunction = require('./solrQueryFilter');
// These specify where the proxy routes to.
var proxyOptions = {
@@ -15,5 +15,8 @@ var proxyOptions = {
port: 8080
};
-solrProxy.createProxy(8008, proxyOptions, qf.verify);
+solrProxy.createProxy(proxyOptions, queryFunction).
+ listen(8008, function() {
+ console.log("Proxy ready.");
+});
View
68 solrQueryFilter.js
@@ -35,46 +35,46 @@
// probably better off using separate cores and keeping one public and one
// 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: {
- qt: function() {return false;}
- },
- handlers: {
- '/solr/select': function(req, query) {
- for (var param in query) {
- if (!query.hasOwnProperty(param)) {
- continue;
- }
+/*
+ * Special processing for certain filters.
+ */
+var filters = {
+ qt: function() {return false;}
+};
+
+/*
+ * 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 (!self.filters[q[0]](req, query[param], q.slice(1))) {
- return false;
- }
+ if (typeof(filters[q[0]]) === 'function') {
+ if (!filters[q[0]](req, query[param], q.slice(1))) {
+ 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;
Please sign in to comment.
Something went wrong with that request. Please try again.