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 timers = require('timers');
|
|
|
|
const util = require('util');
|
2015-06-13 18:44:39 +02:00
|
|
|
const internalUtil = require('internal/util');
|
2015-05-29 19:35:43 +02:00
|
|
|
const Buffer = require('buffer').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;
|
2015-01-21 17:36:59 +01:00
|
|
|
|
|
|
|
const CRLF = common.CRLF;
|
2016-08-23 19:49:21 +02:00
|
|
|
const trfrEncChunkExpression = common.chunkExpression;
|
2015-01-21 17:36:59 +01:00
|
|
|
const debug = common.debug;
|
|
|
|
|
2016-08-23 19:49:21 +02:00
|
|
|
const upgradeExpression = /^Upgrade$/i;
|
2015-02-13 03:20:06 +01:00
|
|
|
const transferEncodingExpression = /^Transfer-Encoding$/i;
|
|
|
|
const contentLengthExpression = /^Content-Length$/i;
|
|
|
|
const dateExpression = /^Date$/i;
|
|
|
|
const expectExpression = /^Expect$/i;
|
2015-03-03 21:01:26 +01:00
|
|
|
const trailerExpression = /^Trailer$/i;
|
2016-08-23 19:49:21 +02:00
|
|
|
const connectionExpression = /^Connection$/i;
|
|
|
|
const connCloseExpression = /(^|\W)close(\W|$)/i;
|
|
|
|
const connUpgradeExpression = /(^|\W)upgrade(\W|$)/i;
|
2015-01-21 17:36:59 +01:00
|
|
|
|
|
|
|
const automaticHeaders = {
|
2012-09-10 04:04:37 +02:00
|
|
|
connection: true,
|
|
|
|
'content-length': true,
|
|
|
|
'transfer-encoding': true,
|
|
|
|
date: true
|
|
|
|
};
|
|
|
|
|
2013-04-12 00:37:14 +02:00
|
|
|
|
|
|
|
var dateCache;
|
|
|
|
function utcDate() {
|
|
|
|
if (!dateCache) {
|
|
|
|
var d = new Date();
|
|
|
|
dateCache = d.toUTCString();
|
2013-07-08 06:11:22 +02:00
|
|
|
timers.enroll(utcDate, 1000 - d.getMilliseconds());
|
|
|
|
timers._unrefActive(utcDate);
|
2013-04-12 00:37:14 +02:00
|
|
|
}
|
|
|
|
return dateCache;
|
|
|
|
}
|
2016-10-12 15:48:40 +02:00
|
|
|
utcDate._onTimeout = function _onTimeout() {
|
2013-07-08 06:11:22 +02:00
|
|
|
dateCache = undefined;
|
|
|
|
};
|
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;
|
2016-08-23 19:49:21 +02:00
|
|
|
this.upgrading = false;
|
2013-04-12 00:37:14 +02:00
|
|
|
this.chunkedEncoding = false;
|
|
|
|
this.shouldKeepAlive = true;
|
|
|
|
this.useChunkedEncodingByDefault = true;
|
|
|
|
this.sendDate = false;
|
2012-09-10 04:04:37 +02:00
|
|
|
this._removedHeader = {};
|
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;
|
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;
|
|
|
|
this._headers = null;
|
|
|
|
this._headerNames = {};
|
2015-10-03 21:27:19 +02:00
|
|
|
|
|
|
|
this._onPendingData = null;
|
2013-04-12 00:37:14 +02:00
|
|
|
}
|
|
|
|
util.inherits(OutgoingMessage, Stream);
|
|
|
|
|
|
|
|
|
|
|
|
exports.OutgoingMessage = OutgoingMessage;
|
|
|
|
|
|
|
|
|
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) {
|
2013-04-22 17:52:42 +02:00
|
|
|
if (this.socket)
|
|
|
|
this.socket.destroy(error);
|
|
|
|
else
|
|
|
|
this.once('socket', function(socket) {
|
|
|
|
socket.destroy(error);
|
|
|
|
});
|
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' &&
|
2013-08-15 07:14:49 +02:00
|
|
|
encoding !== 'hex' &&
|
|
|
|
encoding !== 'base64') {
|
2013-04-12 00:37:14 +02:00
|
|
|
data = this._header + data;
|
|
|
|
} else {
|
|
|
|
this.output.unshift(this._header);
|
2016-06-02 18:55:36 +02:00
|
|
|
this.outputEncodings.unshift('latin1');
|
2013-08-15 17:51:37 +02:00
|
|
|
this.outputCallbacks.unshift(null);
|
2015-10-03 21:27:19 +02:00
|
|
|
this.outputSize += this._header.length;
|
2015-10-13 08:16:39 +02:00
|
|
|
if (typeof this._onPendingData === 'function')
|
2015-10-03 21:27:19 +02:00
|
|
|
this._onPendingData(this._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) {
|
2015-01-29 02:05:53 +01:00
|
|
|
if (typeof encoding === 'function') {
|
2013-08-15 17:51:37 +02:00
|
|
|
callback = encoding;
|
|
|
|
encoding = null;
|
|
|
|
}
|
|
|
|
|
2015-01-26 00:26:40 +01:00
|
|
|
var connection = this.connection;
|
|
|
|
if (connection &&
|
|
|
|
connection._httpMessage === this &&
|
|
|
|
connection.writable &&
|
|
|
|
!connection.destroyed) {
|
2013-04-12 00:37:14 +02:00
|
|
|
// There might be pending data in the this.output buffer.
|
2015-01-26 00:26:40 +01:00
|
|
|
var outputLength = this.output.length;
|
|
|
|
if (outputLength > 0) {
|
2015-10-13 08:16:39 +02:00
|
|
|
this._flushOutput(connection);
|
2015-09-26 05:36:06 +02:00
|
|
|
} else if (data.length === 0) {
|
|
|
|
if (typeof callback === 'function')
|
|
|
|
process.nextTick(callback);
|
|
|
|
return true;
|
2013-04-12 00:37:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Directly write to socket.
|
2015-01-26 00:26:40 +01:00
|
|
|
return connection.write(data, encoding, callback);
|
|
|
|
} else if (connection && connection.destroyed) {
|
2013-04-12 00:37:14 +02:00
|
|
|
// The socket was destroyed. If we're still trying to write to it,
|
|
|
|
// then we haven't gotten the 'close' event yet.
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
// buffer, as long as we're not destroyed.
|
2015-01-26 00:26:40 +01:00
|
|
|
return this._buffer(data, encoding, callback);
|
2013-04-12 00:37:14 +02:00
|
|
|
}
|
2016-10-12 15:48:40 +02:00
|
|
|
}
|
2013-04-12 00:37:14 +02:00
|
|
|
|
|
|
|
|
2016-10-12 15:48:40 +02:00
|
|
|
OutgoingMessage.prototype._buffer = function _buffer(data, encoding, callback) {
|
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;
|
2015-10-13 08:16:39 +02:00
|
|
|
if (typeof this._onPendingData === 'function')
|
2015-10-03 21:27:19 +02:00
|
|
|
this._onPendingData(data.length);
|
2013-04-12 00:37:14 +02:00
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
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'
|
|
|
|
var state = {
|
|
|
|
sentConnectionHeader: false,
|
2016-08-23 19:49:21 +02:00
|
|
|
sentConnectionUpgrade: false,
|
2013-04-12 00:37:14 +02:00
|
|
|
sentContentLengthHeader: false,
|
|
|
|
sentTransferEncodingHeader: false,
|
|
|
|
sentDateHeader: false,
|
|
|
|
sentExpect: false,
|
2015-03-03 21:01:26 +01:00
|
|
|
sentTrailer: false,
|
2016-08-23 19:49:21 +02:00
|
|
|
sentUpgrade: false,
|
2013-04-12 00:37:14 +02:00
|
|
|
messageHeader: firstLine
|
|
|
|
};
|
|
|
|
|
2016-12-20 08:53:47 +01:00
|
|
|
var i;
|
|
|
|
var j;
|
|
|
|
var field;
|
|
|
|
var value;
|
|
|
|
if (headers instanceof Array) {
|
|
|
|
for (i = 0; i < headers.length; ++i) {
|
|
|
|
field = headers[i][0];
|
|
|
|
value = headers[i][1];
|
|
|
|
|
|
|
|
if (value instanceof Array) {
|
|
|
|
for (j = 0; j < value.length; j++) {
|
|
|
|
storeHeader(this, state, field, value[j]);
|
|
|
|
}
|
2013-04-12 00:37:14 +02:00
|
|
|
} else {
|
2016-12-20 08:53:47 +01:00
|
|
|
storeHeader(this, state, field, value);
|
2013-04-12 00:37:14 +02:00
|
|
|
}
|
2016-12-20 08:53:47 +01:00
|
|
|
}
|
|
|
|
} else if (headers) {
|
|
|
|
var keys = Object.keys(headers);
|
|
|
|
for (i = 0; i < keys.length; ++i) {
|
|
|
|
field = keys[i];
|
|
|
|
value = headers[field];
|
2013-04-12 00:37:14 +02:00
|
|
|
|
2016-12-20 08:53:47 +01:00
|
|
|
if (value instanceof Array) {
|
|
|
|
for (j = 0; j < value.length; j++) {
|
2013-04-12 00:37:14 +02:00
|
|
|
storeHeader(this, state, field, value[j]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
storeHeader(this, state, field, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-23 19:49:21 +02:00
|
|
|
// Are we upgrading the connection?
|
|
|
|
if (state.sentConnectionUpgrade && state.sentUpgrade)
|
|
|
|
this.upgrading = true;
|
|
|
|
|
2013-04-12 00:37:14 +02:00
|
|
|
// Date header
|
2016-12-20 08:50:34 +01:00
|
|
|
if (this.sendDate && !state.sentDateHeader) {
|
2013-04-12 00:37:14 +02:00
|
|
|
state.messageHeader += 'Date: ' + utcDate() + CRLF;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
var statusCode = this.statusCode;
|
2016-12-20 08:50:34 +01:00
|
|
|
if ((statusCode === 204 || statusCode === 304) && this.chunkedEncoding) {
|
2013-04-12 00:37:14 +02:00
|
|
|
debug(statusCode + ' response should not use chunked encoding,' +
|
|
|
|
' closing connection.');
|
|
|
|
this.chunkedEncoding = false;
|
|
|
|
this.shouldKeepAlive = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// keep-alive logic
|
2012-09-10 04:04:37 +02:00
|
|
|
if (this._removedHeader.connection) {
|
|
|
|
this._last = true;
|
|
|
|
this.shouldKeepAlive = false;
|
2016-12-20 08:50:34 +01:00
|
|
|
} else if (!state.sentConnectionHeader) {
|
2013-04-12 00:37:14 +02:00
|
|
|
var shouldSendKeepAlive = this.shouldKeepAlive &&
|
|
|
|
(state.sentContentLengthHeader ||
|
|
|
|
this.useChunkedEncodingByDefault ||
|
|
|
|
this.agent);
|
|
|
|
if (shouldSendKeepAlive) {
|
|
|
|
state.messageHeader += 'Connection: keep-alive\r\n';
|
|
|
|
} else {
|
|
|
|
this._last = true;
|
|
|
|
state.messageHeader += 'Connection: close\r\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-20 08:50:34 +01:00
|
|
|
if (!state.sentContentLengthHeader && !state.sentTransferEncodingHeader) {
|
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;
|
|
|
|
} else {
|
|
|
|
if (!state.sentTrailer &&
|
|
|
|
!this._removedHeader['content-length'] &&
|
|
|
|
typeof this._contentLength === 'number') {
|
|
|
|
state.messageHeader += 'Content-Length: ' + this._contentLength +
|
|
|
|
'\r\n';
|
|
|
|
} else if (!this._removedHeader['transfer-encoding']) {
|
2013-04-12 00:37:14 +02:00
|
|
|
state.messageHeader += 'Transfer-Encoding: chunked\r\n';
|
|
|
|
this.chunkedEncoding = true;
|
|
|
|
} else {
|
2015-03-03 21:01:26 +01: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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this._header = state.messageHeader + CRLF;
|
|
|
|
this._headerSent = false;
|
|
|
|
|
|
|
|
// wait until the first body chunk, or close(), is sent to flush,
|
|
|
|
// UNLESS we're sending Expect: 100-continue.
|
|
|
|
if (state.sentExpect) this._send('');
|
2016-10-12 15:48:40 +02:00
|
|
|
}
|
2013-04-12 00:37:14 +02:00
|
|
|
|
|
|
|
function storeHeader(self, state, field, value) {
|
2016-12-20 08:48:18 +01:00
|
|
|
if (!checkIsHttpToken(field)) {
|
2015-08-24 20:58:48 +02:00
|
|
|
throw new TypeError(
|
|
|
|
'Header name must be a valid HTTP Token ["' + field + '"]');
|
|
|
|
}
|
2016-12-20 08:48:18 +01:00
|
|
|
if (checkInvalidHeaderChar(value)) {
|
2016-10-19 23:28:16 +02:00
|
|
|
debug('Header "%s" contains invalid characters', field);
|
2016-02-04 02:32:23 +01:00
|
|
|
throw new TypeError('The header content contains invalid characters');
|
|
|
|
}
|
|
|
|
state.messageHeader += field + ': ' + escapeHeaderValue(value) + CRLF;
|
2013-04-12 00:37:14 +02:00
|
|
|
|
|
|
|
if (connectionExpression.test(field)) {
|
|
|
|
state.sentConnectionHeader = true;
|
2016-08-23 19:49:21 +02:00
|
|
|
if (connCloseExpression.test(value)) {
|
2013-04-12 00:37:14 +02:00
|
|
|
self._last = true;
|
|
|
|
} else {
|
|
|
|
self.shouldKeepAlive = true;
|
|
|
|
}
|
2016-08-23 19:49:21 +02:00
|
|
|
if (connUpgradeExpression.test(value))
|
|
|
|
state.sentConnectionUpgrade = true;
|
2013-04-12 00:37:14 +02:00
|
|
|
} else if (transferEncodingExpression.test(field)) {
|
|
|
|
state.sentTransferEncodingHeader = true;
|
2016-08-23 19:49:21 +02:00
|
|
|
if (trfrEncChunkExpression.test(value)) self.chunkedEncoding = true;
|
2013-04-12 00:37:14 +02:00
|
|
|
|
|
|
|
} else if (contentLengthExpression.test(field)) {
|
|
|
|
state.sentContentLengthHeader = true;
|
|
|
|
} else if (dateExpression.test(field)) {
|
|
|
|
state.sentDateHeader = true;
|
|
|
|
} else if (expectExpression.test(field)) {
|
|
|
|
state.sentExpect = true;
|
2015-03-03 21:01:26 +01:00
|
|
|
} else if (trailerExpression.test(field)) {
|
|
|
|
state.sentTrailer = true;
|
2016-08-23 19:49:21 +02:00
|
|
|
} else if (upgradeExpression.test(field)) {
|
|
|
|
state.sentUpgrade = true;
|
2013-04-12 00:37:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-12 15:48:40 +02:00
|
|
|
OutgoingMessage.prototype.setHeader = function setHeader(name, value) {
|
2016-12-20 08:48:18 +01:00
|
|
|
if (!checkIsHttpToken(name))
|
2015-08-24 20:58:48 +02:00
|
|
|
throw new TypeError(
|
|
|
|
'Header name must be a valid HTTP Token ["' + name + '"]');
|
2014-09-29 21:32:42 +02:00
|
|
|
if (value === undefined)
|
2015-10-14 23:10:25 +02:00
|
|
|
throw new Error('"value" required in setHeader("' + name + '", value)');
|
2014-09-29 21:32:42 +02:00
|
|
|
if (this._header)
|
2016-02-04 02:32:23 +01:00
|
|
|
throw new Error('Can\'t set headers after they are sent.');
|
2016-12-20 08:48:18 +01:00
|
|
|
if (checkInvalidHeaderChar(value)) {
|
2016-10-19 23:28:16 +02:00
|
|
|
debug('Header "%s" contains invalid characters', name);
|
2016-02-04 02:32:23 +01:00
|
|
|
throw new TypeError('The header content contains invalid characters');
|
|
|
|
}
|
2014-09-29 21:32:42 +02:00
|
|
|
if (this._headers === null)
|
|
|
|
this._headers = {};
|
2013-04-12 00:37:14 +02:00
|
|
|
|
|
|
|
var key = name.toLowerCase();
|
|
|
|
this._headers[key] = value;
|
|
|
|
this._headerNames[key] = name;
|
2012-09-10 04:04:37 +02:00
|
|
|
|
2014-09-29 21:32:42 +02:00
|
|
|
if (automaticHeaders[key])
|
2012-09-10 04:04:37 +02:00
|
|
|
this._removedHeader[key] = false;
|
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') {
|
|
|
|
throw new TypeError('"name" argument must be a string');
|
2013-04-12 00:37:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!this._headers) return;
|
|
|
|
|
2016-12-18 14:56:52 +01:00
|
|
|
return this._headers[name.toLowerCase()];
|
2013-04-12 00:37:14 +02: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') {
|
|
|
|
throw new TypeError('"name" argument must be a string');
|
2013-04-12 00:37:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this._header) {
|
2015-10-14 23:10:25 +02:00
|
|
|
throw new Error('Can\'t remove headers after they are sent');
|
2013-04-12 00:37:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var key = name.toLowerCase();
|
2012-09-10 04:04:37 +02:00
|
|
|
|
|
|
|
if (key === 'date')
|
|
|
|
this.sendDate = false;
|
|
|
|
else if (automaticHeaders[key])
|
|
|
|
this._removedHeader[key] = true;
|
|
|
|
|
|
|
|
if (this._headers) {
|
|
|
|
delete this._headers[key];
|
|
|
|
delete this._headerNames[key];
|
|
|
|
}
|
2013-04-12 00:37:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-10-12 15:48:40 +02:00
|
|
|
OutgoingMessage.prototype._renderHeaders = function _renderHeaders() {
|
2013-04-12 00:37:14 +02:00
|
|
|
if (this._header) {
|
2015-10-14 23:10:25 +02:00
|
|
|
throw new Error('Can\'t render headers after they are sent to the client');
|
2013-04-12 00:37:14 +02:00
|
|
|
}
|
|
|
|
|
2015-01-26 00:26:40 +01:00
|
|
|
var headersMap = this._headers;
|
|
|
|
if (!headersMap) return {};
|
2013-04-12 00:37:14 +02:00
|
|
|
|
|
|
|
var headers = {};
|
2015-01-26 00:26:40 +01:00
|
|
|
var keys = Object.keys(headersMap);
|
|
|
|
var headerNames = this._headerNames;
|
2014-09-29 21:32:42 +02:00
|
|
|
|
2013-04-12 00:37:14 +02:00
|
|
|
for (var i = 0, l = keys.length; i < l; i++) {
|
|
|
|
var key = keys[i];
|
2015-01-26 00:26:40 +01:00
|
|
|
headers[headerNames[key]] = headersMap[key];
|
2013-04-12 00:37:14 +02:00
|
|
|
}
|
|
|
|
return headers;
|
|
|
|
};
|
|
|
|
|
2016-10-12 15:48:40 +02:00
|
|
|
OutgoingMessage.prototype._implicitHeader = function _implicitHeader() {
|
2016-08-02 18:28:38 +02:00
|
|
|
throw new Error('_implicitHeader() method is not implemented');
|
|
|
|
};
|
2013-04-12 00:37:14 +02:00
|
|
|
|
|
|
|
Object.defineProperty(OutgoingMessage.prototype, 'headersSent', {
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
get: function() { return !!this._header; }
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2016-10-12 15:48:40 +02:00
|
|
|
OutgoingMessage.prototype.write = function write(chunk, encoding, callback) {
|
2014-09-23 02:21:40 +02:00
|
|
|
if (this.finished) {
|
|
|
|
var err = new Error('write after end');
|
2015-03-05 22:07:27 +01:00
|
|
|
process.nextTick(writeAfterEndNT, this, err, callback);
|
2014-09-23 02:21:40 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-04-12 00:37:14 +02:00
|
|
|
if (!this._header) {
|
|
|
|
this._implicitHeader();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this._hasBody) {
|
|
|
|
debug('This type of response MUST NOT have a body. ' +
|
|
|
|
'Ignoring write() calls.');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-01-29 02:05:53 +01:00
|
|
|
if (typeof chunk !== 'string' && !(chunk instanceof Buffer)) {
|
2015-10-14 23:10:25 +02:00
|
|
|
throw new TypeError('First argument must be a string or Buffer');
|
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
|
|
|
|
|
|
|
var len, ret;
|
|
|
|
if (this.chunkedEncoding) {
|
2015-01-29 02:05:53 +01:00
|
|
|
if (typeof chunk === 'string' &&
|
2013-04-12 00:37:14 +02:00
|
|
|
encoding !== 'hex' &&
|
|
|
|
encoding !== 'base64' &&
|
2016-06-02 18:55:36 +02:00
|
|
|
encoding !== 'latin1') {
|
2013-04-12 00:37:14 +02:00
|
|
|
len = Buffer.byteLength(chunk, encoding);
|
|
|
|
chunk = len.toString(16) + CRLF + chunk + CRLF;
|
2013-08-15 17:51:37 +02:00
|
|
|
ret = this._send(chunk, encoding, callback);
|
2013-04-12 00:37:14 +02:00
|
|
|
} else {
|
|
|
|
// buffer, or a non-toString-friendly encoding
|
2015-01-29 02:05:53 +01:00
|
|
|
if (typeof chunk === 'string')
|
2013-08-15 07:14:49 +02:00
|
|
|
len = Buffer.byteLength(chunk, encoding);
|
|
|
|
else
|
|
|
|
len = chunk.length;
|
2013-04-08 14:40:21 +02:00
|
|
|
|
2013-12-07 02:17:02 +01:00
|
|
|
if (this.connection && !this.connection.corked) {
|
2013-04-08 14:40:21 +02:00
|
|
|
this.connection.cork();
|
2015-03-05 22:07:27 +01:00
|
|
|
process.nextTick(connectionCorkNT, this.connection);
|
2013-12-07 02:17:02 +01:00
|
|
|
}
|
2016-06-02 18:55:36 +02:00
|
|
|
this._send(len.toString(16), 'latin1', null);
|
2013-08-15 17:51:37 +02:00
|
|
|
this._send(crlf_buf, null, null);
|
|
|
|
this._send(chunk, encoding, null);
|
|
|
|
ret = this._send(crlf_buf, null, callback);
|
2013-04-12 00:37:14 +02:00
|
|
|
}
|
|
|
|
} else {
|
2013-08-15 17:51:37 +02:00
|
|
|
ret = this._send(chunk, encoding, callback);
|
2013-04-12 00:37:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
debug('write ret = ' + ret);
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-03-05 22:07:27 +01:00
|
|
|
function writeAfterEndNT(self, err, callback) {
|
|
|
|
self.emit('error', err);
|
|
|
|
if (callback) callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function connectionCorkNT(conn) {
|
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];
|
|
|
|
}
|
2016-12-20 08:48:18 +01:00
|
|
|
if (!checkIsHttpToken(field)) {
|
2015-08-24 20:58:48 +02:00
|
|
|
throw new TypeError(
|
|
|
|
'Trailer name must be a valid HTTP Token ["' + field + '"]');
|
|
|
|
}
|
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);
|
2016-04-20 19:44:01 +02:00
|
|
|
throw new TypeError('The trailer content contains invalid characters');
|
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-01-26 00:00:06 +01:00
|
|
|
const crlf_buf = Buffer.from('\r\n');
|
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
|
|
|
|
2016-10-12 15:48:40 +02:00
|
|
|
OutgoingMessage.prototype.end = function end(data, encoding, callback) {
|
2015-01-29 02:05:53 +01:00
|
|
|
if (typeof data === 'function') {
|
2013-08-15 17:51:37 +02:00
|
|
|
callback = data;
|
|
|
|
data = 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;
|
|
|
|
}
|
|
|
|
|
2015-01-29 02:05:53 +01:00
|
|
|
if (data && typeof data !== 'string' && !(data instanceof Buffer)) {
|
2015-10-14 23:10:25 +02:00
|
|
|
throw new TypeError('First argument must be a string or Buffer');
|
2013-05-08 23:28:56 +02:00
|
|
|
}
|
|
|
|
|
2013-04-12 00:37:14 +02:00
|
|
|
if (this.finished) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-08-15 20:08:19 +02:00
|
|
|
|
2013-04-12 00:37:14 +02:00
|
|
|
if (!this._header) {
|
2015-03-03 21:01:26 +01:00
|
|
|
if (data) {
|
|
|
|
if (typeof data === 'string')
|
|
|
|
this._contentLength = Buffer.byteLength(data, encoding);
|
|
|
|
else
|
|
|
|
this._contentLength = data.length;
|
|
|
|
} else {
|
|
|
|
this._contentLength = 0;
|
|
|
|
}
|
2013-04-12 00:37:14 +02:00
|
|
|
this._implicitHeader();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data && !this._hasBody) {
|
|
|
|
debug('This type of response MUST NOT have a body. ' +
|
|
|
|
'Ignoring data passed to end().');
|
2013-08-15 17:51:37 +02:00
|
|
|
data = null;
|
2013-04-12 00:37:14 +02:00
|
|
|
}
|
|
|
|
|
2013-05-08 23:28:56 +02:00
|
|
|
if (this.connection && data)
|
|
|
|
this.connection.cork();
|
2013-04-12 00:37:14 +02:00
|
|
|
|
2013-05-08 23:28:56 +02:00
|
|
|
if (data) {
|
2013-04-12 00:37:14 +02:00
|
|
|
// Normal body write.
|
2015-12-17 03:31:38 +01:00
|
|
|
this.write(data, encoding);
|
2013-04-12 00:37:14 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2016-12-18 14:56:52 +01:00
|
|
|
var ret;
|
2014-09-17 02:48:09 +02:00
|
|
|
if (this._hasBody && this.chunkedEncoding) {
|
2016-06-02 18:55:36 +02:00
|
|
|
ret = this._send('0\r\n' + this._trailer + '\r\n', 'latin1', finish);
|
2013-05-08 23:28:56 +02:00
|
|
|
} else {
|
|
|
|
// Force a flush, HACK.
|
2016-06-02 18:55:36 +02:00
|
|
|
ret = this._send('', 'latin1', finish);
|
2013-04-12 00:37:14 +02:00
|
|
|
}
|
|
|
|
|
2013-05-08 23:28:56 +02:00
|
|
|
if (this.connection && data)
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
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 = [];
|
|
|
|
if (typeof this._onPendingData === 'function')
|
|
|
|
this._onPendingData(-this.outputSize);
|
|
|
|
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();
|
2015-06-13 18:44:39 +02:00
|
|
|
}, 'OutgoingMessage.flush is deprecated. Use flushHeaders instead.');
|