From 227b2cf9dbff24ec548b43626309e502acbdde41 Mon Sep 17 00:00:00 2001 From: Nicolas Leclerc Date: Mon, 30 Jun 2014 23:48:51 +0200 Subject: [PATCH 1/3] Added option to modify querystring sent to actual script. --- README.md | 8 ++++++++ gateway-rewrite.js | 21 ++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ad163ae..9cce493 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,14 @@ var rwGateway = function (dir){ rule: '^/$', cgi: '/usr/local/opt/php54/bin/php-cgi', to: '/index.php' + }, + + /* Use the following options to pass original uri to target script. */ + { + rule: '^.*$', + cgi: '/usr/local/opt/php54/bin/php-cgi', + to: '/index.php', + query: 'uri={{URI}}&{{QUERY}}' } ]} ); diff --git a/gateway-rewrite.js b/gateway-rewrite.js index d96014d..0532c72 100644 --- a/gateway-rewrite.js +++ b/gateway-rewrite.js @@ -42,6 +42,21 @@ module.exports = function gateway_rewrite(docroot, options) { } } + function replaceStringItems(sourceString, items) { + if (!sourceString || !items) + return sourceString || ''; + + var result = sourceString; + + Object.keys(items).forEach(function(key){ + var regexp = new RegExp('\{\{'+key+'\}\}','g'); + result = result.replace(regexp,items[key]); + }); + + result = result.replace(/\{\{.*\}\}/g, ''); // Remove items with no value. + return result; + } + var matches = 0; var url = URL.parse(req.url) @@ -57,6 +72,10 @@ module.exports = function gateway_rewrite(docroot, options) { var file = options.rules[j].to var uri = url.pathname var path = normalize(join(docroot, file)) + var query = replaceStringItems(options.rules[j].query, { + URI: url.pathname, + QUERY: url.query + }); // populate the environment var host = (req.headers.host || '').split(':') @@ -73,7 +92,7 @@ module.exports = function gateway_rewrite(docroot, options) { SCRIPT_FILENAME: path, PATH_TRANSLATED: path, REQUEST_METHOD: req.method, - QUERY_STRING: url.query || '', + QUERY_STRING: query, GATEWAY_INTERFACE: 'CGI/1.1', SERVER_PROTOCOL: 'HTTP/1.1', PATH: process.env.PATH, From b1148238c61d4fda443314864bb89cbdc0e8b937 Mon Sep 17 00:00:00 2001 From: Nicolas Leclerc Date: Wed, 30 Jul 2014 17:52:16 +0200 Subject: [PATCH 2/3] Added support of relative uri in query string declaration. --- README.md | 4 ++-- gateway-rewrite.js | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9cce493..cc355f5 100644 --- a/README.md +++ b/README.md @@ -35,12 +35,12 @@ var rwGateway = function (dir){ to: '/index.php' }, - /* Use the following options to pass original uri to target script. */ + /* Use the following options to pass original uri to target script. Relative URI doesn't have a leading slash. */ { rule: '^.*$', cgi: '/usr/local/opt/php54/bin/php-cgi', to: '/index.php', - query: 'uri={{URI}}&{{QUERY}}' + query: 'uri={{URI}}&relativeUri={{RELATIVE_URI}}&{{QUERY}}' } ]} ); diff --git a/gateway-rewrite.js b/gateway-rewrite.js index 0532c72..6478110 100644 --- a/gateway-rewrite.js +++ b/gateway-rewrite.js @@ -73,6 +73,7 @@ module.exports = function gateway_rewrite(docroot, options) { var uri = url.pathname var path = normalize(join(docroot, file)) var query = replaceStringItems(options.rules[j].query, { + RELATIVE_URI: url.pathname.replace(/^\//,''), URI: url.pathname, QUERY: url.query }); From f1cff6d48e4b149e98c3ac93fa1a291c198160ea Mon Sep 17 00:00:00 2001 From: Nicolas Leclerc Date: Wed, 30 Jul 2014 17:53:09 +0200 Subject: [PATCH 3/3] Fixed 'null' being inserted in querystring when original query is missing. --- gateway-rewrite.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gateway-rewrite.js b/gateway-rewrite.js index 6478110..5c06ba1 100644 --- a/gateway-rewrite.js +++ b/gateway-rewrite.js @@ -75,7 +75,7 @@ module.exports = function gateway_rewrite(docroot, options) { var query = replaceStringItems(options.rules[j].query, { RELATIVE_URI: url.pathname.replace(/^\//,''), URI: url.pathname, - QUERY: url.query + QUERY: url.query || '' }); // populate the environment