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 assert = require('assert').ok;
|
|
|
|
const Stream = require('stream');
|
|
|
|
const util = require('util');
|
2015-06-13 18:44:39 +02:00
|
|
|
const internalUtil = require('internal/util');
|
2018-07-07 21:24:38 +02:00
|
|
|
const { outHeadersKey, utcDate } = require('internal/http');
|
2017-10-07 16:50:42 +02:00
|
|
|
const { Buffer } = require('buffer');
|
2015-01-21 17:36:59 +01:00
|
|
|
const common = require('_http_common');
|
2016-12-20 08:48:18 +01:00
|
|
|
const checkIsHttpToken = common._checkIsHttpToken;
|
|
|
|
const checkInvalidHeaderChar = common._checkInvalidHeaderChar;
|
2018-03-05 12:20:48 +01:00
|
|
|
const {
|
|
|
|
defaultTriggerAsyncIdScope,
|
|
|
|
symbols: { async_id_symbol }
|
|
|
|
} = require('internal/async_hooks');
|
2018-03-04 22:16:24 +01:00
|
|
|
const {
|
|
|
|
ERR_HTTP_HEADERS_SENT,
|
|
|
|
ERR_HTTP_INVALID_HEADER_VALUE,
|
|
|
|
ERR_HTTP_TRAILER_INVALID,
|
|
|
|
ERR_INVALID_HTTP_TOKEN,
|
|
|
|
ERR_INVALID_ARG_TYPE,
|
|
|
|
ERR_INVALID_CHAR,
|
|
|
|
ERR_METHOD_NOT_IMPLEMENTED,
|
|
|
|
ERR_STREAM_CANNOT_PIPE,
|
|
|
|
ERR_STREAM_WRITE_AFTER_END
|
|
|
|
} = require('internal/errors').codes;
|
2015-01-21 17:36:59 +01:00
|
|
|
|
2017-10-07 16:50:42 +02:00
|
|
|
const { CRLF, debug } = common;
|
2015-01-21 17:36:59 +01:00
|
|
|
|
2018-01-23 21:23:33 +01:00
|
|
|
const kIsCorked = Symbol('isCorked');
|
|
|
|
|
2018-04-24 10:24:51 +02:00
|
|
|
const hasOwnProperty = Function.call.bind(Object.prototype.hasOwnProperty);
|
|
|
|
|
2018-04-18 16:39:27 +02:00
|
|
|
var RE_CONN_CLOSE = /(?:^|\W)close(?:$|\W)/i;
|
2016-12-31 22:18:43 +01:00
|
|
|
var RE_TE_CHUNKED = common.chunkExpression;
|
2013-04-12 00:37:14 +02:00
|
|
|
|
2017-02-11 02:32:32 +01:00
|
|
|
// isCookieField performs a case-insensitive comparison of a provided string
|
2018-04-18 14:57:28 +02:00
|
|
|
// against the word "cookie." As of V8 6.6 this is faster than handrolling or
|
|
|
|
// using a case-insensitive RegExp.
|
2017-02-11 02:32:32 +01:00
|
|
|
function isCookieField(s) {
|
2018-04-18 14:57:28 +02:00
|
|
|
return s.length === 6 && s.toLowerCase() === 'cookie';
|
2017-02-11 02:32:32 +01:00
|
|
|
}
|
|
|
|
|
2017-05-13 16:07:44 +02:00
|
|
|
function noopPendingOutput(amount) {}
|
|
|
|
|
2013-04-12 00:37:14 +02:00
|
|
|
function OutgoingMessage() {
|
|
|
|
Stream.call(this);
|
|
|
|
|
2015-10-05 19:30:30 +02:00
|
|
|
// Queue that holds all currently pending data, until the response will be
|
|
|
|
// assigned to the socket (until it will its turn in the HTTP pipeline).
|
2013-04-12 00:37:14 +02:00
|
|
|
this.output = [];
|
|
|
|
this.outputEncodings = [];
|
2013-08-15 17:51:37 +02:00
|
|
|
this.outputCallbacks = [];
|
2015-10-05 19:30:30 +02:00
|
|
|
|
|
|
|
// `outputSize` is an approximate measure of how much data is queued on this
|
|
|
|
// response. `_onPendingData` will be invoked to update similar global
|
|
|
|
// per-connection counter. That counter will be used to pause/unpause the
|
|
|
|
// TCP socket and HTTP Parser and thus handle the backpressure.
|
2015-10-03 21:27:19 +02:00
|
|
|
this.outputSize = 0;
|
2013-04-12 00:37:14 +02:00
|
|
|
|
|
|
|
this.writable = true;
|
|
|
|
|
|
|
|
this._last = false;
|
|
|
|
this.chunkedEncoding = false;
|
|
|
|
this.shouldKeepAlive = true;
|
|
|
|
this.useChunkedEncodingByDefault = true;
|
|
|
|
this.sendDate = false;
|
2016-12-31 22:20:38 +01:00
|
|
|
this._removedConnection = false;
|
|
|
|
this._removedContLen = false;
|
|
|
|
this._removedTE = false;
|
2013-04-12 00:37:14 +02:00
|
|
|
|
2015-03-03 21:01:26 +01:00
|
|
|
this._contentLength = null;
|
2013-04-12 00:37:14 +02:00
|
|
|
this._hasBody = true;
|
|
|
|
this._trailer = '';
|
|
|
|
|
|
|
|
this.finished = false;
|
2014-09-29 21:32:42 +02:00
|
|
|
this._headerSent = false;
|
2018-01-23 21:23:33 +01:00
|
|
|
this[kIsCorked] = false;
|
2013-04-12 00:37:14 +02:00
|
|
|
|
|
|
|
this.socket = null;
|
|
|
|
this.connection = null;
|
2014-09-29 21:32:42 +02:00
|
|
|
this._header = null;
|
2017-03-06 03:13:09 +01:00
|
|
|
this[outHeadersKey] = null;
|
2015-10-03 21:27:19 +02:00
|
|
|
|
2017-05-13 16:07:44 +02:00
|
|
|
this._onPendingData = noopPendingOutput;
|
2013-04-12 00:37:14 +02:00
|
|
|
}
|
|
|
|
util.inherits(OutgoingMessage, Stream);
|
|
|
|
|
|
|
|
|
2017-03-06 03:15:15 +01:00
|
|
|
Object.defineProperty(OutgoingMessage.prototype, '_headers', {
|
|
|
|
get: function() {
|
|
|
|
return this.getHeaders();
|
|
|
|
},
|
|
|
|
set: function(val) {
|
|
|
|
if (val == null) {
|
|
|
|
this[outHeadersKey] = null;
|
|
|
|
} else if (typeof val === 'object') {
|
2018-04-24 10:24:51 +02:00
|
|
|
const headers = this[outHeadersKey] = Object.create(null);
|
2017-03-06 03:15:15 +01:00
|
|
|
const keys = Object.keys(val);
|
|
|
|
for (var i = 0; i < keys.length; ++i) {
|
|
|
|
const name = keys[i];
|
|
|
|
headers[name.toLowerCase()] = [name, val[name]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Object.defineProperty(OutgoingMessage.prototype, '_headerNames', {
|
|
|
|
get: function() {
|
|
|
|
const headers = this[outHeadersKey];
|
2018-04-24 10:24:51 +02:00
|
|
|
if (headers !== null) {
|
2017-03-20 00:11:10 +01:00
|
|
|
const out = Object.create(null);
|
2017-03-06 03:15:15 +01:00
|
|
|
const keys = Object.keys(headers);
|
|
|
|
for (var i = 0; i < keys.length; ++i) {
|
|
|
|
const key = keys[i];
|
|
|
|
const val = headers[key][0];
|
|
|
|
out[key] = val;
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
2018-04-24 10:24:51 +02:00
|
|
|
return null;
|
2017-03-06 03:15:15 +01:00
|
|
|
},
|
|
|
|
set: function(val) {
|
|
|
|
if (typeof val === 'object' && val !== null) {
|
|
|
|
const headers = this[outHeadersKey];
|
|
|
|
if (!headers)
|
|
|
|
return;
|
|
|
|
const keys = Object.keys(val);
|
|
|
|
for (var i = 0; i < keys.length; ++i) {
|
|
|
|
const header = headers[keys[i]];
|
|
|
|
if (header)
|
|
|
|
header[0] = val[keys[i]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
OutgoingMessage.prototype._renderHeaders = function _renderHeaders() {
|
|
|
|
if (this._header) {
|
2018-03-04 22:16:24 +01:00
|
|
|
throw new ERR_HTTP_HEADERS_SENT('render');
|
2017-03-06 03:15:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var headersMap = this[outHeadersKey];
|
2018-04-24 10:24:51 +02:00
|
|
|
const headers = {};
|
2017-03-06 03:15:15 +01:00
|
|
|
|
2018-04-24 10:24:51 +02:00
|
|
|
if (headersMap !== null) {
|
|
|
|
const keys = Object.keys(headersMap);
|
|
|
|
for (var i = 0, l = keys.length; i < l; i++) {
|
|
|
|
const key = keys[i];
|
|
|
|
headers[headersMap[key][0]] = headersMap[key][1];
|
|
|
|
}
|
2017-03-06 03:15:15 +01:00
|
|
|
}
|
|
|
|
return headers;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-10-12 15:48:40 +02:00
|
|
|
OutgoingMessage.prototype.setTimeout = function setTimeout(msecs, callback) {
|
2015-09-27 20:54:57 +02:00
|
|
|
|
|
|
|
if (callback) {
|
2013-04-12 00:37:14 +02:00
|
|
|
this.on('timeout', callback);
|
2015-09-27 20:54:57 +02:00
|
|
|
}
|
2015-01-26 00:26:40 +01:00
|
|
|
|
2013-04-12 00:37:14 +02:00
|
|
|
if (!this.socket) {
|
|
|
|
this.once('socket', function(socket) {
|
|
|
|
socket.setTimeout(msecs);
|
|
|
|
});
|
2015-04-28 19:46:14 +02:00
|
|
|
} else {
|
2013-04-12 00:37:14 +02:00
|
|
|
this.socket.setTimeout(msecs);
|
2015-04-28 19:46:14 +02:00
|
|
|
}
|
2015-05-14 04:25:57 +02:00
|
|
|
return this;
|
2013-04-12 00:37:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-04-29 14:12:24 +02:00
|
|
|
// It's possible that the socket will be destroyed, and removed from
|
|
|
|
// any messages, before ever calling this. In that case, just skip
|
|
|
|
// it, since something else is destroying this connection anyway.
|
2016-10-12 15:48:40 +02:00
|
|
|
OutgoingMessage.prototype.destroy = function destroy(error) {
|
2017-06-20 23:37:00 +02:00
|
|
|
if (this.socket) {
|
2013-04-22 17:52:42 +02:00
|
|
|
this.socket.destroy(error);
|
2017-06-20 23:37:00 +02:00
|
|
|
} else {
|
2013-04-22 17:52:42 +02:00
|
|
|
this.once('socket', function(socket) {
|
|
|
|
socket.destroy(error);
|
|
|
|
});
|
2017-06-20 23:37:00 +02:00
|
|
|
}
|
2013-04-12 00:37:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// This abstract either writing directly to the socket or buffering it.
|
2016-10-12 15:48:40 +02:00
|
|
|
OutgoingMessage.prototype._send = function _send(data, encoding, callback) {
|
2013-04-12 00:37:14 +02:00
|
|
|
// This is a shameful hack to get the headers and first body chunk onto
|
|
|
|
// the same packet. Future versions of Node are going to take care of
|
|
|
|
// this at a lower level and in a more general way.
|
|
|
|
if (!this._headerSent) {
|
2015-01-29 02:05:53 +01:00
|
|
|
if (typeof data === 'string' &&
|
2017-04-29 20:59:44 +02:00
|
|
|
(encoding === 'utf8' || encoding === 'latin1' || !encoding)) {
|
2013-04-12 00:37:14 +02:00
|
|
|
data = this._header + data;
|
|
|
|
} else {
|
2017-05-13 16:07:44 +02:00
|
|
|
var header = this._header;
|
|
|
|
if (this.output.length === 0) {
|
|
|
|
this.output = [header];
|
|
|
|
this.outputEncodings = ['latin1'];
|
|
|
|
this.outputCallbacks = [null];
|
|
|
|
} else {
|
|
|
|
this.output.unshift(header);
|
|
|
|
this.outputEncodings.unshift('latin1');
|
|
|
|
this.outputCallbacks.unshift(null);
|
|
|
|
}
|
|
|
|
this.outputSize += header.length;
|
|
|
|
this._onPendingData(header.length);
|
2013-04-12 00:37:14 +02:00
|
|
|
}
|
|
|
|
this._headerSent = true;
|
|
|
|
}
|
2013-08-15 17:51:37 +02:00
|
|
|
return this._writeRaw(data, encoding, callback);
|
2013-04-12 00:37:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-10-12 15:48:40 +02:00
|
|
|
OutgoingMessage.prototype._writeRaw = _writeRaw;
|
|
|
|
function _writeRaw(data, encoding, callback) {
|
2016-12-31 22:27:40 +01:00
|
|
|
const conn = this.connection;
|
|
|
|
if (conn && conn.destroyed) {
|
|
|
|
// The socket was destroyed. If we're still trying to write to it,
|
|
|
|
// then we haven't gotten the 'close' event yet.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-01-29 02:05:53 +01:00
|
|
|
if (typeof encoding === 'function') {
|
2013-08-15 17:51:37 +02:00
|
|
|
callback = encoding;
|
|
|
|
encoding = null;
|
|
|
|
}
|
|
|
|
|
2016-12-31 22:27:40 +01:00
|
|
|
if (conn && conn._httpMessage === this && conn.writable && !conn.destroyed) {
|
2013-04-12 00:37:14 +02:00
|
|
|
// There might be pending data in the this.output buffer.
|
2016-12-31 22:27:40 +01:00
|
|
|
if (this.output.length) {
|
|
|
|
this._flushOutput(conn);
|
|
|
|
} else if (!data.length) {
|
2017-03-10 14:17:42 +01:00
|
|
|
if (typeof callback === 'function') {
|
2017-10-19 12:43:40 +02:00
|
|
|
// If the socket was set directly it won't be correctly initialized
|
|
|
|
// with an async_id_symbol.
|
|
|
|
// TODO(AndreasMadsen): @trevnorris suggested some more correct
|
|
|
|
// solutions in:
|
|
|
|
// https://github.com/nodejs/node/pull/14389/files#r128522202
|
2018-03-05 12:20:48 +01:00
|
|
|
defaultTriggerAsyncIdScope(conn[async_id_symbol],
|
|
|
|
process.nextTick,
|
|
|
|
callback);
|
2017-03-10 14:17:42 +01:00
|
|
|
}
|
2015-09-26 05:36:06 +02:00
|
|
|
return true;
|
2013-04-12 00:37:14 +02:00
|
|
|
}
|
|
|
|
// Directly write to socket.
|
2016-12-31 22:27:40 +01:00
|
|
|
return conn.write(data, encoding, callback);
|
2013-04-12 00:37:14 +02:00
|
|
|
}
|
2016-12-31 22:27:40 +01:00
|
|
|
// Buffer, as long as we're not destroyed.
|
2013-04-12 00:37:14 +02:00
|
|
|
this.output.push(data);
|
|
|
|
this.outputEncodings.push(encoding);
|
2013-08-15 17:51:37 +02:00
|
|
|
this.outputCallbacks.push(callback);
|
2015-10-03 21:27:19 +02:00
|
|
|
this.outputSize += data.length;
|
2017-05-13 16:07:44 +02:00
|
|
|
this._onPendingData(data.length);
|
2013-04-12 00:37:14 +02:00
|
|
|
return false;
|
2017-05-13 16:07:44 +02:00
|
|
|
}
|
2013-04-12 00:37:14 +02:00
|
|
|
|
|
|
|
|
2016-10-12 15:48:40 +02:00
|
|
|
OutgoingMessage.prototype._storeHeader = _storeHeader;
|
|
|
|
function _storeHeader(firstLine, headers) {
|
2013-04-12 00:37:14 +02:00
|
|
|
// firstLine in the case of request is: 'GET /index.html HTTP/1.1\r\n'
|
|
|
|
// in the case of response it is: 'HTTP/1.1 200 OK\r\n'
|
2018-04-24 10:24:51 +02:00
|
|
|
const state = {
|
2016-12-31 22:18:43 +01:00
|
|
|
connection: false,
|
|
|
|
contLen: false,
|
|
|
|
te: false,
|
|
|
|
date: false,
|
|
|
|
expect: false,
|
|
|
|
trailer: false,
|
|
|
|
header: firstLine
|
2013-04-12 00:37:14 +02:00
|
|
|
};
|
|
|
|
|
2016-12-31 22:18:43 +01:00
|
|
|
var key;
|
2017-03-06 03:13:09 +01:00
|
|
|
if (headers === this[outHeadersKey]) {
|
2016-12-31 22:18:43 +01:00
|
|
|
for (key in headers) {
|
2018-04-24 10:24:51 +02:00
|
|
|
const entry = headers[key];
|
|
|
|
processHeader(this, state, entry[0], entry[1], false);
|
2016-12-31 22:18:43 +01:00
|
|
|
}
|
2018-04-24 10:24:51 +02:00
|
|
|
} else if (Array.isArray(headers)) {
|
|
|
|
for (var i = 0; i < headers.length; i++) {
|
|
|
|
const entry = headers[i];
|
|
|
|
processHeader(this, state, entry[0], entry[1], true);
|
2016-12-20 08:53:47 +01:00
|
|
|
}
|
|
|
|
} else if (headers) {
|
2018-04-24 10:24:51 +02:00
|
|
|
for (key in headers) {
|
|
|
|
if (hasOwnProperty(headers, key)) {
|
|
|
|
processHeader(this, state, key, headers[key], true);
|
2013-04-12 00:37:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-24 10:24:51 +02:00
|
|
|
let { header } = state;
|
|
|
|
|
2013-04-12 00:37:14 +02:00
|
|
|
// Date header
|
2016-12-31 22:18:43 +01:00
|
|
|
if (this.sendDate && !state.date) {
|
2018-04-24 10:24:51 +02:00
|
|
|
header += 'Date: ' + utcDate() + CRLF;
|
2013-04-12 00:37:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Force the connection to close when the response is a 204 No Content or
|
|
|
|
// a 304 Not Modified and the user has set a "Transfer-Encoding: chunked"
|
|
|
|
// header.
|
|
|
|
//
|
|
|
|
// RFC 2616 mandates that 204 and 304 responses MUST NOT have a body but
|
|
|
|
// node.js used to send out a zero chunk anyway to accommodate clients
|
|
|
|
// that don't have special handling for those responses.
|
|
|
|
//
|
|
|
|
// It was pointed out that this might confuse reverse proxies to the point
|
|
|
|
// of creating security liabilities, so suppress the zero chunk and force
|
|
|
|
// the connection to close.
|
2018-04-24 10:24:51 +02:00
|
|
|
if (this.chunkedEncoding && (this.statusCode === 204 ||
|
|
|
|
this.statusCode === 304)) {
|
|
|
|
debug(this.statusCode + ' response should not use chunked encoding,' +
|
2013-04-12 00:37:14 +02:00
|
|
|
' closing connection.');
|
|
|
|
this.chunkedEncoding = false;
|
|
|
|
this.shouldKeepAlive = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// keep-alive logic
|
2016-12-31 22:20:38 +01:00
|
|
|
if (this._removedConnection) {
|
2012-09-10 04:04:37 +02:00
|
|
|
this._last = true;
|
|
|
|
this.shouldKeepAlive = false;
|
2016-12-31 22:18:43 +01:00
|
|
|
} else if (!state.connection) {
|
2018-04-24 10:24:51 +02:00
|
|
|
const shouldSendKeepAlive = this.shouldKeepAlive &&
|
2016-12-31 22:18:43 +01:00
|
|
|
(state.contLen || this.useChunkedEncodingByDefault || this.agent);
|
2013-04-12 00:37:14 +02:00
|
|
|
if (shouldSendKeepAlive) {
|
2018-04-24 10:24:51 +02:00
|
|
|
header += 'Connection: keep-alive\r\n';
|
2013-04-12 00:37:14 +02:00
|
|
|
} else {
|
|
|
|
this._last = true;
|
2018-04-24 10:24:51 +02:00
|
|
|
header += 'Connection: close\r\n';
|
2013-04-12 00:37:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-31 22:18:43 +01:00
|
|
|
if (!state.contLen && !state.te) {
|
2015-03-03 21:01:26 +01:00
|
|
|
if (!this._hasBody) {
|
|
|
|
// Make sure we don't end the 0\r\n\r\n at the end of the message.
|
|
|
|
this.chunkedEncoding = false;
|
|
|
|
} else if (!this.useChunkedEncodingByDefault) {
|
|
|
|
this._last = true;
|
2017-10-17 00:37:14 +02:00
|
|
|
} else if (!state.trailer &&
|
|
|
|
!this._removedContLen &&
|
|
|
|
typeof this._contentLength === 'number') {
|
2018-04-24 10:24:51 +02:00
|
|
|
header += 'Content-Length: ' + this._contentLength + CRLF;
|
2017-10-17 00:37:14 +02:00
|
|
|
} else if (!this._removedTE) {
|
2018-04-24 10:24:51 +02:00
|
|
|
header += 'Transfer-Encoding: chunked\r\n';
|
2017-10-17 00:37:14 +02:00
|
|
|
this.chunkedEncoding = true;
|
2015-03-03 21:01:26 +01:00
|
|
|
} else {
|
2017-10-17 00:37:14 +02:00
|
|
|
// We should only be able to get here if both Content-Length and
|
|
|
|
// Transfer-Encoding are removed by the user.
|
|
|
|
// See: test/parallel/test-http-remove-header-stays-removed.js
|
|
|
|
debug('Both Content-Length and Transfer-Encoding are removed');
|
2013-04-12 00:37:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-27 05:00:09 +02:00
|
|
|
// Test non-chunked message does not have trailer header set,
|
|
|
|
// message will be terminated by the first empty line after the
|
|
|
|
// header fields, regardless of the header fields present in the
|
|
|
|
// message, and thus cannot contain a message body or 'trailers'.
|
|
|
|
if (this.chunkedEncoding !== true && state.trailer) {
|
2018-03-04 22:16:24 +01:00
|
|
|
throw new ERR_HTTP_TRAILER_INVALID();
|
2017-05-27 05:00:09 +02:00
|
|
|
}
|
|
|
|
|
2018-04-24 10:24:51 +02:00
|
|
|
this._header = header + CRLF;
|
2013-04-12 00:37:14 +02:00
|
|
|
this._headerSent = false;
|
|
|
|
|
|
|
|
// wait until the first body chunk, or close(), is sent to flush,
|
|
|
|
// UNLESS we're sending Expect: 100-continue.
|
2016-12-31 22:18:43 +01:00
|
|
|
if (state.expect) this._send('');
|
2016-10-12 15:48:40 +02:00
|
|
|
}
|
2013-04-12 00:37:14 +02:00
|
|
|
|
2018-04-24 10:24:51 +02:00
|
|
|
function processHeader(self, state, key, value, validate) {
|
|
|
|
if (validate)
|
|
|
|
validateHeaderName(key);
|
|
|
|
if (Array.isArray(value)) {
|
|
|
|
if (value.length < 2 || !isCookieField(key)) {
|
|
|
|
for (var i = 0; i < value.length; i++)
|
|
|
|
storeHeader(self, state, key, value[i], validate);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
value = value.join('; ');
|
2016-02-04 02:32:23 +01:00
|
|
|
}
|
2018-04-24 10:24:51 +02:00
|
|
|
storeHeader(self, state, key, value, validate);
|
|
|
|
}
|
|
|
|
|
|
|
|
function storeHeader(self, state, key, value, validate) {
|
|
|
|
if (validate)
|
|
|
|
validateHeaderValue(key, value);
|
2017-01-06 09:12:59 +01:00
|
|
|
state.header += key + ': ' + escapeHeaderValue(value) + CRLF;
|
|
|
|
matchHeader(self, state, key, value);
|
2016-12-31 22:18:43 +01:00
|
|
|
}
|
2013-04-12 00:37:14 +02:00
|
|
|
|
2016-12-31 22:18:43 +01:00
|
|
|
function matchHeader(self, state, field, value) {
|
2018-04-18 15:45:45 +02:00
|
|
|
if (field.length < 4 || field.length > 17)
|
2016-12-31 22:18:43 +01:00
|
|
|
return;
|
2018-04-18 15:45:45 +02:00
|
|
|
field = field.toLowerCase();
|
|
|
|
switch (field) {
|
|
|
|
case 'connection':
|
|
|
|
state.connection = true;
|
2018-04-24 10:24:51 +02:00
|
|
|
self._removedConnection = false;
|
2018-04-18 16:39:27 +02:00
|
|
|
if (RE_CONN_CLOSE.test(value))
|
|
|
|
self._last = true;
|
|
|
|
else
|
|
|
|
self.shouldKeepAlive = true;
|
2018-04-18 15:45:45 +02:00
|
|
|
break;
|
|
|
|
case 'transfer-encoding':
|
|
|
|
state.te = true;
|
2018-04-24 10:24:51 +02:00
|
|
|
self._removedTE = false;
|
2018-04-18 15:45:45 +02:00
|
|
|
if (RE_TE_CHUNKED.test(value)) self.chunkedEncoding = true;
|
|
|
|
break;
|
|
|
|
case 'content-length':
|
|
|
|
state.contLen = true;
|
2018-04-24 10:24:51 +02:00
|
|
|
self._removedContLen = false;
|
2018-04-18 15:45:45 +02:00
|
|
|
break;
|
|
|
|
case 'date':
|
|
|
|
case 'expect':
|
|
|
|
case 'trailer':
|
|
|
|
state[field] = true;
|
|
|
|
break;
|
2016-12-31 22:18:43 +01:00
|
|
|
}
|
|
|
|
}
|
2013-04-12 00:37:14 +02:00
|
|
|
|
2018-04-24 10:24:51 +02:00
|
|
|
function validateHeaderName(name) {
|
2017-11-03 13:04:12 +01:00
|
|
|
if (typeof name !== 'string' || !name || !checkIsHttpToken(name)) {
|
2018-04-24 10:24:51 +02:00
|
|
|
const err = new ERR_INVALID_HTTP_TOKEN('Header name', name);
|
|
|
|
Error.captureStackTrace(err, validateHeaderName);
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function validateHeaderValue(name, value) {
|
|
|
|
let err;
|
|
|
|
if (value === undefined) {
|
2018-03-04 22:16:24 +01:00
|
|
|
err = new ERR_HTTP_INVALID_HEADER_VALUE(value, name);
|
2017-11-03 13:04:12 +01:00
|
|
|
} else if (checkInvalidHeaderChar(value)) {
|
2016-10-19 23:28:16 +02:00
|
|
|
debug('Header "%s" contains invalid characters', name);
|
2018-03-04 22:16:24 +01:00
|
|
|
err = new ERR_INVALID_CHAR('header content', name);
|
2017-11-03 13:04:12 +01:00
|
|
|
}
|
|
|
|
if (err !== undefined) {
|
2018-04-24 10:24:51 +02:00
|
|
|
Error.captureStackTrace(err, validateHeaderValue);
|
2017-11-03 13:04:12 +01:00
|
|
|
throw err;
|
2016-02-04 02:32:23 +01:00
|
|
|
}
|
2016-12-31 22:18:43 +01:00
|
|
|
}
|
2017-11-03 13:04:12 +01:00
|
|
|
|
2016-12-31 22:18:43 +01:00
|
|
|
OutgoingMessage.prototype.setHeader = function setHeader(name, value) {
|
2017-11-03 13:04:12 +01:00
|
|
|
if (this._header) {
|
2018-03-04 22:16:24 +01:00
|
|
|
throw new ERR_HTTP_HEADERS_SENT('set');
|
2017-11-03 13:04:12 +01:00
|
|
|
}
|
2018-04-24 10:24:51 +02:00
|
|
|
validateHeaderName(name);
|
|
|
|
validateHeaderValue(name, value);
|
2016-12-31 22:18:43 +01:00
|
|
|
|
2018-04-24 10:24:51 +02:00
|
|
|
let headers = this[outHeadersKey];
|
|
|
|
if (headers === null)
|
|
|
|
this[outHeadersKey] = headers = Object.create(null);
|
2013-04-12 00:37:14 +02:00
|
|
|
|
2018-04-24 10:24:51 +02:00
|
|
|
headers[name.toLowerCase()] = [name, value];
|
2013-04-12 00:37:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-10-12 15:48:40 +02:00
|
|
|
OutgoingMessage.prototype.getHeader = function getHeader(name) {
|
2017-01-10 17:58:08 +01:00
|
|
|
if (typeof name !== 'string') {
|
2018-03-19 13:33:46 +01:00
|
|
|
throw new ERR_INVALID_ARG_TYPE('name', 'string', name);
|
2013-04-12 00:37:14 +02:00
|
|
|
}
|
|
|
|
|
2018-04-24 10:24:51 +02:00
|
|
|
const headers = this[outHeadersKey];
|
|
|
|
if (headers === null)
|
2016-12-31 22:18:43 +01:00
|
|
|
return;
|
2018-04-24 10:24:51 +02:00
|
|
|
|
|
|
|
const entry = headers[name.toLowerCase()];
|
|
|
|
return entry && entry[1];
|
2013-04-12 00:37:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-01-14 15:39:58 +01:00
|
|
|
// Returns an array of the names of the current outgoing headers.
|
|
|
|
OutgoingMessage.prototype.getHeaderNames = function getHeaderNames() {
|
2018-04-24 10:24:51 +02:00
|
|
|
return this[outHeadersKey] !== null ? Object.keys(this[outHeadersKey]) : [];
|
2017-01-14 15:39:58 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Returns a shallow copy of the current outgoing headers.
|
|
|
|
OutgoingMessage.prototype.getHeaders = function getHeaders() {
|
2017-03-06 03:13:09 +01:00
|
|
|
const headers = this[outHeadersKey];
|
2017-03-20 00:11:10 +01:00
|
|
|
const ret = Object.create(null);
|
2017-01-14 15:39:58 +01:00
|
|
|
if (headers) {
|
|
|
|
const keys = Object.keys(headers);
|
|
|
|
for (var i = 0; i < keys.length; ++i) {
|
|
|
|
const key = keys[i];
|
|
|
|
const val = headers[key][1];
|
|
|
|
ret[key] = val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
OutgoingMessage.prototype.hasHeader = function hasHeader(name) {
|
|
|
|
if (typeof name !== 'string') {
|
2018-03-19 13:33:46 +01:00
|
|
|
throw new ERR_INVALID_ARG_TYPE('name', 'string', name);
|
2017-01-14 15:39:58 +01:00
|
|
|
}
|
|
|
|
|
2018-04-24 10:24:51 +02:00
|
|
|
return this[outHeadersKey] !== null &&
|
|
|
|
!!this[outHeadersKey][name.toLowerCase()];
|
2017-01-14 15:39:58 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-10-12 15:48:40 +02:00
|
|
|
OutgoingMessage.prototype.removeHeader = function removeHeader(name) {
|
2017-01-10 17:58:08 +01:00
|
|
|
if (typeof name !== 'string') {
|
2018-03-19 13:33:46 +01:00
|
|
|
throw new ERR_INVALID_ARG_TYPE('name', 'string', name);
|
2013-04-12 00:37:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this._header) {
|
2018-03-04 22:16:24 +01:00
|
|
|
throw new ERR_HTTP_HEADERS_SENT('remove');
|
2013-04-12 00:37:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var key = name.toLowerCase();
|
2012-09-10 04:04:37 +02:00
|
|
|
|
2018-01-24 01:04:17 +01:00
|
|
|
switch (key) {
|
|
|
|
case 'connection':
|
|
|
|
this._removedConnection = true;
|
2016-12-31 22:20:38 +01:00
|
|
|
break;
|
2018-01-24 01:04:17 +01:00
|
|
|
case 'content-length':
|
|
|
|
this._removedContLen = true;
|
2016-12-31 22:20:38 +01:00
|
|
|
break;
|
2018-01-24 01:04:17 +01:00
|
|
|
case 'transfer-encoding':
|
|
|
|
this._removedTE = true;
|
2016-12-31 22:20:38 +01:00
|
|
|
break;
|
2018-01-24 01:04:17 +01:00
|
|
|
case 'date':
|
|
|
|
this.sendDate = false;
|
2016-12-31 22:20:38 +01:00
|
|
|
break;
|
|
|
|
}
|
2012-09-10 04:04:37 +02:00
|
|
|
|
2018-04-24 10:24:51 +02:00
|
|
|
if (this[outHeadersKey] !== null) {
|
2017-03-06 03:13:09 +01:00
|
|
|
delete this[outHeadersKey][key];
|
2012-09-10 04:04:37 +02:00
|
|
|
}
|
2013-04-12 00:37:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-10-12 15:48:40 +02:00
|
|
|
OutgoingMessage.prototype._implicitHeader = function _implicitHeader() {
|
2018-02-16 12:53:34 +01:00
|
|
|
this.emit('error', new ERR_METHOD_NOT_IMPLEMENTED('_implicitHeader()'));
|
2016-08-02 18:28:38 +02:00
|
|
|
};
|
2013-04-12 00:37:14 +02:00
|
|
|
|
|
|
|
Object.defineProperty(OutgoingMessage.prototype, 'headersSent', {
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
get: function() { return !!this._header; }
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2016-12-31 22:27:40 +01:00
|
|
|
const crlf_buf = Buffer.from('\r\n');
|
2016-10-12 15:48:40 +02:00
|
|
|
OutgoingMessage.prototype.write = function write(chunk, encoding, callback) {
|
2017-05-13 16:07:44 +02:00
|
|
|
return write_(this, chunk, encoding, callback, false);
|
|
|
|
};
|
|
|
|
|
|
|
|
function write_(msg, chunk, encoding, callback, fromEnd) {
|
|
|
|
if (msg.finished) {
|
2018-03-04 22:16:24 +01:00
|
|
|
const err = new ERR_STREAM_WRITE_AFTER_END();
|
2018-03-05 12:20:48 +01:00
|
|
|
const triggerAsyncId = msg.socket ? msg.socket[async_id_symbol] : undefined;
|
|
|
|
defaultTriggerAsyncIdScope(triggerAsyncId,
|
|
|
|
process.nextTick,
|
|
|
|
writeAfterEndNT,
|
|
|
|
msg,
|
|
|
|
err,
|
|
|
|
callback);
|
2014-09-23 02:21:40 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-13 16:07:44 +02:00
|
|
|
if (!msg._header) {
|
|
|
|
msg._implicitHeader();
|
2013-04-12 00:37:14 +02:00
|
|
|
}
|
|
|
|
|
2017-05-13 16:07:44 +02:00
|
|
|
if (!msg._hasBody) {
|
2013-04-12 00:37:14 +02:00
|
|
|
debug('This type of response MUST NOT have a body. ' +
|
|
|
|
'Ignoring write() calls.');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-13 16:07:44 +02:00
|
|
|
if (!fromEnd && typeof chunk !== 'string' && !(chunk instanceof Buffer)) {
|
2018-03-19 13:33:46 +01:00
|
|
|
throw new ERR_INVALID_ARG_TYPE('first argument',
|
|
|
|
['string', 'Buffer'], chunk);
|
2013-04-12 00:37:14 +02:00
|
|
|
}
|
|
|
|
|
2013-05-25 02:16:08 +02:00
|
|
|
|
|
|
|
// If we get an empty string or buffer, then just do nothing, and
|
|
|
|
// signal the user to keep writing.
|
|
|
|
if (chunk.length === 0) return true;
|
2013-04-12 00:37:14 +02:00
|
|
|
|
2018-01-23 21:23:33 +01:00
|
|
|
if (!fromEnd && msg.connection && !msg[kIsCorked]) {
|
2017-06-07 17:25:24 +02:00
|
|
|
msg.connection.cork();
|
2018-01-23 21:23:33 +01:00
|
|
|
msg[kIsCorked] = true;
|
|
|
|
process.nextTick(connectionCorkNT, msg, msg.connection);
|
2017-06-07 17:25:24 +02:00
|
|
|
}
|
|
|
|
|
2013-04-12 00:37:14 +02:00
|
|
|
var len, ret;
|
2017-05-13 16:07:44 +02:00
|
|
|
if (msg.chunkedEncoding) {
|
|
|
|
if (typeof chunk === 'string')
|
2013-04-12 00:37:14 +02:00
|
|
|
len = Buffer.byteLength(chunk, encoding);
|
2017-05-13 16:07:44 +02:00
|
|
|
else
|
|
|
|
len = chunk.length;
|
2016-12-31 22:18:43 +01:00
|
|
|
|
2017-05-13 16:07:44 +02:00
|
|
|
msg._send(len.toString(16), 'latin1', null);
|
|
|
|
msg._send(crlf_buf, null, null);
|
|
|
|
msg._send(chunk, encoding, null);
|
|
|
|
ret = msg._send(crlf_buf, null, callback);
|
2013-04-12 00:37:14 +02:00
|
|
|
} else {
|
2017-05-13 16:07:44 +02:00
|
|
|
ret = msg._send(chunk, encoding, callback);
|
2013-04-12 00:37:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
debug('write ret = ' + ret);
|
|
|
|
return ret;
|
2017-05-13 16:07:44 +02:00
|
|
|
}
|
2013-04-12 00:37:14 +02:00
|
|
|
|
|
|
|
|
2018-03-05 12:20:48 +01:00
|
|
|
function writeAfterEndNT(msg, err, callback) {
|
|
|
|
msg.emit('error', err);
|
2015-03-05 22:07:27 +01:00
|
|
|
if (callback) callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-23 21:23:33 +01:00
|
|
|
function connectionCorkNT(msg, conn) {
|
|
|
|
msg[kIsCorked] = false;
|
2016-02-15 10:47:06 +01:00
|
|
|
conn.uncork();
|
2015-03-05 22:07:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-18 05:06:35 +02:00
|
|
|
function escapeHeaderValue(value) {
|
|
|
|
// Protect against response splitting. The regex test is there to
|
|
|
|
// minimize the performance impact in the common case.
|
|
|
|
return /[\r\n]/.test(value) ? value.replace(/[\r\n]+[ \t]*/g, '') : value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-12 15:48:40 +02:00
|
|
|
OutgoingMessage.prototype.addTrailers = function addTrailers(headers) {
|
2013-04-12 00:37:14 +02:00
|
|
|
this._trailer = '';
|
|
|
|
var keys = Object.keys(headers);
|
2015-01-29 02:05:53 +01:00
|
|
|
var isArray = Array.isArray(headers);
|
2013-04-12 00:37:14 +02:00
|
|
|
var field, value;
|
|
|
|
for (var i = 0, l = keys.length; i < l; i++) {
|
|
|
|
var key = keys[i];
|
|
|
|
if (isArray) {
|
|
|
|
field = headers[key][0];
|
|
|
|
value = headers[key][1];
|
|
|
|
} else {
|
|
|
|
field = key;
|
|
|
|
value = headers[key];
|
|
|
|
}
|
2017-01-06 09:12:59 +01:00
|
|
|
if (typeof field !== 'string' || !field || !checkIsHttpToken(field)) {
|
2018-03-04 22:16:24 +01:00
|
|
|
throw new ERR_INVALID_HTTP_TOKEN('Trailer name', field);
|
2015-08-24 20:58:48 +02:00
|
|
|
}
|
2016-12-20 08:48:18 +01:00
|
|
|
if (checkInvalidHeaderChar(value)) {
|
2016-10-19 23:28:16 +02:00
|
|
|
debug('Trailer "%s" contains invalid characters', field);
|
2018-03-04 22:16:24 +01:00
|
|
|
throw new ERR_INVALID_CHAR('trailer content', field);
|
2016-02-04 02:32:23 +01:00
|
|
|
}
|
2015-09-18 05:06:35 +02:00
|
|
|
this._trailer += field + ': ' + escapeHeaderValue(value) + CRLF;
|
2013-04-12 00:37:14 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-12-19 03:25:31 +01:00
|
|
|
function onFinish(outmsg) {
|
|
|
|
outmsg.emit('finish');
|
|
|
|
}
|
2013-04-12 00:37:14 +02:00
|
|
|
|
2017-05-13 16:07:44 +02:00
|
|
|
OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
|
|
|
|
if (typeof chunk === 'function') {
|
|
|
|
callback = chunk;
|
|
|
|
chunk = null;
|
2015-01-29 02:05:53 +01:00
|
|
|
} else if (typeof encoding === 'function') {
|
2013-08-15 17:51:37 +02:00
|
|
|
callback = encoding;
|
|
|
|
encoding = null;
|
|
|
|
}
|
|
|
|
|
2013-04-12 00:37:14 +02:00
|
|
|
if (this.finished) {
|
2018-02-14 13:32:01 +01:00
|
|
|
return this;
|
2013-04-12 00:37:14 +02:00
|
|
|
}
|
2013-08-15 20:08:19 +02:00
|
|
|
|
2017-05-13 16:07:44 +02:00
|
|
|
var uncork;
|
|
|
|
if (chunk) {
|
|
|
|
if (typeof chunk !== 'string' && !(chunk instanceof Buffer)) {
|
2018-03-19 14:18:50 +01:00
|
|
|
throw new ERR_INVALID_ARG_TYPE('chunk', ['string', 'Buffer'], chunk);
|
2017-05-13 16:07:44 +02:00
|
|
|
}
|
|
|
|
if (!this._header) {
|
|
|
|
if (typeof chunk === 'string')
|
|
|
|
this._contentLength = Buffer.byteLength(chunk, encoding);
|
2015-03-03 21:01:26 +01:00
|
|
|
else
|
2017-05-13 16:07:44 +02:00
|
|
|
this._contentLength = chunk.length;
|
|
|
|
}
|
|
|
|
if (this.connection) {
|
|
|
|
this.connection.cork();
|
|
|
|
uncork = true;
|
2015-03-03 21:01:26 +01:00
|
|
|
}
|
2017-05-13 16:07:44 +02:00
|
|
|
write_(this, chunk, encoding, null, true);
|
|
|
|
} else if (!this._header) {
|
|
|
|
this._contentLength = 0;
|
2013-04-12 00:37:14 +02:00
|
|
|
this._implicitHeader();
|
|
|
|
}
|
|
|
|
|
2016-06-23 01:35:57 +02:00
|
|
|
if (typeof callback === 'function')
|
|
|
|
this.once('finish', callback);
|
|
|
|
|
2016-12-19 03:25:31 +01:00
|
|
|
var finish = onFinish.bind(undefined, this);
|
2016-06-23 01:35:57 +02:00
|
|
|
|
2014-09-17 02:48:09 +02:00
|
|
|
if (this._hasBody && this.chunkedEncoding) {
|
2018-02-14 13:32:01 +01:00
|
|
|
this._send('0\r\n' + this._trailer + '\r\n', 'latin1', finish);
|
2013-05-08 23:28:56 +02:00
|
|
|
} else {
|
|
|
|
// Force a flush, HACK.
|
2018-02-14 13:32:01 +01:00
|
|
|
this._send('', 'latin1', finish);
|
2013-04-12 00:37:14 +02:00
|
|
|
}
|
|
|
|
|
2017-05-13 16:07:44 +02:00
|
|
|
if (uncork)
|
2013-05-08 23:28:56 +02:00
|
|
|
this.connection.uncork();
|
|
|
|
|
2013-04-12 00:37:14 +02:00
|
|
|
this.finished = true;
|
|
|
|
|
|
|
|
// There is the first message on the outgoing queue, and we've sent
|
|
|
|
// everything to the socket.
|
|
|
|
debug('outgoing message end.');
|
2015-07-13 21:38:25 +02:00
|
|
|
if (this.output.length === 0 &&
|
|
|
|
this.connection &&
|
|
|
|
this.connection._httpMessage === this) {
|
2013-04-12 00:37:14 +02:00
|
|
|
this._finish();
|
|
|
|
}
|
|
|
|
|
2018-02-14 13:32:01 +01:00
|
|
|
return this;
|
2013-04-12 00:37:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-10-12 15:48:40 +02:00
|
|
|
OutgoingMessage.prototype._finish = function _finish() {
|
2013-04-12 00:37:14 +02:00
|
|
|
assert(this.connection);
|
2013-08-15 20:08:19 +02:00
|
|
|
this.emit('prefinish');
|
2013-04-12 00:37:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-08-15 17:54:49 +02:00
|
|
|
// This logic is probably a bit confusing. Let me explain a bit:
|
|
|
|
//
|
|
|
|
// In both HTTP servers and clients it is possible to queue up several
|
|
|
|
// outgoing messages. This is easiest to imagine in the case of a client.
|
|
|
|
// Take the following situation:
|
|
|
|
//
|
|
|
|
// req1 = client.request('GET', '/');
|
|
|
|
// req2 = client.request('POST', '/');
|
|
|
|
//
|
|
|
|
// When the user does
|
|
|
|
//
|
|
|
|
// req2.write('hello world\n');
|
|
|
|
//
|
|
|
|
// it's possible that the first request has not been completely flushed to
|
|
|
|
// the socket yet. Thus the outgoing messages need to be prepared to queue
|
|
|
|
// up data internally before sending it on further to the socket's queue.
|
|
|
|
//
|
|
|
|
// This function, outgoingFlush(), is called by both the Server and Client
|
|
|
|
// to attempt to flush any pending messages out to the socket.
|
2016-10-12 15:48:40 +02:00
|
|
|
OutgoingMessage.prototype._flush = function _flush() {
|
2015-01-26 00:26:40 +01:00
|
|
|
var socket = this.socket;
|
2015-10-13 08:16:39 +02:00
|
|
|
var ret;
|
2015-01-26 00:26:40 +01:00
|
|
|
|
|
|
|
if (socket && socket.writable) {
|
|
|
|
// There might be remaining data in this.output; write it out
|
2015-10-13 08:16:39 +02:00
|
|
|
ret = this._flushOutput(socket);
|
2013-04-12 00:37:14 +02:00
|
|
|
|
2013-08-15 17:54:49 +02:00
|
|
|
if (this.finished) {
|
|
|
|
// This is a queue to the server or client to bring in the next this.
|
|
|
|
this._finish();
|
|
|
|
} else if (ret) {
|
|
|
|
// This is necessary to prevent https from breaking
|
|
|
|
this.emit('drain');
|
|
|
|
}
|
2013-04-12 00:37:14 +02:00
|
|
|
}
|
|
|
|
};
|
2014-04-09 15:02:03 +02:00
|
|
|
|
2015-10-13 08:16:39 +02:00
|
|
|
OutgoingMessage.prototype._flushOutput = function _flushOutput(socket) {
|
|
|
|
var ret;
|
|
|
|
var outputLength = this.output.length;
|
|
|
|
if (outputLength <= 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
var output = this.output;
|
|
|
|
var outputEncodings = this.outputEncodings;
|
|
|
|
var outputCallbacks = this.outputCallbacks;
|
|
|
|
socket.cork();
|
|
|
|
for (var i = 0; i < outputLength; i++) {
|
2016-12-18 14:56:52 +01:00
|
|
|
ret = socket.write(output[i], outputEncodings[i], outputCallbacks[i]);
|
2015-10-13 08:16:39 +02:00
|
|
|
}
|
|
|
|
socket.uncork();
|
|
|
|
|
|
|
|
this.output = [];
|
|
|
|
this.outputEncodings = [];
|
|
|
|
this.outputCallbacks = [];
|
2017-05-13 16:07:44 +02:00
|
|
|
this._onPendingData(-this.outputSize);
|
2015-10-13 08:16:39 +02:00
|
|
|
this.outputSize = 0;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
2014-04-09 15:02:03 +02:00
|
|
|
|
2016-10-12 15:48:40 +02:00
|
|
|
OutgoingMessage.prototype.flushHeaders = function flushHeaders() {
|
2014-04-09 15:02:03 +02:00
|
|
|
if (!this._header) {
|
|
|
|
this._implicitHeader();
|
|
|
|
}
|
2015-05-13 17:52:49 +02:00
|
|
|
|
|
|
|
// Force-flush the headers.
|
|
|
|
this._send('');
|
2014-04-09 15:02:03 +02:00
|
|
|
};
|
2015-03-14 08:24:07 +01:00
|
|
|
|
2015-06-13 18:44:39 +02:00
|
|
|
OutgoingMessage.prototype.flush = internalUtil.deprecate(function() {
|
2015-03-14 08:24:07 +01:00
|
|
|
this.flushHeaders();
|
2016-12-04 21:47:01 +01:00
|
|
|
}, 'OutgoingMessage.flush is deprecated. Use flushHeaders instead.', 'DEP0001');
|
2017-02-28 01:09:32 +01:00
|
|
|
|
2017-07-19 05:30:07 +02:00
|
|
|
OutgoingMessage.prototype.pipe = function pipe() {
|
|
|
|
// OutgoingMessage should be write-only. Piping from it is disabled.
|
2018-03-04 22:16:24 +01:00
|
|
|
this.emit('error', new ERR_STREAM_CANNOT_PIPE());
|
2017-07-19 05:30:07 +02:00
|
|
|
};
|
2017-02-28 01:09:32 +01:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
OutgoingMessage
|
|
|
|
};
|