From 7be4045f54c0a21f707514df62512e933f16646b Mon Sep 17 00:00:00 2001 From: achingbrain Date: Wed, 24 Feb 2016 14:33:25 +0000 Subject: [PATCH 01/37] update dependencies --- package.json | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index a4847ab..c9b5916 100644 --- a/package.json +++ b/package.json @@ -15,16 +15,16 @@ "node": ">=4.0.0" }, "dependencies": { - "boom": "2.X.X", - "hoek": "2.X.X", - "joi": "6.X.X", - "wreck": "6.X.X" + "boom": "^3.1.2", + "hoek": "^3.0.4", + "joi": "^8.0.2", + "wreck": "^7.0.0" }, "devDependencies": { "code": "2.X.X", - "hapi": "11.X.X", + "hapi": "^13.0.0", "inert": "3.X.X", - "lab": "7.X.X" + "lab": "^9.1.0" }, "scripts": { "test": "lab -a code -t 100 -L", From 493b172f15f420d61b67cb457142dcfbff3f1d92 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Wed, 24 Feb 2016 14:33:38 +0000 Subject: [PATCH 02/37] fix linting complaints --- test/index.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/index.js b/test/index.js index 674f38c..8c87e17 100755 --- a/test/index.js +++ b/test/index.js @@ -269,6 +269,7 @@ describe('H2o2', () => { const onResponse = function (err, res, request, reply, settings, ttl) { + expect(err).to.be.null(); reply(res).vary('Something'); }; @@ -340,6 +341,7 @@ describe('H2o2', () => { Fs.readFile(__dirname + '/../package.json', { encoding: 'utf8' }, (err, file) => { + expect(err).to.be.null(); Zlib.unzip(res.rawPayload, (err, unzipped) => { expect(err).to.not.exist(); @@ -488,6 +490,7 @@ describe('H2o2', () => { const onResponseWithError = function (err, res, request, reply, settings, ttl) { + expect(err).to.be.null(); reply(Boom.forbidden('Forbidden')); }; @@ -510,6 +513,7 @@ describe('H2o2', () => { const on = function (err, res, request, reply, settings, ttl) { + expect(err).to.be.null(); reply(res).state('a', 'b'); }; @@ -533,6 +537,7 @@ describe('H2o2', () => { const onResponseWithError = function (err, res, request, reply, settings, ttl) { + expect(err).to.be.null(); reply(this.c); }; @@ -565,6 +570,7 @@ describe('H2o2', () => { const onResponseWithError = function (err, res, request, reply, settings, ttl) { + expect(err).to.be.null(); reply(this.c); }; @@ -609,6 +615,7 @@ describe('H2o2', () => { const onResponseWithError = function (err, res, request, reply, settings, ttl) { + expect(err).to.be.null(); reply(this.c); }; @@ -654,6 +661,7 @@ describe('H2o2', () => { const onResponseWithError = function (err, res, request, reply, settings, ttl) { + expect(err).to.be.null(); reply(this.c); }; @@ -738,6 +746,7 @@ describe('H2o2', () => { Wreck.get('http://127.0.0.1:' + server.info.port + '/', (err, res, body) => { + expect(err).to.be.null(); expect(res.statusCode).to.equal(200); const result = JSON.parse(body); @@ -788,6 +797,7 @@ describe('H2o2', () => { Wreck.get('http://127.0.0.1:' + server.info.port + '/', (err, res, body) => { + expect(err).to.be.null(); expect(res.statusCode).to.equal(200); const result = JSON.parse(body); @@ -1403,6 +1413,7 @@ describe('H2o2', () => { const onResponse304 = function (err, res, request, reply, settings, ttl) { + expect(err).to.be.null(); return reply(res).code(304); }; From 8f3c91678f8f332c323a5fc112d6ab5890bcb871 Mon Sep 17 00:00:00 2001 From: Alex Potsides Date: Wed, 24 Feb 2016 15:15:55 +0000 Subject: [PATCH 03/37] Change ^ to X --- package.json | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index c9b5916..c4bad8f 100644 --- a/package.json +++ b/package.json @@ -15,16 +15,16 @@ "node": ">=4.0.0" }, "dependencies": { - "boom": "^3.1.2", - "hoek": "^3.0.4", - "joi": "^8.0.2", - "wreck": "^7.0.0" + "boom": "3.X.X", + "hoek": "3.X.X", + "joi": "8.X.X", + "wreck": "7.X.X" }, "devDependencies": { "code": "2.X.X", - "hapi": "^13.0.0", + "hapi": "13.X.X", "inert": "3.X.X", - "lab": "^9.1.0" + "lab": "9.X.X" }, "scripts": { "test": "lab -a code -t 100 -L", From 2a4e3ed684d3ffb32d23a5f104f03f056066da67 Mon Sep 17 00:00:00 2001 From: Oscar Funes Date: Mon, 29 Feb 2016 19:38:38 -0600 Subject: [PATCH 04/37] 5.1.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c4bad8f..466f813 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "h2o2", "description": "Proxy handler plugin for hapi.js", - "version": "5.0.0", + "version": "5.1.0", "repository": "git://github.com/hapijs/h2o2", "main": "lib/index.js", "keywords": [ From 0fa0255cf4bcfd1fa5a64b778c41d4bf4560cbf0 Mon Sep 17 00:00:00 2001 From: Daniel Bretoi Date: Mon, 14 Mar 2016 16:41:13 -0700 Subject: [PATCH 05/37] style --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index c177b30..3b44da4 100644 --- a/README.md +++ b/README.md @@ -53,13 +53,13 @@ The proxy handler object has the following properties: * `xforward` - if set to `true`, sets the 'X-Forwarded-For', 'X-Forwarded-Port', 'X-Forwarded-Proto' headers when making a request to the proxied upstream endpoint. Defaults to `false`. * `redirects` - the maximum number of HTTP redirections allowed to be followed automatically by the handler. Set to `false` or `0` to disable all redirections (the response will contain the redirection received from the upstream service). If redirections are enabled, no redirections (301, 302, 307, 308) will be passed along to the client, and reaching the maximum allowed redirections will return an error response. Defaults to `false`. * `timeout` - number of milliseconds before aborting the upstream request. Defaults to `180000` (3 minutes). -* `mapUri` - a function used to map the request URI to the proxied URI. Cannot be used together with `host`, `port`, `protocol`, or `uri`. The function signature is `function(request, callback)` where: +* `mapUri` - a function used to map the request URI to the proxied URI. Cannot be used together with `host`, `port`, `protocol`, or `uri`. The function signature is `function (request, callback)` where: * `request` - is the incoming [request object](http://hapijs.com/api#request-object). - * `callback` - is `function(err, uri, headers)` where: + * `callback` - is `function (err, uri, headers)` where: * `err` - internal error condition. * `uri` - the absolute proxy URI. * `headers` - optional object where each key is an HTTP request header and the value is the header content. -* `onResponse` - a custom function for processing the response from the upstream service before sending to the client. Useful for custom error handling of responses from the proxied endpoint or other payload manipulation. Function signature is `function(err, res, request, reply, settings, ttl)` where: +* `onResponse` - a custom function for processing the response from the upstream service before sending to the client. Useful for custom error handling of responses from the proxied endpoint or other payload manipulation. Function signature is `function (err, res, request, reply, settings, ttl)` where: * `err` - internal or upstream error returned from attempting to contact the upstream proxy. * `res` - the node response object received from the upstream service. `res` is a readable stream (use the [wreck](https://github.com/hapijs/wreck) module `read` method to easily convert it to a Buffer or string). * `request` - is the incoming [request object](http://hapijs.com/api#request-object). @@ -143,7 +143,7 @@ server.route({ onResponse: function (err, res, request, reply, settings, ttl) { console.log('receiving the response from the upstream.'); - wreck.read(res, { json: true }, function (err, payload) { + Wreck.read(res, { json: true }, function (err, payload) { console.log('some payload manipulation if you want to.') reply(payload); From 219b85fb125a767143b11171595da58d9e300ac3 Mon Sep 17 00:00:00 2001 From: Oscar Funes Date: Wed, 30 Mar 2016 11:43:11 -0600 Subject: [PATCH 06/37] Fix link closes hapijs/h2o2#27 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3b44da4..ff78e7d 100644 --- a/README.md +++ b/README.md @@ -82,7 +82,7 @@ Proxies the request to an upstream endpoint where: No return value. -The [response flow control rules](#flow-control) **do not** apply. +The [response flow control rules](http://hapijs.com/api#flow-control) **do not** apply. ```js const handler = function (request, reply) { From 9043beeac7b9fb621d49914497183cdec36581b6 Mon Sep 17 00:00:00 2001 From: Oscar Funes Date: Wed, 30 Mar 2016 12:18:43 -0600 Subject: [PATCH 07/37] Update travis yml Update Lab --- .travis.yml | 3 +-- package.json | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index d78e9af..fee5429 100755 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,7 @@ language: node_js node_js: - - "4.0" - "4" - - "5" + - "node" sudo: false diff --git a/package.json b/package.json index 466f813..bcff494 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "code": "2.X.X", "hapi": "13.X.X", "inert": "3.X.X", - "lab": "9.X.X" + "lab": "10.X.X" }, "scripts": { "test": "lab -a code -t 100 -L", From 847f3ca2fc1e2cb151a35336fcc4e3cff0bb3d3d Mon Sep 17 00:00:00 2001 From: Oscar Funes Date: Sun, 3 Apr 2016 13:54:15 -0600 Subject: [PATCH 08/37] Update link reference for options Update typo on reference link --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ff78e7d..086244d 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,7 @@ As one of the handlers for hapi, it is used through the route configuration obje Proxies the request to an upstream endpoint where: - `options` - an object including the same keys and restrictions defined by the - [route `proxy` handler options](#route.config.proxy). + [route `proxy` handler options](#options). No return value. From a32477c75433893323333751cc83bf4427211430 Mon Sep 17 00:00:00 2001 From: Oscar Funes Date: Thu, 7 Apr 2016 10:37:54 -0600 Subject: [PATCH 09/37] closes hapijs/h2o2#30 Update documentation to show how to copy the original response headers to the new response --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 208070c..7548435 100644 --- a/README.md +++ b/README.md @@ -128,7 +128,7 @@ server.route({ wreck.read(res, { json: true }, function (err, payload) { console.log('some payload manipulation if you want to.') - reply(payload); + reply(payload).headers = res.headers; }); } } From b4e5d710becbbae25168c4cff971d3ce16aa6837 Mon Sep 17 00:00:00 2001 From: Adrian Lynch Date: Thu, 19 May 2016 14:29:51 +0100 Subject: [PATCH 10/37] Fix typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d1a193a..e1895c1 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ The proxy handler object has the following properties: * `protocol` - protocol to use when making the request to the proxied host: * 'http' * 'https' -* `uri` - absolute URI used insted of host, port, protocol, path, and query. Cannot be used with `host`, `port`, `protocol`, or `mapUri`. +* `uri` - absolute URI used instead of host, port, protocol, path, and query. Cannot be used with `host`, `port`, `protocol`, or `mapUri`. * `passThrough` - if set to `true`, it forwards the headers from the client to the upstream service, headers sent from the upstream service will also be forwarded to the client. Defaults to `false`. * `localStatePassThrough` - if set to`false`, any locally defined state is removed from incoming requests before being sent to the upstream service. This value can be overridden on a per state basis via the `server.state()``passThrough` option. Defaults to `false` * `acceptEncoding` - if set to `false`, does not pass-through the 'Accept-Encoding' HTTP header which is useful for the `onResponse` post-processing to avoid receiving an encoded response. Can only be used together with `passThrough`. Defaults to `true` (passing header). From 8329662a89a32d6954b3be2e07bffcffa364ee37 Mon Sep 17 00:00:00 2001 From: Oscar Funes Date: Mon, 22 Aug 2016 22:13:29 -0600 Subject: [PATCH 11/37] Update deps --- package.json | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index bcff494..b6445d4 100644 --- a/package.json +++ b/package.json @@ -16,15 +16,15 @@ }, "dependencies": { "boom": "3.X.X", - "hoek": "3.X.X", - "joi": "8.X.X", - "wreck": "7.X.X" + "hoek": "4.X.X", + "joi": "9.X.X", + "wreck": "9.X.X" }, "devDependencies": { - "code": "2.X.X", - "hapi": "13.X.X", - "inert": "3.X.X", - "lab": "10.X.X" + "code": "3.X.X", + "hapi": "14.X.X", + "inert": "4.X.X", + "lab": "11.X.X" }, "scripts": { "test": "lab -a code -t 100 -L", From 1317c37d01b2370993c85284185707c186ab0d90 Mon Sep 17 00:00:00 2001 From: Oscar Funes Date: Mon, 22 Aug 2016 22:13:43 -0600 Subject: [PATCH 12/37] Update code based on updated lint rules --- test/index.js | 56 +++++++++++++++++++++++++-------------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/test/index.js b/test/index.js index 8c87e17..f6b4daf 100755 --- a/test/index.js +++ b/test/index.js @@ -87,7 +87,7 @@ describe('H2o2', () => { expect(response.statusCode).to.equal(200); expect(response.payload).to.contain('John Doe'); - expect(response.headers['set-cookie']).to.deep.equal(['test=123', 'auto=xyz']); + expect(response.headers['set-cookie']).to.equal(['test=123', 'auto=xyz']); expect(response.headers['cache-control']).to.equal('max-age=2, must-revalidate, private'); server.inject('/profile', (res) => { @@ -279,7 +279,7 @@ describe('H2o2', () => { upstream.start(() => { const server = provisionServer(); - server.route({ method: 'GET', path: '/headers', handler: { proxy: { host: 'localhost', port: upstream.info.port, passThrough: true, onResponse: onResponse } } }); + server.route({ method: 'GET', path: '/headers', handler: { proxy: { host: 'localhost', port: upstream.info.port, passThrough: true, onResponse } } }); server.inject({ url: '/headers', headers: { 'accept-encoding': 'gzip' } }, (res) => { @@ -312,7 +312,7 @@ describe('H2o2', () => { server.inject({ url: '/gzip', headers: { 'accept-encoding': 'gzip' } }, (res) => { expect(res.statusCode).to.equal(200); - expect(res.rawPayload).to.deep.equal(zipped); + expect(res.rawPayload).to.equal(zipped); done(); }); }); @@ -345,7 +345,7 @@ describe('H2o2', () => { Zlib.unzip(res.rawPayload, (err, unzipped) => { expect(err).to.not.exist(); - expect(unzipped.toString('utf8')).to.deep.equal(file); + expect(unzipped.toString('utf8')).to.equal(file); done(); }); }); @@ -550,7 +550,7 @@ describe('H2o2', () => { }; const server = provisionServer(); - server.route({ method: 'GET', path: '/onResponseError', config: { handler: handler, bind: { c: 6 } } }); + server.route({ method: 'GET', path: '/onResponseError', config: { handler, bind: { c: 6 } } }); server.inject('/onResponseError', (res) => { @@ -582,7 +582,7 @@ describe('H2o2', () => { } }; - server.route({ method: 'GET', path: '/', config: { handler: handler, bind: { c: 6 } } }); + server.route({ method: 'GET', path: '/', config: { handler, bind: { c: 6 } } }); return next(); }; @@ -628,7 +628,7 @@ describe('H2o2', () => { }; server.bind({ c: 7 }); - server.route({ method: 'GET', path: '/', config: { handler: handler } }); + server.route({ method: 'GET', path: '/', config: { handler } }); return next(); }; @@ -674,7 +674,7 @@ describe('H2o2', () => { }; server.bind({ c: 7 }); - server.route({ method: 'GET', path: '/', config: { handler: handler, bind: { c: 4 } } }); + server.route({ method: 'GET', path: '/', config: { handler, bind: { c: 4 } } }); return next(); }; @@ -731,7 +731,7 @@ describe('H2o2', () => { const upstream = new Hapi.Server(); upstream.connection(); - upstream.route({ method: 'GET', path: '/', handler: handler }); + upstream.route({ method: 'GET', path: '/', handler }); upstream.start(() => { const mapUri = function (request, callback) { @@ -740,7 +740,7 @@ describe('H2o2', () => { }; const server = provisionServer({ host: '127.0.0.1' }); - server.route({ method: 'GET', path: '/', handler: { proxy: { mapUri: mapUri, xforward: true } } }); + server.route({ method: 'GET', path: '/', handler: { proxy: { mapUri, xforward: true } } }); server.start(() => { @@ -776,7 +776,7 @@ describe('H2o2', () => { const upstream = new Hapi.Server(); upstream.connection(); - upstream.route({ method: 'GET', path: '/', handler: handler }); + upstream.route({ method: 'GET', path: '/', handler }); upstream.start(() => { const mapUri = function (request, callback) { @@ -791,7 +791,7 @@ describe('H2o2', () => { }; const server = provisionServer({ host: '127.0.0.1' }); - server.route({ method: 'GET', path: '/', handler: { proxy: { mapUri: mapUri, xforward: true } } }); + server.route({ method: 'GET', path: '/', handler: { proxy: { mapUri, xforward: true } } }); server.start(() => { @@ -827,7 +827,7 @@ describe('H2o2', () => { const upstream = new Hapi.Server(); upstream.connection(); - upstream.route({ method: 'GET', path: '/', handler: handler }); + upstream.route({ method: 'GET', path: '/', handler }); upstream.start(() => { const mapUri = function (request, callback) { @@ -842,7 +842,7 @@ describe('H2o2', () => { }; const server = provisionServer(); - server.route({ method: 'GET', path: '/', handler: { proxy: { mapUri: mapUri, xforward: true } } }); + server.route({ method: 'GET', path: '/', handler: { proxy: { mapUri, xforward: true } } }); server.inject('/', (res) => { @@ -874,7 +874,7 @@ describe('H2o2', () => { }; const server = provisionServer(); - server.route({ method: 'POST', path: '/echo', handler: { proxy: { mapUri: mapUri } } }); + server.route({ method: 'POST', path: '/echo', handler: { proxy: { mapUri } } }); server.inject({ url: '/echo', method: 'POST', payload: '{"echo":true}' }, (res) => { @@ -998,7 +998,7 @@ describe('H2o2', () => { expect(res.statusCode).to.equal(200); expect(res.payload).to.contain('John Doe'); - expect(res.headers['set-cookie']).to.deep.equal(['test=123', 'auto=xyz']); + expect(res.headers['set-cookie']).to.equal(['test=123', 'auto=xyz']); done(); }); }); @@ -1030,7 +1030,7 @@ describe('H2o2', () => { expect(res.statusCode).to.equal(200); expect(res.payload).to.contain('John Doe'); - expect(res.headers['set-cookie']).to.deep.equal(['test=123', 'auto=xyz']); + expect(res.headers['set-cookie']).to.equal(['test=123', 'auto=xyz']); done(); }); }); @@ -1541,7 +1541,7 @@ describe('H2o2', () => { done(); }; - server.route({ method: 'GET', path: '/agenttest', handler: { proxy: { uri: 'http://localhost', agent: agent } } }); + server.route({ method: 'GET', path: '/agenttest', handler: { proxy: { uri: 'http://localhost', agent } } }); server.inject({ method: 'GET', url: '/agenttest', headers: {} }, (res) => { }); }); @@ -1554,7 +1554,7 @@ describe('H2o2', () => { const upstream = new Hapi.Server(); upstream.connection(); - upstream.route({ method: 'GET', path: '/', handler: handler }); + upstream.route({ method: 'GET', path: '/', handler }); upstream.start(() => { const server = provisionServer(); @@ -1576,7 +1576,7 @@ describe('H2o2', () => { expect(res.statusCode).to.equal(200); const cookies = JSON.parse(res.payload); - expect(cookies).to.deep.equal({ b: '2' }); + expect(cookies).to.equal({ b: '2' }); done(); }); }); @@ -1591,7 +1591,7 @@ describe('H2o2', () => { const upstream = new Hapi.Server(); upstream.connection(); - upstream.route({ method: 'GET', path: '/', handler: handler }); + upstream.route({ method: 'GET', path: '/', handler }); upstream.start(() => { const server = provisionServer(); @@ -1614,7 +1614,7 @@ describe('H2o2', () => { expect(res.statusCode).to.equal(200); const cookies = JSON.parse(res.payload); - expect(cookies).to.deep.equal({ a: '1', b: '2' }); + expect(cookies).to.equal({ a: '1', b: '2' }); done(); }); }); @@ -1629,7 +1629,7 @@ describe('H2o2', () => { const upstream = new Hapi.Server(); upstream.connection(); - upstream.route({ method: 'GET', path: '/', handler: handler }); + upstream.route({ method: 'GET', path: '/', handler }); upstream.start(() => { const server = provisionServer(); @@ -1651,7 +1651,7 @@ describe('H2o2', () => { expect(res.statusCode).to.equal(200); const cookies = JSON.parse(res.payload); - expect(cookies).to.deep.equal({ a: '1', b: '2' }); + expect(cookies).to.equal({ a: '1', b: '2' }); done(); }); }); @@ -1690,7 +1690,7 @@ describe('H2o2', () => { const upstream = new Hapi.Server(); upstream.connection(); - upstream.route({ method: 'GET', path: '/', handler: handler }); + upstream.route({ method: 'GET', path: '/', handler }); upstream.start(() => { const server = provisionServer(); @@ -1712,7 +1712,7 @@ describe('H2o2', () => { expect(res.statusCode).to.equal(200); const cookies = JSON.parse(res.payload); - expect(cookies).to.deep.equal({}); + expect(cookies).to.equal({}); done(); }); }); @@ -1727,7 +1727,7 @@ describe('H2o2', () => { const upstream = new Hapi.Server(); upstream.connection(); - upstream.route({ method: 'GET', path: '/', handler: handler }); + upstream.route({ method: 'GET', path: '/', handler }); upstream.start(() => { const server = provisionServer(); @@ -1749,7 +1749,7 @@ describe('H2o2', () => { expect(res.statusCode).to.equal(200); const cookies = JSON.parse(res.payload); - expect(cookies).to.deep.equal({ b: '2' }); + expect(cookies).to.equal({ b: '2' }); done(); }); }); From 3afd364e77cef1af1a888351977266ec7dcff057 Mon Sep 17 00:00:00 2001 From: Oscar Funes Date: Mon, 22 Aug 2016 22:14:34 -0600 Subject: [PATCH 13/37] 5.1.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index b6445d4..fd3aec4 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "h2o2", "description": "Proxy handler plugin for hapi.js", - "version": "5.1.0", + "version": "5.1.1", "repository": "git://github.com/hapijs/h2o2", "main": "lib/index.js", "keywords": [ From 3e1c3b6f5f01a2c44cd2f0b4c63c084ec61c6892 Mon Sep 17 00:00:00 2001 From: Philip Tom Date: Mon, 26 Sep 2016 11:03:46 -0500 Subject: [PATCH 14/37] Added X-Forwarded-Host to forward headers Adding tests for x-forwarded-host Adding X-Forwarded-Host to documentation --- README.md | 2 +- lib/index.js | 1 + test/index.js | 14 +++++++++++--- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index e1895c1..7114e22 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ The proxy handler object has the following properties: * `localStatePassThrough` - if set to`false`, any locally defined state is removed from incoming requests before being sent to the upstream service. This value can be overridden on a per state basis via the `server.state()``passThrough` option. Defaults to `false` * `acceptEncoding` - if set to `false`, does not pass-through the 'Accept-Encoding' HTTP header which is useful for the `onResponse` post-processing to avoid receiving an encoded response. Can only be used together with `passThrough`. Defaults to `true` (passing header). * `rejectUnauthorized` - sets the `rejectUnauthorized` property on the https [agent](http://nodejs.org/api/https.html#https_https_request_options_callback) making the request. This value is only used when the proxied server uses TLS/SSL. If set it will override the node.js `rejectUnauthorized` property. If `false` then ssl errors will be ignored. When `true` the server certificate is verified and an 500 response will be sent when verification fails. This shouldn't be used alongside the `agent` setting as the `agent` will be used instead. Defaults to the https agent default value of `true`. -* `xforward` - if set to `true`, sets the 'X-Forwarded-For', 'X-Forwarded-Port', 'X-Forwarded-Proto' headers when making a request to the proxied upstream endpoint. Defaults to `false`. +* `xforward` - if set to `true`, sets the 'X-Forwarded-For', 'X-Forwarded-Port', 'X-Forwarded-Proto', 'X-Forwarded-Host' headers when making a request to the proxied upstream endpoint. Defaults to `false`. * `redirects` - the maximum number of HTTP redirections allowed to be followed automatically by the handler. Set to `false` or `0` to disable all redirections (the response will contain the redirection received from the upstream service). If redirections are enabled, no redirections (301, 302, 307, 308) will be passed along to the client, and reaching the maximum allowed redirections will return an error response. Defaults to `false`. * `timeout` - number of milliseconds before aborting the upstream request. Defaults to `180000` (3 minutes). * `mapUri` - a function used to map the request URI to the proxied URI. Cannot be used together with `host`, `port`, `protocol`, or `uri`. The function signature is `function (request, callback)` where: diff --git a/lib/index.js b/lib/index.js index bcb7242..e3411e0 100755 --- a/lib/index.js +++ b/lib/index.js @@ -128,6 +128,7 @@ internals.handler = function (route, handlerOptions) { options.headers['x-forwarded-for'] = (options.headers['x-forwarded-for'] ? options.headers['x-forwarded-for'] + ',' : '') + request.info.remoteAddress; options.headers['x-forwarded-port'] = (options.headers['x-forwarded-port'] ? options.headers['x-forwarded-port'] + ',' : '') + request.info.remotePort; options.headers['x-forwarded-proto'] = (options.headers['x-forwarded-proto'] ? options.headers['x-forwarded-proto'] + ',' : '') + protocol; + options.headers['x-forwarded-host'] = (options.headers['x-forwarded-host'] ? options.headers['x-forwarded-host'] + ',' : '') + request.info.host; } const contentType = request.headers['content-type']; diff --git a/test/index.js b/test/index.js index f6b4daf..12fc677 100755 --- a/test/index.js +++ b/test/index.js @@ -751,13 +751,16 @@ describe('H2o2', () => { const result = JSON.parse(body); const expectedClientAddress = '127.0.0.1'; + const expectedClientAddressAndPort = expectedClientAddress + ':' + server.info.port; if (Net.isIPv6(server.listener.address().address)) { expectedClientAddress = '::ffff:127.0.0.1'; + expectedClientAddressAndPort = '[' + expectedClientAddress + ']:' + server.info.port; } expect(result['x-forwarded-for']).to.equal(expectedClientAddress); expect(result['x-forwarded-port']).to.match(/\d+/); expect(result['x-forwarded-proto']).to.equal('http'); + expect(result['x-forwarded-host']).to.equal(expectedClientAddressAndPort); server.stop(Hoek.ignore); upstream.stop(Hoek.ignore); @@ -784,7 +787,8 @@ describe('H2o2', () => { const headers = { 'x-forwarded-for': 'testhost', 'x-forwarded-port': 1337, - 'x-forwarded-proto': 'https' + 'x-forwarded-proto': 'https', + 'x-forwarded-host': 'example.com' }; return callback(null, 'http://127.0.0.1:' + upstream.info.port + '/', headers); @@ -802,14 +806,16 @@ describe('H2o2', () => { const result = JSON.parse(body); const expectedClientAddress = '127.0.0.1'; + const expectedClientAddressAndPort = expectedClientAddress + ':' + server.info.port; if (Net.isIPv6(server.listener.address().address)) { expectedClientAddress = '::ffff:127.0.0.1'; + expectedClientAddressAndPort = '[' + expectedClientAddress + ']:' + server.info.port; } expect(result['x-forwarded-for']).to.equal('testhost,' + expectedClientAddress); expect(result['x-forwarded-port']).to.match(/1337\,\d+/); expect(result['x-forwarded-proto']).to.equal('https,http'); - + expect(result['x-forwarded-host']).to.equal('example.com,' + expectedClientAddressAndPort); server.stop(Hoek.ignore); upstream.stop(Hoek.ignore); done(); @@ -835,7 +841,8 @@ describe('H2o2', () => { const headers = { 'x-forwarded-for': 'testhost', 'x-forwarded-port': 1337, - 'x-forwarded-proto': 'https' + 'x-forwarded-proto': 'https', + 'x-forwarded-host': 'example.com' }; return callback(null, 'http://127.0.0.1:' + upstream.info.port + '/', headers); @@ -851,6 +858,7 @@ describe('H2o2', () => { expect(result['x-forwarded-for']).to.equal('testhost'); expect(result['x-forwarded-port']).to.equal('1337'); expect(result['x-forwarded-proto']).to.equal('https'); + expect(result['x-forwarded-host']).to.equal('example.com'); done(); }); }); From bc701af99d0e6ea9a9837ba188553372873d540d Mon Sep 17 00:00:00 2001 From: Jesse McCarthy Date: Tue, 30 Aug 2016 20:24:20 -0400 Subject: [PATCH 15/37] Test X-Forwarded-Proto value (Failing.) --- test/index.js | 56 +++++++++++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 24 deletions(-) mode change 100755 => 100644 test/index.js diff --git a/test/index.js b/test/index.js old mode 100755 new mode 100644 index 12fc677..1438775 --- a/test/index.js +++ b/test/index.js @@ -29,6 +29,11 @@ const expect = Code.expect; describe('H2o2', () => { + const tlsOptions = { + key: '-----BEGIN RSA PRIVATE KEY-----\nMIIEpAIBAAKCAQEA0UqyXDCqWDKpoNQQK/fdr0OkG4gW6DUafxdufH9GmkX/zoKz\ng/SFLrPipzSGINKWtyMvo7mPjXqqVgE10LDI3VFV8IR6fnART+AF8CW5HMBPGt/s\nfQW4W4puvBHkBxWSW1EvbecgNEIS9hTGvHXkFzm4xJ2e9DHp2xoVAjREC73B7JbF\nhc5ZGGchKw+CFmAiNysU0DmBgQcac0eg2pWoT+YGmTeQj6sRXO67n2xy/hA1DuN6\nA4WBK3wM3O4BnTG0dNbWUEbe7yAbV5gEyq57G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lqIyYpubCl1rDiEdVzqYYZEwmst36fJRRrVaFuAM/1DYAmT\nWMhU+yTfA+vCS4tql9b9zUhPw/IDHpBDWyR01spoZFBF/hE1MGNpCSXXsAbmCiVf\naxrIgR2DNketbDxkQx671KwF1+1JOMo9ffXp+OhuRo5NaGIxhTsZ+f/MA4y084Aj\nDI39av50sTRTWWShlN+J7PtdQVA5SZD97oYbeUeL7gI18kAJww9eUdmT0nEjcwKs\nxsQT1fyKbo7AlZBY4KSlUMuGnn0VnAsB9b+LxtXlDfnjyM8bVQx1uAfRo0DO8p/5\n3J5DTjAU55deBQ==\n-----END CERTIFICATE-----\n' + }; + const provisionServer = function (options) { const server = new Hapi.Server(); @@ -729,22 +734,40 @@ describe('H2o2', () => { reply(request.raw.req.headers); }; + const host = '127.0.0.1'; + const upstream = new Hapi.Server(); - upstream.connection(); + upstream.connection({ + host + }); upstream.route({ method: 'GET', path: '/', handler }); upstream.start(() => { - const mapUri = function (request, callback) { - - return callback(null, 'http://127.0.0.1:' + upstream.info.port + '/'); - }; + const server = provisionServer({ + host, + tls: tlsOptions + }); - const server = provisionServer({ host: '127.0.0.1' }); - server.route({ method: 'GET', path: '/', handler: { proxy: { mapUri, xforward: true } } }); + server.route({ + method: 'GET', + path: '/', + handler: { + proxy: { + host: upstream.info.host, + port: upstream.info.port, + protocol: 'http', + xforward: true + } + } + }); server.start(() => { - Wreck.get('http://127.0.0.1:' + server.info.port + '/', (err, res, body) => { + const requestProtocol = 'https'; + + Wreck.get(`${requestProtocol}://${server.info.host}:${server.info.port}/`, { + rejectUnauthorized: false + }, (err, res, body) => { expect(err).to.be.null(); expect(res.statusCode).to.equal(200); @@ -759,7 +782,7 @@ describe('H2o2', () => { expect(result['x-forwarded-for']).to.equal(expectedClientAddress); expect(result['x-forwarded-port']).to.match(/\d+/); - expect(result['x-forwarded-proto']).to.equal('http'); + expect(result['x-forwarded-proto']).to.equal(requestProtocol); expect(result['x-forwarded-host']).to.equal(expectedClientAddressAndPort); server.stop(Hoek.ignore); @@ -1141,11 +1164,6 @@ describe('H2o2', () => { it('uses rejectUnauthorized to allow proxy to self signed ssl server', (done) => { - const tlsOptions = { - key: '-----BEGIN RSA PRIVATE KEY-----\nMIIEpAIBAAKCAQEA0UqyXDCqWDKpoNQQK/fdr0OkG4gW6DUafxdufH9GmkX/zoKz\ng/SFLrPipzSGINKWtyMvo7mPjXqqVgE10LDI3VFV8IR6fnART+AF8CW5HMBPGt/s\nfQW4W4puvBHkBxWSW1EvbecgNEIS9hTGvHXkFzm4xJ2e9DHp2xoVAjREC73B7JbF\nhc5ZGGchKw+CFmAiNysU0DmBgQcac0eg2pWoT+YGmTeQj6sRXO67n2xy/hA1DuN6\nA4WBK3wM3O4BnTG0dNbWUEbe7yAbV5gEyq57GhJIeYxRvveVDaX90LoAqM4cUH06\n6rciON0UbDHV2LP/JaH5jzBjUyCnKLLo5snlbwIDAQABAoIBAQDJm7YC3pJJUcxb\nc8x8PlHbUkJUjxzZ5MW4Zb71yLkfRYzsxrTcyQA+g+QzA4KtPY8XrZpnkgm51M8e\n+B16AcIMiBxMC6HgCF503i16LyyJiKrrDYfGy2rTK6AOJQHO3TXWJ3eT3BAGpxuS\n12K2Cq6EvQLCy79iJm7Ks+5G6EggMZPfCVdEhffRm2Epl4T7LpIAqWiUDcDfS05n\nNNfAGxxvALPn+D+kzcSF6hpmCVrFVTf9ouhvnr+0DpIIVPwSK/REAF3Ux5SQvFuL\njPmh3bGwfRtcC5d21QNrHdoBVSN2UBLmbHUpBUcOBI8FyivAWJhRfKnhTvXMFG8L\nwaXB51IZAoGBAP/E3uz6zCyN7l2j09wmbyNOi1AKvr1WSmuBJveITouwblnRSdvc\nsYm4YYE0Vb94AG4n7JIfZLKtTN0xvnCo8tYjrdwMJyGfEfMGCQQ9MpOBXAkVVZvP\ne2k4zHNNsfvSc38UNSt7K0HkVuH5BkRBQeskcsyMeu0qK4wQwdtiCoBDAoGBANF7\nFMppYxSW4ir7Jvkh0P8bP/Z7AtaSm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KslwwqlgyqaDUECv33a9DpBuIFug1Gn8Xbnx/RppF/86Cs4P0hS6z4qc0hiDS\nlrcjL6O5j416qlYBNdCwyN1RVfCEen5wEU/gBfAluRzATxrf7H0FuFuKbrwR5AcV\nkltRL23nIDRCEvYUxrx15Bc5uMSdnvQx6dsaFQI0RAu9weyWxYXOWRhnISsPghZg\nIjcrFNA5gYEHGnNHoNqVqE/mBpk3kI+rEVzuu59scv4QNQ7jegOFgSt8DNzuAZ0x\ntHTW1lBG3u8gG1eYBMquexoSSHmMUb73lQ2l/dC6AKjOHFB9Ouq3IjjdFGwx1diz\n/yWh+Y8wY1Mgpyiy6ObJ5W8CAwEAATANBgkqhkiG9w0BAQUFAAOCAQEAoSc6Skb4\ng1e0ZqPKXBV2qbx7hlqIyYpubCl1rDiEdVzqYYZEwmst36fJRRrVaFuAM/1DYAmT\nWMhU+yTfA+vCS4tql9b9zUhPw/IDHpBDWyR01spoZFBF/hE1MGNpCSXXsAbmCiVf\naxrIgR2DNketbDxkQx671KwF1+1JOMo9ffXp+OhuRo5NaGIxhTsZ+f/MA4y084Aj\nDI39av50sTRTWWShlN+J7PtdQVA5SZD97oYbeUeL7gI18kAJww9eUdmT0nEjcwKs\nxsQT1fyKbo7AlZBY4KSlUMuGnn0VnAsB9b+LxtXlDfnjyM8bVQx1uAfRo0DO8p/5\n3J5DTjAU55deBQ==\n-----END CERTIFICATE-----\n' - }; - const upstream = new Hapi.Server(); upstream.connection({ tls: tlsOptions }); upstream.route({ @@ -1177,11 +1195,6 @@ describe('H2o2', () => { it('uses rejectUnauthorized to not allow proxy to self signed ssl server', (done) => { - const tlsOptions = { - key: '-----BEGIN RSA PRIVATE KEY-----\nMIIEpAIBAAKCAQEA0UqyXDCqWDKpoNQQK/fdr0OkG4gW6DUafxdufH9GmkX/zoKz\ng/SFLrPipzSGINKWtyMvo7mPjXqqVgE10LDI3VFV8IR6fnART+AF8CW5HMBPGt/s\nfQW4W4puvBHkBxWSW1EvbecgNEIS9hTGv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quexoSSHmMUb73lQ2l/dC6AKjOHFB9Ouq3IjjdFGwx1diz\n/yWh+Y8wY1Mgpyiy6ObJ5W8CAwEAATANBgkqhkiG9w0BAQUFAAOCAQEAoSc6Skb4\ng1e0ZqPKXBV2qbx7hlqIyYpubCl1rDiEdVzqYYZEwmst36fJRRrVaFuAM/1DYAmT\nWMhU+yTfA+vCS4tql9b9zUhPw/IDHpBDWyR01spoZFBF/hE1MGNpCSXXsAbmCiVf\naxrIgR2DNketbDxkQx671KwF1+1JOMo9ffXp+OhuRo5NaGIxhTsZ+f/MA4y084Aj\nDI39av50sTRTWWShlN+J7PtdQVA5SZD97oYbeUeL7gI18kAJww9eUdmT0nEjcwKs\nxsQT1fyKbo7AlZBY4KSlUMuGnn0VnAsB9b+LxtXlDfnjyM8bVQx1uAfRo0DO8p/5\n3J5DTjAU55deBQ==\n-----END CERTIFICATE-----\n' - }; - const upstream = new Hapi.Server(); upstream.connection({ tls: tlsOptions }); upstream.route({ @@ -1212,11 +1225,6 @@ describe('H2o2', () => { it('the default rejectUnauthorized should not allow proxied server cert to be self signed', (done) => { - const tlsOptions = { - key: '-----BEGIN RSA PRIVATE KEY-----\nMIIEpAIBAAKCAQEA0UqyXDCqWDKpoNQQK/fdr0OkG4gW6DUafxdufH9GmkX/zoKz\ng/SFLrPipzSGINKWtyMvo7mPjXqqVgE10LDI3VFV8IR6fnART+AF8CW5HMBPGt/s\nfQW4W4puvBHkBxWSW1EvbecgNEIS9hTGvHXkFzm4xJ2e9DHp2xoVAjREC73B7JbF\nhc5ZGGchKw+CFmAiNysU0DmBgQcac0eg2pWoT+YGmTeQj6sRXO67n2xy/hA1DuN6\nA4WBK3wM3O4BnTG0dNbWUEbe7yAbV5gEyq57GhJIeYxRvveVDaX90LoAqM4cUH06\n6rciON0UbDHV2LP/JaH5jzBjUyCnKLLo5snlbwIDAQABAoIBAQDJm7YC3pJJUcxb\nc8x8PlHbUkJUjxzZ5MW4Zb71yLkfRYzsxrTcyQA+g+QzA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o9ffXp+OhuRo5NaGIxhTsZ+f/MA4y084Aj\nDI39av50sTRTWWShlN+J7PtdQVA5SZD97oYbeUeL7gI18kAJww9eUdmT0nEjcwKs\nxsQT1fyKbo7AlZBY4KSlUMuGnn0VnAsB9b+LxtXlDfnjyM8bVQx1uAfRo0DO8p/5\n3J5DTjAU55deBQ==\n-----END CERTIFICATE-----\n' - }; - const upstream = new Hapi.Server(); upstream.connection({ tls: tlsOptions }); upstream.route({ From 8c53fcbebd1bc5f4ed26ee735800f1a26980f07f Mon Sep 17 00:00:00 2001 From: Jesse McCarthy Date: Tue, 30 Aug 2016 20:29:01 -0400 Subject: [PATCH 16/37] Fix X-Forwarded-Proto setting --- lib/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) mode change 100755 => 100644 lib/index.js diff --git a/lib/index.js b/lib/index.js old mode 100755 new mode 100644 index e3411e0..66eddf6 --- a/lib/index.js +++ b/lib/index.js @@ -127,7 +127,7 @@ internals.handler = function (route, handlerOptions) { request.info.remoteAddress) { options.headers['x-forwarded-for'] = (options.headers['x-forwarded-for'] ? options.headers['x-forwarded-for'] + ',' : '') + request.info.remoteAddress; options.headers['x-forwarded-port'] = (options.headers['x-forwarded-port'] ? options.headers['x-forwarded-port'] + ',' : '') + request.info.remotePort; - options.headers['x-forwarded-proto'] = (options.headers['x-forwarded-proto'] ? options.headers['x-forwarded-proto'] + ',' : '') + protocol; + options.headers['x-forwarded-proto'] = (options.headers['x-forwarded-proto'] ? options.headers['x-forwarded-proto'] + ',' : '') + request.connection.info.protocol; options.headers['x-forwarded-host'] = (options.headers['x-forwarded-host'] ? options.headers['x-forwarded-host'] + ',' : '') + request.info.host; } From 1527dabc81c0d04d9ed68392285fc1bad62dc57a Mon Sep 17 00:00:00 2001 From: Oscar Funes Date: Tue, 27 Sep 2016 22:37:18 -0600 Subject: [PATCH 17/37] 5.2.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index fd3aec4..86941bf 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "h2o2", "description": "Proxy handler plugin for hapi.js", - "version": "5.1.1", + "version": "5.2.0", "repository": "git://github.com/hapijs/h2o2", "main": "lib/index.js", "keywords": [ From 615434b7d71e0715d346115df181f726ba0e5900 Mon Sep 17 00:00:00 2001 From: EyePulp Date: Thu, 6 Oct 2016 11:50:52 -0500 Subject: [PATCH 18/37] Add tests for request.param replacement variables --- test/index.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/test/index.js b/test/index.js index 1438775..dedf2b2 100644 --- a/test/index.js +++ b/test/index.js @@ -1336,6 +1336,33 @@ describe('H2o2', () => { }); }); + it('proxies via uri template with request.param replacement', (done) => { + + const upstream = new Hapi.Server(); + upstream.connection(); + upstream.route({ + method: 'GET', + path: '/item/{some_param}', + handler: function (request, reply) { + + return reply({ a: request.params['some_param'] }); + } + }); + + upstream.start(() => { + + const server = provisionServer(); + server.route({ method: 'GET', path: '/handlerTemplate/{some_param}', handler: { proxy: { uri: '{protocol}://localhost:' + upstream.info.port + '/item/{some_param}' } } }); + const some_param = 'foo'; + server.inject('/handlerTemplate/'+some_param, (res) => { + + expect(res.statusCode).to.equal(200); + expect(res.payload).to.contain('"a":"'+some_param+'"'); + done(); + }); + }); + }); + it('passes upstream caching headers', (done) => { const upstream = new Hapi.Server(); From e8cab79424a388e8923d2ddc3c3741e2406e3c9c Mon Sep 17 00:00:00 2001 From: EyePulp Date: Thu, 6 Oct 2016 12:38:16 -0500 Subject: [PATCH 19/37] Add tests and clean up the request.params variable replacement code --- lib/index.js | 8 +++++++- test/index.js | 15 ++++++++------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/lib/index.js b/lib/index.js index 66eddf6..7bd38d6 100644 --- a/lib/index.js +++ b/lib/index.js @@ -195,11 +195,17 @@ internals.mapUri = function (protocol, host, port, uri) { return next(null, uri); } - const address = uri.replace(/{protocol}/g, request.connection.info.protocol) + let address = uri.replace(/{protocol}/g, request.connection.info.protocol) .replace(/{host}/g, request.connection.info.host) .replace(/{port}/g, request.connection.info.port) .replace(/{path}/g, request.url.path); + Object.keys(request.params).forEach((key) => { + + const re = new RegExp(`{${key}}`,'g'); + address = uri.replace(re,request.params[key]); + }); + return next(null, address); }; } diff --git a/test/index.js b/test/index.js index dedf2b2..6fc77f1 100644 --- a/test/index.js +++ b/test/index.js @@ -1336,7 +1336,7 @@ describe('H2o2', () => { }); }); - it('proxies via uri template with request.param replacement', (done) => { + it('proxies via uri template with request.param variables', (done) => { const upstream = new Hapi.Server(); upstream.connection(); @@ -1345,24 +1345,25 @@ describe('H2o2', () => { path: '/item/{some_param}', handler: function (request, reply) { - return reply({ a: request.params['some_param'] }); + return reply({ a: request.params.some_param }); } }); upstream.start(() => { const server = provisionServer(); - server.route({ method: 'GET', path: '/handlerTemplate/{some_param}', handler: { proxy: { uri: '{protocol}://localhost:' + upstream.info.port + '/item/{some_param}' } } }); - const some_param = 'foo'; - server.inject('/handlerTemplate/'+some_param, (res) => { + server.route({ method: 'GET', path: '/handlerTemplate/{some_param}', handler: { proxy: { uri: 'http://localhost:' + upstream.info.port + '/item/{some_param}' } } }); + + const p = 'foo'; + server.inject('/handlerTemplate/' + p, (res) => { expect(res.statusCode).to.equal(200); - expect(res.payload).to.contain('"a":"'+some_param+'"'); + expect(res.payload).to.contain('"a":"' + p + '"'); done(); }); }); }); - + it('passes upstream caching headers', (done) => { const upstream = new Hapi.Server(); From 0a3e827a7d29f742588a0be33fe5c80318f814ae Mon Sep 17 00:00:00 2001 From: EyePulp Date: Thu, 6 Oct 2016 13:04:57 -0500 Subject: [PATCH 20/37] Update the docs to cover uri templating Add some explanation for uri vairable replacement. --- README.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/README.md b/README.md index 7114e22..f8d3900 100644 --- a/README.md +++ b/README.md @@ -124,7 +124,41 @@ server.route({ } }); ``` +### Custom `uri` template values + +When using the `uri` option, there are several **default** template values that can be injected from the incoming request: + +* `{protocol}` +* `{host}` +* `{port}` +* `{path}` + +```javascript +server.route({ + method: 'GET', + path: '/', + handler: { + proxy: { + uri: '{protocol}:{port}//{host}/use/the/{path}' + } + } +}); +``` +Additionally, you can capture request.params values and inject them into the upstream uri value using a similar replacment strategy: +```javascript +server.route({ + method: 'GET', + path: '/foo/{bar}', + handler: { + proxy: { + uri: 'https://some.upstream.service.com/some/path/to/{bar}' + } + } +}); +``` +**Note** The default variables of `{protocol}`, `{host}`, `{port}`, `{path}` take precedence - it's best to treat those as reserved when naming your own request.params. + ### Using the `mapUri` and `onResponse` options Setting both options with custom functions will allow you to map the original request to an upstream service and to processing the response from the upstream service, before sending it to the client. Cannot be used together with `host`, `port`, `protocol`, or `uri`. From 158aba295bfd95ab12786c332baa8854a77bda54 Mon Sep 17 00:00:00 2001 From: EyePulp Date: Sat, 8 Oct 2016 13:18:31 -0500 Subject: [PATCH 21/37] Fix uri variable transposition error in the doc example --- README.md | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index f8d3900..afb695b 100644 --- a/README.md +++ b/README.md @@ -126,7 +126,7 @@ server.route({ ``` ### Custom `uri` template values -When using the `uri` option, there are several **default** template values that can be injected from the incoming request: +When using the `uri` option, there are optional **default** template values that can be injected from the incoming `request`: * `{protocol}` * `{host}` @@ -136,14 +136,17 @@ When using the `uri` option, there are several **default** template values that ```javascript server.route({ method: 'GET', - path: '/', + path: '/foo', handler: { proxy: { - uri: '{protocol}:{port}//{host}/use/the/{path}' + uri: '{protocol}//{host}:{port}/go/to/{path}' } } }); ``` +Requests to `http://127.0.0.1:8080/foo/` would be proxied to an upstream destination of `http://127.0.0.1:8080/go/to/foo` + + Additionally, you can capture request.params values and inject them into the upstream uri value using a similar replacment strategy: ```javascript server.route({ @@ -156,7 +159,7 @@ server.route({ } }); ``` -**Note** The default variables of `{protocol}`, `{host}`, `{port}`, `{path}` take precedence - it's best to treat those as reserved when naming your own request.params. +**Note** The default variables of `{protocol}`, `{host}`, `{port}`, `{path}` take precedence - it's best to treat those as reserved when naming your own `request.params`. ### Using the `mapUri` and `onResponse` options From 668df889d8958f7a3e7f11fbf1e5a9b057ee3e3f Mon Sep 17 00:00:00 2001 From: Oscar Funes Date: Sun, 9 Oct 2016 08:33:40 -0600 Subject: [PATCH 22/37] Add missing colon --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index afb695b..edce4fc 100644 --- a/README.md +++ b/README.md @@ -139,7 +139,7 @@ server.route({ path: '/foo', handler: { proxy: { - uri: '{protocol}//{host}:{port}/go/to/{path}' + uri: '{protocol}://{host}:{port}/go/to/{path}' } } }); From ddb2514bb021b3b79b7b30da6fcfe1b40bad33fc Mon Sep 17 00:00:00 2001 From: Oscar Funes Date: Sun, 9 Oct 2016 08:33:47 -0600 Subject: [PATCH 23/37] 5.3.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 86941bf..d0f1f3f 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "h2o2", "description": "Proxy handler plugin for hapi.js", - "version": "5.2.0", + "version": "5.3.0", "repository": "git://github.com/hapijs/h2o2", "main": "lib/index.js", "keywords": [ From 43e720fbab0b4b53668c5f41de02378b0abedf20 Mon Sep 17 00:00:00 2001 From: EyePulp Date: Mon, 10 Oct 2016 08:02:54 -0500 Subject: [PATCH 24/37] Fix a bad variable reference --- lib/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index 7bd38d6..5b7dbfd 100644 --- a/lib/index.js +++ b/lib/index.js @@ -203,7 +203,7 @@ internals.mapUri = function (protocol, host, port, uri) { Object.keys(request.params).forEach((key) => { const re = new RegExp(`{${key}}`,'g'); - address = uri.replace(re,request.params[key]); + address = address.replace(re,request.params[key]); }); return next(null, address); From d00bf6728dcb915761c4494b224f2e0eb882fba7 Mon Sep 17 00:00:00 2001 From: EyePulp Date: Mon, 10 Oct 2016 08:10:35 -0500 Subject: [PATCH 25/37] Improve uri request param variable replacement tests to verify multiple replacements --- test/index.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/test/index.js b/test/index.js index 6fc77f1..fdd6405 100644 --- a/test/index.js +++ b/test/index.js @@ -1342,23 +1342,25 @@ describe('H2o2', () => { upstream.connection(); upstream.route({ method: 'GET', - path: '/item/{some_param}', + path: '/item/{param_a}/{param_b}', handler: function (request, reply) { - return reply({ a: request.params.some_param }); + return reply({ a: request.params.param_a, b:request.params.param_b }); } }); upstream.start(() => { const server = provisionServer(); - server.route({ method: 'GET', path: '/handlerTemplate/{some_param}', handler: { proxy: { uri: 'http://localhost:' + upstream.info.port + '/item/{some_param}' } } }); + server.route({ method: 'GET', path: '/handlerTemplate/{a}/{b}', handler: { proxy: { uri: 'http://localhost:' + upstream.info.port + '/item/{a}/{b}' } } }); - const p = 'foo'; - server.inject('/handlerTemplate/' + p, (res) => { + const prma = 'foo'; + const prmb = 'bar'; + server.inject(`/handlerTemplate/${prma}/${prmb}`, (res) => { expect(res.statusCode).to.equal(200); - expect(res.payload).to.contain('"a":"' + p + '"'); + expect(res.payload).to.contain(`"a":"${prma}"`); + expect(res.payload).to.contain(`"b":"${prmb}"`); done(); }); }); From 738bb2d789e4569845d6d4c265a2523a0b823cf4 Mon Sep 17 00:00:00 2001 From: Oscar Funes Date: Mon, 10 Oct 2016 16:41:56 -0600 Subject: [PATCH 26/37] 5.4.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d0f1f3f..66af1f1 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "h2o2", "description": "Proxy handler plugin for hapi.js", - "version": "5.3.0", + "version": "5.4.0", "repository": "git://github.com/hapijs/h2o2", "main": "lib/index.js", "keywords": [ From 548f92fa4bcd321a2e38d56840038dcaf0c37200 Mon Sep 17 00:00:00 2001 From: Joe Boutros Date: Tue, 15 Nov 2016 15:01:18 -0600 Subject: [PATCH 27/37] lowercase `x` in dependency version strings --- package.json | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 66af1f1..4cfd1b5 100644 --- a/package.json +++ b/package.json @@ -15,16 +15,16 @@ "node": ">=4.0.0" }, "dependencies": { - "boom": "3.X.X", - "hoek": "4.X.X", - "joi": "9.X.X", - "wreck": "9.X.X" + "boom": "3.x.x", + "hoek": "4.x.x", + "joi": "9.x.x", + "wreck": "9.x.x" }, "devDependencies": { - "code": "3.X.X", - "hapi": "14.X.X", - "inert": "4.X.X", - "lab": "11.X.X" + "code": "3.x.x", + "hapi": "14.x.x", + "inert": "4.x.x", + "lab": "11.x.x" }, "scripts": { "test": "lab -a code -t 100 -L", From 4348c973f55f4a88246bb85450ac9d6a3832da2b Mon Sep 17 00:00:00 2001 From: Szymon Danielczyk Date: Wed, 23 Dec 2015 14:00:29 +0000 Subject: [PATCH 28/37] changes required to support modifyPayload and modifyResponse options --- lib/index.js | 90 +++++++++++++++++++++++++++++++++++----------------- package.json | 6 ++-- 2 files changed, 64 insertions(+), 32 deletions(-) diff --git a/lib/index.js b/lib/index.js index 5b7dbfd..60f42b5 100644 --- a/lib/index.js +++ b/lib/index.js @@ -41,7 +41,11 @@ internals.schema = Joi.object({ onResponse: Joi.func(), agent: Joi.object(), ttl: Joi.string().valid('upstream').allow(null), - maxSockets: Joi.number().positive().allow(false) + maxSockets: Joi.number().positive().allow(false), + + // added for kibi + modifyPayload: Joi.any(), + modifyResponse: Joi.any() }) .xor('host', 'mapUri', 'uri') .without('mapUri', 'port', 'protocol') @@ -50,10 +54,9 @@ internals.schema = Joi.object({ exports.register = function (server, pluginOptions, next) { - server.handler('proxy', internals.handler); - - server.decorate('reply', 'proxy', function (options) { + server.handler('kibi_proxy', internals.handler); + server.decorate('reply', 'kibi_proxy', function (options) { internals.handler(this.request.route, options)(this.request, this); }); @@ -72,13 +75,16 @@ internals.handler = function (route, handlerOptions) { const settings = Hoek.applyToDefaultsWithShallow(internals.defaults, handlerOptions, ['agent']); settings.mapUri = handlerOptions.mapUri || internals.mapUri(handlerOptions.protocol, handlerOptions.host, handlerOptions.port, handlerOptions.uri); + var modifyPayload = handlerOptions.modifyPayload || false; + var modifyResponse = handlerOptions.modifyResponse || false; + if (settings.ttl === 'upstream') { settings._upstreamTtl = true; } return function (request, reply) { - settings.mapUri(request, (err, uri, headers) => { + settings.mapUri(request, function (err, uri, headers) { if (err) { return reply(err); @@ -136,39 +142,65 @@ internals.handler = function (route, handlerOptions) { options.headers['content-type'] = contentType; } - // Send request - Wreck.request(request.method, uri, options, (err, res) => { - let ttl = null; + var _sendResponse = function (res, ttl, settings, data) { + var response = reply(data.body ? data.body : res) + .header('content-type', 'application/json') // sindicetech hack should be set only when there is a data.body + .ttl(ttl) + .code(res.statusCode) + .passThrough(!!settings.passThrough); // Default to false + }; - if (err) { - if (settings.onResponse) { - return settings.onResponse.call(bind, err, res, request, reply, settings, ttl); - } - return reply(err); - } + var _sendRequest = function (data) { + + Wreck.request(request.method, uri, options, function (err, res) { + + var ttl = null; - if (settings._upstreamTtl) { - const cacheControlHeader = res.headers['cache-control']; - if (cacheControlHeader) { - const cacheControl = Wreck.parseCacheControl(cacheControlHeader); - if (cacheControl) { - ttl = cacheControl['max-age'] * 1000; + if (err) { + if (settings.onResponse) { + return settings.onResponse.call(bind, err, res, request, reply, settings, ttl); } + + return reply(err); } - } - if (settings.onResponse) { - return settings.onResponse.call(bind, null, res, request, reply, settings, ttl); - } + if (settings._upstreamTtl) { + var cacheControlHeader = res.headers['cache-control']; + if (cacheControlHeader) { + var cacheControl = Wreck.parseCacheControl(cacheControlHeader); + if (cacheControl) { + ttl = cacheControl['max-age'] * 1000; + } + } + } + + if (settings.onResponse) { + return settings.onResponse.call(bind, null, res, request, reply, settings, ttl); + } + + if (modifyResponse && data) { + modifyResponse(res, data).then(function (ret) { + _sendResponse(ret.response, ttl, settings, ret.data); + }); + } else { + _sendResponse(res, ttl, settings); + } + }); + }; + + // Send request + if (modifyPayload) { + modifyPayload(request).then(function (payloadAndData) { + options.payload = payloadAndData.payload; + _sendRequest(payloadAndData.data); + }); + } else { + _sendRequest(); + } - return reply(res) - .ttl(ttl) - .code(res.statusCode) - .passThrough(!!settings.passThrough); // Default to false - }); }); }; }; diff --git a/package.json b/package.json index 4cfd1b5..cf820af 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,8 @@ { "name": "h2o2", - "description": "Proxy handler plugin for hapi.js", - "version": "5.4.0", - "repository": "git://github.com/hapijs/h2o2", + "description": "Kibi fork of proxy handler plugin for hapi.js", + "version": "5.4.0-kibi", + "repository": "git://github.com/sirensolutions/h2o2", "main": "lib/index.js", "keywords": [ "HTTP", From 3beba610388cc08f469351ceecd6ce6cc4112247 Mon Sep 17 00:00:00 2001 From: Szymon Danielczyk Date: Wed, 23 Dec 2015 14:35:13 +0000 Subject: [PATCH 29/37] fixed tests --- .editorconfig | 13 ++++++ test/index.js | 122 +++++++++++++++++++++++++------------------------- 2 files changed, 74 insertions(+), 61 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..35a8554 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,13 @@ +# editorconfig.org +root = true + +[*] +indent_style = space +indent_size = 4 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.md] +insert_final_newline = false diff --git a/test/index.js b/test/index.js index fdd6405..8993a4e 100644 --- a/test/index.js +++ b/test/index.js @@ -53,7 +53,7 @@ describe('H2o2', () => { }; const server = provisionServer(); - server.route({ method: 'GET', path: '/', handler: { proxy: { host: 'localhost', maxSockets: 213 } } }); + server.route({ method: 'GET', path: '/', handler: { kibi_proxy: { host: 'localhost', maxSockets: 213 } } }); server.inject('/', (res) => { }); }); @@ -68,7 +68,7 @@ describe('H2o2', () => { }; const server = provisionServer(); - server.route({ method: 'GET', path: '/', handler: { proxy: { host: 'localhost', maxSockets: false } } }); + server.route({ method: 'GET', path: '/', handler: { kibi_proxy: { host: 'localhost', maxSockets: false } } }); server.inject('/', (res) => { }); }); @@ -85,7 +85,7 @@ describe('H2o2', () => { upstream.start(() => { const server = provisionServer(); - server.route({ method: 'GET', path: '/profile', handler: { proxy: { host: 'localhost', port: upstream.info.port, xforward: true, passThrough: true } } }); + server.route({ method: 'GET', path: '/profile', handler: { kibi_proxy: { host: 'localhost', port: upstream.info.port, xforward: true, passThrough: true } } }); server.state('auto', { autoValue: 'xyz' }); server.inject('/profile', (response) => { @@ -115,7 +115,7 @@ describe('H2o2', () => { path: '/', config: { handler: { - proxy: { host: 'example.com' } + kibi_proxy: { host: 'example.com' } }, payload: { output: 'file' @@ -136,7 +136,7 @@ describe('H2o2', () => { path: '/', config: { handler: { - proxy: { some: 'key' } + kibi_proxy: { some: 'key' } } } }); @@ -154,7 +154,7 @@ describe('H2o2', () => { path: '/', config: { handler: { - proxy: { host: 'example.com' } + kibi_proxy: { host: 'example.com' } }, payload: { parse: true @@ -175,7 +175,7 @@ describe('H2o2', () => { path: '/', config: { handler: { - proxy: { host: 'example.com' } + kibi_proxy: { host: 'example.com' } }, payload: { output: 'data' @@ -202,7 +202,7 @@ describe('H2o2', () => { upstream.start(() => { const server = provisionServer(); - server.route({ method: 'GET', path: '/', handler: { proxy: { host: 'localhost', port: upstream.info.port, protocol: 'http' } } }); + server.route({ method: 'GET', path: '/', handler: { kibi_proxy: { host: 'localhost', port: upstream.info.port, protocol: 'http' } } }); server.inject('/', (res) => { @@ -228,7 +228,7 @@ describe('H2o2', () => { upstream.start(() => { const server = provisionServer({ routes: { cors: true } }); - server.route({ method: 'GET', path: '/headers', handler: { proxy: { host: 'localhost', port: upstream.info.port, passThrough: true } } }); + server.route({ method: 'GET', path: '/headers', handler: { kibi_proxy: { host: 'localhost', port: upstream.info.port, passThrough: true } } }); server.inject('/headers', (res) => { @@ -254,7 +254,7 @@ describe('H2o2', () => { // upstream.start(function () { // // const server = provisionServer({ routes: { cors: { credentials: true } } }); - // server.route({ method: 'GET', path: '/', handler: { proxy: { host: 'localhost', port: upstream.info.port, passThrough: true } } }); + // server.route({ method: 'GET', path: '/', handler: { kibi_proxy: { host: 'localhost', port: upstream.info.port, passThrough: true } } }); // // server.inject('/', (res) => { // @@ -284,7 +284,7 @@ describe('H2o2', () => { upstream.start(() => { const server = provisionServer(); - server.route({ method: 'GET', path: '/headers', handler: { proxy: { host: 'localhost', port: upstream.info.port, passThrough: true, onResponse } } }); + server.route({ method: 'GET', path: '/headers', handler: { kibi_proxy: { host: 'localhost', port: upstream.info.port, passThrough: true, onResponse } } }); server.inject({ url: '/headers', headers: { 'accept-encoding': 'gzip' } }, (res) => { @@ -308,7 +308,7 @@ describe('H2o2', () => { upstream.start(() => { const server = provisionServer(); - server.route({ method: 'GET', path: '/gzip', handler: { proxy: { host: 'localhost', port: upstream.info.port, passThrough: true } } }); + server.route({ method: 'GET', path: '/gzip', handler: { kibi_proxy: { host: 'localhost', port: upstream.info.port, passThrough: true } } }); Zlib.gzip(new Buffer('123456789012345678901234567890123456789012345678901234567890'), (err, zipped) => { @@ -338,7 +338,7 @@ describe('H2o2', () => { upstream.start(() => { const server = provisionServer(); - server.route({ method: 'GET', path: '/gzipstream', handler: { proxy: { host: 'localhost', port: upstream.info.port, passThrough: true } } }); + server.route({ method: 'GET', path: '/gzipstream', handler: { kibi_proxy: { host: 'localhost', port: upstream.info.port, passThrough: true } } }); server.inject({ url: '/gzipstream', headers: { 'accept-encoding': 'gzip' } }, (res) => { @@ -374,7 +374,7 @@ describe('H2o2', () => { upstream.start(() => { const server = provisionServer(); - server.route({ method: 'GET', path: '/noHeaders', handler: { proxy: { host: 'localhost', port: upstream.info.port } } }); + server.route({ method: 'GET', path: '/noHeaders', handler: { kibi_proxy: { host: 'localhost', port: upstream.info.port } } }); server.inject('/noHeaders', (res) => { @@ -405,7 +405,7 @@ describe('H2o2', () => { upstream.start(() => { const server = provisionServer(); - server.route({ method: 'GET', path: '/item', handler: { proxy: { host: 'localhost', port: upstream.info.port, protocol: 'http:' } }, config: { cache: { expiresIn: 500 } } }); + server.route({ method: 'GET', path: '/item', handler: { kibi_proxy: { host: 'localhost', port: upstream.info.port, protocol: 'http:' } }, config: { cache: { expiresIn: 500 } } }); server.inject('/item', (response) => { @@ -436,7 +436,7 @@ describe('H2o2', () => { upstream.start(() => { const server = provisionServer(); - server.route({ method: 'POST', path: '/item', handler: { proxy: { host: 'localhost', port: upstream.info.port } } }); + server.route({ method: 'POST', path: '/item', handler: { kibi_proxy: { host: 'localhost', port: upstream.info.port } } }); server.inject({ url: '/item', method: 'POST' }, (res) => { @@ -460,7 +460,7 @@ describe('H2o2', () => { upstream.start(() => { const server = provisionServer(); - server.route({ method: 'GET', path: '/unauthorized', handler: { proxy: { host: 'localhost', port: upstream.info.port } }, config: { cache: { expiresIn: 500 } } }); + server.route({ method: 'GET', path: '/unauthorized', handler: { kibi_proxy: { host: 'localhost', port: upstream.info.port } }, config: { cache: { expiresIn: 500 } } }); server.inject('/unauthorized', (res) => { @@ -477,7 +477,7 @@ describe('H2o2', () => { upstream.start(() => { const server = provisionServer(); - server.route({ method: 'POST', path: '/notfound', handler: { proxy: { host: 'localhost', port: upstream.info.port } } }); + server.route({ method: 'POST', path: '/notfound', handler: { kibi_proxy: { host: 'localhost', port: upstream.info.port } } }); server.inject('/notfound', (res) => { @@ -500,7 +500,7 @@ describe('H2o2', () => { }; const server = provisionServer(); - server.route({ method: 'GET', path: '/onResponseError', handler: { proxy: { host: 'localhost', port: upstream.info.port, onResponse: onResponseWithError } } }); + server.route({ method: 'GET', path: '/onResponseError', handler: { kibi_proxy: { host: 'localhost', port: upstream.info.port, onResponse: onResponseWithError } } }); server.inject('/onResponseError', (res) => { @@ -523,7 +523,7 @@ describe('H2o2', () => { }; const server = provisionServer(); - server.route({ method: 'GET', path: '/', handler: { proxy: { host: 'localhost', port: upstream.info.port, onResponse: on } } }); + server.route({ method: 'GET', path: '/', handler: { kibi_proxy: { host: 'localhost', port: upstream.info.port, onResponse: on } } }); server.inject('/', (res) => { @@ -547,7 +547,7 @@ describe('H2o2', () => { }; const handler = { - proxy: { + kibi_proxy: { host: 'localhost', port: upstream.info.port, onResponse: onResponseWithError @@ -580,7 +580,7 @@ describe('H2o2', () => { }; const handler = { - proxy: { + kibi_proxy: { host: 'localhost', port: upstream.info.port, onResponse: onResponseWithError @@ -625,7 +625,7 @@ describe('H2o2', () => { }; const handler = { - proxy: { + kibi_proxy: { host: 'localhost', port: upstream.info.port, onResponse: onResponseWithError @@ -671,7 +671,7 @@ describe('H2o2', () => { }; const handler = { - proxy: { + kibi_proxy: { host: 'localhost', port: upstream.info.port, onResponse: onResponseWithError @@ -717,7 +717,7 @@ describe('H2o2', () => { }; const server = provisionServer(); - server.route({ method: 'GET', path: '/failureResponse', handler: { proxy: { host: 'localhost', port: dummyPort, onResponse: failureResponse } }, config: { cache: { expiresIn: 500 } } }); + server.route({ method: 'GET', path: '/failureResponse', handler: { kibi_proxy: { host: 'localhost', port: dummyPort, onResponse: failureResponse } }, config: { cache: { expiresIn: 500 } } }); server.inject('/failureResponse', (res) => { @@ -752,7 +752,7 @@ describe('H2o2', () => { method: 'GET', path: '/', handler: { - proxy: { + kibi_proxy: { host: upstream.info.host, port: upstream.info.port, protocol: 'http', @@ -818,7 +818,7 @@ describe('H2o2', () => { }; const server = provisionServer({ host: '127.0.0.1' }); - server.route({ method: 'GET', path: '/', handler: { proxy: { mapUri, xforward: true } } }); + server.route({ method: 'GET', path: '/', handler: { kibi_proxy: { mapUri, xforward: true } } }); server.start(() => { @@ -872,7 +872,7 @@ describe('H2o2', () => { }; const server = provisionServer(); - server.route({ method: 'GET', path: '/', handler: { proxy: { mapUri, xforward: true } } }); + server.route({ method: 'GET', path: '/', handler: { kibi_proxy: { mapUri, xforward: true } } }); server.inject('/', (res) => { @@ -905,7 +905,7 @@ describe('H2o2', () => { }; const server = provisionServer(); - server.route({ method: 'POST', path: '/echo', handler: { proxy: { mapUri } } }); + server.route({ method: 'POST', path: '/echo', handler: { kibi_proxy: { mapUri } } }); server.inject({ url: '/echo', method: 'POST', payload: '{"echo":true}' }, (res) => { @@ -924,7 +924,7 @@ describe('H2o2', () => { }; const server = provisionServer(); - server.route({ method: 'GET', path: '/maperror', handler: { proxy: { mapUri: mapUriWithError } } }); + server.route({ method: 'GET', path: '/maperror', handler: { kibi_proxy: { mapUri: mapUriWithError } } }); server.inject('/maperror', (res) => { @@ -946,7 +946,7 @@ describe('H2o2', () => { upstream.start(() => { const server = provisionServer(); - server.route({ method: 'GET', path: '/redirect', handler: { proxy: { host: 'localhost', port: upstream.info.port, passThrough: true, redirects: 2 } } }); + server.route({ method: 'GET', path: '/redirect', handler: { kibi_proxy: { host: 'localhost', port: upstream.info.port, passThrough: true, redirects: 2 } } }); server.inject('/redirect?x=1', (res) => { @@ -969,7 +969,7 @@ describe('H2o2', () => { upstream.start(() => { const server = provisionServer(); - server.route({ method: 'GET', path: '/redirect', handler: { proxy: { host: 'localhost', port: upstream.info.port, passThrough: true, redirects: 2 } } }); + server.route({ method: 'GET', path: '/redirect', handler: { kibi_proxy: { host: 'localhost', port: upstream.info.port, passThrough: true, redirects: 2 } } }); server.inject('/redirect?x=3', (res) => { @@ -982,7 +982,7 @@ describe('H2o2', () => { it('errors on redirection to bad host', (done) => { const server = provisionServer(); - server.route({ method: 'GET', path: '/nowhere', handler: { proxy: { host: 'no.such.domain.x8' } } }); + server.route({ method: 'GET', path: '/nowhere', handler: { kibi_proxy: { host: 'no.such.domain.x8' } } }); server.inject('/nowhere', (res) => { @@ -994,7 +994,7 @@ describe('H2o2', () => { it('errors on redirection to bad host (https)', (done) => { const server = provisionServer(); - server.route({ method: 'GET', path: '/nowhere', handler: { proxy: { host: 'no.such.domain.x8', protocol: 'https' } } }); + server.route({ method: 'GET', path: '/nowhere', handler: { kibi_proxy: { host: 'no.such.domain.x8', protocol: 'https' } } }); server.inject('/nowhere', (res) => { @@ -1022,7 +1022,7 @@ describe('H2o2', () => { upstream.start(() => { const server = provisionServer(); - server.route({ method: 'GET', path: '/redirect', handler: { proxy: { host: 'localhost', port: upstream.info.port, passThrough: true, redirects: 2 } } }); + server.route({ method: 'GET', path: '/redirect', handler: { kibi_proxy: { host: 'localhost', port: upstream.info.port, passThrough: true, redirects: 2 } } }); server.state('auto', { autoValue: 'xyz' }); server.inject('/redirect', (res) => { @@ -1054,7 +1054,7 @@ describe('H2o2', () => { upstream.start(() => { const server = provisionServer(); - server.route({ method: 'GET', path: '/redirect', handler: { proxy: { host: 'localhost', port: upstream.info.port, passThrough: true, redirects: 2 } } }); + server.route({ method: 'GET', path: '/redirect', handler: { kibi_proxy: { host: 'localhost', port: upstream.info.port, passThrough: true, redirects: 2 } } }); server.state('auto', { autoValue: 'xyz' }); server.inject('/redirect?x=2', (res) => { @@ -1092,7 +1092,7 @@ describe('H2o2', () => { upstream.start(() => { const server = provisionServer(); - server.route({ method: 'POST', path: '/post1', handler: { proxy: { host: 'localhost', port: upstream.info.port, redirects: 3 } }, config: { payload: { output: 'stream' } } }); + server.route({ method: 'POST', path: '/post1', handler: { kibi_proxy: { host: 'localhost', port: upstream.info.port, redirects: 3 } }, config: { payload: { output: 'stream' } } }); server.inject({ method: 'POST', url: '/post1', payload: 'test', headers: { 'content-type': 'text/plain' } }, (res) => { @@ -1122,7 +1122,7 @@ describe('H2o2', () => { upstream.start(() => { const server = provisionServer(); - server.route({ method: 'GET', path: '/timeout1', handler: { proxy: { host: 'localhost', port: upstream.info.port, timeout: 5 } } }); + server.route({ method: 'GET', path: '/timeout1', handler: { kibi_proxy: { host: 'localhost', port: upstream.info.port, timeout: 5 } } }); server.inject('/timeout1', (res) => { @@ -1152,7 +1152,7 @@ describe('H2o2', () => { upstream.start(() => { const server = provisionServer(); - server.route({ method: 'GET', path: '/timeout2', handler: { proxy: { host: 'localhost', port: upstream.info.port } } }); + server.route({ method: 'GET', path: '/timeout2', handler: { kibi_proxy: { host: 'localhost', port: upstream.info.port } } }); server.inject('/timeout2', (res) => { @@ -1183,7 +1183,7 @@ describe('H2o2', () => { }; const server = provisionServer(); - server.route({ method: 'GET', path: '/allow', handler: { proxy: { mapUri: mapSslUri, rejectUnauthorized: false } } }); + server.route({ method: 'GET', path: '/allow', handler: { kibi_proxy: { mapUri: mapSslUri, rejectUnauthorized: false } } }); server.inject('/allow', (res) => { expect(res.statusCode).to.equal(200); @@ -1214,7 +1214,7 @@ describe('H2o2', () => { }; const server = provisionServer(); - server.route({ method: 'GET', path: '/reject', handler: { proxy: { mapUri: mapSslUri, rejectUnauthorized: true } } }); + server.route({ method: 'GET', path: '/reject', handler: { kibi_proxy: { mapUri: mapSslUri, rejectUnauthorized: true } } }); server.inject('/reject', (res) => { expect(res.statusCode).to.equal(502); @@ -1244,7 +1244,7 @@ describe('H2o2', () => { }; const server = provisionServer(); - server.route({ method: 'GET', path: '/sslDefault', handler: { proxy: { mapUri: mapSslUri } } }); + server.route({ method: 'GET', path: '/sslDefault', handler: { kibi_proxy: { mapUri: mapSslUri } } }); server.inject('/sslDefault', (res) => { expect(res.statusCode).to.equal(502); @@ -1272,7 +1272,7 @@ describe('H2o2', () => { upstream.start(() => { const server = provisionServer({ routes: { timeout: { server: 8 } } }); - server.route({ method: 'GET', path: '/timeout2', handler: { proxy: { host: 'localhost', port: upstream.info.port, timeout: 2 } } }); + server.route({ method: 'GET', path: '/timeout2', handler: { kibi_proxy: { host: 'localhost', port: upstream.info.port, timeout: 2 } } }); server.inject('/timeout2', (res) => { expect(res.statusCode).to.equal(504); @@ -1300,7 +1300,7 @@ describe('H2o2', () => { upstream.start(() => { const server = provisionServer({ routes: { timeout: { server: 5 } } }); - server.route({ method: 'GET', path: '/timeout1', handler: { proxy: { host: 'localhost', port: upstream.info.port, timeout: 15 } } }); + server.route({ method: 'GET', path: '/timeout1', handler: { kibi_proxy: { host: 'localhost', port: upstream.info.port, timeout: 15 } } }); server.inject('/timeout1', (res) => { expect(res.statusCode).to.equal(503); @@ -1325,7 +1325,7 @@ describe('H2o2', () => { upstream.start(() => { const server = provisionServer(); - server.route({ method: 'GET', path: '/handlerTemplate', handler: { proxy: { uri: '{protocol}://localhost:' + upstream.info.port + '/item' } } }); + server.route({ method: 'GET', path: '/handlerTemplate', handler: { kibi_proxy: { uri: '{protocol}://localhost:' + upstream.info.port + '/item' } } }); server.inject('/handlerTemplate', (res) => { @@ -1387,7 +1387,7 @@ describe('H2o2', () => { upstream.start(() => { const server = provisionServer(); - server.route({ method: 'GET', path: '/cachedItem', handler: { proxy: { host: 'localhost', port: upstream.info.port, ttl: 'upstream' } } }); + server.route({ method: 'GET', path: '/cachedItem', handler: { kibi_proxy: { host: 'localhost', port: upstream.info.port, ttl: 'upstream' } } }); server.state('auto', { autoValue: 'xyz' }); server.inject('/cachedItem', (res) => { @@ -1409,7 +1409,7 @@ describe('H2o2', () => { upstream.listen(0, () => { const server = provisionServer(); - server.route({ method: 'GET', path: '/', handler: { proxy: { host: 'localhost', port: upstream.address().port, ttl: 'upstream' } } }); + server.route({ method: 'GET', path: '/', handler: { kibi_proxy: { host: 'localhost', port: upstream.address().port, ttl: 'upstream' } } }); server.inject('/', (res) => { @@ -1431,7 +1431,7 @@ describe('H2o2', () => { upstream.listen(0, () => { const server = provisionServer(); - server.route({ method: 'GET', path: '/', handler: { proxy: { host: 'localhost', port: upstream.address().port, ttl: 'upstream' } } }); + server.route({ method: 'GET', path: '/', handler: { kibi_proxy: { host: 'localhost', port: upstream.address().port, ttl: 'upstream' } } }); server.inject('/', (res) => { @@ -1464,7 +1464,7 @@ describe('H2o2', () => { }; const server = provisionServer(); - server.route({ method: 'GET', path: '/304', handler: { proxy: { uri: 'http://localhost:' + upstream.info.port + '/item', onResponse: onResponse304 } } }); + server.route({ method: 'GET', path: '/304', handler: { kibi_proxy: { uri: 'http://localhost:' + upstream.info.port + '/item', onResponse: onResponse304 } } }); server.inject('/304', (res) => { @@ -1496,7 +1496,7 @@ describe('H2o2', () => { return reply({ something: 'else' }); }); - server.route({ method: 'GET', path: '/item', handler: { proxy: { host: 'localhost', port: upstream.info.port } } }); + server.route({ method: 'GET', path: '/item', handler: { kibi_proxy: { host: 'localhost', port: upstream.info.port } } }); server.inject('/item', (res) => { @@ -1520,7 +1520,7 @@ describe('H2o2', () => { upstream.start(() => { const server = provisionServer(); - server.route({ method: 'GET', path: '/', handler: { proxy: { host: 'localhost', port: upstream.info.port, acceptEncoding: true, passThrough: true } } }); + server.route({ method: 'GET', path: '/', handler: { kibi_proxy: { host: 'localhost', port: upstream.info.port, acceptEncoding: true, passThrough: true } } }); server.inject({ url: '/', headers: { 'accept-encoding': '*/*' } }, (res) => { @@ -1544,7 +1544,7 @@ describe('H2o2', () => { upstream.start(() => { const server = provisionServer(); - server.route({ method: 'GET', path: '/', handler: { proxy: { host: 'localhost', port: upstream.info.port, acceptEncoding: false, passThrough: true } } }); + server.route({ method: 'GET', path: '/', handler: { kibi_proxy: { host: 'localhost', port: upstream.info.port, acceptEncoding: false, passThrough: true } } }); server.inject({ url: '/', headers: { 'accept-encoding': '*/*' } }, (res) => { @@ -1567,7 +1567,7 @@ describe('H2o2', () => { expect(options.headers['Content-Type']).to.not.exist(); cb(new Error('placeholder')); }; - server.route({ method: 'GET', path: '/test', handler: { proxy: { uri: 'http://localhost', passThrough: true } } }); + server.route({ method: 'GET', path: '/test', handler: { kibi_proxy: { uri: 'http://localhost', passThrough: true } } }); server.inject({ method: 'GET', url: '/test', headers: { 'Content-Type': 'application/json' } }, (res) => { done(); @@ -1587,7 +1587,7 @@ describe('H2o2', () => { done(); }; - server.route({ method: 'GET', path: '/agenttest', handler: { proxy: { uri: 'http://localhost', agent } } }); + server.route({ method: 'GET', path: '/agenttest', handler: { kibi_proxy: { uri: 'http://localhost', agent } } }); server.inject({ method: 'GET', url: '/agenttest', headers: {} }, (res) => { }); }); @@ -1610,7 +1610,7 @@ describe('H2o2', () => { method: 'GET', path: '/', handler: { - proxy: { + kibi_proxy: { host: 'localhost', port: upstream.info.port, passThrough: true @@ -1647,7 +1647,7 @@ describe('H2o2', () => { method: 'GET', path: '/', handler: { - proxy: { + kibi_proxy: { host: 'localhost', port: upstream.info.port, passThrough: true, @@ -1685,7 +1685,7 @@ describe('H2o2', () => { method: 'GET', path: '/', handler: { - proxy: { + kibi_proxy: { host: 'localhost', port: upstream.info.port, passThrough: true @@ -1712,7 +1712,7 @@ describe('H2o2', () => { method: 'GET', path: '/', handler: { - proxy: { + kibi_proxy: { host: 'localhost', port: 8080, passThrough: true @@ -1746,7 +1746,7 @@ describe('H2o2', () => { method: 'GET', path: '/', handler: { - proxy: { + kibi_proxy: { host: 'localhost', port: upstream.info.port, passThrough: true @@ -1783,7 +1783,7 @@ describe('H2o2', () => { method: 'GET', path: '/', handler: { - proxy: { + kibi_proxy: { host: 'localhost', port: upstream.info.port, passThrough: true @@ -1821,7 +1821,7 @@ describe('H2o2', () => { path: '/', handler: function (request, reply) { - return reply.proxy({ host: 'localhost', port: upstream.info.port, xforward: true, passThrough: true }); + return reply.kibi_proxy({ host: 'localhost', port: upstream.info.port, xforward: true, passThrough: true }); } }); From 42e7003065f5110f08d03736e24b7e391e416e2d Mon Sep 17 00:00:00 2001 From: Szymon Danielczyk Date: Wed, 23 Dec 2015 14:59:03 +0000 Subject: [PATCH 30/37] fixed linting problems --- lib/index.js | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/lib/index.js b/lib/index.js index 60f42b5..23d4c0d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,3 +1,4 @@ +/*eslint brace-style: [2, "1tbs"]*/ 'use strict'; // Load modules @@ -42,7 +43,7 @@ internals.schema = Joi.object({ agent: Joi.object(), ttl: Joi.string().valid('upstream').allow(null), maxSockets: Joi.number().positive().allow(false), - + // added for kibi modifyPayload: Joi.any(), modifyResponse: Joi.any() @@ -57,6 +58,7 @@ exports.register = function (server, pluginOptions, next) { server.handler('kibi_proxy', internals.handler); server.decorate('reply', 'kibi_proxy', function (options) { + internals.handler(this.request.route, options)(this.request, this); }); @@ -75,8 +77,8 @@ internals.handler = function (route, handlerOptions) { const settings = Hoek.applyToDefaultsWithShallow(internals.defaults, handlerOptions, ['agent']); settings.mapUri = handlerOptions.mapUri || internals.mapUri(handlerOptions.protocol, handlerOptions.host, handlerOptions.port, handlerOptions.uri); - var modifyPayload = handlerOptions.modifyPayload || false; - var modifyResponse = handlerOptions.modifyResponse || false; + const modifyPayload = handlerOptions.modifyPayload || false; + const modifyResponse = handlerOptions.modifyResponse || false; if (settings.ttl === 'upstream') { settings._upstreamTtl = true; @@ -84,7 +86,7 @@ internals.handler = function (route, handlerOptions) { return function (request, reply) { - settings.mapUri(request, function (err, uri, headers) { + settings.mapUri(request, (err, uri, headers) => { if (err) { return reply(err); @@ -144,20 +146,21 @@ internals.handler = function (route, handlerOptions) { - var _sendResponse = function (res, ttl, settings, data) { - var response = reply(data.body ? data.body : res) - .header('content-type', 'application/json') // sindicetech hack should be set only when there is a data.body + const _sendResponse = function (res, ttl, data) { + + reply((data && data.body) ? data.body : res) + .header('content-type', 'application/json') // hack should be set only when there is a data.body .ttl(ttl) .code(res.statusCode) .passThrough(!!settings.passThrough); // Default to false }; - var _sendRequest = function (data) { + const _sendRequest = function (data) { - Wreck.request(request.method, uri, options, function (err, res) { + Wreck.request(request.method, uri, options, (err, res) => { - var ttl = null; + let ttl = null; if (err) { if (settings.onResponse) { @@ -168,9 +171,9 @@ internals.handler = function (route, handlerOptions) { } if (settings._upstreamTtl) { - var cacheControlHeader = res.headers['cache-control']; + const cacheControlHeader = res.headers['cache-control']; if (cacheControlHeader) { - var cacheControl = Wreck.parseCacheControl(cacheControlHeader); + const cacheControl = Wreck.parseCacheControl(cacheControlHeader); if (cacheControl) { ttl = cacheControl['max-age'] * 1000; } @@ -182,18 +185,20 @@ internals.handler = function (route, handlerOptions) { } if (modifyResponse && data) { - modifyResponse(res, data).then(function (ret) { - _sendResponse(ret.response, ttl, settings, ret.data); + modifyResponse(res, data).then((ret) => { + + _sendResponse(ret.response, ttl, ret.data); }); } else { - _sendResponse(res, ttl, settings); + _sendResponse(res, ttl); } }); }; // Send request if (modifyPayload) { - modifyPayload(request).then(function (payloadAndData) { + modifyPayload(request).then((payloadAndData) => { + options.payload = payloadAndData.payload; _sendRequest(payloadAndData.data); }); From cd4488dbd245a68ea788bb12790993ccbd08f94d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Campinas?= Date: Mon, 4 Jan 2016 16:26:10 +0000 Subject: [PATCH 31/37] added test --- lib/index.js | 23 ++++++++------- package.json | 3 +- test/index.js | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 12 deletions(-) diff --git a/lib/index.js b/lib/index.js index 23d4c0d..052b6da 100644 --- a/lib/index.js +++ b/lib/index.js @@ -48,9 +48,9 @@ internals.schema = Joi.object({ modifyPayload: Joi.any(), modifyResponse: Joi.any() }) - .xor('host', 'mapUri', 'uri') - .without('mapUri', 'port', 'protocol') - .without('uri', 'port', 'protocol'); +.xor('host', 'mapUri', 'uri') +.without('mapUri', 'port', 'protocol') +.without('uri', 'port', 'protocol'); exports.register = function (server, pluginOptions, next) { @@ -145,12 +145,15 @@ internals.handler = function (route, handlerOptions) { } - const _sendResponse = function (res, ttl, data) { - reply((data && data.body) ? data.body : res) - .header('content-type', 'application/json') // hack should be set only when there is a data.body - .ttl(ttl) + let r; + if (data && data.body) { + r = reply(data.body).header('content-type', 'application/json'); + } else { + r = reply(res); + } + r.ttl(ttl) .code(res.statusCode) .passThrough(!!settings.passThrough); // Default to false }; @@ -184,7 +187,7 @@ internals.handler = function (route, handlerOptions) { return settings.onResponse.call(bind, null, res, request, reply, settings, ttl); } - if (modifyResponse && data) { + if (modifyResponse) { modifyResponse(res, data).then((ret) => { _sendResponse(ret.response, ttl, ret.data); @@ -205,7 +208,6 @@ internals.handler = function (route, handlerOptions) { } else { _sendRequest(); } - }); }; }; @@ -247,8 +249,7 @@ internals.mapUri = function (protocol, host, port, uri) { }; } - if (protocol && - protocol[protocol.length - 1] !== ':') { + if (protocol && protocol[protocol.length - 1] !== ':') { protocol += ':'; } diff --git a/package.json b/package.json index cf820af..fde4c17 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,8 @@ "code": "3.x.x", "hapi": "14.x.x", "inert": "4.x.x", - "lab": "11.x.x" + "lab": "11.x.x", + "bluebird": "3.1.1" }, "scripts": { "test": "lab -a code -t 100 -L", diff --git a/test/index.js b/test/index.js index 8993a4e..edf9935 100644 --- a/test/index.js +++ b/test/index.js @@ -12,6 +12,7 @@ const Hapi = require('hapi'); const Hoek = require('hoek'); const Lab = require('lab'); const Wreck = require('wreck'); +const Promise = require('bluebird'); // Declare internals @@ -42,6 +43,86 @@ describe('H2o2', () => { return server; }; + it('pre/post-process the request', { parallel: false }, (done) => { + + const dataHandler = function (request, reply) { + + return reply(request.payload); + }; + + const upstream = new Hapi.Server(); + upstream.connection(); + upstream.route({ method: 'POST', path: '/data', handler: dataHandler }); + upstream.start(() => { + + const server = provisionServer({ routes: { cors: true } }); + server.route({ + method: 'POST', + path: '/data', + handler: { + kibi_proxy: { + host: 'localhost', + port: upstream.info.port, + passThrough: true, + modifyPayload: (request) => { + + const req = request.raw.req; + return new Promise((fulfill, reject) => { + + const chunks = []; + req.on('error', reject); + req.on('data', (chunk) => { + + chunks.push(chunk); + }); + req.on('end', () => { + + const body = JSON.parse(Buffer.concat(chunks)); + body.copy = body.msg; + const buffer = new Buffer(JSON.stringify(body)); + fulfill({ payload: buffer }); + }); + }); + }, + modifyResponse: (response, data) => { + + return new Promise((fulfill, reject) => { + + const chunks = []; + response.on('error', reject); + response.on('data', (d) => { + + chunks.push(d); + }); + response.on('end', () => { + + const body = JSON.parse(Buffer.concat(chunks).toString()); + body.copy = body.copy.toUpperCase(); + fulfill({ + response: response, + data: { body: new Buffer(JSON.stringify(body)) } + }); + }); + }); + } + } + } + }); + + server.inject({ + method: 'POST', + url: '/data', + payload: '{"msg":"hello"}', + headers: { 'content-type': 'application/json' } + }, + (res) => { + + expect(res.payload).to.equal('{\"msg\":\"hello\",\"copy\":\"HELLO\"}'); + done(); + }); + }); + }); + it('overrides maxSockets', { parallel: false }, (done) => { const orig = Wreck.request; From 1104837dbf2c3b04a5c0e6135c96942f71b834b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Campinas?= Date: Tue, 5 Jan 2016 10:33:39 +0000 Subject: [PATCH 32/37] corrected test --- test/index.js | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/test/index.js b/test/index.js index edf9935..3a5f452 100644 --- a/test/index.js +++ b/test/index.js @@ -43,6 +43,21 @@ describe('H2o2', () => { return server; }; + it('overrides maxSockets', { parallel: false }, (done) => { + + const orig = Wreck.request; + Wreck.request = function (method, uri, options, callback) { + + Wreck.request = orig; + expect(options.agent.maxSockets).to.equal(213); + done(); + }; + + const server = provisionServer(); + server.route({ method: 'GET', path: '/', handler: { kibi_proxy: { host: 'localhost', maxSockets: 213 } } }); + server.inject('/', (res) => { }); + }); + it('pre/post-process the request', { parallel: false }, (done) => { const dataHandler = function (request, reply) { @@ -55,7 +70,7 @@ describe('H2o2', () => { upstream.route({ method: 'POST', path: '/data', handler: dataHandler }); upstream.start(() => { - const server = provisionServer({ routes: { cors: true } }); + const server = provisionServer(); server.route({ method: 'POST', path: '/data', @@ -63,7 +78,6 @@ describe('H2o2', () => { kibi_proxy: { host: 'localhost', port: upstream.info.port, - passThrough: true, modifyPayload: (request) => { const req = request.raw.req; @@ -112,32 +126,16 @@ describe('H2o2', () => { server.inject({ method: 'POST', url: '/data', - payload: '{"msg":"hello"}', - headers: { 'content-type': 'application/json' } + payload: JSON.stringify({ msg: 'hello' }) }, (res) => { - expect(res.payload).to.equal('{\"msg\":\"hello\",\"copy\":\"HELLO\"}'); + expect(res.payload).to.equal(JSON.stringify({ msg: 'hello', copy: 'HELLO' })); done(); }); }); }); - it('overrides maxSockets', { parallel: false }, (done) => { - - const orig = Wreck.request; - Wreck.request = function (method, uri, options, callback) { - - Wreck.request = orig; - expect(options.agent.maxSockets).to.equal(213); - done(); - }; - - const server = provisionServer(); - server.route({ method: 'GET', path: '/', handler: { kibi_proxy: { host: 'localhost', maxSockets: 213 } } }); - server.inject('/', (res) => { }); - }); - it('uses node default with maxSockets set to false', { parallel: false }, (done) => { const orig = Wreck.request; From d69485e2c608839235f8e04851a493c16c769909 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Campinas?= Date: Tue, 5 Jan 2016 10:52:52 +0000 Subject: [PATCH 33/37] added data to the test to show that some data can be passed to the modifyResponse from the modifyPayload --- test/index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/index.js b/test/index.js index 3a5f452..340ceca 100644 --- a/test/index.js +++ b/test/index.js @@ -94,7 +94,7 @@ describe('H2o2', () => { const body = JSON.parse(Buffer.concat(chunks)); body.copy = body.msg; const buffer = new Buffer(JSON.stringify(body)); - fulfill({ payload: buffer }); + fulfill({ data: 'connor', payload: buffer }); }); }); }, @@ -112,6 +112,7 @@ describe('H2o2', () => { const body = JSON.parse(Buffer.concat(chunks).toString()); body.copy = body.copy.toUpperCase(); + body.john = data; fulfill({ response: response, data: { body: new Buffer(JSON.stringify(body)) } @@ -130,7 +131,7 @@ describe('H2o2', () => { }, (res) => { - expect(res.payload).to.equal(JSON.stringify({ msg: 'hello', copy: 'HELLO' })); + expect(res.payload).to.equal(JSON.stringify({ msg: 'hello', copy: 'HELLO', john: 'connor' })); done(); }); }); From f25c7d8496a5937a2ad8518cbeb3a82da86e7eb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Campinas?= Date: Tue, 5 Jan 2016 15:05:20 +0000 Subject: [PATCH 34/37] ECMAscript5 style and rename module --- lib/index.js | 13 ++++++------- package.json | 2 +- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/index.js b/lib/index.js index 052b6da..f0476e0 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,5 +1,4 @@ /*eslint brace-style: [2, "1tbs"]*/ -'use strict'; // Load modules const Http = require('http'); @@ -86,7 +85,7 @@ internals.handler = function (route, handlerOptions) { return function (request, reply) { - settings.mapUri(request, (err, uri, headers) => { + settings.mapUri(request, function (err, uri, headers) { if (err) { return reply(err); @@ -147,7 +146,7 @@ internals.handler = function (route, handlerOptions) { const _sendResponse = function (res, ttl, data) { - let r; + var r; if (data && data.body) { r = reply(data.body).header('content-type', 'application/json'); } else { @@ -161,9 +160,9 @@ internals.handler = function (route, handlerOptions) { const _sendRequest = function (data) { - Wreck.request(request.method, uri, options, (err, res) => { + Wreck.request(request.method, uri, options, function (err, res) { - let ttl = null; + var ttl = null; if (err) { if (settings.onResponse) { @@ -188,7 +187,7 @@ internals.handler = function (route, handlerOptions) { } if (modifyResponse) { - modifyResponse(res, data).then((ret) => { + modifyResponse(res, data).then(function (ret) { _sendResponse(ret.response, ttl, ret.data); }); @@ -200,7 +199,7 @@ internals.handler = function (route, handlerOptions) { // Send request if (modifyPayload) { - modifyPayload(request).then((payloadAndData) => { + modifyPayload(request).then(function (payloadAndData) { options.payload = payloadAndData.payload; _sendRequest(payloadAndData.data); diff --git a/package.json b/package.json index fde4c17..6a9af8f 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "h2o2", + "name": "kibi-h2o2", "description": "Kibi fork of proxy handler plugin for hapi.js", "version": "5.4.0-kibi", "repository": "git://github.com/sirensolutions/h2o2", From 51b7889598f71f47b657d6260cb57d724e94a315 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Campinas?= Date: Fri, 29 Apr 2016 09:57:45 +0100 Subject: [PATCH 35/37] catch errors thrown from a modify* method --- lib/index.js | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/index.js b/lib/index.js index f0476e0..d9bf43d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -6,6 +6,7 @@ const Https = require('https'); const Hoek = require('hoek'); const Joi = require('joi'); const Wreck = require('wreck'); +const Boom = require('boom'); // Declare internals @@ -157,6 +158,13 @@ internals.handler = function (route, handlerOptions) { .passThrough(!!settings.passThrough); // Default to false }; + const _onError = function (err) { + if (settings.onResponse) { + return settings.onResponse.call(bind, err, res, request, reply, settings, ttl); + } + + return reply(err); + }; const _sendRequest = function (data) { @@ -165,11 +173,7 @@ internals.handler = function (route, handlerOptions) { var ttl = null; if (err) { - if (settings.onResponse) { - return settings.onResponse.call(bind, err, res, request, reply, settings, ttl); - } - - return reply(err); + return _onError(err); } if (settings._upstreamTtl) { @@ -190,6 +194,12 @@ internals.handler = function (route, handlerOptions) { modifyResponse(res, data).then(function (ret) { _sendResponse(ret.response, ttl, ret.data); + }).catch((err) => { + let msg = 'Failed request'; + if (err.message) { + msg = err.message; + } + return _onError(Boom.badRequest(msg, err)); }); } else { _sendResponse(res, ttl); @@ -203,6 +213,12 @@ internals.handler = function (route, handlerOptions) { options.payload = payloadAndData.payload; _sendRequest(payloadAndData.data); + }).catch((err) => { + let msg = 'Failed request'; + if (err.message) { + msg = err.message; + } + return _onError(Boom.badRequest(msg, err)); }); } else { _sendRequest(); From 929c464051a9e2061e05d0cf1f70fc6f879825ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Campinas?= Date: Fri, 29 Apr 2016 10:38:11 +0100 Subject: [PATCH 36/37] added tests --- lib/index.js | 15 +++-- test/index.js | 153 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 162 insertions(+), 6 deletions(-) diff --git a/lib/index.js b/lib/index.js index d9bf43d..6835d66 100644 --- a/lib/index.js +++ b/lib/index.js @@ -158,7 +158,8 @@ internals.handler = function (route, handlerOptions) { .passThrough(!!settings.passThrough); // Default to false }; - const _onError = function (err) { + const _onError = function (err, res, ttl) { + if (settings.onResponse) { return settings.onResponse.call(bind, err, res, request, reply, settings, ttl); } @@ -173,7 +174,7 @@ internals.handler = function (route, handlerOptions) { var ttl = null; if (err) { - return _onError(err); + return _onError(err, res, ttl); } if (settings._upstreamTtl) { @@ -194,8 +195,9 @@ internals.handler = function (route, handlerOptions) { modifyResponse(res, data).then(function (ret) { _sendResponse(ret.response, ttl, ret.data); - }).catch((err) => { - let msg = 'Failed request'; + }).catch(function (err) { + + var msg = 'Failed request'; if (err.message) { msg = err.message; } @@ -213,8 +215,9 @@ internals.handler = function (route, handlerOptions) { options.payload = payloadAndData.payload; _sendRequest(payloadAndData.data); - }).catch((err) => { - let msg = 'Failed request'; + }).catch(function (err) { + + var msg = 'Failed request'; if (err.message) { msg = err.message; } diff --git a/test/index.js b/test/index.js index 340ceca..c031b06 100644 --- a/test/index.js +++ b/test/index.js @@ -137,6 +137,159 @@ describe('H2o2', () => { }); }); + it('modifyPayload the request with error', { parallel: false }, (done) => { + + const dataHandler = function (request, reply) { + + return reply(request.payload); + }; + + const upstream = new Hapi.Server(); + upstream.connection(); + upstream.route({ method: 'POST', path: '/data', handler: dataHandler }); + upstream.start(() => { + + const server = provisionServer(); + server.route({ + method: 'POST', + path: '/data', + handler: { + kibi_proxy: { + host: 'localhost', + port: upstream.info.port, + modifyPayload: (request) => { + + const req = request.raw.req; + return new Promise((fulfill, reject) => { + + const chunks = []; + req.on('error', reject); + req.on('data', (chunk) => { + + chunks.push(chunk); + }); + req.on('end', () => { + + reject({ message: 'some error' }); + }); + }); + }, + modifyResponse: (response, data) => { + + return new Promise((fulfill, reject) => { + + const chunks = []; + response.on('error', reject); + response.on('data', (d) => { + + chunks.push(d); + }); + response.on('end', () => { + + const body = JSON.parse(Buffer.concat(chunks).toString()); + body.copy = body.copy.toUpperCase(); + body.john = data; + fulfill({ + response: response, + data: { body: new Buffer(JSON.stringify(body)) } + }); + }); + }); + } + } + } + }); + + server.inject({ + method: 'POST', + url: '/data', + payload: JSON.stringify({ msg: 'hello' }) + }, + (res) => { + + const payload = JSON.parse(res.payload); + expect(payload.statusCode).to.equal(400); + expect(payload.message).to.equal('some error'); + done(); + }); + }); + }); + + it('modifyResponse the request with error', { parallel: false }, (done) => { + + const dataHandler = function (request, reply) { + + return reply(request.payload); + }; + + const upstream = new Hapi.Server(); + upstream.connection(); + upstream.route({ method: 'POST', path: '/data', handler: dataHandler }); + upstream.start(() => { + + const server = provisionServer(); + server.route({ + method: 'POST', + path: '/data', + handler: { + kibi_proxy: { + host: 'localhost', + port: upstream.info.port, + modifyPayload: (request) => { + + const req = request.raw.req; + return new Promise((fulfill, reject) => { + + const chunks = []; + req.on('error', reject); + req.on('data', (chunk) => { + + chunks.push(chunk); + }); + req.on('end', () => { + + const body = JSON.parse(Buffer.concat(chunks)); + body.copy = body.msg; + const buffer = new Buffer(JSON.stringify(body)); + fulfill({ data: 'connor', payload: buffer }); + }); + }); + }, + modifyResponse: (response, data) => { + + return new Promise((fulfill, reject) => { + + const chunks = []; + response.on('error', reject); + response.on('data', (d) => { + + chunks.push(d); + }); + response.on('end', () => { + + reject({ message: 'some error' }); + }); + }); + } + } + } + }); + + server.inject({ + method: 'POST', + url: '/data', + payload: JSON.stringify({ msg: 'hello' }) + }, + (res) => { + + const payload = JSON.parse(res.payload); + expect(payload.statusCode).to.equal(400); + expect(payload.message).to.equal('some error'); + done(); + }); + }); + }); + it('uses node default with maxSockets set to false', { parallel: false }, (done) => { const orig = Wreck.request; From 0736d1f3b0514b44f196f8695c7d13812ff1b7f0 Mon Sep 17 00:00:00 2001 From: szydan Date: Wed, 4 Jan 2017 18:07:20 +0100 Subject: [PATCH 37/37] Added code missing after upstream merge, fix lint issues --- lib/index.js | 28 +++++++++++++++++----------- test/index.js | 6 +++--- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/lib/index.js b/lib/index.js index 6835d66..1c2bdac 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,4 +1,6 @@ +'use strict'; /*eslint brace-style: [2, "1tbs"]*/ + // Load modules const Http = require('http'); @@ -86,7 +88,7 @@ internals.handler = function (route, handlerOptions) { return function (request, reply) { - settings.mapUri(request, function (err, uri, headers) { + settings.mapUri(request, (err, uri, headers) => { if (err) { return reply(err); @@ -147,7 +149,7 @@ internals.handler = function (route, handlerOptions) { const _sendResponse = function (res, ttl, data) { - var r; + let r; if (data && data.body) { r = reply(data.body).header('content-type', 'application/json'); } else { @@ -169,12 +171,16 @@ internals.handler = function (route, handlerOptions) { const _sendRequest = function (data) { - Wreck.request(request.method, uri, options, function (err, res) { + Wreck.request(request.method, uri, options, (err, res) => { - var ttl = null; + let ttl = null; if (err) { - return _onError(err, res, ttl); + if (settings.onResponse) { + return settings.onResponse.call(bind, err, res, request, reply, settings, ttl); + } + + return reply(err); } if (settings._upstreamTtl) { @@ -192,12 +198,12 @@ internals.handler = function (route, handlerOptions) { } if (modifyResponse) { - modifyResponse(res, data).then(function (ret) { + modifyResponse(res, data).then((ret) => { _sendResponse(ret.response, ttl, ret.data); - }).catch(function (err) { + }).catch((err) => { - var msg = 'Failed request'; + let msg = 'Failed request'; if (err.message) { msg = err.message; } @@ -211,13 +217,13 @@ internals.handler = function (route, handlerOptions) { // Send request if (modifyPayload) { - modifyPayload(request).then(function (payloadAndData) { + modifyPayload(request).then((payloadAndData) => { options.payload = payloadAndData.payload; _sendRequest(payloadAndData.data); - }).catch(function (err) { + }).catch((err) => { - var msg = 'Failed request'; + let msg = 'Failed request'; if (err.message) { msg = err.message; } diff --git a/test/index.js b/test/index.js index c031b06..d18377f 100644 --- a/test/index.js +++ b/test/index.js @@ -114,7 +114,7 @@ describe('H2o2', () => { body.copy = body.copy.toUpperCase(); body.john = data; fulfill({ - response: response, + response, data: { body: new Buffer(JSON.stringify(body)) } }); }); @@ -190,7 +190,7 @@ describe('H2o2', () => { body.copy = body.copy.toUpperCase(); body.john = data; fulfill({ - response: response, + response, data: { body: new Buffer(JSON.stringify(body)) } }); }); @@ -1585,7 +1585,7 @@ describe('H2o2', () => { upstream.start(() => { const server = provisionServer(); - server.route({ method: 'GET', path: '/handlerTemplate/{a}/{b}', handler: { proxy: { uri: 'http://localhost:' + upstream.info.port + '/item/{a}/{b}' } } }); + server.route({ method: 'GET', path: '/handlerTemplate/{a}/{b}', handler: { kibi_proxy: { uri: 'http://localhost:' + upstream.info.port + '/item/{a}/{b}' } } }); const prma = 'foo'; const prmb = 'bar';