From c9e47184b1d173507dd99941b1ff663b8b03c67a Mon Sep 17 00:00:00 2001 From: Rick Kilgore Date: Mon, 14 Nov 2016 13:52:34 -0800 Subject: [PATCH 1/5] update docs add description of beforeProxyRequest option update header re-writing example --- README.md | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index ae8ed8110..75dc3ff6a 100644 --- a/README.md +++ b/README.md @@ -162,21 +162,24 @@ modifies the outgoing proxy request by adding a special header. var http = require('http'), httpProxy = require('http-proxy'); +// To modify the proxy connection before data is sent, you can supply a +// 'beforeProxyRequest' handler function in the passed-in config options. +// Before the request is proxied to the target host, http-proxy will call +// this handler with the following arguments: +// (http.IncomingMessage req, Object options, Object requestOutgoingOptions). +// This mechanism is useful when you need to modify the proxy request before +// the proxy connection is made to the target. +// +function beforeProxyRequest(req, options, outgoingOptions) { + outgoingOptions.headers = outgoingOptions.headers || [ ]; + outgoingOptions.headers.push('X-Special-Proxy-Header: foobar'); +}); + // // Create a proxy server with custom application logic // -var proxy = httpProxy.createProxyServer({}); - -// To modify the proxy connection before data is sent, you can listen -// for the 'proxyReq' event. When the event is fired, you will receive -// the following arguments: -// (http.ClientRequest proxyReq, http.IncomingMessage req, -// http.ServerResponse res, Object options). This mechanism is useful when -// you need to modify the proxy request before the proxy connection -// is made to the target. -// -proxy.on('proxyReq', function(proxyReq, req, res, options) { - proxyReq.setHeader('X-Special-Proxy-Header', 'foobar'); +var proxy = httpProxy.createProxyServer({ + beforeProxyRequest: beforeProxyRequest }); var server = http.createServer(function(req, res) { @@ -351,6 +354,10 @@ proxyServer.listen(8015); ``` * **headers**: object with extra headers to be added to target requests. * **proxyTimeout**: timeout (in millis) when proxy receives no response from target +* **beforeProxyRequest**: optional callback function. If passed, it will be called with three arguments immediately before sending the request to the target host: + * `req` the incoming `ClientRequest` object + * `options` - the Options Config object passed to the proxy at initialization. i.e., the subject of this section. + * `outgoing` - the `request()` options, so that this handler can modify the outgoing request (e.g., add a header) **NOTE:** `options.ws` and `options.ssl` are optional. From 08ac9da8c0b5cfed8dd1edd62ebfaf4f862ea0e6 Mon Sep 17 00:00:00 2001 From: Rick Kilgore Date: Mon, 14 Nov 2016 13:54:26 -0800 Subject: [PATCH 2/5] mention beforeProxyRequest should be synchronous in README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 75dc3ff6a..7c8e9ff6b 100644 --- a/README.md +++ b/README.md @@ -354,7 +354,7 @@ proxyServer.listen(8015); ``` * **headers**: object with extra headers to be added to target requests. * **proxyTimeout**: timeout (in millis) when proxy receives no response from target -* **beforeProxyRequest**: optional callback function. If passed, it will be called with three arguments immediately before sending the request to the target host: +* **beforeProxyRequest**: optional callback function - should be synchronous. If passed, it will be called with three arguments immediately before sending the request to the target host: * `req` the incoming `ClientRequest` object * `options` - the Options Config object passed to the proxy at initialization. i.e., the subject of this section. * `outgoing` - the `request()` options, so that this handler can modify the outgoing request (e.g., add a header) From bd4c42811e41a4edf976e43953fcf510384545a8 Mon Sep 17 00:00:00 2001 From: Rick Kilgore Date: Mon, 14 Nov 2016 13:55:36 -0800 Subject: [PATCH 3/5] fix type of arg to beforeProxyRequest callback --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7c8e9ff6b..f1632a2eb 100644 --- a/README.md +++ b/README.md @@ -355,7 +355,7 @@ proxyServer.listen(8015); * **headers**: object with extra headers to be added to target requests. * **proxyTimeout**: timeout (in millis) when proxy receives no response from target * **beforeProxyRequest**: optional callback function - should be synchronous. If passed, it will be called with three arguments immediately before sending the request to the target host: - * `req` the incoming `ClientRequest` object + * `req` the incoming `IncomingMessage` object * `options` - the Options Config object passed to the proxy at initialization. i.e., the subject of this section. * `outgoing` - the `request()` options, so that this handler can modify the outgoing request (e.g., add a header) From cc288d9c19b7bc12c7a6cacd23c8f03f49e2050b Mon Sep 17 00:00:00 2001 From: Rick Kilgore Date: Mon, 14 Nov 2016 14:20:45 -0800 Subject: [PATCH 4/5] test beforeProxyRequest test beforeProxyRequest fix header re-writing example in README --- README.md | 4 ++-- test/lib-http-proxy-common-test.js | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f1632a2eb..485f2117f 100644 --- a/README.md +++ b/README.md @@ -171,8 +171,8 @@ var http = require('http'), // the proxy connection is made to the target. // function beforeProxyRequest(req, options, outgoingOptions) { - outgoingOptions.headers = outgoingOptions.headers || [ ]; - outgoingOptions.headers.push('X-Special-Proxy-Header: foobar'); + outgoingOptions.headers = outgoingOptions.headers || { }; + outgoingOptions.headers['X-Special-Proxy-Header'] = 'foobar'; }); // diff --git a/test/lib-http-proxy-common-test.js b/test/lib-http-proxy-common-test.js index ef99ca109..bae48cea3 100644 --- a/test/lib-http-proxy-common-test.js +++ b/test/lib-http-proxy-common-test.js @@ -6,6 +6,9 @@ describe('lib/http-proxy/common.js', function () { describe('#setupOutgoing', function () { it('should setup the correct headers', function () { var outgoing = {}; + var beforeProxy = function (req, options, outgoingOpts) { + outgoingOpts.headers['X-Special-Proxy-Header'] = 'foobar'; + }; common.setupOutgoing(outgoing, { agent : '?', @@ -17,7 +20,8 @@ describe('lib/http-proxy/common.js', function () { }, headers: {'fizz': 'bang', 'overwritten':true}, localAddress: 'local.address', - auth:'username:pass' + auth:'username:pass', + beforeProxyRequest: beforeProxy }, { method : 'i', @@ -37,6 +41,7 @@ describe('lib/http-proxy/common.js', function () { expect(outgoing.headers.pro).to.eql('xy'); expect(outgoing.headers.fizz).to.eql('bang'); expect(outgoing.headers.overwritten).to.eql(true); + expect(outgoing.headers['X-Special-Proxy-Header']).to.eql('foobar'); expect(outgoing.localAddress).to.eql('local.address'); expect(outgoing.auth).to.eql('username:pass'); }); From 60200bdfd5b99fc1955b80bde400d8202dc7fd87 Mon Sep 17 00:00:00 2001 From: Rick Kilgore Date: Mon, 14 Nov 2016 14:26:20 -0800 Subject: [PATCH 5/5] move beforeProxyRequest test to its own test --- test/lib-http-proxy-common-test.js | 31 ++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/test/lib-http-proxy-common-test.js b/test/lib-http-proxy-common-test.js index bae48cea3..d39aad326 100644 --- a/test/lib-http-proxy-common-test.js +++ b/test/lib-http-proxy-common-test.js @@ -6,9 +6,6 @@ describe('lib/http-proxy/common.js', function () { describe('#setupOutgoing', function () { it('should setup the correct headers', function () { var outgoing = {}; - var beforeProxy = function (req, options, outgoingOpts) { - outgoingOpts.headers['X-Special-Proxy-Header'] = 'foobar'; - }; common.setupOutgoing(outgoing, { agent : '?', @@ -20,8 +17,7 @@ describe('lib/http-proxy/common.js', function () { }, headers: {'fizz': 'bang', 'overwritten':true}, localAddress: 'local.address', - auth:'username:pass', - beforeProxyRequest: beforeProxy + auth:'username:pass' }, { method : 'i', @@ -41,7 +37,6 @@ describe('lib/http-proxy/common.js', function () { expect(outgoing.headers.pro).to.eql('xy'); expect(outgoing.headers.fizz).to.eql('bang'); expect(outgoing.headers.overwritten).to.eql(true); - expect(outgoing.headers['X-Special-Proxy-Header']).to.eql('foobar'); expect(outgoing.localAddress).to.eql('local.address'); expect(outgoing.auth).to.eql('username:pass'); }); @@ -67,6 +62,30 @@ describe('lib/http-proxy/common.js', function () { expect(outgoing.headers.connection).to.eql('upgrade'); }); + it('should run the beforeProxyRequest callback', function () { + var outgoing = {}; + var beforeProxy = function (req, options, outgoingOpts) { + outgoingOpts.headers['X-Special-Proxy-Header'] = 'foobar'; + }; + common.setupOutgoing(outgoing, + { + agent: undefined, + target: { + host : 'hey', + hostname : 'how', + socketPath: 'are', + port : 'you', + }, + beforeProxyRequest: beforeProxy + }, + { + method : 'i', + url : 'am', + headers : {'pro':'xy','overwritten':false} + }); + expect(outgoing.headers['X-Special-Proxy-Header']).to.eql('foobar'); + }); + it('should not override agentless connection: contains upgrade', function () { var outgoing = {}; common.setupOutgoing(outgoing,