Skip to content

Commit

Permalink
Merge pull request #31 from apigee/header-set
Browse files Browse the repository at this point in the history
Adding functionality to set custom headers, and unset headers in plugins.
  • Loading branch information
f1erro authored Nov 17, 2016
2 parents ec85cf0 + 6ce83e0 commit 414ce3d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
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

0 comments on commit 414ce3d

Please sign in to comment.