2017-01-03 22:16:48 +01: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.
|
|
|
|
|
2014-11-22 16:59:48 +01:00
|
|
|
'use strict';
|
|
|
|
|
2015-01-21 17:36:59 +01:00
|
|
|
const util = require('util');
|
|
|
|
const net = require('net');
|
|
|
|
const url = require('url');
|
2017-10-07 16:50:42 +02:00
|
|
|
const { HTTPParser } = process.binding('http_parser');
|
2015-01-21 17:36:59 +01:00
|
|
|
const assert = require('assert').ok;
|
2017-10-07 16:50:42 +02:00
|
|
|
const {
|
|
|
|
_checkIsHttpToken: checkIsHttpToken,
|
|
|
|
debug,
|
|
|
|
freeParser,
|
|
|
|
httpSocketSetup,
|
|
|
|
parsers
|
|
|
|
} = require('_http_common');
|
|
|
|
const { OutgoingMessage } = require('_http_outgoing');
|
2015-01-21 17:36:59 +01:00
|
|
|
const Agent = require('_http_agent');
|
2017-10-07 16:50:42 +02:00
|
|
|
const { Buffer } = require('buffer');
|
2017-05-11 23:57:53 +02:00
|
|
|
const { urlToOptions, searchParamsSymbol } = require('internal/url');
|
2017-10-07 16:50:42 +02:00
|
|
|
const { outHeadersKey } = require('internal/http');
|
|
|
|
const { nextTick } = require('internal/process/next_tick');
|
2017-07-22 13:23:04 +02:00
|
|
|
const errors = require('internal/errors');
|
2013-04-12 01:11:12 +02:00
|
|
|
|
2017-01-06 09:11:38 +01:00
|
|
|
// The actual list of disallowed characters in regexp form is more like:
|
|
|
|
// /[^A-Za-z0-9\-._~!$&'()*+,;=/:@]/
|
|
|
|
// with an additional rule for ignoring percentage-escaped characters, but
|
|
|
|
// that's a) hard to capture in a regular expression that performs well, and
|
|
|
|
// b) possibly too restrictive for real-world usage. So instead we restrict the
|
|
|
|
// filter to just control characters and spaces.
|
|
|
|
//
|
|
|
|
// This function is used in the case of small paths, where manual character code
|
|
|
|
// checks can greatly outperform the equivalent regexp (tested in V8 5.4).
|
|
|
|
function isInvalidPath(s) {
|
|
|
|
var i = 0;
|
|
|
|
if (s.charCodeAt(0) <= 32) return true;
|
|
|
|
if (++i >= s.length) return false;
|
|
|
|
if (s.charCodeAt(1) <= 32) return true;
|
|
|
|
if (++i >= s.length) return false;
|
|
|
|
if (s.charCodeAt(2) <= 32) return true;
|
|
|
|
if (++i >= s.length) return false;
|
|
|
|
if (s.charCodeAt(3) <= 32) return true;
|
|
|
|
if (++i >= s.length) return false;
|
|
|
|
if (s.charCodeAt(4) <= 32) return true;
|
|
|
|
if (++i >= s.length) return false;
|
|
|
|
if (s.charCodeAt(5) <= 32) return true;
|
|
|
|
++i;
|
|
|
|
for (; i < s.length; ++i)
|
|
|
|
if (s.charCodeAt(i) <= 32) return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-04-18 22:33:51 +02:00
|
|
|
function validateHost(host, name) {
|
|
|
|
if (host != null && typeof host !== 'string') {
|
2017-07-22 13:23:04 +02:00
|
|
|
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', `options.${name}`,
|
|
|
|
['string', 'undefined', 'null'], host);
|
2017-04-18 22:33:51 +02:00
|
|
|
}
|
|
|
|
return host;
|
|
|
|
}
|
|
|
|
|
2013-04-12 01:11:12 +02:00
|
|
|
function ClientRequest(options, cb) {
|
2017-02-28 01:53:13 +01:00
|
|
|
OutgoingMessage.call(this);
|
2013-04-12 01:11:12 +02:00
|
|
|
|
2015-01-29 02:05:53 +01:00
|
|
|
if (typeof options === 'string') {
|
2015-05-04 00:05:44 +02:00
|
|
|
options = url.parse(options);
|
2015-09-20 09:37:03 +02:00
|
|
|
if (!options.hostname) {
|
2017-07-22 13:23:04 +02:00
|
|
|
throw new errors.Error('ERR_INVALID_DOMAIN_NAME');
|
2015-09-20 09:37:03 +02:00
|
|
|
}
|
2017-05-11 23:57:53 +02:00
|
|
|
} else if (options && options[searchParamsSymbol] &&
|
|
|
|
options[searchParamsSymbol][searchParamsSymbol]) {
|
|
|
|
// url.URL instance
|
2017-01-05 19:25:46 +01:00
|
|
|
options = urlToOptions(options);
|
2015-04-18 22:22:34 +02:00
|
|
|
} else {
|
|
|
|
options = util._extend({}, options);
|
2014-02-25 23:15:02 +01:00
|
|
|
}
|
2013-08-04 08:39:50 +02:00
|
|
|
|
2014-02-25 23:15:02 +01:00
|
|
|
var agent = options.agent;
|
|
|
|
var defaultAgent = options._defaultAgent || Agent.globalAgent;
|
|
|
|
if (agent === false) {
|
|
|
|
agent = new defaultAgent.constructor();
|
2016-12-07 03:48:55 +01:00
|
|
|
} else if (agent === null || agent === undefined) {
|
|
|
|
if (typeof options.createConnection !== 'function') {
|
|
|
|
agent = defaultAgent;
|
|
|
|
}
|
|
|
|
// Explicitly pass through this statement as agent will not be used
|
|
|
|
// when createConnection is provided.
|
2017-02-26 18:09:30 +01:00
|
|
|
} else if (typeof agent.addRequest !== 'function') {
|
2017-07-22 13:23:04 +02:00
|
|
|
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'Agent option',
|
|
|
|
['Agent-like object', 'undefined', 'false']);
|
2014-02-25 23:15:02 +01:00
|
|
|
}
|
2017-02-28 01:53:13 +01:00
|
|
|
this.agent = agent;
|
2014-02-25 23:15:02 +01:00
|
|
|
|
2014-03-24 23:59:31 +01:00
|
|
|
var protocol = options.protocol || defaultAgent.protocol;
|
|
|
|
var expectedProtocol = defaultAgent.protocol;
|
2017-02-28 01:53:13 +01:00
|
|
|
if (this.agent && this.agent.protocol)
|
|
|
|
expectedProtocol = this.agent.protocol;
|
2014-03-24 23:59:31 +01:00
|
|
|
|
2017-01-06 09:11:38 +01:00
|
|
|
var path;
|
|
|
|
if (options.path) {
|
|
|
|
path = '' + options.path;
|
|
|
|
var invalidPath;
|
|
|
|
if (path.length <= 39) { // Determined experimentally in V8 5.4
|
|
|
|
invalidPath = isInvalidPath(path);
|
|
|
|
} else {
|
|
|
|
invalidPath = /[\u0000-\u0020]/.test(path);
|
|
|
|
}
|
|
|
|
if (invalidPath)
|
2017-07-22 13:23:04 +02:00
|
|
|
throw new errors.TypeError('ERR_UNESCAPED_CHARACTERS', 'Request path');
|
2017-01-06 09:11:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (protocol !== expectedProtocol) {
|
2017-07-22 13:23:04 +02:00
|
|
|
throw new errors.Error('ERR_INVALID_PROTOCOL', protocol, expectedProtocol);
|
2014-02-25 23:15:02 +01:00
|
|
|
}
|
2013-04-12 01:11:12 +02:00
|
|
|
|
2017-04-24 08:19:30 +02:00
|
|
|
var defaultPort = options.defaultPort ||
|
|
|
|
this.agent && this.agent.defaultPort;
|
2013-04-12 01:11:12 +02:00
|
|
|
|
2014-03-02 20:54:19 +01:00
|
|
|
var port = options.port = options.port || defaultPort || 80;
|
2017-04-18 22:33:51 +02:00
|
|
|
var host = options.host = validateHost(options.hostname, 'hostname') ||
|
|
|
|
validateHost(options.host, 'host') || 'localhost';
|
2013-04-12 01:11:12 +02:00
|
|
|
|
2017-01-06 09:13:57 +01:00
|
|
|
var setHost = (options.setHost === undefined);
|
2013-04-12 01:11:12 +02:00
|
|
|
|
2017-02-28 01:53:13 +01:00
|
|
|
this.socketPath = options.socketPath;
|
|
|
|
this.timeout = options.timeout;
|
2013-04-12 01:11:12 +02:00
|
|
|
|
2016-12-04 09:32:51 +01:00
|
|
|
var method = options.method;
|
2017-01-06 09:12:59 +01:00
|
|
|
var methodIsString = (typeof method === 'string');
|
|
|
|
if (method != null && !methodIsString) {
|
2017-07-22 13:23:04 +02:00
|
|
|
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'method',
|
|
|
|
'string', method);
|
2016-12-04 09:32:51 +01:00
|
|
|
}
|
2017-01-06 09:12:59 +01:00
|
|
|
|
|
|
|
if (methodIsString && method) {
|
2017-10-07 16:50:42 +02:00
|
|
|
if (!checkIsHttpToken(method)) {
|
2017-08-10 12:58:22 +02:00
|
|
|
throw new errors.TypeError('ERR_INVALID_HTTP_TOKEN', 'Method', method);
|
2017-01-06 09:12:59 +01:00
|
|
|
}
|
2017-02-28 01:53:13 +01:00
|
|
|
method = this.method = method.toUpperCase();
|
2017-01-06 09:12:59 +01:00
|
|
|
} else {
|
2017-02-28 01:53:13 +01:00
|
|
|
method = this.method = 'GET';
|
2015-08-24 20:58:48 +02:00
|
|
|
}
|
2017-01-06 09:12:59 +01:00
|
|
|
|
2017-02-28 01:53:13 +01:00
|
|
|
this.path = options.path || '/';
|
2013-04-12 01:11:12 +02:00
|
|
|
if (cb) {
|
2017-02-28 01:53:13 +01:00
|
|
|
this.once('response', cb);
|
2013-04-12 01:11:12 +02:00
|
|
|
}
|
|
|
|
|
2017-01-06 09:13:33 +01:00
|
|
|
var headersArray = Array.isArray(options.headers);
|
|
|
|
if (!headersArray) {
|
2013-04-12 01:11:12 +02:00
|
|
|
if (options.headers) {
|
|
|
|
var keys = Object.keys(options.headers);
|
2017-01-06 09:13:57 +01:00
|
|
|
for (var i = 0; i < keys.length; i++) {
|
2013-04-12 01:11:12 +02:00
|
|
|
var key = keys[i];
|
2017-02-28 01:53:13 +01:00
|
|
|
this.setHeader(key, options.headers[key]);
|
2013-04-12 01:11:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (host && !this.getHeader('host') && setHost) {
|
|
|
|
var hostHeader = host;
|
2016-02-19 09:21:49 +01:00
|
|
|
|
|
|
|
// For the Host header, ensure that IPv6 addresses are enclosed
|
|
|
|
// in square brackets, as defined by URI formatting
|
|
|
|
// https://tools.ietf.org/html/rfc3986#section-3.2.2
|
2017-05-19 23:14:53 +02:00
|
|
|
var posColon = hostHeader.indexOf(':');
|
|
|
|
if (posColon !== -1 &&
|
|
|
|
hostHeader.indexOf(':', posColon + 1) !== -1 &&
|
|
|
|
hostHeader.charCodeAt(0) !== 91/*'['*/) {
|
2016-02-19 09:21:49 +01:00
|
|
|
hostHeader = `[${hostHeader}]`;
|
|
|
|
}
|
|
|
|
|
2013-04-12 01:11:12 +02:00
|
|
|
if (port && +port !== defaultPort) {
|
|
|
|
hostHeader += ':' + port;
|
|
|
|
}
|
|
|
|
this.setHeader('Host', hostHeader);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.auth && !this.getHeader('Authorization')) {
|
|
|
|
this.setHeader('Authorization', 'Basic ' +
|
2016-01-26 00:00:06 +01:00
|
|
|
Buffer.from(options.auth).toString('base64'));
|
2013-04-12 01:11:12 +02:00
|
|
|
}
|
|
|
|
|
2013-11-22 03:44:08 +01:00
|
|
|
if (method === 'GET' ||
|
|
|
|
method === 'HEAD' ||
|
|
|
|
method === 'DELETE' ||
|
2014-06-04 05:49:38 +02:00
|
|
|
method === 'OPTIONS' ||
|
2013-11-22 03:44:08 +01:00
|
|
|
method === 'CONNECT') {
|
2017-02-28 01:53:13 +01:00
|
|
|
this.useChunkedEncodingByDefault = false;
|
2013-04-12 01:11:12 +02:00
|
|
|
} else {
|
2017-02-28 01:53:13 +01:00
|
|
|
this.useChunkedEncodingByDefault = true;
|
2013-04-12 01:11:12 +02:00
|
|
|
}
|
|
|
|
|
2017-01-06 09:13:33 +01:00
|
|
|
if (headersArray) {
|
2017-02-28 01:53:13 +01:00
|
|
|
this._storeHeader(this.method + ' ' + this.path + ' HTTP/1.1\r\n',
|
2013-04-12 01:11:12 +02:00
|
|
|
options.headers);
|
2017-02-28 01:53:13 +01:00
|
|
|
} else if (this.getHeader('expect')) {
|
|
|
|
if (this._header) {
|
2017-08-10 12:58:22 +02:00
|
|
|
throw new errors.Error('ERR_HTTP_HEADERS_SENT', 'render');
|
2016-12-31 22:18:43 +01:00
|
|
|
}
|
2017-02-28 01:53:13 +01:00
|
|
|
|
|
|
|
this._storeHeader(this.method + ' ' + this.path + ' HTTP/1.1\r\n',
|
|
|
|
this[outHeadersKey]);
|
2013-04-12 01:11:12 +02:00
|
|
|
}
|
2013-05-23 03:44:24 +02:00
|
|
|
|
2016-10-16 14:47:07 +02:00
|
|
|
this._ended = false;
|
|
|
|
this.res = null;
|
|
|
|
this.aborted = undefined;
|
|
|
|
this.timeoutCb = null;
|
|
|
|
this.upgradeOrConnect = false;
|
|
|
|
this.parser = null;
|
|
|
|
this.maxHeadersCount = null;
|
|
|
|
|
2016-01-12 05:41:01 +01:00
|
|
|
var called = false;
|
2017-02-28 01:53:13 +01:00
|
|
|
|
2017-04-24 08:19:30 +02:00
|
|
|
var oncreate = (err, socket) => {
|
2017-02-28 01:53:13 +01:00
|
|
|
if (called)
|
|
|
|
return;
|
|
|
|
called = true;
|
|
|
|
if (err) {
|
|
|
|
process.nextTick(() => this.emit('error', err));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.onSocket(socket);
|
|
|
|
this._deferToConnect(null, null, () => this._flush());
|
|
|
|
};
|
|
|
|
|
2017-05-25 09:32:04 +02:00
|
|
|
if (this.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
|
2017-02-28 01:53:13 +01:00
|
|
|
if (!this.agent.keepAlive && !Number.isFinite(this.agent.maxSockets)) {
|
|
|
|
this._last = true;
|
|
|
|
this.shouldKeepAlive = false;
|
2013-08-18 03:50:59 +02:00
|
|
|
} else {
|
2017-02-28 01:53:13 +01:00
|
|
|
this._last = false;
|
|
|
|
this.shouldKeepAlive = true;
|
2013-08-18 03:50:59 +02:00
|
|
|
}
|
2017-02-28 01:53:13 +01:00
|
|
|
this.agent.addRequest(this, options);
|
2013-04-12 01:11:12 +02:00
|
|
|
} else {
|
|
|
|
// No agent, default to Connection:close.
|
2017-02-28 01:53:13 +01:00
|
|
|
this._last = true;
|
|
|
|
this.shouldKeepAlive = false;
|
2016-01-12 05:41:01 +01:00
|
|
|
if (typeof options.createConnection === 'function') {
|
2017-05-25 09:32:04 +02:00
|
|
|
const newSocket = options.createConnection(options, oncreate);
|
2016-01-12 05:41:01 +01:00
|
|
|
if (newSocket && !called) {
|
|
|
|
called = true;
|
2017-02-28 01:53:13 +01:00
|
|
|
this.onSocket(newSocket);
|
2016-01-12 05:41:01 +01:00
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
2013-04-12 01:11:12 +02:00
|
|
|
} else {
|
2013-05-23 03:44:24 +02:00
|
|
|
debug('CLIENT use net.createConnection', options);
|
2017-02-28 01:53:13 +01:00
|
|
|
this.onSocket(net.createConnection(options));
|
2013-04-12 01:11:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-28 01:53:13 +01:00
|
|
|
this._deferToConnect(null, null, () => this._flush());
|
2013-04-12 01:11:12 +02:00
|
|
|
}
|
2013-05-23 03:44:24 +02:00
|
|
|
|
2013-04-12 01:11:12 +02:00
|
|
|
util.inherits(ClientRequest, OutgoingMessage);
|
|
|
|
|
|
|
|
|
2016-10-12 13:06:30 +02:00
|
|
|
ClientRequest.prototype._finish = function _finish() {
|
2013-05-31 01:08:03 +02:00
|
|
|
DTRACE_HTTP_CLIENT_REQUEST(this, this.connection);
|
2015-01-22 13:35:16 +01:00
|
|
|
LTTNG_HTTP_CLIENT_REQUEST(this, this.connection);
|
2013-05-31 01:08:03 +02:00
|
|
|
COUNTER_HTTP_CLIENT_REQUEST();
|
|
|
|
OutgoingMessage.prototype._finish.call(this);
|
|
|
|
};
|
|
|
|
|
2016-10-12 13:06:30 +02:00
|
|
|
ClientRequest.prototype._implicitHeader = function _implicitHeader() {
|
2016-12-31 22:18:43 +01:00
|
|
|
if (this._header) {
|
2017-08-10 12:58:22 +02:00
|
|
|
throw new errors.Error('ERR_HTTP_HEADERS_SENT', 'render');
|
2016-12-31 22:18:43 +01:00
|
|
|
}
|
2013-04-12 01:11:12 +02:00
|
|
|
this._storeHeader(this.method + ' ' + this.path + ' HTTP/1.1\r\n',
|
2017-03-06 03:13:09 +01:00
|
|
|
this[outHeadersKey]);
|
2013-04-12 01:11:12 +02:00
|
|
|
};
|
|
|
|
|
2016-10-12 13:06:30 +02:00
|
|
|
ClientRequest.prototype.abort = function abort() {
|
2016-10-16 14:47:07 +02:00
|
|
|
if (!this.aborted) {
|
2017-02-28 01:53:13 +01:00
|
|
|
process.nextTick(emitAbortNT.bind(this));
|
2015-02-25 01:11:11 +01:00
|
|
|
}
|
2014-04-30 15:20:25 +02:00
|
|
|
// Mark as aborting so we can avoid sending queued request data
|
2014-05-22 21:55:03 +02:00
|
|
|
// This is used as a truthy flag elsewhere. The use of Date.now is for
|
|
|
|
// debugging purposes only.
|
2014-04-30 15:20:25 +02:00
|
|
|
this.aborted = Date.now();
|
|
|
|
|
2013-07-13 00:18:53 +02:00
|
|
|
// If we're aborting, we don't care about any more response data.
|
2017-06-20 23:37:00 +02:00
|
|
|
if (this.res) {
|
2013-07-12 22:26:56 +02:00
|
|
|
this.res._dump();
|
2017-06-20 23:37:00 +02:00
|
|
|
} else {
|
2013-07-12 22:26:56 +02:00
|
|
|
this.once('response', function(res) {
|
|
|
|
res._dump();
|
|
|
|
});
|
2017-06-20 23:37:00 +02:00
|
|
|
}
|
2013-07-12 22:26:56 +02:00
|
|
|
|
2014-04-30 15:20:25 +02:00
|
|
|
// In the event that we don't have a socket, we will pop out of
|
|
|
|
// the request queue through handling in onSocket.
|
2013-04-12 01:11:12 +02:00
|
|
|
if (this.socket) {
|
|
|
|
// in-progress
|
|
|
|
this.socket.destroy();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-02-28 01:53:13 +01:00
|
|
|
function emitAbortNT() {
|
|
|
|
this.emit('abort');
|
2015-03-05 22:07:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-12 01:11:12 +02:00
|
|
|
function createHangUpError() {
|
|
|
|
var error = new Error('socket hang up');
|
|
|
|
error.code = 'ECONNRESET';
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function socketCloseListener() {
|
|
|
|
var socket = this;
|
|
|
|
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();
|
|
|
|
|
2015-08-22 08:07:09 +02:00
|
|
|
// NOTE: It's important to get parser here, because it could be freed by
|
2013-11-27 11:54:58 +01:00
|
|
|
// the `socketOnData`.
|
|
|
|
var parser = socket.parser;
|
2013-04-12 01:11:12 +02:00
|
|
|
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.
|
2013-06-13 21:47:31 +02:00
|
|
|
req.socket._hadError = true;
|
2017-07-18 05:05:05 +02:00
|
|
|
req.emit('error', createHangUpError());
|
2013-04-12 01:11:12 +02:00
|
|
|
}
|
2017-09-24 20:27:44 +02:00
|
|
|
req.emit('close');
|
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();
|
2014-08-23 00:57:45 +02:00
|
|
|
freeParser(parser, req, socket);
|
2013-04-12 01:11:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function socketErrorListener(err) {
|
|
|
|
var socket = this;
|
|
|
|
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) {
|
|
|
|
// 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;
|
2017-07-18 05:05:05 +02:00
|
|
|
req.emit('error', err);
|
2013-04-12 01:11:12 +02:00
|
|
|
}
|
|
|
|
|
2015-03-09 01:30:01 +01:00
|
|
|
// Handle any pending data
|
|
|
|
socket.read();
|
|
|
|
|
|
|
|
var parser = socket.parser;
|
2013-04-12 01:11:12 +02:00
|
|
|
if (parser) {
|
|
|
|
parser.finish();
|
2014-08-23 00:57:45 +02:00
|
|
|
freeParser(parser, req, socket);
|
2013-04-12 01:11:12 +02:00
|
|
|
}
|
2015-03-09 01:30:01 +01:00
|
|
|
|
|
|
|
// Ensure that no further data will come out of the socket
|
|
|
|
socket.removeListener('data', socketOnData);
|
|
|
|
socket.removeListener('end', socketOnEnd);
|
2013-04-12 01:11:12 +02:00
|
|
|
socket.destroy();
|
|
|
|
}
|
|
|
|
|
2015-12-31 15:51:46 +01:00
|
|
|
function freeSocketErrorListener(err) {
|
|
|
|
var socket = this;
|
|
|
|
debug('SOCKET ERROR on FREE socket:', err.message, err.stack);
|
|
|
|
socket.destroy();
|
|
|
|
socket.emit('agentRemove');
|
|
|
|
}
|
|
|
|
|
2013-04-12 01:11:12 +02:00
|
|
|
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.
|
2013-06-13 21:47:31 +02:00
|
|
|
req.socket._hadError = true;
|
2017-07-18 05:05:05 +02:00
|
|
|
req.emit('error', createHangUpError());
|
2013-04-12 01:11:12 +02:00
|
|
|
}
|
|
|
|
if (parser) {
|
|
|
|
parser.finish();
|
2014-08-23 00:57:45 +02:00
|
|
|
freeParser(parser, req, socket);
|
2013-04-12 01:11:12 +02:00
|
|
|
}
|
|
|
|
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-11-27 11:54:58 +01:00
|
|
|
assert(parser && parser.socket === socket);
|
2013-07-26 04:33:15 +02:00
|
|
|
|
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) {
|
2017-05-24 22:17:34 +02:00
|
|
|
debug('parse error', ret);
|
2014-08-23 00:57:45 +02:00
|
|
|
freeParser(parser, req, socket);
|
2013-04-12 01:11:12 +02:00
|
|
|
socket.destroy();
|
2013-06-13 21:47:31 +02:00
|
|
|
req.socket._hadError = true;
|
2017-07-18 05:05:05 +02:00
|
|
|
req.emit('error', ret);
|
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';
|
2015-08-11 20:31:50 +02:00
|
|
|
if (req.listenerCount(eventName) > 0) {
|
2013-04-12 01:11:12 +02:00
|
|
|
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();
|
|
|
|
}
|
2014-08-23 00:57:45 +02:00
|
|
|
freeParser(parser, req, socket);
|
2013-04-12 01:11:12 +02:00
|
|
|
} 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);
|
2014-08-23 00:57:45 +02:00
|
|
|
freeParser(parser, req, socket);
|
2013-04-12 01:11:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// client
|
|
|
|
function parserOnIncomingClient(res, shouldKeepAlive) {
|
|
|
|
var socket = this.socket;
|
|
|
|
var req = socket._httpMessage;
|
|
|
|
|
|
|
|
|
2015-06-24 05:42:49 +02:00
|
|
|
// propagate "domain" setting...
|
2013-04-12 01:11:12 +02:00
|
|
|
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;
|
2016-04-19 16:51:05 +02:00
|
|
|
return 2; // skip body, and the rest
|
2013-04-12 01:11:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2014-03-01 03:09:29 +01:00
|
|
|
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
|
|
|
|
2014-03-01 03:09:29 +01:00
|
|
|
if (res.statusCode === 100) {
|
2013-04-12 01:11:12 +02:00
|
|
|
// restart the parser, as this is a continue message.
|
2016-10-16 14:47:07 +02:00
|
|
|
req.res = null; // Clear res so that we don't hit double-responses.
|
2013-04-12 01:11:12 +02:00
|
|
|
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);
|
2015-01-22 13:35:16 +01:00
|
|
|
LTTNG_HTTP_CLIENT_RESPONSE(socket, req);
|
2013-04-12 01:11:12 +02:00
|
|
|
COUNTER_HTTP_CLIENT_RESPONSE();
|
|
|
|
req.res = res;
|
|
|
|
res.req = req;
|
|
|
|
|
|
|
|
// add our listener first, so that we guarantee socket cleanup
|
|
|
|
res.on('end', responseOnEnd);
|
2016-06-04 05:59:04 +02:00
|
|
|
req.on('prefinish', requestOnPrefinish);
|
2013-04-12 01:11:12 +02:00
|
|
|
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
|
2016-06-04 05:59:04 +02:00
|
|
|
function responseKeepAlive(res, req) {
|
2013-04-12 01:11:12 +02:00
|
|
|
var socket = req.socket;
|
|
|
|
|
|
|
|
if (!req.shouldKeepAlive) {
|
|
|
|
if (socket.writable) {
|
|
|
|
debug('AGENT socket.destroySoon()');
|
2017-10-17 22:19:27 +02:00
|
|
|
if (typeof socket.destroySoon === 'function')
|
|
|
|
socket.destroySoon();
|
|
|
|
else
|
|
|
|
socket.end();
|
2013-04-12 01:11:12 +02:00
|
|
|
}
|
|
|
|
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);
|
2015-12-31 15:51:46 +01:00
|
|
|
socket.once('error', freeSocketErrorListener);
|
2017-03-10 14:17:42 +01:00
|
|
|
// There are cases where _handle === null. Avoid those. Passing null to
|
|
|
|
// nextTick() will call initTriggerId() to retrieve the id.
|
|
|
|
const asyncId = socket._handle ? socket._handle.getAsyncId() : null;
|
2013-04-12 01:11:12 +02:00
|
|
|
// Mark this socket as available, AFTER user-added end
|
|
|
|
// handlers have a chance to run.
|
2017-03-10 14:17:42 +01:00
|
|
|
nextTick(asyncId, emitFreeNT, socket);
|
2013-04-12 01:11:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-04 05:59:04 +02:00
|
|
|
function responseOnEnd() {
|
|
|
|
const res = this;
|
|
|
|
const req = this.req;
|
|
|
|
|
|
|
|
req._ended = true;
|
|
|
|
if (!req.shouldKeepAlive || req.finished)
|
|
|
|
responseKeepAlive(res, req);
|
|
|
|
}
|
|
|
|
|
|
|
|
function requestOnPrefinish() {
|
|
|
|
const req = this;
|
|
|
|
const res = this.res;
|
|
|
|
|
|
|
|
if (!req.shouldKeepAlive)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (req._ended)
|
|
|
|
responseKeepAlive(res, req);
|
|
|
|
}
|
|
|
|
|
2015-03-05 22:07:27 +01:00
|
|
|
function emitFreeNT(socket) {
|
|
|
|
socket.emit('free');
|
|
|
|
}
|
|
|
|
|
2013-10-30 00:35:32 +01:00
|
|
|
function tickOnSocket(req, socket) {
|
|
|
|
var parser = parsers.alloc();
|
|
|
|
req.socket = socket;
|
|
|
|
req.connection = socket;
|
|
|
|
parser.reinitialize(HTTPParser.RESPONSE);
|
|
|
|
parser.socket = socket;
|
|
|
|
parser.incoming = null;
|
2015-12-17 23:23:46 +01:00
|
|
|
parser.outgoing = req;
|
2013-10-30 00:35:32 +01:00
|
|
|
req.parser = parser;
|
|
|
|
|
|
|
|
socket.parser = parser;
|
|
|
|
socket._httpMessage = req;
|
|
|
|
|
2015-06-24 05:42:49 +02:00
|
|
|
// Setup "drain" propagation.
|
2013-10-30 00:35:32 +01:00
|
|
|
httpSocketSetup(socket);
|
|
|
|
|
|
|
|
// Propagate headers limit from request object to parser
|
2015-01-29 02:05:53 +01:00
|
|
|
if (typeof req.maxHeadersCount === 'number') {
|
2013-10-30 00:35:32 +01:00
|
|
|
parser.maxHeaderPairs = req.maxHeadersCount << 1;
|
|
|
|
} else {
|
|
|
|
// Set default value because parser may be reused from FreeList
|
|
|
|
parser.maxHeaderPairs = 2000;
|
|
|
|
}
|
|
|
|
|
|
|
|
parser.onIncoming = parserOnIncomingClient;
|
2015-12-31 15:51:46 +01:00
|
|
|
socket.removeListener('error', freeSocketErrorListener);
|
2013-10-30 00:35:32 +01:00
|
|
|
socket.on('error', socketErrorListener);
|
|
|
|
socket.on('data', socketOnData);
|
|
|
|
socket.on('end', socketOnEnd);
|
|
|
|
socket.on('close', socketCloseListener);
|
2016-08-31 02:00:41 +02:00
|
|
|
|
|
|
|
if (req.timeout) {
|
2016-11-01 16:59:31 +01:00
|
|
|
const emitRequestTimeout = () => req.emit('timeout');
|
|
|
|
socket.once('timeout', emitRequestTimeout);
|
|
|
|
req.once('response', (res) => {
|
|
|
|
res.once('end', () => {
|
|
|
|
socket.removeListener('timeout', emitRequestTimeout);
|
|
|
|
});
|
|
|
|
});
|
2016-08-31 02:00:41 +02:00
|
|
|
}
|
2013-10-30 00:35:32 +01:00
|
|
|
req.emit('socket', socket);
|
|
|
|
}
|
|
|
|
|
2016-10-12 13:06:30 +02:00
|
|
|
ClientRequest.prototype.onSocket = function onSocket(socket) {
|
2015-03-05 22:07:27 +01:00
|
|
|
process.nextTick(onSocketNT, this, socket);
|
2013-04-12 01:11:12 +02:00
|
|
|
};
|
|
|
|
|
2015-03-05 22:07:27 +01:00
|
|
|
function onSocketNT(req, socket) {
|
|
|
|
if (req.aborted) {
|
|
|
|
// If we were aborted while waiting for a socket, skip the whole thing.
|
2017-09-28 10:30:07 +02:00
|
|
|
if (!req.agent) {
|
2017-01-15 14:23:20 +01:00
|
|
|
socket.destroy();
|
|
|
|
} else {
|
|
|
|
socket.emit('free');
|
|
|
|
}
|
2015-03-05 22:07:27 +01:00
|
|
|
} else {
|
|
|
|
tickOnSocket(req, socket);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-12 13:06:30 +02:00
|
|
|
ClientRequest.prototype._deferToConnect = _deferToConnect;
|
|
|
|
function _deferToConnect(method, arguments_, cb) {
|
2013-04-12 01:11:12 +02:00
|
|
|
// 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).
|
2015-09-09 15:11:19 +02:00
|
|
|
|
2017-02-28 01:53:13 +01:00
|
|
|
const callSocketMethod = () => {
|
2015-09-09 15:11:19 +02:00
|
|
|
if (method)
|
2017-02-28 01:53:13 +01:00
|
|
|
this.socket[method].apply(this.socket, arguments_);
|
2015-09-09 15:11:19 +02:00
|
|
|
|
|
|
|
if (typeof cb === 'function')
|
|
|
|
cb();
|
2017-02-28 01:53:13 +01:00
|
|
|
};
|
2015-09-09 15:11:19 +02:00
|
|
|
|
2017-02-28 01:53:13 +01:00
|
|
|
const onSocket = () => {
|
|
|
|
if (this.socket.writable) {
|
2015-09-09 15:11:19 +02:00
|
|
|
callSocketMethod();
|
2013-04-12 01:11:12 +02:00
|
|
|
} else {
|
2017-02-28 01:53:13 +01:00
|
|
|
this.socket.once('connect', callSocketMethod);
|
2013-04-12 01:11:12 +02:00
|
|
|
}
|
2015-01-15 18:55:29 +01:00
|
|
|
};
|
2015-09-09 15:11:19 +02:00
|
|
|
|
2017-02-28 01:53:13 +01:00
|
|
|
if (!this.socket) {
|
|
|
|
this.once('socket', onSocket);
|
2013-04-12 01:11:12 +02:00
|
|
|
} else {
|
|
|
|
onSocket();
|
|
|
|
}
|
2016-10-12 13:06:30 +02:00
|
|
|
}
|
2013-04-12 01:11:12 +02:00
|
|
|
|
2016-10-12 13:06:30 +02:00
|
|
|
ClientRequest.prototype.setTimeout = function setTimeout(msecs, callback) {
|
2013-04-12 01:11:12 +02:00
|
|
|
if (callback) this.once('timeout', callback);
|
|
|
|
|
2017-02-28 01:53:13 +01:00
|
|
|
const emitTimeout = () => this.emit('timeout');
|
2013-04-12 01:11:12 +02:00
|
|
|
|
|
|
|
if (this.socket && this.socket.writable) {
|
|
|
|
if (this.timeoutCb)
|
|
|
|
this.socket.setTimeout(0, this.timeoutCb);
|
|
|
|
this.timeoutCb = emitTimeout;
|
|
|
|
this.socket.setTimeout(msecs, emitTimeout);
|
2015-05-14 04:25:57 +02:00
|
|
|
return this;
|
2013-04-12 01:11:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
});
|
2015-05-14 04:25:57 +02:00
|
|
|
return this;
|
2013-04-12 01:11:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this.once('socket', function(sock) {
|
2016-10-02 12:10:33 +02:00
|
|
|
sock.once('connect', function() {
|
|
|
|
sock.setTimeout(msecs, emitTimeout);
|
|
|
|
});
|
2013-04-12 01:11:12 +02:00
|
|
|
});
|
2015-05-14 04:25:57 +02:00
|
|
|
|
|
|
|
return this;
|
2013-04-12 01:11:12 +02:00
|
|
|
};
|
|
|
|
|
2017-01-16 09:56:48 +01:00
|
|
|
ClientRequest.prototype.setNoDelay = function setNoDelay(noDelay) {
|
|
|
|
this._deferToConnect('setNoDelay', [noDelay]);
|
2013-04-12 01:11:12 +02:00
|
|
|
};
|
|
|
|
|
2017-01-16 09:56:48 +01:00
|
|
|
ClientRequest.prototype.setSocketKeepAlive =
|
|
|
|
function setSocketKeepAlive(enable, initialDelay) {
|
|
|
|
this._deferToConnect('setKeepAlive', [enable, initialDelay]);
|
|
|
|
};
|
|
|
|
|
2016-10-12 13:06:30 +02:00
|
|
|
ClientRequest.prototype.clearTimeout = function clearTimeout(cb) {
|
2013-04-12 01:11:12 +02:00
|
|
|
this.setTimeout(0, cb);
|
|
|
|
};
|
2017-02-28 01:09:32 +01:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
ClientRequest
|
|
|
|
};
|