2018-03-18 07:35:47 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const { Buffer } = require('buffer');
|
2018-10-21 06:34:00 +00:00
|
|
|
const { FastBuffer } = require('internal/buffer');
|
|
|
|
const {
|
|
|
|
WriteWrap,
|
|
|
|
kReadBytesOrError,
|
|
|
|
kArrayBufferOffset,
|
2018-10-23 06:23:02 +00:00
|
|
|
kBytesWritten,
|
|
|
|
kLastWriteWasAsync,
|
2018-10-21 06:34:00 +00:00
|
|
|
streamBaseState
|
|
|
|
} = internalBinding('stream_wrap');
|
2018-05-05 09:46:50 +00:00
|
|
|
const { UV_EOF } = internalBinding('uv');
|
2019-01-04 02:40:22 +00:00
|
|
|
const {
|
|
|
|
codes: {
|
|
|
|
ERR_INVALID_CALLBACK
|
|
|
|
},
|
|
|
|
errnoException
|
|
|
|
} = require('internal/errors');
|
2018-05-05 09:46:50 +00:00
|
|
|
const { owner_symbol } = require('internal/async_hooks').symbols;
|
2019-01-04 02:40:22 +00:00
|
|
|
const {
|
|
|
|
kTimeout,
|
|
|
|
setUnrefTimeout,
|
2019-03-20 11:12:48 +00:00
|
|
|
getTimerDuration
|
2019-01-04 02:40:22 +00:00
|
|
|
} = require('internal/timers');
|
2019-01-10 20:52:27 +00:00
|
|
|
const { isUint8Array } = require('internal/util/types');
|
2019-04-05 21:56:00 +00:00
|
|
|
const { clearTimeout } = require('timers');
|
2018-03-18 07:35:47 +00:00
|
|
|
|
2018-05-05 09:46:50 +00:00
|
|
|
const kMaybeDestroy = Symbol('kMaybeDestroy');
|
|
|
|
const kUpdateTimer = Symbol('kUpdateTimer');
|
2018-11-03 18:00:41 +00:00
|
|
|
const kAfterAsyncWrite = Symbol('kAfterAsyncWrite');
|
2019-03-07 11:22:36 +00:00
|
|
|
const kHandle = Symbol('kHandle');
|
|
|
|
const kSession = Symbol('kSession');
|
2018-03-18 07:35:47 +00:00
|
|
|
|
2019-04-17 15:45:53 +00:00
|
|
|
const debug = require('internal/util/debuglog').debuglog('stream');
|
2019-01-10 20:52:27 +00:00
|
|
|
const kBuffer = Symbol('kBuffer');
|
|
|
|
const kBufferGen = Symbol('kBufferGen');
|
|
|
|
const kBufferCb = Symbol('kBufferCb');
|
tls: support TLSv1.3
This introduces TLS1.3 support and makes it the default max protocol,
but also supports CLI/NODE_OPTIONS switches to disable it if necessary.
TLS1.3 is a major update to the TLS protocol, with many security
enhancements. It should be preferred over TLS1.2 whenever possible.
TLS1.3 is different enough that even though the OpenSSL APIs are
technically API/ABI compatible, that when TLS1.3 is negotiated, the
timing of protocol records and of callbacks broke assumptions hard-coded
into the 'tls' module.
This change introduces no API incompatibilities when TLS1.2 is
negotiated. It is the intention that it be backported to current and LTS
release lines with the default maximum TLS protocol reset to 'TLSv1.2'.
This will allow users of those lines to explicitly enable TLS1.3 if they
want.
API incompatibilities between TLS1.2 and TLS1.3 are:
- Renegotiation is not supported by TLS1.3 protocol, attempts to call
`.renegotiate()` will always fail.
- Compiling against a system OpenSSL lower than 1.1.1 is no longer
supported (OpenSSL-1.1.0 used to be supported with configure flags).
- Variations of `conn.write('data'); conn.destroy()` have undefined
behaviour according to the streams API. They may or may not send the
'data', and may or may not cause a ERR_STREAM_DESTROYED error to be
emitted. This has always been true, but conditions under which the write
suceeds is slightly but observably different when TLS1.3 is negotiated
vs when TLS1.2 or below is negotiated.
- If TLS1.3 is negotiated, and a server calls `conn.end()` in its
'secureConnection' listener without any data being written, the client
will not receive session tickets (no 'session' events will be emitted,
and `conn.getSession()` will never return a resumable session).
- The return value of `conn.getSession()` API may not return a resumable
session if called right after the handshake. The effect will be that
clients using the legacy `getSession()` API will resume sessions if
TLS1.2 is negotiated, but will do full handshakes if TLS1.3 is
negotiated. See https://github.com/nodejs/node/pull/25831 for more
information.
PR-URL: https://github.com/nodejs/node/pull/26209
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rod Vagg <rod@vagg.org>
2018-11-29 01:58:08 +00:00
|
|
|
|
2018-03-18 07:35:47 +00:00
|
|
|
function handleWriteReq(req, data, encoding) {
|
|
|
|
const { handle } = req;
|
|
|
|
|
|
|
|
switch (encoding) {
|
|
|
|
case 'buffer':
|
2018-10-23 06:23:02 +00:00
|
|
|
{
|
|
|
|
const ret = handle.writeBuffer(req, data);
|
|
|
|
if (streamBaseState[kLastWriteWasAsync])
|
|
|
|
req.buffer = data;
|
|
|
|
return ret;
|
|
|
|
}
|
2018-03-18 07:35:47 +00:00
|
|
|
case 'latin1':
|
|
|
|
case 'binary':
|
|
|
|
return handle.writeLatin1String(req, data);
|
|
|
|
case 'utf8':
|
|
|
|
case 'utf-8':
|
|
|
|
return handle.writeUtf8String(req, data);
|
|
|
|
case 'ascii':
|
|
|
|
return handle.writeAsciiString(req, data);
|
|
|
|
case 'ucs2':
|
|
|
|
case 'ucs-2':
|
|
|
|
case 'utf16le':
|
|
|
|
case 'utf-16le':
|
|
|
|
return handle.writeUcs2String(req, data);
|
|
|
|
default:
|
2018-10-23 06:23:02 +00:00
|
|
|
{
|
|
|
|
const buffer = Buffer.from(data, encoding);
|
|
|
|
const ret = handle.writeBuffer(req, buffer);
|
|
|
|
if (streamBaseState[kLastWriteWasAsync])
|
|
|
|
req.buffer = buffer;
|
|
|
|
return ret;
|
|
|
|
}
|
2018-03-18 07:35:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-03 18:00:41 +00:00
|
|
|
function onWriteComplete(status) {
|
tls: support TLSv1.3
This introduces TLS1.3 support and makes it the default max protocol,
but also supports CLI/NODE_OPTIONS switches to disable it if necessary.
TLS1.3 is a major update to the TLS protocol, with many security
enhancements. It should be preferred over TLS1.2 whenever possible.
TLS1.3 is different enough that even though the OpenSSL APIs are
technically API/ABI compatible, that when TLS1.3 is negotiated, the
timing of protocol records and of callbacks broke assumptions hard-coded
into the 'tls' module.
This change introduces no API incompatibilities when TLS1.2 is
negotiated. It is the intention that it be backported to current and LTS
release lines with the default maximum TLS protocol reset to 'TLSv1.2'.
This will allow users of those lines to explicitly enable TLS1.3 if they
want.
API incompatibilities between TLS1.2 and TLS1.3 are:
- Renegotiation is not supported by TLS1.3 protocol, attempts to call
`.renegotiate()` will always fail.
- Compiling against a system OpenSSL lower than 1.1.1 is no longer
supported (OpenSSL-1.1.0 used to be supported with configure flags).
- Variations of `conn.write('data'); conn.destroy()` have undefined
behaviour according to the streams API. They may or may not send the
'data', and may or may not cause a ERR_STREAM_DESTROYED error to be
emitted. This has always been true, but conditions under which the write
suceeds is slightly but observably different when TLS1.3 is negotiated
vs when TLS1.2 or below is negotiated.
- If TLS1.3 is negotiated, and a server calls `conn.end()` in its
'secureConnection' listener without any data being written, the client
will not receive session tickets (no 'session' events will be emitted,
and `conn.getSession()` will never return a resumable session).
- The return value of `conn.getSession()` API may not return a resumable
session if called right after the handshake. The effect will be that
clients using the legacy `getSession()` API will resume sessions if
TLS1.2 is negotiated, but will do full handshakes if TLS1.3 is
negotiated. See https://github.com/nodejs/node/pull/25831 for more
information.
PR-URL: https://github.com/nodejs/node/pull/26209
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rod Vagg <rod@vagg.org>
2018-11-29 01:58:08 +00:00
|
|
|
debug('onWriteComplete', status, this.error);
|
|
|
|
|
2018-11-03 18:00:41 +00:00
|
|
|
const stream = this.handle[owner_symbol];
|
|
|
|
|
|
|
|
if (stream.destroyed) {
|
|
|
|
if (typeof this.callback === 'function')
|
|
|
|
this.callback(null);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status < 0) {
|
|
|
|
const ex = errnoException(status, 'write', this.error);
|
|
|
|
stream.destroy(ex, this.callback);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream[kUpdateTimer]();
|
|
|
|
stream[kAfterAsyncWrite](this);
|
|
|
|
|
|
|
|
if (typeof this.callback === 'function')
|
|
|
|
this.callback(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
function createWriteWrap(handle) {
|
2019-03-26 04:21:27 +00:00
|
|
|
const req = new WriteWrap();
|
2018-03-18 07:35:47 +00:00
|
|
|
|
|
|
|
req.handle = handle;
|
2018-11-03 18:00:41 +00:00
|
|
|
req.oncomplete = onWriteComplete;
|
2018-03-18 07:35:47 +00:00
|
|
|
req.async = false;
|
2018-10-23 06:23:02 +00:00
|
|
|
req.bytes = 0;
|
|
|
|
req.buffer = null;
|
2018-03-18 07:35:47 +00:00
|
|
|
|
|
|
|
return req;
|
|
|
|
}
|
|
|
|
|
2019-03-07 11:22:36 +00:00
|
|
|
function writevGeneric(self, data, cb) {
|
|
|
|
const req = createWriteWrap(self[kHandle]);
|
2019-03-26 04:21:27 +00:00
|
|
|
const allBuffers = data.allBuffers;
|
2018-03-18 07:35:47 +00:00
|
|
|
var chunks;
|
|
|
|
var i;
|
|
|
|
if (allBuffers) {
|
|
|
|
chunks = data;
|
|
|
|
for (i = 0; i < data.length; i++)
|
|
|
|
data[i] = data[i].chunk;
|
|
|
|
} else {
|
|
|
|
chunks = new Array(data.length << 1);
|
|
|
|
for (i = 0; i < data.length; i++) {
|
|
|
|
var entry = data[i];
|
|
|
|
chunks[i * 2] = entry.chunk;
|
|
|
|
chunks[i * 2 + 1] = entry.encoding;
|
|
|
|
}
|
|
|
|
}
|
2019-03-26 04:21:27 +00:00
|
|
|
const err = req.handle.writev(req, chunks, allBuffers);
|
2018-03-18 07:35:47 +00:00
|
|
|
|
|
|
|
// Retain chunks
|
|
|
|
if (err === 0) req._chunks = chunks;
|
|
|
|
|
2018-04-01 18:56:11 +00:00
|
|
|
afterWriteDispatched(self, req, err, cb);
|
2019-03-07 11:22:36 +00:00
|
|
|
return req;
|
2018-03-18 07:35:47 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 11:22:36 +00:00
|
|
|
function writeGeneric(self, data, encoding, cb) {
|
|
|
|
const req = createWriteWrap(self[kHandle]);
|
2019-03-26 04:21:27 +00:00
|
|
|
const err = handleWriteReq(req, data, encoding);
|
2018-03-18 07:35:47 +00:00
|
|
|
|
2018-04-01 18:56:11 +00:00
|
|
|
afterWriteDispatched(self, req, err, cb);
|
2019-03-07 11:22:36 +00:00
|
|
|
return req;
|
2018-04-01 18:56:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function afterWriteDispatched(self, req, err, cb) {
|
2018-10-23 06:23:02 +00:00
|
|
|
req.bytes = streamBaseState[kBytesWritten];
|
|
|
|
req.async = !!streamBaseState[kLastWriteWasAsync];
|
|
|
|
|
2018-04-01 18:56:11 +00:00
|
|
|
if (err !== 0)
|
2018-03-18 07:35:47 +00:00
|
|
|
return self.destroy(errnoException(err, 'write', req.error), cb);
|
2018-04-01 18:56:11 +00:00
|
|
|
|
|
|
|
if (!req.async) {
|
|
|
|
cb();
|
|
|
|
} else {
|
|
|
|
req.callback = cb;
|
|
|
|
}
|
2018-03-18 07:35:47 +00:00
|
|
|
}
|
|
|
|
|
2018-10-21 06:34:00 +00:00
|
|
|
function onStreamRead(arrayBuffer) {
|
|
|
|
const nread = streamBaseState[kReadBytesOrError];
|
|
|
|
|
2018-05-05 09:46:50 +00:00
|
|
|
const handle = this;
|
|
|
|
const stream = this[owner_symbol];
|
|
|
|
|
|
|
|
stream[kUpdateTimer]();
|
|
|
|
|
|
|
|
if (nread > 0 && !stream.destroyed) {
|
2019-01-10 20:52:27 +00:00
|
|
|
let ret;
|
|
|
|
let result;
|
|
|
|
const userBuf = stream[kBuffer];
|
|
|
|
if (userBuf) {
|
|
|
|
result = (stream[kBufferCb](nread, userBuf) !== false);
|
|
|
|
const bufGen = stream[kBufferGen];
|
|
|
|
if (bufGen !== null) {
|
|
|
|
const nextBuf = bufGen();
|
|
|
|
if (isUint8Array(nextBuf))
|
|
|
|
stream[kBuffer] = ret = nextBuf;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const offset = streamBaseState[kArrayBufferOffset];
|
|
|
|
const buf = new FastBuffer(arrayBuffer, offset, nread);
|
|
|
|
result = stream.push(buf);
|
|
|
|
}
|
|
|
|
if (!result) {
|
2018-05-05 09:46:50 +00:00
|
|
|
handle.reading = false;
|
|
|
|
if (!stream.destroyed) {
|
|
|
|
const err = handle.readStop();
|
|
|
|
if (err)
|
|
|
|
stream.destroy(errnoException(err, 'read'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-10 20:52:27 +00:00
|
|
|
return ret;
|
2018-05-05 09:46:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (nread === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nread !== UV_EOF) {
|
|
|
|
return stream.destroy(errnoException(nread, 'read'));
|
|
|
|
}
|
|
|
|
|
2019-03-07 00:03:53 +00:00
|
|
|
// Defer this until we actually emit end
|
2018-05-05 09:46:50 +00:00
|
|
|
if (stream._readableState.endEmitted) {
|
|
|
|
if (stream[kMaybeDestroy])
|
|
|
|
stream[kMaybeDestroy]();
|
|
|
|
} else {
|
|
|
|
if (stream[kMaybeDestroy])
|
|
|
|
stream.on('end', stream[kMaybeDestroy]);
|
|
|
|
|
2019-03-07 00:03:53 +00:00
|
|
|
// Push a null to signal the end of data.
|
2018-05-05 09:46:50 +00:00
|
|
|
// Do it before `maybeDestroy` for correct order of events:
|
|
|
|
// `end` -> `close`
|
|
|
|
stream.push(null);
|
|
|
|
stream.read(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-04 02:40:22 +00:00
|
|
|
function setStreamTimeout(msecs, callback) {
|
|
|
|
if (this.destroyed)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this.timeout = msecs;
|
|
|
|
|
|
|
|
// Type checking identical to timers.enroll()
|
2019-03-20 11:12:48 +00:00
|
|
|
msecs = getTimerDuration(msecs, 'msecs');
|
2019-01-04 02:40:22 +00:00
|
|
|
|
|
|
|
// Attempt to clear an existing timer in both cases -
|
|
|
|
// even if it will be rescheduled we don't want to leak an existing timer.
|
|
|
|
clearTimeout(this[kTimeout]);
|
|
|
|
|
|
|
|
if (msecs === 0) {
|
|
|
|
if (callback !== undefined) {
|
|
|
|
if (typeof callback !== 'function')
|
2019-04-02 01:46:17 +00:00
|
|
|
throw new ERR_INVALID_CALLBACK(callback);
|
2019-01-04 02:40:22 +00:00
|
|
|
this.removeListener('timeout', callback);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this[kTimeout] = setUnrefTimeout(this._onTimeout.bind(this), msecs);
|
|
|
|
if (this[kSession]) this[kSession][kUpdateTimer]();
|
|
|
|
|
|
|
|
if (callback !== undefined) {
|
|
|
|
if (typeof callback !== 'function')
|
2019-04-02 01:46:17 +00:00
|
|
|
throw new ERR_INVALID_CALLBACK(callback);
|
2019-01-04 02:40:22 +00:00
|
|
|
this.once('timeout', callback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2018-03-18 07:35:47 +00:00
|
|
|
module.exports = {
|
|
|
|
createWriteWrap,
|
|
|
|
writevGeneric,
|
2018-05-05 09:46:50 +00:00
|
|
|
writeGeneric,
|
|
|
|
onStreamRead,
|
2018-11-03 18:00:41 +00:00
|
|
|
kAfterAsyncWrite,
|
2018-05-05 09:46:50 +00:00
|
|
|
kMaybeDestroy,
|
|
|
|
kUpdateTimer,
|
2019-03-07 11:22:36 +00:00
|
|
|
kHandle,
|
2019-01-04 02:40:22 +00:00
|
|
|
kSession,
|
2019-01-10 20:52:27 +00:00
|
|
|
setStreamTimeout,
|
|
|
|
kBuffer,
|
|
|
|
kBufferCb,
|
|
|
|
kBufferGen
|
2018-03-18 07:35:47 +00:00
|
|
|
};
|