-
Notifications
You must be signed in to change notification settings - Fork 36
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
Protocol not supported error on plugin redirecting to HTTPS target #81
Comments
While it is possible to do this. It's not recommended. Here is one of two ways to make this work. Set the target for your proxy to be an https proxy in edge. This will correctly configure everything internally. You can also attempt to replace the agent used to make requests. You simply create an instance of https.Agent and assign it as the proxy agent. Brief code snippet below to document how to possibly do this. var https = require('https');
//...elided
onrequest: function(req, res, next) {
var agent = new https.Agent({
keepAlive: true
})
res.proxy.agent = agent;
//...elided
} |
Just to add more information In the plugins-middleware.js const proxy = sourceResponse.proxy; // get the proxy from sourceResponse
...
var httpLibrary = http;
if (sourceRequest.targetSecure || proxy.secureHttpProxy) {
httpLibrary = https;
}
// As the plugin sets the targetSecure to "true", the httpLibrary will be "https"
var targetRequestOptions = {
hostname: sourceRequest.targetHostname,
port: parseInt(sourceRequest.targetPort),
path: sourceRequest.targetPath,
method: sourceRequest.method,
headers: target_headers,
agent: proxy.agent // this agent has a "protocol" property with "http" as value
};
...
const targetRequest = httpLibrary.request(targetRequestOptions,
(targetResponse) => cb(null, targetResponse)); // execute the request passing the targetRequestOptions above the execution will land in the following code in the _http_client.js file var protocol = options.protocol || defaultAgent.protocol; // protocol will be "https"
var expectedProtocol = defaultAgent.protocol;
if (self.agent && self.agent.protocol) // self.agent is the same targetRequestOptions.agent
expectedProtocol = self.agent.protocol; // expectedProtocol will be set to "http"
if (options.path && / /.test(options.path)) {
throw new TypeError('Request path contains unescaped characters');
} else if (protocol !== expectedProtocol) { //this will be true: Error!!
throw new Error('Protocol "' + protocol + '" not supported. ' +
'Expected "' + expectedProtocol + '"');
} |
@mdobson Thanks for the answer! |
@mdobson It worked as you said, sorry about the delay and thank you again! |
Great. One small change. You'll want to create an https.Agent outside of the onrequest function, or you'll get some performance issues down the line. var https = require('https');
var agent = new https.Agent({
keepAlive: true
})
//...elided
onrequest: function(req, res, next) {
res.proxy.agent = agent;
//...elided
} |
Great! thanks for the tip! |
@mdobson is this issue addressed by https://github.com/apigee-internal/microgateway/releases/tag/v2.4.6-beta or should we still use that tiop? |
Still need to use the above. |
Hello All,
I have a plugin that overrides the target URL for one set in the plugin configuration.
On this plugin, i'm changing the
req.targetHostname and req.targetPath
and, if the target protocol is https, i'm also changing thereq.targetSecure
totrue
and thereq.targetPort
to 443With this, when the request is executed, i'm getting the following error:
Just to be sure, i've configured my edgemicro instance to work with ssl but the error was the same:
Is it possible to do this? Change the target URL and also change from HTTP to HTTPS?
I'm using the following versions:
The text was updated successfully, but these errors were encountered: