Browse Source

http: replace uses of self

PR-URL: https://github.com/nodejs/node/pull/11594
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
v6
James M Snell 8 years ago
parent
commit
74c1e02642
  1. 139
      lib/_http_client.js
  2. 6
      lib/_http_outgoing.js

139
lib/_http_client.js

@ -66,8 +66,7 @@ function isInvalidPath(s) {
} }
function ClientRequest(options, cb) { function ClientRequest(options, cb) {
var self = this; OutgoingMessage.call(this);
OutgoingMessage.call(self);
if (typeof options === 'string') { if (typeof options === 'string') {
options = url.parse(options); options = url.parse(options);
@ -95,12 +94,12 @@ function ClientRequest(options, cb) {
'Agent option must be an instance of http.Agent, undefined or false.' 'Agent option must be an instance of http.Agent, undefined or false.'
); );
} }
self.agent = agent; this.agent = agent;
var protocol = options.protocol || defaultAgent.protocol; var protocol = options.protocol || defaultAgent.protocol;
var expectedProtocol = defaultAgent.protocol; var expectedProtocol = defaultAgent.protocol;
if (self.agent && self.agent.protocol) if (this.agent && this.agent.protocol)
expectedProtocol = self.agent.protocol; expectedProtocol = this.agent.protocol;
var path; var path;
if (options.path) { if (options.path) {
@ -121,15 +120,15 @@ function ClientRequest(options, cb) {
} }
const defaultPort = options.defaultPort || const defaultPort = options.defaultPort ||
self.agent && self.agent.defaultPort; this.agent && this.agent.defaultPort;
var port = options.port = options.port || defaultPort || 80; var port = options.port = options.port || defaultPort || 80;
var host = options.host = options.hostname || options.host || 'localhost'; var host = options.host = options.hostname || options.host || 'localhost';
var setHost = (options.setHost === undefined); var setHost = (options.setHost === undefined);
self.socketPath = options.socketPath; this.socketPath = options.socketPath;
self.timeout = options.timeout; this.timeout = options.timeout;
var method = options.method; var method = options.method;
var methodIsString = (typeof method === 'string'); var methodIsString = (typeof method === 'string');
@ -141,14 +140,14 @@ function ClientRequest(options, cb) {
if (!common._checkIsHttpToken(method)) { if (!common._checkIsHttpToken(method)) {
throw new TypeError('Method must be a valid HTTP token'); throw new TypeError('Method must be a valid HTTP token');
} }
method = self.method = method.toUpperCase(); method = this.method = method.toUpperCase();
} else { } else {
method = self.method = 'GET'; method = this.method = 'GET';
} }
self.path = options.path || '/'; this.path = options.path || '/';
if (cb) { if (cb) {
self.once('response', cb); this.once('response', cb);
} }
var headersArray = Array.isArray(options.headers); var headersArray = Array.isArray(options.headers);
@ -157,7 +156,7 @@ function ClientRequest(options, cb) {
var keys = Object.keys(options.headers); var keys = Object.keys(options.headers);
for (var i = 0; i < keys.length; i++) { for (var i = 0; i < keys.length; i++) {
var key = keys[i]; var key = keys[i];
self.setHeader(key, options.headers[key]); this.setHeader(key, options.headers[key]);
} }
} }
if (host && !this.getHeader('host') && setHost) { if (host && !this.getHeader('host') && setHost) {
@ -190,21 +189,22 @@ function ClientRequest(options, cb) {
method === 'DELETE' || method === 'DELETE' ||
method === 'OPTIONS' || method === 'OPTIONS' ||
method === 'CONNECT') { method === 'CONNECT') {
self.useChunkedEncodingByDefault = false; this.useChunkedEncodingByDefault = false;
} else { } else {
self.useChunkedEncodingByDefault = true; this.useChunkedEncodingByDefault = true;
} }
if (headersArray) { if (headersArray) {
self._storeHeader(self.method + ' ' + self.path + ' HTTP/1.1\r\n', this._storeHeader(this.method + ' ' + this.path + ' HTTP/1.1\r\n',
options.headers); options.headers);
} else if (self.getHeader('expect')) { } else if (this.getHeader('expect')) {
if (self._header) { if (this._header) {
throw new Error('Can\'t render headers after they are sent to the ' + throw new Error('Can\'t render headers after they are sent to the ' +
'client'); 'client');
} }
self._storeHeader(self.method + ' ' + self.path + ' HTTP/1.1\r\n',
self[outHeadersKey]); this._storeHeader(this.method + ' ' + this.path + ' HTTP/1.1\r\n',
this[outHeadersKey]);
} }
this._ended = false; this._ended = false;
@ -216,72 +216,65 @@ function ClientRequest(options, cb) {
this.maxHeadersCount = null; this.maxHeadersCount = null;
var called = false; var called = false;
if (self.socketPath) {
self._last = true; const oncreate = (err, socket) => {
self.shouldKeepAlive = false; if (called)
return;
called = true;
if (err) {
process.nextTick(() => this.emit('error', err));
return;
}
this.onSocket(socket);
this._deferToConnect(null, null, () => this._flush());
};
if (this.socketPath) {
this._last = true;
this.shouldKeepAlive = false;
const optionsPath = { const optionsPath = {
path: self.socketPath, path: this.socketPath,
timeout: self.timeout timeout: this.timeout
}; };
const newSocket = self.agent.createConnection(optionsPath, oncreate); const newSocket = this.agent.createConnection(optionsPath, oncreate);
if (newSocket && !called) { if (newSocket && !called) {
called = true; called = true;
self.onSocket(newSocket); this.onSocket(newSocket);
} else { } else {
return; return;
} }
} else if (self.agent) { } else if (this.agent) {
// If there is an agent we should default to Connection:keep-alive, // If there is an agent we should default to Connection:keep-alive,
// but only if the Agent will actually reuse the connection! // but only if the Agent will actually reuse the connection!
// If it's not a keepAlive agent, and the maxSockets==Infinity, then // If it's not a keepAlive agent, and the maxSockets==Infinity, then
// there's never a case where this socket will actually be reused // there's never a case where this socket will actually be reused
if (!self.agent.keepAlive && !Number.isFinite(self.agent.maxSockets)) { if (!this.agent.keepAlive && !Number.isFinite(this.agent.maxSockets)) {
self._last = true; this._last = true;
self.shouldKeepAlive = false; this.shouldKeepAlive = false;
} else { } else {
self._last = false; this._last = false;
self.shouldKeepAlive = true; this.shouldKeepAlive = true;
} }
self.agent.addRequest(self, options); this.agent.addRequest(this, options);
} else { } else {
// No agent, default to Connection:close. // No agent, default to Connection:close.
self._last = true; this._last = true;
self.shouldKeepAlive = false; this.shouldKeepAlive = false;
if (typeof options.createConnection === 'function') { if (typeof options.createConnection === 'function') {
const newSocket = options.createConnection(options, oncreate); const newSocket = options.createConnection(options, oncreate);
if (newSocket && !called) { if (newSocket && !called) {
called = true; called = true;
self.onSocket(newSocket); this.onSocket(newSocket);
} else { } else {
return; return;
} }
} else { } else {
debug('CLIENT use net.createConnection', options); debug('CLIENT use net.createConnection', options);
self.onSocket(net.createConnection(options)); this.onSocket(net.createConnection(options));
}
}
function oncreate(err, socket) {
if (called)
return;
called = true;
if (err) {
process.nextTick(function() {
self.emit('error', err);
});
return;
} }
self.onSocket(socket);
self._deferToConnect(null, null, function() {
self._flush();
self = null;
});
} }
self._deferToConnect(null, null, function() { this._deferToConnect(null, null, () => this._flush());
self._flush();
self = null;
});
} }
util.inherits(ClientRequest, OutgoingMessage); util.inherits(ClientRequest, OutgoingMessage);
@ -304,7 +297,7 @@ ClientRequest.prototype._implicitHeader = function _implicitHeader() {
ClientRequest.prototype.abort = function abort() { ClientRequest.prototype.abort = function abort() {
if (!this.aborted) { if (!this.aborted) {
process.nextTick(emitAbortNT, this); process.nextTick(emitAbortNT.bind(this));
} }
// Mark as aborting so we can avoid sending queued request data // Mark as aborting so we can avoid sending queued request data
// This is used as a truthy flag elsewhere. The use of Date.now is for // This is used as a truthy flag elsewhere. The use of Date.now is for
@ -328,8 +321,8 @@ ClientRequest.prototype.abort = function abort() {
}; };
function emitAbortNT(self) { function emitAbortNT() {
self.emit('abort'); this.emit('abort');
} }
@ -681,26 +674,25 @@ function _deferToConnect(method, arguments_, cb) {
// calls that happen either now (when a socket is assigned) or // 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 // in the future (when a socket gets assigned out of the pool and is
// eventually writable). // eventually writable).
var self = this;
function callSocketMethod() { const callSocketMethod = () => {
if (method) if (method)
self.socket[method].apply(self.socket, arguments_); this.socket[method].apply(this.socket, arguments_);
if (typeof cb === 'function') if (typeof cb === 'function')
cb(); cb();
} };
var onSocket = function onSocket() { const onSocket = () => {
if (self.socket.writable) { if (this.socket.writable) {
callSocketMethod(); callSocketMethod();
} else { } else {
self.socket.once('connect', callSocketMethod); this.socket.once('connect', callSocketMethod);
} }
}; };
if (!self.socket) { if (!this.socket) {
self.once('socket', onSocket); this.once('socket', onSocket);
} else { } else {
onSocket(); onSocket();
} }
@ -709,10 +701,7 @@ function _deferToConnect(method, arguments_, cb) {
ClientRequest.prototype.setTimeout = function setTimeout(msecs, callback) { ClientRequest.prototype.setTimeout = function setTimeout(msecs, callback) {
if (callback) this.once('timeout', callback); if (callback) this.once('timeout', callback);
var self = this; const emitTimeout = () => this.emit('timeout');
function emitTimeout() {
self.emit('timeout');
}
if (this.socket && this.socket.writable) { if (this.socket && this.socket.writable) {
if (this.timeoutCb) if (this.timeoutCb)

6
lib/_http_outgoing.js

@ -625,7 +625,7 @@ const crlf_buf = Buffer.from('\r\n');
OutgoingMessage.prototype.write = function write(chunk, encoding, callback) { OutgoingMessage.prototype.write = function write(chunk, encoding, callback) {
if (this.finished) { if (this.finished) {
var err = new Error('write after end'); var err = new Error('write after end');
process.nextTick(writeAfterEndNT, this, err, callback); process.nextTick(writeAfterEndNT.bind(this), err, callback);
return true; return true;
} }
@ -684,8 +684,8 @@ OutgoingMessage.prototype.write = function write(chunk, encoding, callback) {
}; };
function writeAfterEndNT(self, err, callback) { function writeAfterEndNT(err, callback) {
self.emit('error', err); this.emit('error', err);
if (callback) callback(err); if (callback) callback(err);
} }

Loading…
Cancel
Save