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

Allow custom responses in onRequest #274

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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@ __Arguments__
}
{onRequest: (req, res, target) => {
// called before forwarding is occurred, you can modify req.headers for example
// return the res object to send custom responses (e.g. redirects)
// return undefined to forward to default target
}}
```
Expand Down
42 changes: 32 additions & 10 deletions lib/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ function ReverseProxy(opts) {
}
}

function hasCustomResponse(target, res) {
return target && res && target instanceof http.ServerResponse && target === res;
}

ReverseProxy.prototype.setupHttpProxy = function (proxy, websocketsUpgrade, log, opts) {
var _this = this;
var httpServerModule = opts.serverModule || http;
Expand All @@ -195,7 +199,11 @@ ReverseProxy.prototype.setupHttpProxy = function (proxy, websocketsUpgrade, log,
.bind(_this)
.then(function (target) {
if (target) {
if (shouldRedirectToHttps(_this.certs, src, target, _this)) {
if (hasCustomResponse(target, res)) {
if (!res.writableEnded) res.end()
return;
}
else if (shouldRedirectToHttps(_this.certs, src, target, _this)) {
redirectToHttps(req, res, target, opts.ssl, log);
} else {
proxy.web(req, res, {
Expand Down Expand Up @@ -627,6 +635,7 @@ ReverseProxy.buildRoute = function (route) {
});

routeObject.path = route.path || '/';
routeObject.opts = route.opts || {};
}
routeCache.set(cacheKey, routeObject);
return routeObject;
Expand Down Expand Up @@ -681,15 +690,28 @@ ReverseProxy.prototype._getTarget = function (src, req, res) {
req.host = target.host;
}

if (route.opts.onRequest) {
const resultFromRequestHandler = route.opts.onRequest(req, res, target);
if (resultFromRequestHandler !== undefined) {
this.log &&
this.log.info(
'Proxying %s received result from onRequest handler, returning.',
src + url
);
return resultFromRequestHandler;
if (route.opts) {
//
// Process optional stuff here
//
if (typeof route.opts.onRequest === 'function') {
const resultFromRequestHandler = route.opts.onRequest(req, res, target);
if (resultFromRequestHandler !== undefined) {
if (hasCustomResponse(resultFromRequestHandler, res)) {
this.log &&
this.log.info(
'Proxying %s received custom response from onRequest handler, returning.',
src + url
);
} else {
this.log &&
this.log.info(
'Proxying %s received result from onRequest handler, returning.',
src + url
);
}
return resultFromRequestHandler;
}
}
}

Expand Down
Loading