diff --git a/lib/_http_common.js b/lib/_http_common.js index 642cdea41f68b1..796deeff055767 100644 --- a/lib/_http_common.js +++ b/lib/_http_common.js @@ -268,7 +268,7 @@ module.exports = { _checkIsHttpToken: checkIsHttpToken, chunkExpression: /(?:^|\W)chunked(?:$|\W)/i, continueExpression: /(?:^|\W)100-continue(?:$|\W)/i, - CRLF: '\r\n', + CRLF: '\r\n', // TODO: Deprecate this. freeParser, methods, parsers, diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js index c6f68d4329c7da..25fe8fb1ede73f 100644 --- a/lib/_http_outgoing.js +++ b/lib/_http_outgoing.js @@ -45,9 +45,11 @@ const Stream = require('stream'); const internalUtil = require('internal/util'); const { kOutHeaders, utcDate, kNeedDrain } = require('internal/http'); const { Buffer } = require('buffer'); -const common = require('_http_common'); -const checkIsHttpToken = common._checkIsHttpToken; -const checkInvalidHeaderChar = common._checkInvalidHeaderChar; +const { + _checkIsHttpToken: checkIsHttpToken, + _checkInvalidHeaderChar: checkInvalidHeaderChar, + chunkExpression: RE_TE_CHUNKED, +} = require('_http_common'); const { defaultTriggerAsyncIdScope, symbols: { async_id_symbol } @@ -78,14 +80,12 @@ let debug = require('internal/util/debuglog').debuglog('http', (fn) => { }); const HIGH_WATER_MARK = getDefaultHighWaterMark(); -const { CRLF } = common; const kCorked = Symbol('corked'); const nop = () => {}; const RE_CONN_CLOSE = /(?:^|\W)close(?:$|\W)/i; -const RE_TE_CHUNKED = common.chunkExpression; // isCookieField performs a case-insensitive comparison of a provided string // against the word "cookie." As of V8 6.6 this is faster than handrolling or @@ -417,7 +417,7 @@ function _storeHeader(firstLine, headers) { // Date header if (this.sendDate && !state.date) { - header += 'Date: ' + utcDate() + CRLF; + header += 'Date: ' + utcDate() + '\r\n'; } // Force the connection to close when the response is a 204 No Content or @@ -447,14 +447,14 @@ function _storeHeader(firstLine, headers) { const shouldSendKeepAlive = this.shouldKeepAlive && (state.contLen || this.useChunkedEncodingByDefault || this.agent); if (shouldSendKeepAlive) { - header += 'Connection: keep-alive' + CRLF; + header += 'Connection: keep-alive\r\n'; if (this._keepAliveTimeout && this._defaultKeepAlive) { const timeoutSeconds = MathFloor(this._keepAliveTimeout / 1000); - header += `Keep-Alive: timeout=${timeoutSeconds}${CRLF}`; + header += `Keep-Alive: timeout=${timeoutSeconds}\r\n`; } } else { this._last = true; - header += 'Connection: close' + CRLF; + header += 'Connection: close\r\n'; } } @@ -467,9 +467,9 @@ function _storeHeader(firstLine, headers) { } else if (!state.trailer && !this._removedContLen && typeof this._contentLength === 'number') { - header += 'Content-Length: ' + this._contentLength + CRLF; + header += 'Content-Length: ' + this._contentLength + '\r\n'; } else if (!this._removedTE) { - header += 'Transfer-Encoding: chunked' + CRLF; + header += 'Transfer-Encoding: chunked\r\n'; this.chunkedEncoding = true; } else { // We should only be able to get here if both Content-Length and @@ -487,7 +487,7 @@ function _storeHeader(firstLine, headers) { throw new ERR_HTTP_TRAILER_INVALID(); } - this._header = header + CRLF; + this._header = header + '\r\n'; this._headerSent = false; // Wait until the first body chunk, or close(), is sent to flush, @@ -514,7 +514,7 @@ function processHeader(self, state, key, value, validate) { function storeHeader(self, state, key, value, validate) { if (validate) validateHeaderValue(key, value); - state.header += key + ': ' + value + CRLF; + state.header += key + ': ' + value + '\r\n'; matchHeader(self, state, key, value); } @@ -694,7 +694,7 @@ ObjectDefineProperty(OutgoingMessage.prototype, 'writableNeedDrain', { } }); -const crlf_buf = Buffer.from(CRLF); +const crlf_buf = Buffer.from('\r\n'); OutgoingMessage.prototype.write = function write(chunk, encoding, callback) { if (typeof encoding === 'function') { callback = encoding; @@ -818,7 +818,7 @@ OutgoingMessage.prototype.addTrailers = function addTrailers(headers) { debug('Trailer "%s" contains invalid characters', field); throw new ERR_INVALID_CHAR('trailer content', field); } - this._trailer += field + ': ' + value + CRLF; + this._trailer += field + ': ' + value + '\r\n'; } }; diff --git a/lib/_http_server.js b/lib/_http_server.js index 64cd44c066cf8a..bafd615a0fdc60 100644 --- a/lib/_http_server.js +++ b/lib/_http_server.js @@ -37,7 +37,6 @@ const assert = require('internal/assert'); const { parsers, freeParser, - CRLF, continueExpression, chunkExpression, kIncomingMessage, @@ -252,12 +251,12 @@ ServerResponse.prototype.detachSocket = function detachSocket(socket) { }; ServerResponse.prototype.writeContinue = function writeContinue(cb) { - this._writeRaw(`HTTP/1.1 100 Continue${CRLF}${CRLF}`, 'ascii', cb); + this._writeRaw('HTTP/1.1 100 Continue\r\n\r\n', 'ascii', cb); this._sent100 = true; }; ServerResponse.prototype.writeProcessing = function writeProcessing(cb) { - this._writeRaw(`HTTP/1.1 102 Processing${CRLF}${CRLF}`, 'ascii', cb); + this._writeRaw('HTTP/1.1 102 Processing\r\n\r\n', 'ascii', cb); }; ServerResponse.prototype._implicitHeader = function _implicitHeader() { @@ -320,7 +319,7 @@ function writeHead(statusCode, reason, obj) { if (checkInvalidHeaderChar(this.statusMessage)) throw new ERR_INVALID_CHAR('statusMessage'); - const statusLine = `HTTP/1.1 ${statusCode} ${this.statusMessage}${CRLF}`; + const statusLine = `HTTP/1.1 ${statusCode} ${this.statusMessage}\r\n`; if (statusCode === 204 || statusCode === 304 || (statusCode >= 100 && statusCode <= 199)) { @@ -646,16 +645,16 @@ function onParserTimeout(server, socket) { const noop = () => {}; const badRequestResponse = Buffer.from( - `HTTP/1.1 400 ${STATUS_CODES[400]}${CRLF}` + - `Connection: close${CRLF}${CRLF}`, 'ascii' + `HTTP/1.1 400 ${STATUS_CODES[400]}\r\n` + + 'Connection: close\r\n\r\n', 'ascii' ); const requestTimeoutResponse = Buffer.from( - `HTTP/1.1 408 ${STATUS_CODES[408]}${CRLF}` + - `Connection: close${CRLF}${CRLF}`, 'ascii' + `HTTP/1.1 408 ${STATUS_CODES[408]}\r\n` + + 'Connection: close\r\n\r\n', 'ascii' ); const requestHeaderFieldsTooLargeResponse = Buffer.from( - `HTTP/1.1 431 ${STATUS_CODES[431]}${CRLF}` + - `Connection: close${CRLF}${CRLF}`, 'ascii' + `HTTP/1.1 431 ${STATUS_CODES[431]}\r\n` + + 'Connection: close\r\n\r\n', 'ascii' ); function socketOnError(e) { // Ignore further errors