in lib/http/node.js [12:114]
handleRequest: function handleRequest(httpRequest, httpOptions, callback, errCallback) {
var self = this;
var endpoint = httpRequest.endpoint;
var pathPrefix = '';
if (!httpOptions) httpOptions = {};
if (httpOptions.proxy) {
pathPrefix = endpoint.protocol + '//' + endpoint.hostname;
if (endpoint.port !== 80 && endpoint.port !== 443) {
pathPrefix += ':' + endpoint.port;
}
endpoint = new AWS.Endpoint(httpOptions.proxy);
}
var useSSL = endpoint.protocol === 'https:';
var http = useSSL ? require('https') : require('http');
var options = {
host: endpoint.hostname,
port: endpoint.port,
method: httpRequest.method,
headers: httpRequest.headers,
path: pathPrefix + httpRequest.path
};
AWS.util.update(options, httpOptions);
if (!httpOptions.agent) {
options.agent = this.getAgent(useSSL, {
keepAlive: process.env[CONNECTION_REUSE_ENV_NAME] === '1' ? true : false
});
}
delete options.proxy; // proxy isn't an HTTP option
delete options.timeout; // timeout isn't an HTTP option
var stream = http.request(options, function (httpResp) {
if (stream.didCallback) return;
callback(httpResp);
httpResp.emit(
'headers',
httpResp.statusCode,
httpResp.headers,
httpResp.statusMessage
);
});
httpRequest.stream = stream; // attach stream to httpRequest
stream.didCallback = false;
// connection timeout support
if (httpOptions.connectTimeout) {
var connectTimeoutId;
stream.on('socket', function(socket) {
if (socket.connecting) {
connectTimeoutId = setTimeout(function connectTimeout() {
if (stream.didCallback) return; stream.didCallback = true;
stream.abort();
errCallback(AWS.util.error(
new Error('Socket timed out without establishing a connection'),
{code: 'TimeoutError'}
));
}, httpOptions.connectTimeout);
socket.on('connect', function() {
clearTimeout(connectTimeoutId);
connectTimeoutId = null;
});
}
});
}
// timeout support
stream.setTimeout(httpOptions.timeout || 0, function() {
if (stream.didCallback) return; stream.didCallback = true;
var msg = 'Connection timed out after ' + httpOptions.timeout + 'ms';
errCallback(AWS.util.error(new Error(msg), {code: 'TimeoutError'}));
stream.abort();
});
stream.on('error', function(err) {
if (connectTimeoutId) {
clearTimeout(connectTimeoutId);
connectTimeoutId = null;
}
if (stream.didCallback) return; stream.didCallback = true;
if ('ECONNRESET' === err.code || 'EPIPE' === err.code || 'ETIMEDOUT' === err.code) {
errCallback(AWS.util.error(err, {code: 'TimeoutError'}));
} else {
errCallback(err);
}
});
var expect = httpRequest.headers.Expect || httpRequest.headers.expect;
if (expect === '100-continue') {
stream.once('continue', function() {
self.writeBody(stream, httpRequest);
});
} else {
this.writeBody(stream, httpRequest);
}
return stream;
},