From f30a4641dda41199d4a7feaee515d9b2daadbe60 Mon Sep 17 00:00:00 2001 From: kai Date: Fri, 14 Oct 2016 14:30:39 +0200 Subject: [PATCH 1/2] Added onRequest() and onResponse() callbacks --- lib/proxy.js | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/lib/proxy.js b/lib/proxy.js index 2a2b41f..c8e38f2 100644 --- a/lib/proxy.js +++ b/lib/proxy.js @@ -85,12 +85,16 @@ function ReverseProxy(opts) { */ }); - proxy.on('proxyReq', function (p, req) { + proxy.on('proxyReq', function (p, req, res, options) { + onRequestCallback(p,req,res,options); + if (req.host != null) { p.setHeader('host', req.host); } }); - + proxy.on('proxyRes', function (res) { + onResponseCallback(res); + }); // // Support NTLM auth // @@ -635,13 +639,36 @@ var respondNotFound = function (req, res) { res.end(); }; +var onRequestCallback = function (req, res) { +} + +var onResponseCallback = function (req, res) { +} + ReverseProxy.prototype.notFound = function (callback) { + respondNotFound = testIsFunction(callback); +}; + +ReverseProxy.prototype.onRequest = function (callback) { + onRequestCallback = testIsFunction(callback); +}; + +ReverseProxy.prototype.onResponse = function (callback) { + onResponseCallback = testIsFunction(callback); +}; + +// +// Checks if +// argument is a function and returns it +// or +// throws an exception +// +function testIsFunction(callback) { if (typeof callback == "function") - respondNotFound = callback; + return callback; else throw Error('notFound callback is not a function'); -}; - +} // // Redirect to the HTTPS proxy // From ee5bc8a601131f64d0ece7f6f7a2be3415795431 Mon Sep 17 00:00:00 2001 From: kai Date: Fri, 14 Oct 2016 14:53:11 +0200 Subject: [PATCH 2/2] Added onRequest and onResponse to the documentation --- README.md | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/README.md b/README.md index 85dd93c..cae0f72 100644 --- a/README.md +++ b/README.md @@ -370,6 +370,8 @@ setTimeout(function() { [register](#register) [unregister](#unregister) [notFound](#notFound) +[onRequest](#onRequest) +[onResponse](#onResponse) [close](#close) @@ -468,6 +470,44 @@ __Arguments__ --------------------------------------- + +#### Redbird##onRequest(callback) + + Gives Redbird a callback function with four parameters, the proxy, the HTTP request/response and options objects, respectively, which will be called when a proxy route is executed. The default is +```javascript + function (proxy, request, response, options) { + } +``` +. + +__Arguments__ + +```javascript + src {Function(proxy, request, response, options)} The callback which will be + called before the proxy redirects the request to the target +``` + +--------------------------------------- + + +#### Redbird##onResponse(callback) + + Gives Redbird a callback function with one parameter, the HTTP response object, respectively, which will be called when a proxy route was executed. The default is +```javascript + function (response) { + } +``` +. + +__Arguments__ + +```javascript + src {Function(response)} The callback which will be + called after the proxy server received a response from the target server +``` + +--------------------------------------- + #### Redbird##close()