Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added onRequest and onResponse callbacks for Redbird #72

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,8 @@ setTimeout(function() {
[register](#register)
[unregister](#unregister)
[notFound](#notFound)
[onRequest](#onRequest)
[onResponse](#onResponse)
[close](#close)

<a name="redbird"/>
Expand Down Expand Up @@ -468,6 +470,44 @@ __Arguments__

---------------------------------------

<a name="onRequest"/>
#### 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
```

---------------------------------------

<a name="onResponse"/>
#### 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
```

---------------------------------------

<a name="close"/>
#### Redbird##close()

Expand Down
37 changes: 32 additions & 5 deletions lib/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand Down Expand Up @@ -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
//
Expand Down