From 3ca657ac186e702b373ca37238bb178e7feeab18 Mon Sep 17 00:00:00 2001 From: Abe Petrillo Date: Thu, 18 Feb 2016 12:17:46 -0500 Subject: [PATCH 1/2] Add support for custom request headers --- allowedRequestHeaders.json | 4 ++++ index.js | 14 ++++++++++++-- package.json | 3 ++- 3 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 allowedRequestHeaders.json diff --git a/allowedRequestHeaders.json b/allowedRequestHeaders.json new file mode 100644 index 0000000..f7887d6 --- /dev/null +++ b/allowedRequestHeaders.json @@ -0,0 +1,4 @@ +[ + "Accept", + "Content-Type" +] diff --git a/index.js b/index.js index 9e0b8bf..5458b65 100644 --- a/index.js +++ b/index.js @@ -12,7 +12,9 @@ var http = require('http'), faviconPNG = fsRead('favicon.png') faviconPNGGZip = gzip(faviconPNG) port = process.env.PORT || 8080, - allowedOriginalHeaders = new RegExp('^' + require('./allowedOriginalHeaders.json').join('|'), 'i') + allowedOriginalHeaders = new RegExp('^' + require('./allowedOriginalHeaders.json').join('|'), 'i'), + allowedRequestHeaders = require('./allowedRequestHeaders.json'), + _ = require('lodash'), bannedUrls = new RegExp(require('./bannedUrls.json').join('|'), 'i'), defaultOptions = { gzip:true @@ -78,6 +80,14 @@ var http = require('http'), break; } } + + var toLower = function (str) { + return str.toLowerCase(); + } + //add headers from original request + for ( var header of _.map(allowedRequestHeaders, toLower)) { + opts.headers[header] = req.headers[header] + } return opts }, handler = function handler(req, res) { @@ -110,7 +120,7 @@ var http = require('http'), r.pipefilter = function(response, dest) { for (var header in response.headers) { if (!allowedOriginalHeaders.test(header)) { - dest.removeHeader(header); + dest.removeHeader(header); } if (options.flags.gzip === true && header === 'content-encoding') dest.setHeader('content-encoding', response.headers[header]) } diff --git a/package.json b/package.json index 67afca0..aaeb153 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,8 @@ }, "license": "MIT", "dependencies": { - "request": "^2.40.0" + "request": "^2.40.0", + "lodash": "latest" }, "keywords": [ "cors", From aaa2b964fac039e363e1163e66175be11238f90b Mon Sep 17 00:00:00 2001 From: Abe Petrillo Date: Tue, 23 Feb 2016 16:54:58 -0500 Subject: [PATCH 2/2] remove lodash dependency --- index.js | 3 +-- package.json | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 5458b65..1f631ff 100644 --- a/index.js +++ b/index.js @@ -14,7 +14,6 @@ var http = require('http'), port = process.env.PORT || 8080, allowedOriginalHeaders = new RegExp('^' + require('./allowedOriginalHeaders.json').join('|'), 'i'), allowedRequestHeaders = require('./allowedRequestHeaders.json'), - _ = require('lodash'), bannedUrls = new RegExp(require('./bannedUrls.json').join('|'), 'i'), defaultOptions = { gzip:true @@ -85,7 +84,7 @@ var http = require('http'), return str.toLowerCase(); } //add headers from original request - for ( var header of _.map(allowedRequestHeaders, toLower)) { + for ( var header of allowedRequestHeaders.map(toLower)) { opts.headers[header] = req.headers[header] } return opts diff --git a/package.json b/package.json index aaeb153..67afca0 100644 --- a/package.json +++ b/package.json @@ -8,8 +8,7 @@ }, "license": "MIT", "dependencies": { - "request": "^2.40.0", - "lodash": "latest" + "request": "^2.40.0" }, "keywords": [ "cors",