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');
|
2016-10-17 20:38:52 +02:00
|
|
|
const internalHttp = 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;
|
2017-10-07 16:50:42 +02:00
|
|
|
const { outHeadersKey } = require('internal/http');
|
|
|
|
const { async_id_symbol } = process.binding('async_wrap');
|
|
|
|
const { nextTick } = require('internal/process/next_tick');
|
2017-05-27 05:00:09 +02:00
|
|
|
const errors = require('internal/errors');
|
2015-01-21 17:36:59 +01:00
|
|
|
|
2017-10-07 16:50:42 +02:00
|
|
|
const { CRLF, debug } = common;
|
|
|
|
const { utcDate } = internalHttp;
|
2015-01-21 17:36:59 +01:00
|
|
|
|
2018-01-23 21:23:33 +01:00
|
|
|
const kIsCorked = Symbol('isCorked');
|
|
|
|
|
2017-05-03 08:12:14 +02:00
|
|
|
var RE_FIELDS =
|
|
|
|
/^(?:Connection|Transfer-Encoding|Content-Length|Date|Expect|Trailer|Upgrade)$/i;
|
2016-12-31 22:18:43 +01:00
|
|
|
var RE_CONN_VALUES = /(?:^|\W)close|upgrade(?:$|\W)/ig;
|
|
|
|
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
|
|
|
|
// against the word "cookie." This method (at least as of V8 5.4) is faster than
|
|
|
|
// the equivalent case-insensitive regexp, even if isCookieField does not get
|
|
|
|
// inlined.
|
|
|
|
function isCookieField(s) {
|
|
|
|
if (s.length !== 6) return false;
|
|
|
|
var ch = s.charCodeAt(0);
|
|
|
|
if (ch !== 99 && ch !== 67) return false;
|
|
|
|
ch = s.charCodeAt(1);
|
|
|
|
if (ch !== 111 && ch !== 79) return false;
|
|
|
|
ch = s.charCodeAt(2);
|
|
|
|
if (ch !== 111 && ch !== 79) return false;
|
|
|
|
ch = s.charCodeAt(3);
|
|
|
|
if (ch !== 107 && ch !== 75) return false;
|
|
|
|
ch = s.charCodeAt(4);
|
|
|
|
if (ch !== 105 && ch !== 73) return false;
|
|
|
|
ch = s.charCodeAt(5);
|
|
|
|
if (ch !== 101 && ch !== 69) return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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;
|
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;
|
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') {
|
|
|
|
const headers = this[outHeadersKey] = {};
|
|
|
|
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];
|
|
|
|
if (headers) {
|
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;
|
|
|
|
} else {
|
|
|
|
return headers;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
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) {
|
2017-08-10 12:58:22 +02:00
|
|
|
throw new errors.Error('ERR_HTTP_HEADERS_SENT', 'render');
|
2017-03-06 03:15:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var headersMap = this[outHeadersKey];
|
|
|
|
if (!headersMap) return {};
|
|
|
|
|
|
|
|
var headers = {};
|
|
|
|
var keys = Object.keys(headersMap);
|
|
|
|
|
|
|
|
for (var i = 0, l = keys.length; i < l; i++) {
|
|
|
|
var key = keys[i];
|
|
|
|
headers[headersMap[key][0]] = headersMap[key][1];
|
|
|
|
}
|
|
|
|
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
|
|
|
let socketAsyncId = this.socket[async_id_symbol];
|
|
|
|
// 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
|
|
|
|
if (socketAsyncId === undefined) socketAsyncId = null;
|
|
|
|
|
|
|
|
nextTick(socketAsyncId, 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'
|
|
|
|
var state = {
|
2016-12-31 22:18:43 +01:00
|
|
|
connection: false,
|
|
|
|
connUpgrade: false,
|
|
|
|
contLen: false,
|
|
|
|
te: false,
|
|
|
|
date: false,
|
|
|
|
expect: false,
|
|
|
|
trailer: false,
|
|
|
|
upgrade: false,
|
|
|
|
header: firstLine
|
2013-04-12 00:37:14 +02:00
|
|
|
};
|
|
|
|
|
2016-12-20 08:53:47 +01:00
|
|
|
var field;
|
2016-12-31 22:18:43 +01:00
|
|
|
var key;
|
2016-12-20 08:53:47 +01:00
|
|
|
var value;
|
2016-12-31 22:18:43 +01:00
|
|
|
var i;
|
|
|
|
var j;
|
2017-03-06 03:13:09 +01:00
|
|
|
if (headers === this[outHeadersKey]) {
|
2016-12-31 22:18:43 +01:00
|
|
|
for (key in headers) {
|
|
|
|
var entry = headers[key];
|
|
|
|
field = entry[0];
|
|
|
|
value = entry[1];
|
|
|
|
|
|
|
|
if (value instanceof Array) {
|
2017-02-11 02:32:32 +01:00
|
|
|
if (value.length < 2 || !isCookieField(field)) {
|
|
|
|
for (j = 0; j < value.length; j++)
|
|
|
|
storeHeader(this, state, field, value[j], false);
|
|
|
|
continue;
|
2016-12-31 22:18:43 +01:00
|
|
|
}
|
2017-02-11 02:32:32 +01:00
|
|
|
value = value.join('; ');
|
2016-12-31 22:18:43 +01:00
|
|
|
}
|
2017-02-11 02:32:32 +01:00
|
|
|
storeHeader(this, state, field, value, false);
|
2016-12-31 22:18:43 +01:00
|
|
|
}
|
|
|
|
} else if (headers instanceof Array) {
|
|
|
|
for (i = 0; i < headers.length; i++) {
|
2016-12-20 08:53:47 +01:00
|
|
|
field = headers[i][0];
|
|
|
|
value = headers[i][1];
|
|
|
|
|
|
|
|
if (value instanceof Array) {
|
|
|
|
for (j = 0; j < value.length; j++) {
|
2016-12-31 22:18:43 +01:00
|
|
|
storeHeader(this, state, field, value[j], true);
|
2016-12-20 08:53:47 +01:00
|
|
|
}
|
2013-04-12 00:37:14 +02:00
|
|
|
} else {
|
2016-12-31 22:18:43 +01:00
|
|
|
storeHeader(this, state, field, value, true);
|
2013-04-12 00:37:14 +02:00
|
|
|
}
|
2016-12-20 08:53:47 +01:00
|
|
|
}
|
|
|
|
} else if (headers) {
|
|
|
|
var keys = Object.keys(headers);
|
2016-12-31 22:18:43 +01:00
|
|
|
for (i = 0; i < keys.length; i++) {
|
2016-12-20 08:53:47 +01:00
|
|
|
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) {
|
2017-02-11 02:32:32 +01:00
|
|
|
if (value.length < 2 || !isCookieField(field)) {
|
|
|
|
for (j = 0; j < value.length; j++)
|
|
|
|
storeHeader(this, state, field, value[j], true);
|
|
|
|
continue;
|
2013-04-12 00:37:14 +02:00
|
|
|
}
|
2017-02-11 02:32:32 +01:00
|
|
|
value = value.join('; ');
|
2013-04-12 00:37:14 +02:00
|
|
|
}
|
2017-02-11 02:32:32 +01:00
|
|
|
storeHeader(this, state, field, value, true);
|
2013-04-12 00:37:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-23 19:49:21 +02:00
|
|
|
// Are we upgrading the connection?
|
2016-12-31 22:18:43 +01:00
|
|
|
if (state.connUpgrade && state.upgrade)
|
2016-08-23 19:49:21 +02:00
|
|
|
this.upgrading = true;
|
|
|
|
|
2013-04-12 00:37:14 +02:00
|
|
|
// Date header
|
2016-12-31 22:18:43 +01:00
|
|
|
if (this.sendDate && !state.date) {
|
|
|
|
state.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.
|
|
|
|
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
|
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) {
|
2013-04-12 00:37:14 +02:00
|
|
|
var 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) {
|
2016-12-31 22:18:43 +01:00
|
|
|
state.header += 'Connection: keep-alive\r\n';
|
2013-04-12 00:37:14 +02:00
|
|
|
} else {
|
|
|
|
this._last = true;
|
2016-12-31 22:18:43 +01:00
|
|
|
state.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') {
|
|
|
|
state.header += 'Content-Length: ' + this._contentLength + CRLF;
|
|
|
|
} else if (!this._removedTE) {
|
|
|
|
state.header += 'Transfer-Encoding: chunked\r\n';
|
|
|
|
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) {
|
|
|
|
throw new errors.Error('ERR_HTTP_TRAILER_INVALID');
|
|
|
|
}
|
|
|
|
|
2016-12-31 22:18:43 +01:00
|
|
|
this._header = state.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
|
|
|
|
2017-01-06 09:12:59 +01:00
|
|
|
function storeHeader(self, state, key, value, validate) {
|
2016-12-31 22:18:43 +01:00
|
|
|
if (validate) {
|
2017-11-03 13:04:12 +01:00
|
|
|
validateHeader(key, value);
|
2016-02-04 02:32:23 +01:00
|
|
|
}
|
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 matchConnValue(self, state, value) {
|
|
|
|
var sawClose = false;
|
|
|
|
var m = RE_CONN_VALUES.exec(value);
|
|
|
|
while (m) {
|
|
|
|
if (m[0].length === 5)
|
|
|
|
sawClose = true;
|
|
|
|
else
|
|
|
|
state.connUpgrade = true;
|
|
|
|
m = RE_CONN_VALUES.exec(value);
|
2013-04-12 00:37:14 +02:00
|
|
|
}
|
2016-12-31 22:18:43 +01:00
|
|
|
if (sawClose)
|
|
|
|
self._last = true;
|
|
|
|
else
|
|
|
|
self.shouldKeepAlive = true;
|
2013-04-12 00:37:14 +02:00
|
|
|
}
|
|
|
|
|
2016-12-31 22:18:43 +01:00
|
|
|
function matchHeader(self, state, field, value) {
|
|
|
|
var m = RE_FIELDS.exec(field);
|
|
|
|
if (!m)
|
|
|
|
return;
|
|
|
|
var len = m[0].length;
|
|
|
|
if (len === 10) {
|
|
|
|
state.connection = true;
|
|
|
|
matchConnValue(self, state, value);
|
|
|
|
} else if (len === 17) {
|
|
|
|
state.te = true;
|
|
|
|
if (RE_TE_CHUNKED.test(value)) self.chunkedEncoding = true;
|
|
|
|
} else if (len === 14) {
|
|
|
|
state.contLen = true;
|
|
|
|
} else if (len === 4) {
|
|
|
|
state.date = true;
|
|
|
|
} else if (len === 6) {
|
|
|
|
state.expect = true;
|
|
|
|
} else if (len === 7) {
|
|
|
|
var ch = m[0].charCodeAt(0);
|
|
|
|
if (ch === 85 || ch === 117)
|
|
|
|
state.upgrade = true;
|
|
|
|
else
|
|
|
|
state.trailer = true;
|
|
|
|
}
|
|
|
|
}
|
2013-04-12 00:37:14 +02:00
|
|
|
|
2017-11-03 13:04:12 +01:00
|
|
|
function validateHeader(name, value) {
|
|
|
|
let err;
|
|
|
|
if (typeof name !== 'string' || !name || !checkIsHttpToken(name)) {
|
|
|
|
err = new errors.TypeError('ERR_INVALID_HTTP_TOKEN', 'Header name', name);
|
|
|
|
} else if (value === undefined) {
|
|
|
|
err = new errors.TypeError('ERR_HTTP_INVALID_HEADER_VALUE', value, name);
|
|
|
|
} else if (checkInvalidHeaderChar(value)) {
|
2016-10-19 23:28:16 +02:00
|
|
|
debug('Header "%s" contains invalid characters', name);
|
2017-11-03 13:04:12 +01:00
|
|
|
err = new errors.TypeError('ERR_INVALID_CHAR', 'header content', name);
|
|
|
|
}
|
|
|
|
if (err !== undefined) {
|
|
|
|
Error.captureStackTrace(err, validateHeader);
|
|
|
|
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) {
|
|
|
|
throw new errors.Error('ERR_HTTP_HEADERS_SENT', 'set');
|
|
|
|
}
|
|
|
|
validateHeader(name, value);
|
2016-12-31 22:18:43 +01:00
|
|
|
|
2017-03-06 03:13:09 +01:00
|
|
|
if (!this[outHeadersKey])
|
|
|
|
this[outHeadersKey] = {};
|
2013-04-12 00:37:14 +02:00
|
|
|
|
2016-12-31 22:18:43 +01:00
|
|
|
const key = name.toLowerCase();
|
2017-03-06 03:13:09 +01:00
|
|
|
this[outHeadersKey][key] = [name, value];
|
2012-09-10 04:04:37 +02:00
|
|
|
|
2018-01-24 01:04:17 +01:00
|
|
|
switch (key) {
|
|
|
|
case 'connection':
|
|
|
|
this._removedConnection = false;
|
2016-12-31 22:20:38 +01:00
|
|
|
break;
|
2018-01-24 01:04:17 +01:00
|
|
|
case 'content-length':
|
|
|
|
this._removedContLen = false;
|
2016-12-31 22:20:38 +01:00
|
|
|
break;
|
2018-01-24 01:04:17 +01:00
|
|
|
case 'transfer-encoding':
|
|
|
|
this._removedTE = false;
|
2016-12-31 22:20:38 +01:00
|
|
|
break;
|
|
|
|
}
|
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') {
|
2017-08-10 12:58:22 +02:00
|
|
|
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'name', 'string');
|
2013-04-12 00:37:14 +02:00
|
|
|
}
|
|
|
|
|
2017-03-06 03:13:09 +01:00
|
|
|
if (!this[outHeadersKey]) return;
|
2013-04-12 00:37:14 +02:00
|
|
|
|
2017-03-06 03:13:09 +01:00
|
|
|
var entry = this[outHeadersKey][name.toLowerCase()];
|
2016-12-31 22:18:43 +01:00
|
|
|
if (!entry)
|
|
|
|
return;
|
|
|
|
return 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() {
|
2017-03-06 03:13:09 +01:00
|
|
|
return (this[outHeadersKey] ? 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') {
|
2017-08-10 12:58:22 +02:00
|
|
|
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'name', 'string');
|
2017-01-14 15:39:58 +01:00
|
|
|
}
|
|
|
|
|
2017-03-06 03:13:09 +01:00
|
|
|
return !!(this[outHeadersKey] && 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') {
|
2017-08-10 12:58:22 +02:00
|
|
|
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'name', 'string');
|
2013-04-12 00:37:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this._header) {
|
2017-08-10 12:58:22 +02:00
|
|
|
throw new errors.Error('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
|
|
|
|
2017-03-06 03:13:09 +01:00
|
|
|
if (this[outHeadersKey]) {
|
|
|
|
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() {
|
2017-08-10 12:58:22 +02:00
|
|
|
throw new errors.Error('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) {
|
2017-12-23 10:53:17 +01:00
|
|
|
const err = new errors.Error('ERR_STREAM_WRITE_AFTER_END');
|
2017-07-20 18:02:54 +02:00
|
|
|
nextTick(msg.socket && msg.socket[async_id_symbol],
|
2017-05-13 16:07:44 +02:00
|
|
|
writeAfterEndNT.bind(msg),
|
2017-03-10 14:17:42 +01:00
|
|
|
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)) {
|
2017-08-10 12:58:22 +02:00
|
|
|
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'first argument',
|
2017-10-28 11:39:55 +02:00
|
|
|
['string', '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
|
|
|
|
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
|
|
|
|
|
|
|
|
2017-02-28 01:53:13 +01:00
|
|
|
function writeAfterEndNT(err, callback) {
|
|
|
|
this.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)) {
|
2017-08-10 12:58:22 +02:00
|
|
|
throw new errors.TypeError('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);
|
2017-08-10 12:58:22 +02:00
|
|
|
throw new errors.TypeError('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) {
|
|
|
|
return false;
|
|
|
|
}
|
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)) {
|
2017-08-10 12:58:22 +02:00
|
|
|
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'first argument',
|
2017-10-28 11:39:55 +02:00
|
|
|
['string', 'Buffer']);
|
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
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
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 = [];
|
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.
|
2017-12-23 10:53:17 +01:00
|
|
|
this.emit('error', new errors.Error('ERR_STREAM_CANNOT_PIPE'));
|
2017-07-19 05:30:07 +02:00
|
|
|
};
|
2017-02-28 01:09:32 +01:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
OutgoingMessage
|
|
|
|
};
|