0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-30 07:27:22 +01:00
nodejs/lib/_http_client.js

755 lines
22 KiB
JavaScript
Raw Normal View History

// 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.
'use strict';
const util = require('util');
const net = require('net');
const url = require('url');
const { HTTPParser } = process.binding('http_parser');
const assert = require('assert').ok;
const {
_checkIsHttpToken: checkIsHttpToken,
debug,
freeParser,
httpSocketSetup,
parsers
} = require('_http_common');
const { OutgoingMessage } = require('_http_outgoing');
const Agent = require('_http_agent');
const { Buffer } = require('buffer');
const { urlToOptions, searchParamsSymbol } = require('internal/url');
const { outHeadersKey } = require('internal/http');
const { nextTick } = require('internal/process/next_tick');
const errors = require('internal/errors');
// 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;
}
function validateHost(host, name) {
if (host != null && typeof host !== 'string') {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', `options.${name}`,
['string', 'undefined', 'null'], host);
}
return host;
}
function ClientRequest(options, cb) {
OutgoingMessage.call(this);
if (typeof options === 'string') {
options = url.parse(options);
if (!options.hostname) {
throw new errors.Error('ERR_INVALID_DOMAIN_NAME');
}
} else if (options && options[searchParamsSymbol] &&
options[searchParamsSymbol][searchParamsSymbol]) {
// url.URL instance
options = urlToOptions(options);
} else {
options = util._extend({}, options);
}
var agent = options.agent;
var defaultAgent = options._defaultAgent || Agent.globalAgent;
if (agent === false) {
agent = new defaultAgent.constructor();
} 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.
} else if (typeof agent.addRequest !== 'function') {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'Agent option',
['Agent-like object', 'undefined', 'false']);
}
this.agent = agent;
var protocol = options.protocol || defaultAgent.protocol;
var expectedProtocol = defaultAgent.protocol;
if (this.agent && this.agent.protocol)
expectedProtocol = this.agent.protocol;
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)
throw new errors.TypeError('ERR_UNESCAPED_CHARACTERS', 'Request path');
}
if (protocol !== expectedProtocol) {
throw new errors.Error('ERR_INVALID_PROTOCOL', protocol, expectedProtocol);
}
var defaultPort = options.defaultPort ||
this.agent && this.agent.defaultPort;
var port = options.port = options.port || defaultPort || 80;
var host = options.host = validateHost(options.hostname, 'hostname') ||
validateHost(options.host, 'host') || 'localhost';
var setHost = (options.setHost === undefined);
this.socketPath = options.socketPath;
this.timeout = options.timeout;
var method = options.method;
var methodIsString = (typeof method === 'string');
if (method != null && !methodIsString) {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'method',
'string', method);
}
if (methodIsString && method) {
if (!checkIsHttpToken(method)) {
throw new errors.TypeError('ERR_INVALID_HTTP_TOKEN', 'Method', method);
}
method = this.method = method.toUpperCase();
} else {
method = this.method = 'GET';
}
this.path = options.path || '/';
if (cb) {
this.once('response', cb);
}
var headersArray = Array.isArray(options.headers);
if (!headersArray) {
if (options.headers) {
var keys = Object.keys(options.headers);
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
this.setHeader(key, options.headers[key]);
}
}
if (host && !this.getHeader('host') && setHost) {
var hostHeader = host;
// 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
var posColon = hostHeader.indexOf(':');
if (posColon !== -1 &&
hostHeader.indexOf(':', posColon + 1) !== -1 &&
hostHeader.charCodeAt(0) !== 91/*'['*/) {
hostHeader = `[${hostHeader}]`;
}
if (port && +port !== defaultPort) {
hostHeader += ':' + port;
}
this.setHeader('Host', hostHeader);
}
}
if (options.auth && !this.getHeader('Authorization')) {
this.setHeader('Authorization', 'Basic ' +
Buffer.from(options.auth).toString('base64'));
}
if (method === 'GET' ||
method === 'HEAD' ||
method === 'DELETE' ||
method === 'OPTIONS' ||
method === 'CONNECT') {
this.useChunkedEncodingByDefault = false;
} else {
this.useChunkedEncodingByDefault = true;
}
if (headersArray) {
this._storeHeader(this.method + ' ' + this.path + ' HTTP/1.1\r\n',
options.headers);
} else if (this.getHeader('expect')) {
if (this._header) {
throw new errors.Error('ERR_HTTP_HEADERS_SENT', 'render');
}
this._storeHeader(this.method + ' ' + this.path + ' HTTP/1.1\r\n',
this[outHeadersKey]);
}
2013-05-23 03:44:24 +02:00
this._ended = false;
this.res = null;
this.aborted = undefined;
this.timeoutCb = null;
this.upgradeOrConnect = false;
this.parser = null;
this.maxHeadersCount = null;
var called = false;
var oncreate = (err, socket) => {
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.agent) {
// 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
if (!this.agent.keepAlive && !Number.isFinite(this.agent.maxSockets)) {
this._last = true;
this.shouldKeepAlive = false;
} else {
this._last = false;
this.shouldKeepAlive = true;
}
this.agent.addRequest(this, options);
} else {
// No agent, default to Connection:close.
this._last = true;
this.shouldKeepAlive = false;
if (typeof options.createConnection === 'function') {
const newSocket = options.createConnection(options, oncreate);
if (newSocket && !called) {
called = true;
this.onSocket(newSocket);
} else {
return;
}
} else {
2013-05-23 03:44:24 +02:00
debug('CLIENT use net.createConnection', options);
this.onSocket(net.createConnection(options));
}
}
this._deferToConnect(null, null, () => this._flush());
}
2013-05-23 03:44:24 +02:00
util.inherits(ClientRequest, OutgoingMessage);
ClientRequest.prototype._finish = function _finish() {
DTRACE_HTTP_CLIENT_REQUEST(this, this.connection);
LTTNG_HTTP_CLIENT_REQUEST(this, this.connection);
COUNTER_HTTP_CLIENT_REQUEST();
OutgoingMessage.prototype._finish.call(this);
};
ClientRequest.prototype._implicitHeader = function _implicitHeader() {
if (this._header) {
throw new errors.Error('ERR_HTTP_HEADERS_SENT', 'render');
}
this._storeHeader(this.method + ' ' + this.path + ' HTTP/1.1\r\n',
this[outHeadersKey]);
};
ClientRequest.prototype.abort = function abort() {
if (!this.aborted) {
process.nextTick(emitAbortNT.bind(this));
}
// 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.
this.aborted = Date.now();
// If we're aborting, we don't care about any more response data.
if (this.res) {
Merge remote-tracking branch 'upstream/v0.10' Conflicts: AUTHORS ChangeLog deps/npm/Makefile deps/npm/doc/api/npm-commands.md deps/npm/doc/api/npm-deprecate.md deps/npm/doc/api/npm-init.md deps/npm/doc/api/npm-owner.md deps/npm/doc/api/npm-publish.md deps/npm/doc/api/npm-run-script.md deps/npm/doc/cli/npm-adduser.md deps/npm/doc/cli/npm-bin.md deps/npm/doc/cli/npm-bugs.md deps/npm/doc/cli/npm-build.md deps/npm/doc/cli/npm-cache.md deps/npm/doc/cli/npm-completion.md deps/npm/doc/cli/npm-deprecate.md deps/npm/doc/cli/npm-docs.md deps/npm/doc/cli/npm-edit.md deps/npm/doc/cli/npm-explore.md deps/npm/doc/cli/npm-help-search.md deps/npm/doc/cli/npm-help.md deps/npm/doc/cli/npm-init.md deps/npm/doc/cli/npm-install.md deps/npm/doc/cli/npm-link.md deps/npm/doc/cli/npm-ls.md deps/npm/doc/cli/npm-outdated.md deps/npm/doc/cli/npm-owner.md deps/npm/doc/cli/npm-pack.md deps/npm/doc/cli/npm-prefix.md deps/npm/doc/cli/npm-prune.md deps/npm/doc/cli/npm-publish.md deps/npm/doc/cli/npm-restart.md deps/npm/doc/cli/npm-rm.md deps/npm/doc/cli/npm-root.md deps/npm/doc/cli/npm-run-script.md deps/npm/doc/cli/npm-search.md deps/npm/doc/cli/npm-shrinkwrap.md deps/npm/doc/cli/npm-start.md deps/npm/doc/cli/npm-stop.md deps/npm/doc/cli/npm-submodule.md deps/npm/doc/cli/npm-tag.md deps/npm/doc/cli/npm-test.md deps/npm/doc/cli/npm-uninstall.md deps/npm/doc/cli/npm-unpublish.md deps/npm/doc/cli/npm-update.md deps/npm/doc/cli/npm-version.md deps/npm/doc/cli/npm-view.md deps/npm/doc/cli/npm-whoami.md deps/npm/doc/files/npm-folders.md deps/npm/doc/files/package.json.md deps/npm/doc/misc/npm-coding-style.md deps/npm/doc/misc/npm-config.md deps/npm/doc/misc/npm-developers.md deps/npm/doc/misc/npm-disputes.md deps/npm/doc/misc/npm-faq.md deps/npm/doc/misc/npm-registry.md deps/npm/doc/misc/npm-scripts.md deps/npm/doc/misc/semver.md deps/npm/html/doc/README.html deps/npm/html/doc/api/npm-bin.html deps/npm/html/doc/api/npm-bugs.html deps/npm/html/doc/api/npm-commands.html deps/npm/html/doc/api/npm-config.html deps/npm/html/doc/api/npm-deprecate.html deps/npm/html/doc/api/npm-docs.html deps/npm/html/doc/api/npm-edit.html deps/npm/html/doc/api/npm-explore.html deps/npm/html/doc/api/npm-help-search.html deps/npm/html/doc/api/npm-init.html deps/npm/html/doc/api/npm-install.html deps/npm/html/doc/api/npm-link.html deps/npm/html/doc/api/npm-load.html deps/npm/html/doc/api/npm-ls.html deps/npm/html/doc/api/npm-outdated.html deps/npm/html/doc/api/npm-owner.html deps/npm/html/doc/api/npm-pack.html deps/npm/html/doc/api/npm-prefix.html deps/npm/html/doc/api/npm-prune.html deps/npm/html/doc/api/npm-publish.html deps/npm/html/doc/api/npm-rebuild.html deps/npm/html/doc/api/npm-restart.html deps/npm/html/doc/api/npm-root.html deps/npm/html/doc/api/npm-run-script.html deps/npm/html/doc/api/npm-search.html deps/npm/html/doc/api/npm-shrinkwrap.html deps/npm/html/doc/api/npm-start.html deps/npm/html/doc/api/npm-stop.html deps/npm/html/doc/api/npm-submodule.html deps/npm/html/doc/api/npm-tag.html deps/npm/html/doc/api/npm-test.html deps/npm/html/doc/api/npm-uninstall.html deps/npm/html/doc/api/npm-unpublish.html deps/npm/html/doc/api/npm-update.html deps/npm/html/doc/api/npm-version.html deps/npm/html/doc/api/npm-view.html deps/npm/html/doc/api/npm-whoami.html deps/npm/html/doc/api/npm.html deps/npm/html/doc/cli/npm-adduser.html deps/npm/html/doc/cli/npm-bin.html deps/npm/html/doc/cli/npm-bugs.html deps/npm/html/doc/cli/npm-build.html deps/npm/html/doc/cli/npm-bundle.html deps/npm/html/doc/cli/npm-cache.html deps/npm/html/doc/cli/npm-completion.html deps/npm/html/doc/cli/npm-config.html deps/npm/html/doc/cli/npm-dedupe.html deps/npm/html/doc/cli/npm-deprecate.html deps/npm/html/doc/cli/npm-docs.html deps/npm/html/doc/cli/npm-edit.html deps/npm/html/doc/cli/npm-explore.html deps/npm/html/doc/cli/npm-help-search.html deps/npm/html/doc/cli/npm-help.html deps/npm/html/doc/cli/npm-init.html deps/npm/html/doc/cli/npm-install.html deps/npm/html/doc/cli/npm-link.html deps/npm/html/doc/cli/npm-ls.html deps/npm/html/doc/cli/npm-outdated.html deps/npm/html/doc/cli/npm-owner.html deps/npm/html/doc/cli/npm-pack.html deps/npm/html/doc/cli/npm-prefix.html deps/npm/html/doc/cli/npm-prune.html deps/npm/html/doc/cli/npm-publish.html deps/npm/html/doc/cli/npm-rebuild.html deps/npm/html/doc/cli/npm-restart.html deps/npm/html/doc/cli/npm-rm.html deps/npm/html/doc/cli/npm-root.html deps/npm/html/doc/cli/npm-run-script.html deps/npm/html/doc/cli/npm-search.html deps/npm/html/doc/cli/npm-shrinkwrap.html deps/npm/html/doc/cli/npm-star.html deps/npm/html/doc/cli/npm-stars.html deps/npm/html/doc/cli/npm-start.html deps/npm/html/doc/cli/npm-stop.html deps/npm/html/doc/cli/npm-submodule.html deps/npm/html/doc/cli/npm-tag.html deps/npm/html/doc/cli/npm-test.html deps/npm/html/doc/cli/npm-uninstall.html deps/npm/html/doc/cli/npm-unpublish.html deps/npm/html/doc/cli/npm-update.html deps/npm/html/doc/cli/npm-version.html deps/npm/html/doc/cli/npm-view.html deps/npm/html/doc/cli/npm-whoami.html deps/npm/html/doc/cli/npm.html deps/npm/html/doc/files/npm-folders.html deps/npm/html/doc/files/npm-global.html deps/npm/html/doc/files/npm-json.html deps/npm/html/doc/files/npmrc.html deps/npm/html/doc/files/package.json.html deps/npm/html/doc/index.html deps/npm/html/doc/misc/npm-coding-style.html deps/npm/html/doc/misc/npm-config.html deps/npm/html/doc/misc/npm-developers.html deps/npm/html/doc/misc/npm-disputes.html deps/npm/html/doc/misc/npm-faq.html deps/npm/html/doc/misc/npm-index.html deps/npm/html/doc/misc/npm-registry.html deps/npm/html/doc/misc/npm-scripts.html deps/npm/html/doc/misc/removing-npm.html deps/npm/html/doc/misc/semver.html deps/npm/man/man1/npm-README.1 deps/npm/man/man1/npm-adduser.1 deps/npm/man/man1/npm-bin.1 deps/npm/man/man1/npm-bugs.1 deps/npm/man/man1/npm-build.1 deps/npm/man/man1/npm-bundle.1 deps/npm/man/man1/npm-cache.1 deps/npm/man/man1/npm-completion.1 deps/npm/man/man1/npm-dedupe.1 deps/npm/man/man1/npm-deprecate.1 deps/npm/man/man1/npm-docs.1 deps/npm/man/man1/npm-edit.1 deps/npm/man/man1/npm-explore.1 deps/npm/man/man1/npm-help-search.1 deps/npm/man/man1/npm-help.1 deps/npm/man/man1/npm-init.1 deps/npm/man/man1/npm-install.1 deps/npm/man/man1/npm-link.1 deps/npm/man/man1/npm-ls.1 deps/npm/man/man1/npm-outdated.1 deps/npm/man/man1/npm-owner.1 deps/npm/man/man1/npm-pack.1 deps/npm/man/man1/npm-prefix.1 deps/npm/man/man1/npm-prune.1 deps/npm/man/man1/npm-publish.1 deps/npm/man/man1/npm-rebuild.1 deps/npm/man/man1/npm-restart.1 deps/npm/man/man1/npm-rm.1 deps/npm/man/man1/npm-root.1 deps/npm/man/man1/npm-run-script.1 deps/npm/man/man1/npm-search.1 deps/npm/man/man1/npm-shrinkwrap.1 deps/npm/man/man1/npm-star.1 deps/npm/man/man1/npm-stars.1 deps/npm/man/man1/npm-start.1 deps/npm/man/man1/npm-stop.1 deps/npm/man/man1/npm-submodule.1 deps/npm/man/man1/npm-tag.1 deps/npm/man/man1/npm-test.1 deps/npm/man/man1/npm-uninstall.1 deps/npm/man/man1/npm-unpublish.1 deps/npm/man/man1/npm-update.1 deps/npm/man/man1/npm-version.1 deps/npm/man/man1/npm-view.1 deps/npm/man/man1/npm-whoami.1 deps/npm/man/man1/npm.1 deps/npm/man/man3/npm-bin.3 deps/npm/man/man3/npm-bugs.3 deps/npm/man/man3/npm-commands.3 deps/npm/man/man3/npm-config.3 deps/npm/man/man3/npm-deprecate.3 deps/npm/man/man3/npm-docs.3 deps/npm/man/man3/npm-edit.3 deps/npm/man/man3/npm-explore.3 deps/npm/man/man3/npm-help-search.3 deps/npm/man/man3/npm-init.3 deps/npm/man/man3/npm-install.3 deps/npm/man/man3/npm-link.3 deps/npm/man/man3/npm-load.3 deps/npm/man/man3/npm-ls.3 deps/npm/man/man3/npm-outdated.3 deps/npm/man/man3/npm-owner.3 deps/npm/man/man3/npm-pack.3 deps/npm/man/man3/npm-prefix.3 deps/npm/man/man3/npm-prune.3 deps/npm/man/man3/npm-publish.3 deps/npm/man/man3/npm-rebuild.3 deps/npm/man/man3/npm-restart.3 deps/npm/man/man3/npm-root.3 deps/npm/man/man3/npm-run-script.3 deps/npm/man/man3/npm-search.3 deps/npm/man/man3/npm-shrinkwrap.3 deps/npm/man/man3/npm-start.3 deps/npm/man/man3/npm-stop.3 deps/npm/man/man3/npm-submodule.3 deps/npm/man/man3/npm-tag.3 deps/npm/man/man3/npm-test.3 deps/npm/man/man3/npm-uninstall.3 deps/npm/man/man3/npm-unpublish.3 deps/npm/man/man3/npm-update.3 deps/npm/man/man3/npm-version.3 deps/npm/man/man3/npm-view.3 deps/npm/man/man3/npm-whoami.3 deps/npm/man/man3/npm.3 deps/npm/man/man5/npm-folders.5 deps/npm/man/man5/npm-global.5 deps/npm/man/man5/npm-json.5 deps/npm/man/man7/npm-coding-style.7 deps/npm/man/man7/npm-config.7 deps/npm/man/man7/npm-developers.7 deps/npm/man/man7/npm-disputes.7 deps/npm/man/man7/npm-faq.7 deps/npm/man/man7/npm-registry.7 deps/npm/man/man7/npm-scripts.7 deps/npm/man/man7/removing-npm.7 deps/npm/man/man7/semver.7 deps/npm/package.json deps/uv/AUTHORS deps/uv/ChangeLog deps/uv/src/version.c deps/uv/test/test-fs.c deps/uv/test/test-list.h lib/http.js lib/tls.js src/node_version.h
2013-07-12 22:26:56 +02:00
this.res._dump();
} else {
Merge remote-tracking branch 'upstream/v0.10' Conflicts: AUTHORS ChangeLog deps/npm/Makefile deps/npm/doc/api/npm-commands.md deps/npm/doc/api/npm-deprecate.md deps/npm/doc/api/npm-init.md deps/npm/doc/api/npm-owner.md deps/npm/doc/api/npm-publish.md deps/npm/doc/api/npm-run-script.md deps/npm/doc/cli/npm-adduser.md deps/npm/doc/cli/npm-bin.md deps/npm/doc/cli/npm-bugs.md deps/npm/doc/cli/npm-build.md deps/npm/doc/cli/npm-cache.md deps/npm/doc/cli/npm-completion.md deps/npm/doc/cli/npm-deprecate.md deps/npm/doc/cli/npm-docs.md deps/npm/doc/cli/npm-edit.md deps/npm/doc/cli/npm-explore.md deps/npm/doc/cli/npm-help-search.md deps/npm/doc/cli/npm-help.md deps/npm/doc/cli/npm-init.md deps/npm/doc/cli/npm-install.md deps/npm/doc/cli/npm-link.md deps/npm/doc/cli/npm-ls.md deps/npm/doc/cli/npm-outdated.md deps/npm/doc/cli/npm-owner.md deps/npm/doc/cli/npm-pack.md deps/npm/doc/cli/npm-prefix.md deps/npm/doc/cli/npm-prune.md deps/npm/doc/cli/npm-publish.md deps/npm/doc/cli/npm-restart.md deps/npm/doc/cli/npm-rm.md deps/npm/doc/cli/npm-root.md deps/npm/doc/cli/npm-run-script.md deps/npm/doc/cli/npm-search.md deps/npm/doc/cli/npm-shrinkwrap.md deps/npm/doc/cli/npm-start.md deps/npm/doc/cli/npm-stop.md deps/npm/doc/cli/npm-submodule.md deps/npm/doc/cli/npm-tag.md deps/npm/doc/cli/npm-test.md deps/npm/doc/cli/npm-uninstall.md deps/npm/doc/cli/npm-unpublish.md deps/npm/doc/cli/npm-update.md deps/npm/doc/cli/npm-version.md deps/npm/doc/cli/npm-view.md deps/npm/doc/cli/npm-whoami.md deps/npm/doc/files/npm-folders.md deps/npm/doc/files/package.json.md deps/npm/doc/misc/npm-coding-style.md deps/npm/doc/misc/npm-config.md deps/npm/doc/misc/npm-developers.md deps/npm/doc/misc/npm-disputes.md deps/npm/doc/misc/npm-faq.md deps/npm/doc/misc/npm-registry.md deps/npm/doc/misc/npm-scripts.md deps/npm/doc/misc/semver.md deps/npm/html/doc/README.html deps/npm/html/doc/api/npm-bin.html deps/npm/html/doc/api/npm-bugs.html deps/npm/html/doc/api/npm-commands.html deps/npm/html/doc/api/npm-config.html deps/npm/html/doc/api/npm-deprecate.html deps/npm/html/doc/api/npm-docs.html deps/npm/html/doc/api/npm-edit.html deps/npm/html/doc/api/npm-explore.html deps/npm/html/doc/api/npm-help-search.html deps/npm/html/doc/api/npm-init.html deps/npm/html/doc/api/npm-install.html deps/npm/html/doc/api/npm-link.html deps/npm/html/doc/api/npm-load.html deps/npm/html/doc/api/npm-ls.html deps/npm/html/doc/api/npm-outdated.html deps/npm/html/doc/api/npm-owner.html deps/npm/html/doc/api/npm-pack.html deps/npm/html/doc/api/npm-prefix.html deps/npm/html/doc/api/npm-prune.html deps/npm/html/doc/api/npm-publish.html deps/npm/html/doc/api/npm-rebuild.html deps/npm/html/doc/api/npm-restart.html deps/npm/html/doc/api/npm-root.html deps/npm/html/doc/api/npm-run-script.html deps/npm/html/doc/api/npm-search.html deps/npm/html/doc/api/npm-shrinkwrap.html deps/npm/html/doc/api/npm-start.html deps/npm/html/doc/api/npm-stop.html deps/npm/html/doc/api/npm-submodule.html deps/npm/html/doc/api/npm-tag.html deps/npm/html/doc/api/npm-test.html deps/npm/html/doc/api/npm-uninstall.html deps/npm/html/doc/api/npm-unpublish.html deps/npm/html/doc/api/npm-update.html deps/npm/html/doc/api/npm-version.html deps/npm/html/doc/api/npm-view.html deps/npm/html/doc/api/npm-whoami.html deps/npm/html/doc/api/npm.html deps/npm/html/doc/cli/npm-adduser.html deps/npm/html/doc/cli/npm-bin.html deps/npm/html/doc/cli/npm-bugs.html deps/npm/html/doc/cli/npm-build.html deps/npm/html/doc/cli/npm-bundle.html deps/npm/html/doc/cli/npm-cache.html deps/npm/html/doc/cli/npm-completion.html deps/npm/html/doc/cli/npm-config.html deps/npm/html/doc/cli/npm-dedupe.html deps/npm/html/doc/cli/npm-deprecate.html deps/npm/html/doc/cli/npm-docs.html deps/npm/html/doc/cli/npm-edit.html deps/npm/html/doc/cli/npm-explore.html deps/npm/html/doc/cli/npm-help-search.html deps/npm/html/doc/cli/npm-help.html deps/npm/html/doc/cli/npm-init.html deps/npm/html/doc/cli/npm-install.html deps/npm/html/doc/cli/npm-link.html deps/npm/html/doc/cli/npm-ls.html deps/npm/html/doc/cli/npm-outdated.html deps/npm/html/doc/cli/npm-owner.html deps/npm/html/doc/cli/npm-pack.html deps/npm/html/doc/cli/npm-prefix.html deps/npm/html/doc/cli/npm-prune.html deps/npm/html/doc/cli/npm-publish.html deps/npm/html/doc/cli/npm-rebuild.html deps/npm/html/doc/cli/npm-restart.html deps/npm/html/doc/cli/npm-rm.html deps/npm/html/doc/cli/npm-root.html deps/npm/html/doc/cli/npm-run-script.html deps/npm/html/doc/cli/npm-search.html deps/npm/html/doc/cli/npm-shrinkwrap.html deps/npm/html/doc/cli/npm-star.html deps/npm/html/doc/cli/npm-stars.html deps/npm/html/doc/cli/npm-start.html deps/npm/html/doc/cli/npm-stop.html deps/npm/html/doc/cli/npm-submodule.html deps/npm/html/doc/cli/npm-tag.html deps/npm/html/doc/cli/npm-test.html deps/npm/html/doc/cli/npm-uninstall.html deps/npm/html/doc/cli/npm-unpublish.html deps/npm/html/doc/cli/npm-update.html deps/npm/html/doc/cli/npm-version.html deps/npm/html/doc/cli/npm-view.html deps/npm/html/doc/cli/npm-whoami.html deps/npm/html/doc/cli/npm.html deps/npm/html/doc/files/npm-folders.html deps/npm/html/doc/files/npm-global.html deps/npm/html/doc/files/npm-json.html deps/npm/html/doc/files/npmrc.html deps/npm/html/doc/files/package.json.html deps/npm/html/doc/index.html deps/npm/html/doc/misc/npm-coding-style.html deps/npm/html/doc/misc/npm-config.html deps/npm/html/doc/misc/npm-developers.html deps/npm/html/doc/misc/npm-disputes.html deps/npm/html/doc/misc/npm-faq.html deps/npm/html/doc/misc/npm-index.html deps/npm/html/doc/misc/npm-registry.html deps/npm/html/doc/misc/npm-scripts.html deps/npm/html/doc/misc/removing-npm.html deps/npm/html/doc/misc/semver.html deps/npm/man/man1/npm-README.1 deps/npm/man/man1/npm-adduser.1 deps/npm/man/man1/npm-bin.1 deps/npm/man/man1/npm-bugs.1 deps/npm/man/man1/npm-build.1 deps/npm/man/man1/npm-bundle.1 deps/npm/man/man1/npm-cache.1 deps/npm/man/man1/npm-completion.1 deps/npm/man/man1/npm-dedupe.1 deps/npm/man/man1/npm-deprecate.1 deps/npm/man/man1/npm-docs.1 deps/npm/man/man1/npm-edit.1 deps/npm/man/man1/npm-explore.1 deps/npm/man/man1/npm-help-search.1 deps/npm/man/man1/npm-help.1 deps/npm/man/man1/npm-init.1 deps/npm/man/man1/npm-install.1 deps/npm/man/man1/npm-link.1 deps/npm/man/man1/npm-ls.1 deps/npm/man/man1/npm-outdated.1 deps/npm/man/man1/npm-owner.1 deps/npm/man/man1/npm-pack.1 deps/npm/man/man1/npm-prefix.1 deps/npm/man/man1/npm-prune.1 deps/npm/man/man1/npm-publish.1 deps/npm/man/man1/npm-rebuild.1 deps/npm/man/man1/npm-restart.1 deps/npm/man/man1/npm-rm.1 deps/npm/man/man1/npm-root.1 deps/npm/man/man1/npm-run-script.1 deps/npm/man/man1/npm-search.1 deps/npm/man/man1/npm-shrinkwrap.1 deps/npm/man/man1/npm-star.1 deps/npm/man/man1/npm-stars.1 deps/npm/man/man1/npm-start.1 deps/npm/man/man1/npm-stop.1 deps/npm/man/man1/npm-submodule.1 deps/npm/man/man1/npm-tag.1 deps/npm/man/man1/npm-test.1 deps/npm/man/man1/npm-uninstall.1 deps/npm/man/man1/npm-unpublish.1 deps/npm/man/man1/npm-update.1 deps/npm/man/man1/npm-version.1 deps/npm/man/man1/npm-view.1 deps/npm/man/man1/npm-whoami.1 deps/npm/man/man1/npm.1 deps/npm/man/man3/npm-bin.3 deps/npm/man/man3/npm-bugs.3 deps/npm/man/man3/npm-commands.3 deps/npm/man/man3/npm-config.3 deps/npm/man/man3/npm-deprecate.3 deps/npm/man/man3/npm-docs.3 deps/npm/man/man3/npm-edit.3 deps/npm/man/man3/npm-explore.3 deps/npm/man/man3/npm-help-search.3 deps/npm/man/man3/npm-init.3 deps/npm/man/man3/npm-install.3 deps/npm/man/man3/npm-link.3 deps/npm/man/man3/npm-load.3 deps/npm/man/man3/npm-ls.3 deps/npm/man/man3/npm-outdated.3 deps/npm/man/man3/npm-owner.3 deps/npm/man/man3/npm-pack.3 deps/npm/man/man3/npm-prefix.3 deps/npm/man/man3/npm-prune.3 deps/npm/man/man3/npm-publish.3 deps/npm/man/man3/npm-rebuild.3 deps/npm/man/man3/npm-restart.3 deps/npm/man/man3/npm-root.3 deps/npm/man/man3/npm-run-script.3 deps/npm/man/man3/npm-search.3 deps/npm/man/man3/npm-shrinkwrap.3 deps/npm/man/man3/npm-start.3 deps/npm/man/man3/npm-stop.3 deps/npm/man/man3/npm-submodule.3 deps/npm/man/man3/npm-tag.3 deps/npm/man/man3/npm-test.3 deps/npm/man/man3/npm-uninstall.3 deps/npm/man/man3/npm-unpublish.3 deps/npm/man/man3/npm-update.3 deps/npm/man/man3/npm-version.3 deps/npm/man/man3/npm-view.3 deps/npm/man/man3/npm-whoami.3 deps/npm/man/man3/npm.3 deps/npm/man/man5/npm-folders.5 deps/npm/man/man5/npm-global.5 deps/npm/man/man5/npm-json.5 deps/npm/man/man7/npm-coding-style.7 deps/npm/man/man7/npm-config.7 deps/npm/man/man7/npm-developers.7 deps/npm/man/man7/npm-disputes.7 deps/npm/man/man7/npm-faq.7 deps/npm/man/man7/npm-registry.7 deps/npm/man/man7/npm-scripts.7 deps/npm/man/man7/removing-npm.7 deps/npm/man/man7/semver.7 deps/npm/package.json deps/uv/AUTHORS deps/uv/ChangeLog deps/uv/src/version.c deps/uv/test/test-fs.c deps/uv/test/test-list.h lib/http.js lib/tls.js src/node_version.h
2013-07-12 22:26:56 +02:00
this.once('response', function(res) {
res._dump();
});
}
Merge remote-tracking branch 'upstream/v0.10' Conflicts: AUTHORS ChangeLog deps/npm/Makefile deps/npm/doc/api/npm-commands.md deps/npm/doc/api/npm-deprecate.md deps/npm/doc/api/npm-init.md deps/npm/doc/api/npm-owner.md deps/npm/doc/api/npm-publish.md deps/npm/doc/api/npm-run-script.md deps/npm/doc/cli/npm-adduser.md deps/npm/doc/cli/npm-bin.md deps/npm/doc/cli/npm-bugs.md deps/npm/doc/cli/npm-build.md deps/npm/doc/cli/npm-cache.md deps/npm/doc/cli/npm-completion.md deps/npm/doc/cli/npm-deprecate.md deps/npm/doc/cli/npm-docs.md deps/npm/doc/cli/npm-edit.md deps/npm/doc/cli/npm-explore.md deps/npm/doc/cli/npm-help-search.md deps/npm/doc/cli/npm-help.md deps/npm/doc/cli/npm-init.md deps/npm/doc/cli/npm-install.md deps/npm/doc/cli/npm-link.md deps/npm/doc/cli/npm-ls.md deps/npm/doc/cli/npm-outdated.md deps/npm/doc/cli/npm-owner.md deps/npm/doc/cli/npm-pack.md deps/npm/doc/cli/npm-prefix.md deps/npm/doc/cli/npm-prune.md deps/npm/doc/cli/npm-publish.md deps/npm/doc/cli/npm-restart.md deps/npm/doc/cli/npm-rm.md deps/npm/doc/cli/npm-root.md deps/npm/doc/cli/npm-run-script.md deps/npm/doc/cli/npm-search.md deps/npm/doc/cli/npm-shrinkwrap.md deps/npm/doc/cli/npm-start.md deps/npm/doc/cli/npm-stop.md deps/npm/doc/cli/npm-submodule.md deps/npm/doc/cli/npm-tag.md deps/npm/doc/cli/npm-test.md deps/npm/doc/cli/npm-uninstall.md deps/npm/doc/cli/npm-unpublish.md deps/npm/doc/cli/npm-update.md deps/npm/doc/cli/npm-version.md deps/npm/doc/cli/npm-view.md deps/npm/doc/cli/npm-whoami.md deps/npm/doc/files/npm-folders.md deps/npm/doc/files/package.json.md deps/npm/doc/misc/npm-coding-style.md deps/npm/doc/misc/npm-config.md deps/npm/doc/misc/npm-developers.md deps/npm/doc/misc/npm-disputes.md deps/npm/doc/misc/npm-faq.md deps/npm/doc/misc/npm-registry.md deps/npm/doc/misc/npm-scripts.md deps/npm/doc/misc/semver.md deps/npm/html/doc/README.html deps/npm/html/doc/api/npm-bin.html deps/npm/html/doc/api/npm-bugs.html deps/npm/html/doc/api/npm-commands.html deps/npm/html/doc/api/npm-config.html deps/npm/html/doc/api/npm-deprecate.html deps/npm/html/doc/api/npm-docs.html deps/npm/html/doc/api/npm-edit.html deps/npm/html/doc/api/npm-explore.html deps/npm/html/doc/api/npm-help-search.html deps/npm/html/doc/api/npm-init.html deps/npm/html/doc/api/npm-install.html deps/npm/html/doc/api/npm-link.html deps/npm/html/doc/api/npm-load.html deps/npm/html/doc/api/npm-ls.html deps/npm/html/doc/api/npm-outdated.html deps/npm/html/doc/api/npm-owner.html deps/npm/html/doc/api/npm-pack.html deps/npm/html/doc/api/npm-prefix.html deps/npm/html/doc/api/npm-prune.html deps/npm/html/doc/api/npm-publish.html deps/npm/html/doc/api/npm-rebuild.html deps/npm/html/doc/api/npm-restart.html deps/npm/html/doc/api/npm-root.html deps/npm/html/doc/api/npm-run-script.html deps/npm/html/doc/api/npm-search.html deps/npm/html/doc/api/npm-shrinkwrap.html deps/npm/html/doc/api/npm-start.html deps/npm/html/doc/api/npm-stop.html deps/npm/html/doc/api/npm-submodule.html deps/npm/html/doc/api/npm-tag.html deps/npm/html/doc/api/npm-test.html deps/npm/html/doc/api/npm-uninstall.html deps/npm/html/doc/api/npm-unpublish.html deps/npm/html/doc/api/npm-update.html deps/npm/html/doc/api/npm-version.html deps/npm/html/doc/api/npm-view.html deps/npm/html/doc/api/npm-whoami.html deps/npm/html/doc/api/npm.html deps/npm/html/doc/cli/npm-adduser.html deps/npm/html/doc/cli/npm-bin.html deps/npm/html/doc/cli/npm-bugs.html deps/npm/html/doc/cli/npm-build.html deps/npm/html/doc/cli/npm-bundle.html deps/npm/html/doc/cli/npm-cache.html deps/npm/html/doc/cli/npm-completion.html deps/npm/html/doc/cli/npm-config.html deps/npm/html/doc/cli/npm-dedupe.html deps/npm/html/doc/cli/npm-deprecate.html deps/npm/html/doc/cli/npm-docs.html deps/npm/html/doc/cli/npm-edit.html deps/npm/html/doc/cli/npm-explore.html deps/npm/html/doc/cli/npm-help-search.html deps/npm/html/doc/cli/npm-help.html deps/npm/html/doc/cli/npm-init.html deps/npm/html/doc/cli/npm-install.html deps/npm/html/doc/cli/npm-link.html deps/npm/html/doc/cli/npm-ls.html deps/npm/html/doc/cli/npm-outdated.html deps/npm/html/doc/cli/npm-owner.html deps/npm/html/doc/cli/npm-pack.html deps/npm/html/doc/cli/npm-prefix.html deps/npm/html/doc/cli/npm-prune.html deps/npm/html/doc/cli/npm-publish.html deps/npm/html/doc/cli/npm-rebuild.html deps/npm/html/doc/cli/npm-restart.html deps/npm/html/doc/cli/npm-rm.html deps/npm/html/doc/cli/npm-root.html deps/npm/html/doc/cli/npm-run-script.html deps/npm/html/doc/cli/npm-search.html deps/npm/html/doc/cli/npm-shrinkwrap.html deps/npm/html/doc/cli/npm-star.html deps/npm/html/doc/cli/npm-stars.html deps/npm/html/doc/cli/npm-start.html deps/npm/html/doc/cli/npm-stop.html deps/npm/html/doc/cli/npm-submodule.html deps/npm/html/doc/cli/npm-tag.html deps/npm/html/doc/cli/npm-test.html deps/npm/html/doc/cli/npm-uninstall.html deps/npm/html/doc/cli/npm-unpublish.html deps/npm/html/doc/cli/npm-update.html deps/npm/html/doc/cli/npm-version.html deps/npm/html/doc/cli/npm-view.html deps/npm/html/doc/cli/npm-whoami.html deps/npm/html/doc/cli/npm.html deps/npm/html/doc/files/npm-folders.html deps/npm/html/doc/files/npm-global.html deps/npm/html/doc/files/npm-json.html deps/npm/html/doc/files/npmrc.html deps/npm/html/doc/files/package.json.html deps/npm/html/doc/index.html deps/npm/html/doc/misc/npm-coding-style.html deps/npm/html/doc/misc/npm-config.html deps/npm/html/doc/misc/npm-developers.html deps/npm/html/doc/misc/npm-disputes.html deps/npm/html/doc/misc/npm-faq.html deps/npm/html/doc/misc/npm-index.html deps/npm/html/doc/misc/npm-registry.html deps/npm/html/doc/misc/npm-scripts.html deps/npm/html/doc/misc/removing-npm.html deps/npm/html/doc/misc/semver.html deps/npm/man/man1/npm-README.1 deps/npm/man/man1/npm-adduser.1 deps/npm/man/man1/npm-bin.1 deps/npm/man/man1/npm-bugs.1 deps/npm/man/man1/npm-build.1 deps/npm/man/man1/npm-bundle.1 deps/npm/man/man1/npm-cache.1 deps/npm/man/man1/npm-completion.1 deps/npm/man/man1/npm-dedupe.1 deps/npm/man/man1/npm-deprecate.1 deps/npm/man/man1/npm-docs.1 deps/npm/man/man1/npm-edit.1 deps/npm/man/man1/npm-explore.1 deps/npm/man/man1/npm-help-search.1 deps/npm/man/man1/npm-help.1 deps/npm/man/man1/npm-init.1 deps/npm/man/man1/npm-install.1 deps/npm/man/man1/npm-link.1 deps/npm/man/man1/npm-ls.1 deps/npm/man/man1/npm-outdated.1 deps/npm/man/man1/npm-owner.1 deps/npm/man/man1/npm-pack.1 deps/npm/man/man1/npm-prefix.1 deps/npm/man/man1/npm-prune.1 deps/npm/man/man1/npm-publish.1 deps/npm/man/man1/npm-rebuild.1 deps/npm/man/man1/npm-restart.1 deps/npm/man/man1/npm-rm.1 deps/npm/man/man1/npm-root.1 deps/npm/man/man1/npm-run-script.1 deps/npm/man/man1/npm-search.1 deps/npm/man/man1/npm-shrinkwrap.1 deps/npm/man/man1/npm-star.1 deps/npm/man/man1/npm-stars.1 deps/npm/man/man1/npm-start.1 deps/npm/man/man1/npm-stop.1 deps/npm/man/man1/npm-submodule.1 deps/npm/man/man1/npm-tag.1 deps/npm/man/man1/npm-test.1 deps/npm/man/man1/npm-uninstall.1 deps/npm/man/man1/npm-unpublish.1 deps/npm/man/man1/npm-update.1 deps/npm/man/man1/npm-version.1 deps/npm/man/man1/npm-view.1 deps/npm/man/man1/npm-whoami.1 deps/npm/man/man1/npm.1 deps/npm/man/man3/npm-bin.3 deps/npm/man/man3/npm-bugs.3 deps/npm/man/man3/npm-commands.3 deps/npm/man/man3/npm-config.3 deps/npm/man/man3/npm-deprecate.3 deps/npm/man/man3/npm-docs.3 deps/npm/man/man3/npm-edit.3 deps/npm/man/man3/npm-explore.3 deps/npm/man/man3/npm-help-search.3 deps/npm/man/man3/npm-init.3 deps/npm/man/man3/npm-install.3 deps/npm/man/man3/npm-link.3 deps/npm/man/man3/npm-load.3 deps/npm/man/man3/npm-ls.3 deps/npm/man/man3/npm-outdated.3 deps/npm/man/man3/npm-owner.3 deps/npm/man/man3/npm-pack.3 deps/npm/man/man3/npm-prefix.3 deps/npm/man/man3/npm-prune.3 deps/npm/man/man3/npm-publish.3 deps/npm/man/man3/npm-rebuild.3 deps/npm/man/man3/npm-restart.3 deps/npm/man/man3/npm-root.3 deps/npm/man/man3/npm-run-script.3 deps/npm/man/man3/npm-search.3 deps/npm/man/man3/npm-shrinkwrap.3 deps/npm/man/man3/npm-start.3 deps/npm/man/man3/npm-stop.3 deps/npm/man/man3/npm-submodule.3 deps/npm/man/man3/npm-tag.3 deps/npm/man/man3/npm-test.3 deps/npm/man/man3/npm-uninstall.3 deps/npm/man/man3/npm-unpublish.3 deps/npm/man/man3/npm-update.3 deps/npm/man/man3/npm-version.3 deps/npm/man/man3/npm-view.3 deps/npm/man/man3/npm-whoami.3 deps/npm/man/man3/npm.3 deps/npm/man/man5/npm-folders.5 deps/npm/man/man5/npm-global.5 deps/npm/man/man5/npm-json.5 deps/npm/man/man7/npm-coding-style.7 deps/npm/man/man7/npm-config.7 deps/npm/man/man7/npm-developers.7 deps/npm/man/man7/npm-disputes.7 deps/npm/man/man7/npm-faq.7 deps/npm/man/man7/npm-registry.7 deps/npm/man/man7/npm-scripts.7 deps/npm/man/man7/removing-npm.7 deps/npm/man/man7/semver.7 deps/npm/package.json deps/uv/AUTHORS deps/uv/ChangeLog deps/uv/src/version.c deps/uv/test/test-fs.c deps/uv/test/test-list.h lib/http.js lib/tls.js src/node_version.h
2013-07-12 22:26:56 +02:00
// In the event that we don't have a socket, we will pop out of
// the request queue through handling in onSocket.
if (this.socket) {
// in-progress
this.socket.destroy();
}
};
function emitAbortNT() {
this.emit('abort');
}
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');
// 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();
// NOTE: It's important to get parser here, because it could be freed by
// the `socketOnData`.
var parser = socket.parser;
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);
} else if (!req.res && !req.socket._hadError) {
// This socket error fired before we started to
// receive a response. The error needs to
// fire on the request.
req.socket._hadError = true;
req.emit('error', createHangUpError());
}
req.emit('close');
// 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, socket);
}
}
function socketErrorListener(err) {
var socket = this;
var req = socket._httpMessage;
debug('SOCKET ERROR:', err.message, err.stack);
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.
req.socket._hadError = true;
req.emit('error', err);
}
// Handle any pending data
socket.read();
var parser = socket.parser;
if (parser) {
parser.finish();
freeParser(parser, req, socket);
}
// Ensure that no further data will come out of the socket
socket.removeListener('data', socketOnData);
socket.removeListener('end', socketOnEnd);
socket.destroy();
}
function freeSocketErrorListener(err) {
var socket = this;
debug('SOCKET ERROR on FREE socket:', err.message, err.stack);
socket.destroy();
socket.emit('agentRemove');
}
function socketOnEnd() {
var socket = this;
var req = this._httpMessage;
var parser = this.parser;
if (!req.res && !req.socket._hadError) {
// 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.socket._hadError = true;
req.emit('error', createHangUpError());
}
if (parser) {
parser.finish();
freeParser(parser, req, socket);
}
socket.destroy();
}
function socketOnData(d) {
var socket = this;
var req = this._httpMessage;
var parser = this.parser;
assert(parser && parser.socket === socket);
var ret = parser.execute(d);
if (ret instanceof Error) {
debug('parse error', ret);
freeParser(parser, req, socket);
socket.destroy();
req.socket._hadError = true;
req.emit('error', ret);
} else if (parser.incoming && parser.incoming.upgrade) {
// Upgrade or CONNECT
var bytesParsed = ret;
var res = parser.incoming;
req.res = res;
socket.removeListener('data', socketOnData);
socket.removeListener('end', socketOnEnd);
parser.finish();
var bodyHead = d.slice(bytesParsed, d.length);
var eventName = req.method === 'CONNECT' ? 'connect' : 'upgrade';
if (req.listenerCount(eventName) > 0) {
req.upgradeOrConnect = true;
// detach the socket
socket.emit('agentRemove');
socket.removeListener('close', socketCloseListener);
socket.removeListener('error', socketErrorListener);
// TODO(isaacs): Need a way to reset a stream to fresh state
// IE, not flowing, and not explicitly paused.
socket._readableState.flowing = null;
req.emit(eventName, res, socket, bodyHead);
req.emit('close');
} else {
// Got Upgrade header or CONNECT method, but have no handler.
socket.destroy();
}
freeParser(parser, req, socket);
} 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) {
socket.removeListener('data', socketOnData);
socket.removeListener('end', socketOnEnd);
freeParser(parser, req, socket);
}
}
// client
function parserOnIncomingClient(res, shouldKeepAlive) {
var socket = this.socket;
var req = socket._httpMessage;
// propagate "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 2; // skip body, and the rest
}
// 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';
debug('AGENT isHeadResponse', isHeadResponse);
if (res.statusCode === 100) {
// restart the parser, as this is a continue message.
req.res = null; // 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);
LTTNG_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);
req.on('prefinish', requestOnPrefinish);
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 responseKeepAlive(res, req) {
var socket = req.socket;
if (!req.shouldKeepAlive) {
if (socket.writable) {
debug('AGENT socket.destroySoon()');
if (typeof socket.destroySoon === 'function')
socket.destroySoon();
else
socket.end();
}
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);
socket.once('error', freeSocketErrorListener);
// 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;
// Mark this socket as available, AFTER user-added end
// handlers have a chance to run.
nextTick(asyncId, emitFreeNT, socket);
}
}
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);
}
function emitFreeNT(socket) {
socket.emit('free');
}
function tickOnSocket(req, socket) {
var parser = parsers.alloc();
req.socket = socket;
req.connection = socket;
parser.reinitialize(HTTPParser.RESPONSE);
parser.socket = socket;
parser.incoming = null;
parser.outgoing = req;
req.parser = parser;
socket.parser = parser;
socket._httpMessage = req;
// Setup "drain" propagation.
httpSocketSetup(socket);
// Propagate headers limit from request object to parser
if (typeof req.maxHeadersCount === 'number') {
parser.maxHeaderPairs = req.maxHeadersCount << 1;
} else {
// Set default value because parser may be reused from FreeList
parser.maxHeaderPairs = 2000;
}
parser.onIncoming = parserOnIncomingClient;
socket.removeListener('error', freeSocketErrorListener);
socket.on('error', socketErrorListener);
socket.on('data', socketOnData);
socket.on('end', socketOnEnd);
socket.on('close', socketCloseListener);
if (req.timeout) {
const emitRequestTimeout = () => req.emit('timeout');
socket.once('timeout', emitRequestTimeout);
req.once('response', (res) => {
res.once('end', () => {
socket.removeListener('timeout', emitRequestTimeout);
});
});
}
req.emit('socket', socket);
}
ClientRequest.prototype.onSocket = function onSocket(socket) {
process.nextTick(onSocketNT, this, socket);
};
function onSocketNT(req, socket) {
if (req.aborted) {
// If we were aborted while waiting for a socket, skip the whole thing.
if (!req.agent) {
socket.destroy();
} else {
socket.emit('free');
}
} else {
tickOnSocket(req, socket);
}
}
ClientRequest.prototype._deferToConnect = _deferToConnect;
function _deferToConnect(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).
const callSocketMethod = () => {
if (method)
this.socket[method].apply(this.socket, arguments_);
if (typeof cb === 'function')
cb();
};
const onSocket = () => {
if (this.socket.writable) {
callSocketMethod();
} else {
this.socket.once('connect', callSocketMethod);
}
};
if (!this.socket) {
this.once('socket', onSocket);
} else {
onSocket();
}
}
ClientRequest.prototype.setTimeout = function setTimeout(msecs, callback) {
if (callback) this.once('timeout', callback);
const emitTimeout = () => this.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 this;
}
// 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;
}
this.once('socket', function(sock) {
sock.once('connect', function() {
sock.setTimeout(msecs, emitTimeout);
});
});
return this;
};
ClientRequest.prototype.setNoDelay = function setNoDelay(noDelay) {
this._deferToConnect('setNoDelay', [noDelay]);
};
ClientRequest.prototype.setSocketKeepAlive =
function setSocketKeepAlive(enable, initialDelay) {
this._deferToConnect('setKeepAlive', [enable, initialDelay]);
};
ClientRequest.prototype.clearTimeout = function clearTimeout(cb) {
this.setTimeout(0, cb);
};
module.exports = {
ClientRequest
};