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

Adding functionality to set custom headers, and unset headers in plugins. #31

Merged
merged 3 commits into from
Nov 17, 2016
Merged
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
11 changes: 11 additions & 0 deletions lib/config-proxy-middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ module.exports = function () {

req.reqUrl = reqUrl;

req._overrideHeaders = {};
req._headersToUnset = [];

req.setOverrideHeader = function(k, v) {
req._overrideHeaders[k] = v;
};

req.unsetHeader = function(k) {
req._headersToUnset.push(k);
};

const proxy = getProxyfromBasePath(config.proxies, reqUrl.pathname);

if (!proxy) {
Expand Down
19 changes: 18 additions & 1 deletion lib/plugins-middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ function _sendTargetRequest(sourceRequest, sourceResponse, plugins, startTime, c
if (target_headers['content-length']) {
delete target_headers['content-length'];
}

}

const httpLibrary = proxy.secure ? https : http;
Expand Down Expand Up @@ -191,6 +192,7 @@ function _sendTargetRequest(sourceRequest, sourceResponse, plugins, startTime, c
agent: proxy.agent
};


const targetRequest = httpLibrary.request(targetRequestOptions,
(targetResponse) => {

Expand Down Expand Up @@ -329,7 +331,10 @@ function _subscribeToSourceRequestEvents(plugins, promises, sourceRequest, sourc
logger.error(err);
return reject(err);
}
result && targetRequest.write(result); // write transformed data to target

if(result) {
targetRequest.write(result); // write transformed data to target
}
resolve();
});
});
Expand All @@ -355,6 +360,18 @@ function _subscribeToSourceRequestEvents(plugins, promises, sourceRequest, sourc
logger.error(err);
return reject(err);
}

if(!targetRequest._headerSent) {
Object.keys(sourceRequest._overrideHeaders).forEach(function(k) {
var value = sourceRequest._overrideHeaders[k];
targetRequest.setHeader(k, value);
});

sourceRequest._headersToUnset.forEach(function(h) {
targetRequest.unsetHeader(h);
});
}

targetRequest.end(result); // write transformed data to target
resolve();
});
Expand Down