2013-04-12 01:11:12 +02:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
|
|
var util = require('util');
|
|
|
|
var net = require('net');
|
|
|
|
var EventEmitter = require('events').EventEmitter;
|
|
|
|
var HTTPParser = process.binding('http_parser').HTTPParser;
|
|
|
|
var assert = require('assert').ok;
|
|
|
|
|
|
|
|
var common = require('_http_common');
|
|
|
|
|
|
|
|
var httpSocketSetup = common.httpSocketSetup;
|
|
|
|
var parsers = common.parsers;
|
|
|
|
var freeParser = common.freeParser;
|
|
|
|
var debug = common.debug;
|
|
|
|
|
|
|
|
var IncomingMessage = require('_http_incoming').IncomingMessage;
|
|
|
|
var OutgoingMessage = require('_http_outgoing').OutgoingMessage;
|
|
|
|
|
|
|
|
var agent = require('_http_agent');
|
|
|
|
var globalAgent = agent.globalAgent;
|
|
|
|
|
|
|
|
|
|
|
|
function ClientRequest(options, cb) {
|
|
|
|
var self = this;
|
|
|
|
OutgoingMessage.call(self);
|
|
|
|
|
2013-08-04 08:39:50 +02:00
|
|
|
options = util._extend({}, options);
|
|
|
|
|
2013-07-26 23:38:08 +02:00
|
|
|
self.agent = util.isUndefined(options.agent) ? globalAgent : options.agent;
|
2013-04-12 01:11:12 +02:00
|
|
|
|
2013-08-04 08:39:50 +02:00
|
|
|
var defaultPort = options.defaultPort || self.agent.defaultPort;
|
2013-04-12 01:11:12 +02:00
|
|
|
|
2013-08-04 08:39:50 +02:00
|
|
|
var port = options.port = options.port || defaultPort;
|
|
|
|
var host = options.host = options.hostname || options.host || 'localhost';
|
2013-04-12 01:11:12 +02:00
|
|
|
|
2013-07-26 23:38:08 +02:00
|
|
|
if (util.isUndefined(options.setHost)) {
|
2013-04-12 01:11:12 +02:00
|
|
|
var setHost = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.socketPath = options.socketPath;
|
|
|
|
|
|
|
|
var method = self.method = (options.method || 'GET').toUpperCase();
|
|
|
|
self.path = options.path || '/';
|
|
|
|
if (cb) {
|
|
|
|
self.once('response', cb);
|
|
|
|
}
|
|
|
|
|
2013-07-26 23:38:08 +02:00
|
|
|
if (!util.isArray(options.headers)) {
|
2013-04-12 01:11:12 +02:00
|
|
|
if (options.headers) {
|
|
|
|
var keys = Object.keys(options.headers);
|
|
|
|
for (var i = 0, l = keys.length; i < l; i++) {
|
|
|
|
var key = keys[i];
|
|
|
|
self.setHeader(key, options.headers[key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (host && !this.getHeader('host') && setHost) {
|
|
|
|
var hostHeader = host;
|
|
|
|
if (port && +port !== defaultPort) {
|
|
|
|
hostHeader += ':' + port;
|
|
|
|
}
|
|
|
|
this.setHeader('Host', hostHeader);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.auth && !this.getHeader('Authorization')) {
|
|
|
|
//basic auth
|
|
|
|
this.setHeader('Authorization', 'Basic ' +
|
|
|
|
new Buffer(options.auth).toString('base64'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (method === 'GET' || method === 'HEAD' || method === 'CONNECT') {
|
|
|
|
self.useChunkedEncodingByDefault = false;
|
|
|
|
} else {
|
|
|
|
self.useChunkedEncodingByDefault = true;
|
|
|
|
}
|
|
|
|
|
2013-07-26 23:38:08 +02:00
|
|
|
if (util.isArray(options.headers)) {
|
2013-04-12 01:11:12 +02:00
|
|
|
self._storeHeader(self.method + ' ' + self.path + ' HTTP/1.1\r\n',
|
|
|
|
options.headers);
|
|
|
|
} else if (self.getHeader('expect')) {
|
|
|
|
self._storeHeader(self.method + ' ' + self.path + ' HTTP/1.1\r\n',
|
|
|
|
self._renderHeaders());
|
|
|
|
}
|
2013-05-23 03:44:24 +02:00
|
|
|
|
2013-04-12 01:11:12 +02:00
|
|
|
if (self.socketPath) {
|
|
|
|
self._last = true;
|
|
|
|
self.shouldKeepAlive = false;
|
2013-05-23 03:44:24 +02:00
|
|
|
var conn = self.agent.createConnection({ path: self.socketPath });
|
|
|
|
self.onSocket(conn);
|
2013-04-12 01:11:12 +02:00
|
|
|
} else if (self.agent) {
|
2013-08-18 03:50:59 +02:00
|
|
|
// If there is an agent we should default to Connection:keep-alive,
|
|
|
|
// but only if the Agent will actually reuse the connection!
|
|
|
|
// If it's not a keepAlive agent, and the maxSockets==Infinity, then
|
|
|
|
// there's never a case where this socket will actually be reused
|
|
|
|
var agent = self.agent;
|
|
|
|
if (!agent.keepAlive && !Number.isFinite(agent.maxSockets)) {
|
|
|
|
self._last = true;
|
|
|
|
self.shouldKeepAlive = false;
|
|
|
|
} else {
|
|
|
|
self._last = false;
|
|
|
|
self.shouldKeepAlive = true;
|
|
|
|
}
|
2013-05-23 03:44:24 +02:00
|
|
|
self.agent.addRequest(self, options);
|
2013-04-12 01:11:12 +02:00
|
|
|
} else {
|
|
|
|
// No agent, default to Connection:close.
|
|
|
|
self._last = true;
|
|
|
|
self.shouldKeepAlive = false;
|
|
|
|
if (options.createConnection) {
|
|
|
|
var conn = options.createConnection(options);
|
|
|
|
} else {
|
2013-05-23 03:44:24 +02:00
|
|
|
debug('CLIENT use net.createConnection', options);
|
|
|
|
var conn = net.createConnection(options);
|
2013-04-12 01:11:12 +02:00
|
|
|
}
|
|
|
|
self.onSocket(conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
self._deferToConnect(null, null, function() {
|
|
|
|
self._flush();
|
|
|
|
self = null;
|
|
|
|
});
|
|
|
|
}
|
2013-05-23 03:44:24 +02:00
|
|
|
|
2013-04-12 01:11:12 +02:00
|
|
|
util.inherits(ClientRequest, OutgoingMessage);
|
|
|
|
|
|
|
|
exports.ClientRequest = ClientRequest;
|
|
|
|
|
2013-05-31 01:08:03 +02:00
|
|
|
ClientRequest.prototype._finish = function() {
|
|
|
|
DTRACE_HTTP_CLIENT_REQUEST(this, this.connection);
|
|
|
|
COUNTER_HTTP_CLIENT_REQUEST();
|
|
|
|
OutgoingMessage.prototype._finish.call(this);
|
|
|
|
};
|
|
|
|
|
2013-04-12 01:11:12 +02:00
|
|
|
ClientRequest.prototype._implicitHeader = function() {
|
|
|
|
this._storeHeader(this.method + ' ' + this.path + ' HTTP/1.1\r\n',
|
|
|
|
this._renderHeaders());
|
|
|
|
};
|
|
|
|
|
|
|
|
ClientRequest.prototype.abort = function() {
|
2013-07-13 00:18:53 +02:00
|
|
|
// If we're aborting, we don't care about any more response data.
|
2013-07-12 22:26:56 +02:00
|
|
|
if (this.res)
|
|
|
|
this.res._dump();
|
|
|
|
else
|
|
|
|
this.once('response', function(res) {
|
|
|
|
res._dump();
|
|
|
|
});
|
|
|
|
|
2013-04-12 01:11:12 +02:00
|
|
|
if (this.socket) {
|
|
|
|
// in-progress
|
|
|
|
this.socket.destroy();
|
|
|
|
} else {
|
|
|
|
// haven't been assigned a socket yet.
|
|
|
|
// this could be more efficient, it could
|
|
|
|
// remove itself from the pending requests
|
|
|
|
this._deferToConnect('destroy', []);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
function createHangUpError() {
|
|
|
|
var error = new Error('socket hang up');
|
|
|
|
error.code = 'ECONNRESET';
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function socketCloseListener() {
|
|
|
|
var socket = this;
|
|
|
|
var parser = socket.parser;
|
|
|
|
var req = socket._httpMessage;
|
|
|
|
debug('HTTP socket close');
|
http client: pull last chunk on socket close
When the socket closes, the client's http incoming message object was
emitting an 'aborted' event if it had not yet been ended.
However, it's possible, when a response is being repeatedly paused and
resumed (eg, if piped to a slow FS write stream), that there will be a
final chunk remaining in the js-land buffer when the socket is torn
down.
When that happens, the socketCloseListener function detects that we have
not yet reached the end of the response message data, and treats this as
an abrupt abort, immediately (and forcibly) ending the incoming message
data stream, and discarding that final chunk of data.
The result is that, for example, npm will have problems because tarballs
are missing a few bytes off the end, every time.
Closes GH-6402
2013-10-23 22:08:06 +02:00
|
|
|
|
|
|
|
// Pull through final chunk, if anything is buffered.
|
|
|
|
// the ondata function will handle it properly, and this
|
|
|
|
// is a no-op if no final chunk remains.
|
|
|
|
socket.read();
|
|
|
|
|
2013-04-12 01:11:12 +02:00
|
|
|
req.emit('close');
|
|
|
|
if (req.res && req.res.readable) {
|
|
|
|
// Socket closed before we emitted 'end' below.
|
|
|
|
req.res.emit('aborted');
|
|
|
|
var res = req.res;
|
|
|
|
res.on('end', function() {
|
|
|
|
res.emit('close');
|
|
|
|
});
|
|
|
|
res.push(null);
|
2013-06-13 21:47:31 +02:00
|
|
|
} else if (!req.res && !req.socket._hadError) {
|
2013-04-12 01:11:12 +02:00
|
|
|
// This socket error fired before we started to
|
|
|
|
// receive a response. The error needs to
|
|
|
|
// fire on the request.
|
|
|
|
req.emit('error', createHangUpError());
|
2013-06-13 21:47:31 +02:00
|
|
|
req.socket._hadError = true;
|
2013-04-12 01:11:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Too bad. That output wasn't getting written.
|
|
|
|
// This is pretty terrible that it doesn't raise an error.
|
|
|
|
// Fixed better in v0.10
|
|
|
|
if (req.output)
|
|
|
|
req.output.length = 0;
|
|
|
|
if (req.outputEncodings)
|
|
|
|
req.outputEncodings.length = 0;
|
|
|
|
|
|
|
|
if (parser) {
|
|
|
|
parser.finish();
|
|
|
|
freeParser(parser, req);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function socketErrorListener(err) {
|
|
|
|
var socket = this;
|
|
|
|
var parser = socket.parser;
|
|
|
|
var req = socket._httpMessage;
|
2013-05-22 00:22:05 +02:00
|
|
|
debug('SOCKET ERROR:', err.message, err.stack);
|
2013-04-12 01:11:12 +02:00
|
|
|
|
|
|
|
if (req) {
|
|
|
|
req.emit('error', err);
|
|
|
|
// For Safety. Some additional errors might fire later on
|
|
|
|
// and we need to make sure we don't double-fire the error event.
|
2013-06-13 21:47:31 +02:00
|
|
|
req.socket._hadError = true;
|
2013-04-12 01:11:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (parser) {
|
|
|
|
parser.finish();
|
|
|
|
freeParser(parser, req);
|
|
|
|
}
|
|
|
|
socket.destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
function socketOnEnd() {
|
|
|
|
var socket = this;
|
|
|
|
var req = this._httpMessage;
|
|
|
|
var parser = this.parser;
|
|
|
|
|
2013-06-13 21:47:31 +02:00
|
|
|
if (!req.res && !req.socket._hadError) {
|
2013-04-12 01:11:12 +02:00
|
|
|
// If we don't have a response then we know that the socket
|
|
|
|
// ended prematurely and we need to emit an error on the request.
|
|
|
|
req.emit('error', createHangUpError());
|
2013-06-13 21:47:31 +02:00
|
|
|
req.socket._hadError = true;
|
2013-04-12 01:11:12 +02:00
|
|
|
}
|
|
|
|
if (parser) {
|
|
|
|
parser.finish();
|
|
|
|
freeParser(parser, req);
|
|
|
|
}
|
|
|
|
socket.destroy();
|
|
|
|
}
|
|
|
|
|
2013-06-21 20:35:29 +02:00
|
|
|
function socketOnData(d) {
|
2013-04-12 01:11:12 +02:00
|
|
|
var socket = this;
|
|
|
|
var req = this._httpMessage;
|
|
|
|
var parser = this.parser;
|
|
|
|
|
2013-07-26 04:33:15 +02:00
|
|
|
assert(parser);
|
|
|
|
|
2013-06-21 20:35:29 +02:00
|
|
|
var ret = parser.execute(d);
|
2013-04-12 01:11:12 +02:00
|
|
|
if (ret instanceof Error) {
|
|
|
|
debug('parse error');
|
|
|
|
freeParser(parser, req);
|
|
|
|
socket.destroy();
|
|
|
|
req.emit('error', ret);
|
2013-06-13 21:47:31 +02:00
|
|
|
req.socket._hadError = true;
|
2013-04-12 01:11:12 +02:00
|
|
|
} else if (parser.incoming && parser.incoming.upgrade) {
|
|
|
|
// Upgrade or CONNECT
|
|
|
|
var bytesParsed = ret;
|
|
|
|
var res = parser.incoming;
|
|
|
|
req.res = res;
|
|
|
|
|
2013-07-26 04:33:15 +02:00
|
|
|
socket.removeListener('data', socketOnData);
|
|
|
|
socket.removeListener('end', socketOnEnd);
|
2013-04-12 01:11:12 +02:00
|
|
|
parser.finish();
|
|
|
|
|
2013-06-21 20:35:29 +02:00
|
|
|
var bodyHead = d.slice(bytesParsed, d.length);
|
2013-04-12 01:11:12 +02:00
|
|
|
|
|
|
|
var eventName = req.method === 'CONNECT' ? 'connect' : 'upgrade';
|
|
|
|
if (EventEmitter.listenerCount(req, eventName) > 0) {
|
|
|
|
req.upgradeOrConnect = true;
|
|
|
|
|
|
|
|
// detach the socket
|
|
|
|
socket.emit('agentRemove');
|
|
|
|
socket.removeListener('close', socketCloseListener);
|
|
|
|
socket.removeListener('error', socketErrorListener);
|
|
|
|
|
2013-07-26 04:33:15 +02:00
|
|
|
// TODO(isaacs): Need a way to reset a stream to fresh state
|
|
|
|
// IE, not flowing, and not explicitly paused.
|
|
|
|
socket._readableState.flowing = null;
|
|
|
|
|
2013-06-25 20:12:33 +02:00
|
|
|
req.emit(eventName, res, socket, bodyHead);
|
2013-04-12 01:11:12 +02:00
|
|
|
req.emit('close');
|
|
|
|
} else {
|
|
|
|
// Got Upgrade header or CONNECT method, but have no handler.
|
|
|
|
socket.destroy();
|
|
|
|
}
|
|
|
|
freeParser(parser, req);
|
|
|
|
} else if (parser.incoming && parser.incoming.complete &&
|
|
|
|
// When the status code is 100 (Continue), the server will
|
|
|
|
// send a final response after this client sends a request
|
|
|
|
// body. So, we must not free the parser.
|
|
|
|
parser.incoming.statusCode !== 100) {
|
2013-07-26 04:33:15 +02:00
|
|
|
socket.removeListener('data', socketOnData);
|
|
|
|
socket.removeListener('end', socketOnEnd);
|
2013-04-12 01:11:12 +02:00
|
|
|
freeParser(parser, req);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// client
|
|
|
|
function parserOnIncomingClient(res, shouldKeepAlive) {
|
|
|
|
var socket = this.socket;
|
|
|
|
var req = socket._httpMessage;
|
|
|
|
|
|
|
|
|
|
|
|
// propogate "domain" setting...
|
|
|
|
if (req.domain && !res.domain) {
|
|
|
|
debug('setting "res.domain"');
|
|
|
|
res.domain = req.domain;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug('AGENT incoming response!');
|
|
|
|
|
|
|
|
if (req.res) {
|
|
|
|
// We already have a response object, this means the server
|
|
|
|
// sent a double response.
|
|
|
|
socket.destroy();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
req.res = res;
|
|
|
|
|
|
|
|
// Responses to CONNECT request is handled as Upgrade.
|
|
|
|
if (req.method === 'CONNECT') {
|
|
|
|
res.upgrade = true;
|
|
|
|
return true; // skip body
|
|
|
|
}
|
|
|
|
|
|
|
|
// Responses to HEAD requests are crazy.
|
|
|
|
// HEAD responses aren't allowed to have an entity-body
|
|
|
|
// but *can* have a content-length which actually corresponds
|
|
|
|
// to the content-length of the entity-body had the request
|
|
|
|
// been a GET.
|
|
|
|
var isHeadResponse = req.method == 'HEAD';
|
2013-05-22 00:22:05 +02:00
|
|
|
debug('AGENT isHeadResponse', isHeadResponse);
|
2013-04-12 01:11:12 +02:00
|
|
|
|
|
|
|
if (res.statusCode == 100) {
|
|
|
|
// restart the parser, as this is a continue message.
|
|
|
|
delete req.res; // Clear res so that we don't hit double-responses.
|
|
|
|
req.emit('continue');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.shouldKeepAlive && !shouldKeepAlive && !req.upgradeOrConnect) {
|
|
|
|
// Server MUST respond with Connection:keep-alive for us to enable it.
|
|
|
|
// If we've been upgraded (via WebSockets) we also shouldn't try to
|
|
|
|
// keep the connection open.
|
|
|
|
req.shouldKeepAlive = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DTRACE_HTTP_CLIENT_RESPONSE(socket, req);
|
|
|
|
COUNTER_HTTP_CLIENT_RESPONSE();
|
|
|
|
req.res = res;
|
|
|
|
res.req = req;
|
|
|
|
|
|
|
|
// add our listener first, so that we guarantee socket cleanup
|
|
|
|
res.on('end', responseOnEnd);
|
|
|
|
var handled = req.emit('response', res);
|
|
|
|
|
|
|
|
// If the user did not listen for the 'response' event, then they
|
|
|
|
// can't possibly read the data, so we ._dump() it into the void
|
|
|
|
// so that the socket doesn't hang there in a paused state.
|
|
|
|
if (!handled)
|
|
|
|
res._dump();
|
|
|
|
|
|
|
|
return isHeadResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
// client
|
|
|
|
function responseOnEnd() {
|
|
|
|
var res = this;
|
|
|
|
var req = res.req;
|
|
|
|
var socket = req.socket;
|
|
|
|
|
|
|
|
if (!req.shouldKeepAlive) {
|
|
|
|
if (socket.writable) {
|
|
|
|
debug('AGENT socket.destroySoon()');
|
|
|
|
socket.destroySoon();
|
|
|
|
}
|
|
|
|
assert(!socket.writable);
|
|
|
|
} else {
|
|
|
|
debug('AGENT socket keep-alive');
|
|
|
|
if (req.timeoutCb) {
|
|
|
|
socket.setTimeout(0, req.timeoutCb);
|
|
|
|
req.timeoutCb = null;
|
|
|
|
}
|
|
|
|
socket.removeListener('close', socketCloseListener);
|
|
|
|
socket.removeListener('error', socketErrorListener);
|
|
|
|
// Mark this socket as available, AFTER user-added end
|
|
|
|
// handlers have a chance to run.
|
|
|
|
process.nextTick(function() {
|
|
|
|
socket.emit('free');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ClientRequest.prototype.onSocket = function(socket) {
|
|
|
|
var req = this;
|
|
|
|
|
|
|
|
process.nextTick(function() {
|
|
|
|
var parser = parsers.alloc();
|
|
|
|
req.socket = socket;
|
|
|
|
req.connection = socket;
|
|
|
|
parser.reinitialize(HTTPParser.RESPONSE);
|
|
|
|
parser.socket = socket;
|
|
|
|
parser.incoming = null;
|
|
|
|
req.parser = parser;
|
|
|
|
|
|
|
|
socket.parser = parser;
|
|
|
|
socket._httpMessage = req;
|
|
|
|
|
|
|
|
// Setup "drain" propogation.
|
|
|
|
httpSocketSetup(socket);
|
|
|
|
|
|
|
|
// Propagate headers limit from request object to parser
|
2013-07-26 23:38:08 +02:00
|
|
|
if (util.isNumber(req.maxHeadersCount)) {
|
2013-04-12 01:11:12 +02:00
|
|
|
parser.maxHeaderPairs = req.maxHeadersCount << 1;
|
|
|
|
} else {
|
|
|
|
// Set default value because parser may be reused from FreeList
|
|
|
|
parser.maxHeaderPairs = 2000;
|
|
|
|
}
|
|
|
|
|
2013-07-26 04:33:15 +02:00
|
|
|
parser.onIncoming = parserOnIncomingClient;
|
2013-04-12 01:11:12 +02:00
|
|
|
socket.on('error', socketErrorListener);
|
2013-07-26 04:33:15 +02:00
|
|
|
socket.on('data', socketOnData);
|
|
|
|
socket.on('end', socketOnEnd);
|
2013-04-12 01:11:12 +02:00
|
|
|
socket.on('close', socketCloseListener);
|
|
|
|
req.emit('socket', socket);
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
ClientRequest.prototype._deferToConnect = function(method, arguments_, cb) {
|
|
|
|
// This function is for calls that need to happen once the socket is
|
|
|
|
// connected and writable. It's an important promisy thing for all the socket
|
|
|
|
// calls that happen either now (when a socket is assigned) or
|
|
|
|
// in the future (when a socket gets assigned out of the pool and is
|
|
|
|
// eventually writable).
|
|
|
|
var self = this;
|
|
|
|
var onSocket = function() {
|
|
|
|
if (self.socket.writable) {
|
|
|
|
if (method) {
|
|
|
|
self.socket[method].apply(self.socket, arguments_);
|
|
|
|
}
|
|
|
|
if (cb) { cb(); }
|
|
|
|
} else {
|
|
|
|
self.socket.once('connect', function() {
|
|
|
|
if (method) {
|
|
|
|
self.socket[method].apply(self.socket, arguments_);
|
|
|
|
}
|
|
|
|
if (cb) { cb(); }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!self.socket) {
|
|
|
|
self.once('socket', onSocket);
|
|
|
|
} else {
|
|
|
|
onSocket();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ClientRequest.prototype.setTimeout = function(msecs, callback) {
|
|
|
|
if (callback) this.once('timeout', callback);
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
function emitTimeout() {
|
|
|
|
self.emit('timeout');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.socket && this.socket.writable) {
|
|
|
|
if (this.timeoutCb)
|
|
|
|
this.socket.setTimeout(0, this.timeoutCb);
|
|
|
|
this.timeoutCb = emitTimeout;
|
|
|
|
this.socket.setTimeout(msecs, emitTimeout);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set timeoutCb so that it'll get cleaned up on request end
|
|
|
|
this.timeoutCb = emitTimeout;
|
|
|
|
if (this.socket) {
|
|
|
|
var sock = this.socket;
|
|
|
|
this.socket.once('connect', function() {
|
|
|
|
sock.setTimeout(msecs, emitTimeout);
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.once('socket', function(sock) {
|
|
|
|
sock.setTimeout(msecs, emitTimeout);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
ClientRequest.prototype.setNoDelay = function() {
|
|
|
|
this._deferToConnect('setNoDelay', arguments);
|
|
|
|
};
|
|
|
|
ClientRequest.prototype.setSocketKeepAlive = function() {
|
|
|
|
this._deferToConnect('setKeepAlive', arguments);
|
|
|
|
};
|
|
|
|
|
|
|
|
ClientRequest.prototype.clearTimeout = function(cb) {
|
|
|
|
this.setTimeout(0, cb);
|
|
|
|
};
|