Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
this.proxy = function(options, callback){
debug("proxy options",options.proxy);
// creare a new proxy tunnel, and use to connect to API URL
var proxyTunnel = http.request(options.proxy),
self = this;
proxyTunnel.on('connect',function(res, socket, head){
debug("proxy connected",socket);
// set tunnel socket in request options, that's the tunnel
// itself
options.socket = socket;
var buffer=[],
protocol = (options.protocol =="http")?http:https,
clientRequest = options.clientRequest,
requestConfig = options.requestConfig,
responseConfig = options.responseConfig;
import { http, https } from 'follow-redirects';
http.request({
host: 'localhost',
path: '/a/b',
port: 8000,
maxRedirects: 12,
beforeRedirect: (options) => {
options.followRedirects = false;
}
}, (response) => {
console.log(response.responseUrl, response.redirects);
response.on('data', (chunk) => {
console.log(chunk);
});
}).on('error', (err) => {
console.error(err);
});
this._prepareRequestOptions(function(error, requestOptions) {
if (error) {
return callback(error);
}
var request = http.request(requestOptions);
request.on('response', function(response) {
if (Math.floor(response.statusCode / 100) !== 2) {
return callback(new Error("Invalid status '" + response.statusCode +
"' message='" + response.statusMessage + "' for url=" + url));
}
callback(null, response);
});
request.on('error', function(error) {
callback(error + " for url=" + url);
});
});
};
data = JSON.stringify(opts);
optionsf.headers['Content-Type'] = 'application/json';
}
if(data) {
optionsf.headers['Content-Length'] = data.length;
}
if(this.socketPath) {
optionsf.socketPath = this.socketPath;
} else {
optionsf.hostname = url.parse(address).hostname;
optionsf.port = url.parse(address).port;
}
var req = http.request(optionsf, function() {});
req.on('response', function(res) {
if (options.isStream) {
self.buildPayload(null, options.isStream, options.statusCodes, res, null, callback);
} else {
var chunks = '';
res.on('data', function(chunk) {
chunks += chunk;
});
res.on('end', function() {
var json;
try {
json = JSON.parse(chunks);
} catch(e) {
json = chunks;
exports.proxy = function(req, res) {
if (!req.headers.referer)
return res.status(400).send();
var ref_parts = url.parse(req.headers.referer, true);
if (ref_parts.host !== config.hostname)
return res.status(400).send();
var url_parts = url.parse(req.query.url, true);
return http.request(
{
host: url_parts.host,
path: url_parts.path,
},
(response) => {
if (response.statusCode === 200) {
res.writeHead(200, {
'Content-Type': response.headers['content-type'],
});
response.pipe(res);
} else {
res.writeHead(response.statusCode);
res.end();
}
}
).end();
});
res.on('error', error => {
reject(error);
});
responseStream.on('end', () => {
resolve(buffer);
})
});
req.on('error', err => {
reject(err);
});
req.end();
} else {
let req = http.request(url, res => {
let encoding = res.headers['content-encoding']
let responseStream = res;
let buffer = false;
if (encoding && encoding == 'gzip') {
responseStream = res.pipe(zlib.createGunzip());
} else if (encoding && encoding == 'deflate') {
responseStream = res.pipe(zlib.createInflate())
}
responseStream.on('data', chunk => {
if (!buffer) {
buffer = chunk;
} else {
buffer = Buffer.concat([buffer, chunk], buffer.length + chunk.length);
}