|
|
'use strict'; |
|
|
|
|
|
var chunkLength = require('../../lib/chunkLength'); |
|
|
|
|
|
function sendProxyRequest(Container) { |
|
|
var req = Container.user.req; |
|
|
var bodyContent = Container.proxy.bodyContent; |
|
|
var reqOpt = Container.proxy.reqBuilder; |
|
|
var options = Container.options; |
|
|
|
|
|
return new Promise(function (resolve, reject) { |
|
|
var protocol = Container.proxy.requestModule; |
|
|
var proxyReq = Container.proxy.req = protocol.request(reqOpt, function (rsp) { |
|
|
if (options.stream) { |
|
|
Container.proxy.res = rsp; |
|
|
return resolve(Container); |
|
|
} |
|
|
|
|
|
var chunks = []; |
|
|
rsp.on('data', function (chunk) { chunks.push(chunk); }); |
|
|
rsp.on('end', function () { |
|
|
Container.proxy.res = rsp; |
|
|
Container.proxy.resData = Buffer.concat(chunks, chunkLength(chunks)); |
|
|
resolve(Container); |
|
|
}); |
|
|
rsp.on('error', reject); |
|
|
}); |
|
|
|
|
|
proxyReq.on('socket', function (socket) { |
|
|
if (options.timeout) { |
|
|
socket.setTimeout(options.timeout, function () { |
|
|
proxyReq.abort(); |
|
|
}); |
|
|
} |
|
|
}); |
|
|
|
|
|
proxyReq.on('error', reject); |
|
|
|
|
|
|
|
|
|
|
|
if (options.parseReqBody) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (bodyContent.length) { |
|
|
var body = bodyContent; |
|
|
var contentType = proxyReq.getHeader('Content-Type'); |
|
|
if (contentType === 'x-www-form-urlencoded' || contentType === 'application/x-www-form-urlencoded') { |
|
|
try { |
|
|
var params = JSON.parse(body); |
|
|
body = Object.keys(params).map(function (k) { return k + '=' + params[k]; }).join('&'); |
|
|
} catch (e) { |
|
|
|
|
|
} |
|
|
} |
|
|
proxyReq.setHeader('Content-Length', Buffer.byteLength(body)); |
|
|
proxyReq.write(body); |
|
|
} |
|
|
proxyReq.end(); |
|
|
} else { |
|
|
|
|
|
|
|
|
req.pipe(proxyReq); |
|
|
} |
|
|
|
|
|
req.on('aborted', function () { |
|
|
|
|
|
|
|
|
proxyReq.abort(); |
|
|
}); |
|
|
}); |
|
|
} |
|
|
|
|
|
|
|
|
module.exports = sendProxyRequest; |
|
|
|